~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to src/auto4_ruby_dialog.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2006, 2007, Niels Martin Hansen, Patryk Pomykalski
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are met:
 
6
//
 
7
//   * Redistributions of source code must retain the above copyright notice,
 
8
//     this list of conditions and the following disclaimer.
 
9
//   * Redistributions in binary form must reproduce the above copyright notice,
 
10
//     this list of conditions and the following disclaimer in the documentation
 
11
//     and/or other materials provided with the distribution.
 
12
//   * Neither the name of the Aegisub Group nor the names of its contributors
 
13
//     may be used to endorse or promote products derived from this software
 
14
//     without specific prior written permission.
 
15
//
 
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
// POSSIBILITY OF SUCH DAMAGE.
 
27
//
 
28
// -----------------------------------------------------------------------------
 
29
//
 
30
// AEGISUB
 
31
//
 
32
// Website: http://aegisub.cellosoft.com
 
33
// Contact: mailto:pomyk@go2.pl
 
34
//
 
35
 
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#ifdef WITH_RUBY
 
40
#include "auto4_ruby.h"
 
41
#include <ruby.h>
 
42
#include <wx/window.h>
 
43
#include <wx/spinctrl.h>
 
44
#include <wx/gbsizer.h>
 
45
#include <wx/button.h>
 
46
#include <wx/validate.h>
 
47
#include <assert.h>
 
