2007-02-06 23:58:05 +01:00
|
|
|
// Copyright (c) 2006, 2007, Niels Martin Hansen, Patryk Pomykalski
|
2007-01-29 18:52:46 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
2007-02-06 23:58:05 +01:00
|
|
|
// Contact: mailto:pomyk@go2.pl
|
2007-01-29 18:52:46 +01:00
|
|
|
//
|
|
|
|
|
2008-01-16 19:29:29 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#ifdef WITH_RUBY
|
2007-01-29 18:52:46 +01:00
|
|
|
#include "auto4_ruby.h"
|
2007-02-01 19:20:55 +01:00
|
|
|
#include <ruby.h>
|
2007-01-29 18:52:46 +01:00
|
|
|
#include <wx/window.h>
|
|
|
|
#include <wx/spinctrl.h>
|
|
|
|
#include <wx/gbsizer.h>
|
|
|
|
#include <wx/button.h>
|
|
|
|
#include <wx/validate.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
namespace Automation4 {
|
|
|
|
|
|
|
|
|
|
|
|
// RubyConfigDialogControl
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
RubyConfigDialogControl::RubyConfigDialogControl(VALUE opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("name"));
|
|
|
|
name_sym = val;
|
|
|
|
if(TYPE(val) == T_STRING) {
|
2007-02-03 21:31:20 +01:00
|
|
|
name = wxString(StringValueCStr(val), wxConvUTF8);
|
2007-02-07 19:52:23 +01:00
|
|
|
} else if(TYPE(val) == T_SYMBOL) {
|
|
|
|
name = wxString(rb_id2name(SYM2ID(val)), wxConvUTF8);
|
|
|
|
} else name = _T("");
|
2007-02-03 21:31:20 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("x"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
x = FIX2INT(val);
|
2007-01-29 18:52:46 +01:00
|
|
|
if (x < 0) x = 0;
|
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
else x = 0;
|
2007-01-29 18:52:46 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("y"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
y = FIX2INT(val);
|
2007-01-29 18:52:46 +01:00
|
|
|
if (y < 0) y = 0;
|
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
else y = 0;
|
2007-01-29 18:52:46 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("width"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
width = FIX2INT(val);
|
2007-01-29 18:52:46 +01:00
|
|
|
if (width < 1) width = 1;
|
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
else width = 1;
|
2007-01-29 18:52:46 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("height"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
height = FIX2INT(val);
|
|
|
|
if (height < 1) width = 1;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
else height = 1;
|
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("hint"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_STRING)
|
|
|
|
hint = wxString(StringValueCStr(val), wxConvUTF8);
|
|
|
|
else hint = _T("");
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
wxLogDebug(_T("created control: '%s', (%d,%d)(%d,%d), '%s'"), name.c_str(), x, y, width, height, hint.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace RubyControl {
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
|
|
|
class Label : public RubyConfigDialogControl {
|
|
|
|
public:
|
|
|
|
wxString label;
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
Label(){};
|
|
|
|
Label(VALUE opts)
|
|
|
|
: RubyConfigDialogControl(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("label"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_STRING)
|
|
|
|
label = wxString(StringValueCStr(val), wxConvUTF8);
|
|
|
|
else label = _T("");
|
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
virtual ~Label() { }
|
|
|
|
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
return cw = new wxStaticText(parent, -1, label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
return Qnil;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Basic edit
|
|
|
|
|
|
|
|
class Edit : public RubyConfigDialogControl {
|
|
|
|
public:
|
|
|
|
wxString text;
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
Edit(){};
|
|
|
|
Edit(VALUE opts)
|
|
|
|
: RubyConfigDialogControl(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("text"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_STRING)
|
|
|
|
text = wxString(StringValueCStr(val), wxConvUTF8);
|
|
|
|
else text = _T("");
|
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
virtual ~Edit() { }
|
|
|
|
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
text = ((wxTextCtrl*)cw)->GetValue();
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-06 23:58:05 +01:00
|
|
|
// if(text.IsEmpty()) return rb_str_new("", 0);
|
2007-02-03 21:31:20 +01:00
|
|
|
return rb_str_new2(text.mb_str(wxConvUTF8));
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Multiline edit
|
|
|
|
|
|
|
|
class Textbox : public Edit {
|
|
|
|
public:
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
Textbox(){};
|
|
|
|
Textbox(VALUE opts)
|
|
|
|
: Edit(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
|
|
|
// Nothing more
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Textbox() { }
|
|
|
|
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
|
|
|
|
cw->SetMinSize(wxSize(0, 30));
|
|
|
|
return cw;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Integer only edit
|
|
|
|
|
|
|
|
class IntEdit : public Edit {
|
|
|
|
public:
|
|
|
|
int value;
|
|
|
|
bool hasspin;
|
|
|
|
int min, max;
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
IntEdit(){};
|
|
|
|
IntEdit(VALUE opts)
|
|
|
|
: Edit(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("value"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
value = FIX2INT(val);
|
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
hasspin = false;
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("min"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
|
|
|
min = FIX2INT(val);
|
|
|
|
}
|
|
|
|
else return;
|
2007-01-29 18:52:46 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("max"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FIXNUM) {
|
2007-02-07 19:52:23 +01:00
|
|
|
max = FIX2INT(val);
|
2007-02-03 21:31:20 +01:00
|
|
|
hasspin = true;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
virtual ~IntEdit() { }
|
|
|
|
|
|
|
|
typedef wxValidator IntTextValidator; // TODO
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
if (hasspin) {
|
|
|
|
return cw = new wxSpinCtrl(parent, -1, wxString::Format(_T("%d"), value), wxDefaultPosition, wxDefaultSize, min, max, value);
|
|
|
|
} else {
|
|
|
|
return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0); //, IntTextValidator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
if (hasspin) {
|
|
|
|
value = ((wxSpinCtrl*)cw)->GetValue();
|
|
|
|
} else {
|
|
|
|
long newval;
|
|
|
|
text = ((wxTextCtrl*)cw)->GetValue();
|
|
|
|
if (text.ToLong(&newval)) {
|
|
|
|
value = newval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
return INT2FIX(value);
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Float only edit
|
|
|
|
|
|
|
|
class FloatEdit : public Edit {
|
|
|
|
public:
|
|
|
|
float value;
|
|
|
|
// FIXME: Can't support spin button atm
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
FloatEdit(){};
|
|
|
|
FloatEdit(VALUE opts)
|
|
|
|
: Edit(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("value"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_FLOAT) {
|
|
|
|
value = NUM2DBL(val);
|
|
|
|
} else if (TYPE(val) == T_FIXNUM) {
|
|
|
|
value = FIX2INT(val);
|
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
// TODO: spin button support
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~FloatEdit() { }
|
|
|
|
|
|
|
|
typedef wxValidator FloatTextValidator;
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
return cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, 0); //, FloatTextValidator());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
double newval;
|
|
|
|
text = ((wxTextCtrl*)cw)->GetValue();
|
|
|
|
if (text.ToDouble(&newval)) {
|
|
|
|
value = newval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
return rb_float_new(value);
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Dropdown
|
|
|
|
|
|
|
|
class Dropdown : public RubyConfigDialogControl {
|
|
|
|
public:
|
|
|
|
wxArrayString items;
|
|
|
|
wxString value;
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
Dropdown(){};
|
|
|
|
Dropdown(VALUE opts)
|
|
|
|
: RubyConfigDialogControl(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("value"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_STRING)
|
|
|
|
value = wxString(StringValueCStr(val), wxConvUTF8);
|
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("items"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_ARRAY)
|
|
|
|
{
|
|
|
|
long len = RARRAY(val)->len;
|
|
|
|
VALUE *ptr = RARRAY(val)->ptr;
|
|
|
|
for(int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if(TYPE(ptr[i]) == T_STRING)
|
|
|
|
items.Add(wxString(StringValueCStr(ptr[i]), wxConvUTF8));
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
virtual ~Dropdown() { }
|
|
|
|
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
|
|
|
return cw = new wxComboBox(parent, -1, value, wxDefaultPosition, wxDefaultSize, items, wxCB_READONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
value = ((wxComboBox*)cw)->GetValue();
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
return rb_str_new2(value.mb_str(wxConvUTF8));
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Checkbox
|
|
|
|
|
|
|
|
class Checkbox : public RubyConfigDialogControl {
|
|
|
|
public:
|
|
|
|
wxString label;
|
|
|
|
bool value;
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
Checkbox(){};
|
|
|
|
Checkbox(VALUE opts)
|
|
|
|
: RubyConfigDialogControl(opts)
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE val = rb_hash_aref(opts, STR2SYM("label"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(val) == T_STRING)
|
|
|
|
label = wxString(StringValueCStr(val), wxConvUTF8);
|
2007-01-29 18:52:46 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
val = rb_hash_aref(opts, STR2SYM("value"));
|
2007-02-03 21:31:20 +01:00
|
|
|
if(val == Qtrue) value = true;
|
|
|
|
else value = false;
|
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
virtual ~Checkbox() { }
|
|
|
|
|
|
|
|
wxControl *Create(wxWindow *parent)
|
|
|
|
{
|
2007-02-06 23:58:05 +01:00
|
|
|
cw = new wxCheckBox(parent, -1, label);
|
|
|
|
((wxCheckBox*)cw)->SetValue(value);
|
|
|
|
return cw;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlReadBack()
|
|
|
|
{
|
|
|
|
value = ((wxCheckBox*)cw)->GetValue();
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
if(value) return Qtrue;
|
|
|
|
return Qfalse;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// RubyConfigDialog
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
RubyConfigDialog::RubyConfigDialog(VALUE config, VALUE btn_data, bool include_buttons)
|
2007-01-29 18:52:46 +01:00
|
|
|
: use_buttons(include_buttons)
|
|
|
|
{
|
|
|
|
wxLogDebug(_T("creating RubyConfigDialog, this addr is %p"), this);
|
|
|
|
button_pushed = 0;
|
2007-02-03 21:31:20 +01:00
|
|
|
|
|
|
|
if(include_buttons && TYPE(btn_data) == T_ARRAY)
|
|
|
|
{
|
2007-02-06 23:58:05 +01:00
|
|
|
long len = RARRAY(btn_data)->len;
|
|
|
|
VALUE *ptr = RARRAY(btn_data)->ptr;
|
2007-02-03 21:31:20 +01:00
|
|
|
for(int i = 0; i < len; i++)
|
|
|
|
{
|
2007-02-06 23:58:05 +01:00
|
|
|
if(rb_respond_to(ptr[i], rb_intern("to_s")))
|
2007-02-03 21:31:20 +01:00
|
|
|
{
|
2007-02-06 23:58:05 +01:00
|
|
|
ptr[i] = rb_funcall(ptr[i], rb_intern("to_s"), 0);
|
2007-02-03 21:31:20 +01:00
|
|
|
wxString s(StringValueCStr(ptr[i]), wxConvUTF8);
|
2007-01-29 18:52:46 +01:00
|
|
|
buttons.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(config) != T_ARRAY) {
|
|
|
|
if(rb_respond_to(config, rb_intern("to_ary")))
|
|
|
|
config = rb_funcall(config, rb_intern("to_ary"), 0);
|
|
|
|
else throw "Cannot create config dialog from something non-table";
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
long len = RARRAY(config)->len;
|
|
|
|
VALUE *ptr = RARRAY(config)->ptr;
|
|
|
|
for(int i = 0; i < len; i++)
|
2007-02-06 23:58:05 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
if(TYPE(ptr[i]) != T_HASH)
|
|
|
|
continue; // skip invalid entry
|
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE ctrlclass = rb_hash_aref(ptr[i], STR2SYM("class"));
|
2007-02-03 21:31:20 +01:00
|
|
|
|
2007-02-07 19:52:23 +01:00
|
|
|
const char *cls_name;
|
|
|
|
if (TYPE(ctrlclass) == T_SYMBOL) {
|
|
|
|
cls_name = rb_id2name(SYM2ID(ctrlclass));
|
|
|
|
} else if (TYPE(ctrlclass) == T_STRING) {
|
|
|
|
cls_name = StringValueCStr(ctrlclass);
|
|
|
|
} else continue;
|
|
|
|
wxString controlclass(cls_name, wxConvUTF8);
|
2007-02-03 21:31:20 +01:00
|
|
|
|
|
|
|
RubyConfigDialogControl *ctl;
|
|
|
|
|
|
|
|
// Check control class and create relevant control
|
|
|
|
if (controlclass == _T("label")) {
|
|
|
|
ctl = new RubyControl::Label(ptr[i]);
|
|
|
|
} else if (controlclass == _T("edit")) {
|
|
|
|
ctl = new RubyControl::Edit(ptr[i]);
|
|
|
|
} else if (controlclass == _T("intedit")) {
|
|
|
|
ctl = new RubyControl::IntEdit(ptr[i]);
|
|
|
|
} else if (controlclass == _T("floatedit")) {
|
|
|
|
ctl = new RubyControl::FloatEdit(ptr[i]);
|
|
|
|
} else if (controlclass == _T("textbox")) {
|
|
|
|
ctl = new RubyControl::Textbox(ptr[i]);
|
|
|
|
} else if (controlclass == _T("dropdown")) {
|
|
|
|
ctl = new RubyControl::Dropdown(ptr[i]);
|
|
|
|
} else if (controlclass == _T("checkbox")) {
|
|
|
|
ctl = new RubyControl::Checkbox(ptr[i]);
|
|
|
|
} else if (controlclass == _T("color")) {
|
|
|
|
// FIXME
|
|
|
|
ctl = new RubyControl::Edit(ptr[i]);
|
|
|
|
} else if (controlclass == _T("coloralpha")) {
|
|
|
|
// FIXME
|
|
|
|
ctl = new RubyControl::Edit(ptr[i]);
|
|
|
|
} else if (controlclass == _T("alpha")) {
|
|
|
|
// FIXME
|
|
|
|
ctl = new RubyControl::Edit(ptr[i]);
|
|
|
|
} else continue; // skip
|
|
|
|
|
|
|
|
controls.push_back(ctl);
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
|
|
|
|
RubyConfigDialog::~RubyConfigDialog()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < controls.size(); ++i)
|
|
|
|
delete controls[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
wxWindow* RubyConfigDialog::CreateWindow(wxWindow *parent)
|
|
|
|
{
|
|
|
|
wxWindow *w = new wxPanel(parent);
|
|
|
|
wxGridBagSizer *s = new wxGridBagSizer(4, 4);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < controls.size(); ++i) {
|
|
|
|
RubyConfigDialogControl *c = controls[i];
|
|
|
|
c->Create(w);
|
|
|
|
if (dynamic_cast<RubyControl::Label*>(c)) {
|
|
|
|
s->Add(c->cw, wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT);
|
|
|
|
} else {
|
|
|
|
s->Add(c->cw, wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), wxEXPAND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_buttons) {
|
|
|
|
wxStdDialogButtonSizer *bs = new wxStdDialogButtonSizer();
|
|
|
|
if (buttons.size() > 0) {
|
|
|
|
wxLogDebug(_T("creating user buttons"));
|
|
|
|
for (size_t i = 0; i < buttons.size(); ++i) {
|
|
|
|
wxLogDebug(_T("button '%s' gets id %d"), buttons[i].c_str(), 1001+(wxWindowID)i);
|
|
|
|
bs->Add(new wxButton(w, 1001+(wxWindowID)i, buttons[i]));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wxLogDebug(_T("creating default buttons"));
|
|
|
|
bs->Add(new wxButton(w, wxID_OK));
|
|
|
|
bs->Add(new wxButton(w, wxID_CANCEL));
|
|
|
|
}
|
|
|
|
bs->Realize();
|
|
|
|
|
|
|
|
button_event = new ButtonEventHandler();
|
|
|
|
button_event->button_pushed = &button_pushed;
|
|
|
|
// passing button_event as userdata because wx will then delete it
|
|
|
|
w->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RubyConfigDialog::ButtonEventHandler::OnButtonPush), button_event, button_event);
|
|
|
|
wxLogDebug(_T("set event handler, this addr is %p"), this);
|
|
|
|
|
|
|
|
wxBoxSizer *ms = new wxBoxSizer(wxVERTICAL);
|
|
|
|
ms->Add(s, 0, wxBOTTOM, 5);
|
|
|
|
ms->Add(bs);
|
|
|
|
w->SetSizerAndFit(ms);
|
|
|
|
} else {
|
|
|
|
w->SetSizerAndFit(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE RubyConfigDialog::RubyReadBack()
|
2007-01-29 18:52:46 +01:00
|
|
|
{
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE cfg = rb_hash_new();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < controls.size(); ++i) {
|
2007-02-07 19:52:23 +01:00
|
|
|
VALUE v = controls[i]->RubyReadBack();
|
|
|
|
if(v != Qnil)
|
|
|
|
rb_hash_aset(cfg, controls[i]->name_sym, v);
|
2007-02-03 21:31:20 +01:00
|
|
|
}
|
2007-01-29 18:52:46 +01:00
|
|
|
if (use_buttons) {
|
2007-02-03 21:31:20 +01:00
|
|
|
VALUE res = rb_ary_new();
|
|
|
|
|
2007-01-29 18:52:46 +01:00
|
|
|
wxLogDebug(_T("reading back button_pushed"));
|
|
|
|
int btn = button_pushed;
|
|
|
|
if (btn == 0) {
|
|
|
|
wxLogDebug(_T("was zero, cancelled"));
|
|
|
|
// Always cancel/closed
|
2007-02-03 21:31:20 +01:00
|
|
|
rb_ary_push(res, Qfalse);
|
2007-01-29 18:52:46 +01:00
|
|
|
} else {
|
|
|
|
wxLogDebug(_T("nonzero, something else: %d"), btn);
|
|
|
|
if (buttons.size() > 0) {
|
|
|
|
wxLogDebug(_T("user button: %s"), buttons[btn-1].c_str());
|
|
|
|
// button_pushed is index+1 to reserve 0 for Cancel
|
2007-02-03 21:31:20 +01:00
|
|
|
rb_ary_push(res, rb_str_new2(buttons[btn-1].mb_str(wxConvUTF8)));
|
2007-01-29 18:52:46 +01:00
|
|
|
} else {
|
|
|
|
wxLogDebug(_T("default button, must be Ok"));
|
|
|
|
// Cancel case already covered, must be Ok then
|
2007-02-03 21:31:20 +01:00
|
|
|
rb_ary_push(res, Qtrue);
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
2007-02-03 21:31:20 +01:00
|
|
|
rb_ary_push(res, cfg); // return array [button, hash with config]
|
|
|
|
return res;
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
2007-02-03 21:31:20 +01:00
|
|
|
return cfg; // if no buttons return only hash with config
|
2007-01-29 18:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RubyConfigDialog::ReadBack()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < controls.size(); ++i) {
|
|
|
|
controls[i]->ControlReadBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RubyConfigDialog::ButtonEventHandler::OnButtonPush(wxCommandEvent &evt)
|
|
|
|
{
|
|
|
|
// Let button_pushed == 0 mean "cancelled", such that pushing Cancel or closing the dialog
|
|
|
|
// will both result in button_pushed == 0
|
|
|
|
if (evt.GetId() == wxID_OK) {
|
|
|
|
wxLogDebug(_T("was wxID_OK"));
|
|
|
|
*button_pushed = 1;
|
|
|
|
} else if (evt.GetId() == wxID_CANCEL) {
|
|
|
|
wxLogDebug(_T("was wxID_CANCEL"));
|
|
|
|
*button_pushed = 0;
|
|
|
|
} else {
|
|
|
|
wxLogDebug(_T("was user button"));
|
|
|
|
// Therefore, when buttons are numbered from 1001 to 1000+n, make sure to set it to i+1
|
|
|
|
*button_pushed = evt.GetId() - 1000;
|
|
|
|
evt.SetId(wxID_OK); // hack to make sure the dialog will be closed
|
|
|
|
}
|
|
|
|
wxLogDebug(_T("button_pushed set to %d"), *button_pushed);
|
|
|
|
evt.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2007-12-31 07:46:22 +01:00
|
|
|
|
|
|
|
#endif // WITH_RUBY
|