943b077b7721bdafc0a4566a8b14c1de7450954f
[ardour.git] / gtk2_ardour / luadialog.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <algorithm>
20
21 #include <gtkmm.h>
22
23 #include "ardour/dB.h"
24 #include "ardour/rc_configuration.h"
25 #include "gtk2_ardour/stripable_colorpicker.h"
26
27 #include "gtkmm2ext/utils.h"
28
29 #include "widgets/ardour_dropdown.h"
30 #include "widgets/slider_controller.h"
31
32 #include "ardour_dialog.h"
33 #include "luadialog.h"
34 #include "splash.h"
35 #include "utils.h"
36
37 using namespace LuaDialog;
38
39 /*******************************************************************************
40  * Simple Message Dialog
41  */
42 Message::Message (std::string const& title, std::string const& msg, Message::MessageType mt, Message::ButtonType bt)
43         : _message_dialog (msg, false, to_gtk_mt (mt), to_gtk_bt (bt), true)
44 {
45         _message_dialog.set_title (title);
46 }
47
48 int
49 Message::run ()
50 {
51
52         bool splash_pushed = false;
53         Splash* spl = Splash::instance();
54         if (spl && spl->is_visible()) {
55                 spl->pop_back_for (_message_dialog);
56                 splash_pushed = true;
57         }
58
59         int rv = _message_dialog.run ();
60         _message_dialog.hide ();
61
62         if (splash_pushed) {
63                 spl = Splash::instance();
64                 if (spl) {
65                         spl->pop_front();
66                 }
67         }
68
69         switch (rv) {
70                 case Gtk::RESPONSE_OK:
71                         return 0;
72                 case Gtk::RESPONSE_CANCEL:
73                         return 1;
74                 case Gtk::RESPONSE_CLOSE:
75                         return 2;
76                 case Gtk::RESPONSE_YES:
77                         return 3;
78                 case Gtk::RESPONSE_NO:
79                         return 4;
80                 default:
81                         break;
82         }
83         return -1;
84 }
85
86 Gtk::ButtonsType
87 Message::to_gtk_bt (ButtonType bt)
88 {
89         switch (bt) {
90                 case OK:
91                         return Gtk::BUTTONS_OK;
92                 case Close:
93                         return Gtk::BUTTONS_CLOSE;
94                 case Cancel:
95                         return Gtk::BUTTONS_CANCEL;
96                 case Yes_No:
97                         return Gtk::BUTTONS_YES_NO;
98                 case OK_Cancel:
99                         return Gtk::BUTTONS_OK_CANCEL;
100         }
101         assert (0);
102         return Gtk::BUTTONS_OK;
103 }
104
105 Gtk::MessageType
106 Message::to_gtk_mt (MessageType mt)
107 {
108         switch (mt) {
109                 case Info:
110                         return Gtk::MESSAGE_INFO;
111                 case Warning:
112                         return Gtk::MESSAGE_WARNING;
113                 case Question:
114                         return Gtk::MESSAGE_QUESTION;
115                 case Error:
116                         return Gtk::MESSAGE_ERROR;
117         }
118         assert (0);
119         return Gtk::MESSAGE_INFO;
120 }
121
122
123 /* *****************************************************************************
124  * Lua Dialog Widgets
125  */
126
127 class LuaDialogLabel : public LuaDialogWidget
128 {
129 public:
130         LuaDialogLabel (std::string const& title, Gtk::AlignmentEnum xalign)
131                 : LuaDialogWidget ("", "", 0, 2)
132                 , _lbl (title, xalign, Gtk::ALIGN_CENTER, false)
133         { }
134
135         Gtk::Widget* widget ()
136         {
137                 return &_lbl;
138         }
139
140         void assign (luabridge::LuaRef* rv) const { }
141 protected:
142         Gtk::Label _lbl;
143 };
144
145
146 class LuaDialogHeading : public LuaDialogLabel
147 {
148 public:
149         LuaDialogHeading (std::string const& title, Gtk::AlignmentEnum xalign)
150         : LuaDialogLabel ("<b>" + title + "</b>", xalign)
151         {
152                 _lbl.set_use_markup ();
153         }
154 };
155
156 class LuaHSeparator : public LuaDialogWidget
157 {
158 public:
159         LuaHSeparator ()
160                 : LuaDialogWidget ("", "", 0, 2)
161         {}
162
163         Gtk::Widget* widget ()
164         {
165                 return &_sep;
166         }
167
168         void assign (luabridge::LuaRef* rv) const { }
169 protected:
170         Gtk::HSeparator _sep;
171 };
172
173 class LuaColorPicker : public LuaDialogWidget
174 {
175 public:
176         LuaColorPicker (std::string const& key)
177                 : LuaDialogWidget (key, "", 0, 1)
178         {}
179
180         Gtk::Widget* widget ()
181         {
182                 return &_cs;
183         }
184         void assign (luabridge::LuaRef* rv) const {
185                 uint32_t rgba = ARDOUR_UI_UTILS::gdk_color_to_rgba(_cs.get_color());
186                 (*rv)[_key] = rgba;
187         }
188 protected:
189         Gtk::ColorButton _cs;
190 };
191 /*
192 local a = {
193         {type = "color", key = "col", title = ""}
194 }
195
196 local rv = LuaDialog.Dialog("", a):run()
197
198 print(rv['col'])
199 */
200
201 class LuaDialogCheckbox : public LuaDialogWidget
202 {
203 public:
204         LuaDialogCheckbox (std::string const& key, std::string const& title, bool on)
205                 : LuaDialogWidget (key, "", 1, 1)
206         {
207                 if (!title.empty ()) {
208                         _cb.add_label (title, false, 0);
209                 }
210                 _cb.set_active (on);
211         }
212
213         Gtk::Widget* widget ()
214         {
215                 return &_cb;
216         }
217
218         void assign (luabridge::LuaRef* rv) const
219         {
220                 (*rv)[_key] = _cb.get_active ();
221         }
222
223 protected:
224         Gtk::CheckButton _cb;
225 };
226
227 class LuaDialogEntry : public LuaDialogWidget
228 {
229 public:
230         LuaDialogEntry (std::string const& key, std::string const& title, std::string const& dflt)
231                 : LuaDialogWidget (key, title)
232         {
233                 _entry.set_text (dflt);
234         }
235
236         Gtk::Widget* widget ()
237         {
238                 return &_entry;
239         }
240
241         void assign (luabridge::LuaRef* rv) const
242         {
243                 (*rv)[_key] = std::string (_entry.get_text ());
244         }
245
246 protected:
247         Gtk::Entry _entry;
248 };
249
250 class LuaDialogFader : public LuaDialogWidget
251 {
252 public:
253         LuaDialogFader (std::string const& key, std::string const& title, double dflt)
254                 : LuaDialogWidget (key, title)
255                 , _db_adjustment (ARDOUR::gain_to_slider_position_with_max (1.0, ARDOUR::Config->get_max_gain ()), 0, 1, 0.01, 0.1)
256         {
257                 _db_slider = Gtk::manage (new ArdourWidgets::HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable> (), 220, 18));
258
259                 _fader_centering_box.pack_start (*_db_slider, true, false);
260
261                 _box.set_spacing (4);
262                 _box.set_homogeneous (false);
263                 _box.pack_start (_fader_centering_box, false, false);
264                 _box.pack_start (_db_display, false, false);
265                 _box.pack_start (*Gtk::manage (new Gtk::Label ("dB")), false, false);
266
267                 Gtkmm2ext::set_size_request_to_display_given_text (_db_display, "-99.00", 12, 0);
268
269                 _db_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &LuaDialogFader::db_changed));
270                 _db_display.signal_activate ().connect (sigc::mem_fun (*this, &LuaDialogFader::on_activate));
271                 _db_display.signal_key_press_event ().connect (sigc::mem_fun (*this, &LuaDialogFader::on_key_press), false);
272
273                 double coeff_val = dB_to_coefficient (dflt);
274                 _db_adjustment.set_value (ARDOUR::gain_to_slider_position_with_max (coeff_val, ARDOUR::Config->get_max_gain ()));
275                 db_changed ();
276         }
277
278         Gtk::Widget* widget ()
279         {
280                 return &_box;
281         }
282
283         void assign (luabridge::LuaRef* rv) const
284         {
285                 double const val = ARDOUR::slider_position_to_gain_with_max (_db_adjustment.get_value (), ARDOUR::Config->get_max_gain ());
286                 (*rv)[_key] = accurate_coefficient_to_dB (val);
287         }
288
289 protected:
290         void db_changed ()
291         {
292                 double const val = ARDOUR::slider_position_to_gain_with_max (_db_adjustment.get_value (), ARDOUR::Config->get_max_gain ());
293                 char buf[16];
294                 if (val == 0.0) {
295                         snprintf (buf, sizeof (buf), "-inf");
296                 } else {
297                         snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
298                 }
299                 _db_display.set_text (buf);
300         }
301
302         void on_activate ()
303         {
304                 float db_val = atof (_db_display.get_text ().c_str ());
305                 double coeff_val = dB_to_coefficient (db_val);
306                 _db_adjustment.set_value (ARDOUR::gain_to_slider_position_with_max (coeff_val, ARDOUR::Config->get_max_gain ()));
307         }
308
309         bool on_key_press (GdkEventKey* ev)
310         {
311                 if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
312                         return false;
313                 }
314                 return true;
315         }
316
317         Gtk::Adjustment _db_adjustment;
318         ArdourWidgets::HSliderController* _db_slider;
319         Gtk::Entry _db_display;
320         Gtk::HBox _box;
321         Gtk::VBox _fader_centering_box;
322 };
323
324 class LuaDialogSlider : public LuaDialogWidget
325 {
326 public:
327         LuaDialogSlider (std::string const& key, std::string const& title, double lower, double upper, double dflt, int digits, luabridge::LuaRef scalepoints)
328                 : LuaDialogWidget (key, title)
329                 , _adj (dflt, lower, upper, 1, (upper - lower) / 20, 0)
330                 , _hscale (_adj)
331         {
332                 _hscale.set_digits (digits);
333                 _hscale.set_draw_value (true);
334                 _hscale.set_value_pos (Gtk::POS_TOP);
335
336                 if (!scalepoints.isTable ()) {
337                         return;
338                 }
339
340                 for (luabridge::Iterator i (scalepoints); !i.isNil (); ++i) {
341                         if (!i.key ().isNumber ())  { continue; }
342                         if (!i.value ().isString ())  { continue; }
343                         _hscale.add_mark (i.key ().cast<double> (), Gtk::POS_BOTTOM, i.value ().cast<std::string> ());
344                 }
345         }
346
347         Gtk::Widget* widget ()
348         {
349                 return &_hscale;
350         }
351
352         void assign (luabridge::LuaRef* rv) const
353         {
354                 (*rv)[_key] = _adj.get_value ();
355         }
356
357 protected:
358         Gtk::Adjustment _adj;
359         Gtk::HScale _hscale;
360 };
361
362 class LuaDialogSpinBox : public LuaDialogWidget
363 {
364 public:
365         LuaDialogSpinBox (std::string const& key, std::string const& title, double lower, double upper, double dflt, double step, int digits)
366                 : LuaDialogWidget (key, title)
367                 , _adj (dflt, lower, upper, step, step, 0)
368                 , _spin (_adj)
369         {
370                 _spin.set_digits (digits);
371         }
372
373         Gtk::Widget* widget ()
374         {
375                 return &_spin;
376         }
377
378         void assign (luabridge::LuaRef* rv) const
379         {
380                 (*rv)[_key] = _adj.get_value ();
381         }
382
383 protected:
384         Gtk::Adjustment _adj;
385         Gtk::SpinButton _spin;
386 };
387
388 class LuaDialogRadio : public LuaDialogWidget
389 {
390 public:
391         LuaDialogRadio (std::string const& key, std::string const& title, luabridge::LuaRef values, std::string const& dflt)
392                 : LuaDialogWidget (key, title)
393                 , _rv (0)
394         {
395                 for (luabridge::Iterator i (values); !i.isNil (); ++i) {
396                         if (!i.key ().isString ())  { continue; }
397                         std::string key = i.key ().cast<std::string> ();
398                         Gtk::RadioButton* rb = Gtk::manage (new Gtk::RadioButton (_group, key));
399                         _hbox.pack_start (*rb);
400                         luabridge::LuaRef* ref = new luabridge::LuaRef (i.value ());
401                         _refs.push_back (ref);
402                         if (!_rv) { _rv = ref; }
403                         rb->signal_toggled ().connect (sigc::bind (
404                                                 sigc::mem_fun (*this, &LuaDialogRadio::rb_toggled), rb, ref
405                                                 ) , false);
406
407                         if (key == dflt) {
408                                 rb->set_active ();
409                         }
410                 }
411         }
412
413         ~LuaDialogRadio ()
414         {
415                 for (std::vector<luabridge::LuaRef*>::const_iterator i = _refs.begin (); i != _refs.end (); ++i) {
416                         delete *i;
417                 }
418                 _refs.clear ();
419         }
420
421         Gtk::Widget* widget ()
422         {
423                 return &_hbox;
424         }
425
426         void assign (luabridge::LuaRef* rv) const
427         {
428                 if (_rv) {
429                         (*rv)[_key] = *_rv;
430                 } else {
431                         (*rv)[_key] = luabridge::Nil ();
432                 }
433         }
434
435 protected:
436         LuaDialogRadio (LuaDialogRadio const&); // prevent cc
437         void rb_toggled (Gtk::RadioButton* b, luabridge::LuaRef* rv) {
438                 if (b->get_active ()) {
439                         _rv = rv;
440                 }
441         }
442
443         Gtk::HBox _hbox;
444         Gtk::RadioButtonGroup _group;
445         std::vector<luabridge::LuaRef*> _refs;
446         luabridge::LuaRef* _rv;
447 };
448
449 class LuaDialogDropDown : public LuaDialogWidget
450 {
451 public:
452         LuaDialogDropDown (std::string const& key, std::string const& title, luabridge::LuaRef values, std::string const& dflt)
453                 : LuaDialogWidget (key, title)
454                 , _rv (0)
455         {
456                 populate (_dd.items (), values, dflt);
457         }
458
459         ~LuaDialogDropDown ()
460         {
461                 for (std::vector<luabridge::LuaRef*>::const_iterator i = _refs.begin (); i != _refs.end (); ++i) {
462                         delete *i;
463                 }
464                 _refs.clear ();
465         }
466
467         Gtk::Widget* widget ()
468         {
469                 return &_dd;
470         }
471
472         void assign (luabridge::LuaRef* rv) const
473         {
474                 if (_rv) {
475                         (*rv)[_key] = *_rv;
476                 } else {
477                         (*rv)[_key] = luabridge::Nil ();
478                 }
479         }
480
481 protected:
482         void populate (Gtk::Menu_Helpers::MenuList& items, luabridge::LuaRef values, std::string const& dflt)
483         {
484                 using namespace Gtk::Menu_Helpers;
485                 std::vector<std::string> keys;
486
487                 for (luabridge::Iterator i (values); !i.isNil (); ++i) {
488                         if (!i.key ().isString ())  { continue; }
489                         keys.push_back (i.key ().cast<std::string> ());
490                 }
491
492                 std::sort (keys.begin(), keys.end());
493
494                 for (std::vector<std::string>::const_iterator i = keys.begin (); i != keys.end(); ++i) {
495                         std::string key = *i;
496
497                         if (values[key].isTable ())  {
498                                 Gtk::Menu* menu  = Gtk::manage (new Gtk::Menu);
499                                 items.push_back (MenuElem (key, *menu));
500                                 populate (menu->items (), values[key], dflt);
501                                 continue;
502                         }
503                         luabridge::LuaRef* ref = new luabridge::LuaRef (values[key]);
504                         _refs.push_back (ref);
505                         items.push_back (MenuElem (key,
506                                                 sigc::bind (sigc::mem_fun (*this, &LuaDialogDropDown::dd_select), key, ref)));
507
508                         if (!_rv || key == dflt) {
509                                 _rv = ref;
510                                 _dd.set_text (key);
511                         }
512                 }
513         }
514
515         void dd_select (std::string const& key, luabridge::LuaRef* rv) {
516                 _dd.set_text (key);
517                 _rv = rv;
518         }
519
520         ArdourWidgets::ArdourDropdown _dd;
521         std::vector<luabridge::LuaRef*> _refs;
522         luabridge::LuaRef* _rv;
523 };
524
525 class LuaFileChooser : public LuaDialogWidget
526 {
527 public:
528         LuaFileChooser (std::string const& key, std::string const& title, Gtk::FileChooserAction a, const std::string& path)
529                 : LuaDialogWidget (key, title)
530                 , _fc (a)
531         {
532                 if (!path.empty ()) {
533                         switch (a) {
534                                 case Gtk::FILE_CHOOSER_ACTION_OPEN:
535                                 case Gtk::FILE_CHOOSER_ACTION_SAVE:
536                                         if (Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR|Glib::FILE_TEST_EXISTS)) {
537                                                 _fc.set_filename (path);
538                                         }
539                                         break;
540                                 case Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER:
541                                         if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
542                                                 _fc.set_filename (path);
543                                         }
544                                         break;
545                                 case Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER:
546                                         break;
547                         }
548                 }
549         }
550
551         Gtk::Widget* widget ()
552         {
553                 return &_fc;
554         }
555
556         void assign (luabridge::LuaRef* rv) const
557         {
558                 (*rv)[_key] = std::string (_fc.get_filename ());
559         }
560
561 protected:
562         Gtk::FileChooserButton _fc;
563 };
564
565
566
567 /*******************************************************************************
568  * Lua Parameter Dialog
569  */
570 Dialog::Dialog (std::string const& title, luabridge::LuaRef lr)
571         :_ad (title, true, false)
572         , _title (title)
573 {
574         if (!lr.isTable ()) {
575                 return;
576         }
577         for (luabridge::Iterator i (lr); !i.isNil (); ++i) {
578                 if (!i.key ().isNumber ())  { continue; }
579                 if (!i.value ().isTable ()) { continue; }
580                 if (!i.value ()["title"].isString ()) { continue; }
581                 if (!i.value ()["type"].isString ()) { continue; }
582
583                 std::string title = i.value ()["title"].cast<std::string> ();
584                 std::string type = i.value ()["type"].cast<std::string> ();
585                 std::string key;
586
587                 if (i.value ()["key"].isString ()) {
588                         key = i.value ()["key"].cast<std::string> ();
589                 }
590
591                 LuaDialogWidget* w = NULL;
592
593                 if (type == "heading") {
594                         Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
595                         if (i.value ()["align"].isString ()) {
596                                 std::string align = i.value ()["align"].cast <std::string> ();
597                                 if (align == "left") {
598                                         xalign = Gtk::ALIGN_LEFT;
599                                 } else if (align == "right") {
600                                         xalign = Gtk::ALIGN_RIGHT;
601                                 }
602                         }
603                         w = new LuaDialogHeading (title, xalign);
604                 } else if (type == "label") {
605                         Gtk::AlignmentEnum xalign = Gtk::ALIGN_CENTER;
606                         if (i.value ()["align"].isString ()) {
607                                 std::string align = i.value ()["align"].cast <std::string> ();
608                                 if (align == "left") {
609                                         xalign = Gtk::ALIGN_LEFT;
610                                 } else if (align == "right") {
611                                         xalign = Gtk::ALIGN_RIGHT;
612                                 }
613                         }
614                         w = new LuaDialogLabel (title, xalign);
615                 } else if (type == "hseparator") {
616                         w = new LuaHSeparator ();
617                 }
618                 /* the following widgets do require a key */
619                 else if (key.empty ()) {
620                         continue;
621                 }
622                 else if (type == "checkbox") {
623                         bool dflt = false;
624                         if (i.value ()["default"].isBoolean ()) {
625                                 dflt = i.value ()["default"].cast<bool> ();
626                         }
627                         w = new LuaDialogCheckbox (key, title, dflt);
628                 } else if (type == "entry") {
629                         std::string dflt;
630                         if (i.value ()["default"].isString ()) {
631                                 dflt = i.value ()["default"].cast<std::string> ();
632                         }
633                         w = new LuaDialogEntry (key, title, dflt);
634                 } else if (type == "radio") {
635                         std::string dflt;
636                         if (!i.value ()["values"].isTable ()) {
637                                 continue;
638                         }
639                         if (i.value ()["default"].isString ()) {
640                                 dflt = i.value ()["default"].cast<std::string> ();
641                         }
642                         w = new LuaDialogRadio (key, title, i.value ()["values"], dflt);
643                 } else if (type == "fader") {
644                         double dflt = 0;
645                         if (i.value ()["default"].isNumber ()) {
646                                 dflt = i.value ()["default"].cast<double> ();
647                         }
648                         w = new LuaDialogFader (key, title, dflt);
649                 } else if (type == "slider") {
650                         double lower, upper, dflt;
651                         int digits = 0;
652                         if (!i.value ()["min"].isNumber ()) { continue; }
653                         if (!i.value ()["max"].isNumber ()) { continue; }
654                         lower = i.value ()["min"].cast<double> ();
655                         upper = i.value ()["max"].cast<double> ();
656                         if (i.value ()["default"].isNumber ()) {
657                                 dflt = i.value ()["default"].cast<double> ();
658                         } else {
659                                 dflt = lower;
660                         }
661                         if (i.value ()["digits"].isNumber ()) {
662                                 digits = i.value ()["digits"].cast<int> ();
663                         }
664                         w = new LuaDialogSlider (key, title, lower, upper, dflt, digits, i.value ()["scalepoints"]);
665                 } else if (type == "number") {
666                         double lower, upper, dflt, step;
667                         int digits = 0;
668                         if (!i.value ()["min"].isNumber ()) { continue; }
669                         if (!i.value ()["max"].isNumber ()) { continue; }
670                         lower = i.value ()["min"].cast<double> ();
671                         upper = i.value ()["max"].cast<double> ();
672                         if (i.value ()["default"].isNumber ()) {
673                                 dflt = i.value ()["default"].cast<double> ();
674                         } else {
675                                 dflt = lower;
676                         }
677                         if (i.value ()["step"].isNumber ()) {
678                                 step = i.value ()["step"].cast<double> ();
679                         } else {
680                                 step = 1.0;
681                         }
682                         if (i.value ()["digits"].isNumber ()) {
683                                 digits = i.value ()["digits"].cast<int> ();
684                         }
685                         w = new LuaDialogSpinBox (key, title, lower, upper, dflt, step, digits);
686                 } else if (type == "dropdown") {
687                         std::string dflt;
688                         if (!i.value ()["values"].isTable ()) {
689                                 continue;
690                         }
691                         if (i.value ()["default"].isString ()) {
692                                 dflt = i.value ()["default"].cast<std::string> ();
693                         }
694                         w = new LuaDialogDropDown (key, title, i.value ()["values"], dflt);
695                 } else if (type == "file") {
696                         std::string path;
697                         if (i.value ()["path"].isString ()) {
698                                 path = i.value ()["path"].cast<std::string> ();
699                         }
700                         w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_OPEN, path);
701                 } else if (type == "folder") {
702                         std::string path;
703                         if (i.value ()["path"].isString ()) {
704                                 path = i.value ()["path"].cast<std::string> ();
705                         }
706                         w = new LuaFileChooser (key, title, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, path);
707                 } else if (type == "color") {
708                         w = new LuaColorPicker (key);
709                 }
710
711                 if (w) {
712                         if (i.value ()["col"].isNumber ()) {
713                                 w->set_col (i.value ()["col"].cast<int> ());
714                         }
715                         if (i.value ()["colspan"].isNumber ()) {
716                                 w->set_span (i.value ()["colspan"].cast<int> ());
717                         }
718                         _widgets.push_back(w);
719                 }
720         }
721
722         _ad.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
723         _ad.add_button (Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
724
725         Gtk::Table* table = Gtk::manage (new Gtk::Table ());
726         table->set_col_spacings (20);
727         table->set_row_spacings (8);
728         _ad.get_vbox ()->pack_start (*table);
729
730         int row = 0;
731         int last_end = -1;
732
733         for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end (); ++i) {
734                 int col = (*i)->col();
735                 int cend = col + (*i)->span();
736
737                 if (col < last_end) {
738                         ++row;
739                 }
740                 last_end = cend;
741
742                 std::string const& label = (*i)->label ();
743                 if (!label.empty ()) {
744                         /* items with implicit label (title) */
745                         Gtk::Label* lbl = Gtk::manage (new Gtk::Label (label + ":", Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
746                         if (cend - col > 1) {
747                                 table->attach (*lbl, col, col + 1, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
748                                 table->attach (*((*i)->widget ()), col + 1, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
749                         } else {
750                                 Gtk::HBox* hb = Gtk::manage (new Gtk::HBox());
751                                 hb->set_spacing(4);
752                                 hb->pack_start (*lbl, true, false);
753                                 hb->pack_start (*(*i)->widget (), true, false);
754                                 table->attach (*hb, col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
755                         }
756                 } else {
757                         table->attach (*((*i)->widget ()), col, cend, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
758                 }
759         }
760 }
761
762 Dialog::~Dialog ()
763 {
764         for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end () ; ++i) {
765                 delete *i;
766         }
767         _widgets.clear ();
768 }
769
770 int
771 Dialog::run (lua_State *L)
772 {
773         _ad.get_vbox ()->show_all ();
774         switch (_ad.run ()) {
775                 case Gtk::RESPONSE_ACCEPT:
776                         break;
777                 default:
778                         lua_pushnil (L);
779                         return 1;
780         }
781
782         luabridge::LuaRef rv (luabridge::newTable (L));
783         for (DialogWidgets::const_iterator i = _widgets.begin (); i != _widgets.end () ; ++i) {
784                 (*i)->assign (&rv);
785         }
786         luabridge::push (L, rv);
787         return 1;
788 }