Module: DemoTree

Defined in:
sample/tkextlib/bwidget/tree.rb

Constant Summary collapse

@@count =
0
@@dblclick =
false
@@top =
nil

Class Method Summary collapse

Class Method Details

.create(nb) ⇒ Object



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
116
117
118
119
120
121
122
123
124
# File 'sample/tkextlib/bwidget/tree.rb', line 13

def self.create(nb)
  frame = nb.insert('end', 'demoTree', :text=>'Tree')
  pw = Tk::BWidget::PanedWindow.new(frame, :side=>:top)

  pane = pw.add(:weight=>1)
  title = Tk::BWidget::TitleFrame.new(pane, :text=>'Directory tree')
  sw = Tk::BWidget::ScrolledWindow.new(title.get_frame,
                                       :relief=>:sunken, :borderwidth=>2)
  tree = Tk::BWidget::Tree.new(sw, :relief=>:flat, :borderwidth=>0,
                               :width=>15, :highlightthickness=>0,
                               :redraw=>false, :dropenabled=>true,
                               :dragenabled=>true, :dragevent=>3,
                               :droptypes=>[
                                 'TREE_NODE', [
                                   :copy, [],
                                   :move, [],
                                   :link, []
                                 ],
                                 'LISTBOX_ITEM', [
                                   :copy, [],
                                   :move, [],
                                   :link, []
                                 ]
                               ],
                               :opencmd=>proc{|node|
                                 moddir(1, tree, node)
                               },
                               :closecmd=>proc{|node|
                                 moddir(0, tree, node)
                               })
  sw.set_widget(tree)

  sw.pack(:side=>:top, :expand=>true, :fill=>:both)
  title.pack(:fill=>:both, :expand=>true)

  pane = pw.add(:weight=>2)
  lf = Tk::BWidget::TitleFrame.new(pane, :text=>'Content')
  sw = Tk::BWidget::ScrolledWindow.new(lf.get_frame,
                                       :scrollbar=>:horizontal,
                                       :auto=>:none, :relief=>:sunken,
                                       :borderwidth=>2)

  list = Tk::BWidget::ListBox.new(sw, :relief=>:flat, :borderwidth=>0,
                                  :highlightthickness=>0, :width=>20,
                                  :multicolumn=>true, :redraw=>false,
                                  :dragevent=>3, :dropenabled=>true,
                                  :dragenabled=>true,
                               :droptypes=>[
                                    'TREE_NODE', [
                                      :copy, [],
                                      :move, [],
                                      :link, []
                                    ],
                                    'LISTBOX_ITEM', [
                                      :copy, [],
                                      :move, [],
                                      :link, []
                                    ]
                                  ])
  sw.set_widget(list)

  Tk.pack(sw, lf, :fill=>:both, :expand=>true)

  pw.pack(:fill=>:both, :expand=>true)

  tree.textbind('ButtonPress-1',
                proc{|node, ev| select('tree', 1, tree, list, node)})
  tree.textbind('Double-ButtonPress-1',
                proc{|node, ev| select('tree', 2, tree, list, node)})

  list.textbind('ButtonPress-1',
                proc{|node, ev| select('list', 1, tree, list, node)})
  list.textbind('Double-ButtonPress-1',
                proc{|node, ev| select('list', 2, tree, list, node)})

  list.imagebind('Double-ButtonPress-1',
                 proc{|node, ev| select('list', 2, tree, list, node)})

  nb.itemconfigure('demoTree',
                   :createcmd=>proc{|*args| init(tree, list, *args)},
                   :raisecmd=>proc{
                     Tk.root.geometry =~
                       /\d+x\d+([+-]{1,2}\d+)([+-]{1,2}\d+)/
                     global_w = ($1 || 0).to_i
                     global_h = ($2 || 0).to_i
                     if @@top
                       Tk::BWidget.place(@@top, 0, 0, :at,
                          global_w - Tk.root.winfo_screenwidth, global_h)
                       @@top.deiconify
                       Tk.root.bind('Unmap', proc{@@top.withdraw})
                       Tk.root.bind('Map',   proc{@@top.deiconify})
                       Tk.root.bind('Configure', proc{|w|
                              if w == Tk.root
                                Tk.root.geometry =~
                                  /\d+x\d+([+-]{1,2}\d+)([+-]{1,2}\d+)/
                                global_w = ($1 || 0).to_i
                                global_h = ($2 || 0).to_i
                                Tk::BWidget.place(@@top, 0, 0, :at,
                                     global_w - Tk.root.winfo_screenwidth,
                                     global_h)
                              end
                       }, '%W')
                     end
                   },
                   :leavecmd=>proc{
                     @@top.withdraw if @@top
                     Tk.root.bind_remove('Unmap')
                     Tk.root.bind_remove('Map')
                     Tk.root.bind_remove('Configure')
                     true
                   })
end

.edit(where, tree, list, node) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'sample/tkextlib/bwidget/tree.rb', line 244

