OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tix/] [demos/] [samples/] [EditGrid.tcl] - Blame information for rev 1781

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Tix Demostration Program
2
#
3
# This sample program is structured in such a way so that it can be
4
# executed from the Tix demo program "widget": it must have a
5
# procedure called "RunSample". It should also have the "if" statment
6
# at the end of this file so that it can be run as a standalone
7
# program using tixwish.
8
 
9
# Demonstrates the use of editable entries in a Grid widget.
10
#
11
 
12
proc RunSample {w} {
13
    global editgrid
14
 
15
    wm title $w "Doe Inc. Performance"
16
    wm geometry $w 640x300
17
 
18
    label $w.lab -justify left -text \
19
"The left column is calculated automatically. To calculate the right column,
20
press the \"Calculate\" button"
21
    pack $w.lab -side top -anchor c -padx 3 -pady 3
22
 
23
    # Create the buttons
24
    #
25
    set f [frame $w.f -relief flat]
26
    pack $f -side right -fill y
27
    set add   [button $f.add   -text "Add Row"   -width 9 \
28
        -command "EditGrid_addRow"]
29
    set edit  [button $f.edit  -text "Edit"      -width 9 \
30
        -command "EditGrid_edit"]
31
    set cal   [button $f.cal   -text "Calculate" -width 9 \
32
        -command "EditGrid_calculate"]
33
    set close [button $f.close -text "Close"     -width 9 \
34
        -command "destroy $w"]
35
    pack $add   -side top    -padx 10
36
    pack $edit  -side top    -padx 10
37
    pack $cal   -side top    -padx 10 -pady 2
38
    pack $close -side bottom -padx 10
39
 
40
    # Create the grid and set options to make it editable.
41
    #
42
    tixScrolledGrid $w.g -bd 0
43
    pack $w.g -expand yes -fill both -padx 3 -pady 3
44
 
45
    set grid [$w.g subwidget grid]
46
    $grid config \
47
        -formatcmd "EditGrid_format $grid" \
48
        -editnotifycmd "EditGrid_editNotify" \
49
        -editdonecmd "EditGrid_editDone" \
50
        -selectunit cell \
51
        -selectmode single
52
 
53
    # Insert some initial data
54
    #
55
    $grid set 0 1 -text "City #1"
56
    $grid set 0 2 -text "City #2"
57
    $grid set 0 3 -text "City #3"
58
    $grid set 0 5 -text "Combined"
59
 
60
    $grid set 2 0 -text "Population"
61
    $grid set 4 0 -text "Avg. Income"
62
 
63
    $grid set 2 1 -text 125
64
    $grid set 2 2 -text  81
65
    $grid set 2 3 -text 724
66
 
67
    $grid set 4 1 -text 24432.12
68
    $grid set 4 2 -text 18290.24
69
    $grid set 4 3 -text 18906.34
70
 
71
    # Global data used by other EditGrid_ procedures.
72
    #
73
    set editgrid(g)   $grid
74
    set editgrid(top) 1
75
    set editgrid(bot) 3
76
    set editgrid(result) 5
77
 
78
    EditGrid_calPop
79
    EditGrid_calIncome
80
}
81
 
82
# EditGrid_edit --
83
#
84
#       Prompts the user to edit a cell.
85
#
86
proc EditGrid_edit {} {
87
    global editgrid
88
    set grid $editgrid(g)
89
 
90
    set ent [$grid anchor get]
91
    if [string comp $ent ""] {
92
        $grid edit set [lindex $ent 0]  [lindex $ent 1]
93
    }
94
}
95
 
96
# EditGrid_addRow --
97
#
98
#       Adds a new row to the table.
99
#
100
proc EditGrid_addRow {} {
101
    global editgrid
102
    set grid $editgrid(g)
103
 
104
    $grid edit apply
105
 
106
    $grid move row $editgrid(result) $editgrid(result) 1
107
 
108
    incr editgrid(bot)
109
    set editgrid(result) [expr $editgrid(bot) + 2]
110
    $grid set 0 $editgrid(bot) -text "City #$editgrid(bot)"
111
    $grid set 2 $editgrid(bot) -text 0
112
    $grid set 4 $editgrid(bot) -text 0.0
113
 
114
    EditGrid_calPop
115
    EditGrid_calIncome
116
}
117
 
118
# EditGrid_calPop --
119
#
120
#       Calculates the total population
121
#
122
proc EditGrid_calPop {} {
123
    global editgrid
124
    set grid $editgrid(g)
125
 
126
    set pop 0
127
 
128
    for {set i $editgrid(top)} {$i <= $editgrid(bot)} {incr i} {
129
        incr pop [$grid entrycget 2 $i -text]
130
    }
131
 
132
    $grid set 2 $editgrid(result) -text $pop
133
}
134
 