48
 
 
49
namespace Automation4 {
 
50
 
 
51
 
 
52
        // RubyConfigDialogControl
 
53
 
 
54
        RubyConfigDialogControl::RubyConfigDialogControl(VALUE opts)
 
55
        {
 
56
                VALUE val = rb_hash_aref(opts, STR2SYM("name"));
 
57
                name_sym = val;
 
58
                if(TYPE(val) == T_STRING) {
 
59
                        name = wxString(StringValueCStr(val), wxConvUTF8);
 
60
                } else if(TYPE(val) == T_SYMBOL) {
 
61
                        name = wxString(rb_id2name(SYM2ID(val)), wxConvUTF8);
 
62
                } else name = _T("");
 
63
 
 
64
                val = rb_hash_aref(opts, STR2SYM("x"));
 
65
                if(TYPE(val) == T_FIXNUM) {
 
66
                        x = FIX2INT(val);
 
67
                        if (x < 0) x = 0;
 
68
                }
 
69
                else x = 0;
 
70
 
 
71
                val = rb_hash_aref(opts, STR2SYM("y"));
 
72
                if(TYPE(val) == T_FIXNUM) {
 
73
                        y = FIX2INT(val);
 
74
                        if (y < 0) y = 0;
 
75
                }
 
76
                else y = 0;
 
77
 
 
78
                val = rb_hash_aref(opts, STR2SYM("width"));
 
79
                if(TYPE(val) == T_FIXNUM) {
 
80
                        width = FIX2INT(val);
 
81
                        if (width < 1) width = 1;
 
82
                }
 
83
                else width = 1;
 
84
 
 
85
                val = rb_hash_aref(opts, STR2SYM("height"));
 
86
                if(TYPE(val) == T_FIXNUM) {
 
87
                        height = FIX2INT(val);
 
88
                        if (height < 1) width = 1;
 
89
                }
 
90
                else height = 1;
 
91
 
 
92
                val = rb_hash_aref(opts, STR2SYM("hint"));
 
93
                if(TYPE(val) == T_STRING)
 
94
                        hint = wxString(StringValueCStr(val), wxConvUTF8);
 
95
                else hint = _T("");
 
96
 
 
97
                wxLogDebug(_T("created control: '%s', (%d,%d)(%d,%d), '%s'"), name.c_str(), x, y, width, height, hint.c_str());
 
98
        }
 
99
 
 
100
        namespace RubyControl {
 
101
 
 
102
                // Label
 
103
 
 
104
                class Label : public RubyConfigDialogControl {
 
105
                public:
 
106
                        wxString label;
 
107
 
 
108
                        Label(){};
 
109
                        Label(VALUE opts)
 
110
                                : RubyConfigDialogControl(opts)
 
111
                        {
 
112
                                VALUE val = rb_hash_aref(opts, STR2SYM("label"));
 
113
                                if(TYPE(val) == T_STRING)
 
114
                                        label = wxString(StringValueCStr(val), wxConvUTF8);
 
115
                                else label = _T("");
 
116
                        }
 
117
 
 
118
                        virtual ~Label() { }
 
119
 
 
120
                        wxControl *Create(wxWindow *parent)
 
121
                        {
 
122
                                return cw = new wxStaticText(parent, -1, label);
 
123
                        }
 
124
 
 
125
                        void ControlReadBack()
 
126
                        {
 
127
                                // Nothing here
 
128
                        }
 
129
 
 
130
                        VALUE RubyReadBack()
 
131
                        {
 
132
                                return Qnil;
 
133
                        }
 
134
                };
 
135
 
 
136
 
 
137
                // Basic edit
 
138
 
 
139
                class Edit : public RubyConfigDialogControl {
 
140
                public:
 
141
                        wxString text;
 
142
 
 
143
                        Edit(){};
 
144
                        Edit(VALUE opts)
 
145
                                : RubyConfigDialogControl(opts)
 
146
                        {
 
147
                                VALUE val = rb_hash_aref(opts, STR2SYM("text"));
 
148
                                if(TYPE(val) == T_STRING)
 
149
                                        text = wxString(StringValueCStr(val), wxConvUTF8);
 
150
                                else text = _T("");
 
151
                        }
 
152
 
 
153
                        virtual ~Edit() { }
 
154
 
 
155
                        wxControl *Create(wxWindow *parent)
 
156
                        {
 
157
                                return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0);
 
158
                        }
 
159
 
 
160
                        void ControlReadBack()
 
161
                        {
 
162
                                text = ((wxTextCtrl*)cw)->GetValue();
 
163
                        }
 
164
 
 
165
                        VALUE RubyReadBack()
 
166
                        {
 
167
                //              if(text.IsEmpty()) return rb_str_new("", 0);
 
168
                                return rb_str_new2(text.mb_str(wxConvUTF8));
 
169
                        }
 
170
 
 
171
                };
 
172
 
 
173
                
 
174
                // Multiline edit
 
175
 
 
176
                class Textbox : public Edit {
 
177
                public:
 
178
 
 
179
                        Textbox(){};
 
180
                        Textbox(VALUE opts)
 
181
                                : Edit(opts)
 
182
                        {
 
183
                                // Nothing more
 
184
                        }
 
185
 
 
186
                        virtual ~Textbox() { }
 
187
 
 
188
                        wxControl *Create(wxWindow *parent)
 
189
                        {
 
190
                                cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
 
191
                                cw->SetMinSize(wxSize(0, 30));
 
192
                                return cw;
 
193
                        }
 
194
 
 
195
                };
 
196
 
 
197
 
 
198
                // Integer only edit
 
199
 
 
200
                class IntEdit : public Edit {
 
201
                public:
 
202
                        int value;
 
203
                        bool hasspin;
 
204
                        int min, max;
 
205
 
 
206
                        IntEdit(){};
 
207
                        IntEdit(VALUE opts)
 
208
                                : Edit(opts)
 
209
                        {
 
210
                                VALUE val = rb_hash_aref(opts, STR2SYM("value"));
 
211
                                if(TYPE(val) == T_FIXNUM) {
 
212
                                        value = FIX2INT(val);
 
213
                                }
 
214
 
 
215
                                hasspin = false;
 
216
                                val = rb_hash_aref(opts, STR2SYM("min"));
 
217
                                if(TYPE(val) == T_FIXNUM) {
 
218
                                        min = FIX2INT(val);
 
219
                                }
 
220
                                else return;
 
221
 
 
222
                                val = rb_hash_aref(opts, STR2SYM("max"));
 
223
                                if(TYPE(val) == T_FIXNUM) {
 
224
                                        max = FIX2INT(val);
 
225
                                        hasspin = true;
 
226
                                }
 
227
                        }
 
228
 
 
229
                        virtual ~IntEdit() { }
 
230
 
 
231
                        typedef wxValidator IntTextValidator; // TODO
 
232
                        wxControl *Create(wxWindow *parent)
 
233
                        {
 
234
                                if (hasspin) {
 
235
                                        return cw = new wxSpinCtrl(parent, -1, wxString::Format(_T("%d"), value), wxDefaultPosition, wxDefaultSize, min, max, value);
 
236
                                } else {
 
237
                                        return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0); //, IntTextValidator());
 
238
                                }
 
239
                        }
 
240
 
 
241
                        void ControlReadBack()
 
242
                        {
 
243
                                if (hasspin) {
 
244
                                        value = ((wxSpinCtrl*)cw)->GetValue();
 
245
                                } else {
 
246
                                        long newval;
 
247
                                        text = ((wxTextCtrl*)cw)->GetValue();
 
248
                                        if (text.ToLong(&newval)) {
 
249
                                                value = newval;
 
250
                                        }
 
251
                                }
 
252
                        }
 
253
 
 
254
                        VALUE RubyReadBack()
 
255
                        {
 
256
                                return INT2FIX(value);
 
257
                        }
 
258
 
 
259
                };
 
