|
| 1 | +#include "widget.h" |
| 2 | +#include "check_box.h" |
| 3 | +#include "exception_guard.h" |
| 4 | + |
| 5 | +static void |
| 6 | +dealloc(YCheckBox *box) |
| 7 | +{ |
| 8 | + widget_object_map_remove(box); |
| 9 | +} |
| 10 | + |
| 11 | +VALUE |
| 12 | +ui_wrap_check_box(YCheckBox *box) |
| 13 | +{ |
| 14 | + return Data_Wrap_Struct(cUICheckBox, ui_widget_mark, dealloc, box); |
| 15 | +} |
| 16 | + |
| 17 | +static YCheckBox * |
| 18 | +ui_unwrap_check_box(VALUE box) |
| 19 | +{ |
| 20 | + YCheckBox *ptr = 0L; |
| 21 | + Data_Get_Struct(box, YCheckBox, ptr); |
| 22 | + return ptr; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @return [Boolean] Current on/off value: |
| 27 | + * +true+ if checked, +false+ if unchecked. |
| 28 | + * +nil+ if don't care (tri-state) |
| 29 | + */ |
| 30 | +static VALUE |
| 31 | +get_value(VALUE self) |
| 32 | +{ |
| 33 | + YEXCEPTION_TRY |
| 34 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 35 | + switch(ptr->value()) |
| 36 | + { |
| 37 | + case YCheckBox_on: |
| 38 | + return Qtrue; |
| 39 | + case YCheckBox_off: |
| 40 | + return Qfalse; |
| 41 | + default: |
| 42 | + return Qnil; |
| 43 | + } |
| 44 | + YEXCEPTION_CATCH |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Get the current value: |
| 49 | + * @return [Boolean] true if CheckBox is checked |
| 50 | + * @return [Boolean] false if CheckBox is unchecked |
| 51 | + * @return [nil] tri-state: CheckBox is greyed out, |
| 52 | + * neither checked nor unchecked |
| 53 | + * |
| 54 | + * The user cannot set the 3rd state directly. This status is always |
| 55 | + * only set from the outside, usually because a setting cannot be clearly |
| 56 | + * determined. For example, a checkbox |
| 57 | + * |
| 58 | + * [ ] Read only |
| 59 | + * |
| 60 | + * would be set to "don't care" (by the application, not directly by the |
| 61 | + * user) when it is to display the read-only state of a group of files |
| 62 | + * where some are read-only and some are writeable. |
| 63 | + * |
| 64 | + */ |
| 65 | +static VALUE |
| 66 | +set_value(VALUE self, VALUE val) |
| 67 | +{ |
| 68 | + YEXCEPTION_TRY |
| 69 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 70 | + if (NIL_P(val)) { |
| 71 | + ptr->setValue(YCheckBox_dont_care); |
| 72 | + } |
| 73 | + else { |
| 74 | + ptr->setValue(RTEST(val) ? YCheckBox_on : YCheckBox_off); |
| 75 | + } |
| 76 | + YEXCEPTION_CATCH |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @return [String] the label (the text on the CheckBox) |
| 81 | + */ |
| 82 | +static VALUE |
| 83 | +get_label(VALUE self) |
| 84 | +{ |
| 85 | + YEXCEPTION_TRY |
| 86 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 87 | + return rb_str_new2(ptr->label().c_str()); |
| 88 | + YEXCEPTION_CATCH |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Set the label (the text on the CheckBox). |
| 93 | + */ |
| 94 | +static VALUE |
| 95 | +set_label(VALUE self, VALUE label) |
| 96 | +{ |
| 97 | + YEXCEPTION_TRY |
| 98 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 99 | + ptr->setLabel(StringValueCStr(label)); |
| 100 | + YEXCEPTION_CATCH |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @return [Boolean] true if a bold font should be used. |
| 105 | + */ |
| 106 | +static VALUE |
| 107 | +get_use_bold_font(VALUE self) |
| 108 | +{ |
| 109 | + YEXCEPTION_TRY |
| 110 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 111 | + return ptr->useBoldFont() ? Qtrue : Qfalse; |
| 112 | + YEXCEPTION_CATCH |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Indicate whether or not a bold font should be used. |
| 117 | + */ |
| 118 | +static VALUE |
| 119 | +set_use_bold_font(VALUE self, VALUE use) |
| 120 | +{ |
| 121 | + YEXCEPTION_TRY |
| 122 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 123 | + ptr->setUseBoldFont(RTEST(use)); |
| 124 | + YEXCEPTION_CATCH |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Simplified access to value() |
| 129 | + * @return [Boolean] true if the CheckBox is checked. |
| 130 | + */ |
| 131 | +static VALUE |
| 132 | +get_checked(VALUE self) |
| 133 | +{ |
| 134 | + YEXCEPTION_TRY |
| 135 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 136 | + return ptr->isChecked() ? Qtrue : Qfalse; |
| 137 | + YEXCEPTION_CATCH |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Simplified access to setValue(): Check of uncheck the CheckBox. |
| 142 | + */ |
| 143 | +static VALUE |
| 144 | +set_checked(VALUE self, VALUE checked) |
| 145 | +{ |
| 146 | + YEXCEPTION_TRY |
| 147 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 148 | + ptr->setChecked(RTEST(checked)); |
| 149 | + YEXCEPTION_CATCH |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +/** |
| 154 | + * Simplified access to tri-state ("don't care"). |
| 155 | + * @return [Boolean] true if the CheckBox is "don't care". |
| 156 | + */ |
| 157 | +static VALUE |
| 158 | +get_dont_care(VALUE self) |
| 159 | +{ |
| 160 | + YEXCEPTION_TRY |
| 161 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 162 | + return ptr->dontCare() ? Qtrue : Qfalse; |
| 163 | + YEXCEPTION_CATCH |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Simplified access to setting tri-state ("don't care"). |
| 168 | + */ |
| 169 | +static VALUE |
| 170 | +set_dont_care(VALUE self) |
| 171 | +{ |
| 172 | + YEXCEPTION_TRY |
| 173 | + YCheckBox *ptr = ui_unwrap_check_box(self); |
| 174 | + ptr->setDontCare(); |
| 175 | + YEXCEPTION_CATCH |
| 176 | +} |
| 177 | + |
| 178 | +VALUE cUICheckBox; |
| 179 | +void init_ui_check_box() |
| 180 | +{ |
| 181 | + VALUE ui = rb_define_module("UI"); |
| 182 | + |
| 183 | + VALUE klass = rb_define_class_under(ui, "CheckBox", cUIWidget); |
| 184 | + rb_define_method(klass, "value", RUBY_METHOD_FUNC(get_value), 0); |
| 185 | + rb_define_method(klass, "value=", RUBY_METHOD_FUNC(set_value), 1); |
| 186 | + rb_define_method(klass, "label", RUBY_METHOD_FUNC(get_label), 0); |
| 187 | + rb_define_method(klass, "label=", RUBY_METHOD_FUNC(set_label), 1); |
| 188 | + rb_define_method(klass, "use_bold_font?", RUBY_METHOD_FUNC(get_use_bold_font), 0); |
| 189 | + rb_define_method(klass, "use_bold_font=", RUBY_METHOD_FUNC(set_use_bold_font), 1); |
| 190 | + rb_define_method(klass, "checked?", RUBY_METHOD_FUNC(get_checked), 0); |
| 191 | + rb_define_method(klass, "checked=", RUBY_METHOD_FUNC(set_checked), 1); |
| 192 | + rb_define_method(klass, "dont_care?", RUBY_METHOD_FUNC(get_dont_care), 0); |
| 193 | + rb_define_method(klass, "dont_care!", RUBY_METHOD_FUNC(set_dont_care), 0); |
| 194 | + cUICheckBox = klass; |
| 195 | + |
| 196 | +} |
| 197 | + |
0 commit comments