135
# EditGrid_calIncome --
136
#
137
#       Calculates the average income.
138
#
139
proc EditGrid_calIncome {} {
140
    global editgrid
141
    set grid $editgrid(g)
142
 
143
    set income 0
144
    set total_pop 0
145
    for {set i $editgrid(top)} {$i <= $editgrid(bot)} {incr i} {
146
        set pop [$grid entrycget 2 $i -text]
147
        set inc [$grid entrycget 4 $i -text]
148
        set income [expr $income + $pop.0 * $inc]
149
        incr total_pop $pop
150
    }
151
 
152
    $grid set 4 $editgrid(result) -text [expr $income/$total_pop]
153
 
154
}
155
 
156
# EditGrid_calculate --
157
#
158
#       Recalculates both columns.
159
#
160
proc EditGrid_calculate {} {
161
    global editgrid
162
    set grid $editgrid(g)
163
 
164
    $grid edit apply
165
    EditGrid_calIncome
166
}
167
 
168
# EditGrid_editNotify --
169
#
170
#       Returns true if an entry can be edited.
171
#
172
proc EditGrid_editNotify {x y} {
173
    global editgrid
174
    set grid $editgrid(g)
175
 
176
    if {$x == 2 || $x == 4} {
177
        if {$y >= $editgrid(top) && $y <= $editgrid(bot)} {
178
            set editgrid(oldValue) [$grid entrycget $x $y -text]
179
            return 1
180
        }
181
    }
182
    return 0
183
}
184
 
185
# EditGrid_editDone --
186
#
187
#       Gets called when the user is done editing an entry.
188
#
189
proc EditGrid_editDone {x y} {
190
    global editgrid
191
    set grid $editgrid(g)
192
 
193
    if {$x == 2} {
194
        set pop [$grid entrycget $x $y -text]
195
        if [catch {
196
            format %d $pop
197
        }] {
198
            $grid entryconfig $x $y -text $editgrid(oldValue)
199
            tk_dialog .editGridWarn "" \
200
                "$pop is not an valid integer. Try again" \
201
                warning 0 Ok
202
        } else {
203
            $grid entryconfig 4 $editgrid(result) -text "-"
204
            EditGrid_calPop
205
        }
206
    } else {
207
        set income [$grid entrycget $x $y -text]
208
        if [catch {
209
            format %f $income
210
        }] {
211
            $grid entryconfig $x $y -text $editgrid(oldValue)
212
            tk_dialog .editGridWarn "" \
213
                "$income is not an valid floating number. Try again" \
214
                warning 0 Ok
215
        } else {
216
            $grid entryconfig 4 $editgrid(result) -text "-"
217
        }
218
    }
219
}
220
 
221
# EditGrid_format --
222
#
223
#       This command is called whenever the background of the grid
224
#       needs to be reformatted. The x1, y1, x2, y2 sprcifies the four
225
#       corners of the area that needs to be reformatted.
226
#
227
proc EditGrid_format {w area x1 y1 x2 y2} {
228
    global editgrid
229
 
230
    set bg(s-margin) gray65
231
    set bg(x-margin) gray65
232
    set bg(y-margin) gray65
233
    set bg(main)     gray20
234
 
235
    case $area {
236
        main {
237
            foreach col {2 4} {
238
                $w format border $col 1 $col $editgrid(bot) \
239
                    -relief flat -filled 1 -yon 1 -yoff 1\
240
                    -bd 0 -bg #b0b0f0 -selectbackground #a0b0ff
241
                $w format border $col 2 $col $editgrid(bot) \
242
                    -relief flat -filled 1 -yon 1 -yoff 1\
243
                    -bd 0 -bg #80b080 -selectbackground #80b0ff
244
            }
245
 
246
            $w format grid $x1 $y1 $x2 $y2 \
247
                -relief raised -bd 1 -bordercolor $bg($area) -filled 0 -bg red\
248
                -xon 1 -yon 1 -xoff 0 -yoff 0 -anchor se
249
        }
250
        y-margin {
251
            $w format border $x1 $y1 $x2 $y2 \
252
                -fill 1 -relief raised -bd 1 -bg $bg($area) \
253
                -selectbackground gray80
254
        }
255
        default {
256
            $w format border $x1 $y1 $x2 $y2 \
257
                -filled 1 \
258
                -relief raised -bd 1 -bg $bg($area) \
259
                -selectbackground gray80
260
        }
261
    }
262
 
263
#    case $area {
264
#       {main y-margin} {
265
#           set y [expr $editgrid(bot) + 1]
266
#           $w format border 0 $y 100 $y -bg black -filled 1 -bd 0
267
#       }
268
#   }
269
}
270
 
271
if {![info exists tix_demo_running]} {
272
    wm withdraw .
273
    set w .demo
274
    toplevel $w
275
    RunSample $w
276
    bind $w <Destroy> exit
277
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.