]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/dialog.cpp
netradiant: strip 16-bit png to 8-bit, fix #153
[xonotic/netradiant.git] / radiant / dialog.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
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 //
23 // Base dialog class, provides a way to run modal dialogs and
24 // set/get the widget values in member variables.
25 //
26 // Leonardo Zide (leo@lokigames.com)
27 //
28
29 #include "dialog.h"
30
31 #include <gtk/gtk.h>
32
33 #include "debugging/debugging.h"
34
35
36 #include "mainframe.h"
37
38 #include <stdlib.h>
39
40 #include "stream/stringstream.h"
41 #include "convert.h"
42 #include "gtkutil/dialog.h"
43 #include "gtkutil/button.h"
44 #include "gtkutil/entry.h"
45 #include "gtkutil/image.h"
46
47 #include "gtkmisc.h"
48
49
50 ui::Entry DialogEntry_new(){
51         auto entry = ui::Entry(ui::New);
52         entry.show();
53         entry.dimensions(64, -1);
54         return entry;
55 }
56
57 class DialogEntryRow
58 {
59 public:
60 DialogEntryRow( ui::Widget row, ui::Entry entry ) : m_row( row ), m_entry( entry ){
61 }
62 ui::Widget m_row;
63 ui::Entry m_entry;
64 };
65
66 DialogEntryRow DialogEntryRow_new( const char* name ){
67         auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
68         alignment.show();
69
70         auto entry = DialogEntry_new();
71         alignment.add(entry);
72
73         return DialogEntryRow( ui::Widget(DialogRow_new( name, alignment  )), entry );
74 }
75
76
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 )
81         {
82                 ++digits;
83         }
84         auto spin = ui::SpinButton( ui::Adjustment( value, lower, upper, step, 10, 0 ), step, digits );
85         spin.show();
86         spin.dimensions(64, -1);
87         return spin;
88 }
89
90 class DialogSpinnerRow
91 {
92 public:
93 DialogSpinnerRow( ui::Widget row, ui::SpinButton spin ) : m_row( row ), m_spin( spin ){
94 }
95 ui::Widget m_row;
96 ui::SpinButton m_spin;
97 };
98
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 );
101         alignment.show();
102
103         auto spin = DialogSpinner_new( value, lower, upper, fraction );
104         alignment.add(spin);
105
106         return DialogSpinnerRow( ui::Widget(DialogRow_new( name, alignment  )), spin );
107 }
108
109
110 struct BoolToggle {
111         static void Export(const ui::ToggleButton &self, const Callback<void(bool)> &returnz) {
112                 returnz(self.active());
113         }
114
115         static void Import(ui::ToggleButton &self, bool value) {
116                 self.active(value);
117         }
118 };
119
120 using BoolToggleImportExport = PropertyAdaptor<ui::ToggleButton, bool, BoolToggle>;
121
122 struct IntEntry {
123         static void Export(const ui::Entry &self, const Callback<void(int)> &returnz) {
124                 returnz(atoi(gtk_entry_get_text(self)));
125         }
126
127         static void Import(ui::Entry &self, int value) {
128                 entry_set_int(self, value);
129         }
130 };
131
132 using IntEntryImportExport = PropertyAdaptor<ui::Entry, int, IntEntry>;
133
134 struct IntRadio {
135         static void Export(const ui::RadioButton &self, const Callback<void(int)> &returnz) {
136                 returnz(radio_button_get_active(self));
137         }
138
139         static void Import(ui::RadioButton &self, int value) {
140                 radio_button_set_active(self, value);
141         }
142 };
143
144 using IntRadioImportExport = PropertyAdaptor<ui::RadioButton, int, IntRadio>;
145
146 struct IntCombo {
147         static void Export(const ui::ComboBox &self, const Callback<void(int)> &returnz) {
148                 returnz(gtk_combo_box_get_active(self));
149         }
150
151         static void Import(ui::ComboBox &self, int value) {
152                 gtk_combo_box_set_active(self, value);
153         }
154 };
155
156 using IntComboImportExport = PropertyAdaptor<ui::ComboBox, int, IntCombo>;
157
158 struct IntAdjustment {
159         static void Export(const ui::Adjustment &self, const Callback<void(int)> &returnz) {
160                 returnz(int(gtk_adjustment_get_value(self)));
161         }
162
163         static void Import(ui::Adjustment &self, int value) {
164                 gtk_adjustment_set_value(self, value);
165         }
166 };
167
168 using IntAdjustmentImportExport = PropertyAdaptor<ui::Adjustment, int, IntAdjustment>;
169
170 struct IntSpinner {
171         static void Export(const ui::SpinButton &self, const Callback<void(int)> &returnz) {
172                 returnz(gtk_spin_button_get_value_as_int(self));
173         }
174
175         static void Import(ui::SpinButton &self, int value) {
176                 gtk_spin_button_set_value(self, value);
177         }
178 };
179
180 using IntSpinnerImportExport = PropertyAdaptor<ui::SpinButton, int, IntSpinner>;
181
182 struct TextEntry {
183         static void Export(const ui::Entry &self, const Callback<void(const char *)> &returnz) {
184                 returnz(gtk_entry_get_text(self));
185         }
186
187         static void Import(ui::Entry &self, const char *value) {
188                 self.text(value);
189         }
190 };
191
192 using TextEntryImportExport = PropertyAdaptor<ui::Entry, const char *, TextEntry>;
193
194 struct SizeEntry {
195         static void Export(const ui::Entry &self, const Callback<void(std::size_t)> &returnz) {
196                 int value = atoi(gtk_entry_get_text(self));
197                 if (value < 0) {
198                         value = 0;
199                 }
200                 returnz(value);
201         }
202
203         static void Import(ui::Entry &self, std::size_t value) {
204                 entry_set_int(self, int(value));
205         }
206 };
207
208 using SizeEntryImportExport = PropertyAdaptor<ui::Entry, std::size_t, SizeEntry>;
209
210 struct FloatEntry {
211         static void Export(const ui::Entry &self, const Callback<void(float)> &returnz) {
212                 returnz(float(atof(gtk_entry_get_text(self))));
213         }
214
215         static void Import(ui::Entry &self, float value) {
216                 entry_set_float(self, value);
217         }
218 };
219
220 using FloatEntryImportExport = PropertyAdaptor<ui::Entry, float, FloatEntry>;
221
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)));
225         }
226
227         static void Import(ui::SpinButton &self, float value) {
228                 gtk_spin_button_set_value(self, value);
229         }
230 };
231
232 using FloatSpinnerImportExport = PropertyAdaptor<ui::SpinButton, float, FloatSpinner>;
233
234
235
236 template<typename T>
237 class CallbackDialogData : public DLG_DATA {
238         Property<T> m_pWidget;
239         Property<T> m_pData;
240
241 public:
242         CallbackDialogData(const Property<T> &pWidget, const Property<T> &pData)
243                         : m_pWidget(pWidget), m_pData(pData) {
244         }
245
246         void release() {
247                 delete this;
248         }
249
250         void importData() const {
251                 m_pData.get(m_pWidget.set);
252         }
253
254         void exportData() const {
255                 m_pWidget.get(m_pData.set);
256         }
257 };
258
259 template<class Widget, class Self, class T, class native>
260 struct AddDataCustom_Wrapper {
261     static void Export(const native &self, const Callback<void(T)> &returnz) {
262         native *p = &const_cast<native &>(self);
263         auto widget = Self::from(p);
264         Widget::Get::thunk_(widget, returnz);
265     }
266
267     static void Import(native &self, T value) {
268         native *p = &self;
269         auto widget = Self::from(p);
270         Widget::Set::thunk_(widget, value);
271     }
272 };
273
274 template<class Widget>
275 void AddDataCustom(DialogDataList &self, typename Widget::Type widget, Property<typename Widget::Other> const &property) {
276     using Self = typename Widget::Type;
277     using T = typename Widget::Other;
278     using native = typename std::remove_pointer<typename Self::native>::type;
279     using Wrapper = AddDataCustom_Wrapper<Widget, Self, T, native>;
280     
281     self.push_back(new CallbackDialogData<typename Widget::Other>(
282             make_property<PropertyAdaptor<native, T, Wrapper>>(*static_cast<native *>(widget)),
283             property
284     ));
285 }
286
287 template<class Widget, class D>
288 void AddData(DialogDataList &self, typename Widget::Type widget, D &data) {
289     AddDataCustom<Widget>(self, widget, make_property<PropertyAdaptor<D, typename Widget::Other>>(data));
290 }
291
292 // =============================================================================
293 // Dialog class
294
295 Dialog::Dialog() : m_window( ui::null ), m_parent( ui::null ){
296 }
297
298 Dialog::~Dialog(){
299         for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
300         {
301                 ( *i )->release();
302         }
303
304         ASSERT_MESSAGE( !m_window, "dialog window not destroyed" );
305 }
306
307 void Dialog::ShowDlg(){
308         ASSERT_MESSAGE( m_window, "dialog was not constructed" );
309         importData();
310         m_window.show();
311 }
312
313 void Dialog::HideDlg(){
314         ASSERT_MESSAGE( m_window, "dialog was not constructed" );
315         exportData();
316         m_window.hide();
317 }
318
319 static gint delete_event_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
320         reinterpret_cast<Dialog*>( data )->HideDlg();
321         reinterpret_cast<Dialog*>( data )->EndModal( eIDCANCEL );
322         return TRUE;
323 }
324
325 void Dialog::Create(){
326         ASSERT_MESSAGE( !m_window, "dialog cannot be constructed" );
327
328         m_window = BuildDialog();
329         m_window.connect( "delete_event", G_CALLBACK( delete_event_callback ), this );
330 }
331
332 void Dialog::Destroy(){
333         ASSERT_MESSAGE( m_window, "dialog cannot be destroyed" );
334
335         m_window.destroy();
336         m_window = ui::Window{ui::null};
337 }
338
339
340 void Dialog::AddBoolToggleData( ui::ToggleButton widget, Property<bool> const &cb ){
341     AddDataCustom<BoolToggleImportExport>( m_data, widget, cb );
342 }
343
344 void Dialog::AddIntRadioData( ui::RadioButton widget, Property<int> const &cb ){
345     AddDataCustom<IntRadioImportExport>( m_data, widget, cb );
346 }
347
348 void Dialog::AddTextEntryData( ui::Entry widget, Property<const char *> const &cb ){
349     AddDataCustom<TextEntryImportExport>( m_data, widget, cb );
350 }
351
352 void Dialog::AddIntEntryData( ui::Entry widget, Property<int> const &cb ){
353     AddDataCustom<IntEntryImportExport>( m_data, widget, cb );
354 }
355
356 void Dialog::AddSizeEntryData( ui::Entry widget, Property<std::size_t> const &cb ){
357     AddDataCustom<SizeEntryImportExport>( m_data, widget, cb );
358 }
359
360 void Dialog::AddFloatEntryData( ui::Entry widget, Property<float> const &cb ){
361     AddDataCustom<FloatEntryImportExport>( m_data, widget, cb );
362 }
363
364 void Dialog::AddFloatSpinnerData( ui::SpinButton widget, Property<float> const &cb ){
365     AddDataCustom<FloatSpinnerImportExport>( m_data, widget, cb );
366 }
367
368 void Dialog::AddIntSpinnerData( ui::SpinButton widget, Property<int> const &cb ){
369     AddDataCustom<IntSpinnerImportExport>( m_data, widget, cb );
370 }
371
372 void Dialog::AddIntAdjustmentData( ui::Adjustment widget, Property<int> const &cb ){
373     AddDataCustom<IntAdjustmentImportExport>( m_data, widget, cb );
374 }
375
376 void Dialog::AddIntComboData( ui::ComboBox widget, Property<int> const &cb ){
377     AddDataCustom<IntComboImportExport>( m_data, widget, cb );
378 }
379
380
381 void Dialog::AddDialogData( ui::ToggleButton widget, bool& data ){
382         AddData<BoolToggleImportExport>( m_data, widget, data );
383 }
384 void Dialog::AddDialogData( ui::RadioButton widget, int& data ){
385         AddData<IntRadioImportExport>( m_data, widget, data );
386 }
387 void Dialog::AddDialogData( ui::Entry widget, CopiedString& data ){
388         AddData<TextEntryImportExport>( m_data, widget, data );
389 }
390 void Dialog::AddDialogData( ui::Entry widget, int& data ){
391         AddData<IntEntryImportExport>( m_data, widget, data );
392 }
393 void Dialog::AddDialogData( ui::Entry widget, std::size_t& data ){
394         AddData<SizeEntryImportExport>( m_data, widget, data );
395 }
396 void Dialog::AddDialogData( ui::Entry widget, float& data ){
397         AddData<FloatEntryImportExport>( m_data, widget, data );
398 }
399 void Dialog::AddDialogData( ui::SpinButton widget, float& data ){
400         AddData<FloatSpinnerImportExport>( m_data, widget, data );
401 }
402 void Dialog::AddDialogData( ui::SpinButton widget, int& data ){
403         AddData<IntSpinnerImportExport>( m_data, widget, data );
404 }
405 void Dialog::AddDialogData( ui::Adjustment widget, int& data ){
406         AddData<IntAdjustmentImportExport>( m_data, widget, data );
407 }
408 void Dialog::AddDialogData( ui::ComboBox widget, int& data ){
409         AddData<IntComboImportExport>( m_data, widget, data );
410 }
411
412 void Dialog::exportData(){
413         for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
414         {
415                 ( *i )->exportData();
416         }
417 }
418
419 void Dialog::importData(){
420         for ( DialogDataList::iterator i = m_data.begin(); i != m_data.end(); ++i )
421         {
422                 ( *i )->importData();
423         }
424 }
425
426 void Dialog::EndModal( EMessageBoxReturn code ){
427         m_modal.loop = 0;
428         m_modal.ret = code;
429 }
430
431 EMessageBoxReturn Dialog::DoModal(){
432         importData();
433
434         PreModal();
435
436         EMessageBoxReturn ret = modal_dialog_show( m_window, m_modal );
437         ASSERT_TRUE( (bool) m_window );
438         if ( ret == eIDOK ) {
439                 exportData();
440         }
441
442         m_window.hide();
443
444         PostModal( m_modal.ret );
445
446         return m_modal.ret;
447 }
448
449 ui::VBox Dialog::addSpacer( ui::VBox vbox, int dimension ){
450         auto spacer = ui::VBox( FALSE, 0 );
451         gtk_widget_set_size_request( GTK_WIDGET( spacer ), dimension, dimension );
452         spacer.show();
453
454         DialogVBox_packRow( vbox, spacer );
455         return spacer;
456 }
457
458 ui::Label Dialog::addLabel( ui::VBox vbox, const char* name, const char* text ){
459         auto label = ui::Label( text );
460         gtk_misc_set_alignment( GTK_MISC( label ), 0.0, 0.5 );
461         label.show();
462
463         DialogVBox_packRow( vbox, ui::Widget(DialogRow_new( name, label ) ));
464         return label;
465 }
466
467 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, Property<bool> const &cb ){
468         auto check = ui::CheckButton( flag );
469         check.show();
470         AddBoolToggleData( check, cb );
471
472         DialogVBox_packRow( vbox, ui::Widget(DialogRow_new( name, check  ) ));
473         return check;
474 }
475
476 ui::CheckButton Dialog::addCheckBox( ui::VBox vbox, const char* name, const char* flag, bool& data ){
477         return addCheckBox(vbox, name, flag, make_property(data));
478 }
479
480 void Dialog::addCombo( ui::VBox vbox, const char* name, StringArrayRange values, Property<int> const &cb ){
481         auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
482         alignment.show();
483         {
484                 auto combo = ui::ComboBoxText(ui::New);
485
486                 for ( StringArrayRange::Iterator i = values.first; i != values.last; ++i )
487                 {
488                         gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), *i );
489                 }
490
491                 AddIntComboData( combo, cb );
492
493                 combo.show();
494                 alignment.add(combo);
495         }
496
497         auto row = DialogRow_new( name, alignment );
498         DialogVBox_packRow( vbox, row );
499 }
500
501 void Dialog::addCombo( ui::VBox vbox, const char* name, int& data, StringArrayRange values ){
502         addCombo(vbox, name, values, make_property(data));
503 }
504
505 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 ){
506 #if 0
507         if ( draw_value == FALSE ) {
508                 auto hbox2 = ui::HBox( FALSE, 0 );
509                 hbox2.show();
510                 vbox.pack_start( hbox2 , FALSE, FALSE, 0 );
511                 {
512                         ui::Widget label = ui::Label( low );
513                         label.show();
514                         hbox2.pack_start( label, FALSE, FALSE, 0 );
515                 }
516                 {
517                         ui::Widget label = ui::Label( high );
518                         label.show();
519                         hbox2.pack_end(label, FALSE, FALSE, 0);
520                 }
521         }
522 #endif
523
524         // adjustment
525         auto adj = ui::Adjustment( value, lower, upper, step_increment, page_increment, 0 );
526         AddIntAdjustmentData(adj, make_property(data));
527
528         // scale
529         auto alignment = ui::Alignment( 0.0, 0.5, 1.0, 0.0 );
530         alignment.show();
531
532         ui::Widget scale = ui::HScale( adj );
533         gtk_scale_set_value_pos( GTK_SCALE( scale ), GTK_POS_LEFT );
534         scale.show();
535         alignment.add(scale);
536
537         gtk_scale_set_draw_value( GTK_SCALE( scale ), draw_value );
538         gtk_scale_set_digits( GTK_SCALE( scale ), 0 );
539
540         auto row = DialogRow_new( name, alignment );
541         DialogVBox_packRow( vbox, row );
542 }
543
544 void Dialog::addRadio( ui::VBox vbox, const char* name, StringArrayRange names, Property<int> const &cb ){
545         auto alignment = ui::Alignment( 0.0, 0.5, 0.0, 0.0 );
546         alignment.show();;
547         {
548                 RadioHBox radioBox = RadioHBox_new( names );
549                 alignment.add(radioBox.m_hbox);
550                 AddIntRadioData( radioBox.m_radio, cb );
551         }
552
553         auto row = DialogRow_new( name, alignment );
554         DialogVBox_packRow( vbox, row );
555 }
556
557 void Dialog::addRadio( ui::VBox vbox, const char* name, int& data, StringArrayRange names ){
558         addRadio(vbox, name, names, make_property(data));
559 }
560
561 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, StringArrayRange icons, Property<int> const &cb ){
562     auto table = ui::Table(2, icons.last - icons.first, FALSE);
563     table.show();
564
565     gtk_table_set_row_spacings(table, 5);
566     gtk_table_set_col_spacings(table, 5);
567
568         GSList* group = 0;
569         ui::RadioButton radio{ui::null};
570         for ( StringArrayRange::Iterator icon = icons.first; icon != icons.last; ++icon )
571         {
572                 guint pos = static_cast<guint>( icon - icons.first );
573                 auto image = new_local_image( *icon );
574                 image.show();
575         table.attach(image, {pos, pos + 1, 0, 1}, {0, 0});
576
577                 radio = ui::RadioButton::from(gtk_radio_button_new( group ));
578                 radio.show();
579         table.attach(radio, {pos, pos + 1, 1, 2}, {0, 0});
580
581                 group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( radio ) );
582         }
583
584         AddIntRadioData( radio, cb );
585
586         DialogVBox_packRow( vbox, DialogRow_new( name, table ) );
587 }
588
589 void Dialog::addRadioIcons( ui::VBox vbox, const char* name, int& data, StringArrayRange icons ){
590         addRadioIcons(vbox, name, icons, make_property(data));
591 }
592
593 ui::Widget Dialog::addIntEntry( ui::VBox vbox, const char* name, Property<int> const &cb ){
594         DialogEntryRow row( DialogEntryRow_new( name ) );
595         AddIntEntryData( row.m_entry, cb );
596         DialogVBox_packRow( vbox, row.m_row );
597         return row.m_row;
598 }
599
600 ui::Widget Dialog::addSizeEntry( ui::VBox vbox, const char* name, Property<std::size_t> const &cb ){
601         DialogEntryRow row( DialogEntryRow_new( name ) );
602         AddSizeEntryData( row.m_entry, cb );
603         DialogVBox_packRow( vbox, row.m_row );
604         return row.m_row;
605 }
606
607 ui::Widget Dialog::addFloatEntry( ui::VBox vbox, const char* name, Property<float> const &cb ){
608         DialogEntryRow row( DialogEntryRow_new( name ) );
609         AddFloatEntryData( row.m_entry, cb );
610         DialogVBox_packRow( vbox, row.m_row );
611         return row.m_row;
612 }
613
614 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, bool browse_directory, Property<const char *> const &cb ){
615         PathEntry pathEntry = PathEntry_new();
616         pathEntry.m_button.connect( "clicked", G_CALLBACK( browse_directory ? button_clicked_entry_browse_directory : button_clicked_entry_browse_file ), pathEntry.m_entry );
617
618         AddTextEntryData( pathEntry.m_entry, cb );
619
620         auto row = DialogRow_new( name, ui::Widget(pathEntry.m_frame ) );
621         DialogVBox_packRow( vbox, row );
622
623         return row;
624 }
625
626 ui::Widget Dialog::addPathEntry( ui::VBox vbox, const char* name, CopiedString& data, bool browse_directory ){
627     return addPathEntry(vbox, name, browse_directory, make_property<CopiedString, const char *>(data));
628 }
629
630 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<int> const &cb ){
631         DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 1 ) );
632         AddIntSpinnerData( row.m_spin, cb );
633         DialogVBox_packRow( vbox, row.m_row );
634         return row.m_spin;
635 }
636
637 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, int& data, double value, double lower, double upper ){
638         return addSpinner(vbox, name, value, lower, upper, make_property(data));
639 }
640
641 ui::SpinButton Dialog::addSpinner( ui::VBox vbox, const char* name, double value, double lower, double upper, Property<float> const &cb ){
642         DialogSpinnerRow row( DialogSpinnerRow_new( name, value, lower, upper, 10 ) );
643         AddFloatSpinnerData( row.m_spin, cb );
644         DialogVBox_packRow( vbox, row.m_row );
645         return row.m_spin;
646 }