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

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [itcl/] [iwidgets3.0.0/] [generic/] [spinner.itk] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Spinner
2
# ----------------------------------------------------------------------
3
# Implements a spinner widget.  The Spinner is comprised of an
4
# EntryField plus up and down arrow buttons.
5
# Spinner is meant to be used as a base class for creating more
6
# specific spinners such as SpinInt.itk
7
# Arrows may be drawn horizontally or vertically.
8
# User may define arrow behavior or accept the default arrow behavior.
9
#
10
# ----------------------------------------------------------------------
11
#   AUTHOR:  Sue Yockey               Phone: (214) 519-2517
12
#                                     E-mail: syockey@spd.dsccc.com
13
#                                             yockey@acm.org
14
#
15
#   @(#) $Id: spinner.itk,v 1.1.1.1 2002-01-16 10:24:50 markom Exp $
16
# ----------------------------------------------------------------------
17
#            Copyright (c) 1995 DSC Technologies Corporation
18
# ======================================================================
19
# Permission to use, copy, modify, distribute and license this software
20
# and its documentation for any purpose, and without fee or written
21
# agreement with DSC, is hereby granted, provided that the above copyright
22
# notice appears in all copies and that both the copyright notice and
23
# warranty disclaimer below appear in supporting documentation, and that
24
# the names of DSC Technologies Corporation or DSC Communications
25
# Corporation not be used in advertising or publicity pertaining to the
26
# software without specific, written prior permission.
27
#
28
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
29
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
30
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
31
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
32
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL
33
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
34
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
36
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37
# SOFTWARE.
38
# ======================================================================
39
 
40
#
41
# Usual options.
42
#
43
itk::usual Spinner {
44
    keep -background -borderwidth -cursor -foreground -highlightcolor \
45
         -highlightthickness -insertbackground -insertborderwidth \
46
         -insertofftime -insertontime -insertwidth -labelfont \
47
         -selectbackground -selectborderwidth -selectforeground \
48
         -textbackground -textfont
49
}
50
 
51
# ------------------------------------------------------------------
52
#                              SPINNER
53
# ------------------------------------------------------------------
54
class iwidgets::Spinner {
55
    inherit iwidgets::Entryfield
56
 
57
    constructor {args} {}
58
    destructor {}
59
 
60
    itk_option define -arroworient arrowOrient Orient vertical
61
    itk_option define -textfont textFont \
62
            Font -Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*
63
    itk_option define -borderwidth borderWidth BorderWidth 2
64
    itk_option define -highlightthickness highlightThickness \
65
            HighlightThickness 2
66
    itk_option define -increment increment Command {}
67
    itk_option define -decrement decrement Command {}
68
    itk_option define -repeatdelay repeatDelay RepeatDelay 300
69
    itk_option define -repeatinterval repeatInterval RepeatInterval 100
70
    itk_option define -foreground foreground Foreground black
71
 
72
    public method down {}
73
    public method up {}
74
 
75
    protected method _pushup {}
76
    protected method _pushdown {}
77
    protected method _relup {}
78
    protected method _reldown {}
79
    protected method _doup {rate}
80
    protected method _dodown {rate}
81
    protected method _up {}
82
    protected method _down {}
83
 
84
    protected method _positionArrows {{when later}}
85
 
86
    protected variable _interior {}
87
    protected variable _reposition ""  ;# non-null => _positionArrows pending
88
    protected variable _uptimer ""     ;# non-null => _uptimer pending
89
    protected variable _downtimer ""   ;# non-null => _downtimer pending
90
}
91
 
92
#
93
# Provide a lowercased access method for the Spinner class.
94
#
95
proc ::iwidgets::spinner {pathName args} {
96
    uplevel ::iwidgets::Spinner $pathName $args
97
}
98
 
