1# twind.rb 2# 3# This demonstration script creates a text widget with a bunch of 4# embedded windows. 5# 6# text (embedded windows) widget demo (called by 'widget') 7# 8 9# toplevel widget 10if defined?($twind_demo) && $twind_demo 11 $twind_demo.destroy 12 $twind_demo = nil 13end 14 15# demo toplevel widget 16$twind_demo = TkToplevel.new {|w| 17 title("Text Demonstration - Embedded Windows") 18 iconname("Embedded Windows") 19 positionWindow(w) 20} 21 22base_frame = TkFrame.new($twind_demo).pack(:fill=>:both, :expand=>true) 23 24# frame 25$twind_buttons = TkFrame.new(base_frame) {|frame| 26 TkButton.new(frame) { 27 text 'Dismiss' 28 command proc{ 29 tmppath = $twind_demo 30 $twind_demo = nil 31 tmppath.destroy 32 } 33 }.pack('side'=>'left', 'expand'=>'yes') 34 35 TkButton.new(frame) { 36 text 'Show Code' 37 command proc{showCode 'twind'} 38 }.pack('side'=>'left', 'expand'=>'yes') 39} 40$twind_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m') 41 42# frame 43$twind_text = nil 44TkFrame.new(base_frame, 'highlightthickness'=>2, 'borderwidth'=>2, 45 'relief'=>'sunken') {|f| 46 $twind_text = TkText.new(f, 'setgrid'=>'true', 'font'=>$font, 47 'width'=>'70', 'height'=>35, 'wrap'=>'word', 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# 57$tag_center = TkTextTag.new($twind_text, 58 'justify' =>'center', 59 'spacing1'=>'5m', 60 'spacing3'=>'5m' ) 61$tag_buttons = TkTextTag.new($twind_text, 62 'lmargin1'=>'1c', 63 'lmargin2'=>'1c', 64 'rmargin' =>'1c', 65 'spacing1'=>'3m', 66 'spacing2'=>0, 67 'spacing3'=>0 ) 68 69$twind_text.insert('end', "A text widget can contain other widgets embedded ") 70$twind_text.insert('end', "it. These are called \"embedded windows\", ") 71$twind_text.insert('end', "and they can consist of arbitrary widgets. ") 72$twind_text.insert('end', "For example, here are two embedded button ") 73$twind_text.insert('end', "widgets. You can click on the first button to ") 74TkTextWindow.new($twind_text, 'end', 75 'window'=>TkButton.new($twind_text) { 76 #text 'ON' 77 text 'Turn On' 78 command proc{textWindOn $twind_text,$twind_buttons} 79 cursor 'top_left_arrow' 80 }) 81$twind_text.insert('end', " horizontal scrolling, which also turns off ") 82$twind_text.insert('end', "word wrapping. Or, you can click on the second ") 83$twind_text.insert('end', "button to\n") 84TkTextWindow.new($twind_text, 'end', 85 'window'=>TkButton.new($twind_text) { 86 #text 'OFF' 87 text 'Turn Off' 88 command proc{textWindOff $twind_text} 89 cursor 'top_left_arrow' 90 }) 91 92$twind_text.insert('end', " horizontal scrolling and turn back on word wrapping.\n\n") 93$twind_text.insert('end', "Or, here is another example. If you ") 94TkTextWindow.new($twind_text, 'end', 95 'window'=>TkButton.new($twind_text) { 96 text 'Click Here' 97 command proc{textWindPlot $twind_text} 98 cursor 'top_left_arrow' 99 }) 100$twind_text.insert('end', " a canvas displaying an x-y plot will appear right here.") 101$mark_plot = TkTextMark.new($twind_text, 'insert') 102$mark_plot.gravity='left' 103$twind_text.insert('end', " You can drag the data points around with the mouse, ") 104$twind_text.insert('end', "or you can click here to ") 105TkTextWindow.new($twind_text, 'end', 106 'window'=>TkButton.new($twind_text) { 107 text 'Delete' 108 command proc{textWindDel $twind_text} 109 cursor 'top_left_arrow' 110 }) 111 112$twind_text.insert('end', " the plot again.\n\n") 113$twind_text.insert('end', "You may also find it useful to put embedded windows in ") 114$twind_text.insert('end', "a text without any actual text. In this case the ") 115$twind_text.insert('end', "text widget acts like a geometry manager. For ") 116$twind_text.insert('end', "example, here is a collection of buttons laid out ") 117$twind_text.insert('end', "neatly into rows by the text widget. These buttons ") 118$twind_text.insert('end', "can be used to change the background color of the ") 119$twind_text.insert('end', "text widget (\"Default\" restores the color to ") 120$twind_text.insert('end', "its default). If you click on the button labeled ") 121$twind_text.insert('end', "\"Short\", it changes to a longer string so that ") 122$twind_text.insert('end', "you can see how the text widget automatically ") 123$twind_text.insert('end', "changes the layout. Click on the button again ") 124$twind_text.insert('end', "to restore the short string.\n") 125 126 127TkTextWindow.new($twind_text, 'end', 128 'window'=>TkButton.new($twind_text) {|b| 129 text 'Default' 130 command proc{embDefBg $twind_text} 131 cursor 'top_left_arrow' 132 $tag_buttons.add('end') 133 }, 134 'padx'=>3 ) 135embToggle = TkVariable.new('Short') 136TkTextWindow.new($twind_text, 'end', 137 'window'=>TkCheckButton.new($twind_text) { 138 textvariable embToggle 139 indicatoron 0 140 variable embToggle 141 onvalue 'A much longer string' 142 offvalue 'Short' 143 cursor 'top_left_arrow' 144 pady 5 145 padx 2 146 }, 147 'padx'=>3, 148 'pady'=>2 ) 149 150[ 'AntiqueWhite3', 'Bisque1', 'Bisque2', 'Bisque3', 'Bisque4', 151 'SlateBlue3', 'RoyalBlue1', 'SteelBlue2', 'DeepSkyBlue3', 'LightBlue1', 152 'DarkSlateGray1', 'Aquamarine2', 'DarkSeaGreen2', 'SeaGreen1', 153 'Yellow1', 'IndianRed1', 'IndianRed2', 'Tan1', 'Tan4' 154].each{|twind_color| 155 TkTextWindow.new($twind_text, 'end', 156 'window'=>TkButton.new($twind_text) { 157 text twind_color 158 cursor 'top_left_arrow' 159 command proc{$twind_text.bg twind_color} 160 }, 161 'padx'=>3, 162 'pady'=>2 ) 163} 164 165# 166def textWindOn (w,f) 167 if defined? $twind_scroll 168 begin 169 $twind_scroll.destroy 170 rescue 171 end 172 $twind_scroll = nil 173 end 174 175 base = TkWinfo.parent( TkWinfo.parent(w) ) 176 $twind_scroll = TkScrollbar.new(base) {|s| 177 orient 'horizontal' 178 command proc{|*args| w.xview(*args)} 179 w.xscrollcommand proc{|first,last| s.set first,last} 180 w.wrap 'none' 181 pack('after'=>f, 'side'=>'bottom', 'fill'=>'x') 182 } 183 184 return nil 185end 186 187def textWindOff (w) 188 if defined? $twind_scroll 189 begin 190 $twind_scroll.destroy 191 rescue 192 end 193 $twind_scroll = nil 194 end 195 w.xscrollcommand '' 196 w.wrap 'word' 197end 198 199def textWindPlot (t) 200 if (defined? $twind_plot) && TkWinfo.exist?($twind_plot) 201 return 202 end 203 204 $twind_plot = TkCanvas.new(t) { 205 relief 'sunken' 206 width 450 207 height 300 208 cursor 'top_left_arrow' 209 } 210 211 if $tk_version =~ /^4.*/ 212 font = '-Adobe-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*' 213 else 214 font = 'Helvetica 18' 215 end 216 217 TkcLine.new($twind_plot, 100, 250, 400, 250, 'width'=>2) 218 TkcLine.new($twind_plot, 100, 250, 100, 50, 'width'=>2) 219 TkcText.new($twind_plot, 225, 20, 220 'text'=>"A Simple Plot", 'font'=>font, 'fill'=>'brown') 221 222 (0..10).each {|i| 223 x = 100 + (i * 30) 224 TkcLine.new($twind_plot, x, 250, x, 245, 'width'=>2) 225 TkcText.new($twind_plot, x, 254, 226 'text'=>10*i, 'font'=>font, 'anchor'=>'n') 227 } 228 (0..5).each {|i| 229 y = 250 - (i * 40) 230 TkcLine.new($twind_plot, 100, y, 105, y, 'width'=>2) 231 TkcText.new($twind_plot, 96, y, 232 'text'=>"#{i*50}.0", 'font'=>font, 'anchor'=>'e') 233 } 234 235 for xx, yy in [[12,56],[20,94],[33,98],[32,120],[61,180],[75,160],[98,223]] 236 x = 100 + (3*xx) 237 y = 250 - (4*yy)/5 238 item = TkcOval.new($twind_plot, x-6, y-6, x+6, y+6, 239 'width'=>1, 'outline'=>'black', 'fill'=>'SkyBlue2') 240 item.addtag 'point' 241 end 242 243 $twind_plot.itembind('point', 'Any-Enter', 244 proc{$twind_plot.itemconfigure 'current', 'fill', 'red'}) 245 $twind_plot.itembind('point', 'Any-Leave', 246 proc{$twind_plot.itemconfigure 'current', 'fill', 'SkyBlue2'}) 247 $twind_plot.itembind('point', '1', 248 proc{|x,y| embPlotDown $twind_plot,x,y}, "%x %y") 249 $twind_plot.itembind('point', 'ButtonRelease-1', 250 proc{$twind_plot.dtag 'selected'}) 251 $twind_plot.bind('B1-Motion', 252 proc{|x,y| embPlotMove $twind_plot,x,y}, "%x %y") 253 while ($twind_text.get($mark_plot) =~ /[ \t\n]/) 254 $twind_text.delete $mark_plot 255 end 256 $twind_text.insert $mark_plot,"\n" 257 TkTextWindow.new($twind_text, $mark_plot, 'window'=>$twind_plot) 258 $tag_center.add $mark_plot 259 $twind_text.insert $mark_plot,"\n" 260end 261 262$embPlot = {'lastX'=>0, 'lastY'=>0} 263 264def embPlotDown (w, x, y) 265 w.dtag 'selected' 266 w.addtag_withtag 'selected', 'current' 267 w.raise 'current' 268 $embPlot['lastX'] = x 269 $embPlot['lastY'] = y 270end 271 272def embPlotMove (w, x, y) 273 w.move 'selected', x - $embPlot['lastX'], y - $embPlot['lastY'] 274 $embPlot['lastX'] = x 275 $embPlot['lastY'] = y 276end 277 278def textWindDel (w) 279 if (defined? $twind_text) && TkWinfo.exist?($twind_plot) 280 $twind_text.delete $twind_plot 281 $twind_plot = nil 282 while ($twind_text.get($mark_plot) =~ /[ \t\n]/) 283 $twind_text.delete $mark_plot 284 end 285 $twind_text.insert $mark_plot," " 286 end 287end 288 289def embDefBg (w) 290 w['background'] = w.configinfo('background')[3] 291end 292