260
 
 
261
 
 
262
                // Float only edit
 
263
 
 
264
                class FloatEdit : public Edit {
 
265
                public:
 
266
                        float value;
 
267
                        // FIXME: Can't support spin button atm
 
268
 
 
269
                        FloatEdit(){};
 
270
                        FloatEdit(VALUE opts)
 
271
                                : Edit(opts)
 
272
                        {
 
273
                                VALUE val = rb_hash_aref(opts, STR2SYM("value"));
 
274
                                if(TYPE(val) == T_FLOAT) {
 
275
                                        value = NUM2DBL(val);
 
276
                                } else if (TYPE(val) == T_FIXNUM) {
 
277
                                        value = FIX2INT(val);
 
278
                                }
 
279
                                // TODO: spin button support
 
280
                        }
 
281
 
 
282
                        virtual ~FloatEdit() { }
 
283
 
 
284
                        typedef wxValidator FloatTextValidator;
 
285
                        wxControl *Create(wxWindow *parent)
 
286
                        {
 
287
                                return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0); //, FloatTextValidator());
 
288
                        }
 
289
 
 
290
                        void ControlReadBack()
 
291
                        {
 
292
                                double newval;
 
293
                                text = ((wxTextCtrl*)cw)->GetValue();
 
294
                                if (text.ToDouble(&newval)) {
 
295
                                        value = newval;
 
296
                                }
 
297
                        }
 
298
 
 
299
                        VALUE RubyReadBack()
 
300
                        {
 
301
                                return rb_float_new(value);
 
302
                        }
 
303
 
 
304
                };
 
305
 
 
306
 
 
307
                // Dropdown
 
308
 
 
309
                class Dropdown : public RubyConfigDialogControl {
 
310
                public:
 
311
                        wxArrayString items;
 
312
                        wxString value;
 
313
 
 
314
                        Dropdown(){};
 
315
                        Dropdown(VALUE opts)
 
316
                                : RubyConfigDialogControl(opts)
 
317
                        {
 
318
                                VALUE val = rb_hash_aref(opts, STR2SYM("value"));
 
319
                                if(TYPE(val) == T_STRING)
 
320
                                        value = wxString(StringValueCStr(val), wxConvUTF8);
 
321
                                
 
322
                                val = rb_hash_aref(opts, STR2SYM("items"));
 
323
                                if(TYPE(val) == T_ARRAY)
 
324
                                {
 
325
                                        long len = RARRAY(val)->len;
 
326
                                        VALUE *ptr = RARRAY(val)->ptr;
 
327
                                        for(int i = 0; i < len; i++)
 
328
                                        {
 
329
                                                if(TYPE(ptr[i]) == T_STRING)
 
330
                                                        items.Add(wxString(StringValueCStr(ptr[i]), wxConvUTF8));
 
331
                                        }
 
332
                                }
 
333
                        }
 
334
 
 
335
                        virtual ~Dropdown() { }
 
336
 
 
337
                        wxControl *Create(wxWindow *parent)
 
338
                        {
 
339
                                return cw = new wxComboBox(parent, -1, value, wxDefaultPosition, wxDefaultSize, items, wxCB_READONLY);
 
340
                        }
 
341
 
 
342
                        void ControlReadBack()
 
343
                        {
 
344
                                value = ((wxComboBox*)cw)->GetValue();
 
345
                        }
 
346
 
 
347
                        VALUE RubyReadBack()
 
348
                        {
 
349
                                return rb_str_new2(value.mb_str(wxConvUTF8));
 
350
                        }
 
351
                };
 