99
# ------------------------------------------------------------------
100
#                        CONSTRUCTOR
101
# ------------------------------------------------------------------
102
body iwidgets::Spinner::constructor {args} {
103
    #
104
    # Save off the interior for later use.
105
    #
106
    set _interior $itk_interior
107
 
108
    #
109
    # Create up arrow button.
110
    #
111
    itk_component add uparrow {
112
        canvas $itk_interior.uparrow -height 10 -width 10 \
113
                -relief raised -highlightthickness 0
114
    } {
115
        keep -background -borderwidth
116
    }
117
 
118
    #
119
    # Create down arrow button.
120
    #
121
    itk_component add downarrow {
122
        canvas $itk_interior.downarrow -height 10 -width 10 \
123
                -relief raised -highlightthickness 0
124
    } {
125
        keep -background -borderwidth
126
    }
127
 
128
    #
129
    # Add bindings for button press events on the up and down buttons.
130
    #
131
    bind $itk_component(uparrow)  [code $this _pushup]
132
    bind $itk_component(uparrow)  [code $this _relup]
133
 
134
    bind $itk_component(downarrow)   [code $this _pushdown]
135
    bind $itk_component(downarrow)  [code $this _reldown]
136
 
137
    eval itk_initialize $args
138
 
139
    #
140
    # When idle, position the arrows.
141
    #
142
    _positionArrows
143
}
144
 
145
# ------------------------------------------------------------------
146
#                          DESTRUCTOR
147
# ------------------------------------------------------------------
148
 
149
body iwidgets::Spinner::destructor {} {
150
    if {$_reposition != ""} {after cancel $_reposition}
151
    if {$_uptimer != ""} {after cancel $_uptimer}
152
    if {$_downtimer != ""} {after cancel $_downtimer}
153
}
154
 
155
# ------------------------------------------------------------------
156
#                             OPTIONS
157
# ------------------------------------------------------------------
158
 
159
# ------------------------------------------------------------------
160
# OPTION: -arroworient
161
#
162
# Place arrows vertically or horizontally .
163
# ------------------------------------------------------------------
164
configbody iwidgets::Spinner::arroworient {
165
    _positionArrows
166
}
167
 
168
# ------------------------------------------------------------------
169
# OPTION: -textfont
170
#
171
# Change font, resize arrow buttons.
172
# ------------------------------------------------------------------
173
configbody iwidgets::Spinner::textfont {
174
    _positionArrows
175
}
176
 
177
# ------------------------------------------------------------------
178
# OPTION: -highlightthickness
179
#
180
# Change highlightthickness, resize arrow buttons.
181
# ------------------------------------------------------------------
182
configbody iwidgets::Spinner::highlightthickness {
183
    _positionArrows
184
}
185
 
186
# ------------------------------------------------------------------
187
# OPTION: -borderwidth
188
#
189
# Change borderwidth, resize arrow buttons.
190
# ------------------------------------------------------------------
191
configbody iwidgets::Spinner::borderwidth {
192
    _positionArrows
193
}
194
 
195
# ------------------------------------------------------------------
196
# OPTION: -increment
197
#
198
# Up arrow callback.
199
# ------------------------------------------------------------------
200
configbody iwidgets::Spinner::increment {
201
    if {$itk_option(-increment) == {}} {
202
        set itk_option(-increment) [code $this up]
203
    }
204
}
205
 
206
# ------------------------------------------------------------------
207
# OPTION: -decrement
208
#
209
# Down arrow callback.
210
# ------------------------------------------------------------------
211
configbody iwidgets::Spinner::decrement {
212
    if {$itk_option(-decrement) == {}} {
213
        set itk_option(-decrement) [code $this down]
214
    }
215
}
216
 
217
# ------------------------------------------------------------------
218
# OPTION: -repeatinterval
219
#
220
# Arrow repeat rate in milliseconds. A repeatinterval of 0 disables
221
# button repeat.
222
# ------------------------------------------------------------------
223
configbody iwidgets::Spinner::repeatinterval {
224
    if {$itk_option(-repeatinterval) < 0} {
225
       set itk_option(-repeatinterval) 0
226
    }
227
}
228
 
229
# ------------------------------------------------------------------
230
# OPTION: -repeatdelay
231
#
232
# Arrow repeat delay in milliseconds.
233
# ------------------------------------------------------------------
234
configbody iwidgets::Spinner::repeatdelay {
235
    if {$itk_option(-repeatdelay) < 0} {
236
       set itk_option(-repeatdelay) 0
237
    }
238
}
239
 
