summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/sample/tkextlib/tktable/buttons.rb
blob: e35c137a28bc69c6aaf195af39377cf65743b4f9 (plain)
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
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
#!/usr/bin/env ruby
##
## buttons.rb
##
## demonstrates the simulation of a button array
##
## ( based on 'buttons.tcl' included source archive of tktable extension )
##
require 'tk'
require 'tkextlib/tktable'

# create the table
tab  = TkVariable.new_hash
rows = 20
cols = 20

table = Tk::TkTable.new(:rows=>rows + 1, :cols=>cols + 1,
                        :variable=>tab, :titlerows=>1, :titlecols=>1,
                        :roworigin=>-1, :colorigin=>-1,
                        :colwidth=>4, :width=>8, :height=>8,
                        :cursor=>'top_left_arrow', :borderwidth=>2,
                        :flashmode=>false, :state=>:disabled)

sx = table.xscrollbar(TkScrollbar.new)
sy = table.yscrollbar(TkScrollbar.new)

Tk.grid(table, sy, :sticky=>:news)
Tk.grid(sx, :sticky=>:ew)

Tk.root.grid_columnconfig(0, :weight=>1)
Tk.root.grid_rowconfig(0, :weight=>1)

# set up tags for the various states of the buttons
table.tag_configure('OFF', :bg=>'red',    :relief=>:raised)
table.tag_configure('ON',  :bg=>'green',  :relief=>:sunken)
table.tag_configure('sel', :bg=>'gray75', :relief=>:flat)

# clean up if mouse leaves the widget
table.bind('Leave', proc{|w| w.selection_clear_all}, '%W')

# highlight the cell under the mouse
table.bind('Motion', proc{|w, x, y|
             Tk.callback_break if w.selection_include?(TkComm._at(x,y))
             w.selection_clear_all
             w.selection_set(TkComm._at(x,y))
             Tk.callback_break
             ## "break" prevents the call to tkTableCheckBorder
           }, '%W %x %y')

# mousebutton 1 toggles the value of the cell
# use of "selection includes" would work here
table.bind('1', proc{|w, x, y|
             #rc = w.curselection[0]
             rc = w.index(TkComm._at(x,y))
             if tab[rc] == 'ON'
               tab[rc] = 'OFF'
               w.tag_cell('OFF', rc)
             else
               tab[rc] = 'ON'
               w.tag_cell('ON', rc)
             end}, '%W %x %y')


# inititialize the array, titles, and celltags
0.step(rows){|i|
  tab[i,-1] = i
  0.step(cols){|j|
    if i == 0
      tab[-1,j] = j
    end
    tab[i,j] = "OFF"
    table.tag_cell('OFF', "#{i},#{j}")
  }
}

Tk.mainloop