352
 
 
353
 
 
354
                // Checkbox
 
355
 
 
356
                class Checkbox : public RubyConfigDialogControl {
 
357
                public:
 
358
                        wxString label;
 
359
                        bool value;
 
360
 
 
361
                        Checkbox(){};
 
362
                        Checkbox(VALUE opts)
 
363
                                : RubyConfigDialogControl(opts)
 
364
                        {
 
365
                                VALUE val = rb_hash_aref(opts, STR2SYM("label"));
 
366
                                if(TYPE(val) == T_STRING)
 
367
                                        label = wxString(StringValueCStr(val), wxConvUTF8);
 
368
 
 
369
                                val = rb_hash_aref(opts, STR2SYM("value"));
 
370
                                if(val == Qtrue) value = true;
 
371
                                else value = false;
 
372
                        }
 
373
 
 
374
                        virtual ~Checkbox() { }
 
375
 
 
376
                        wxControl *Create(wxWindow *parent)
 
377
                        {
 
378
                                cw = new wxCheckBox(parent, -1, label);
 
379
                                ((wxCheckBox*)cw)->SetValue(value);
 
380
                                return cw;
 
381
                        }
 
382
 
 
383
                        void ControlReadBack()
 
384
                        {
 
385
                                value = ((wxCheckBox*)cw)->GetValue();
 
386
                        }
 
387
 
 
388
                        VALUE RubyReadBack()
 
389
                        {
 
390
                                if(value) return Qtrue;
 
391
                                return Qfalse;
 
392
                        }
 
393
 
 
394
                };
 
395
 
 
396
        };
 
397
 
 
398
 
 
399
        // RubyConfigDialog
 
400
 
 
401
        RubyConfigDialog::RubyConfigDialog(VALUE config, VALUE btn_data, bool include_buttons)
 
402
                : use_buttons(include_buttons)
 
