Split "Establish Embassy".
[freeciv.git] / client / gui-gtk-3.0 / choice_dialog.c
blobc2f85b230e65135455731a2c94bd7745438e2d12
1 /***********************************************************************
2 Freeciv - Copyright (C) 1996-2005 - Freeciv Development Team
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.
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 #ifdef HAVE_CONFIG_H
14 #include <fc_config.h>
15 #endif
17 #include <stdarg.h>
19 #include <gtk/gtk.h>
21 /* utility */
22 #include "support.h"
24 /* gui-gtk-3.0 */
25 #include "gui_main.h"
26 #include "gui_stuff.h"
28 #include "choice_dialog.h"
30 /****************************************************************
31 Choice dialog: A dialog with a label and a list of buttons
32 placed vertically
33 ****************************************************************/
35 /***********************************************************************
36 Get the number of buttons in the choice dialog.
37 ***********************************************************************/
38 int choice_dialog_get_number_of_buttons(GtkWidget *cd)
40 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cd), "nbuttons"));
43 /****************************************************************
44 Get nth button widget from dialog
45 *****************************************************************/
46 static GtkWidget* choice_dialog_get_nth_button(GtkWidget *cd,
47 int button)
49 char button_name[512];
50 GtkWidget *b;
52 fc_snprintf(button_name, sizeof(button_name), "button%d", button);
54 b = g_object_get_data(G_OBJECT(cd), button_name);
56 return b;
59 /****************************************************************
60 Set sensitivity state of choice dialog button.
61 *****************************************************************/
62 void choice_dialog_button_set_sensitive(GtkWidget *cd, int button,
63 gboolean state)
65 gtk_widget_set_sensitive(choice_dialog_get_nth_button(cd, button), state);
68 /****************************************************************
69 Set label for choice dialog button.
70 *****************************************************************/
71 void choice_dialog_button_set_label(GtkWidget *cd, int number,
72 const char* label)
74 GtkWidget* button = choice_dialog_get_nth_button(cd, number);
75 gtk_button_set_label(GTK_BUTTON(button), label);
78 /****************************************************************
79 Set tool tip for choice dialog button.
80 *****************************************************************/
81 void choice_dialog_button_set_tooltip(GtkWidget *cd, int number,
82 const char* tool_tip)
84 GtkWidget* button = choice_dialog_get_nth_button(cd, number);
85 gtk_widget_set_tooltip_text(button, tool_tip);
88 /****************************************************************
89 Move the specified button to the end.
90 *****************************************************************/
91 void choice_dialog_button_move_to_the_end(GtkWidget *cd,
92 const int number)
94 GtkWidget *button = choice_dialog_get_nth_button(cd, number);
95 GtkWidget *bbox = g_object_get_data(G_OBJECT(cd), "bbox");
97 gtk_box_reorder_child(GTK_BOX(bbox), button, -1);
100 /****************************************************************
101 Create choice dialog
102 *****************************************************************/
103 GtkWidget *choice_dialog_start(GtkWindow *parent, const gchar *name,
104 const gchar *text)
106 GtkWidget *dshell, *dlabel, *vbox, *bbox;
108 dshell = gtk_window_new(GTK_WINDOW_TOPLEVEL);
109 setup_dialog(dshell, toplevel);
110 gtk_window_set_position (GTK_WINDOW(dshell), GTK_WIN_POS_MOUSE);
112 gtk_window_set_title(GTK_WINDOW(dshell), name);
114 gtk_window_set_transient_for(GTK_WINDOW(dshell), parent);
115 gtk_window_set_destroy_with_parent(GTK_WINDOW(dshell), TRUE);
117 vbox = gtk_grid_new();
118 gtk_orientable_set_orientation(GTK_ORIENTABLE(vbox),
119 GTK_ORIENTATION_VERTICAL);
120 gtk_grid_set_row_spacing(GTK_GRID(vbox), 5);
121 gtk_container_add(GTK_CONTAINER(dshell),vbox);
123 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
125 dlabel = gtk_label_new(text);
126 gtk_container_add(GTK_CONTAINER(vbox), dlabel);
128 bbox = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
129 gtk_box_set_spacing(GTK_BOX(bbox), 2);
130 gtk_container_add(GTK_CONTAINER(vbox), bbox);
132 g_object_set_data(G_OBJECT(dshell), "bbox", bbox);
133 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(0));
134 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(FALSE));
136 gtk_widget_show(vbox);
137 gtk_widget_show(dlabel);
139 return dshell;
142 /****************************************************************
143 Choice dialog has been clicked and primary handling has
144 taken place already.
145 *****************************************************************/
146 static void choice_dialog_clicked(GtkWidget *w, gpointer data)
148 if (g_object_get_data(G_OBJECT(data), "hide")) {
149 gtk_widget_hide(GTK_WIDGET(data));
150 } else {
151 gtk_widget_destroy(GTK_WIDGET(data));
155 /****************************************************************
156 Add button to choice dialog.
157 *****************************************************************/
158 void choice_dialog_add(GtkWidget *dshell, const gchar *label,
159 GCallback handler, gpointer data,
160 bool meta, const gchar *tool_tip)
162 GtkWidget *button, *bbox;
163 char name[512];
164 int nbuttons;
166 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
167 nbuttons = choice_dialog_get_number_of_buttons(dshell);
168 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(nbuttons+1));
170 fc_snprintf(name, sizeof(name), "button%d", nbuttons);
172 button = gtk_button_new_from_stock(label);
173 gtk_container_add(GTK_CONTAINER(bbox), button);
174 g_object_set_data(G_OBJECT(dshell), name, button);
176 if (handler) {
177 g_signal_connect(button, "clicked", handler, data);
180 if (!meta) {
181 /* This button makes the choice. */
182 g_signal_connect_after(button, "clicked",
183 G_CALLBACK(choice_dialog_clicked), dshell);
186 if (tool_tip != NULL) {
187 gtk_widget_set_tooltip_text(button, tool_tip);
191 /****************************************************************
192 Choice dialog construction ready
193 *****************************************************************/
194 void choice_dialog_end(GtkWidget *dshell)
196 GtkWidget *bbox;
198 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
200 gtk_widget_show_all(bbox);
201 gtk_widget_show(dshell);
204 /****************************************************************
205 Set hide property of choice dialog
206 *****************************************************************/
207 void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
209 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(setting));
212 /****************************************************************
213 Open new choice dialog.
214 *****************************************************************/
215 GtkWidget *popup_choice_dialog(GtkWindow *parent, const gchar *dialogname,
216 const gchar *text, ...)
218 GtkWidget *dshell;
219 va_list args;
220 gchar *name;
222 dshell = choice_dialog_start(parent, dialogname, text);
224 va_start(args, text);
226 while ((name = va_arg(args, gchar *))) {
227 GCallback handler;
228 gpointer data;
230 handler = va_arg(args, GCallback);
231 data = va_arg(args, gpointer);
233 choice_dialog_add(dshell, name, handler, data, FALSE, NULL);
236 va_end(args);
238 choice_dialog_end(dshell);
240 return dshell;