| 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 | window.show(); |
| 90 | g_assert( GTK_IS_WINDOW(window) ); |
| 91 | gtk_grab_add( GTK_WIDGET(window) ); |
| 92 | |
| 93 | dialog.loop = true; |
| 94 | while ( dialog.loop ) |
| 95 | { |
| 96 | gtk_main_iteration(); |
| 97 | } |
| 98 | |
| 99 | window.hide(); |
| 100 | gtk_grab_remove( window ); |
| 101 | |
| 102 | return dialog.ret; |
| 103 | } |
| 104 | |
| 105 | ui::Button create_modal_dialog_button( const char* label, ModalDialogButton& button ){ |
| 106 | return create_dialog_button( label, G_CALLBACK( modal_dialog_button_clicked ), &button ); |
| 107 | } |
| 108 | |
| 109 | ui::Window create_modal_dialog_window( ui::Window parent, const char* title, ModalDialog& dialog, int default_w, int default_h ){ |
| 110 | return create_dialog_window( parent, title, G_CALLBACK( modal_dialog_delete ), &dialog, default_w, default_h ); |
| 111 | } |
| 112 | |
| 113 | ui::Window create_fixedsize_modal_dialog_window( ui::Window parent, const char* title, ModalDialog& dialog, int width, int height ){ |
| 114 | auto window = create_modal_dialog_window( parent, title, dialog, width, height ); |
| 115 | |
| 116 | gtk_window_set_resizable( window, FALSE ); |
| 117 | gtk_window_set_modal( window, TRUE ); |
| 118 | gtk_window_set_position( window, GTK_WIN_POS_CENTER ); |
| 119 | |
| 120 | window_remove_minmax( window ); |
| 121 | |
| 122 | //window.dimensions(width, height); |
| 123 | //gtk_window_set_default_size(window, width, height); |
| 124 | //gtk_window_resize(window, width, height); |
| 125 | //GdkGeometry geometry = { width, height, -1, -1, width, height, -1, -1, -1, -1, GDK_GRAVITY_STATIC, }; |
| 126 | //gtk_window_set_geometry_hints(window, window, &geometry, (GdkWindowHints)(GDK_HINT_POS|GDK_HINT_MIN_SIZE|GDK_HINT_BASE_SIZE)); |
| 127 | |
| 128 | return window; |
| 129 | } |
| 130 | |
| 131 | gboolean dialog_button_ok( ui::Widget widget, ModalDialog* data ){ |
| 132 | data->loop = false; |
| 133 | data->ret = eIDOK; |
| 134 | return TRUE; |
| 135 | } |
| 136 | |
| 137 | gboolean dialog_button_cancel( ui::Widget widget, ModalDialog* data ){ |
| 138 | data->loop = false; |
| 139 | data->ret = eIDCANCEL; |
| 140 | return TRUE; |
| 141 | } |
| 142 | |
| 143 | gboolean dialog_button_yes( ui::Widget widget, ModalDialog* data ){ |
| 144 | data->loop = false; |
| 145 | data->ret = eIDYES; |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | gboolean dialog_button_no( ui::Widget widget, ModalDialog* data ){ |
| 150 | data->loop = false; |
| 151 | data->ret = eIDNO; |
| 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | gboolean dialog_delete_callback( ui::Widget widget, GdkEventAny* event, ModalDialog* data ){ |
| 156 | widget.hide(); |
| 157 | data->loop = false; |
| 158 | return TRUE; |
| 159 | } |
| 160 | |
| 161 | ui::Window create_simple_modal_dialog_window( const char* title, ModalDialog& dialog, ui::Widget contents ){ |
| 162 | ui::Window window = create_fixedsize_modal_dialog_window(ui::Window{ui::null}, title, dialog ); |
| 163 | |
| 164 | auto vbox1 = create_dialog_vbox( 8, 4 ); |
| 165 | window.add(vbox1); |
| 166 | |
| 167 | vbox1.add(contents); |
| 168 | |
| 169 | ui::Alignment alignment = ui::Alignment( 0.5, 0.0, 0.0, 0.0 ); |
| 170 | alignment.show(); |
| 171 | vbox1.pack_start( alignment, FALSE, FALSE, 0 ); |
| 172 | |
| 173 | auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog ); |
| 174 | alignment.add(button); |
| 175 | gtk_widget_grab_focus (button); |
| 176 | |
| 177 | return window; |
| 178 | } |
| 179 | |
| 180 | RadioHBox RadioHBox_new( StringArrayRange names ){ |
| 181 | auto hbox = ui::HBox( TRUE, 4 ); |
| 182 | hbox.show(); |
| 183 | |
| 184 | GSList* group = 0; |
| 185 | auto radio = ui::RadioButton(ui::null); |
| 186 | for ( StringArrayRange::Iterator i = names.first; i != names.last; ++i ) |
| 187 | { |
| 188 | radio = ui::RadioButton::from( gtk_radio_button_new_with_label( group, *i ) ); |
| 189 | radio.show(); |
| 190 | hbox.pack_start( radio, FALSE, FALSE, 0 ); |
| 191 | |
| 192 | group = gtk_radio_button_get_group( radio ); |
| 193 | } |
| 194 | |
| 195 | return RadioHBox( hbox, radio ); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | PathEntry PathEntry_new(){ |
| 200 | auto frame = ui::Frame(); |
| 201 | frame.show(); |
| 202 | gtk_frame_set_shadow_type( frame, GTK_SHADOW_IN ); |
| 203 | |
| 204 | // path entry |
| 205 | auto hbox = ui::HBox( FALSE, 0 ); |
| 206 | hbox.show(); |
| 207 | |
| 208 | auto entry = ui::Entry(ui::New); |
| 209 | gtk_entry_set_has_frame( entry, FALSE ); |
| 210 | entry.show(); |
| 211 | hbox.pack_start( entry, TRUE, TRUE, 0 ); |
| 212 | |
| 213 | // browse button |
| 214 | auto button = ui::Button(ui::New); |
| 215 | button_set_icon( button, "ellipsis.png" ); |
| 216 | button.show(); |
| 217 | hbox.pack_end(button, FALSE, FALSE, 0); |
| 218 | |
| 219 | frame.add(hbox); |
| 220 | |
| 221 | return PathEntry( frame, entry, button ); |
| 222 | } |
| 223 | |
| 224 | void PathEntry_setPath( PathEntry& self, const char* path ){ |
| 225 | gtk_entry_set_text( self.m_entry, path ); |
| 226 | } |
| 227 | typedef ReferenceCaller<PathEntry, void(const char*), PathEntry_setPath> PathEntrySetPathCaller; |
| 228 | |
| 229 | void BrowsedPathEntry_clicked( ui::Widget widget, BrowsedPathEntry* self ){ |
| 230 | self->m_browse( PathEntrySetPathCaller( self->m_entry ) ); |
| 231 | } |
| 232 | |
| 233 | BrowsedPathEntry::BrowsedPathEntry( const BrowseCallback& browse ) : |
| 234 | m_entry( PathEntry_new() ), |
| 235 | m_browse( browse ){ |
| 236 | m_entry.m_button.connect( "clicked", G_CALLBACK( BrowsedPathEntry_clicked ), this ); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | ui::Label DialogLabel_new( const char* name ){ |
| 241 | auto label = ui::Label( name ); |
| 242 | label.show(); |
| 243 | gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 ); |
| 244 | gtk_label_set_justify( label, GTK_JUSTIFY_LEFT ); |
| 245 | |
| 246 | return label; |
| 247 | } |
| 248 | |
| 249 | ui::Table DialogRow_new( const char* name, ui::Widget widget ){ |
| 250 | auto table = ui::Table( 1, 3, TRUE ); |
| 251 | table.show(); |
| 252 | |
| 253 | gtk_table_set_col_spacings( table, 4 ); |
| 254 | gtk_table_set_row_spacings( table, 0 ); |
| 255 | |
| 256 | table.attach(DialogLabel_new(name), {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0}); |
| 257 | |
| 258 | table.attach(widget, {1, 3, 0, 1}, {GTK_EXPAND | GTK_FILL, 0}); |
| 259 | |
| 260 | return table; |
| 261 | } |
| 262 | |
| 263 | void DialogVBox_packRow( ui::Box vbox, ui::Widget row ){ |
| 264 | vbox.pack_start( row, FALSE, FALSE, 0 ); |
| 265 | } |