3 * Demonstrates migrating a window between different displays and
4 * screens. A display is a mouse and keyboard with some number of
5 * associated monitors. A screen is a set of monitors grouped
6 * into a single physical work area. The neat thing about having
7 * multiple displays is that they can be on a completely separate
8 * computers, as long as there is a network connection to the
9 * computer where the application is running.
11 * Only some of the windowing systems where GTK+ runs have the
12 * concept of multiple displays and screens. (The X Window System
13 * is the main example.) Other windowing systems can only
14 * handle one keyboard and mouse, and combine all monitors into
17 * This is a moderately complex example, and demonstrates:
19 * - Tracking the currently open displays and screens
21 * - Changing the screen for a window
23 * - Letting the user choose a window by clicking on it
25 * - Using GtkListStore and GtkTreeView
31 #include "demo-common.h"
33 /* The ChangeDisplayInfo structure corresponds to a toplevel window and
34 * holds pointers to widgets inside the toplevel window along with other
35 * information about the contents of the window.
36 * This is a common organizational structure in real applications.
38 typedef struct _ChangeDisplayInfo ChangeDisplayInfo;
40 struct _ChangeDisplayInfo
43 GtkSizeGroup *size_group;
45 GtkTreeModel *display_model;
46 GtkTreeModel *screen_model;
47 GtkTreeSelection *screen_selection;
49 GdkDisplay *current_display;
50 GdkScreen *current_screen;
53 /* These enumerations provide symbolic names for the columns
54 * in the two GtkListStore models.
59 DISPLAY_COLUMN_DISPLAY,
70 /* Finds the toplevel window under the mouse pointer, if any.
73 find_toplevel_at_pointer (GdkDisplay *display)
75 GdkWindow *pointer_window;
76 GtkWidget *widget = NULL;
78 pointer_window = gdk_display_get_window_at_pointer (display, NULL, NULL);
80 /* The user data field of a GdkWindow is used to store a pointer
81 * to the widget that created it.
86 gdk_window_get_user_data (pointer_window, &widget_ptr);
90 return widget ? gtk_widget_get_toplevel (widget) : NULL;
94 button_release_event_cb (GtkWidget *widget,
95 GdkEventButton *event,
102 /* Asks the user to click on a window, then waits for them click
103 * the mouse. When the mouse is released, returns the toplevel
104 * window under the pointer, or NULL, if there is none.
107 query_for_toplevel (GdkScreen *screen,
110 GdkDisplay *display = gdk_screen_get_display (screen);
111 GtkWidget *popup, *label, *frame;
113 GtkWidget *toplevel = NULL;
115 popup = gtk_window_new (GTK_WINDOW_POPUP);
116 gtk_window_set_screen (GTK_WINDOW (popup), screen);
117 gtk_window_set_modal (GTK_WINDOW (popup), TRUE);
118 gtk_window_set_position (GTK_WINDOW (popup), GTK_WIN_POS_CENTER);
120 frame = gtk_frame_new (NULL);
121 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
122 gtk_container_add (GTK_CONTAINER (popup), frame);
124 label = gtk_label_new (prompt);
125 gtk_misc_set_padding (GTK_MISC (label), 10, 10);
126 gtk_container_add (GTK_CONTAINER (frame), label);
128 gtk_widget_show_all (popup);
129 cursor = gdk_cursor_new_for_display (display, GDK_CROSSHAIR);
131 if (gdk_pointer_grab (popup->window, FALSE,
132 GDK_BUTTON_RELEASE_MASK,
135 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS)
137 gboolean clicked = FALSE;
139 g_signal_connect (popup, "button-release-event",
140 G_CALLBACK (button_release_event_cb), &clicked);
142 /* Process events until clicked is set by button_release_event_cb.
143 * We pass in may_block=TRUE since we want to wait if there
144 * are no events currently.
147 g_main_context_iteration (NULL, TRUE);
149 toplevel = find_toplevel_at_pointer (gdk_screen_get_display (screen));
150 if (toplevel == popup)
154 gdk_cursor_unref (cursor);
155 gtk_widget_destroy (popup);
156 gdk_flush (); /* Really release the grab */
161 /* Prompts the user for a toplevel window to move, and then moves
162 * that window to the currently selected display
165 query_change_display (ChangeDisplayInfo *info)
167 GdkScreen *screen = gtk_widget_get_screen (info->window);
170 toplevel = query_for_toplevel (screen,
171 "Please select the toplevel\n"
172 "to move to the new screen");
175 gtk_window_set_screen (GTK_WINDOW (toplevel), info->current_screen);
177 gdk_display_beep (gdk_screen_get_display (screen));
180 /* Fills in the screen list based on the current display
183 fill_screens (ChangeDisplayInfo *info)
185 gtk_list_store_clear (GTK_LIST_STORE (info->screen_model));
187 if (info->current_display)
189 gint n_screens = gdk_display_get_n_screens (info->current_display);
192 for (i = 0; i < n_screens; i++)
194 GdkScreen *screen = gdk_display_get_screen (info->current_display, i);
197 gtk_list_store_append (GTK_LIST_STORE (info->screen_model), &iter);
198 gtk_list_store_set (GTK_LIST_STORE (info->screen_model), &iter,
199 SCREEN_COLUMN_NUMBER, i,
200 SCREEN_COLUMN_SCREEN, screen,
204 gtk_tree_selection_select_iter (info->screen_selection, &iter);
209 /* Called when the user clicks on a button in our dialog or
210 * closes the dialog through the window manager. Unless the
211 * "Change" button was clicked, we destroy the dialog.
214 response_cb (GtkDialog *dialog,
216 ChangeDisplayInfo *info)
218 if (response_id == GTK_RESPONSE_OK)
219 query_change_display (info);
221 gtk_widget_destroy (GTK_WIDGET (dialog));
224 /* Called when the user clicks on "Open..." in the display
225 * frame. Prompts for a new display, and then opens a connection
229 open_display_cb (GtkWidget *button,
230 ChangeDisplayInfo *info)
233 GtkWidget *display_entry;
234 GtkWidget *dialog_label;
235 gchar *new_screen_name = NULL;
236 GdkDisplay *result = NULL;
238 dialog = gtk_dialog_new_with_buttons ("Open Display",
239 GTK_WINDOW (info->window),
241 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
242 GTK_STOCK_OK, GTK_RESPONSE_OK,
245 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
246 display_entry = gtk_entry_new ();
247 gtk_entry_set_activates_default (GTK_ENTRY (display_entry), TRUE);
249 gtk_label_new ("Please enter the name of\nthe new display\n");
251 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), dialog_label);
252 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), display_entry);
254 gtk_widget_grab_focus (display_entry);
255 gtk_widget_show_all (gtk_bin_get_child (GTK_BIN (dialog)));
259 gint response_id = gtk_dialog_run (GTK_DIALOG (dialog));
260 if (response_id != GTK_RESPONSE_OK)
263 new_screen_name = gtk_editable_get_chars (GTK_EDITABLE (display_entry),
266 if (strcmp (new_screen_name, "") != 0)
268 result = gdk_display_open (new_screen_name);
272 g_strdup_printf ("Can't open display :\n\t%s\nplease try another one\n",
274 gtk_label_set_text (GTK_LABEL (dialog_label), error_msg);
278 g_free (new_screen_name);
282 gtk_widget_destroy (dialog);
285 /* Called when the user clicks on the "Close" button in the
286 * "Display" frame. Closes the selected display.
289 close_display_cb (GtkWidget *button,
290 ChangeDisplayInfo *info)
292 if (info->current_display)
293 gdk_display_close (info->current_display);
296 /* Called when the selected row in the display list changes.
297 * Updates info->current_display, then refills the list of
301 display_changed_cb (GtkTreeSelection *selection,
302 ChangeDisplayInfo *info)
307 if (info->current_display)
308 g_object_unref (info->current_display);
309 if (gtk_tree_selection_get_selected (selection, &model, &iter))
310 gtk_tree_model_get (model, &iter,
311 DISPLAY_COLUMN_DISPLAY, &info->current_display,
314 info->current_display = NULL;
319 /* Called when the selected row in the sceen list changes.
320 * Updates info->current_screen.
323 screen_changed_cb (GtkTreeSelection *selection,
324 ChangeDisplayInfo *info)
329 if (info->current_screen)
330 g_object_unref (info->current_screen);
331 if (gtk_tree_selection_get_selected (selection, &model, &iter))
332 gtk_tree_model_get (model, &iter,
333 SCREEN_COLUMN_SCREEN, &info->current_screen,
336 info->current_screen = NULL;
339 /* This function is used both for creating the "Display" and
340 * "Screen" frames, since they have a similar structure. The
341 * caller hooks up the right context for the value returned
342 * in tree_view, and packs any relevant buttons into button_vbox.
345 create_frame (ChangeDisplayInfo *info,
348 GtkWidget **tree_view,
349 GtkWidget **button_vbox)
351 GtkTreeSelection *selection;
352 GtkWidget *scrollwin;
355 *frame = gtk_frame_new (title);
357 hbox = gtk_hbox_new (FALSE, 8);
358 gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
359 gtk_container_add (GTK_CONTAINER (*frame), hbox);
361 scrollwin = gtk_scrolled_window_new (NULL, NULL);
362 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
363 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
364 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin),
366 gtk_box_pack_start (GTK_BOX (hbox), scrollwin, TRUE, TRUE, 0);
368 *tree_view = gtk_tree_view_new ();
369 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (*tree_view), FALSE);
370 gtk_container_add (GTK_CONTAINER (scrollwin), *tree_view);
372 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (*tree_view));
373 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
375 *button_vbox = gtk_vbox_new (FALSE, 5);
376 gtk_box_pack_start (GTK_BOX (hbox), *button_vbox, FALSE, FALSE, 0);
378 if (!info->size_group)
379 info->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
381 gtk_size_group_add_widget (GTK_SIZE_GROUP (info->size_group), *button_vbox);
384 /* If we have a stack of buttons, it often looks better if their contents
385 * are left-aligned, rather than centered. This function creates a button
386 * and left-aligns it contents.
389 left_align_button_new (const char *label)
391 GtkWidget *button = gtk_button_new_with_mnemonic (label);
392 GtkWidget *child = gtk_bin_get_child (GTK_BIN (button));
394 gtk_misc_set_alignment (GTK_MISC (child), 0., 0.5);
399 /* Creates the "Display" frame in the main window.
402 create_display_frame (ChangeDisplayInfo *info)
405 GtkWidget *tree_view;
406 GtkWidget *button_vbox;
407 GtkTreeViewColumn *column;
408 GtkTreeSelection *selection;
411 create_frame (info, "Display", &frame, &tree_view, &button_vbox);
413 button = left_align_button_new ("_Open...");
414 g_signal_connect (button, "clicked", G_CALLBACK (open_display_cb), info);
415 gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
417 button = left_align_button_new ("_Close");
418 g_signal_connect (button, "clicked", G_CALLBACK (close_display_cb), info);
419 gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
421 info->display_model = (GtkTreeModel *)gtk_list_store_new (DISPLAY_NUM_COLUMNS,
425 gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), info->display_model);
427 column = gtk_tree_view_column_new_with_attributes ("Name",
428 gtk_cell_renderer_text_new (),
429 "text", DISPLAY_COLUMN_NAME,
431 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
433 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
434 g_signal_connect (selection, "changed",
435 G_CALLBACK (display_changed_cb), info);
440 /* Creates the "Screen" frame in the main window.
443 create_screen_frame (ChangeDisplayInfo *info)
446 GtkWidget *tree_view;
447 GtkWidget *button_vbox;
448 GtkTreeViewColumn *column;
450 create_frame (info, "Screen", &frame, &tree_view, &button_vbox);
452 info->screen_model = (GtkTreeModel *)gtk_list_store_new (SCREEN_NUM_COLUMNS,
456 gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), info->screen_model);
458 column = gtk_tree_view_column_new_with_attributes ("Number",
459 gtk_cell_renderer_text_new (),
460 "text", SCREEN_COLUMN_NUMBER,
462 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
464 info->screen_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
465 g_signal_connect (info->screen_selection, "changed",
466 G_CALLBACK (screen_changed_cb), info);
471 /* Called when one of the currently open displays is closed.
472 * Remove it from our list of displays.
475 display_closed_cb (GdkDisplay *display,
477 ChangeDisplayInfo *info)
482 for (valid = gtk_tree_model_get_iter_first (info->display_model, &iter);
484 valid = gtk_tree_model_iter_next (info->display_model, &iter))
486 GdkDisplay *tmp_display;
488 gtk_tree_model_get (info->display_model, &iter,
489 DISPLAY_COLUMN_DISPLAY, &tmp_display,
491 if (tmp_display == display)
493 gtk_list_store_remove (GTK_LIST_STORE (info->display_model), &iter);
499 /* Adds a new display to our list of displays, and connects
500 * to the "closed" signal so that we can remove it from the
501 * list of displays again.
504 add_display (ChangeDisplayInfo *info,
507 const gchar *name = gdk_display_get_name (display);
510 gtk_list_store_append (GTK_LIST_STORE (info->display_model), &iter);
511 gtk_list_store_set (GTK_LIST_STORE (info->display_model), &iter,
512 DISPLAY_COLUMN_NAME, name,
513 DISPLAY_COLUMN_DISPLAY, display,
516 g_signal_connect (display, "closed",
517 G_CALLBACK (display_closed_cb), info);
520 /* Called when a new display is opened
523 display_opened_cb (GdkDisplayManager *manager,
525 ChangeDisplayInfo *info)
527 add_display (info, display);
530 /* Adds all currently open displays to our list of displays,
531 * and set up a signal connection so that we'll be notified
532 * when displays are opened in the future as well.
535 initialize_displays (ChangeDisplayInfo *info)
537 GdkDisplayManager *manager = gdk_display_manager_get ();
538 GSList *displays = gdk_display_manager_list_displays (manager);
541 for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
542 add_display (info, tmp_list->data);
544 g_slist_free (tmp_list);
546 g_signal_connect (manager, "display-opened",
547 G_CALLBACK (display_opened_cb), info);
550 /* Cleans up when the toplevel is destroyed; we remove the
551 * connections we use to track currently open displays, then
552 * free the ChangeDisplayInfo structure.
555 destroy_info (ChangeDisplayInfo *info)
557 GdkDisplayManager *manager = gdk_display_manager_get ();
558 GSList *displays = gdk_display_manager_list_displays (manager);
561 g_signal_handlers_disconnect_by_func (manager,
565 for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
566 g_signal_handlers_disconnect_by_func (tmp_list->data,
570 g_slist_free (tmp_list);
572 g_object_unref (info->size_group);
573 g_object_unref (info->display_model);
574 g_object_unref (info->screen_model);
576 if (info->current_display)
577 g_object_unref (info->current_display);
578 if (info->current_screen)
579 g_object_unref (info->current_screen);
585 destroy_cb (GtkObject *object,
586 ChangeDisplayInfo **info)
588 destroy_info (*info);
592 /* Main entry point. If the dialog for this demo doesn't yet exist, creates
593 * it. Otherwise, destroys it.
596 do_changedisplay (GtkWidget *do_widget)
598 static ChangeDisplayInfo *info = NULL;
605 info = g_new0 (ChangeDisplayInfo, 1);
607 info->window = gtk_dialog_new_with_buttons ("Change Screen or display",
608 GTK_WINDOW (do_widget),
609 GTK_DIALOG_NO_SEPARATOR,
610 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
611 "Change", GTK_RESPONSE_OK,
614 gtk_window_set_default_size (GTK_WINDOW (info->window), 300, 400);
616 g_signal_connect (info->window, "response",
617 G_CALLBACK (response_cb), info);
618 g_signal_connect (info->window, "destroy",
619 G_CALLBACK (destroy_cb), &info);
621 vbox = gtk_vbox_new (FALSE, 5);
622 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
624 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (info->window)->vbox), vbox,
627 frame = create_display_frame (info);
628 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
630 frame = create_screen_frame (info);
631 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
633 initialize_displays (info);
635 gtk_widget_show_all (info->window);
640 gtk_widget_destroy (info->window);