]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/messagebox.cpp
[q3map2] Unwind script stack in case of script loading error.
[xonotic/netradiant.git] / libs / gtkutil / messagebox.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 "messagebox.h"
23
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtk.h>
26
27 #include "dialog.h"
28 #include "widget.h"
29
30 ui::Widget create_padding(int width, int height)
31 {
32     ui::Alignment widget = ui::Alignment(0.0, 0.0, 0.0, 0.0);
33     widget.show();
34     widget.dimensions(width, height);
35     return widget;
36 }
37
38 const char *messagebox_stock_icon(EMessageBoxIcon type)
39 {
40     switch (type) {
41         default:
42         case eMB_ICONDEFAULT:
43             return GTK_STOCK_DIALOG_INFO;
44         case eMB_ICONERROR:
45             return GTK_STOCK_DIALOG_ERROR;
46         case eMB_ICONWARNING:
47             return GTK_STOCK_DIALOG_WARNING;
48         case eMB_ICONQUESTION:
49             return GTK_STOCK_DIALOG_QUESTION;
50         case eMB_ICONASTERISK:
51             return GTK_STOCK_DIALOG_INFO;
52     }
53 }
54
55 EMessageBoxReturn
56 gtk_MessageBox(ui::Window parentWindow, const char *text, const char *title, EMessageBoxType type, EMessageBoxIcon icon)
57 {
58     ModalDialog dialog;
59     ModalDialogButton ok_button(dialog, eIDOK);
60     ModalDialogButton cancel_button(dialog, eIDCANCEL);
61     ModalDialogButton yes_button(dialog, eIDYES);
62     ModalDialogButton no_button(dialog, eIDNO);
63
64     ui::Window window = create_fixedsize_modal_dialog_window(parentWindow, title, dialog, 400, 100);
65
66     if (parentWindow) {
67         //window.connect( "delete_event", G_CALLBACK(floating_window_delete_present), parent);
68         gtk_window_deiconify(parentWindow);
69     }
70
71     auto accel = ui::AccelGroup(ui::New);
72     window.add_accel_group(accel);
73
74     auto vbox = create_dialog_vbox(8, 8);
75     window.add(vbox);
76
77
78     auto hboxDummy = create_dialog_hbox(0, 0);
79     vbox.pack_start(hboxDummy, FALSE, FALSE, 0);
80
81     hboxDummy.pack_start(create_padding(0, 50), FALSE, FALSE, 0); // HACK to force minimum height
82
83     auto iconBox = create_dialog_hbox(16, 0);
84     hboxDummy.pack_start(iconBox, FALSE, FALSE, 0);
85
86     auto image = ui::Image::from(gtk_image_new_from_stock(messagebox_stock_icon(icon), GTK_ICON_SIZE_DIALOG));
87     image.show();
88     iconBox.pack_start(image, FALSE, FALSE, 0);
89
90     auto label = ui::Label(text);
91     label.show();
92     gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
93     gtk_label_set_justify(label, GTK_JUSTIFY_LEFT);
94     gtk_label_set_line_wrap(label, TRUE);
95     iconBox.pack_start(label, TRUE, TRUE, 0);
96
97
98     auto vboxDummy = create_dialog_vbox(0, 0);
99     vbox.pack_start(vboxDummy, FALSE, FALSE, 0);
100
101     auto alignment = ui::Alignment(0.5, 0.0, 0.0, 0.0);
102     alignment.show();
103     vboxDummy.pack_start(alignment, FALSE, FALSE, 0);
104
105     auto hbox = create_dialog_hbox(8, 0);
106     alignment.add(hbox);
107
108     vboxDummy.pack_start(create_padding(400, 0), FALSE, FALSE, 0); // HACK to force minimum width
109
110
111     if (type == eMB_OK) {
112         auto button = create_modal_dialog_button("OK", ok_button);
113         hbox.pack_start(button, TRUE, FALSE, 0);
114         gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Escape, (GdkModifierType) 0, (GtkAccelFlags) 0);
115         gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Return, (GdkModifierType) 0, (GtkAccelFlags) 0);
116         widget_make_default(button);
117         button.show();
118
119         dialog.ret = eIDOK;
120     } else if (type == eMB_OKCANCEL) {
121         {
122             auto button = create_modal_dialog_button("OK", ok_button);
123             hbox.pack_start(button, TRUE, FALSE, 0);
124             gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Return, (GdkModifierType) 0,
125                                        (GtkAccelFlags) 0);
126             widget_make_default(button);
127             button.show();
128         }
129
130         {
131             auto button = create_modal_dialog_button("OK", cancel_button);
132             hbox.pack_start(button, TRUE, FALSE, 0);
133             gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Escape, (GdkModifierType) 0,
134                                        (GtkAccelFlags) 0);
135             button.show();
136         }
137
138         dialog.ret = eIDCANCEL;
139     } else if (type == eMB_YESNOCANCEL) {
140         {
141             auto button = create_modal_dialog_button("Yes", yes_button);
142             hbox.pack_start(button, TRUE, FALSE, 0);
143             widget_make_default(button);
144             button.show();
145         }
146
147         {
148             auto button = create_modal_dialog_button("No", no_button);
149             hbox.pack_start(button, TRUE, FALSE, 0);
150             button.show();
151         }
152         {
153             auto button = create_modal_dialog_button("Cancel", cancel_button);
154             hbox.pack_start(button, TRUE, FALSE, 0);
155             button.show();
156         }
157
158         dialog.ret = eIDCANCEL;
159     } else if (type == eMB_NOYES) {
160         {
161             auto button = create_modal_dialog_button("No", no_button);
162             hbox.pack_start(button, TRUE, FALSE, 0);
163             widget_make_default(button);
164             button.show();
165         }
166         {
167             auto button = create_modal_dialog_button("Yes", yes_button);
168             hbox.pack_start(button, TRUE, FALSE, 0);
169             button.show();
170         }
171
172         dialog.ret = eIDNO;
173     } else /* if (type == eMB_YESNO) */
174     {
175         {
176             auto button = create_modal_dialog_button("Yes", yes_button);
177             hbox.pack_start(button, TRUE, FALSE, 0);
178             widget_make_default(button);
179             button.show();
180         }
181
182         {
183             auto button = create_modal_dialog_button("No", no_button);
184             hbox.pack_start(button, TRUE, FALSE, 0);
185             button.show();
186         }
187         dialog.ret = eIDNO;
188     }
189
190     modal_dialog_show(window, dialog);
191
192     window.destroy();
193
194     return dialog.ret;
195 }