]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/dialog.cpp
allow undo “make detail/structural”, <3 @SpiKe, thanks @Garux, fix #76
[xonotic/netradiant.git] / libs / gtkutil / dialog.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "dialog.h"
23
24 #include <gtk/gtk.h>
25
26 #include "button.h"
27 #include "window.h"
28
29 ui::VBox create_dialog_vbox(int spacing, int border)
30 {
31     auto vbox = ui::VBox(FALSE, spacing);
32     vbox.show();
33     gtk_container_set_border_width(GTK_CONTAINER(vbox), border);
34     return vbox;
35 }
36
37 ui::HBox create_dialog_hbox(int spacing, int border)
38 {
39     auto hbox = ui::HBox(FALSE, spacing);
40     hbox.show();
41     gtk_container_set_border_width(GTK_CONTAINER(hbox), border);
42     return hbox;
43 }
44
45 ui::Frame create_dialog_frame(const char *label, ui::Shadow shadow)
46 {
47     auto frame = ui::Frame(label);
48     frame.show();
49     gtk_frame_set_shadow_type(frame, (GtkShadowType) shadow);
50     return frame;
51 }
52
53 ui::Table
54 create_dialog_table(unsigned int rows, unsigned int columns, unsigned int row_spacing, unsigned int col_spacing,
55                     int border)
56 {
57     auto table = ui::Table(rows, columns, FALSE);
58     table.show();
59     gtk_table_set_row_spacings(table, row_spacing);
60     gtk_table_set_col_spacings(table, col_spacing);
61     gtk_container_set_border_width(GTK_CONTAINER(table), border);
62     return table;
63 }
64
65 ui::Button create_dialog_button(const char *label, GCallback func, gpointer data)
66 {
67     auto button = ui::Button(label);
68     button.dimensions(64, -1);
69     button.show();
70     button.connect("clicked", func, data);
71     return button;
72 }
73
74 ui::Window
75 create_dialog_window(ui::Window parent, const char *title, GCallback func, gpointer data, int default_w, int default_h)
76 {
77     ui::Window window = create_floating_window(title, parent);
78     gtk_window_set_default_size(window, default_w, default_h);
79     gtk_window_set_position(window, GTK_WIN_POS_CENTER_ON_PARENT);
80     window.connect("delete_event", func, data);
81
82     return window;
83 }
84
85 gboolean modal_dialog_button_clicked(ui::Widget widget, ModalDialogButton *button)
86 {
87     button->m_dialog.loop = false;
88     button->m_dialog.ret = button->m_value;
89     return TRUE;
90 }
91
92 gboolean modal_dialog_delete(ui::Widget widget, GdkEvent *event, ModalDialog *dialog)
93 {
94     dialog->loop = 0;
95     dialog->ret = eIDCANCEL;
96     return TRUE;
97 }
98
99 EMessageBoxReturn modal_dialog_show(ui::Window window, ModalDialog &dialog)
100 {
101     gtk_grab_add(window);
102     window.show();
103
104     dialog.loop = true;
105     while (dialog.loop) {
106         gtk_main_iteration();
107     }
108
109     window.hide();
110     gtk_grab_remove(window);
111
112     return dialog.ret;
113 }
114
115 ui::Button create_modal_dialog_button(const char *label, ModalDialogButton &button)
116 {
117     return create_dialog_button(label, G_CALLBACK(modal_dialog_button_clicked), &button);
118 }
119
120 ui::Window
121 create_modal_dialog_window(ui::Window parent, const char *title, ModalDialog &dialog, int default_w, int default_h)
122 {
123     return create_dialog_window(parent, title, G_CALLBACK(modal_dialog_delete), &dialog, default_w, default_h);
124 }
125
126 ui::Window
127 create_fixedsize_modal_dialog_window(ui::Window parent, const char *title, ModalDialog &dialog, int width, int height)
128 {
129     auto window = create_modal_dialog_window(parent, title, dialog, width, height);
130
131     gtk_window_set_resizable(window, FALSE);
132     gtk_window_set_modal(window, TRUE);
133     gtk_window_set_position(window, GTK_WIN_POS_CENTER);
134
135     window_remove_minmax(window);
136
137     //window.dimensions(width, height);
138     //gtk_window_set_default_size(window, width, height);
139     //gtk_window_resize(window, width, height);
140     //GdkGeometry geometry = { width, height, -1, -1, width, height, -1, -1, -1, -1, GDK_GRAVITY_STATIC, };
141     //gtk_window_set_geometry_hints(window, window, &geometry, (GdkWindowHints)(GDK_HINT_POS|GDK_HINT_MIN_SIZE|GDK_HINT_BASE_SIZE));
142
143     return window;
144 }
145
146 gboolean dialog_button_ok(ui::Widget widget, ModalDialog *data)
147 {
148     data->loop = false;
149     data->ret = eIDOK;
150     return TRUE;
151 }
152
153 gboolean dialog_button_cancel(ui::Widget widget, ModalDialog *data)
154 {
155     data->loop = false;
156     data->ret = eIDCANCEL;
157     return TRUE;
158 }
159
160 gboolean dialog_button_yes(ui::Widget widget, ModalDialog *data)
161 {
162     data->loop = false;
163     data->ret = eIDYES;
164     return TRUE;
165 }
166
167 gboolean dialog_button_no(ui::Widget widget, ModalDialog *data)
168 {
169     data->loop = false;
170     data->ret = eIDNO;
171     return TRUE;
172 }
173
174 gboolean dialog_delete_callback(ui::Widget widget, GdkEventAny *event, ModalDialog *data)
175 {
176     widget.hide();
177     data->loop = false;
178     return TRUE;
179 }
180
181 ui::Window create_simple_modal_dialog_window(const char *title, ModalDialog &dialog, ui::Widget contents)
182 {
183     ui::Window window = create_fixedsize_modal_dialog_window(ui::Window{ui::null}, title, dialog);
184
185     auto vbox1 = create_dialog_vbox(8, 4);
186     window.add(vbox1);
187
188     vbox1.add(contents);
189
190     ui::Alignment alignment = ui::Alignment(0.5, 0.0, 0.0, 0.0);
191     alignment.show();
192     vbox1.pack_start(alignment, FALSE, FALSE, 0);
193
194     auto button = create_dialog_button("OK", G_CALLBACK(dialog_button_ok), &dialog);
195     alignment.add(button);
196
197     return window;
198 }
199
200 RadioHBox RadioHBox_new(StringArrayRange names)
201 {
202     auto hbox = ui::HBox(TRUE, 4);
203     hbox.show();
204
205     GSList *group = 0;
206     auto radio = ui::RadioButton(ui::null);
207     for (StringArrayRange::Iterator i = names.first; i != names.last; ++i) {
208         radio = ui::RadioButton::from(gtk_radio_button_new_with_label(group, *i));
209         radio.show();
210         hbox.pack_start(radio, FALSE, FALSE, 0);
211
212         group = gtk_radio_button_get_group(radio);
213     }
214
215     return RadioHBox(hbox, radio);
216 }
217
218
219 PathEntry PathEntry_new()
220 {
221     auto frame = ui::Frame();
222     frame.show();
223     gtk_frame_set_shadow_type(frame, GTK_SHADOW_IN);
224
225     // path entry
226     auto hbox = ui::HBox(FALSE, 0);
227     hbox.show();
228
229     auto entry = ui::Entry(ui::New);
230     gtk_entry_set_has_frame(entry, FALSE);
231     entry.show();
232     hbox.pack_start(entry, TRUE, TRUE, 0);
233
234     // browse button
235     auto button = ui::Button(ui::New);
236     button_set_icon(button, "ellipsis.bmp");
237     button.show();
238     hbox.pack_end(button, FALSE, FALSE, 0);
239
240     frame.add(hbox);
241
242     return PathEntry(frame, entry, button);
243 }
244
245 void PathEntry_setPath(PathEntry &self, const char *path)
246 {
247     gtk_entry_set_text(self.m_entry, path);
248 }
249
250 typedef ReferenceCaller<PathEntry, void(const char *), PathEntry_setPath> PathEntrySetPathCaller;
251
252 void BrowsedPathEntry_clicked(ui::Widget widget, BrowsedPathEntry *self)
253 {
254     self->m_browse(PathEntrySetPathCaller(self->m_entry));
255 }
256
257 BrowsedPathEntry::BrowsedPathEntry(const BrowseCallback &browse) :
258         m_entry(PathEntry_new()),
259         m_browse(browse)
260 {
261     m_entry.m_button.connect("clicked", G_CALLBACK(BrowsedPathEntry_clicked), this);
262 }
263
264
265 ui::Label DialogLabel_new(const char *name)
266 {
267     auto label = ui::Label(name);
268     label.show();
269     gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
270     gtk_label_set_justify(label, GTK_JUSTIFY_LEFT);
271
272     return label;
273 }
274
275 ui::Table DialogRow_new(const char *name, ui::Widget widget)
276 {
277     auto table = ui::Table(1, 3, TRUE);
278     table.show();
279
280     gtk_table_set_col_spacings(table, 4);
281     gtk_table_set_row_spacings(table, 0);
282
283     table.attach(DialogLabel_new(name), {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
284
285     table.attach(widget, {1, 3, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
286
287     return table;
288 }
289
290 void DialogVBox_packRow(ui::Box vbox, ui::Widget row)
291 {
292     vbox.pack_start(row, FALSE, FALSE, 0);
293 }