Qt client - remove right click menu from end turn sidebar
[freeciv.git] / client / gui-gtk-2.0 / choice_dialog.c
blobe2679ccf4c5590a26bef81584ad114a70b270108
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 #include "support.h"
23 #include "gui_main.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
30 placed vertically
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,
45 int button)
47 char button_name[512];
48 GtkWidget *b;
50 fc_snprintf(button_name, sizeof(button_name), "button%d", button);
52 b = g_object_get_data(G_OBJECT(cd), button_name);
54 return b;
57 /****************************************************************
58 Set sensitivity state of choice dialog button.
59 *****************************************************************/
60 void choice_dialog_button_set_sensitive(GtkWidget *cd, int button,
61 gboolean state)
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,
70 const char* label)
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,
80 const char* tool_tip)
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,
90 const int number)
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 /****************************************************************
99 Create choice dialog
100 *****************************************************************/
101 GtkWidget *choice_dialog_start(GtkWindow *parent, const gchar *name,
102 const gchar *text)
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);
134 return dshell;
137 /****************************************************************
138 Choice dialog has been clicked and primary handling has
139 taken place already.
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));
145 } else {
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;
158 char name[512];
159 int nbuttons;
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);
171 if (handler) {
172 g_signal_connect(button, "clicked", handler, data);
175 if (!meta) {
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)
191 GtkWidget *bbox;
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, ...)
213 GtkWidget *dshell;
214 va_list args;
215 gchar *name;
217 dshell = choice_dialog_start(parent, dialogname, text);
219 va_start(args, text);
221 while ((name = va_arg(args, gchar *))) {
222 GCallback handler;
223 gpointer data;
225 handler = va_arg(args, GCallback);
226 data = va_arg(args, gpointer);
228 choice_dialog_add(dshell, name, handler, data, FALSE, NULL);
231 va_end(args);
233 choice_dialog_end(dshell);
235 return dshell;