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)
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 ***********************************************************************/
14 #include <fc_config.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
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
,
49 char button_name
[512];
52 fc_snprintf(button_name
, sizeof(button_name
), "button%d", button
);
54 b
= g_object_get_data(G_OBJECT(cd
), button_name
);
59 /****************************************************************
60 Set sensitivity state of choice dialog button.
61 *****************************************************************/
62 void choice_dialog_button_set_sensitive(GtkWidget
*cd
, int button
,
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
,
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
,
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
,
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 /****************************************************************
102 *****************************************************************/
103 GtkWidget
*choice_dialog_start(GtkWindow
*parent
, const gchar
*name
,
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
);
142 /****************************************************************
143 Choice dialog has been clicked and primary handling has
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
));
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
;
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
);
177 g_signal_connect(button
, "clicked", handler
, data
);
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
)
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
, ...)
222 dshell
= choice_dialog_start(parent
, dialogname
, text
);
224 va_start(args
, text
);
226 while ((name
= va_arg(args
, gchar
*))) {
230 handler
= va_arg(args
, GCallback
);
231 data
= va_arg(args
, gpointer
);
233 choice_dialog_add(dshell
, name
, handler
, data
, FALSE
, NULL
);
238 choice_dialog_end(dshell
);