1#
2# text (embedded windows) widget demo 2 (called by 'widget')
3#
4
5# delete toplevel widget
6if defined?($twind2_demo) && $twind2_demo
7  $twind2_demo.destroy
8  $twind2_demo = nil
9end
10
11# demo toplevel widget
12$twind2_demo = TkToplevel.new {|w|
13  title("Text Demonstration - Embedded Windows 2")
14  iconname("Embedded Windows")
15  positionWindow(w)
16}
17
18base_frame = TkFrame.new($twind2_demo).pack(:fill=>:both, :expand=>true)
19
20# frame
21$twind2_buttons = TkFrame.new(base_frame) {|frame|
22  TkGrid(TkFrame.new(frame, :height=>2, :relief=>:sunken, :bd=>2),
23         :columnspan=>4, :row=>0, :sticky=>'ew', :pady=>2)
24  TkGrid('x',
25         TkButton.new(frame, :text=>'See Code',
26                      :image=>$image['view'], :compound=>:left,
27                      :command=>proc{showCode 'twind2'}),
28         TkButton.new(frame, :text=>'Dismiss',
29                      :image=>$image['delete'], :compound=>:left,
30                      :command=>proc{
31                        tmppath = $twind2_demo
32                        $twind2_demo = nil
33                        $showVarsWin[tmppath.path] = nil
34                        tmppath.destroy
35                      }),
36         :padx=>4, :pady=>4)
37  frame.grid_columnconfigure(0, :weight=>1)
38}
39$twind2_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
40
41# frame
42$twind2_text = nil
43TkFrame.new(base_frame, 'highlightthickness'=>2, 'borderwidth'=>2,
44            'relief'=>'sunken') {|f|
45  $twind2_text = TkText.new(f, 'setgrid'=>true, 'font'=>$font,
46                            # 'width'=>'70', 'height'=>35, 'wrap'=>'word',
47                            'width'=>'70', 'height'=>35, 'wrap'=>'char',
48                            'highlightthickness'=>0, 'borderwidth'=>0 ){|t|
49    TkScrollbar.new(f) {|s|
50      command proc{|*args| t.yview(*args)}
51      t.yscrollcommand proc{|first,last| s.set first,last}
52    }.pack('side'=>'right', 'fill'=>'y')
53  }.pack('expand'=>'yes', 'fill'=>'both')
54}.pack('expand'=>'yes', 'fill'=>'both')
55
56# text tags
57$tag2_center = TkTextTag.new($twind2_text,
58                            'justify' =>'center',
59                            'spacing1'=>'5m',
60                            'spacing3'=>'5m'  )
61$tag2_buttons = TkTextTag.new($twind2_text,
62                             'lmargin1'=>'1c',
63                             'lmargin2'=>'1c',
64                             'rmargin' =>'1c',
65                             'spacing1'=>'3m',
66                             'spacing2'=>0,
67                             'spacing3'=>0 )
68
69# insert text
70$twind2_text.insert('end',
71                  'A text widget can contain many different kinds of items, ')
72$twind2_text.insert('end',
73                  "both active and passive.  It can lay these out in various ")
74$twind2_text.insert('end',
75                   "ways, with wrapping, tabs, centering, etc.  In addition, ")
76$twind2_text.insert('end',
77                    "when the contents are too big for the window, smooth ")
78$twind2_text.insert('end', "scrolling in all directions is provided.\n\n")
79
80$twind2_text.insert('end', "A text widget can contain other widgets embedded ")
81$twind2_text.insert('end', "it.  These are called \"embedded windows\", ")
82$twind2_text.insert('end', "and they can consist of arbitrary widgets.  ")
83$twind2_text.insert('end', "For example, here are two embedded button ")
84$twind2_text.insert('end', "widgets.  You can click on the first button to ")
85TkTextWindow.new($twind2_text, 'end',
86                 'window'=>TkButton.new($twind2_text) {
87                   text 'ON'
88                   command proc{textWindOn2 $twind2_text,$twind2_buttons}
89                   cursor 'top_left_arrow'
90                 })
91$twind2_text.insert('end', " horizontal scrolling, which also turns off ")
92$twind2_text.insert('end', "word wrapping.  Or, you can click on the second ")
93$twind2_text.insert('end', "button to\n")
94TkTextWindow.new($twind2_text, 'end',
95                 'window'=>TkButton.new($twind2_text) {
96                   text 'OFF'
97                   command proc{textWindOff2 $twind2_text}
98                   cursor 'top_left_arrow'
99                 })
100$twind2_text.insert('end',
101                   " horizontal scrolling and turn back on word wrapping.\n\n")
102
103$twind2_text.insert('end', "Or, here is another example.  If you ")
104TkTextWindow.new($twind2_text, 'end',
105                 'window'=>TkButton.new($twind2_text) {
106                   text 'Click Here'
107                   command proc{textWindPlot2 $twind2_text}
108                   cursor 'top_left_arrow'
109                 })
110$twind2_text.insert('end',
111                    " a canvas displaying an x-y plot will appear right here.")
112$mark2_plot = TkTextMark.new($twind2_text, 'insert')
113$mark2_plot.gravity='left'
114$twind2_text.insert('end',
115                    "  You can drag the data points around with the mouse, ")
116$twind2_text.insert('end', "or you can click here to ")
117TkTextWindow.new($twind2_text, 'end',
118                 'window'=>TkButton.new($twind2_text) {
119                   text 'Delete'
120                   command proc{textWindDel2 $twind2_text}
121                   cursor 'top_left_arrow'
122                 })
123$twind2_text.insert('end', " the plot again.\n\n")
124
125$twind2_text.insert('end',
126                    "You may also find it useful to put embedded windows in ")
127$twind2_text.insert('end',
128                    "a text without any actual text.  In this case the ")
129$twind2_text.insert('end', "text widget acts like a geometry manager.  For ")
130$twind2_text.insert('end',
131                    "example, here is a collection of buttons laid out ")
132$twind2_text.insert('end',
133                    "neatly into rows by the text widget.  These buttons ")
134$twind2_text.insert('end',
135                    "can be used to change the background color of the ")
136$twind2_text.insert('end', "text widget (\"Default\" restores the color to ")
137$twind2_text.insert('end',
138                    "its default).  If you click on the button labeled ")
139$twind2_text.insert('end', "\"Short\", it changes to a longer string so that ")
140$twind2_text.insert('end', "you can see how the text widget automatically ")
141$twind2_text.insert('end', "changes the layout.  Click on the button again ")
142$twind2_text.insert('end', "to restore the short string.\n")
143
144btn_default = TkButton.new($twind2_text) {|b|
145  text 'Default'
146  command proc{embDefBg2 $twind2_text}
147  cursor 'top_left_arrow'
148}
149TkTextWindow.new($twind2_text, 'end', 'window'=>btn_default, 'padx'=>3)
150embToggle = TkVariable.new('Short')
151TkTextWindow.new($twind2_text, 'end',
152                 'window'=>TkCheckButton.new($twind2_text) {
153                   textvariable embToggle
154                   indicatoron 0
155                   variable embToggle
156                   onvalue 'A much longer string'
157                   offvalue 'Short'
158                   cursor 'top_left_arrow'
159                   pady 5
160                   padx 2
161                 },
162                 'padx'=>3,
163                 'pady'=>2 )
164
165[ 'AntiqueWhite3', 'Bisque1', 'Bisque2', 'Bisque3', 'Bisque4',
166  'SlateBlue3', 'RoyalBlue1', 'SteelBlue2', 'DeepSkyBlue3', 'LightBlue1',
167  'DarkSlateGray1', 'Aquamarine2', 'DarkSeaGreen2', 'SeaGreen1',
168  'Yellow1', 'IndianRed1', 'IndianRed2', 'Tan1', 'Tan4'
169].each{|twind_color|
170  TkTextWindow.new($twind2_text, 'end',
171                   'window'=>TkButton.new($twind2_text) {
172                     text twind_color
173                     cursor 'top_left_arrow'
174                     command proc{$twind2_text.bg twind_color}
175                   },
176                   'padx'=>3,
177                   'pady'=>2 )
178}
179
180$tag2_buttons.add(btn_default, 'end')
181
182$text_normal2 = {}
183$text_normal2['border'] = $twind2_text.cget('borderwidth')
184$text_normal2['highlight'] = $twind2_text.cget('highlightthickness')
185$text_normal2['pad'] = $twind2_text.cget('padx')
186
187$twind2_text.insert('end', "\nYou can also change the usual border width and ")
188$twind2_text.insert('end', "highlightthickness and padding.\n")
189
190TkTextWindow.new($twind2_text, 'end',
191                 'window'=>TkButton.new($twind2_text, :text=>"Big borders",
192                                        :cursor=>'top_left_arrow',
193                                        'command'=>proc{
194                                          textWinBigB2 $twind2_text
195                                        }))
196
197TkTextWindow.new($twind2_text, 'end',
198                 'window'=>TkButton.new($twind2_text, :text=>"Small borders",
199                                        :cursor=>'top_left_arrow',
200                                        'command'=>proc{
201                                          textWinSmallB2 $twind2_text
202                                        }))
203
204TkTextWindow.new($twind2_text, 'end',
205                 'window'=>TkButton.new($twind2_text, :text=>"Big highlight",
206                                        :cursor=>'top_left_arrow',
207                                        'command'=>proc{
208                                          textWinBigH2 $twind2_text
209                                        }))
210
211TkTextWindow.new($twind2_text, 'end',
212                 'window'=>TkButton.new($twind2_text, :text=>"Small highlight",
213                                        :cursor=>'top_left_arrow',
214                                        'command'=>proc{
215                                          textWinSmallH2 $twind2_text
216                                        }))
217
218TkTextWindow.new($twind2_text, 'end',
219                 'window'=>TkButton.new($twind2_text, :text=>"Big pad",
220                                        :cursor=>'top_left_arrow',
221                                        'command'=>proc{
222                                          textWinBigP2 $twind2_text
223                                        }))
224
225TkTextWindow.new($twind2_text, 'end',
226                 'window'=>TkButton.new($twind2_text, :text=>"Small pad",
227                                        :cursor=>'top_left_arrow',
228                                        'command'=>proc{
229                                          textWinSmallP2 $twind2_text
230                                        }))
231
232$twind2_text.insert('end',
233                    "\n\nFinally, images fit comfortably in text widgets too:")
234
235TkTextImage.new($twind2_text, 'end',
236                'image'=>TkBitmapImage.new(:file=>[
237                                             $demo_dir, '..',
238                                             'images', 'face.xbm'
239                                           ].join(File::Separator)))
240
241# methods
242def textWinBigB2(w)
243  w.borderwidth 15
244end
245def textWinSmallB2(w)
246  w.borderwidth $text_normal2['border']
247end
248def textWinBigH2(w)
249  w.highlightthickness 15
250end
251def textWinSmallH2(w)
252  w.highlightthickness $text_normal2['highlight']
253end
254def textWinBigP2(w)
255  w.configure(:padx=>15, :pady=>15)
256end
257def textWinSmallP2(w)
258  w.configure(:padx=>$text_normal2['pad'], :pady=>$text_normal2['pad'])
259end
260
261def textWindOn2 (w,f)
262  if defined? $twind2_scroll
263    begin
264      $twind2_scroll.destroy
265    rescue
266    end
267    $twind2_scroll = nil
268  end
269
270  base = TkWinfo.parent( TkWinfo.parent(w) )
271  $twind2_scroll = TkScrollbar.new(base) {|s|
272    orient 'horizontal'
273    command proc{|*args| w.xview(*args)}
274    w.xscrollcommand proc{|first,last| s.set first,last}
275    w.wrap 'none'
276    pack('after'=>f, 'side'=>'bottom', 'fill'=>'x')
277  }
278
279  return nil
280end
281
282def textWindOff2 (w)
283  if defined? $twind2_scroll
284    begin
285      $twind2_scroll.destroy
286    rescue
287    end
288    $twind2_scroll = nil
289  end
290  w.xscrollcommand ''
291  #w.wrap 'word'
292  w.wrap 'char'
293end
294
295def textWindPlot2 (t)
296  if (defined? $twind2_plot) && (TkWinfo.exist?($twind2_plot))
297    return
298  end
299
300  $twind2_plot = TkCanvas.new(t) {
301    relief 'sunken'
302    width  450
303    height 300
304    cursor 'top_left_arrow'
305  }
306
307  #font = '-Adobe-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*'
308  font = 'Helvetica 18'
309
310  TkcLine.new($twind2_plot, 100, 250, 400, 250, 'width'=>2)
311  TkcLine.new($twind2_plot, 100, 250, 100,  50, 'width'=>2)
312  TkcText.new($twind2_plot, 225, 20,
313              'text'=>"A Simple Plot", 'font'=>font, 'fill'=>'brown')
314
315  (0..10).each {|i|
316    x = 100 + (i * 30)
317    TkcLine.new($twind2_plot, x, 250, x, 245, 'width'=>2)
318    TkcText.new($twind2_plot, x, 254,
319                'text'=>10*i, 'font'=>font, 'anchor'=>'n')
320  }
321  (0..5).each {|i|
322    y = 250 - (i * 40)
323    TkcLine.new($twind2_plot, 100, y, 105, y, 'width'=>2)
324    TkcText.new($twind2_plot, 96, y,
325                'text'=>"#{i*50}.0", 'font'=>font, 'anchor'=>'e')
326  }
327
328  for xx, yy in [[12,56],[20,94],[33,98],[32,120],[61,180],[75,160],[98,223]]
329    x = 100 + (3*xx)
330    y = 250 - (4*yy)/5
331    item = TkcOval.new($twind2_plot, x-6, y-6, x+6, y+6,
332                       'width'=>1, 'outline'=>'black', 'fill'=>'SkyBlue2')
333    item.addtag 'point'
334  end
335
336  $twind2_plot.itembind('point', 'Any-Enter',
337                        proc{$twind2_plot.itemconfigure 'current', 'fill', 'red'})
338  $twind2_plot.itembind('point', 'Any-Leave',
339                        proc{$twind2_plot.itemconfigure 'current', 'fill', 'SkyBlue2'})
340  $twind2_plot.itembind('point', '1',
341                        proc{|x,y| embPlotDown2 $twind2_plot,x,y}, "%x %y")
342  $twind2_plot.itembind('point', 'ButtonRelease-1',
343                        proc{$twind2_plot.dtag 'selected'})
344  $twind2_plot.bind('B1-Motion',
345                    proc{|x,y| embPlotMove2 $twind2_plot,x,y}, "%x %y")
346  while ($twind2_text.get($mark2_plot) =~ /[ \t\n]/)
347    $twind2_text.delete $mark2_plot
348  end
349  $twind2_text.insert $mark2_plot,"\n"
350  TkTextWindow.new($twind2_text, $mark2_plot, 'window'=>$twind2_plot)
351  $tag2_center.add $mark2_plot
352  $twind2_text.insert $mark2_plot,"\n"
353end
354
355$embPlot2 = {'lastX'=>0, 'lastY'=>0}
356
357def embPlotDown2 (w, x, y)
358  w.dtag 'selected'
359  w.addtag_withtag 'selected', 'current'
360  w.raise 'current'
361  $embPlot2['lastX'] = x
362  $embPlot2['lastY'] = y
363end
364
365def embPlotMove2 (w, x, y)
366  w.move 'selected', x - $embPlot2['lastX'], y - $embPlot2['lastY']
367  $embPlot2['lastX'] = x
368  $embPlot2['lastY'] = y
369end
370
371def textWindDel2 (w)
372  if (defined? $twind2_text) && TkWinfo.exist?($twind2_plot)
373    $twind2_text.delete $twind2_plot
374    $twind2_plot = nil
375    while ($twind2_text.get($mark2_plot) =~ /[ \t\n]/)
376      $twind2_text.delete $mark2_plot
377    end
378    $twind2_text.insert $mark2_plot,"  "
379  end
380end
381
382def embDefBg2 (w)
383  w['background'] = w.configinfo('background')[3]
384end
385