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