240
# ------------------------------------------------------------------
241
# OPTION: -foreground
242
#
243
# Set the foreground color of the up and down arrows. Remember
244
# to make sure the "tag" exists before setting them...
245
# ------------------------------------------------------------------
246
configbody iwidgets::Spinner::foreground {
247
 
248
    if { [$itk_component(uparrow) gettags up] != "" } {
249
        $itk_component(uparrow) itemconfigure up \
250
                -fill $itk_option(-foreground)
251
    }
252
 
253
    if { [$itk_component(downarrow) gettags down] != "" } {
254
        $itk_component(downarrow) itemconfigure down \
255
                -fill $itk_option(-foreground)
256
    }
257
}
258
 
259
# ------------------------------------------------------------------
260
#                            METHODS
261
# ------------------------------------------------------------------
262
 
263
# ------------------------------------------------------------------
264
# METHOD: up
265
#
266
# Up arrow command.  Meant to be overloaded by derived class.
267
# ------------------------------------------------------------------
268
body iwidgets::Spinner::up {} {
269
}
270
 
271
# ------------------------------------------------------------------
272
# METHOD: down
273
#
274
# Down arrow command.  Meant to be overloaded by derived class.
275
# ------------------------------------------------------------------
276
body iwidgets::Spinner::down {} {
277
}
278
 
279
# ------------------------------------------------------------------
280
# PROTECTED METHOD: _positionArrows ?when?
281
#
282
# Draw Arrows for spinner. If "when" is "now", the change is applied
283
# immediately.  If it is "later" or it is not specified, then the
284
# change is applied later, when the application is idle.
285
# ------------------------------------------------------------------
286
body iwidgets::Spinner::_positionArrows {{when later}} {
287
    if {$when == "later"} {
288
        if {$_reposition == ""} {
289
            set _reposition [after idle [code $this _positionArrows now]]
290
        }
291
        return
292
    } elseif {$when != "now"} {
293
        error "bad option \"$when\": should be now or later"
294
    }
295
 
296
    set _reposition ""
297
 
298
    set bdw [cget -borderwidth]
299
 
300
    #
301
    # Based on the orientation of the arrows, pack them accordingly and
302
    # determine the width and height of the spinners.  For vertical
303
    # orientation, it is really tight in the y direction, so we'll take
304
    # advantage of the highlightthickness.  Horizontal alignment has
305
    # plenty of space vertically, thus we'll ignore the thickness.
306
    #
307
    switch $itk_option(-arroworient) {
308
        vertical {
309
            grid $itk_component(uparrow) -row 0 -column 0
310
            grid $itk_component(downarrow) -row 1 -column 0
311
 
312
            set totalHgt [winfo reqheight $itk_component(entry)]
313
            set spinHgt [expr $totalHgt / 2]
314
            set spinWid [expr round ($spinHgt * 1.6)]
315
        }
316
        horizontal {
317
            grid $itk_component(uparrow) -row 0 -column 0
318
            grid $itk_component(downarrow) -row 0 -column 1
319
 
320
            set spinHgt [expr [winfo reqheight $itk_component(entry)] - \
321
                    (2 * [$itk_component(entry) cget -highlightthickness])]
322
            set spinWid $spinHgt
323
        }
324
        default {
325
            error "bad orientation option \"$itk_option(-arroworient)\",\
326
                   should be horizontal or vertical"
327
        }
328
    }
329
 
330
    #
331
    # Configure the width and height of the spinners minus the borderwidth.
332
    # Next delete the previous spinner polygons and create new ones.
333
    #
334
    $itk_component(uparrow) config \
335
            -height [expr $spinHgt - (2 * $bdw)] \
336
            -width [expr $spinWid - (2 * $bdw)]
337
    $itk_component(uparrow) delete up
338
    $itk_component(uparrow) create polygon \
339
            [expr $spinWid / 2] $bdw \
340
            [expr $spinWid - $bdw - 1] [expr $spinHgt - $bdw -1] \
341
            [expr $bdw + 1] [expr $spinHgt - $bdw - 1] \
342
            -fill $itk_option(-foreground) -tags up
343
 
344
    $itk_component(downarrow) config \
345
            -height [expr $spinHgt - (2 * $bdw)] \
346
            -width [expr $spinWid - (2 * $bdw)]
347
    $itk_component(downarrow) delete down
348
    $itk_component(downarrow) create polygon \
349
            [expr $spinWid / 2] [expr ($spinHgt - $bdw) - 1] \
350
            [expr $bdw + 2] [expr $bdw + 1] \
351
            [expr $spinWid - $bdw - 2] [expr $bdw + 1] \
352
            -fill $itk_option(-foreground) -tags down
353
}
354
 
