File: gtk_form_dsl_select.rb

package info (click to toggle)
mikutter 3.8.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,544 kB
  • sloc: ruby: 20,548; sh: 99; makefile: 19
file content (115 lines) | stat: -rw-r--r-- 3,721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
miquire :mui, 'form_dsl'

class Gtk::FormDSL::Select
  def initialize(parent_dslobj, values = [])
    @parent_dslobj = parent_dslobj
    @options = values.to_a.freeze end

  # セレクトボックスに要素を追加する
  # ==== Args
  # [value] 選択されたらセットされる値
  # [label] ラベル。 _&block_ がなければ使われる。文字列。
  # [&block] Plugin::Settings のインスタンス内で評価され、そのインスタンスが内容として使われる
  def option(value, label = nil)
    if block_given?
      widget = @parent_dslobj.create_inner_setting.set_border_width(4)
      widget.instance_eval(&Proc.new)
      @options += [[value, label, widget].freeze]
    else
      @options += [[value, label].freeze]
    end
    @options.freeze
    self
  end

  # 項目として、ウィジェットを持っているかを返す。
  # ==== Return
  # ウィジェットを持っているなら真
  def has_widget?
    not @options.all?{ |option| option.last.is_a? String }
  end

  # optionメソッドで追加された項目をウィジェットに組み立てる
  # ==== Args
  # [label] ラベル。文字列。
  # [config_key] 設定のキー
  # ==== Return
  # ウィジェット
  def build(label, config_key)
    if has_widget?
      group = Gtk::Frame.new.set_border_width(8)
      group.set_label(label)
      group.add(build_box(config_key))
      group
    else
      Gtk::HBox.new(false, 0).add(Gtk::Label.new(label).left).closeup(build_combobox(config_key))
    end
  end

  def method_missing(*args, &block)
    @parent_dslobj.method_missing(*args, &block)
  end

  private

  def build_box(config_key)
    box = Gtk::VBox.new
    group = Gtk::RadioButton.new

    options = @options
    options.each{ |value, face, setting|
      radio = nil
      if (not setting) and face.is_a? String
        box.closeup radio = Gtk::RadioButton.new(group, face)
      elsif setting.is_a? Gtk::FormDSL
        if face.is_a? String
          container = Gtk::Table.new(2, 2)
          radio = Gtk::RadioButton.new(group)
          container.attach(radio, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL)
          container.attach(Gtk::Label.new(face).left, 1, 2, 0, 1, Gtk::SHRINK|Gtk::FILL, Gtk::FILL)
          container.attach(setting, 1, 2, 1, 2, Gtk::FILL|Gtk::SHRINK|Gtk::EXPAND, Gtk::FILL|Gtk::SHRINK|Gtk::EXPAND)
          box.closeup container
        else
          container = Gtk::HBox.new
          radio = Gtk::RadioButton.new(group)
          box.closeup container.closeup(radio).add(setting)
        end
      end
      if radio
        radio.ssc(:toggled, &generate_toggled_listener(config_key, value, setting))
        radio.active = @parent_dslobj[config_key] == value
        setting.sensitive = radio.active? if setting.is_a? Gtk::Widget
      end
    }
    box
  end

  # すべてテキストなら、コンボボックスで要素を描画する
  def build_combobox(config_key)
    input = Gtk::ComboBox.new(true)
    @options.each{ |t|
      input.append_text(t.last) }
    input.active = (@options.index{ |i| i.first.to_s == @parent_dslobj[config_key].to_s } || 0)
    @parent_dslobj[config_key] = @options[input.active].first
    input.ssc(:changed){ |widget|
      @parent_dslobj[config_key] = @options[widget.active].first
      false }
    input
  end

  def generate_toggled_listener(config_key, value, setting=nil)
    if setting.is_a? Gtk::Widget
      ->(widget) do
        @parent_dslobj[config_key] = value if widget.active?
        setting.sensitive = widget.active?
        false
      end
    else
      ->(widget) do
        @parent_dslobj[config_key] = value if widget.active?
        false
      end
    end
  end
end