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 GtkToggleButton &self, const Callback<void(bool)> &returnz) {
112 returnz(gtk_toggle_button_get_active(const_cast<GtkToggleButton *>(&self)) != FALSE);
115 static void Import(GtkToggleButton &self, bool value) {
116 gtk_toggle_button_set_active(&self, value);
120 using BoolToggleImportExport = PropertyAdaptor<GtkToggleButton, bool, BoolToggle>;
123 static void Export(const GtkEntry &self, const Callback<void(int)> &returnz) {
124 returnz(atoi(gtk_entry_get_text(const_cast<GtkEntry *>(&self))));
127 static void Import(GtkEntry &self, int value) {
128 entry_set_int(ui::Entry(&self), value);
132 using IntEntryImportExport = PropertyAdaptor<GtkEntry, int, IntEntry>;
135 static void Export(const GtkRadioButton &self, const Callback<void(int)> &returnz) {
136 returnz(radio_button_get_active(ui::RadioButton(const_cast<GtkRadioButton *>(&self))));
139 static void Import(GtkRadioButton &self, int value) {
140 radio_button_set_active(ui::RadioButton(&self), value);
144 using IntRadioImportExport = PropertyAdaptor<GtkRadioButton, int, IntRadio>;
147 static void Export(const GtkComboBox &self, const Callback<void(int)> &returnz) {
148 returnz(gtk_combo_box_get_active(const_cast<GtkComboBox *>(&self)));
151 static void Import(GtkComboBox &self, int value) {
152 gtk_combo_box_set_active(&self, value);
156 using IntComboImportExport = PropertyAdaptor<GtkComboBox, int, IntCombo>;
158 struct IntAdjustment {
159 static void Export(const GtkAdjustment &self, const Callback<void(int)> &returnz) {
160 returnz((int) gtk_adjustment_get_value(const_cast<GtkAdjustment *>(&self)));
163 static void Import(GtkAdjustment &self, int value) {
164 gtk_adjustment_set_value(&self, value);
168 using IntAdjustmentImportExport = PropertyAdaptor<GtkAdjustment, int, IntAdjustment>;
171 static void Export(const GtkSpinButton &self, const Callback<void(int)> &returnz) {
172 returnz(gtk_spin_button_get_value_as_int(const_cast<GtkSpinButton *>(&self)));
175 static void Import(GtkSpinButton &self, int value) {
176 gtk_spin_button_set_value(&self, value);
180 using IntSpinnerImportExport = PropertyAdaptor<GtkSpinButton, int, IntSpinner>;
182 using StringImportExport = PropertyAdaptor<CopiedString, const char *>;
185 static void Export(const GtkEntry &self, const Callback<void(const char *)> &returnz) {
186 returnz(gtk_entry_get_text(const_cast<GtkEntry *>(&self)));
189 static void Import(GtkEntry &self, const char *value) {
190 ui::Entry(&self).text(value);
194 using TextEntryImportExport = PropertyAdaptor<GtkEntry, const char *, TextEntry>;
197 static void Export(const GtkEntry &self, const Callback<void(std::size_t)> &returnz) {
198 int value = atoi(gtk_entry_get_text(const_cast<GtkEntry *>(&self)));
205 static void Import(GtkEntry &self, std::size_t value) {
206 entry_set_int(ui::Entry(&self), int(value));
210 using SizeEntryImportExport = PropertyAdaptor<GtkEntry, std::size_t, SizeEntry>;
213 static void Export(const GtkEntry &self, const Callback<void(float)> &returnz) {
214 returnz((float) atof(gtk_entry_get_text(const_cast<GtkEntry *>(&self))));
217 static void Import(GtkEntry &self, float value) {
218 entry_set_float(ui::Entry(&self), value);
222 using FloatEntryImportExport = PropertyAdaptor<GtkEntry, float, FloatEntry>;
224 struct FloatSpinner {
225 static void Export(const GtkSpinButton &self, const Callback<void(float)> &returnz) {
226 returnz(float(gtk_spin_button_get_value(const_cast<GtkSpinButton *>(&self))));
229 static void Import(GtkSpinButton &self, float value) {
230 gtk_spin_button_set_value(&self, value);
234 using FloatSpinnerImportExport = PropertyAdaptor<GtkSpinButton, float, FloatSpinner>;
239 class CallbackDialogData : public DLG_DATA {
240 Property<T> m_cbWidget;
241 Property<T> m_cbViewer;
244 CallbackDialogData(const Property<T> &cbWidget, const Property<T> &cbViewer)
245 : m_cbWidget(cbWidget), m_cbViewer(cbViewer) {
252 void importData() const {
253 m_cbViewer.get(m_cbWidget.set);
256 void exportData() const {
257 m_cbWidget.get(m_cbViewer.set);
261 template<typename Widget, typename Viewer>
262 void AddData(DialogDataList &data, typename Widget::Type &widget, typename Viewer::Type &viewer) {
264 new CallbackDialogData<typename Widget::Other>(
265 make_property<Widget>(widget),
266 make_property<Viewer>(viewer)
271 template<typename Widget>
273 DialogDataList &data,
274 typename Widget::Type &widget,
275 Property<typename Widget::Other> const &cbViewer
278 new CallbackDialogData<typename Widget::Other>(
279 make_property<Widget>(widget),
285 // =============================================================================
288 Dialog::Dialog() : m_window( 0 ), m_parent( 0 ){
292 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
297 ASSERT_MESSAGE( !m_window, "dialog window not destroyed" );
300 void Dialog::ShowDlg(){
301 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
306 void Dialog::HideDlg(){
307 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
312 static gint delete_event_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
313 reinterpret_cast<Dialog*>( data )->HideDlg();
314 reinterpret_cast<Dialog*>( data )->EndModal( eIDCANCEL );
318 void Dialog::Create(){
319 ASSERT_MESSAGE( !m_window, "dialog cannot be constructed" );
321 m_window = BuildDialog();
322 m_window.connect( "delete_event", G_CALLBACK( delete_event_callback ), this );
325 void Dialog::Destroy(){
326 ASSERT_MESSAGE( m_window, "dialog cannot be destroyed" );
329 m_window = ui::Window{ui::null};
333 void Dialog::AddBoolToggleData( GtkToggleButton& widget, Property<bool> const &cb ){
334 AddCustomData<BoolToggleImportExport>( m_data, widget, cb );
337 void Dialog::AddIntRadioData( GtkRadioButton& widget, Property<int> const &cb ){
338 AddCustomData<IntRadioImportExport>( m_data, widget, cb );
341 void Dialog::AddTextEntryData( GtkEntry& widget, Property<const char *> const &cb ){
342 AddCustomData<TextEntryImportExport>( m_data, widget, cb );
345 void Dialog::AddIntEntryData( GtkEntry& widget, Property<int> const &cb ){
346 AddCustomData<IntEntryImportExport>( m_data, widget, cb );
349 void Dialog::AddSizeEntryData( GtkEntry& widget, Property<std::size_t> const &cb ){
350 AddCustomData<SizeEntryImportExport>( m_data, widget, cb );
353 void Dialog::AddFloatEntryData( GtkEntry& widget, Property<float> const &cb ){
354 AddCustomData<FloatEntryImportExport>( m_data, widget, cb );
357 void Dialog::AddFloatSpinnerData( GtkSpinButton& widget, Property<float> const &cb ){
358 AddCustomData<FloatSpinnerImportExport>( m_data, widget, cb );
361 void Dialog::AddIntSpinnerData( GtkSpinButton& widget, Property<int> const &cb ){
362 AddCustomData<IntSpinnerImportExport>( m_data, widget, cb );
365 void Dialog::AddIntAdjustmentData( GtkAdjustment& widget, Property<int> const &cb ){
366 AddCustomData<IntAdjustmentImportExport>( m_data, widget, cb );
369 void Dialog::AddIntComboData( GtkComboBox& widget, Property<int> const &cb ){
370 AddCustomData<IntComboImportExport>( m_data, widget, cb );
374 void Dialog::AddDialogData( GtkToggleButton& widget, bool& data ){
375 AddData<BoolToggleImportExport, PropertyAdaptor<bool>>( m_data, widget, data );
377 void Dialog::AddDialogData( GtkRadioButton& widget, int& data ){
378 AddData<IntRadioImportExport, PropertyAdaptor<int>>( m_data, widget, data );
380 void Dialog::AddDialogData( GtkEntry& widget, CopiedString& data ){
381 AddData<TextEntryImportExport, StringImportExport>( m_data, widget, data );
383 void Dialog::AddDialogData( GtkEntry& widget, int& data ){
384 AddData<IntEntryImportExport, PropertyAdaptor<int>>( m_data, widget, data );
386 void Dialog::AddDialogData( GtkEntry& widget, std::size_t& data ){
387 AddData<SizeEntryImportExport, PropertyAdaptor<std::size_t>>( m_data, widget, data );
389 void Dialog::AddDialogData( GtkEntry& widget, float& data ){
390 AddData<FloatEntryImportExport, PropertyAdaptor<float>>( m_data, widget, data );
392 void Dialog::AddDialogData( GtkSpinButton& widget, float& data ){
393 AddData<FloatSpinnerImportExport, PropertyAdaptor<float>>( m_data, widget, data );
395 void Dialog::AddDialogData( GtkSpinButton& widget, int& data ){
396 AddData<IntSpinnerImportExport, PropertyAdaptor<int>>( m_data, widget, data );
398 void Dialog::AddDialogData( GtkAdjustment& widget, int& data ){
399 AddData<IntAdjustmentImportExport, PropertyAdaptor<int>>( m_data, widget, data );
401 void Dialog::AddDialogData( GtkComboBox& widget, int& data ){
402 AddData<IntComboImportExport, PropertyAdaptor<int>>( m_data, widget, data );
405 void Dialog::exportData(){
406 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
408 ( *i )->exportData();
412 void Dialog::importData(){
413 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
415 ( *i )->importData();
419 void Dialog::EndModal( EMessageBoxReturn code ){
424 EMessageBoxReturn Dialog::DoModal(){
429 EMessageBoxReturn ret = modal_dialog_show( m_window, m_modal );
430 ASSERT_TRUE( (bool) m_window );
431 if ( ret == eIDOK ) {
437 PostModal( m_modal.ret );
443 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, Property<bool> const &cb ){
444 auto check = ui::CheckButton( flag );
446 AddBoolToggleData( *GTK_TOGGLE_BUTTON( check ), cb );
448 DialogVBox_packRow( vbox, ui::Widget(DialogRow_new( name, check ) ));
452 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, bool& data ){
453 return addCheckBox(vbox, name, flag, make_property(data));
456 void Dialog::addCombo( ui::VBox vbox, const char* name, StringArrayRange values, Property<int> const &cb ){
457 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
460 auto combo = ui::ComboBoxText(ui::New);
462 for ( StringArrayRange::Iterator i = values.first; i != values.last; ++i )
464 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), *i );
467 AddIntComboData( *GTK_COMBO_BOX( combo ), cb );
470 alignment.add(combo);
473 auto row = DialogRow_new( name, alignment );
474 DialogVBox_packRow( vbox, row );
477 void Dialog::addCombo( ui::VBox vbox, const char* name, int& data, StringArrayRange values ){
478 addCombo(vbox, name, values, make_property(data));
481 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 ){
483 if ( draw_value == FALSE ) {
484 auto hbox2 = ui::HBox( FALSE, 0 );
486 vbox.pack_start( hbox2 , FALSE, FALSE, 0 );
488 ui::Widget label = ui::Label( low );
490 hbox2.pack_start( label, FALSE, FALSE, 0 );
493 ui::Widget label = ui::Label( high );
495 hbox2.pack_end(label, FALSE, FALSE, 0);
501 auto adj = ui::Adjustment( value, lower, upper, step_increment, page_increment, 0 );
502 AddIntAdjustmentData(*GTK_ADJUSTMENT(adj), make_property(data));
505 auto alignment = ui::Alignment( 0.0, 0.5, 1.0, 0.0 );
508 ui::Widget scale = ui::HScale( adj );
509 gtk_scale_set_value_pos( GTK_SCALE( scale ), GTK_POS_LEFT );
511 alignment.add(scale);
513 gtk_scale_set_draw_value( GTK_SCALE( scale ), draw_value );
514 gtk_scale_set_digits( GTK_SCALE( scale ), 0 );
516 auto row = DialogRow_new( name, alignment );
517 DialogVBox_packRow( vbox, row );
520 void Dialog::addRadio( ui::VBox vbox, const char* name, StringArrayRange names, Property<int> const &cb ){
521 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
524 RadioHBox radioBox = RadioHBox_new( names );
525 alignment.add(radioBox.m_hbox);
526 AddIntRadioData( *GTK_RADIO_BUTTON( radioBox.m_radio ), cb );
529 auto row = DialogRow_new( name, alignment );
530 DialogVBox_packRow( vbox, row );
533 void Dialog::addRadio( ui::VBox vbox, const char* name, int& data, StringArrayRange names ){
534 addRadio(vbox, name, names, make_property(data));
537 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, StringArrayRange icons, Property<int> const &cb ){
538 auto table = ui::Table(2, icons.last - icons.first, FALSE);
541 gtk_table_set_row_spacings(table, 5);
542 gtk_table_set_col_spacings(table, 5);
545 ui::RadioButton radio{ui::null};
546 for ( StringArrayRange::Iterator icon = icons.first; icon != icons.last; ++icon )
548 guint pos = static_cast<guint>( icon - icons.first );
549 auto image = new_local_image( *icon );
551 table.attach(image, {pos, pos + 1, 0, 1}, {0, 0});
553 radio = ui::RadioButton(GTK_RADIO_BUTTON(gtk_radio_button_new( group )));
555 table.attach(radio, {pos, pos + 1, 1, 2}, {0, 0});
557 group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( radio ) );
560 AddIntRadioData( *GTK_RADIO_BUTTON( radio ), cb );
562 DialogVBox_packRow( vbox, DialogRow_new( name, table ) );
565 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, int& data, StringArrayRange icons ){
566 addRadioIcons(vbox, name, icons, make_property(data));
569 ui::Widget Dialog::addIntEntry( ui::VBox vbox, const char* name, Property<int> const &cb ){
570 DialogEntryRow row( DialogEntryRow_new( name ) );
571 AddIntEntryData( *GTK_ENTRY(row.m_entry), cb );
572 DialogVBox_packRow( vbox, row.m_row );
576 ui::Widget Dialog::addSizeEntry( ui::VBox vbox, const char* name, Property<std::size_t> const &cb ){
577 DialogEntryRow row( DialogEntryRow_new( name ) );
578 AddSizeEntryData( *GTK_ENTRY(row.m_entry), cb );
579 DialogVBox_packRow( vbox, row.m_row );
583 ui::Widget Dialog::addFloatEntry( ui::VBox vbox, const char* name, Property<float> const &cb ){
584 DialogEntryRow row( DialogEntryRow_new( name ) );
585 AddFloatEntryData( *GTK_ENTRY(row.m_entry), cb );
586 DialogVBox_packRow( vbox, row.m_row );
590 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, bool browse_directory, Property<const char *> const &cb ){
591 PathEntry pathEntry = PathEntry_new();
592 pathEntry.m_button.connect( "clicked", G_CALLBACK( browse_directory ? button_clicked_entry_browse_directory : button_clicked_entry_browse_file ), pathEntry.m_entry );
594 AddTextEntryData( *GTK_ENTRY(pathEntry.m_entry), cb );
596 auto row = DialogRow_new( name, ui::Widget(pathEntry.m_frame ) );
597 DialogVBox_packRow( vbox, row );
602 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, CopiedString& data, bool browse_directory ){
603 return addPathEntry(vbox, name, browse_directory, make_property<CopiedString, const char *>(data));
606 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<int> const &cb ){
607 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 1 ) );
608 AddIntSpinnerData( *GTK_SPIN_BUTTON(row.m_spin), cb );
609 DialogVBox_packRow( vbox, row.m_row );
613 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, int& data, double value, double lower, double upper ){
614 return addSpinner(vbox, name, value, lower, upper, make_property(data));
617 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<float> const &cb ){
618 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 10 ) );
619 AddFloatSpinnerData( *GTK_SPIN_BUTTON(row.m_spin), cb );
620 DialogVBox_packRow( vbox, row.m_row );