File: gtk_hierarchycal_selectbox.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 (43 lines) | stat: -rw-r--r-- 1,314 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-

require 'gtk2'
miquire :mui, 'selectbox'

=begin rdoc
  複数選択ウィジェットを作成する。
  これで作成されたウィジェットは、チェックボックスで複数の項目が選択できる。
  各項目は文字列でしか指定できない。
  項目名に/が入っていると階層化される
=end

class Gtk::HierarchycalSelectBox < Gtk::SelectBox

  private
  def initialize_model
    set_model(Gtk::TreeStore.new(*column_schemer.flatten.map{|x| x[:type]}))
  end

  def setting_values(values, selected)
    root_nodes = []
    values.each do |pair|
      id, name = *pair
      fullpath = []
      last_node = name.inject(nil) do |parent_node, hierarchy|
        fullpath << hierarchy
        if parent_node
          node = parent_node.n_children.times.lazy.map(&parent_node.method(:nth_child)).find{|_| _[ITER_STRING] == hierarchy } || model.append(parent_node)
        else
          node = root_nodes.find{|_| _[ITER_STRING] == hierarchy }
          if node.nil?
            node = model.append(nil)
            root_nodes << node end end
        node[ITER_ID] = @none
        node[ITER_STRING] = hierarchy
        node
      end
      last_node[ITER_ID] = id
      last_node[ITER_CHECK] = (selected and @selected.include?(id))
    end
  end

end