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-3.22 */
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(GTK_BOX(bbox), button, -1);
98}
99
100/*******************************************************************/
103GtkWidget *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(GTK_WINDOW_TOPLEVEL);
110 gtk_window_set_position (GTK_WINDOW(dshell), GTK_WIN_POS_MOUSE);
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 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);
122
123 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
124
125 dlabel = gtk_label_new(text);
126 gtk_container_add(GTK_CONTAINER(vbox), dlabel);
127
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);
131
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));
135
136 gtk_widget_show(vbox);
137 gtk_widget_show(dlabel);
138
139 return dshell;
140}
141
142/*******************************************************************/
146static void choice_dialog_clicked(GtkWidget *w, gpointer data)
147{
148 if (g_object_get_data(G_OBJECT(data), "hide")) {
149 gtk_widget_hide(GTK_WIDGET(data));
150 } else {
151 gtk_widget_destroy(GTK_WIDGET(data));
152 }
153}
154
155/*******************************************************************/
158void choice_dialog_add(GtkWidget *dshell, const gchar *label,
159 GCallback handler, gpointer data,
160 bool meta, const gchar *tool_tip)
161{
162 GtkWidget *button, *bbox;
163 char name[512];
164 int nbuttons;
165
166 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
168 g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(nbuttons+1));
169
170 fc_snprintf(name, sizeof(name), "button%d", nbuttons);
171
172 button = gtk_button_new_with_mnemonic(label);
173 gtk_container_add(GTK_CONTAINER(bbox), button);
174 g_object_set_data(G_OBJECT(dshell), name, button);
175
176 if (handler) {
177 g_signal_connect(button, "clicked", handler, data);
178 }
179
180 if (!meta) {
181 /* This button makes the choice. */
182 g_signal_connect_after(button, "clicked",
183 G_CALLBACK(choice_dialog_clicked), dshell);
184 }
185
186 if (tool_tip != NULL) {
187 gtk_widget_set_tooltip_text(button, tool_tip);
188 }
189}
190
191/*******************************************************************/
194void choice_dialog_end(GtkWidget *dshell)
195{
196 GtkWidget *bbox;
197
198 bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
199
200 gtk_widget_show_all(bbox);
201 gtk_widget_show(dshell);
202}
203
204/*******************************************************************/
207void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
208{
209 g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(setting));
210}
211
212/*******************************************************************/
215GtkWidget *popup_choice_dialog(GtkWindow *parent, const gchar *dialogname,
216 const gchar *text, ...)
217{
218 GtkWidget *dshell;
219 va_list args;
220 gchar *name;
221
222 dshell = choice_dialog_start(parent, dialogname, text);
223
224 va_start(args, text);
225
226 while ((name = va_arg(args, gchar *))) {
227 GCallback handler;
228 gpointer data;
229
230 handler = va_arg(args, GCallback);
231 data = va_arg(args, gpointer);
232
233 choice_dialog_add(dshell, name, handler, data, FALSE, NULL);
234 }
235
236 va_end(args);
237
239
240 return dshell;
241}
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
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