def self.edit(where, tree, list, node)
  return if @@dblclick

  if (where == 'tree' &&
      tree.selection_get.find{|x|
        TkUtil._get_eval_string(x) == TkUtil._get_eval_string(node)
      })
    res = tree.edit(node, tree.itemcget(node, :text))
    if res != ''
      tree.itemconfigure(node, :text=>res)
      if list.exist?(node)
        list.itemconfigure(node, :text=>res)
      end
      tree.selection_set(node)
    end
    return
  end

  if (where == 'list')
    res = list.edit(node, list.igemcget(node, :text))
    if res != ''
      list.itemconfigure(node, :text=>res)
      if tree.exist?(node)
        tree.itemconfigure(node, :text=>res)
      else
        cursel = tree.selection_get[0]
        index  = list.index(node) - tree.nodes(cursel).size
        data   = TkComm.simplelist(tree.itemcget(cursel, :data))
        data[index] = res
        tree.itemconfigure(cursel, :date=>data)
      end
      list.selection_set(node)
    end
  end
end

.expand(tree, but) ⇒ Object



280
281
282
283
284
285
286
287
288
# File 'sample/tkextlib/bwidget/tree.rb', line 280

def self.expand(tree, but)
  unless (cur = tree.selection_get).empty?
    if TkComm.bool(but)
      tree.opentree(cur)
    else
      tree.closetree(cur)
    end
  end
end

.getdir(tree, node, path) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'sample/tkextlib/bwidget/tree.rb', line 155

def self.getdir(tree, node, path)
  lentries = Dir.glob(File.join(path, '*')).sort
  lfiles = []
  lentries.each{|f|
    basename = File.basename(f)
    if File.directory?(f)
      Tk::BWidget::Tree::Node.new(tree, node,
                                  :index=>'end', :text=>basename,
                                  :image=>Tk::BWidget::Bitmap.new('folder'),
                                  :drawcross=>:allways, :data=>f)
      @@count += 1
    else
      lfiles << basename
    end
  }
  tree.itemconfigure(node, :drawcross=>:auto, :data=>lfiles)
end

.init(tree, list, *args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'sample/tkextlib/bwidget/tree.rb', line 126

def self.init(tree, list, *args)
  @@count = 0
  if Tk::PLATFORM['platform'] == 'unix'
    rootdir = File.expand_path('~')
  else
    rootdir = 'c:'
  end

  tree.insert('end', 'root', 'home',
              :text=>rootdir, :data=>rootdir, :open=>true,
              :image=>Tk::BWidget::Bitmap.new('openfold'))
  getdir(tree, 'home', rootdir)
  select('tree', 1, tree, list, 'home')
  tree.redraw(true)
  list.redraw(true)

  @@top = TkToplevel.new
  @@top.withdraw
  @@top.protocol('WM_DELETE_WINDOW'){
    # don't kill me
  }
  @@top.resizable(false, false)
  @@top.title('Drag rectangle to scroll directory tree')
  @@top.transient(Tk.root)
  Tk::BWidget::ScrollView.new(@@top, :window=>tree, :fill=>'white',
                              :width=>300, :height=>300, :relief=>:sunken,
                              :bd=>1).pack(:fill=>:both, :expand=>true)
end

.moddir(idx, tree, node) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'sample/tkextlib/bwidget/tree.rb', line 173

def self.moddir(idx, tree, node)
  if (idx != 0 && tree.itemcget(node, :drawcross).to_s == 'allways')
    getdir(tree, node, tree.itemcget(node, :data))
    if tree.nodes(node).empty?
      tree.itemconfigure(node, :image=>Tk::BWidget::Bitmap.new('folder'))
    else
      tree.itemconfigure(node, :image=>Tk::BWidget::Bitmap.new('openfold'))
    end
  else
    img = %w(folder openfold)[idx] || 'openfold'
    tree.itemconfigure(node, :image=>Tk::BWidget::Bitmap.new(img))
  end
end

.select(where, num, tree, list, node) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'sample/tkextlib/bwidget/tree.rb', line 187

def self.select(where, num, tree, list, node)
  @@dblclick = true
  if num == 1
    if (where == 'tree' &&
        tree.selection_get.find{|x|
          TkUtil._get_eval_string(x) == TkUtil._get_eval_string(node)
        })
      @@dblclick = false
      Tk.after(500, proc{edit('tree', tree, list, node)})
      return
    end
    if (where == 'list' &&
        list.selection_get.find{|x|
          TkUtil._get_eval_string(x) == TkUtil._get_eval_string(node)
        })
      @@dblclick = false
      Tk.after(500, proc{edit('list', tree, list, node)})
      return
    end
    if where == 'tree'
      select_node(tree, list, node)
    else
      list.selection_set(node)
    end
  elsif (where == 'list' && tree.exist?(node))
    parent = tree.parent(node)
    while TkUtil._get_eval_string(parent) != 'root'
      tree.itemconfigure(parent, :open=>true)
      parent = tree.parent(parent)
    end
    select_node(tree, list, node)
  end
end

.select_node(tree, list, node) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'sample/tkextlib/bwidget/tree.rb', line 221

def self.select_node(tree, list, node)
  tree.selection_set(node)
  Tk.update
  list.delete(*(list.items(0, 'end')))

  dir = tree.itemcget(node, :data)
  if tree.itemcget(node, :drawcross).to_s == 'allways'
    getdir(tree, node, dir)
    dir = tree.itemcget(node, :data)
  end

  tree.nodes(node).each{|subnode|
    list.insert('end', subnode,
                :text=>tree.itemcget(subnode, :text),
                :image=>Tk::BWidget::Bitmap.new('folder'))
  }

  TkComm.simplelist(dir).each{|f|
    Tk::BWidget::ListBox::Item.new(list, 'end', :text=>f,
                                   :image=>Tk::BWidget::Bitmap.new('file'))
  }
end