Debian Codegraph @

demos/gtk-demo/changedisplay.c

raw
1 /* Change Display
2 *
3 * Demonstrates migrating a window between different displays.
4 * A display is a mouse and keyboard with some number of
5 * associated monitors. The neat thing about having multiple
6 * displays is that they can be on a completely separate
7 * computers, as long as there is a network connection to the
8 * computer where the application is running.
9 *
10 * Only some of the windowing systems where GTK+ runs have the
11 * concept of multiple displays. (The X Window System is the
12 * main example.) Other windowing systems can only handle one
13 * keyboard and mouse, and combine all monitors into
14 * a single display.
15 *
16 * This is a moderately complex example, and demonstrates:
17 *
18 * - Tracking the currently open displays
19 *
20 * - Changing the display for a window
21 *
22 * - Letting the user choose a window by clicking on it
23 *
24 * - Using GtkListStore and GtkTreeView
25 *
26 * - Using GtkDialog
27 */
28 #include <string.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 /* The ChangeDisplayInfo structure corresponds to a toplevel window and
33 * holds pointers to widgets inside the toplevel window along with other
34 * information about the contents of the window.
35 * This is a common organizational structure in real applications.
36 */
37 typedef struct _ChangeDisplayInfo ChangeDisplayInfo;
38
39 struct _ChangeDisplayInfo
40 {
41 GtkWidget *window;
42 GtkSizeGroup *size_group;
43
44 GtkTreeModel *display_model;
45
46 GdkDisplay *current_display;
47 };
48
49 /* These enumerations provide symbolic names for the columns
50 * in the two GtkListStore models.
51 */
52 enum
53 {
54 DISPLAY_COLUMN_NAME,
55 DISPLAY_COLUMN_DISPLAY,
56 DISPLAY_NUM_COLUMNS
57 };
58
59 enum
60 {
61 SCREEN_COLUMN_NUMBER,
62 SCREEN_COLUMN_SCREEN,
63 SCREEN_NUM_COLUMNS
64 };
65
66 /* Finds the toplevel window under the mouse pointer, if any.
67 */
68 static GtkWidget *
69 find_toplevel_at_pointer (GdkDisplay *display)
70 {
71 GdkWindow *pointer_window;
72 GtkWidget *widget = NULL;
73
74 pointer_window = gdk_device_get_window_at_position (gtk_get_current_event_device (),
75 NULL, NULL);
76
77 /* The user data field of a GdkWindow is used to store a pointer
78 * to the widget that created it.
79 */
80 if (pointer_window)
81 {
82 gpointer widget_ptr;
83 gdk_window_get_user_data (pointer_window, &widget_ptr);
84 widget = widget_ptr;
85 }
86
87 return widget ? gtk_widget_get_toplevel (widget) : NULL;
88 }
89
90 static gboolean
91 button_release_event_cb (GtkWidget *widget,
92 GdkEventButton *event,
93 gboolean *clicked)
94 {
95 *clicked = TRUE;
96 return TRUE;
97 }
98
99 /* Asks the user to click on a window, then waits for them click
100 * the mouse. When the mouse is released, returns the toplevel
101 * window under the pointer, or NULL, if there is none.
102 */
103 static GtkWidget *
104 query_for_toplevel (GdkScreen *screen,
105 const char *prompt)
106 {
107 GdkDisplay *display = gdk_screen_get_display (screen);
108 GtkWidget *popup, *label, *frame;
109 GdkCursor *cursor;
110 GtkWidget *toplevel = NULL;
111 GdkDevice *device;
112
113 popup = gtk_window_new (GTK_WINDOW_POPUP);
114 gtk_window_set_screen (GTK_WINDOW (popup), screen);
115 gtk_window_set_modal (GTK_WINDOW (popup), TRUE);
116 gtk_window_set_position (GTK_WINDOW (popup), GTK_WIN_POS_CENTER);
117
118 frame = gtk_frame_new (NULL);
119 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
120 gtk_container_add (GTK_CONTAINER (popup), frame);
121
122 label = gtk_label_new (prompt);
123 g_object_set (label, "margin", 10, NULL);
124 gtk_container_add (GTK_CONTAINER (frame), label);
125
126 gtk_widget_show_all (popup);
127 cursor = gdk_cursor_new_from_name (display, "crosshair");
128 device = gtk_get_current_event_device ();
129
130 if (gdk_seat_grab (gdk_device_get_seat (device),
131 gtk_widget_get_window (popup),
132 GDK_SEAT_CAPABILITY_ALL_POINTING,
133 FALSE, cursor, NULL, NULL, NULL) == GDK_GRAB_SUCCESS)
134 {
135 gboolean clicked = FALSE;
136
137 g_signal_connect (popup, "button-release-event",
138 G_CALLBACK (button_release_event_cb), &clicked);
139
140 /* Process events until clicked is set by button_release_event_cb.
141 * We pass in may_block=TRUE since we want to wait if there
142 * are no events currently.
143 */
144 while (!clicked)
145 g_main_context_iteration (NULL, TRUE);
146
147 toplevel = find_toplevel_at_pointer (gdk_screen_get_display (screen));
148 if (toplevel == popup)
149 toplevel = NULL;
150 }
151
152 g_object_unref (cursor);
153 gtk_widget_destroy (popup);
154 gdk_display_flush (display); /* Really release the grab */
155
156 return toplevel;
157 }
158
159 /* Prompts the user for a toplevel window to move, and then moves
160 * that window to the currently selected display
161 */
162 static void
163 query_change_display (ChangeDisplayInfo *info)
164 {
165 GdkScreen *screen = gtk_widget_get_screen (info->window);
166 GtkWidget *toplevel;
167
168 toplevel = query_for_toplevel (screen,
169 "Please select the toplevel\n"
170 "to move to the new screen");
171
172 if (toplevel)
173 gtk_window_set_screen (GTK_WINDOW (toplevel), gdk_display_get_default_screen (info->current_display));
174 else
175 gdk_display_beep (gdk_screen_get_display (screen));
176 }
177
178 /* Called when the user clicks on a button in our dialog or
179 * closes the dialog through the window manager. Unless the
180 * "Change" button was clicked, we destroy the dialog.
181 */
182 static void
183 response_cb (GtkDialog *dialog,
184 gint response_id,
185 ChangeDisplayInfo *info)
186 {
187 if (response_id == GTK_RESPONSE_OK)
188 query_change_display (info);
189 else
190 gtk_widget_destroy (GTK_WIDGET (dialog));
191 }
192
193 /* Called when the user clicks on "Open..." in the display
194 * frame. Prompts for a new display, and then opens a connection
195 * to that display.
196 */
197 static void
198 open_display_cb (GtkWidget *button,
199 ChangeDisplayInfo *info)
200 {
201 GtkWidget *content_area;
202 GtkWidget *dialog;
203 GtkWidget *display_entry;
204 GtkWidget *dialog_label;
205 gchar *new_screen_name = NULL;
206 GdkDisplay *result = NULL;
207
208 dialog = gtk_dialog_new_with_buttons ("Open Display",
209 GTK_WINDOW (info->window),
210 GTK_DIALOG_MODAL,
211 _("_Cancel"), GTK_RESPONSE_CANCEL,
212 _("_OK"), GTK_RESPONSE_OK,
213 NULL);
214
215 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
216 display_entry = gtk_entry_new ();
217 gtk_entry_set_activates_default (GTK_ENTRY (display_entry), TRUE);
218 dialog_label =
219 gtk_label_new ("Please enter the name of\nthe new display\n");
220
221 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
222
223 gtk_container_add (GTK_CONTAINER (content_area), dialog_label);
224 gtk_container_add (GTK_CONTAINER (content_area), display_entry);
225
226 gtk_widget_grab_focus (display_entry);
227 gtk_widget_show_all (gtk_bin_get_child (GTK_BIN (dialog)));
228
229 while (!result)
230 {
231 gint response_id = gtk_dialog_run (GTK_DIALOG (dialog));
232 if (response_id != GTK_RESPONSE_OK)
233 break;
234
235 new_screen_name = gtk_editable_get_chars (GTK_EDITABLE (display_entry),
236 0, -1);
237
238 if (strcmp (new_screen_name, "") != 0)
239 {
240 result = gdk_display_open (new_screen_name);
241 if (!result)
242 {
243 gchar *error_msg =
244 g_strdup_printf ("Can't open display:\n\t%s\nplease try another one\n",
245 new_screen_name);
246 gtk_label_set_text (GTK_LABEL (dialog_label), error_msg);
247 g_free (error_msg);
248 }
249
250 g_free (new_screen_name);
251 }
252 }
253
254 gtk_widget_destroy (dialog);
255 }
256
257 /* Called when the user clicks on the "Close" button in the
258 * "Display" frame. Closes the selected display.
259 */
260 static void
261 close_display_cb (GtkWidget *button,
262 ChangeDisplayInfo *info)
263 {
264 if (info->current_display)
265 gdk_display_close (info->current_display);
266 }
267
268 /* Called when the selected row in the display list changes.
269 * Updates info->current_display, then refills the list of
270 * screens.
271 */
272 static void
273 display_changed_cb (GtkTreeSelection *selection,
274 ChangeDisplayInfo *info)
275 {
276 GtkTreeModel *model;
277 GtkTreeIter iter;
278
279 if (info->current_display)
280 g_object_unref (info->current_display);
281 if (gtk_tree_selection_get_selected (selection, &model, &iter))
282 gtk_tree_model_get (model, &iter,
283 DISPLAY_COLUMN_DISPLAY, &info->current_display,
284 -1);
285 else
286 info->current_display = NULL;
287 }
288
289 /* This function is used both for creating the "Display" and
290 * "Screen" frames, since they have a similar structure. The
291 * caller hooks up the right context for the value returned
292 * in tree_view, and packs any relevant buttons into button_vbox.
293 */
294 static void
295 create_frame (ChangeDisplayInfo *info,
296 const char *title,
297 GtkWidget **frame,
298 GtkWidget **tree_view,
299 GtkWidget **button_vbox)
300 {
301 GtkTreeSelection *selection;
302 GtkWidget *scrollwin;
303 GtkWidget *hbox;
304
305 *frame = gtk_frame_new (title);
306
307 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
308 gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
309 gtk_container_add (GTK_CONTAINER (*frame), hbox);
310
311 scrollwin = gtk_scrolled_window_new (NULL, NULL);
312 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
313 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
314 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin),
315 GTK_SHADOW_IN);
316 gtk_box_pack_start (GTK_BOX (hbox), scrollwin, TRUE, TRUE, 0);
317
318 *tree_view = gtk_tree_view_new ();
319 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (*tree_view), FALSE);
320 gtk_container_add (GTK_CONTAINER (scrollwin), *tree_view);
321
322 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (*tree_view));
323 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
324
325 *button_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
326 gtk_box_pack_start (GTK_BOX (hbox), *button_vbox, FALSE, FALSE, 0);
327
328 if (!info->size_group)
329 info->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
330
331 gtk_size_group_add_widget (GTK_SIZE_GROUP (info->size_group), *button_vbox);
332 }
333
334 /* If we have a stack of buttons, it often looks better if their contents
335 * are left-aligned, rather than centered. This function creates a button
336 * and left-aligns it contents.
337 */
338 GtkWidget *
339 left_align_button_new (const char *label)
340 {
341 GtkWidget *button = gtk_button_new_with_mnemonic (label);
342 GtkWidget *child = gtk_bin_get_child (GTK_BIN (button));
343
344 gtk_widget_set_halign (child, GTK_ALIGN_START);
345 gtk_widget_set_valign (child, GTK_ALIGN_CENTER);
346
347 return button;
348 }
349
350 /* Creates the "Display" frame in the main window.
351 */
352 GtkWidget *
353 create_display_frame (ChangeDisplayInfo *info)
354 {
355 GtkWidget *frame;
356 GtkWidget *tree_view;
357 GtkWidget *button_vbox;
358 GtkTreeViewColumn *column;
359 GtkTreeSelection *selection;
360 GtkWidget *button;
361
362 create_frame (info, "Display", &frame, &tree_view, &button_vbox);
363
364 button = left_align_button_new ("_Open...");
365 g_signal_connect (button, "clicked", G_CALLBACK (open_display_cb), info);
366 gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
367
368 button = left_align_button_new ("_Close");
369 g_signal_connect (button, "clicked", G_CALLBACK (close_display_cb), info);
370 gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
371
372 info->display_model = (GtkTreeModel *)gtk_list_store_new (DISPLAY_NUM_COLUMNS,
373 G_TYPE_STRING,
374 GDK_TYPE_DISPLAY);
375
376 gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), info->display_model);
377
378 column = gtk_tree_view_column_new_with_attributes ("Name",
379 gtk_cell_renderer_text_new (),
380 "text", DISPLAY_COLUMN_NAME,
381 NULL);
382 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
383
384 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
385 g_signal_connect (selection, "changed",
386 G_CALLBACK (display_changed_cb), info);
387
388 return frame;
389 }
390
391 /* Called when one of the currently open displays is closed.
392 * Remove it from our list of displays.
393 */
394 static void
395 display_closed_cb (GdkDisplay *display,
396 gboolean is_error,
397 ChangeDisplayInfo *info)
398 {
399 GtkTreeIter iter;
400 gboolean valid;
401
402 for (valid = gtk_tree_model_get_iter_first (info->display_model, &iter);
403 valid;
404 valid = gtk_tree_model_iter_next (info->display_model, &iter))
405 {
406 GdkDisplay *tmp_display;
407
408 gtk_tree_model_get (info->display_model, &iter,
409 DISPLAY_COLUMN_DISPLAY, &tmp_display,
410 -1);
411 if (tmp_display == display)
412 {
413 gtk_list_store_remove (GTK_LIST_STORE (info->display_model), &iter);
414 break;
415 }
416 }
417 }
418
419 /* Adds a new display to our list of displays, and connects
420 * to the "closed" signal so that we can remove it from the
421 * list of displays again.
422 */
423 static void
424 add_display (ChangeDisplayInfo *info,
425 GdkDisplay *display)
426 {
427 const gchar *name = gdk_display_get_name (display);
428 GtkTreeIter iter;
429
430 gtk_list_store_append (GTK_LIST_STORE (info->display_model), &iter);
431 gtk_list_store_set (GTK_LIST_STORE (info->display_model), &iter,
432 DISPLAY_COLUMN_NAME, name,
433 DISPLAY_COLUMN_DISPLAY, display,
434 -1);
435
436 g_signal_connect (display, "closed",
437 G_CALLBACK (display_closed_cb), info);
438 }
439
440 /* Called when a new display is opened
441 */
442 static void
443 display_opened_cb (GdkDisplayManager *manager,
444 GdkDisplay *display,
445 ChangeDisplayInfo *info)
446 {
447 add_display (info, display);
448 }
449
450 /* Adds all currently open displays to our list of displays,
451 * and set up a signal connection so that we'll be notified
452 * when displays are opened in the future as well.
453 */
454 static void
455 initialize_displays (ChangeDisplayInfo *info)
456 {
457 GdkDisplayManager *manager = gdk_display_manager_get ();
458 GSList *displays = gdk_display_manager_list_displays (manager);
459 GSList *tmp_list;
460
461 for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
462 add_display (info, tmp_list->data);
463
464 g_slist_free (tmp_list);
465
466 g_signal_connect (manager, "display-opened",
467 G_CALLBACK (display_opened_cb), info);
468 }
469
470 /* Cleans up when the toplevel is destroyed; we remove the
471 * connections we use to track currently open displays, then
472 * free the ChangeDisplayInfo structure.
473 */
474 static void
475 destroy_info (ChangeDisplayInfo *info)
476 {
477 GdkDisplayManager *manager = gdk_display_manager_get ();
478 GSList *displays = gdk_display_manager_list_displays (manager);
479 GSList *tmp_list;
480
481 g_signal_handlers_disconnect_by_func (manager,
482 display_opened_cb,
483 info);
484
485 for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
486 g_signal_handlers_disconnect_by_func (tmp_list->data,
487 display_closed_cb,
488 info);
489
490 g_slist_free (tmp_list);
491
492 g_object_unref (info->size_group);
493 g_object_unref (info->display_model);
494
495 if (info->current_display)
496 g_object_unref (info->current_display);
497
498 g_free (info);
499 }
500
501 static void
502 destroy_cb (GObject *object,
503 ChangeDisplayInfo **info)
504 {
505 destroy_info (*info);
506 *info = NULL;
507 }
508
509 /* Main entry point. If the dialog for this demo doesn't yet exist, creates
510 * it. Otherwise, destroys it.
511 */
512 GtkWidget *
513 do_changedisplay (GtkWidget *do_widget)
514 {
515 static ChangeDisplayInfo *info = NULL;
516
517 if (!info)
518 {
519 GtkWidget *content_area;
520 GtkWidget *vbox;
521 GtkWidget *frame;
522
523 info = g_new0 (ChangeDisplayInfo, 1);
524
525 info->window = gtk_dialog_new_with_buttons ("Change Display",
526 GTK_WINDOW (do_widget),
527 0,
528 "Close", GTK_RESPONSE_CLOSE,
529 "Change", GTK_RESPONSE_OK,
530 NULL);
531
532 gtk_window_set_default_size (GTK_WINDOW (info->window), 300, 400);
533
534 g_signal_connect (info->window, "response",
535 G_CALLBACK (response_cb), info);
536 g_signal_connect (info->window, "destroy",
537 G_CALLBACK (destroy_cb), &info);
538
539 content_area = gtk_dialog_get_content_area (GTK_DIALOG (info->window));
540
541 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
542 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
543 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
544
545 frame = create_display_frame (info);
546 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
547
548 initialize_displays (info);
549
550 gtk_widget_show_all (info->window);
551 return info->window;
552 }
553 else
554 {
555 gtk_widget_destroy (info->window);
556 return NULL;
557 }
558 }