2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
23 // Base dialog class, provides a way to run modal dialogs and
24 // set/get the widget values in member variables.
26 // Leonardo Zide (leo@lokigames.com)
33 #include "debugging/debugging.h"
36 #include "mainframe.h"
40 #include "stream/stringstream.h"
42 #include "gtkutil/dialog.h"
43 #include "gtkutil/button.h"
44 #include "gtkutil/entry.h"
45 #include "gtkutil/image.h"
50 ui::Entry DialogEntry_new(){
51 auto entry = ui::Entry(ui::New);
53 entry.dimensions(64, -1);
60 DialogEntryRow( ui::Widget row, ui::Entry entry ) : m_row( row ), m_entry( entry ){
66 DialogEntryRow DialogEntryRow_new( const char* name ){
67 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
70 auto entry = DialogEntry_new();
73 return DialogEntryRow( ui::Widget(DialogRow_new( name, alignment )), entry );
77 ui::SpinButton DialogSpinner_new( double value, double lower, double upper, int fraction ){
78 double step = 1.0 / double(fraction);
79 unsigned int digits = 0;
80 for (; fraction > 1; fraction /= 10 )
84 auto spin = ui::SpinButton( ui::Adjustment( value, lower, upper, step, 10, 0 ), step, digits );
86 spin.dimensions(64, -1);
90 class DialogSpinnerRow
93 DialogSpinnerRow( ui::Widget row, ui::SpinButton spin ) : m_row( row ), m_spin( spin ){
96 ui::SpinButton m_spin;
99 DialogSpinnerRow DialogSpinnerRow_new( const char* name, double value, double lower, double upper, int fraction ){
100 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
103 auto spin = DialogSpinner_new( value, lower, upper, fraction );
106 return DialogSpinnerRow( ui::Widget(DialogRow_new( name, alignment )), spin );
111 static void Export(const ui::ToggleButton &self, const Callback<void(bool)> &returnz) {
112 returnz(self.active());
115 static void Import(ui::ToggleButton &self, bool value) {
120 using BoolToggleImportExport = PropertyAdaptor<ui::ToggleButton, bool, BoolToggle>;
123 static void Export(const ui::Entry &self, const Callback<void(int)> &returnz) {
124 returnz(atoi(gtk_entry_get_text(self)));
127 static void Import(ui::Entry &self, int value) {
128 entry_set_int(self, value);
132 using IntEntryImportExport = PropertyAdaptor<ui::Entry, int, IntEntry>;
135 static void Export(const ui::RadioButton &self, const Callback<void(int)> &returnz) {
136 returnz(radio_button_get_active(self));
139 static void Import(ui::RadioButton &self, int value) {
140 radio_button_set_active(self, value);
144 using IntRadioImportExport = PropertyAdaptor<ui::RadioButton, int, IntRadio>;
147 static void Export(const ui::ComboBox &self, const Callback<void(int)> &returnz) {
148 returnz(gtk_combo_box_get_active(self));
151 static void Import(ui::ComboBox &self, int value) {
152 gtk_combo_box_set_active(self, value);
156 using IntComboImportExport = PropertyAdaptor<ui::ComboBox, int, IntCombo>;
158 struct IntAdjustment {
159 static void Export(const ui::Adjustment &self, const Callback<void(int)> &returnz) {
160 returnz(int(gtk_adjustment_get_value(self)));
163 static void Import(ui::Adjustment &self, int value) {
164 gtk_adjustment_set_value(self, value);
168 using IntAdjustmentImportExport = PropertyAdaptor<ui::Adjustment, int, IntAdjustment>;
171 static void Export(const ui::SpinButton &self, const Callback<void(int)> &returnz) {
172 returnz(gtk_spin_button_get_value_as_int(self));
175 static void Import(ui::SpinButton &self, int value) {
176 gtk_spin_button_set_value(self, value);
180 using IntSpinnerImportExport = PropertyAdaptor<ui::SpinButton, int, IntSpinner>;
183 static void Export(const ui::Entry &self, const Callback<void(const char *)> &returnz) {
184 returnz(gtk_entry_get_text(self));
187 static void Import(ui::Entry &self, const char *value) {
192 using TextEntryImportExport = PropertyAdaptor<ui::Entry, const char *, TextEntry>;
195 static void Export(const ui::Entry &self, const Callback<void(std::size_t)> &returnz) {
196 int value = atoi(gtk_entry_get_text(self));
203 static void Import(ui::Entry &self, std::size_t value) {
204 entry_set_int(self, int(value));
208 using SizeEntryImportExport = PropertyAdaptor<ui::Entry, std::size_t, SizeEntry>;
211 static void Export(const ui::Entry &self, const Callback<void(float)> &returnz) {
212 returnz(float(atof(gtk_entry_get_text(self))));
215 static void Import(ui::Entry &self, float value) {
216 entry_set_float(self, value);
220 using FloatEntryImportExport = PropertyAdaptor<ui::Entry, float, FloatEntry>;
222 struct FloatSpinner {
223 static void Export(const ui::SpinButton &self, const Callback<void(float)> &returnz) {
224 returnz(float(gtk_spin_button_get_value(self)));
227 static void Import(ui::SpinButton &self, float value) {
228 gtk_spin_button_set_value(self, value);
232 using FloatSpinnerImportExport = PropertyAdaptor<ui::SpinButton, float, FloatSpinner>;
237 class CallbackDialogData : public DLG_DATA {
238 Property<T> m_pWidget;
242 CallbackDialogData(const Property<T> &pWidget, const Property<T> &pData)
243 : m_pWidget(pWidget), m_pData(pData) {
250 void importData() const {
251 m_pData.get(m_pWidget.set);
254 void exportData() const {
255 m_pWidget.get(m_pData.set);
259 template<class Widget>
260 void AddDataCustom(DialogDataList &self, typename Widget::Type widget, Property<typename Widget::Other> const &property) {
261 using Self = typename Widget::Type;
262 using T = typename Widget::Other;
263 using native = typename std::remove_pointer<typename Self::native>::type;
265 static void Export(const native &self, const Callback<void(T)> &returnz) {
266 native *p = &const_cast<native &>(self);
267 auto widget = Self::from(p);
268 Widget::Get::thunk_(widget, returnz);
271 static void Import(native &self, T value) {
273 auto widget = Self::from(p);
274 Widget::Set::thunk_(widget, value);
277 self.push_back(new CallbackDialogData<typename Widget::Other>(
278 make_property<PropertyAdaptor<native, T, Wrapper>>(*static_cast<native *>(widget)),
283 template<class Widget, class D>
284 void AddData(DialogDataList &self, typename Widget::Type widget, D &data) {
285 AddDataCustom<Widget>(self, widget, make_property<PropertyAdaptor<D, typename Widget::Other>>(data));
288 // =============================================================================
291 Dialog::Dialog() : m_window( ui::null ), m_parent( ui::null ){
295 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
300 ASSERT_MESSAGE( !m_window, "dialog window not destroyed" );
303 void Dialog::ShowDlg(){
304 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
309 void Dialog::HideDlg(){
310 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
315 static gint delete_event_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
316 reinterpret_cast<Dialog*>( data )->HideDlg();
317 reinterpret_cast<Dialog*>( data )->EndModal( eIDCANCEL );
321 void Dialog::Create(){
322 ASSERT_MESSAGE( !m_window, "dialog cannot be constructed" );
324 m_window = BuildDialog();
325 m_window.connect( "delete_event", G_CALLBACK( delete_event_callback ), this );
328 void Dialog::Destroy(){
329 ASSERT_MESSAGE( m_window, "dialog cannot be destroyed" );
332 m_window = ui::Window{ui::null};
336 void Dialog::AddBoolToggleData( ui::ToggleButton widget, Property<bool> const &cb ){
337 AddDataCustom<BoolToggleImportExport>( m_data, widget, cb );
340 void Dialog::AddIntRadioData( ui::RadioButton widget, Property<int> const &cb ){
341 AddDataCustom<IntRadioImportExport>( m_data, widget, cb );
344 void Dialog::AddTextEntryData( ui::Entry widget, Property<const char *> const &cb ){
345 AddDataCustom<TextEntryImportExport>( m_data, widget, cb );
348 void Dialog::AddIntEntryData( ui::Entry widget, Property<int> const &cb ){
349 AddDataCustom<IntEntryImportExport>( m_data, widget, cb );
352 void Dialog::AddSizeEntryData( ui::Entry widget, Property<std::size_t> const &cb ){
353 AddDataCustom<SizeEntryImportExport>( m_data, widget, cb );
356 void Dialog::AddFloatEntryData( ui::Entry widget, Property<float> const &cb ){
357 AddDataCustom<FloatEntryImportExport>( m_data, widget, cb );
360 void Dialog::AddFloatSpinnerData( ui::SpinButton widget, Property<float> const &cb ){
361 AddDataCustom<FloatSpinnerImportExport>( m_data, widget, cb );
364 void Dialog::AddIntSpinnerData( ui::SpinButton widget, Property<int> const &cb ){
365 AddDataCustom<IntSpinnerImportExport>( m_data, widget, cb );
368 void Dialog::AddIntAdjustmentData( ui::Adjustment widget, Property<int> const &cb ){
369 AddDataCustom<IntAdjustmentImportExport>( m_data, widget, cb );
372 void Dialog::AddIntComboData( ui::ComboBox widget, Property<int> const &cb ){
373 AddDataCustom<IntComboImportExport>( m_data, widget, cb );
377 void Dialog::AddDialogData( ui::ToggleButton widget, bool& data ){
378 AddData<BoolToggleImportExport>( m_data, widget, data );
380 void Dialog::AddDialogData( ui::RadioButton widget, int& data ){
381 AddData<IntRadioImportExport>( m_data, widget, data );
383 void Dialog::AddDialogData( ui::Entry widget, CopiedString& data ){
384 AddData<TextEntryImportExport>( m_data, widget, data );
386 void Dialog::AddDialogData( ui::Entry widget, int& data ){
387 AddData<IntEntryImportExport>( m_data, widget, data );
389 void Dialog::AddDialogData( ui::Entry widget, std::size_t& data ){
390 AddData<SizeEntryImportExport>( m_data, widget, data );
392 void Dialog::AddDialogData( ui::Entry widget, float& data ){
393 AddData<FloatEntryImportExport>( m_data, widget, data );
395 void Dialog::AddDialogData( ui::SpinButton widget, float& data ){
396 AddData<FloatSpinnerImportExport>( m_data, widget, data );
398 void Dialog::AddDialogData( ui::SpinButton widget, int& data ){
399 AddData<IntSpinnerImportExport>( m_data, widget, data );
401 void Dialog::AddDialogData( ui::Adjustment widget, int& data ){
402 AddData<IntAdjustmentImportExport>( m_data, widget, data );
404 void Dialog::AddDialogData( ui::ComboBox widget, int& data ){
405 AddData<IntComboImportExport>( m_data, widget, data );
408 void Dialog::exportData(){
409 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
411 ( *i )->exportData();
415 void Dialog::importData(){
416 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
418 ( *i )->importData();
422 void Dialog::EndModal( EMessageBoxReturn code ){
427 EMessageBoxReturn Dialog::DoModal(){
432 EMessageBoxReturn ret = modal_dialog_show( m_window, m_modal );
433 ASSERT_TRUE( (bool) m_window );
434 if ( ret == eIDOK ) {
440 PostModal( m_modal.ret );
446 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, Property<bool> const &cb ){
447 auto check = ui::CheckButton( flag );
449 AddBoolToggleData( check, cb );
451 DialogVBox_packRow( vbox, ui::Widget(DialogRow_new( name, check ) ));
455 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, bool& data ){
456 return addCheckBox(vbox, name, flag, make_property(data));
459 void Dialog::addCombo( ui::VBox vbox, const char* name, StringArrayRange values, Property<int> const &cb ){
460 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
463 auto combo = ui::ComboBoxText(ui::New);
465 for ( StringArrayRange::Iterator i = values.first; i != values.last; ++i )
467 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), *i );
470 AddIntComboData( combo, cb );
473 alignment.add(combo);
476 auto row = DialogRow_new( name, alignment );
477 DialogVBox_packRow( vbox, row );
480 void Dialog::addCombo( ui::VBox vbox, const char* name, int& data, StringArrayRange values ){
481 addCombo(vbox, name, values, make_property(data));
484 void Dialog::addSlider( ui::VBox vbox, const char* name, int& data, gboolean draw_value, const char* low, const char* high, double value, double lower, double upper, double step_increment, double page_increment ){
486 if ( draw_value == FALSE ) {
487 auto hbox2 = ui::HBox( FALSE, 0 );
489 vbox.pack_start( hbox2 , FALSE, FALSE, 0 );
491 ui::Widget label = ui::Label( low );
493 hbox2.pack_start( label, FALSE, FALSE, 0 );
496 ui::Widget label = ui::Label( high );
498 hbox2.pack_end(label, FALSE, FALSE, 0);
504 auto adj = ui::Adjustment( value, lower, upper, step_increment, page_increment, 0 );
505 AddIntAdjustmentData(adj, make_property(data));
508 auto alignment = ui::Alignment( 0.0, 0.5, 1.0, 0.0 );
511 ui::Widget scale = ui::HScale( adj );
512 gtk_scale_set_value_pos( GTK_SCALE( scale ), GTK_POS_LEFT );
514 alignment.add(scale);
516 gtk_scale_set_draw_value( GTK_SCALE( scale ), draw_value );
517 gtk_scale_set_digits( GTK_SCALE( scale ), 0 );
519 auto row = DialogRow_new( name, alignment );
520 DialogVBox_packRow( vbox, row );
523 void Dialog::addRadio( ui::VBox vbox, const char* name, StringArrayRange names, Property<int> const &cb ){
524 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
527 RadioHBox radioBox = RadioHBox_new( names );
528 alignment.add(radioBox.m_hbox);
529 AddIntRadioData( radioBox.m_radio, cb );
532 auto row = DialogRow_new( name, alignment );
533 DialogVBox_packRow( vbox, row );
536 void Dialog::addRadio( ui::VBox vbox, const char* name, int& data, StringArrayRange names ){
537 addRadio(vbox, name, names, make_property(data));
540 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, StringArrayRange icons, Property<int> const &cb ){
541 auto table = ui::Table(2, icons.last - icons.first, FALSE);
544 gtk_table_set_row_spacings(table, 5);
545 gtk_table_set_col_spacings(table, 5);
548 ui::RadioButton radio{ui::null};
549 for ( StringArrayRange::Iterator icon = icons.first; icon != icons.last; ++icon )
551 guint pos = static_cast<guint>( icon - icons.first );
552 auto image = new_local_image( *icon );
554 table.attach(image, {pos, pos + 1, 0, 1}, {0, 0});
556 radio = ui::RadioButton::from(gtk_radio_button_new( group ));
558 table.attach(radio, {pos, pos + 1, 1, 2}, {0, 0});
560 group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( radio ) );
563 AddIntRadioData( radio, cb );
565 DialogVBox_packRow( vbox, DialogRow_new( name, table ) );
568 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, int& data, StringArrayRange icons ){
569 addRadioIcons(vbox, name, icons, make_property(data));
572 ui::Widget Dialog::addIntEntry( ui::VBox vbox, const char* name, Property<int> const &cb ){
573 DialogEntryRow row( DialogEntryRow_new( name ) );
574 AddIntEntryData( row.m_entry, cb );
575 DialogVBox_packRow( vbox, row.m_row );
579 ui::Widget Dialog::addSizeEntry( ui::VBox vbox, const char* name, Property<std::size_t> const &cb ){
580 DialogEntryRow row( DialogEntryRow_new( name ) );
581 AddSizeEntryData( row.m_entry, cb );
582 DialogVBox_packRow( vbox, row.m_row );
586 ui::Widget Dialog::addFloatEntry( ui::VBox vbox, const char* name, Property<float> const &cb ){
587 DialogEntryRow row( DialogEntryRow_new( name ) );
588 AddFloatEntryData( row.m_entry, cb );
589 DialogVBox_packRow( vbox, row.m_row );
593 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, bool browse_directory, Property<const char *> const &cb ){
594 PathEntry pathEntry = PathEntry_new();
595 pathEntry.m_button.connect( "clicked", G_CALLBACK( browse_directory ? button_clicked_entry_browse_directory : button_clicked_entry_browse_file ), pathEntry.m_entry );
597 AddTextEntryData( pathEntry.m_entry, cb );
599 auto row = DialogRow_new( name, ui::Widget(pathEntry.m_frame ) );
600 DialogVBox_packRow( vbox, row );
605 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, CopiedString& data, bool browse_directory ){
606 return addPathEntry(vbox, name, browse_directory, make_property<CopiedString, const char *>(data));
609 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<int> const &cb ){
610 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 1 ) );
611 AddIntSpinnerData( row.m_spin, cb );
612 DialogVBox_packRow( vbox, row.m_row );
616 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, int& data, double value, double lower, double upper ){
617 return addSpinner(vbox, name, value, lower, upper, make_property(data));
620 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<float> const &cb ){
621 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 10 ) );
622 AddFloatSpinnerData( row.m_spin, cb );
623 DialogVBox_packRow( vbox, row.m_row );