Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-5.0/optiondlg.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
optiondlg.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdlib.h>
19
20#include <gtk/gtk.h>
21
22/* utility */
23#include "log.h"
24#include "mem.h"
25#include "string_vector.h"
26
27/* client */
28#include "options.h"
29
30/* client/gui-gtk-5.0 */
31#include "colors.h"
32#include "dialogs.h"
33#include "gui_main.h"
34#include "gui_stuff.h"
35#include "pages.h"
36
37#include "optiondlg.h"
38
39
40/* The option dialog data. */
41struct option_dialog {
42 const struct option_set *poptset; /* The option set. */
43 GtkWidget *shell; /* The main widget. */
44 GtkWidget *notebook; /* The notebook. */
45 GtkWidget **vboxes; /* Category boxes. */
46 int *box_children; /* The number of children for
47 * each category. */
48};
49
50#define SPECLIST_TAG option_dialog
51#define SPECLIST_TYPE struct option_dialog
52#include "speclist.h"
53#define option_dialogs_iterate(pdialog) \
54 TYPED_LIST_ITERATE(struct option_dialog, option_dialogs, pdialog)
55#define option_dialogs_iterate_end LIST_ITERATE_END
56
57/* All option dialog are set on this list. */
58 static struct option_dialog_list *option_dialogs = NULL;
59
60enum {
61 RESPONSE_CANCEL,
62 RESPONSE_OK,
63 RESPONSE_APPLY,
64 RESPONSE_RESET,
65 RESPONSE_REFRESH,
66 RESPONSE_SAVE
67};
68
69 static GtkWidget *opt_popover = NULL;
70
71
72/* Option dialog main functions. */
73static struct option_dialog *
74 option_dialog_get(const struct option_set *poptset);
75static struct option_dialog *
76 option_dialog_new(const char *name, const struct option_set *poptset);
77static void option_dialog_destroy(struct option_dialog *pdialog);
78
79static void option_dialog_reorder_notebook(struct option_dialog *pdialog);
80static inline void option_dialog_foreach(struct option_dialog *pdialog,
81 void (*option_action)
82 (struct option *));
83
84/* Option dialog option-specific functions. */
85static void option_dialog_option_add(struct option_dialog *pdialog,
86 struct option *poption,
87 bool reorder_notebook);
88static void option_dialog_option_remove(struct option_dialog *pdialog,
89 struct option *poption);
90
91static void option_dialog_option_refresh(struct option *poption);
92static void option_dialog_option_reset(struct option *poption);
93static void option_dialog_option_apply(struct option *poption);
94
95
96/************************************************************************/
99 static void option_dialog_reponse_callback(GtkDialog *dialog,
100 gint response_id, gpointer data)
101{
102 struct option_dialog *pdialog = (struct option_dialog *) data;
103
104 switch (response_id) {
105 case RESPONSE_CANCEL:
106 gtk_window_destroy(GTK_WINDOW(dialog));
107 break;
108 case RESPONSE_OK:
109 option_dialog_foreach(pdialog, option_dialog_option_apply);
110 gtk_window_destroy(GTK_WINDOW(dialog));
111 break;
112 case RESPONSE_APPLY:
113 option_dialog_foreach(pdialog, option_dialog_option_apply);
114 break;
115 case RESPONSE_RESET:
116 option_dialog_foreach(pdialog, option_dialog_option_reset);
117 break;
118 case RESPONSE_REFRESH:
119 option_dialog_foreach(pdialog, option_dialog_option_refresh);
120 break;
121 case RESPONSE_SAVE:
122 desired_settable_options_update();
123 options_save(NULL);
124 break;
125 }
126}
127
128/************************************************************************/
131 static void option_dialog_destroy_callback(GtkWidget *object, gpointer data)
132{
133 struct option_dialog *pdialog = (struct option_dialog *) data;
134
135 if (NULL != pdialog->shell) {
136 /* Mark as destroyed, see also option_dialog_destroy(). */
137 pdialog->shell = NULL;
138 option_dialog_destroy(pdialog);
139 }
140}
141
142/************************************************************************/
145 static void option_refresh_callback(GSimpleAction *action, GVariant *parameter,
146 gpointer data)
147{
148 struct option *poption = (struct option *)data;
149 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
150
151 if (NULL != pdialog) {
152 option_dialog_option_refresh(poption);
153 }
154
155 gtk_widget_unparent(opt_popover);
156 g_object_unref(opt_popover);
157}
158
159/************************************************************************/
162 static void option_reset_callback(GSimpleAction *action, GVariant *parameter,
163 gpointer data)
164{
165 struct option *poption = (struct option *) data;
166 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
167
168 if (NULL != pdialog) {
169 option_dialog_option_reset(poption);
170 }
171
172 gtk_widget_unparent(opt_popover);
173 g_object_unref(opt_popover);
174}
175
176/************************************************************************/
179 static void option_apply_callback(GSimpleAction *action, GVariant *parameter,
180 gpointer data)
181{
182 struct option *poption = (struct option *) data;
183 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
184
185 if (NULL != pdialog) {
186 option_dialog_option_apply(poption);
187 }
188
189 gtk_widget_unparent(opt_popover);
190 g_object_unref(opt_popover);
191}
192
193/************************************************************************/
196 static gboolean option_button_press_callback(GtkGestureClick *gesture,
197 int n_press,
198 double x, double y,
199 gpointer data)
200{
201 struct option *poption = (struct option *) data;
202 GtkEventController *controller = GTK_EVENT_CONTROLLER(gesture);
203 GtkWidget *parent = gtk_event_controller_get_widget(controller);
204 GMenu *menu;
205 GActionGroup *group;
206 GSimpleAction *act;
207 GdkRectangle rect = { .x = x, .y = y, .width = 1, .height = 1};
208
209 if (!option_is_changeable(poption)) {
210 return FALSE;
211 }
212
213 group = G_ACTION_GROUP(g_simple_action_group_new());
214
215 menu = g_menu_new();
216
217 act = g_simple_action_new("refresh", NULL);
218 g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act));
219 g_signal_connect(act, "activate", G_CALLBACK(option_refresh_callback), poption);
220 menu_item_append_unref(menu, g_menu_item_new(_("Refresh this option"),
221 "win.refresh"));
222
223 act = g_simple_action_new("unit_reset", NULL);
224 g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act));
225 g_signal_connect(act, "activate", G_CALLBACK(option_reset_callback), poption);
226 menu_item_append_unref(menu, g_menu_item_new(_("Reset this option"),
227 "win.unit_reset"));
228
229 act = g_simple_action_new("units_apply", NULL);
230 g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act));
231 g_signal_connect(act, "activate", G_CALLBACK(option_apply_callback), poption);
232 menu_item_append_unref(menu,
233 g_menu_item_new(_("Apply the changes for this option"),
234 "win.units_apply"));
235
236 opt_popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
237 g_object_ref(opt_popover);
238 gtk_widget_insert_action_group(opt_popover, "win", group);
239
240 gtk_widget_set_parent(opt_popover, parent);
241 gtk_popover_set_pointing_to(GTK_POPOVER(opt_popover), &rect);
242
243 gtk_popover_popup(GTK_POPOVER(opt_popover));
244
245 return TRUE;
246}
247
248/************************************************************************/
251static struct option_dialog *
252 option_dialog_get(const struct option_set *poptset)
253{
254 if (NULL != option_dialogs) {
255 option_dialogs_iterate(pdialog) {
256 if (pdialog->poptset == poptset) {
257 return pdialog;
258 }
259 } option_dialogs_iterate_end;
260 }
261 return NULL;
262}
263
264/************************************************************************/
267 static void option_color_destroy_notify(gpointer data)
268{
269 GdkRGBA *color = (GdkRGBA *) data;
270
271 if (NULL != color) {
272 gdk_rgba_free(color);
273 }
274}
275
276/************************************************************************/
279 static void option_color_set_button_color(GtkButton *button,
280 const GdkRGBA *new_color)
281{
282 GdkRGBA *current_color = g_object_get_data(G_OBJECT(button), "color");
283 GtkWidget *child;
284
285 if (NULL == new_color) {
286 if (NULL != current_color) {
287 g_object_set_data(G_OBJECT(button), "color", NULL);
288 gtk_button_set_child(button, NULL);
289 }
290 } else {
291 GdkPixbuf *pixbuf;
292
293 /* Apply the new color. */
294 if (NULL != current_color) {
295 /* We already have a GdkRGBA pointer. */
296 *current_color = *new_color;
297 } else {
298 /* We need to make a GdkRGBA pointer. */
299 current_color = gdk_rgba_copy(new_color);
300 g_object_set_data_full(G_OBJECT(button), "color", current_color,
301 option_color_destroy_notify);
302 }
303 gtk_button_set_child(button, nullptr);
304
305 /* Update the button. */
306 {
307 cairo_surface_t *surface = cairo_image_surface_create(
308 CAIRO_FORMAT_RGB24, 16, 16);
309 cairo_t *cr = cairo_create(surface);
310
311 gdk_cairo_set_source_rgba(cr, current_color);
312 cairo_paint(cr);
313 cairo_destroy(cr);
314 pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, 16, 16);
315 cairo_surface_destroy(surface);
316 }
317
318 child = gtk_image_new_from_pixbuf(pixbuf);
319 gtk_button_set_child(GTK_BUTTON(button), child);
320 gtk_widget_set_visible(child, TRUE);
321 g_object_unref(G_OBJECT(pixbuf));
322 }
323}
324
325/************************************************************************/
328 static void color_selector_response_callback(GtkDialog *dialog,
329 gint res, gpointer data)
330{
331 if (res == GTK_RESPONSE_REJECT) {
332 /* Clears the current color. */
333 option_color_set_button_color(GTK_BUTTON(data), NULL);
334 } else if (res == GTK_RESPONSE_OK) {
335 /* Apply the new color. */
336 GtkColorChooser *chooser =
337 GTK_COLOR_CHOOSER(g_object_get_data(G_OBJECT(dialog), "chooser"));
338 GdkRGBA new_color;
339
340 gtk_color_chooser_get_rgba(chooser, &new_color);
341 option_color_set_button_color(GTK_BUTTON(data), &new_color);
342 }
343
344 gtk_window_destroy(GTK_WINDOW(dialog));
345}
346
347/************************************************************************/
350 static void option_color_select_callback(GtkButton *button, gpointer data)
351{
352 GtkWidget *dialog, *chooser;
353 GdkRGBA *current_color = g_object_get_data(G_OBJECT(button), "color");
354
355 dialog = gtk_dialog_new_with_buttons(_("Select a color"), NULL,
356 GTK_DIALOG_MODAL,
357 _("_Cancel"), GTK_RESPONSE_CANCEL,
358 _("C_lear"), GTK_RESPONSE_REJECT,
359 _("_OK"), GTK_RESPONSE_OK, NULL);
360 setup_dialog(dialog, toplevel);
361 g_signal_connect(dialog, "response",
362 G_CALLBACK(color_selector_response_callback), button);
363
364 chooser = gtk_color_chooser_widget_new();
365 g_object_set_data(G_OBJECT(dialog), "chooser", chooser);
366 gtk_box_insert_child_after(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
367 chooser, nullptr);
368 if (current_color != nullptr) {
369 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(chooser), current_color);
370 }
371
372 gtk_widget_set_visible(dialog, TRUE);
373}
374
375/************************************************************************/
378static struct option_dialog *
379 option_dialog_new(const char *name, const struct option_set *poptset)
380{
381 struct option_dialog *pdialog;
382 const int CATEGORY_NUM = optset_category_number(poptset);
383
384 /* Create the dialog structure. */
385 pdialog = fc_malloc(sizeof(*pdialog));
386 pdialog->poptset = poptset;
387 pdialog->shell = gtk_dialog_new_with_buttons(name, NULL, 0,
388 _("_Cancel"), RESPONSE_CANCEL,
389 _("_Save"), RESPONSE_SAVE,
390 _("_Refresh"), RESPONSE_REFRESH,
391 _("Reset"), RESPONSE_RESET,
392 _("_Apply"), RESPONSE_APPLY,
393 _("_OK"), RESPONSE_OK, NULL);
394 pdialog->notebook = gtk_notebook_new();
395 pdialog->vboxes = fc_calloc(CATEGORY_NUM, sizeof(*pdialog->vboxes));
396 pdialog->box_children = fc_calloc(CATEGORY_NUM,
397 sizeof(*pdialog->box_children));
398
399 /* Append to the option dialog list. */
400 if (NULL == option_dialogs) {
401 option_dialogs = option_dialog_list_new();
402 }
403 option_dialog_list_append(option_dialogs, pdialog);
404
405 /* Shell */
406 setup_dialog(pdialog->shell, toplevel);
407 gtk_window_set_default_size(GTK_WINDOW(pdialog->shell), -1, 480);
408 g_signal_connect(pdialog->shell, "response",
409 G_CALLBACK(option_dialog_reponse_callback), pdialog);
410 g_signal_connect(pdialog->shell, "destroy",
411 G_CALLBACK(option_dialog_destroy_callback), pdialog);
412
413 gtk_box_insert_child_after(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(pdialog->shell))),
414 pdialog->notebook, NULL);
415
416 /* Add the options. */
417 options_iterate(poptset, poption) {
418 option_dialog_option_add(pdialog, poption, FALSE);
419 } options_iterate_end;
420
421 option_dialog_reorder_notebook(pdialog);
422
423 /* Show the widgets. */
424 gtk_widget_set_visible(pdialog->shell, TRUE);
425
426 return pdialog;
427}
428
429/************************************************************************/
432 static void option_dialog_destroy(struct option_dialog *pdialog)
433{
434 GtkWidget *shell = pdialog->shell;
435
436 if (NULL != option_dialogs) {
437 option_dialog_list_remove(option_dialogs, pdialog);
438 }
439
440 options_iterate(pdialog->poptset, poption) {
441 option_set_gui_data(poption, NULL);
442 } options_iterate_end;
443
444 if (NULL != shell) {
445 /* Maybe already destroyed, see also option_dialog_destroy_callback(). */
446 pdialog->shell = NULL;
447 gtk_window_destroy(GTK_WINDOW(shell));
448 }
449
450 free(pdialog->vboxes);
451 free(pdialog->box_children);
452 free(pdialog);
453}
454
455/************************************************************************/
458 static int option_dialog_pages_sort_func(const void *w1, const void *w2)
459{
460 GObject *obj1 = G_OBJECT(*(GtkWidget **) w1);
461 GObject *obj2 = G_OBJECT(*(GtkWidget **) w2);
462
463 return (GPOINTER_TO_INT(g_object_get_data(obj1, "category"))
464 - GPOINTER_TO_INT(g_object_get_data(obj2, "category")));
465}
466
467/************************************************************************/
470 static void option_dialog_reorder_notebook(struct option_dialog *pdialog)
471{
472 GtkNotebook *notebook = GTK_NOTEBOOK(pdialog->notebook);
473 const int pages_num = gtk_notebook_get_n_pages(notebook);
474
475 if (0 < pages_num) {
476 GtkWidget *pages[pages_num];
477 int i;
478
479 for (i = 0; i < pages_num; i++) {
480 pages[i] = gtk_notebook_get_nth_page(notebook, i);
481 }
482 qsort(pages, pages_num, sizeof(*pages), option_dialog_pages_sort_func);
483 for (i = 0; i < pages_num; i++) {
484 gtk_notebook_reorder_child(notebook, pages[i], i);
485 }
486 }
487}
488
489/************************************************************************/
492 static inline void option_dialog_foreach(struct option_dialog *pdialog,
493 void (*option_action)
494 (struct option *))
495{
496 fc_assert_ret(NULL != pdialog);
497
498 options_iterate(pdialog->poptset, poption) {
499 option_action(poption);
500 } options_iterate_end;
501}
502
503/************************************************************************/
506 static void option_dialog_option_add(struct option_dialog *pdialog,
507 struct option *poption,
508 bool reorder_notebook)
509{
510 const int category = option_category(poption);
511 GtkWidget *main_hbox, *label, *w = NULL;
512 int main_col = 0;
513 GtkGesture *gesture;
514 GtkEventController *controller;
515
516 fc_assert(NULL == option_get_gui_data(poption));
517
518 /* Add category if needed. */
519 if (NULL == pdialog->vboxes[category]) {
520 GtkWidget *sw;
521
522 sw = gtk_scrolled_window_new();
523 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
524 GTK_POLICY_NEVER,
525 GTK_POLICY_AUTOMATIC);
526 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(sw), TRUE);
527 g_object_set_data(G_OBJECT(sw), "category", GINT_TO_POINTER(category));
528 gtk_notebook_append_page(GTK_NOTEBOOK(pdialog->notebook), sw,
529 gtk_label_new_with_mnemonic
530 (option_category_name(poption)));
531
532 if (reorder_notebook) {
533 option_dialog_reorder_notebook(pdialog);
534 }
535
536 pdialog->vboxes[category] = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
537 gtk_widget_set_margin_bottom(pdialog->vboxes[category], 8);
538 gtk_widget_set_margin_end(pdialog->vboxes[category], 8);
539 gtk_widget_set_margin_start(pdialog->vboxes[category], 8);
540 gtk_widget_set_margin_top(pdialog->vboxes[category], 8);
541 gtk_widget_set_hexpand(pdialog->vboxes[category], TRUE);
542 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw),
543 pdialog->vboxes[category]);
544
545 gtk_widget_set_visible(sw, TRUE);
546 }
547 pdialog->box_children[category]++;
548
549 main_hbox = gtk_grid_new();
550 label = gtk_label_new(option_description(poption));
551 gtk_widget_set_margin_bottom(label, 2);
552 gtk_widget_set_margin_end(label, 2);
553 gtk_widget_set_margin_start(label, 2);
554 gtk_widget_set_margin_top(label, 2);
555 gtk_grid_attach(GTK_GRID(main_hbox), label, main_col++, 0, 1, 1);
556 gtk_widget_set_tooltip_text(main_hbox, option_help_text(poption));
557 gesture = gtk_gesture_click_new();
558 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 3);
559 controller = GTK_EVENT_CONTROLLER(gesture);
560 g_signal_connect(controller, "pressed",
561 G_CALLBACK(option_button_press_callback), poption);
562 gtk_widget_add_controller(main_hbox, controller);
563 gtk_box_append(GTK_BOX(pdialog->vboxes[category]), main_hbox);
564
565 switch (option_type(poption)) {
566 case OT_BOOLEAN:
567 w = gtk_check_button_new();
568 break;
569
570 case OT_INTEGER:
571 {
572 int min = option_int_min(poption), max = option_int_max(poption);
573
574 w = gtk_spin_button_new_with_range(min, max, MAX((max - min) / 50, 1));
575 }
576 break;
577
578 case OT_STRING:
579 {
580 const struct strvec *values = option_str_values(poption);
581
582 if (NULL != values) {
583 w = gtk_combo_box_text_new_with_entry();
584 strvec_iterate(values, value) {
585 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(w), value);
586 } strvec_iterate_end;
587 } else {
588 w = gtk_entry_new();
589 }
590 }
591 break;
592
593 case OT_ENUM:
594 {
595 int i;
596 const char *str;
597 GtkListStore *model;
598 GtkCellRenderer *renderer;
599 GtkTreeIter iter;
600
601 /* 0: enum index, 1: translated enum name. */
602 model = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
603 w = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
604 g_object_unref(model);
605
606 renderer = gtk_cell_renderer_text_new();
607 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(w), renderer, FALSE);
608 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(w), renderer,
609 "text", 1, NULL);
610 for (i = 0; (str = option_enum_int_to_str(poption, i)); i++) {
611 gtk_list_store_append(model, &iter);
612 gtk_list_store_set(model, &iter, 0, i, 1, _(str), -1);
613 }
614 }
615 break;
616
617 case OT_BITWISE:
618 {
619 GList *list = NULL;
620 GtkWidget *grid, *check;
621 const struct strvec *values = option_bitwise_values(poption);
622 int i;
623
624 w = gtk_frame_new(NULL);
625 grid = gtk_grid_new();
626 gtk_grid_set_column_spacing(GTK_GRID(grid), 4);
627 gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
628 gtk_frame_set_child(GTK_FRAME(w), grid);
629 for (i = 0; i < strvec_size(values); i++) {
630 check = gtk_check_button_new();
631 gtk_grid_attach(GTK_GRID(grid), check, 0, i, 1, 1);
632 label = gtk_label_new(_(strvec_get(values, i)));
633 gtk_grid_attach(GTK_GRID(grid), label, 1, i, 1, 1);
634 list = g_list_append(list, check);
635 }
636 g_object_set_data_full(G_OBJECT(w), "check_buttons", list,
637 (GDestroyNotify) g_list_free);
638 }
639 break;
640
641 case OT_FONT:
642 w = gtk_font_button_new();
643 g_object_set(G_OBJECT(w), "use-font", TRUE, NULL);
644 break;
645
646 case OT_COLOR:
647 {
648 GtkWidget *button;
649 int grid_col = 0;
650
651 w = gtk_grid_new();
652 gtk_grid_set_column_spacing(GTK_GRID(w), 4);
653 gtk_grid_set_row_homogeneous(GTK_GRID(w), TRUE);
654
655 /* Foreground color selector button. */
656 button = gtk_button_new();
657 gtk_grid_attach(GTK_GRID(w), button, grid_col++, 0, 1, 1);
658 gtk_widget_set_tooltip_text(GTK_WIDGET(button),
659 _("Select the text color"));
660 g_object_set_data(G_OBJECT(w), "fg_button", button);
661 g_signal_connect(button, "clicked",
662 G_CALLBACK(option_color_select_callback), NULL);
663
664 /* Background color selector button. */
665 button = gtk_button_new();
666 gtk_grid_attach(GTK_GRID(w), button, grid_col++, 0, 1, 1);
667 gtk_widget_set_tooltip_text(GTK_WIDGET(button),
668 _("Select the background color"));
669 g_object_set_data(G_OBJECT(w), "bg_button", button);
670 g_signal_connect(button, "clicked",
671 G_CALLBACK(option_color_select_callback), NULL);
672 }
673 break;
674
675 case OT_VIDEO_MODE:
676 log_error("Option type %s (%d) not supported yet.",
677 option_type_name(option_type(poption)),
678 option_type(poption));
679 break;
680 }
681
682 option_set_gui_data(poption, w);
683 if (NULL == w) {
684 log_error("Failed to create a widget for option %d \"%s\".",
685 option_number(poption), option_name(poption));
686 } else {
687 g_object_set_data(G_OBJECT(w), "main_widget", main_hbox);
688 g_object_set_data(G_OBJECT(w), "parent_of_main", pdialog->vboxes[category]);
689 gtk_widget_set_hexpand(w, TRUE);
690 gtk_widget_set_halign(w, GTK_ALIGN_END);
691 gtk_grid_attach(GTK_GRID(main_hbox), w, main_col++, 0, 1, 1);
692 }
693
694 gtk_widget_set_visible(main_hbox, TRUE);
695
696 /* Set as current value. */
697 option_dialog_option_refresh(poption);
698}
699
700/************************************************************************/
703 static void option_dialog_option_remove(struct option_dialog *pdialog,
704 struct option *poption)
705{
706 GObject *object = G_OBJECT(option_get_gui_data(poption));
707
708 if (NULL != object) {
709 const int category = option_category(poption);
710
711 option_set_gui_data(poption, NULL);
712 gtk_box_remove(GTK_BOX(g_object_get_data(object, "parent_of_main")),
713 GTK_WIDGET(g_object_get_data(object, "main_widget")));
714
715 /* Remove category if needed. */
716 if (0 == --pdialog->box_children[category]) {
717 gtk_notebook_remove_page(GTK_NOTEBOOK(pdialog->notebook), category);
718 pdialog->vboxes[category] = NULL;
719 }
720 }
721}
722
723/************************************************************************/
726 static inline void option_dialog_option_bool_set(struct option *poption,
727 bool value)
728{
729 gtk_check_button_set_active(GTK_CHECK_BUTTON
730 (option_get_gui_data(poption)),
731 value);
732}
733
734/************************************************************************/
737 static inline void option_dialog_option_int_set(struct option *poption,
738 int value)
739{
740 gtk_spin_button_set_value(GTK_SPIN_BUTTON(option_get_gui_data(poption)),
741 value);
742}
743
744/************************************************************************/
747 static inline void option_dialog_option_str_set(struct option *poption,
748 const char *string)
749{
750 GtkWidget *wdg = option_get_gui_data(poption);
751
752 if (NULL != option_str_values(poption)) {
753 GtkWidget *child = gtk_combo_box_get_child(GTK_COMBO_BOX(wdg));
754
755 gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(child)), string, -1);
756 } else {
757 gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(wdg)), string, -1);
758 }
759}
760
761/************************************************************************/
764 static inline void option_dialog_option_enum_set(struct option *poption,
765 int value)
766{
767 GtkComboBox *combo = GTK_COMBO_BOX(option_get_gui_data(poption));
768 GtkTreeModel *model = gtk_combo_box_get_model(combo);
769 GtkTreeIter iter;
770 int i;
771
772 if (gtk_tree_model_get_iter_first(model, &iter)) {
773 do {
774 gtk_tree_model_get(model, &iter, 0, &i, -1);
775 if (i == value) {
776 gtk_combo_box_set_active_iter(combo, &iter);
777 return;
778 }
779 } while (gtk_tree_model_iter_next(model, &iter));
780 }
781
782 log_error("Didn't find the value %d for option \"%s\" (nb %d).",
783 value, option_name(poption), option_number(poption));
784}
785
786/************************************************************************/
789 static inline void option_dialog_option_bitwise_set(struct option *poption,
790 unsigned value)
791{
792 GObject *data = option_get_gui_data(poption);
793 GList *iter = g_object_get_data(data, "check_buttons");
794 int bit;
795
796 for (bit = 0; NULL != iter; iter = g_list_next(iter), bit++) {
797 gtk_check_button_set_active(GTK_CHECK_BUTTON(iter->data),
798 value & (1 << bit));
799 }
800}
801
802/************************************************************************/
805 static inline void option_dialog_option_font_set(struct option *poption,
806 const char *font)
807{
808 gtk_font_chooser_set_font(GTK_FONT_CHOOSER
809 (option_get_gui_data(poption)), font);
810}
811
812/************************************************************************/
815 static inline void option_dialog_option_color_set(struct option *poption,
816 struct ft_color color)
817{
818 GtkWidget *w = option_get_gui_data(poption);
819 GdkRGBA gdk_color;
820
821 /* Update the foreground button. */
822 if (NULL != color.foreground
823 && '\0' != color.foreground[0]
824 && gdk_rgba_parse(&gdk_color, color.foreground)) {
825 option_color_set_button_color(g_object_get_data(G_OBJECT(w),
826 "fg_button"),
827 &gdk_color);
828 } else {
829 option_color_set_button_color(g_object_get_data(G_OBJECT(w),
830 "fg_button"), NULL);
831 }
832
833 /* Update the background button. */
834 if (NULL != color.background
835 && '\0' != color.background[0]
836 && gdk_rgba_parse(&gdk_color, color.background)) {
837 option_color_set_button_color(g_object_get_data(G_OBJECT(w),
838 "bg_button"),
839 &gdk_color);
840 } else {
841 option_color_set_button_color(g_object_get_data(G_OBJECT(w),
842 "bg_button"), NULL);
843 }
844}
845
846/************************************************************************/
849 static void option_dialog_option_refresh(struct option *poption)
850{
851 switch (option_type(poption)) {
852 case OT_BOOLEAN:
853 option_dialog_option_bool_set(poption, option_bool_get(poption));
854 break;
855 case OT_INTEGER:
856 option_dialog_option_int_set(poption, option_int_get(poption));
857 break;
858 case OT_STRING:
859 option_dialog_option_str_set(poption, option_str_get(poption));
860 break;
861 case OT_ENUM:
862 option_dialog_option_enum_set(poption, option_enum_get_int(poption));
863 break;
864 case OT_BITWISE:
865 option_dialog_option_bitwise_set(poption, option_bitwise_get(poption));
866 break;
867 case OT_FONT:
868 option_dialog_option_font_set(poption, option_font_get(poption));
869 break;
870 case OT_COLOR:
871 option_dialog_option_color_set(poption, option_color_get(poption));
872 break;
873 case OT_VIDEO_MODE:
874 log_error("Option type %s (%d) not supported yet.",
875 option_type_name(option_type(poption)),
876 option_type(poption));
877 break;
878 }
879
880 gtk_widget_set_sensitive(option_get_gui_data(poption),
881 option_is_changeable(poption));
882}
883
884/************************************************************************/
887 static void option_dialog_option_reset(struct option *poption)
888{
889 switch (option_type(poption)) {
890 case OT_BOOLEAN:
891 option_dialog_option_bool_set(poption, option_bool_def(poption));
892 break;
893 case OT_INTEGER:
894 option_dialog_option_int_set(poption, option_int_def(poption));
895 break;
896 case OT_STRING:
897 option_dialog_option_str_set(poption, option_str_def(poption));
898 break;
899 case OT_ENUM:
900 option_dialog_option_enum_set(poption, option_enum_def_int(poption));
901 break;
902 case OT_BITWISE:
903 option_dialog_option_bitwise_set(poption, option_bitwise_def(poption));
904 break;
905 case OT_FONT:
906 option_dialog_option_font_set(poption, option_font_def(poption));
907 break;
908 case OT_COLOR:
909 option_dialog_option_color_set(poption, option_color_def(poption));
910 break;
911 case OT_VIDEO_MODE:
912 log_error("Option type %s (%d) not supported yet.",
913 option_type_name(option_type(poption)),
914 option_type(poption));
915 break;
916 }
917}
918
919/************************************************************************/
922 static void option_dialog_option_apply(struct option *poption)
923{
924 GtkWidget *w = GTK_WIDGET(option_get_gui_data(poption));
925
926 switch (option_type(poption)) {
927 case OT_BOOLEAN:
928 (void) option_bool_set(poption, gtk_check_button_get_active
929 (GTK_CHECK_BUTTON(w)));
930 break;
931
932 case OT_INTEGER:
933 (void) option_int_set(poption, gtk_spin_button_get_value_as_int
934 (GTK_SPIN_BUTTON(w)));
935 break;
936
937 case OT_STRING:
938 if (NULL != option_str_values(poption)) {
939 (void) option_str_set(poption,
940 gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(w)));
941 } else {
942 (void) option_str_set(poption, gtk_entry_buffer_get_text(
943 gtk_entry_get_buffer(GTK_ENTRY(w))));
944 }
945 break;
946
947 case OT_ENUM:
948 {
949 GtkTreeIter iter;
950 int value;
951
952 if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w), &iter)) {
953 break;
954 }
955
956 gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(w)),
957 &iter, 0, &value, -1);
958 (void) option_enum_set_int(poption, value);
959 }
960 break;
961
962 case OT_BITWISE:
963 {
964 GList *iter = g_object_get_data(G_OBJECT(w), "check_buttons");
965 unsigned value = 0;
966 int bit;
967
968 for (bit = 0; NULL != iter; iter = g_list_next(iter), bit++) {
969 if (gtk_check_button_get_active(GTK_CHECK_BUTTON(iter->data))) {
970 value |= 1 << bit;
971 }
972 }
973 (void) option_bitwise_set(poption, value);
974 }
975 break;
976
977 case OT_FONT:
978 (void) option_font_set(poption, gtk_font_chooser_get_font
979 (GTK_FONT_CHOOSER(w)));
980 break;
981
982 case OT_COLOR:
983 {
984 gchar *fg_color_text = NULL, *bg_color_text = NULL;
985 GObject *button;
986 GdkRGBA *color;
987
988 /* Get foreground color. */
989 button = g_object_get_data(G_OBJECT(w), "fg_button");
990 color = g_object_get_data(button, "color");
991 if (color) fg_color_text = gdk_rgba_to_string(color);
992
993 /* Get background color. */
994 button = g_object_get_data(G_OBJECT(w), "bg_button");
995 color = g_object_get_data(button, "color");
996 if (color) bg_color_text = gdk_rgba_to_string(color);
997
998 (void) option_color_set(poption,
999 ft_color_construct(fg_color_text, bg_color_text));
1000 g_free(fg_color_text);
1001 g_free(bg_color_text);
1002 }
1003 break;
1004
1005 case OT_VIDEO_MODE:
1006 log_error("Option type %s (%d) not supported yet.",
1007 option_type_name(option_type(poption)),
1008 option_type(poption));
1009 break;
1010 }
1011}
1012
1013/************************************************************************/
1016 void option_dialog_popup(const char *name, const struct option_set *poptset)
1017{
1018 struct option_dialog *pdialog = option_dialog_get(poptset);
1019
1020 if (NULL != pdialog) {
1021 option_dialog_foreach(pdialog, option_dialog_option_refresh);
1022 } else {
1023 (void) option_dialog_new(name, poptset);
1024 }
1025}
1026
1027/************************************************************************/
1030 void option_dialog_popdown(const struct option_set *poptset)
1031{
1032 struct option_dialog *pdialog = option_dialog_get(poptset);
1033
1034 if (NULL != pdialog) {
1035 option_dialog_destroy(pdialog);
1036 }
1037}
1038
1039/************************************************************************/
1043 static void option_gui_update_extra(struct option *poption)
1044{
1045 if (option_optset(poption) == server_optset) {
1046 if (strcmp(option_name(poption), "aifill") == 0) {
1047 ai_fill_changed_by_server(option_int_get(poption));
1048 } else if (strcmp(option_name(poption), "nationset") == 0) {
1049 nationset_sync_to_server(option_str_get(poption));
1050 }
1051 }
1052}
1053
1054/************************************************************************/
1057 void option_gui_update(struct option *poption)
1058{
1059 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
1060
1061 if (NULL != pdialog) {
1062 option_dialog_option_refresh(poption);
1063 }
1064
1065 option_gui_update_extra(poption);
1066}
1067
1068/************************************************************************/
1071 void option_gui_add(struct option *poption)
1072{
1073 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
1074
1075 if (NULL != pdialog) {
1076 option_dialog_option_add(pdialog, poption, TRUE);
1077 }
1078
1079 option_gui_update_extra(poption);
1080}
1081
1082/************************************************************************/
1085 void option_gui_remove(struct option *poption)
1086{
1087 struct option_dialog *pdialog = option_dialog_get(option_optset(poption));
1088
1089 if (NULL != pdialog) {
1090 option_dialog_option_remove(pdialog, poption);
1091 }
1092}
str
#define str
Definition astring.c:76
list
static struct fc_sockaddr_list * list
Definition clinet.c:102
incite_cost
char * incite_cost
Definition comments.c:74
_
#define _(String)
Definition fcintl.h:67
ft_color_construct
static struct ft_color ft_color_construct(const char *foreground, const char *background)
Definition featured_text.h:149
nationset_sync_to_server
void nationset_sync_to_server(const char *nationset)
Definition dialogs.c:801
toplevel
GtkWidget * toplevel
Definition gui_main.c:125
setup_dialog
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
shell
static struct gui_dialog * shell
Definition messagedlg.c:39
option_dialog_destroy_callback
static void option_dialog_destroy_callback(GtkWidget *object, gpointer data)
Definition optiondlg.c:129
color_selector_response_callback
static void color_selector_response_callback(GtkDialog *dialog, gint res, gpointer data)
Definition optiondlg.c:299
option_dialog_get
static struct option_dialog * option_dialog_get(const struct option_set *poptset)
Definition optiondlg.c:221
option_gui_remove
void option_gui_remove(struct option *poption)
Definition optiondlg.c:1041
option_dialog_option_refresh
static void option_dialog_option_refresh(struct option *poption)
Definition optiondlg.c:806
option_dialog_popdown
void option_dialog_popdown(const struct option_set *poptset)
Definition optiondlg.c:986
option_dialog_option_color_set
static void option_dialog_option_color_set(struct option *poption, struct ft_color color)
Definition optiondlg.c:772
option_color_set_button_color
static void option_color_set_button_color(GtkButton *button, const GdkRGBA *new_color)
Definition optiondlg.c:248
RESPONSE_SAVE
@ RESPONSE_SAVE
Definition optiondlg.c:66
RESPONSE_APPLY
@ RESPONSE_APPLY
Definition optiondlg.c:63
RESPONSE_CANCEL
@ RESPONSE_CANCEL
Definition optiondlg.c:61
RESPONSE_OK
@ RESPONSE_OK
Definition optiondlg.c:62
RESPONSE_RESET
@ RESPONSE_RESET
Definition optiondlg.c:64
RESPONSE_REFRESH
@ RESPONSE_REFRESH
Definition optiondlg.c:65
option_dialog_option_add
static void option_dialog_option_add(struct option_dialog *pdialog, struct option *poption, bool reorder_notebook)
Definition optiondlg.c:479
option_dialogs_iterate_end
#define option_dialogs_iterate_end
Definition optiondlg.c:55
option_dialog_option_bitwise_set
static void option_dialog_option_bitwise_set(struct option *poption, unsigned value)
Definition optiondlg.c:746
option_dialog_destroy
static void option_dialog_destroy(struct option_dialog *pdialog)
Definition optiondlg.c:405
option_dialog_option_bool_set
static void option_dialog_option_bool_set(struct option *poption, bool value)
Definition optiondlg.c:686
option_dialog_option_remove
static void option_dialog_option_remove(struct option_dialog *pdialog, struct option *poption)
Definition optiondlg.c:664
option_gui_add
void option_gui_add(struct option *poption)
Definition optiondlg.c:1027
option_color_select_callback
static void option_color_select_callback(GtkButton *button, gpointer data)
Definition optiondlg.c:321
option_dialog_option_int_set
static void option_dialog_option_int_set(struct option *poption, int value)
Definition optiondlg.c:697
option_dialog_reponse_callback
static void option_dialog_reponse_callback(GtkDialog *dialog, gint response_id, gpointer data)
Definition optiondlg.c:97
option_dialogs_iterate
#define option_dialogs_iterate(pdialog)
Definition optiondlg.c:53
option_gui_update_extra
static void option_gui_update_extra(struct option *poption)
Definition optiondlg.c:999
option_apply_callback
static void option_apply_callback(GtkMenuItem *menuitem, gpointer data)
Definition optiondlg.c:169
option_color_destroy_notify
static void option_color_destroy_notify(gpointer data)
Definition optiondlg.c:236
option_dialog_option_font_set
static void option_dialog_option_font_set(struct option *poption, const char *font)
Definition optiondlg.c:762
option_gui_update
void option_gui_update(struct option *poption)
Definition optiondlg.c:1013
option_dialog_pages_sort_func
static int option_dialog_pages_sort_func(const void *w1, const void *w2)
Definition optiondlg.c:431
option_dialog_popup
void option_dialog_popup(const char *name, const struct option_set *poptset)
Definition optiondlg.c:972
option_dialog_reorder_notebook
static void option_dialog_reorder_notebook(struct option_dialog *pdialog)
Definition optiondlg.c:443
option_dialog_option_apply
static void option_dialog_option_apply(struct option *poption)
Definition optiondlg.c:879
option_button_press_callback
static gboolean option_button_press_callback(GtkWidget *widget, GdkEventButton *event, gpointer data)
Definition optiondlg.c:182
option_dialog_option_reset
static void option_dialog_option_reset(struct option *poption)
Definition optiondlg.c:844
option_reset_callback
static void option_reset_callback(GtkMenuItem *menuitem, gpointer data)
Definition optiondlg.c:156
option_dialog_foreach
static void option_dialog_foreach(struct option_dialog *pdialog, void(*option_action)(struct option *))
Definition optiondlg.c:465
option_dialog_option_str_set
static void option_dialog_option_str_set(struct option *poption, const char *string)
Definition optiondlg.c:707
option_dialogs
static struct option_dialog_list * option_dialogs
Definition optiondlg.c:58
option_dialog_option_enum_set
static void option_dialog_option_enum_set(struct option *poption, int value)
Definition optiondlg.c:721
option_refresh_callback
static void option_refresh_callback(GtkMenuItem *menuitem, gpointer data)
Definition optiondlg.c:143
ai_fill_changed_by_server
void ai_fill_changed_by_server(int aifill)
Definition pages.c:1637
menu_item_append_unref
#define menu_item_append_unref(menu, item)
Definition gui_stuff.h:149
opt_popover
static GtkWidget * opt_popover
Definition optiondlg.c:69
gui_stuff.h
option_dialog_new
static struct option_dialog * option_dialog_new(void)
Definition optiondlg.c:788
colors.h
dialogs.h
gui_main.h
optiondlg.h
pages.h
name
const char * name
Definition inputfile.c:127
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
option_str_values
const struct strvec * option_str_values(const struct option *poption)
Definition options.c:950
option_bitwise_def
unsigned option_bitwise_def(const struct option *poption)
Definition options.c:1120
option_int_min
int option_int_min(const struct option *poption)
Definition options.c:891
option_bool_def
bool option_bool_def(const struct option *poption)
Definition options.c:843
option_font_def
const char * option_font_def(const struct option *poption)
Definition options.c:1187
server_optset
const struct option_set * server_optset
Definition options.c:4281
option_description
const char * option_description(const struct option *poption)
Definition options.c:673
option_help_text
const char * option_help_text(const struct option *poption)
Definition options.c:683
option_color_get
struct ft_color option_color_get(const struct option *poption)
Definition options.c:1225
option_enum_get_int
int option_enum_get_int(const struct option *poption)
Definition options.c:1015
option_number
int option_number(const struct option *poption)
Definition options.c:653
option_category
int option_category(const struct option *poption)
Definition options.c:703
option_str_set
bool option_str_set(struct option *poption, const char *str)
Definition options.c:961
option_name
const char * option_name(const struct option *poption)
Definition options.c:663
option_int_get
int option_int_get(const struct option *poption)
Definition options.c:869
option_color_set
bool option_color_set(struct option *poption, struct ft_color color)
Definition options.c:1248
option_bool_set
bool option_bool_set(struct option *poption, bool val)
Definition options.c:854
option_is_changeable
bool option_is_changeable(const struct option *poption)
Definition options.c:724
option_set_gui_data
void option_set_gui_data(struct option *poption, void *data)
Definition options.c:802
option_str_get
const char * option_str_get(const struct option *poption)
Definition options.c:928
option_get_gui_data
void * option_get_gui_data(const struct option *poption)
Definition options.c:812
option_enum_set_int
bool option_enum_set_int(struct option *poption, int val)
Definition options.c:1075
option_bool_get
bool option_bool_get(const struct option *poption)
Definition options.c:832
optset_category_number
int optset_category_number(const struct option_set *poptset)
Definition options.c:492
option_type
enum option_type option_type(const struct option *poption)
Definition options.c:693
option_enum_int_to_str
const char * option_enum_int_to_str(const struct option *poption, int val)
Definition options.c:1000
option_optset
const struct option_set * option_optset(const struct option *poption)
Definition options.c:643
option_str_def
const char * option_str_def(const struct option *poption)
Definition options.c:939
option_int_max
int option_int_max(const struct option *poption)
Definition options.c:902
option_font_set
bool option_font_set(struct option *poption, const char *font)
Definition options.c:1209
options_save
void options_save(option_save_log_callback log_cb)
Definition options.c:6341
option_bitwise_values
const struct strvec * option_bitwise_values(const struct option *poption)
Definition options.c:1148
option_bitwise_set
bool option_bitwise_set(struct option *poption, unsigned val)
Definition options.c:1159
option_font_get
const char * option_font_get(const struct option *poption)
Definition options.c:1176
desired_settable_options_update
void desired_settable_options_update(void)
Definition options.c:5793
option_color_def
struct ft_color option_color_def(const struct option *poption)
Definition options.c:1236
option_bitwise_get
unsigned option_bitwise_get(const struct option *poption)
Definition options.c:1109
option_enum_def_int
int option_enum_def_int(const struct option *poption)
Definition options.c:1039
option_int_set
bool option_int_set(struct option *poption, int val)
Definition options.c:913
option_category_name
const char * option_category_name(const struct option *poption)
Definition options.c:713
option_int_def
int option_int_def(const struct option *poption)
Definition options.c:880
options.h
options_iterate
#define options_iterate(poptset, poption)
Definition options.h:588
options_iterate_end
#define options_iterate_end
Definition options.h:593
MAX
#define MAX(x, y)
Definition shared.h:54
speclist.h
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
strvec_get
const char * strvec_get(const struct strvec *psv, size_t svindex)
Definition string_vector.c:389
strvec_size
size_t strvec_size(const struct strvec *psv)
Definition string_vector.c:344
string_vector.h
strvec_iterate
#define strvec_iterate(psv, str)
Definition string_vector.h:61
strvec_iterate_end
#define strvec_iterate_end
Definition string_vector.h:68
action
Definition actions.h:106
color
Definition colors.h:21
ft_color
Definition featured_text.h:141
option_dialog
Definition optiondlg.c:41
option_dialog::box_children
int * box_children
Definition optiondlg.c:46
option_dialog::vboxes
GtkWidget ** vboxes
Definition optiondlg.c:45
option_dialog::shell
GtkWidget * shell
Definition optiondlg.c:43
option_dialog::poptset
const struct option_set * poptset
Definition optiondlg.c:42
option_dialog::notebook
GtkWidget * notebook
Definition optiondlg.c:44
option_set
Definition options.c:444
option
Definition options.c:514
strvec
Definition string_vector.c:29
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47