Debian Codegraph @

src/modules/frontend/gtk/ui.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file ui.c
35 * Common user interface related functions for the GTK+ frontend of cdebconf
36 */
37
38 #include "ui.h"
39
40 #include <gdk/gdkkeysyms.h>
41 #include <gtk/gtk.h>
42
43 #include <string.h>
44
45 #include "frontend.h"
46 #include "question.h"
47
48 #include "cdebconf_gtk.h"
49 #include "fe_data.h"
50 #include "descriptions.h"
51
52 /* documented in cdebconf_gtk.h */
53 void cdebconf_gtk_add_common_layout(struct frontend * fe,
54 struct question * question,
55 GtkWidget * container, GtkWidget * widget)
56 {
57 GtkWidget * description_box;
58 GtkWidget * hpadbox;
59 GtkWidget * vpadbox;
60 gboolean is_single;
61
62 is_single = IS_QUESTION_SINGLE(question);
63
64 description_box = cdebconf_gtk_create_description(fe, question);
65
66 vpadbox = gtk_vbox_new(FALSE /* don't make children equal */,
67 DEFAULT_PADDING);
68 gtk_box_pack_start(GTK_BOX(vpadbox), description_box,
69 FALSE /* don't expand */, FALSE /* don't fill */,
70 0 /* padding */);
71 gtk_box_pack_start(GTK_BOX(vpadbox), widget, is_single /* expand? */,
72 is_single /* fill? */, 0 /* padding */);
73 hpadbox = gtk_hbox_new(FALSE /* don't make children equal */,
74 DEFAULT_PADDING);
75 gtk_box_pack_start(GTK_BOX(hpadbox), vpadbox, TRUE /* expand */,
76 TRUE /* fill */, 0 /* padding */);
77 gtk_box_pack_start(GTK_BOX(container), hpadbox, is_single /* expand? */,
78 is_single /* fill? */, QUESTIONBOX_VPADDING);
79 }
80
81 /** Handle the expose event for the banner.
82 *
83 * This function will redraw the last INFO text on top of the banner.
84 *
85 * @param widget the banner
86 * @param event expose event
87 * @param fe cdebconf frontend
88 * @return FALSE as the event does not need to propagate further
89 */
90 static gboolean handle_exposed_banner(GtkWidget * widget,
91 GdkEventExpose * event,
92 struct frontend * fe)
93 {
94 struct frontend_data * fe_data = fe->data;
95 GdkScreen * screen;
96 GdkWindow * window;
97 PangoFontDescription * font;
98 PangoLayout * layout;
99 gint text_width;
100 gint text_height;
101 gchar * message;
102 char * text;
103
104 /* There's no way to get the window size before it's realized, so
105 * defer (possibly) adjusting the logo size until the first expose
106 * event.
107 */
108 if (TRUE != fe_data->banner_adjusted) {
109 /* If logo is smaller than the window, scale and show the relevant
110 * left/right side(s). Equal is usual, greater is also possible.
111 */
112 GdkPixbuf * scaled;
113 GtkAllocation allocation;
114 gtk_widget_get_allocation(fe_data->window, &allocation);
115 fprintf(stderr, "Checking widths: logo (%d) vs. window (%d): ",
116 fe_data->logo_width, allocation.width);
117
118 if (fe_data->logo_width < allocation.width) {
119 if (fe_data->banner_expands_left && fe_data->banner_expands_right) {
120 /* Keep track of left_width to avoid an (improbable) off-by-one */
121 int left_width = (allocation.width - fe_data->logo_width ) / 2;
122 scaled = gdk_pixbuf_scale_simple(gtk_image_get_pixbuf(GTK_IMAGE(fe_data->logo_widget_l)),
123 left_width,
124 fe_data->logo_height,
125 GDK_INTERP_BILINEAR);
126 gtk_image_set_from_pixbuf(GTK_IMAGE(fe_data->logo_widget_l), scaled);
127 gtk_widget_show(fe_data->logo_widget_l);
128
129 scaled = gdk_pixbuf_scale_simple(gtk_image_get_pixbuf(GTK_IMAGE(fe_data->logo_widget_r)),
130 allocation.width - fe_data->logo_width - left_width,
131 fe_data->logo_height,
132 GDK_INTERP_BILINEAR);
133 gtk_image_set_from_pixbuf(GTK_IMAGE(fe_data->logo_widget_r), scaled);
134 gtk_widget_show(fe_data->logo_widget_r);
135
136 fprintf(stderr, "both sides scaled.\n");
137 }
138 else if (fe_data->banner_expands_left) {
139 scaled = gdk_pixbuf_scale_simple(gtk_image_get_pixbuf(GTK_IMAGE(fe_data->logo_widget_l)),
140 allocation.width - fe_data->logo_width,
141 fe_data->logo_height,
142 GDK_INTERP_BILINEAR);
143 gtk_image_set_from_pixbuf(GTK_IMAGE(fe_data->logo_widget_l), scaled);
144 gtk_widget_show(fe_data->logo_widget_l);
145
146 fprintf(stderr, "left side scaled.\n");
147 }
148 else if (fe_data->banner_expands_right) {
149 scaled = gdk_pixbuf_scale_simple(gtk_image_get_pixbuf(GTK_IMAGE(fe_data->logo_widget_r)),
150 allocation.width - fe_data->logo_width,
151 fe_data->logo_height,
152 GDK_INTERP_BILINEAR);
153 gtk_image_set_from_pixbuf(GTK_IMAGE(fe_data->logo_widget_r), scaled);
154 gtk_widget_show(fe_data->logo_widget_r);
155 fprintf(stderr, "right side scaled.\n");
156 }
157 else {
158 /* Default settings and config parsing should make that impossible */
159 fprintf(stderr, "no sides scaled!\n");
160 }
161 } else {
162 fprintf(stderr, "no scaling needed.\n");
163 }
164 fe_data->banner_adjusted = TRUE;
165 }
166
167 if (NULL != fe->info) {
168 text = q_get_description(fe, fe->info);
169 message = g_strdup_printf(
170 "<b><span foreground=\"#ffffff\">%s</span></b>", text);
171 layout = gtk_widget_create_pango_layout(
172 widget, NULL /* no text for the layout */);
173 pango_layout_set_markup(layout, message, -1 /* until '\0' */);
174 font = pango_font_description_from_string("Sans 12");
175 pango_layout_set_font_description(layout, font);
176 pango_layout_get_pixel_size(layout, &text_width, &text_height);
177 screen = gtk_window_get_screen(GTK_WINDOW(fe_data->window));
178 window = gtk_widget_get_window(widget);
179 /* Align according to banner metadata, always vertically-center */
180 int position_x = fe_data->banner_label_position == GTK_JUSTIFY_RIGHT
181 ? gdk_screen_get_width(screen) - text_width - DEFAULT_PADDING * 2
182 : DEFAULT_PADDING * 2;
183 cairo_t *cr = gdk_cairo_create(window);
184 cairo_move_to(cr,
185 position_x,
186 (fe_data->logo_height - text_height) / 2);
187 pango_cairo_show_layout(cr, layout);
188 cairo_destroy(cr);
189
190 g_object_unref(layout);
191 pango_font_description_free(font);
192 g_free(message);
193 g_free(text);
194 }
195 return FALSE;
196 }
197
198 /* Query the gtk theme currently in use
199 *
200 * Currently it is likely to be either "Clearlooks" (default) or "dark"
201 * (for better accessibility).
202 *
203 * The caller is responsible for freeing the returned value.
204 */
205 static char * get_gtk_theme_name()
206 {
207 GtkSettings * settings;
208 gchar * theme_name;
209
210 settings = gtk_settings_get_default();
211 g_return_val_if_fail(settings != NULL, NULL);
212 g_object_get(settings, "gtk-theme-name", &theme_name, NULL);
213
214 return theme_name;
215 }
216
217 /** Configure the banner
218 *
219 * This makes it possible for the package providing the logo to also configure
220 * where the label should be placed, and which side(s) can be expanded when the
221 * GTK window is wider than the logo.
222 *
223 * @param fe cdebconf frontend
224 * @see LOGO_CONFIG_PATH
225 */
226 static void initialize_banner_metadata(struct frontend * fe) {
227 struct frontend_data * fe_data = fe->data;
228
229 GKeyFile * config = NULL;
230 gchar * value;
231
232 /* Default settings */
233 fe_data->banner_label_position = GTK_JUSTIFY_RIGHT;
234 fe_data->banner_expands_left = FALSE;
235 fe_data->banner_expands_right = TRUE;
236
237 /* Mini config file shipped by rootskel-gtk */
238 config = g_key_file_new();
239 g_return_if_fail(g_key_file_load_from_file(config, LOGO_CONFIG_PATH, G_KEY_FILE_NONE, NULL) == TRUE);
240 g_return_if_fail(config != NULL);
241
242 value = g_key_file_get_string(config, "banner", "label-position", NULL);
243 if (value) {
244 if (!strcmp(value, "left"))
245 fe_data->banner_label_position = GTK_JUSTIFY_LEFT;
246 else if (value && !strcmp(value, "right"))
247 fe_data->banner_label_position = GTK_JUSTIFY_RIGHT;
248 else
249 fprintf(stderr, "unsupported value for banner.label-position: %s\n", value);
250 }
251
252 value = g_key_file_get_string(config, "banner", "expand-direction", NULL);
253 if (value) {
254 if (!strcmp(value, "both")) {
255 fe_data->banner_expands_left = TRUE;
256 fe_data->banner_expands_right = TRUE;
257 }
258 else if (!strcmp(value, "left")) {
259 fe_data->banner_expands_left = TRUE;
260 fe_data->banner_expands_right = FALSE;
261 }
262 else if (!strcmp(value, "right")) {
263 fe_data->banner_expands_left = FALSE;
264 fe_data->banner_expands_right = TRUE;
265 }
266 else
267 fprintf(stderr, "unsupported value for expand-direction: %s\n", value);
268 }
269
270 g_key_file_free(config);
271 }
272
273 /** Create the banner with the logo inside the given container.
274 *
275 * The logo will be centered in the banner initially. Upon the initial expose
276 * event, if the GTK window is wider than the logo, based on banner metadata,
277 * the left-most and/or the right-most 1-px column can be repeated to cover the
278 * full width of the GTK window.
279 *
280 * @param fe cdebconf frontend
281 * @param container container in which the banner will be added
282 * @see LOGO_IMAGE_PATH
283 * @see LOGO_DARK_IMAGE_PATH
284 */
285 static void create_banner(struct frontend * fe, GtkWidget * container)
286 {
287 struct frontend_data * fe_data = fe->data;
288 GtkWidget * banner;
289 GtkWidget * banner_box;
290 GtkWidget * logo, * logo_l, * logo_r;
291 GdkPixbuf * pixbuf, * pixbuf_l, * pixbuf_r;
292 const char * banner_path = LOGO_IMAGE_PATH;
293 char * theme_name;
294
295 /* Switch to an alternative banner for theme=dark; don't do
296 * anything in case of errors.
297 */
298 theme_name = get_gtk_theme_name();
299 if ((theme_name != NULL) && !strcmp(theme_name, "dark")) {
300 if (g_file_test(LOGO_DARK_IMAGE_PATH, G_FILE_TEST_EXISTS) == TRUE) {
301 fprintf(stderr, "theme=dark detected, switching to alternate banner\n");
302 banner_path = LOGO_DARK_IMAGE_PATH;
303 } else {
304 fprintf(stderr, "theme=dark detected, not switching to alternate banner (not available)\n");
305 }
306 }
307 g_free(theme_name);
308
309 initialize_banner_metadata(fe);
310
311 banner = gtk_event_box_new();
312 banner_box = gtk_hbox_new(FALSE, 0);
313 logo = gtk_image_new_from_file(banner_path);
314 pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(logo));
315
316 /* Extract first and last column (left and right) to build an object in any
317 * case, let the first expose event deal with a possible resize.
318 */
319 pixbuf_l = gdk_pixbuf_new_subpixbuf(pixbuf,
320 0, 0,
321 1, gdk_pixbuf_get_height(pixbuf));
322 pixbuf_r = gdk_pixbuf_new_subpixbuf(pixbuf,
323 gdk_pixbuf_get_width(pixbuf)-1, 0,
324 1, gdk_pixbuf_get_height(pixbuf));
325 logo_l = gtk_image_new_from_pixbuf(pixbuf_l);
326 logo_r = gtk_image_new_from_pixbuf(pixbuf_r);
327
328 gtk_box_pack_start(GTK_BOX(banner_box), GTK_WIDGET(logo_l),
329 TRUE, TRUE, 0);
330 gtk_box_pack_start(GTK_BOX(banner_box), GTK_WIDGET(logo),
331 FALSE, FALSE, 0);
332 gtk_box_pack_start(GTK_BOX(banner_box), GTK_WIDGET(logo_r),
333 TRUE, TRUE, 0);
334 gtk_container_add(GTK_CONTAINER(banner), banner_box);
335
336 /* Hide both expanders by default, leaving the main logo a chance to be
337 * displayed properly from the get-go.
338 */
339 gtk_widget_hide(logo_l);
340 gtk_widget_hide(logo_r);
341
342 /* Remember the logo size: */
343 if (gtk_image_get_storage_type(GTK_IMAGE(logo)) == GTK_IMAGE_PIXBUF)
344 {
345 pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(logo));
346 fe_data->logo_width = gdk_pixbuf_get_width(pixbuf);
347 fe_data->logo_height = gdk_pixbuf_get_height(pixbuf);
348 fe_data->banner_adjusted = FALSE;
349 }
350 else
351 {
352 /* kibi/2023-05-19: does that ever happen? kept as is for now, but would
353 * probably error out way before, with the two new images and pixbufs.
354 */
355 fe_data->logo_height = 24;
356 fe_data->banner_adjusted = TRUE;
357 }
358 fe_data->logo_widget = logo;
359 fe_data->logo_widget_l = logo_l;
360 fe_data->logo_widget_r = logo_r;
361
362 #if GTK_CHECK_VERSION(3,0,0)
363 g_signal_connect_after(G_OBJECT(banner), "draw",
364 #else
365 g_signal_connect_after(G_OBJECT(banner), "expose_event",
366 #endif
367 G_CALLBACK(handle_exposed_banner), fe);
368
369 gtk_box_pack_start(GTK_BOX(container), banner,
370 FALSE /* don't expand */, FALSE /* don't fill */,
371 0 /* padding */);
372
373 /* Workaround for #882804, GTK 2 only */
374 fe_data->banner_widget = banner;
375 fe_data->banner_workaround_needed = TRUE;
376 }
377
378 /** Create the label where the current operation will be displayed.
379 *
380 * @param fe cdebconf frontend
381 * @param container the container in which the label will be added
382 */
383 static void create_label_title(struct frontend * fe, GtkWidget * container)
384 {
385 struct frontend_data * fe_data = fe->data;
386 GtkWidget * label_title;
387
388 label_title = gtk_label_new(NULL /* no label */);
389
390 gtk_misc_set_alignment(GTK_MISC(label_title), 0 /* left */, 0 /* top */);
391
392 g_object_ref(G_OBJECT(label_title));
393 fe_data->title = label_title;
394
395 cdebconf_gtk_center_widget(&label_title,
396 DEFAULT_PADDING /* horizontal padding */,
397 0 /* no vertical padding */);
398 gtk_box_pack_start(GTK_BOX(container), label_title,
399 FALSE /* expand */, FALSE /* fill */, DEFAULT_PADDING);
400 }
401
402 /** Create the main container where questions or progress will be added.
403 *
404 * @param fe cdebconf frontend
405 * @param container the container in which the target box will be added
406 */
407 static void create_target_box(struct frontend * fe, GtkWidget * container)
408 {
409 struct frontend_data * fe_data = fe->data;
410 GtkWidget * target_box;
411
412 /* This is the box were question(s) will be displayed */
413 target_box = gtk_vbox_new(FALSE /* don't make children equal */,
414 0 /* padding */);
415
416 gtk_box_pack_start(GTK_BOX(container), target_box, TRUE /* expand */,
417 TRUE /* fill */, DEFAULT_PADDING);
418
419 g_object_ref(G_OBJECT(target_box));
420 fe_data->target_box = target_box;
421 }
422
423 /** Create the container for buttons.
424 *
425 * @param fe cdebconf frontend
426 * @param container the container in which the action box will be added
427 */
428 static void create_action_box(struct frontend * fe, GtkWidget * container)
429 {
430 struct frontend_data * fe_data = fe->data;
431 GtkWidget * action_box;
432
433 g_assert(NULL == fe_data->action_box);
434
435 action_box = gtk_hbutton_box_new();
436
437 gtk_button_box_set_layout(GTK_BUTTON_BOX(action_box), GTK_BUTTONBOX_END);
438 gtk_box_set_spacing(GTK_BOX(action_box), DEFAULT_PADDING);
439
440 gtk_box_pack_start(GTK_BOX(container), action_box,
441 FALSE /* don't expand */, FALSE /* don't fill */,
442 DEFAULT_PADDING);
443
444 g_object_ref(G_OBJECT(action_box));
445 fe_data->action_box = action_box;
446 }
447
448 /** Create all common widgets inside the main window.
449 *
450 * This currentyl means the banner, the title label, the target box and
451 * the action box.
452 *
453 * @param fe cdebconf frontend
454 * @param window the main window
455 */
456 static void create_main_widgets(struct frontend * fe, GtkWidget * window)
457 {
458 GtkWidget * outer_box;
459 GtkWidget * v_mainbox;
460 GtkWidget * h_mainbox;
461
462 outer_box = gtk_vbox_new(FALSE /* don't make children equal */,
463 0 /* padding */);
464 create_banner(fe, outer_box);
465 v_mainbox = gtk_vbox_new(FALSE /* don't make children equal */,
466 0 /* padding */);
467 h_mainbox = gtk_hbox_new(FALSE /* don't make children equal */,
468 0 /* padding */);
469 create_label_title(fe, v_mainbox);
470 create_target_box(fe, v_mainbox);
471 create_action_box(fe, v_mainbox);
472 gtk_box_pack_start(GTK_BOX(h_mainbox), v_mainbox, TRUE /* expand */,
473 TRUE /* fill */, DEFAULT_PADDING * 2);
474 gtk_box_pack_start(GTK_BOX(outer_box), h_mainbox, TRUE /* expand */,
475 TRUE /* fill */, DEFAULT_PADDING);
476 gtk_container_add(GTK_CONTAINER(window), outer_box);
477 }
478
479 /** Handle the closing of the frontend window.
480 *
481 * This will unreference the window from frontend_data and force the
482 * frontend to quit.
483 *
484 * @param fe cdebconf frontend
485 * @param window the main window
486 * @see cdebconf_gtk_force_quit
487 */
488 static void handle_closed_main_window(struct frontend * fe,
489 GtkWidget * window)
490 {
491 struct frontend_data * fe_data = fe->data;
492
493 fe_data->window = NULL;
494 g_object_unref(G_OBJECT(window));
495 cdebconf_gtk_force_quit(fe);
496 }
497
498 /** Create the main window.
499 *
500 * When the main window is destroyed (e.g. by user closing it)
501 * handle_closed_main_window() will be called.
502 *
503 * @param fe cdebconf frontend
504 * @return FALSE in case of errors
505 */
506 gboolean cdebconf_gtk_create_main_window(struct frontend * fe)
507 {
508 struct frontend_data * fe_data = fe->data;
509 GtkWidget * window;
510
511 g_assert(NULL != fe_data);
512 g_assert(NULL == fe_data->window);
513 if (NULL == (window = gtk_window_new(GTK_WINDOW_TOPLEVEL))) {
514 g_critical("gtk_window_new failed.");
515 return FALSE;
516 }
517 gtk_window_set_resizable(GTK_WINDOW(window), TRUE /* resizable */);
518 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
519 gtk_window_set_decorated(GTK_WINDOW(window), TRUE /* resizable */);
520 create_main_widgets(fe, window);
521
522 g_signal_connect_swapped(window, "destroy",
523 G_CALLBACK(handle_closed_main_window), fe);
524
525 g_object_ref(G_OBJECT(window));
526 fe_data->window = window;
527
528 return TRUE;
529 }
530
531 /** Internal structure for keyboard shortcuts handling.
532 *
533 * This will be used to give the necessary data to remove_shortcut().
534 */
535 struct shortcut {
536 /** the main window */
537 GtkWidget * window;
538 /** signal handler */
539 gulong handler_id;
540 };
541
542 /** Remove the keyboard shortcut.
543 *
544 * This is a callback for the "destroy" event of the widget to which was
545 * attached the shortcut.
546 *
547 * @param widget destroyed widget
548 * @param shortcut shortcut data
549 */
550 #if GTK_CHECK_VERSION(3,0,0)
551 static void remove_shortcut(GtkWidget * widget, struct shortcut * shortcut)
552 #else
553 static void remove_shortcut(GtkObject * widget, struct shortcut * shortcut)
554 #endif
555 {
556 g_signal_handler_disconnect(G_OBJECT(shortcut->window),
557 shortcut->handler_id);
558 g_free(shortcut);
559 }
560
561 /* documented in cdebconf_gtk.h */
562 void cdebconf_gtk_add_global_key_handler(struct frontend * fe,
563 GtkWidget * widget,
564 GCallback key_event_handler)
565 {
566 struct frontend_data * fe_data = fe->data;
567 struct shortcut * shortcut;
568
569 shortcut = g_malloc0(sizeof (struct shortcut));
570 shortcut->window = fe_data->window;
571 shortcut->handler_id = g_signal_connect_after(
572 fe_data->window, "key_press_event", key_event_handler, fe);
573 g_signal_connect(G_OBJECT(widget), "destroy",
574 G_CALLBACK(remove_shortcut), shortcut);
575 }
576
577 /** Destroy the main window.
578 *
579 * @param fe cdebconf frontend
580 */
581 void cdebconf_gtk_destroy_main_window(struct frontend * fe)
582 {
583 struct frontend_data * fe_data = fe->data;
584 GtkWidget * window = fe_data->window;
585
586 if (NULL != window) {
587 g_signal_handlers_disconnect_by_func(
588 window, G_CALLBACK(handle_closed_main_window), fe);
589 fe_data->window = NULL;
590 g_object_unref(G_OBJECT(window));
591 gtk_widget_destroy(window);
592 }
593 }
594
595 /* documented in cdebconf_gtk.h */
596 void cdebconf_gtk_center_widget(GtkWidget ** widget, guint horizontal_padding,
597 guint vertical_padding)
598 {
599 GtkWidget * vbox;
600 GtkWidget * hbox;
601
602 vbox = gtk_vbox_new(FALSE /* don't make children equal */,
603 0 /* padding */);
604 hbox = gtk_hbox_new(FALSE /* don't make children equal */,
605 0 /* padding */);
606 gtk_box_pack_start(GTK_BOX(vbox), *widget, TRUE /* expand */,
607 TRUE /* fill */, vertical_padding);
608 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE /* expand */, TRUE /* fill */,
609 horizontal_padding);
610 *widget = hbox;
611 }
612
613 /** Create buttons for the DIY dialog box.
614 *
615 * @param fe cdebconf frontend
616 * @param dialog the dialog in which the buttons will be added
617 * @return the created widget
618 */
619 static GtkWidget * create_dialog_action_box(struct frontend * fe,
620 GtkWidget * dialog)
621 {
622 GtkWidget * action_box;
623 GtkWidget * close_button;
624 char * label;
625
626 action_box = gtk_hbutton_box_new();
627 gtk_button_box_set_layout(GTK_BUTTON_BOX(action_box), GTK_BUTTONBOX_END);
628
629 label = cdebconf_gtk_get_text(fe, "debconf/button-continue", "Continue");
630 close_button = gtk_button_new_with_label(label);
631 g_free(label);
632
633 g_signal_connect_swapped(G_OBJECT(close_button), "clicked",
634 G_CALLBACK(gtk_widget_destroy), dialog);
635
636 gtk_box_pack_end(GTK_BOX(action_box), close_button,
637 TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
638 return action_box;
639 }
640
641 /** Create a label displaying the title for a DIY dialog box.
642 *
643 * @param title the title to display
644 * @return the created widget
645 */
646 static GtkWidget * create_dialog_title_label(const gchar * title)
647 {
648 GtkWidget * label;
649 gchar * markup;
650
651 label = gtk_label_new(NULL /* no text */);
652 gtk_misc_set_alignment(GTK_MISC(label), 0 /* left aligned */,
653 0 /* top aligned */);
654
655 markup = g_strdup_printf("<b>%s</b>", title);
656 gtk_label_set_markup(GTK_LABEL(label), markup);
657 g_free(markup);
658
659 return label;
660 }
661
662 /* documented in cdebconf_gtk.h */
663 gboolean cdebconf_gtk_run_message_dialog(struct frontend * fe,
664 const gchar * title,
665 const gchar * message)
666 {
667 struct frontend_data * fe_data = fe->data;
668 GtkWidget * dialog;
669 GtkWidget * vbox;
670 GtkWidget * frame;
671 GtkWidget * label;
672
673 dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
674 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE /* modal */);
675 gtk_window_set_transient_for(GTK_WINDOW(dialog),
676 GTK_WINDOW(fe_data->window));
677 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE /* not resizable */);
678 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
679 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE /* no decoration */);
680 gtk_container_set_border_width(GTK_CONTAINER(dialog), 0 /* no border */);
681
682 /* Create a wrapped label */
683 label = gtk_label_new(message);
684 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
685
686 vbox = gtk_vbox_new(FALSE /* don't make children equal */,
687 DEFAULT_PADDING);
688 gtk_box_pack_start(GTK_BOX(vbox), create_dialog_title_label(title),
689 FALSE /* don't expand */, FALSE /* don't fill */,
690 0 /* no padding */);
691 gtk_box_pack_start(GTK_BOX(vbox), label,
692 FALSE /* don't expand */, FALSE /* don't fill */,
693 DEFAULT_PADDING);
694 gtk_box_pack_start(GTK_BOX(vbox), gtk_hseparator_new(),
695 FALSE /* don't expand */, FALSE /* don't fill */,
696 0 /* no padding */);
697 gtk_box_pack_start(GTK_BOX(vbox), create_dialog_action_box(fe, dialog),
698 FALSE /* don't expand */, FALSE /* don't fill */,
699 0 /* no padding */);
700 cdebconf_gtk_center_widget(&vbox, DEFAULT_PADDING, DEFAULT_PADDING);
701
702 frame = gtk_frame_new(NULL /* no label */);
703 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
704 gtk_container_add(GTK_CONTAINER(frame), vbox);
705 gtk_container_add(GTK_CONTAINER(dialog), frame);
706
707 gtk_widget_show_all(dialog);
708 return TRUE;
709 }
710
711 /* documented in cdebconf_gtk.h. */
712 void cdebconf_gtk_set_buttons_sensitive(struct frontend * fe,
713 gboolean sensitive)
714 {
715 struct frontend_data * fe_data = fe->data;
716 GList * child;
717
718 child = gtk_container_get_children(GTK_CONTAINER(fe_data->action_box));
719 while (NULL != child) {
720 gtk_widget_set_sensitive(GTK_WIDGET(child->data), sensitive);
721 child = g_list_next(child);
722 }
723 }
724
725 /** Show buttons in the main window.
726 *
727 * @param fe cdebconf frontend
728 */
729 void cdebconf_gtk_show_buttons(struct frontend * fe)
730 {
731 struct frontend_data * fe_data = fe->data;
732
733 gtk_widget_show_all(fe_data->action_box);
734 }
735
736 /* documented in cdebconf_gtk.h */
737 void cdebconf_gtk_add_button(struct frontend * fe, GtkWidget * button)
738 {
739 struct frontend_data * fe_data = fe->data;
740 GtkWidget * action_box = fe_data->action_box;
741 GList * focus_chain;
742
743 gtk_box_pack_start(GTK_BOX(action_box), button,
744 TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
745 gtk_container_get_focus_chain(GTK_CONTAINER(action_box), &focus_chain);
746 focus_chain = g_list_append(focus_chain, button);
747 gtk_container_set_focus_chain(GTK_CONTAINER(action_box), focus_chain);
748 g_list_free(focus_chain);
749 }
750
751 /* documented in cdebconf_gtk.h */
752 void cdebconf_gtk_set_button_secondary(struct frontend * fe,
753 GtkWidget * button,
754 gboolean secondary)
755 {
756 struct frontend_data * fe_data = fe->data;
757
758 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(fe_data->action_box),
759 button, secondary);
760 }
761
762 /** Update the title on top of the frontend window.
763 *
764 * @param fe cdebconf frontend
765 */
766 void cdebconf_gtk_update_frontend_title(struct frontend * fe)
767 {
768 struct frontend_data * fe_data = fe->data;
769 gchar * tmp;
770
771 tmp = g_strdup_printf("<b>%s</b>", fe->title);
772 gtk_label_set_markup(GTK_LABEL(fe_data->title), tmp);
773 g_free(tmp);
774 }
775
776 /** Show the target box.
777 *
778 * @param fe cdebconf frontend
779 * @see frontend_data#target_box
780 */
781 void cdebconf_gtk_show_target_box(struct frontend * fe)
782 {
783 struct frontend_data * fe_data = fe->data;
784
785 gtk_widget_show_all(fe_data->target_box);
786 }
787
788 /** Hide the target box.
789 *
790 * @param fe cdebconf frontend
791 * @see frontend_data#target_box
792 */
793 void cdebconf_gtk_hide_target_box(struct frontend * fe)
794 {
795 struct frontend_data * fe_data = fe->data;
796
797 gtk_widget_hide(fe_data->target_box);
798 }
799
800 /** Remove every widgets inside the target box.
801 *
802 * @param fe cdebconf frontend
803 * @see frontend_data#target_box
804 */
805 void cdebconf_gtk_empty_target_box(struct frontend * fe)
806 {
807 struct frontend_data * fe_data = fe->data;
808
809 gtk_container_forall(GTK_CONTAINER(fe_data->target_box),
810 cdebconf_gtk_callback_widget_destroy, NULL /* no data */);
811 }
812
813 /** Returns the width (in pango units) of the given text.
814 *
815 * @param widget widget where the text will be renderered
816 * @param text text to be rendered
817 * @return width of the text in pango units
818 */
819 gint cdebconf_gtk_get_text_width(GtkWidget * widget, gchar * text)
820 {
821 PangoLayout * layout;
822 gint width;
823
824 layout = gtk_widget_create_pango_layout(widget, text);
825 pango_layout_get_size(layout, &width, NULL /* no height */);
826 g_object_unref(layout);
827
828 return width;
829 }
830
831 /** GtkCallback wrapper for gtk_widget_destroy */
832 void cdebconf_gtk_callback_widget_destroy(GtkWidget *widget, gpointer data)
833 {
834 gtk_widget_destroy(widget);
835 }
836
837 /* vim: et sw=4 si
838 */