Freeciv-3.1
Loading...
Searching...
No Matches
choice_dialog.c
Go to the documentation of this file.
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.
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#ifdef HAVE_CONFIG_H
14#include <fc_config.h>
15#endif
16
17#include <stdarg.h>
18
19#include <gtk/gtk.h>
20
21/* utility */
22#include "support.h"
23
24/* gui-gtk-4.0 */
25#include "gui_main.h"
26#include "gui_stuff.h"
27
28#include "choice_dialog.h"
29
30/***********************************************************************
31 Choice dialog: A dialog with a label and a list of buttons
32 placed vertically
33***********************************************************************/
34
35/*******************************************************************/
39{
40 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cd), "nbuttons"));
41}
42
43/*******************************************************************/
46static GtkWidget* choice_dialog_get_nth_button(GtkWidget *cd,
47 int button)
48{
49 char button_name[512];
50 GtkWidget *b;
51
52 fc_snprintf(button_name, sizeof(button_name), "button%d", button);
53
54 b = g_object_get_data(G_OBJECT(cd), button_name);
55
56 return b;
57}
58
59/*******************************************************************/
62void choice_dialog_button_set_sensitive(GtkWidget *cd, int button,
63 gboolean state)
64{
65 gtk_widget_set_sensitive(choice_dialog_get_nth_button(cd, button), state);
66}
67
68/*******************************************************************/
71void choice_dialog_button_set_label(GtkWidget *cd, int number,
72 const char *label)
73{
74 GtkWidget* button = choice_dialog_get_nth_button(cd, number);
75 gtk_button_set_label(GTK_BUTTON(button), label);
76}
77
78/*******************************************************************/
81void choice_dialog_button_set_tooltip(GtkWidget *cd, int number,
82 const char *tool_tip)
83{
84 GtkWidget* button = choice_dialog_get_nth_button(cd, number);
85 gtk_widget_set_tooltip_text(button, tool_tip);
86}
87
88/*******************************************************************/
92 const int number)
93{
94 GtkWidget *button = choice_dialog_get_nth_button(cd, number);
95 GtkWidget *bbox = g_object_get_data(G_OBJECT(cd), "bbox");
96
97 gtk_box_reorder_child_after(GTK_BOX(bbox), button, NULL);
98}
99
100/*******************************************************************/
103GtkWidget *choice_dialog_start(GtkWindow *parent, const gchar *name,
104 const gchar *text)
105{
106 GtkWidget *dshell, *dlabel, *vgrid, *bbox;
107 int grid_row = 0;
108
109 dshell = gtk_window_new();
111
112 gtk_window_set_title(GTK_WINDOW(dshell), name);
113
114 gtk_window_set_transient_for(GTK_WINDOW(dshell), parent);
115 gtk_window_set_destroy_with_parent(GTK_WINDOW(dshell), TRUE);
116
117 vgrid = gtk_grid_new();
118 gtk_orientable_set_orientation(GTK_ORIENTABLE(vgrid),
119 GTK_ORIENTATION_VERTICAL);
120 gtk_grid_set_row_spacing(GTK_GRID(vgrid), 5);
121 gtk_window_set_child(GTK_WINDOW(dshell), vgrid);
122
123 gtk_widget_set_margin_start(vgrid, 5);
124 gtk_widget_set_margin_end(vgrid, 5);
125 gtk_widget_set_margin_top(vgrid, 5);
126 gtk_widget_set_margin_bottom(vgrid, 5);
127
128 dlabel = gtk_label_new(text);
129 gtk_grid_attach(GTK_GRID(vgrid), dlabel, 0, grid_row++, 1, 1);
130
131 bbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
132 gtk_grid_attach(GTK_GRID(vgrid), bbox, 0, grid_row++, 1, 1);
133
134 g_object_set_data(G_OBJECT(dshell), "bbox", bbox);
135 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(0));
136 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(FALSE));
137
138 gtk_widget_show(vgrid);
139 gtk_widget_show(dlabel);
140
141 return dshell;
142}
143
144/*******************************************************************/
148static void choice_dialog_clicked(GtkWidget *w, gpointer data)
149{
150 if (g_object_get_data(G_OBJECT(data), "hide")) {
151 gtk_widget_hide(GTK_WIDGET(data));
152 } else {
153 gtk_window_destroy(GTK_WINDOW(data));
154 }
155}
156
157/*******************************************************************/
160void choice_dialog_add(GtkWidget *dshell, const gchar *label,
161 GCallback handler, gpointer data,
162 bool meta, const gchar *tool_tip)
163{
164 GtkWidget *button, *bbox;
165 char name[512];
166 int nbuttons;
167
168 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
170 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(nbuttons+1));
171
172 fc_snprintf(name, sizeof(name), "button%d", nbuttons);
173
174 button = gtk_button_new_with_mnemonic(label);
175 gtk_box_append(GTK_BOX(bbox), button);
176 g_object_set_data(G_OBJECT(dshell), name, button);
177
178 if (handler) {
179 g_signal_connect(button, "clicked", handler, data);
180 }
181
182 if (!meta) {
183 /* This button makes the choice. */
184 g_signal_connect_after(button, "clicked",
185 G_CALLBACK(choice_dialog_clicked), dshell);
186 }
187
188 if (tool_tip != NULL) {
189 gtk_widget_set_tooltip_text(button, tool_tip);
190 }
191}
192
193/*******************************************************************/
196void choice_dialog_end(GtkWidget *dshell)
197{
198 GtkWidget *bbox;
199
200 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
201
202 gtk_widget_show(bbox);
203 gtk_widget_show(dshell);
204}
205
206/*******************************************************************/
209void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
210{
211 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(setting));
212}
213
214/*******************************************************************/
217GtkWidget *popup_choice_dialog(GtkWindow *parent, const gchar *dialogname,
218 const gchar *text, ...)
219{
220 GtkWidget *dshell;
221 va_list args;
222 gchar *name;
223
224 dshell = choice_dialog_start(parent, dialogname, text);
225
226 va_start(args, text);
227
228 while ((name = va_arg(args, gchar *))) {
229 GCallback handler;
230 gpointer data;
231
232 handler = va_arg(args, GCallback);
233 data = va_arg(args, gpointer);
234
235 choice_dialog_add(dshell, name, handler, data, FALSE, NULL);
236 }
237
238 va_end(args);
239
241
242 return dshell;
243}
244
245/*******************************************************************/
248void choice_dialog_destroy(GtkWidget *dlg)
249{
250 if (dlg != NULL) {
251 gtk_window_destroy(GTK_WINDOW(dlg));
252 }
253}
GtkWidget * choice_dialog_start(GtkWindow *parent, const gchar *name, const gchar *text)
void choice_dialog_end(GtkWidget *dshell)
static GtkWidget * choice_dialog_get_nth_button(GtkWidget *cd, int button)
int choice_dialog_get_number_of_buttons(GtkWidget *cd)
void choice_dialog_button_set_tooltip(GtkWidget *cd, int number, const char *tool_tip)
static void choice_dialog_clicked(GtkWidget *w, gpointer data)
void choice_dialog_button_move_to_the_end(GtkWidget *cd, const int number)
void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
void choice_dialog_add(GtkWidget *dshell, const gchar *label, GCallback handler, gpointer data, bool meta, const gchar *tool_tip)
void choice_dialog_button_set_label(GtkWidget *cd, int number, const char *label)
void choice_dialog_button_set_sensitive(GtkWidget *cd, int button, gboolean state)
GtkWidget * popup_choice_dialog(GtkWindow *parent, const gchar *dialogname, const gchar *text,...)
static GtkWidget * dshell
Definition gotodlg.c:56
GtkWidget * toplevel
Definition gui_main.c:124
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:281
void choice_dialog_destroy(GtkWidget *dlg)
const char * name
Definition inputfile.c:127
static QString button_name(Qt::MouseButton bt)
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47