403
        {
 
404
                wxLogDebug(_T("creating RubyConfigDialog, this addr is %p"), this);
 
405
                button_pushed = 0;
 
406
        
 
407
                if(include_buttons && TYPE(btn_data) == T_ARRAY) 
 
408
                {
 
409
                        long len = RARRAY(btn_data)->len;
 
410
                        VALUE *ptr = RARRAY(btn_data)->ptr;
 
411
                        for(int i = 0; i < len; i++)
 
412
                        {                       
 
413
                                if(rb_respond_to(ptr[i], rb_intern("to_s")))
 
414
                                {
 
415
                                        ptr[i] = rb_funcall(ptr[i], rb_intern("to_s"), 0);
 
416
                                        wxString s(StringValueCStr(ptr[i]), wxConvUTF8);
 
417
                                        buttons.push_back(s);
 
418
                                }
 
419
                        }
 
420
                }
 
421
 
 
422
                if(TYPE(config) != T_ARRAY)     {
 
423
                        if(rb_respond_to(config, rb_intern("to_ary")))
 
424
                                config = rb_funcall(config, rb_intern("to_ary"), 0);
 
425
                        else throw "Cannot create config dialog from something non-table";
 
426
                }
 
427
 
 
428
                long len = RARRAY(config)->len;
 
429
                VALUE *ptr = RARRAY(config)->ptr;
 
430
                for(int i = 0; i < len; i++)
 
431
                {
 
432
                        if(TYPE(ptr[i]) != T_HASH)
 
433
                                continue;       // skip invalid entry
 
434
 
 
435
                        VALUE ctrlclass = rb_hash_aref(ptr[i], STR2SYM("class"));
 
436
 
 
437
                        const char *cls_name;
 
438
                        if (TYPE(ctrlclass) == T_SYMBOL) {
 
439
                                        cls_name = rb_id2name(SYM2ID(ctrlclass));
 
440
                        } else if (TYPE(ctrlclass) == T_STRING) {
 
441
                                cls_name = StringValueCStr(ctrlclass);
 
442
                        } else continue;
 
443
                        wxString controlclass(cls_name, wxConvUTF8);
 
444
 
 
445
                        RubyConfigDialogControl *ctl;
 
446
 
 
447
                        // Check control class and create relevant control
 
448
                        if (controlclass == _T("label")) {
 
449
                                ctl = new RubyControl::Label(ptr[i]);
 
450
                        } else if (controlclass == _T("edit")) {
 
451
                                ctl = new RubyControl::Edit(ptr[i]);
 
452
                        } else if (controlclass == _T("intedit")) {
 
453
                                ctl = new RubyControl::IntEdit(ptr[i]);
 
454
                        } else if (controlclass == _T("floatedit")) {
 
455
                                ctl = new RubyControl::FloatEdit(ptr[i]);
 
456
                        } else if (controlclass == _T("textbox")) {
 
457
                                ctl = new RubyControl::Textbox(ptr[i]);
 
458
                        } else if (controlclass == _T("dropdown")) {
 
459
                                ctl = new RubyControl::Dropdown(ptr[i]);
 
460
                        } else if (controlclass == _T("checkbox")) {
 
461
                                ctl = new RubyControl::Checkbox(ptr[i]);
 
462
                        } else if (controlclass == _T("color")) {
 
463
                                // FIXME
 
464
                                ctl = new RubyControl::Edit(ptr[i]);
 
465
                        } else if (controlclass == _T("coloralpha")) {
 
466
                                // FIXME
 
467
                                ctl = new RubyControl::Edit(ptr[i]);
 
468
                        } else if (controlclass == _T("alpha")) {
 
469
                                // FIXME
 
470
                                ctl = new RubyControl::Edit(ptr[i]);
 
471
                        } else continue;        // skip
 
472
 
 
473
                        controls.push_back(ctl);
 
474
                }
 
475
        }
 
476
 
 
477
        RubyConfigDialog::~RubyConfigDialog()
 
478
        {
 
479
                for (size_t i = 0; i < controls.size(); ++i)
 
480
                        delete controls[i];
 
481
        }
 
482
 
 
483
        wxWindow* RubyConfigDialog::CreateWindow(wxWindow *parent)
 
484
        {
 
485
                wxWindow *w = new wxPanel(parent);
 
486
                wxGridBagSizer *s = new wxGridBagSizer(4, 4);
 
487
 
 
488
                for (size_t i = 0; i < controls.size(); ++i) {
 
489
                        RubyConfigDialogControl *c = controls[i];
 
490
                        c->Create(w);
 
491
                        if (dynamic_cast<RubyControl::Label*>(c)) {
 
492
                                s->Add(c->cw, wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT);
 
493
                        } else {
 
494
                                s->Add(c->cw, wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), wxEXPAND);
 
495
                        }
 
496
                }
 
497
 
 
498
                if (use_buttons) {
 
499
                        wxStdDialogButtonSizer *bs = new wxStdDialogButtonSizer();
 
500
                        if (buttons.size() > 0) {
 
501
                                wxLogDebug(_T("creating user buttons"));
 
502
                                for (size_t i = 0; i < buttons.size(); ++i) {
 
503
                                        wxLogDebug(_T("button '%s' gets id %d"), buttons[i].c_str(), 1001+(wxWindowID)i);
 
504
                                        bs->Add(new wxButton(w, 1001+(wxWindowID)i, buttons[i]));
 
505
                                }
 
506
                        } else {
 
507
                                wxLogDebug(_T("creating default buttons"));
 
508
                                bs->Add(new wxButton(w, wxID_OK));
 
509
                                bs->Add(new wxButton(w, wxID_CANCEL));
 
510
                        }
 
511
                        bs->Realize();
 
512
 
 
513
                        button_event = new ButtonEventHandler();
 
514
                        button_event->button_pushed = &button_pushed;
 
515
                        // passing button_event as userdata because wx will then delete it
 
516
                        w->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RubyConfigDialog::ButtonEventHandler::OnButtonPush), button_event, button_event);
 