355
# ------------------------------------------------------------------
356
# PRIVATE METHOD: _pushup
357
#
358
# Up arrow button press event.  Call _doup with repeatdelay.
359
# ------------------------------------------------------------------
360
body iwidgets::Spinner::_pushup {} {
361
    $itk_component(uparrow) config -relief sunken
362
    _doup $itk_option(-repeatdelay)
363
}
364
 
365
# ------------------------------------------------------------------
366
# PRIVATE METHOD: _pushdown
367
#
368
# Down arrow button press event.  Call _dodown with repeatdelay.
369
# ------------------------------------------------------------------
370
body iwidgets::Spinner::_pushdown {} {
371
    $itk_component(downarrow) config -relief sunken
372
    _dodown $itk_option(-repeatdelay)
373
}
374
 
375
# ------------------------------------------------------------------
376
# PRIVATE METHOD: _doup
377
#
378
# Call _up and post to do another one after "rate" milliseconds if
379
# repeatinterval > 0.
380
# ------------------------------------------------------------------
381
body iwidgets::Spinner::_doup {rate} {
382
    _up
383
 
384
    if {$itk_option(-repeatinterval) > 0} {
385
        set _uptimer [after $rate [code $this _doup $itk_option(-repeatinterval)]]
386
    }
387
}
388
 
389
# ------------------------------------------------------------------
390
# PRIVATE METHOD: _dodown
391
#
392
# Call _down and post to do another one after "rate" milliseconds if
393
# repeatinterval > 0.
394
# ------------------------------------------------------------------
395
body iwidgets::Spinner::_dodown {rate} {
396
    _down
397
 
398
    if {$itk_option(-repeatinterval) > 0} {
399
        set _downtimer \
400
                [after $rate [code $this _dodown $itk_option(-repeatinterval)]]
401
    }
402
}
403
 
404
# ------------------------------------------------------------------
405
# PRIVATE METHOD: _relup
406
#
407
# Up arrow button release event.  Cancel pending up timer.
408
# ------------------------------------------------------------------
409
body iwidgets::Spinner::_relup {} {
410
    $itk_component(uparrow) config -relief raised
411
 
412
    if {$_uptimer != ""} {
413
        after cancel $_uptimer
414
        set _uptimer ""
415
    }
416
}
417
 
418
# ------------------------------------------------------------------
419
# PRIVATE METHOD: _reldown
420
#
421
# Up arrow button release event.  Cancel pending down timer.
422
# ------------------------------------------------------------------
423
body iwidgets::Spinner::_reldown {} {
424
    $itk_component(downarrow) config -relief raised
425
 
426
    if {$_downtimer != ""} {
427
        after cancel $_downtimer
428
        set _downtimer ""
429
    }
430
}
431
 
432
# ------------------------------------------------------------------
433
# PRIVATE METHOD: _up
434
#
435
# Up arrow button press event.  Call defined increment command.
436
# ------------------------------------------------------------------
437
body iwidgets::Spinner::_up {} {
438
    uplevel #0 $itk_option(-increment)
439
}
440
 
441
# ------------------------------------------------------------------
442
# PRIVATE METHOD: _down
443
#
444
# Down arrow button press event.  Call defined decrement command.
445
# ------------------------------------------------------------------
446
body iwidgets::Spinner::_down {} {
447
    uplevel #0 $itk_option(-decrement)
448
}

powered by: WebSVN 2.1.0

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