1#
2# Demo: Help contents
3#
4def demoHelpContents(t)
5  height = t.font.metrics(:linespace)
6  height = 18 if height < 18
7  t.configure(:showroot=>false, :showbuttons=>false, :showlines=>false,
8              :itemheight=>height, :selectmode=>:browse)
9
10  init_pics('help-*')
11
12  if $Version_1_1_OrLater
13    t.column_create(:text=>'Help Contents')
14  else # TreeCtrl 1.0
15    t.column_configure(0, :text=>'Help Contents')
16  end
17
18  # Define a new item state
19  t.state_define('mouseover')
20
21  t.element_create('e1', :image, :image=>@images['help-page'])
22  t.element_create('e2', :image, :image=>[
23                     @images['help-book-open'], ['open'],
24                     @images['help-book-closed'], [],
25                   ])
26  t.element_create('e3', :text,
27                   :font=>[t.font.dup.underline(true), ['mouseover']],
28                   :fill=>[
29                     @SystemHighlightText, ['selected', 'focus'],
30                     'blue', ['mouseover']
31                   ])
32  t.element_create('e4', :rect, :showfocus=>true,
33                   :fill=>[@SystemHighlight, ['selected', 'focus']])
34
35  # book
36  s = t.style_create('s1')
37  t.style_elements(s, ['e4', 'e1', 'e3'])
38  t.style_layout(s, 'e1', :padx=>[0,4], :expand=>:ns)
39  t.style_layout(s, 'e3', :expand=>:ns)
40  t.style_layout(s, 'e4', :union=>['e3'], :iexpand=>:ns, :ipadx=>2)
41
42  # page
43  s = t.style_create('s2')
44  t.style_elements(s, ['e4', 'e2', 'e3'])
45  t.style_layout(s, 'e2', :padx=>[0,4], :expand=>:ns)
46  t.style_layout(s, 'e3', :expand=>:ns)
47  t.style_layout(s, 'e4', :union=>['e3'], :iexpand=>:ns, :ipadx=>2)
48
49  parentList = [:root, '', '', '', '', '', '']
50  parent = :root
51  [
52     [0, 's1', "Welcome to Help"],
53     [0, 's2', "Introducing Windows 98"],
54        [1, 's2', "How to Use Help"],
55           [2, 's1', "Find a topic"],
56           [2, 's1', "Get more out of help"],
57        [1, 's2', "Register Your Software"],
58           [2, 's1', "Registering Windows 98 online"],
59        [1, 's2', "What's New in Windows 98"],
60           [2, 's1', "Innovative, easy-to-use features"],
61           [2, 's1', "Improved reliability"],
62           [2, 's1', "A faster operating system"],
63           [2, 's1', "True Web integration"],
64           [2, 's1', "More entertaining and fun"],
65        [1, 's2', "If You're New to Windows 98"],
66           [2, 's2', "Tips for Macintosh Users"],
67              [3, 's1', "Why does the mouse have two buttons?"]
68  ].each{|depth, style, text|
69    item = t.item_create
70    t.item_style_set(item, 0, style)
71    t.item_element_configure(item, 0, 'e3', :text=>text)
72    if $Version_1_1_OrLater
73      t.item_collapse(item)
74    else # TreeCtrl 1.0
75      t.collapse(item)
76    end
77    t.item_lastchild(parentList[depth], item)
78    depth += 1
79    parentList[depth] = item
80  }
81
82  treeCtrlHelp = TkBindTag.new
83
84  treeCtrlHelp.bind('Double-ButtonPress-1',
85                    proc{|w, x, y|
86                      if w.identify(x, y)[0] == 'header'
87                        Tk::TreeCtrl::BindCallback.doubleButton1(w, x, y)
88                      else
89                        helpButton1(w, x, y)
90                      end
91                      Tk.callback_break
92                    }, '%W %x %y')
93
94  treeCtrlHelp.bind('ButtonPress-1',
95                    proc{|w, x, y|
96                      helpButton1(w, x, y)
97                      Tk.callback_break
98                    }, '%W %x %y')
99
100  treeCtrlHelp.bind('Button1-Motion',
101                    proc{|w, x, y|
102                      helpMotion1(w, x, y)
103                      Tk.callback_break
104                    }, '%W %x %y')
105
106  treeCtrlHelp.bind('Button1-Leave',
107                    proc{|w, x, y|
108                      helpLeave1(w, x, y)
109                      Tk.callback_break
110                    }, '%W %x %y')
111
112  treeCtrlHelp.bind('ButtonRelease-1',
113                    proc{|w, x, y|
114                      helpRelease1(w, x, y)
115                      Tk.callback_break
116                    }, '%W %x %y')
117
118  treeCtrlHelp.bind('Motion', proc{|w, x, y| helpMotion(w, x, y) }, '%W %x %y')
119
120  treeCtrlHelp.bind('Leave', proc{|w, x, y| helpMotion(w, x, y) }, '%W %x %y')
121
122  treeCtrlHelp.bind('KeyPress-Return',
123                    proc{|w, x, y|
124                      if w.selection_get.length == 1
125                        if $Version_1_1_OrLater
126                          w.item_toggle(w.selection_get[0])
127                        else # TreeCtrl 1.0
128                          w.toggle(w.selection_get[0])
129                        end
130                      end
131                      Tk.callback_break
132                    }, '%W %x %y')
133
134  @Priv[:help, :prev] = ''
135
136  t.bindtags = [ t, treeCtrlHelp, Tk::TreeCtrl, t.winfo_toplevel, :all ]
137end
138
139# This is an alternate implementation that does not define a new item state
140# to change the appearance of the item under the cursor.
141def demoHelpContents2(t)
142  height = t.font.metrics(:linespace)
143  height = 18 if height < 18
144  t.configure(:showroot=>false, :showbuttons=>false, :showlines=>false,
145              :itemheight=>height, :selectmode=>:browse)
146
147  init_pics('help-*')
148
149  if $Version_1_1_OrLater
150    t.column_create(:text=>'Help Contents')
151  else # TreeCtrl 1.0
152    t.column_configure(0, :text=>'Help Contents')
153  end
154
155  t.element_create('e1', :image, :image=>@images['help-page'])
156  t.element_create('e2', :image, :image=>[
157                     @images['help-book-open'], ['open'],
158                     @images['help-book-closed'], [],
159                   ])
160  t.element_create('e3', :text,
161                   :fill=>[
162                     @SystemHighlightText, ['selected', 'focus'],
163                     'blue', []
164                   ])
165  t.element_create('e4', :rect, :showfocus=>true,
166                   :fill=>[@SystemHighligh, ['selected', 'focus']])
167  t.element_create('e5', :text, :font=>t.font.dup.underline(true),
168                   :fill=>[
169                     @SystemHighlightText, ['selected', 'focus'],
170                     'blue', []
171                   ])
172
173  # book
174  s = t.style_create('s1')
175  t.style_elements(s, ['e4', 'e1', 'e3'])
176  t.style_layout(s, 'e1', :padx=>[0,4], :expand=>:ns)
177  t.style_layout(s, 'e3', :expand=>:ns)
178  t.style_layout(s, 'e4', :union=>['e3'], :iexpand=>:ns, :ipadx=>2)
179
180  # page
181  s = t.style_create('s2')
182  t.style_elements(s, ['e4', 'e2', 'e3'])
183  t.style_layout(s, 'e2', :padx=>[0,4], :expand=>:ns)
184  t.style_layout(s, 'e3', :expand=>:ns)
185  t.style_layout(s, 'e4', :union=>['e3'], :iexpand=>:ns, :ipadx=>2)
186
187  # book (focus)
188  s = t.style_create('s1.f')
189  t.style_elements(s, ['e4', 'e1', 'e5'])
190  t.style_layout(s, 'e1', :padx=>[0,4], :expand=>:ns)
191  t.style_layout(s, 'e5', :expand=>:ns)
192  t.style_layout(s, 'e4', :union=>['e5'], :iexpand=>:ns, :ipadx=>2)
193
194  # page (focus)
195  s = t.style_create('s2')
196  t.style_elements(s, ['e4', 'e2', 'e5'])
197  t.style_layout(s, 'e2', :padx=>[0,4], :expand=>:ns)
198  t.style_layout(s, 'e5', :expand=>:ns)
199  t.style_layout(s, 'e4', :union=>['e5'], :iexpand=>:ns, :ipadx=>2)
200
201  parentList = [:root, '', '', '', '', '', '']
202  parent = :root
203  [
204     [0, 's1', "Welcome to Help"],
205     [0, 's2', "Introducing Windows 98"],
206        [1, 's2', "How to Use Help"],
207           [2, 's1' "Find a topic"],
208           [2, 's1', "Get more out of help"],
209        [1, 's2', "Register Your Software"],
210           [2, 's1', "Registering Windows 98 online"],
211        [1, 's2', "What's New in Windows 98"],
212           [2, 's1', "Innovative, easy-to-use features"],
213           [2, 's1', "Improved reliability"],
214           [2, 's1', "A faster operating system"],
215           [2, 's1', "True Web integration"],
216           [2, 's1', "More entertaining and fun"],
217        [1, 's2', "If You're New to Windows 98"],
218           [2, 's2', "Tips for Macintosh Users"],
219              [3, 's1', "Why does the mouse have two buttons?"]
220  ].each{|depth, style, text|
221    item = t.item_create
222    t.item_style_set(item, 0, style)
223    t.item_element_configure(item, 0, 'e3', :text=>text)
224    if $Version_1_1_OrLater
225      t.item_collapse(item)
226    else # TreeCtrl 1.0
227      t.collapse(item)
228    end
229    t.item_lastchild(parentList[depth], item)
230    depth += 1
231    parentList[depth] = item
232  }
233
234  treeCtrlHelp = TkBindTag.new
235
236  treeCtrlHelp.bind('Double-ButtonPress-1',
237                    proc{|w, x, y|
238                      if w.identify(x, y)[0] == 'header'
239                        Tk::TreeCtrl::BindCallback.doubleButton1(w, x, y)
240                      else
241                        helpButton1(w, x, y)
242                      end
243                      Tk.callback_break
244                    }, '%W %x %y')
245
246  treeCtrlHelp.bind('ButtonPress-1',
247                    proc{|w, x, y|
248                      helpButton1(w, x, y)
249                      Tk.callback_break
250                    }, '%W %x %y')
251
252  treeCtrlHelp.bind('Button1-Motion',
253                    proc{|w, x, y|
254                      helpMotion1(w, x, y)
255                      Tk.callback_break
256                    }, '%W %x %y')
257
258  treeCtrlHelp.bind('Button1-Leave',
259                    proc{|w, x, y|
260                      helpLeave1(w, x, y)
261                      Tk.callback_break
262                    }, '%W %x %y')
263
264  treeCtrlHelp.bind('ButtonRelease-1',
265                    proc{|w, x, y|
266                      helpRelease1(w, x, y)
267                      Tk.callback_break
268                    }, '%W %x %y')
269
270  treeCtrlHelp.bind('Motion', proc{|w, x, y| helpMotion(w, x, y) }, '%W %x %y')
271
272  treeCtrlHelp.bind('Leave', proc{|w, x, y| helpMotion(w, x, y) }, '%W %x %y')
273
274  treeCtrlHelp.bind('KeyPress-Return',
275                    proc{|w, x, y|
276                      if w.selection_get.length == 1
277                        w.item_toggle(w.selection_get[0])
278                      end
279                      Tk.callback_break
280                    }, '%W %x %y')
281
282  @Priv[:help, :prev] = ''
283
284  t.bindtags = [ t, treeCtrlHelp, Tk::TreeCtrl, t.winfo_toplevel, :all ]
285end
286
287def helpButton1(w, x, y)
288  w.set_focus
289  id = w.identify(x, y)
290  @Priv['buttonMode'] = ''
291  if id[0] == 'header'
292    Tk::TreeCtrl::BindCallback.buttonPress1(w, x, y)
293  elsif id[0] == 'item'
294    item = id[1]
295    # didn't click an element
296    return if id.length != 6
297    if w.selection_includes(item)
298      w.toggle(item)
299      return
300    end
301    if w.selection_get.length > 0
302      item2 = w.selection_get[0]
303      if $Version_1_1_OrLater
304        w.item_collapse(item2)
305      else # TreeCtrl 1.0
306        w.collapse(item2)
307      end
308      w.item_ancestors(item2).each{|i|
309        if $Version_1_1_OrLater
310          w.item_collapse(i) if w.compare(item, '!=', i)
311        else # TreeCtrl 1.0
312          w.collapse(i) if w.compare(item, '!=', i)
313        end
314      }
315    end
316    w.activate(item)
317    if $Version_1_1_OrLater
318      w.item_ancestors(item).each{|i|
319        w.item_expand(i)
320      }
321      w.item_toggle(item)
322    else # TreeCtrl 1.0
323      w.expand(*(w.item_ancestors(item)))
324      w.toggle(item)
325    end
326    w.selection_modify(item, :all)
327  end
328end
329
330def helpMotion1(w, x, y)
331  case @Priv['buttonMode']
332  when 'resize', 'header'
333    Tk::TreeCtrl::BindCallback.motion1(w, x, y)
334  end
335end
336
337def helpLeave1(w, x, y)
338  # This is called when I do ButtonPress-1 on Unix for some reason,
339  # and buttonMode is undefined.
340  return unless @Priv.exist?('buttonMode')
341  case @Priv['buttonMode']
342  when 'header'
343    w.column_configure(@Priv['column'], :sunken=>false)
344  end
345end
346
347def helpRelease1(w, x, y)
348  case @Priv['buttonMode']
349  when 'resize', 'header'
350    Tk::TreeCtrl::BindCallback.release1(w, x, y)
351  end
352  @Priv['buttonMode'] = ''
353end
354
355def helpMotion(w, x, y)
356  id = w.identify(x, y)
357  if id.empty?
358  elsif id[0] == 'header'
359  elsif id[0] == 'item'
360    item = id[1]
361    if id.length == 6
362      if @Priv[:help, :prev] != TkComm._get_eval_string(item)
363        if @Priv[:help, :prev] != ''
364          w.item_state_set(@Priv[:help, :prev], '!mouseover')
365        end
366        w.item_state_set(item, 'mouseover')
367        @Priv[:help, :prev] = item
368      end
369      return
370    end
371  end
372  if @Priv[:help, :prev] != ''
373    w.item_state_set(@Priv[:help, :prev], '!mouseover')
374    @Priv[:help, :prev] = ''
375  end
376end
377
378# Alternate implementation doesn't rely on mouseover state
379def helpMotion2(w, x, y)
380  id = w.identify(x, y)
381  if id[0] == 'header'
382  elsif !id.empty?
383    item = id[1]
384    if id.kength == 6
385      if @Priv[:help, :prev] != TkComm._get_eval_string(item)
386        if @Priv[:help, :prev] != ''
387          style = w.item_style_set(@Priv[:help, :prev], 0)
388          style.sub!(/\.f$/, '')
389          w.item_style_map(@Priv[:help, :prev], 0, style, ['e5', 'e3'])
390        end
391        style = w.item_style_set(item, 0)
392        w.item_style_map(item, 0, style + '.f', ['e3', 'e5'])
393        @Priv[:help, :prev] = item
394      end
395      return
396    end
397  end
398  if @Priv[:help, :prev] != ''
399    style = w.item_style_set(@Priv[:help, :prev], 0)
400    style.sub!(/\.f$/, '')
401    w.item_style_map(@Priv[:help, :prev], 0, style, ['e5', 'e3'])
402    @Priv[:help, :prev] = ''
403  end
404end
405