517
                        wxLogDebug(_T("set event handler, this addr is %p"), this);
 
518
 
 
519
                        wxBoxSizer *ms = new wxBoxSizer(wxVERTICAL);
 
520
                        ms->Add(s, 0, wxBOTTOM, 5);
 
521
                        ms->Add(bs);
 
522
                        w->SetSizerAndFit(ms);
 
523
                } else {
 
524
                        w->SetSizerAndFit(s);
 
525
                }
 
526
 
 
527
                return w;
 
528
        }
 
529
 
 
530
        VALUE RubyConfigDialog::RubyReadBack()
 
531
        {
 
532
                VALUE cfg = rb_hash_new();
 
533
 
 
534
                for (size_t i = 0; i < controls.size(); ++i) {
 
535
                        VALUE v = controls[i]->RubyReadBack();
 
536
                        if(v != Qnil)
 
537
                                rb_hash_aset(cfg, controls[i]->name_sym, v);    
 
538
                }
 
539
                if (use_buttons) {
 
540
                        VALUE res = rb_ary_new();
 
541
 
 
542
                        wxLogDebug(_T("reading back button_pushed"));
 
543
                        int btn = button_pushed;
 
544
                        if (btn == 0) {
 
545
                                wxLogDebug(_T("was zero, cancelled"));
 
546
                                // Always cancel/closed
 
547
                                rb_ary_push(res, Qfalse);
 
548
                        } else {
 
549
                                wxLogDebug(_T("nonzero, something else: %d"), btn);
 
550
                                if (buttons.size() > 0) {
 
551
                                        wxLogDebug(_T("user button: %s"), buttons[btn-1].c_str());
 
552
                                        // button_pushed is index+1 to reserve 0 for Cancel
 
553
                                        rb_ary_push(res, rb_str_new2(buttons[btn-1].mb_str(wxConvUTF8)));                                       
 
554
                                } else {
 
555
                                        wxLogDebug(_T("default button, must be Ok"));
 
556
                                        // Cancel case already covered, must be Ok then
 
557
                                        rb_ary_push(res, Qtrue);                                        
 
558
                                }
 
559
                        }
 
560
                        rb_ary_push(res, cfg);  // return array [button, hash with config]
 
561
                        return res;
 
562
                }
 
563
 
 
564
                return cfg;     // if no buttons return only hash with config
 
565
        }
 
566
 
 
567
        void RubyConfigDialog::ReadBack()
 
568
        {
 
569
                for (size_t i = 0; i < controls.size(); ++i) {
 
570
                        controls[i]->ControlReadBack();
 
571
                }
 
572
        }
 
573
 
 
574
        void RubyConfigDialog::ButtonEventHandler::OnButtonPush(wxCommandEvent &evt)
 
575
        {
 
576
                // Let button_pushed == 0 mean "cancelled", such that pushing Cancel or closing the dialog
 
577
                // will both result in button_pushed == 0
 
578
                if (evt.GetId() == wxID_OK) {
 
579
                        wxLogDebug(_T("was wxID_OK"));
 
580
                        *button_pushed = 1;
 
581
                } else if (evt.GetId() == wxID_CANCEL) {
 
582
                        wxLogDebug(_T("was wxID_CANCEL"));
 
583
                        *button_pushed = 0;
 
584
                } else {
 
585
                        wxLogDebug(_T("was user button"));
 
586
                        // Therefore, when buttons are numbered from 1001 to 1000+n, make sure to set it to i+1
 
587
                        *button_pushed = evt.GetId() - 1000;
 
588
                        evt.SetId(wxID_OK); // hack to make sure the dialog will be closed
 
589
                }
 
590
                wxLogDebug(_T("button_pushed set to %d"), *button_pushed);
 
591
                evt.Skip();
 
592
        }
 
593
 
 
594
};
 
595
 
 
596
#endif // WITH_RUBY