Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-4.0/choice_dialog.c Source File

Freeciv-3.3

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/*******************************************************************/
38 int choice_dialog_get_number_of_buttons(GtkWidget *cd)
39{
40 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cd), "nbuttons"));
41}
42
43/*******************************************************************/
46 static 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/*******************************************************************/
62 void 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/*******************************************************************/
71 void 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/*******************************************************************/
81 void 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/*******************************************************************/
91 void choice_dialog_button_move_to_the_end(GtkWidget *cd,
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/*******************************************************************/
103 GtkWidget *choice_dialog_start(GtkWindow *parent, const gchar *name,
104 const gchar *text)
105{
106 GtkWidget *dshell, *dlabel, *vbox, *bbox;
107
108 dshell = gtk_window_new();
109 setup_dialog(dshell, toplevel);
110
111 gtk_window_set_title(GTK_WINDOW(dshell), name);
112
113 gtk_window_set_transient_for(GTK_WINDOW(dshell), parent);
114 gtk_window_set_destroy_with_parent(GTK_WINDOW(dshell), TRUE);
115
116 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
117 gtk_window_set_child(GTK_WINDOW(dshell), vbox);
118
119 gtk_widget_set_margin_start(vbox, 5);
120 gtk_widget_set_margin_end(vbox, 5);
121 gtk_widget_set_margin_top(vbox, 5);
122 gtk_widget_set_margin_bottom(vbox, 5);
123
124 dlabel = gtk_label_new(text);
125 gtk_box_append(GTK_BOX(vbox), dlabel);
126
127 bbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
128 gtk_box_append(GTK_BOX(vbox), bbox);
129
130 g_object_set_data(G_OBJECT(dshell), "bbox", bbox);
131 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(0));
132 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(FALSE));
133
134 gtk_widget_set_visible(vbox, TRUE);
135 gtk_widget_set_visible(dlabel, TRUE);
136
137 return dshell;
138}
139
140/*******************************************************************/
144 static void choice_dialog_clicked(GtkWidget *w, gpointer data)
145{
146 if (g_object_get_data(G_OBJECT(data), "hide")) {
147 gtk_widget_set_visible(GTK_WIDGET(data), FALSE);
148 } else {
149 gtk_window_destroy(GTK_WINDOW(data));
150 }
151}
152
153/*******************************************************************/
156 void choice_dialog_add(GtkWidget *dshell, const gchar *label,
157 GCallback handler, gpointer data,
158 bool meta, const gchar *tool_tip)
159{
160 GtkWidget *button, *bbox;
161 char name[512];
162 int nbuttons;
163
164 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
165 nbuttons = choice_dialog_get_number_of_buttons(dshell);
166 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(nbuttons+1));
167
168 fc_snprintf(name, sizeof(name), "button%d", nbuttons);
169
170 button = gtk_button_new_with_mnemonic(label);
171 gtk_box_append(GTK_BOX(bbox), button);
172 g_object_set_data(G_OBJECT(dshell), name, button);
173
174 if (handler) {
175 g_signal_connect(button, "clicked", handler, data);
176 }
177
178 if (!meta) {
179 /* This button makes the choice. */
180 g_signal_connect_after(button, "clicked",
181 G_CALLBACK(choice_dialog_clicked), dshell);
182 }
183
184 if (tool_tip != NULL) {
185 gtk_widget_set_tooltip_text(button, tool_tip);
186 }
187}
188
189/*******************************************************************/
192 void choice_dialog_end(GtkWidget *dshell)
193{
194 GtkWidget *bbox;
195
196 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
197
198 gtk_widget_set_visible(bbox, TRUE);
199 gtk_widget_set_visible(dshell, TRUE);
200}
201
202/*******************************************************************/
205 void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
206{
207 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(setting));
208}
209
210/*******************************************************************/
213 GtkWidget *popup_choice_dialog(GtkWindow *parent, const gchar *dialogname,
214 const gchar *text, ...)
215{
216 GtkWidget *dshell;
217 va_list args;
218 gchar *name;
219
220 dshell = choice_dialog_start(parent, dialogname, text);
221
222 va_start(args, text);
223
224 while ((name = va_arg(args, gchar *))) {
225 GCallback handler;
226 gpointer data;
227
228 handler = va_arg(args, GCallback);
229 data = va_arg(args, gpointer);
230
231 choice_dialog_add(dshell, name, handler, data, FALSE, NULL);
232 }
233
234 va_end(args);
235
236 choice_dialog_end(dshell);
237
238 return dshell;
239}
240
241/*******************************************************************/
244 void choice_dialog_destroy(GtkWidget *dlg)
245{
246 if (dlg != NULL) {
247 gtk_window_destroy(GTK_WINDOW(dlg));
248 }
249}
incite_cost
char * incite_cost
Definition comments.c:74
choice_dialog_start
GtkWidget * choice_dialog_start(GtkWindow *parent, const gchar *name, const gchar *text)
Definition choice_dialog.c:103
choice_dialog_end
void choice_dialog_end(GtkWidget *dshell)
Definition choice_dialog.c:194
choice_dialog_get_nth_button
static GtkWidget * choice_dialog_get_nth_button(GtkWidget *cd, int button)
Definition choice_dialog.c:46
choice_dialog_get_number_of_buttons
int choice_dialog_get_number_of_buttons(GtkWidget *cd)
Definition choice_dialog.c:38
choice_dialog_button_set_tooltip
void choice_dialog_button_set_tooltip(GtkWidget *cd, int number, const char *tool_tip)
Definition choice_dialog.c:81
choice_dialog_clicked
static void choice_dialog_clicked(GtkWidget *w, gpointer data)
Definition choice_dialog.c:146
choice_dialog_button_move_to_the_end
void choice_dialog_button_move_to_the_end(GtkWidget *cd, const int number)
Definition choice_dialog.c:91
choice_dialog_set_hide
void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
Definition choice_dialog.c:207
choice_dialog_add
void choice_dialog_add(GtkWidget *dshell, const gchar *label, GCallback handler, gpointer data, bool meta, const gchar *tool_tip)
Definition choice_dialog.c:158
choice_dialog_button_set_label
void choice_dialog_button_set_label(GtkWidget *cd, int number, const char *label)
Definition choice_dialog.c:71
choice_dialog_button_set_sensitive
void choice_dialog_button_set_sensitive(GtkWidget *cd, int button, gboolean state)
Definition choice_dialog.c:62
popup_choice_dialog
GtkWidget * popup_choice_dialog(GtkWindow *parent, const gchar *dialogname, const gchar *text,...)
Definition choice_dialog.c:215
dshell
static GtkWidget * dshell
Definition gotodlg.c:56
toplevel
GtkWidget * toplevel
Definition gui_main.c:125
setup_dialog
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
choice_dialog_destroy
void choice_dialog_destroy(GtkWidget *dlg)
Definition choice_dialog.c:244
choice_dialog.h
gui_stuff.h
gui_main.h
name
const char * name
Definition inputfile.c:127
button_name
static QString button_name(Qt::MouseButton bt)
Definition shortcuts.cpp:453
setting
Definition settings.c:92
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47