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 );
112 typename Other_ = Type_,
113 class T = impexp<Type_, Other_>
118 using Other = Other_;
120 using ImportCaller = ReferenceCaller<Type, void(Other), T::Import>;
121 using ExportCaller = ReferenceCaller<Type, void(const Callback<void(Other)> &), T::Export>;
125 using BoolImportExport = ImportExport<bool>;
128 static void Import(GtkToggleButton &widget, bool value) {
129 gtk_toggle_button_set_active(&widget, value);
132 static void Export(GtkToggleButton &widget, const ImportExportCallback<bool>::Import_t &importCallback) {
133 importCallback(gtk_toggle_button_get_active(&widget) != FALSE);
137 using BoolToggleImportExport = ImportExport<GtkToggleButton, bool, BoolToggle>;
139 using IntImportExport = ImportExport<int>;
142 static void Import(GtkEntry &widget, int value) {
143 entry_set_int(ui::Entry(&widget), value);
146 static void Export(GtkEntry &widget, const ImportExportCallback<int>::Import_t &importCallback) {
147 importCallback(atoi(gtk_entry_get_text(&widget)));
151 using IntEntryImportExport = ImportExport<GtkEntry, int, IntEntry>;
154 static void Import(GtkRadioButton &widget, int index) {
155 radio_button_set_active(ui::RadioButton(&widget), index);
158 static void Export(GtkRadioButton &widget, const ImportExportCallback<int>::Import_t &importCallback) {
159 importCallback(radio_button_get_active(ui::RadioButton(&widget)));
163 using IntRadioImportExport = ImportExport<GtkRadioButton, int, IntRadio>;
166 static void Import(GtkComboBox &widget, int value) {
167 gtk_combo_box_set_active(&widget, value);
170 static void Export(GtkComboBox &widget, const ImportExportCallback<int>::Import_t &importCallback) {
171 importCallback(gtk_combo_box_get_active(&widget));
175 using IntComboImportExport = ImportExport<GtkComboBox, int, IntCombo>;
177 struct IntAdjustment {
178 static void Import(GtkAdjustment &widget, int value) {
179 gtk_adjustment_set_value(&widget, value);
182 static void Export(GtkAdjustment &widget, const ImportExportCallback<int>::Import_t &importCallback) {
183 importCallback((int) gtk_adjustment_get_value(&widget));
187 using IntAdjustmentImportExport = ImportExport<GtkAdjustment, int, IntAdjustment>;
190 static void Import(GtkSpinButton &widget, int value) {
191 gtk_spin_button_set_value(&widget, value);
194 static void Export(GtkSpinButton &widget, const ImportExportCallback<int>::Import_t &importCallback) {
195 importCallback(gtk_spin_button_get_value_as_int(&widget));
199 using IntSpinnerImportExport = ImportExport<GtkSpinButton, int, IntSpinner>;
201 using StringImportExport = ImportExport<CopiedString, const char *>;
204 static void Import(GtkEntry &widget, const char *text) {
205 ui::Entry(&widget).text(text);
208 static void Export(GtkEntry &widget, const ImportExportCallback<const char *>::Import_t &importCallback) {
209 importCallback(gtk_entry_get_text(&widget));
213 using TextEntryImportExport = ImportExport<GtkEntry, const char *, TextEntry>;
215 using SizeImportExport = ImportExport<std::size_t>;
218 static void Import(GtkEntry &widget, std::size_t value) {
219 entry_set_int(ui::Entry(&widget), int(value));
222 static void Export(GtkEntry &widget, const ImportExportCallback<std::size_t>::Import_t &importCallback) {
223 int value = atoi(gtk_entry_get_text(&widget));
227 importCallback(value);
231 using SizeEntryImportExport = ImportExport<GtkEntry, std::size_t, SizeEntry>;
233 using FloatImportExport = ImportExport<float>;
236 static void Import(GtkEntry &widget, float value) {
237 entry_set_float(ui::Entry(&widget), value);
240 static void Export(GtkEntry &widget, const ImportExportCallback<float>::Import_t &importCallback) {
241 importCallback((float) atof(gtk_entry_get_text(&widget)));
245 using FloatEntryImportExport = ImportExport<GtkEntry, float, FloatEntry>;
247 struct FloatSpinner {
248 static void Import(GtkSpinButton &widget, float value) {
249 gtk_spin_button_set_value(&widget, value);
252 static void Export(GtkSpinButton &widget, const ImportExportCallback<float>::Import_t &importCallback) {
253 importCallback(float(gtk_spin_button_get_value(&widget)));
257 using FloatSpinnerImportExport = ImportExport<GtkSpinButton, float, FloatSpinner>;
262 class CallbackDialogData : public DLG_DATA {
263 ImportExportCallback<T> m_cbWidget;
264 ImportExportCallback<T> m_cbViewer;
267 CallbackDialogData(const ImportExportCallback<T> &cbWidget, const ImportExportCallback<T> &cbViewer)
268 : m_cbWidget(cbWidget), m_cbViewer(cbViewer) {
275 void importData() const {
276 m_cbViewer.Export(m_cbWidget.Import);
279 void exportData() const {
280 m_cbWidget.Export(m_cbViewer.Import);
284 template<typename Widget, typename Viewer>
285 void AddData(DialogDataList &data, typename Widget::Type &widget, typename Viewer::Type &viewer) {
287 new CallbackDialogData<typename Widget::Other>(
288 {typename Widget::ImportCaller(widget),
289 typename Widget::ExportCaller(widget)},
290 {typename Viewer::ImportCaller(viewer),
291 typename Viewer::ExportCaller(viewer)}
296 template<typename Widget>
298 DialogDataList &data,
299 typename Widget::Type &widget,
300 ImportExportCallback<typename Widget::Other> const &cbViewer
303 new CallbackDialogData<typename Widget::Other>(
304 {typename Widget::ImportCaller(widget),
305 typename Widget::ExportCaller(widget)},
311 // =============================================================================
314 Dialog::Dialog() : m_window( 0 ), m_parent( 0 ){
318 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
323 ASSERT_MESSAGE( !m_window, "dialog window not destroyed" );
326 void Dialog::ShowDlg(){
327 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
332 void Dialog::HideDlg(){
333 ASSERT_MESSAGE( m_window, "dialog was not constructed" );
338 static gint delete_event_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
339 reinterpret_cast<Dialog*>( data )->HideDlg();
340 reinterpret_cast<Dialog*>( data )->EndModal( eIDCANCEL );
344 void Dialog::Create(){
345 ASSERT_MESSAGE( !m_window, "dialog cannot be constructed" );
347 m_window = BuildDialog();
348 m_window.connect( "delete_event", G_CALLBACK( delete_event_callback ), this );
351 void Dialog::Destroy(){
352 ASSERT_MESSAGE( m_window, "dialog cannot be destroyed" );
355 m_window = ui::Window{ui::null};
359 void Dialog::AddBoolToggleData( GtkToggleButton& widget, ImportExportCallback<bool> const &cb ){
360 AddCustomData<BoolToggleImportExport>( m_data, widget, cb );
363 void Dialog::AddIntRadioData( GtkRadioButton& widget, ImportExportCallback<int> const &cb ){
364 AddCustomData<IntRadioImportExport>( m_data, widget, cb );
367 void Dialog::AddTextEntryData( GtkEntry& widget, ImportExportCallback<const char *> const &cb ){
368 AddCustomData<TextEntryImportExport>( m_data, widget, cb );
371 void Dialog::AddIntEntryData( GtkEntry& widget, ImportExportCallback<int> const &cb ){
372 AddCustomData<IntEntryImportExport>( m_data, widget, cb );
375 void Dialog::AddSizeEntryData( GtkEntry& widget, ImportExportCallback<std::size_t> const &cb ){
376 AddCustomData<SizeEntryImportExport>( m_data, widget, cb );
379 void Dialog::AddFloatEntryData( GtkEntry& widget, ImportExportCallback<float> const &cb ){
380 AddCustomData<FloatEntryImportExport>( m_data, widget, cb );
383 void Dialog::AddFloatSpinnerData( GtkSpinButton& widget, ImportExportCallback<float> const &cb ){
384 AddCustomData<FloatSpinnerImportExport>( m_data, widget, cb );
387 void Dialog::AddIntSpinnerData( GtkSpinButton& widget, ImportExportCallback<int> const &cb ){
388 AddCustomData<IntSpinnerImportExport>( m_data, widget, cb );
391 void Dialog::AddIntAdjustmentData( GtkAdjustment& widget, ImportExportCallback<int> const &cb ){
392 AddCustomData<IntAdjustmentImportExport>( m_data, widget, cb );
395 void Dialog::AddIntComboData( GtkComboBox& widget, ImportExportCallback<int> const &cb ){
396 AddCustomData<IntComboImportExport>( m_data, widget, cb );
400 void Dialog::AddDialogData( GtkToggleButton& widget, bool& data ){
401 AddData<BoolToggleImportExport, BoolImportExport>( m_data, widget, data );
403 void Dialog::AddDialogData( GtkRadioButton& widget, int& data ){
404 AddData<IntRadioImportExport, IntImportExport>( m_data, widget, data );
406 void Dialog::AddDialogData( GtkEntry& widget, CopiedString& data ){
407 AddData<TextEntryImportExport, StringImportExport>( m_data, widget, data );
409 void Dialog::AddDialogData( GtkEntry& widget, int& data ){
410 AddData<IntEntryImportExport, IntImportExport>( m_data, widget, data );
412 void Dialog::AddDialogData( GtkEntry& widget, std::size_t& data ){
413 AddData<SizeEntryImportExport, SizeImportExport>( m_data, widget, data );
415 void Dialog::AddDialogData( GtkEntry& widget, float& data ){
416 AddData<FloatEntryImportExport, FloatImportExport>( m_data, widget, data );
418 void Dialog::AddDialogData( GtkSpinButton& widget, float& data ){
419 AddData<FloatSpinnerImportExport, FloatImportExport>( m_data, widget, data );
421 void Dialog::AddDialogData( GtkSpinButton& widget, int& data ){
422 AddData<IntSpinnerImportExport, IntImportExport>( m_data, widget, data );
424 void Dialog::AddDialogData( GtkAdjustment& widget, int& data ){
425 AddData<IntAdjustmentImportExport, IntImportExport>( m_data, widget, data );
427 void Dialog::AddDialogData( GtkComboBox& widget, int& data ){
428 AddData<IntComboImportExport, IntImportExport>( m_data, widget, data );
431 void Dialog::exportData(){
432 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
434 ( *i )->exportData();
438 void Dialog::importData(){
439 for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
441 ( *i )->importData();
445 void Dialog::EndModal( EMessageBoxReturn code ){
450 EMessageBoxReturn Dialog::DoModal(){
455 EMessageBoxReturn ret = modal_dialog_show( m_window, m_modal );
456 ASSERT_TRUE( (bool) m_window );
457 if ( ret == eIDOK ) {
463 PostModal( m_modal.ret );
469 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, ImportExportCallback<bool> const &cb ){
470 auto check = ui::CheckButton( flag );
472 AddBoolToggleData( *GTK_TOGGLE_BUTTON( check ), cb );
474 DialogVBox_packRow( vbox, ui::Widget(DialogRow_new( name, check ) ));
478 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, bool& data ){
479 return addCheckBox(vbox, name, flag, mkImportExportCallback(data));
482 void Dialog::addCombo( ui::VBox vbox, const char* name, StringArrayRange values, ImportExportCallback<int> const &cb ){
483 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
486 auto combo = ui::ComboBoxText(ui::New);
488 for ( StringArrayRange::Iterator i = values.first; i != values.last; ++i )
490 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), *i );
493 AddIntComboData( *GTK_COMBO_BOX( combo ), cb );
496 alignment.add(combo);
499 auto row = DialogRow_new( name, alignment );
500 DialogVBox_packRow( vbox, row );
503 void Dialog::addCombo( ui::VBox vbox, const char* name, int& data, StringArrayRange values ){
504 addCombo(vbox, name, values, mkImportExportCallback(data));
507 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 ){
509 if ( draw_value == FALSE ) {
510 auto hbox2 = ui::HBox( FALSE, 0 );
512 vbox.pack_start( hbox2 , FALSE, FALSE, 0 );
514 ui::Widget label = ui::Label( low );
516 hbox2.pack_start( label, FALSE, FALSE, 0 );
519 ui::Widget label = ui::Label( high );
521 hbox2.pack_end(label, FALSE, FALSE, 0);
527 auto adj = ui::Adjustment( value, lower, upper, step_increment, page_increment, 0 );
528 AddIntAdjustmentData(*GTK_ADJUSTMENT(adj), mkImportExportCallback(data));
531 auto alignment = ui::Alignment( 0.0, 0.5, 1.0, 0.0 );
534 ui::Widget scale = ui::HScale( adj );
535 gtk_scale_set_value_pos( GTK_SCALE( scale ), GTK_POS_LEFT );
537 alignment.add(scale);
539 gtk_scale_set_draw_value( GTK_SCALE( scale ), draw_value );
540 gtk_scale_set_digits( GTK_SCALE( scale ), 0 );
542 auto row = DialogRow_new( name, alignment );
543 DialogVBox_packRow( vbox, row );
546 void Dialog::addRadio( ui::VBox vbox, const char* name, StringArrayRange names, ImportExportCallback<int> const &cb ){
547 auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
550 RadioHBox radioBox = RadioHBox_new( names );
551 alignment.add(radioBox.m_hbox);
552 AddIntRadioData( *GTK_RADIO_BUTTON( radioBox.m_radio ), cb );
555 auto row = DialogRow_new( name, alignment );
556 DialogVBox_packRow( vbox, row );
559 void Dialog::addRadio( ui::VBox vbox, const char* name, int& data, StringArrayRange names ){
560 addRadio(vbox, name, names, mkImportExportCallback(data));
563 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, StringArrayRange icons, ImportExportCallback<int> const &cb ){
564 auto table = ui::Table(2, icons.last - icons.first, FALSE);
567 gtk_table_set_row_spacings(table, 5);
568 gtk_table_set_col_spacings(table, 5);
571 ui::RadioButton radio{ui::null};
572 for ( StringArrayRange::Iterator icon = icons.first; icon != icons.last; ++icon )
574 guint pos = static_cast<guint>( icon - icons.first );
575 auto image = new_local_image( *icon );
577 table.attach(image, {pos, pos + 1, 0, 1}, {0, 0});
579 radio = ui::RadioButton(GTK_RADIO_BUTTON(gtk_radio_button_new( group )));
581 table.attach(radio, {pos, pos + 1, 1, 2}, {0, 0});
583 group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( radio ) );
586 AddIntRadioData( *GTK_RADIO_BUTTON( radio ), cb );
588 DialogVBox_packRow( vbox, DialogRow_new( name, table ) );
591 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, int& data, StringArrayRange icons ){
592 addRadioIcons(vbox, name, icons, mkImportExportCallback(data));
595 ui::Widget Dialog::addIntEntry( ui::VBox vbox, const char* name, ImportExportCallback<int> const &cb ){
596 DialogEntryRow row( DialogEntryRow_new( name ) );
597 AddIntEntryData( *GTK_ENTRY(row.m_entry), cb );
598 DialogVBox_packRow( vbox, row.m_row );
602 ui::Widget Dialog::addSizeEntry( ui::VBox vbox, const char* name, ImportExportCallback<std::size_t> const &cb ){
603 DialogEntryRow row( DialogEntryRow_new( name ) );
604 AddSizeEntryData( *GTK_ENTRY(row.m_entry), cb );
605 DialogVBox_packRow( vbox, row.m_row );
609 ui::Widget Dialog::addFloatEntry( ui::VBox vbox, const char* name, ImportExportCallback<float> const &cb ){
610 DialogEntryRow row( DialogEntryRow_new( name ) );
611 AddFloatEntryData( *GTK_ENTRY(row.m_entry), cb );
612 DialogVBox_packRow( vbox, row.m_row );
616 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, bool browse_directory, ImportExportCallback<const char *> const &cb ){
617 PathEntry pathEntry = PathEntry_new();
618 pathEntry.m_button.connect( "clicked", G_CALLBACK( browse_directory ? button_clicked_entry_browse_directory : button_clicked_entry_browse_file ), pathEntry.m_entry );
620 AddTextEntryData( *GTK_ENTRY(pathEntry.m_entry), cb );
622 auto row = DialogRow_new( name, ui::Widget(pathEntry.m_frame ) );
623 DialogVBox_packRow( vbox, row );
628 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, CopiedString& data, bool browse_directory ){
629 return addPathEntry(vbox, name, browse_directory, mkImportExportCallback<CopiedString, const char *>(data));
632 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, ImportExportCallback<int> const &cb ){
633 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 1 ) );
634 AddIntSpinnerData( *GTK_SPIN_BUTTON(row.m_spin), cb );
635 DialogVBox_packRow( vbox, row.m_row );
639 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, int& data, double value, double lower, double upper ){
640 return addSpinner(vbox, name, value, lower, upper, mkImportExportCallback(data));
643 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, ImportExportCallback<float> const &cb ){
644 DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 10 ) );
645 AddFloatSpinnerData( *GTK_SPIN_BUTTON(row.m_spin), cb );
646 DialogVBox_packRow( vbox, row.m_row );