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>
24 #include "gui_stuff.h"
26 #include "choice_dialog.h"
28 /****************************************************************
29 Choice dialog: A dialog with a label and a list of buttons
31 ****************************************************************/
33 /***********************************************************************
34 Get the number of buttons in the choice dialog.
35 ***********************************************************************/
36 int choice_dialog_get_number_of_buttons(GtkWidget
*cd
)
38 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cd
), "nbuttons"));
41 /****************************************************************
42 Get nth button widget from dialog
43 *****************************************************************/
44 static GtkWidget
* choice_dialog_get_nth_button(GtkWidget
*cd
,
47 char button_name
[512];
50 fc_snprintf(button_name
, sizeof(button_name
), "button%d", button
);
52 b
= g_object_get_data(G_OBJECT(cd
), button_name
);
57 /****************************************************************
58 Set sensitivity state of choice dialog button.
59 *****************************************************************/
60 void choice_dialog_button_set_sensitive(GtkWidget
*cd
, int button
,
63 gtk_widget_set_sensitive(choice_dialog_get_nth_button(cd
, button
), state
);
66 /****************************************************************
67 Set label for choice dialog button.
68 *****************************************************************/
69 void choice_dialog_button_set_label(GtkWidget
*cd
, int number
,
72 GtkWidget
* button
= choice_dialog_get_nth_button(cd
, number
);
73 gtk_button_set_label(GTK_BUTTON(button
), label
);
76 /****************************************************************
77 Set tool tip for choice dialog button.
78 *****************************************************************/
79 void choice_dialog_button_set_tooltip(GtkWidget
*cd
, int number
,
82 GtkWidget
* button
= choice_dialog_get_nth_button(cd
, number
);
83 gtk_widget_set_tooltip_text(button
, tool_tip
);
86 /****************************************************************
87 Move the specified button to the end.
88 *****************************************************************/
89 void choice_dialog_button_move_to_the_end(GtkWidget
*cd
,
92 GtkWidget
*button
= choice_dialog_get_nth_button(cd
, number
);
93 GtkWidget
*bbox
= g_object_get_data(G_OBJECT(cd
), "bbox");
95 gtk_box_reorder_child(GTK_BOX(bbox
), button
, -1);
98 /****************************************************************
100 *****************************************************************/
101 GtkWidget
*choice_dialog_start(GtkWindow
*parent
, const gchar
*name
,
104 GtkWidget
*dshell
, *dlabel
, *vbox
, *bbox
;
106 dshell
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
107 setup_dialog(dshell
, toplevel
);
108 gtk_window_set_position (GTK_WINDOW(dshell
), GTK_WIN_POS_MOUSE
);
110 gtk_window_set_title(GTK_WINDOW(dshell
), name
);
112 gtk_window_set_transient_for(GTK_WINDOW(dshell
), parent
);
113 gtk_window_set_destroy_with_parent(GTK_WINDOW(dshell
), TRUE
);
115 vbox
= gtk_vbox_new(FALSE
, 5);
116 gtk_container_add(GTK_CONTAINER(dshell
),vbox
);
118 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 5);
120 dlabel
= gtk_label_new(text
);
121 gtk_container_add(GTK_CONTAINER(vbox
), dlabel
);
123 bbox
= gtk_vbutton_box_new();
124 gtk_box_set_spacing(GTK_BOX(bbox
), 2);
125 gtk_container_add(GTK_CONTAINER(vbox
), bbox
);
127 g_object_set_data(G_OBJECT(dshell
), "bbox", bbox
);
128 g_object_set_data(G_OBJECT(dshell
), "nbuttons", GINT_TO_POINTER(0));
129 g_object_set_data(G_OBJECT(dshell
), "hide", GINT_TO_POINTER(FALSE
));
131 gtk_widget_show(vbox
);
132 gtk_widget_show(dlabel
);
137 /****************************************************************
138 Choice dialog has been clicked and primary handling has
140 *****************************************************************/
141 static void choice_dialog_clicked(GtkWidget
*w
, gpointer data
)
143 if (g_object_get_data(G_OBJECT(data
), "hide")) {
144 gtk_widget_hide(GTK_WIDGET(data
));
146 gtk_widget_destroy(GTK_WIDGET(data
));
150 /****************************************************************
151 Add button to choice dialog.
152 *****************************************************************/
153 void choice_dialog_add(GtkWidget
*dshell
, const gchar
*label
,
154 GCallback handler
, gpointer data
,
155 bool meta
, const gchar
*tool_tip
)
157 GtkWidget
*button
, *bbox
;
161 bbox
= g_object_get_data(G_OBJECT(dshell
), "bbox");
162 nbuttons
= choice_dialog_get_number_of_buttons(dshell
);
163 g_object_set_data(G_OBJECT(dshell
), "nbuttons", GINT_TO_POINTER(nbuttons
+1));
165 fc_snprintf(name
, sizeof(name
), "button%d", nbuttons
);
167 button
= gtk_button_new_from_stock(label
);
168 gtk_container_add(GTK_CONTAINER(bbox
), button
);
169 g_object_set_data(G_OBJECT(dshell
), name
, button
);
172 g_signal_connect(button
, "clicked", handler
, data
);
176 /* This button makes the choice. */
177 g_signal_connect_after(button
, "clicked",
178 G_CALLBACK(choice_dialog_clicked
), dshell
);
181 if (tool_tip
!= NULL
) {
182 gtk_widget_set_tooltip_text(button
, tool_tip
);
186 /****************************************************************
187 Choice dialog construction ready
188 *****************************************************************/
189 void choice_dialog_end(GtkWidget
*dshell
)
193 bbox
= g_object_get_data(G_OBJECT(dshell
), "bbox");
195 gtk_widget_show_all(bbox
);
196 gtk_widget_show(dshell
);
199 /****************************************************************
200 Set hide property of choice dialog
201 *****************************************************************/
202 void choice_dialog_set_hide(GtkWidget
*dshell
, gboolean setting
)
204 g_object_set_data(G_OBJECT(dshell
), "hide", GINT_TO_POINTER(setting
));
207 /****************************************************************
208 Open new choice dialog.
209 *****************************************************************/
210 GtkWidget
*popup_choice_dialog(GtkWindow
*parent
, const gchar
*dialogname
,
211 const gchar
*text
, ...)
217 dshell
= choice_dialog_start(parent
, dialogname
, text
);
219 va_start(args
, text
);
221 while ((name
= va_arg(args
, gchar
*))) {
225 handler
= va_arg(args
, GCallback
);
226 data
= va_arg(args
, gpointer
);
228 choice_dialog_add(dshell
, name
, handler
, data
, FALSE
, NULL
);
233 choice_dialog_end(dshell
);