]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/dialog.cpp
netradiant: strip 16-bit png to 8-bit, fix #153
[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         auto vbox = ui::VBox( FALSE, spacing );
31         vbox.show();
32         gtk_container_set_border_width( GTK_CONTAINER( vbox ), border );
33         return vbox;
34 }
35
36 ui::HBox create_dialog_hbox( int spacing, int border ){
37         auto hbox = ui::HBox( FALSE, spacing );
38         hbox.show();
39         gtk_container_set_border_width( GTK_CONTAINER( hbox ), border );
40         return hbox;
41 }
42
43 ui::Frame create_dialog_frame( const char* label, ui::Shadow shadow ){
44         auto frame = ui::Frame( label );
45         frame.show();
46         gtk_frame_set_shadow_type( frame, (GtkShadowType) shadow );
47         return frame;
48 }
49
50 ui::Table create_dialog_table( unsigned int rows, unsigned int columns, unsigned int row_spacing, unsigned int col_spacing, int border ){
51         auto table = ui::Table( rows, columns, FALSE );
52         table.show();
53         gtk_table_set_row_spacings( table, row_spacing );
54         gtk_table_set_col_spacings( table, col_spacing );
55         gtk_container_set_border_width( GTK_CONTAINER( table ), border );
56         return table;
57 }
58
59 ui::Button create_dialog_button( const char* label, GCallback func, gpointer data ){
60         auto button = ui::Button( label );
61         button.dimensions(64, -1);
62         button.show();
63         button.connect( "clicked", func, data );
64         return button;
65 }
66
67 ui::Window create_dialog_window( ui::Window parent, const char* title, GCallback func, gpointer data, int default_w, int default_h ){
68         ui::Window window = create_floating_window( title, parent );
69         gtk_window_set_default_size( window, default_w, default_h );
70         gtk_window_set_position( window, GTK_WIN_POS_CENTER_ON_PARENT );
71         window.connect( "delete_event", func, data );
72
73         return window;
74 }
75
76 gboolean modal_dialog_button_clicked( ui::Widget widget, ModalDialogButton* button ){
77         button->m_dialog.loop = false;
78         button->m_dialog.ret = button->m_value;
79         return TRUE;
80 }
81
82 gboolean modal_dialog_delete( ui::Widget widget, GdkEvent* event, ModalDialog* dialog ){
83         dialog->loop = 0;
84         dialog->ret = eIDCANCEL;
85         return TRUE;
86 }
87
88 EMessageBoxReturn modal_dialog_show( ui::Window window, ModalDialog& dialog ){
89         gtk_grab_add( window  );
90         window.show();
91
92         dialog.loop = true;
93         while ( dialog.loop )
94         {
95                 gtk_main_iteration();
96         }
97
98         window.hide();
99         gtk_grab_remove( window  );
100
101         return dialog.ret;
102 }
103
104 ui::Button create_modal_dialog_button( const char* label, ModalDialogButton& button ){
105         return create_dialog_button( label, G_CALLBACK( modal_dialog_button_clicked ), &button );
106 }
107
108 ui::Window create_modal_dialog_window( ui::Window parent, const char* title, ModalDialog& dialog, int default_w, int default_h ){
109         return create_dialog_window( parent, title, G_CALLBACK( modal_dialog_delete ), &dialog, default_w, default_h );
110 }
111
112 ui::Window create_fixedsize_modal_dialog_window( ui::Window parent, const char* title, ModalDialog& dialog, int width, int height ){
113         auto window = create_modal_dialog_window( parent, title, dialog, width, height );
114
115         gtk_window_set_resizable( window, FALSE );
116         gtk_window_set_modal( window, TRUE );
117         gtk_window_set_position( window, GTK_WIN_POS_CENTER );
118
119         window_remove_minmax( window );
120
121         //window.dimensions(width, height);
122         //gtk_window_set_default_size(window, width, height);
123         //gtk_window_resize(window, width, height);
124         //GdkGeometry geometry = { width, height, -1, -1, width, height, -1, -1, -1, -1, GDK_GRAVITY_STATIC, };
125         //gtk_window_set_geometry_hints(window, window, &geometry, (GdkWindowHints)(GDK_HINT_POS|GDK_HINT_MIN_SIZE|GDK_HINT_BASE_SIZE));
126
127         return window;
128 }
129
130 gboolean dialog_button_ok( ui::Widget widget, ModalDialog* data ){
131         data->loop = false;
132         data->ret = eIDOK;
133         return TRUE;
134 }
135
136 gboolean dialog_button_cancel( ui::Widget widget, ModalDialog* data ){
137         data->loop = false;
138         data->ret = eIDCANCEL;
139         return TRUE;
140 }
141
142 gboolean dialog_button_yes( ui::Widget widget, ModalDialog* data ){
143         data->loop = false;
144         data->ret = eIDYES;
145         return TRUE;
146 }
147
148 gboolean dialog_button_no( ui::Widget widget, ModalDialog* data ){
149         data->loop = false;
150         data->ret = eIDNO;
151         return TRUE;
152 }
153
154 gboolean dialog_delete_callback( ui::Widget widget, GdkEventAny* event, ModalDialog* data ){
155         widget.hide();
156         data->loop = false;
157         return TRUE;
158 }
159
160 ui::Window create_simple_modal_dialog_window( const char* title, ModalDialog& dialog, ui::Widget contents ){
161         ui::Window window = create_fixedsize_modal_dialog_window(ui::Window{ui::null}, title, dialog );
162
163         auto vbox1 = create_dialog_vbox( 8, 4 );
164         window.add(vbox1);
165
166         vbox1.add(contents);
167
168         ui::Alignment alignment = ui::Alignment( 0.5, 0.0, 0.0, 0.0 );
169         alignment.show();
170         vbox1.pack_start( alignment, FALSE, FALSE, 0 );
171
172         auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
173         alignment.add(button);
174
175         return window;
176 }
177
178 RadioHBox RadioHBox_new( StringArrayRange names ){
179         auto hbox = ui::HBox( TRUE, 4 );
180         hbox.show();
181
182         GSList* group = 0;
183         auto radio = ui::RadioButton(ui::null);
184         for ( StringArrayRange::Iterator i = names.first; i != names.last; ++i )
185         {
186                 radio = ui::RadioButton::from( gtk_radio_button_new_with_label( group, *i ) );
187                 radio.show();
188                 hbox.pack_start( radio, FALSE, FALSE, 0 );
189
190                 group = gtk_radio_button_get_group( radio );
191         }
192
193         return RadioHBox( hbox, radio );
194 }
195
196
197 PathEntry PathEntry_new(){
198         auto frame = ui::Frame();
199         frame.show();
200         gtk_frame_set_shadow_type( frame, GTK_SHADOW_IN );
201
202         // path entry
203         auto hbox = ui::HBox( FALSE, 0 );
204         hbox.show();
205
206         auto entry = ui::Entry(ui::New);
207         gtk_entry_set_has_frame( entry, FALSE );
208         entry.show();
209         hbox.pack_start( entry, TRUE, TRUE, 0 );
210
211         // browse button
212         auto button = ui::Button(ui::New);
213         button_set_icon( button, "ellipsis.png" );
214         button.show();
215         hbox.pack_end(button, FALSE, FALSE, 0);
216
217         frame.add(hbox);
218
219         return PathEntry( frame, entry, button );
220 }
221
222 void PathEntry_setPath( PathEntry& self, const char* path ){
223         gtk_entry_set_text( self.m_entry, path );
224 }
225 typedef ReferenceCaller<PathEntry, void(const char*), PathEntry_setPath> PathEntrySetPathCaller;
226
227 void BrowsedPathEntry_clicked( ui::Widget widget, BrowsedPathEntry* self ){
228         self->m_browse( PathEntrySetPathCaller( self->m_entry ) );
229 }
230
231 BrowsedPathEntry::BrowsedPathEntry( const BrowseCallback& browse ) :
232         m_entry( PathEntry_new() ),
233         m_browse( browse ){
234         m_entry.m_button.connect( "clicked", G_CALLBACK( BrowsedPathEntry_clicked ), this );
235 }
236
237
238 ui::Label DialogLabel_new( const char* name ){
239         auto label = ui::Label( name );
240         label.show();
241         gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 );
242         gtk_label_set_justify( label, GTK_JUSTIFY_LEFT );
243
244         return label;
245 }
246
247 ui::Table DialogRow_new( const char* name, ui::Widget widget ){
248         auto table = ui::Table( 1, 3, TRUE );
249         table.show();
250
251         gtk_table_set_col_spacings( table, 4 );
252         gtk_table_set_row_spacings( table, 0 );
253
254     table.attach(DialogLabel_new(name), {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
255
256     table.attach(widget, {1, 3, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
257
258         return table;
259 }
260
261 void DialogVBox_packRow( ui::Box vbox, ui::Widget row ){
262         vbox.pack_start( row, FALSE, FALSE, 0 );
263 }