File: gtk_hierarchycal_selectbox.rb

package info (click to toggle)
mikutter 5.0.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,700 kB
  • sloc: ruby: 21,307; sh: 181; makefile: 19
file content (42 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

require 'gtk3'
require 'mui/gtk_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 |id, name|
      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