summaryrefslogtreecommitdiff
path: root/itcl/iwidgets3.0.0/generic/pushbutton.itk
blob: 5961458a89bc6e7a197c9fd60d0b3e6a05a68275 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#
# Pushbutton
# ----------------------------------------------------------------------
# Implements a Motif-like Pushbutton with an optional default ring.
#
# WISH LIST:
#    1)  Allow bitmaps and text on the same button face (Tk limitation).
#    2)  provide arm and disarm bitmaps.
#
# ----------------------------------------------------------------------
#   AUTHOR:  Mark L. Ulferts        EMAIL: mulferts@austin.dsccc.com
#            Bret A. Schuhmacher    EMAIL: bas@wn.com
#
#   @(#) $Id$
# ----------------------------------------------------------------------
#            Copyright (c) 1995 DSC Technologies Corporation
# ======================================================================
# Permission to use, copy, modify, distribute and license this software 
# and its documentation for any purpose, and without fee or written 
# agreement with DSC, is hereby granted, provided that the above copyright 
# notice appears in all copies and that both the copyright notice and 
# warranty disclaimer below appear in supporting documentation, and that 
# the names of DSC Technologies Corporation or DSC Communications 
# Corporation not be used in advertising or publicity pertaining to the 
# software without specific, written prior permission.
# 
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
# SOFTWARE.
# ======================================================================

#
# Usual options.
#
itk::usual Pushbutton {
    keep -activebackground -activeforeground -background -borderwidth \
	 -cursor -disabledforeground -font -foreground -highlightbackground \
	 -highlightcolor -highlightthickness 
}

# ------------------------------------------------------------------
#                            PUSHBUTTON
# ------------------------------------------------------------------
class iwidgets::Pushbutton {
    inherit itk::Widget

    constructor {args} {}
    destructor {}

    itk_option define -padx padX Pad 11
    itk_option define -pady padY Pad 4
    itk_option define -font font Font \
	    -Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*
    itk_option define -text text Text {}
    itk_option define -bitmap bitmap Bitmap {}
    itk_option define -image image Image {}
    itk_option define -highlightthickness highlightThickness \
	    HighlightThickness 2
    itk_option define -borderwidth borderWidth BorderWidth 2
    itk_option define -defaultring defaultRing DefaultRing 0
    itk_option define -defaultringpad defaultRingPad Pad 4
    itk_option define -height height Height 0
    itk_option define -width width Width 0
    itk_option define -takefocus takeFocus TakeFocus 0

    public method flash {} 
    public method invoke {} 

    protected method _relayout {{when later}} 
    protected variable _reposition ""  ;# non-null => _relayout pending
}

#
# Provide a lowercased access method for the Pushbutton class.
# 
proc ::iwidgets::pushbutton {pathName args} {
    uplevel ::iwidgets::Pushbutton $pathName $args
}

#
# Use option database to override default resources of base classes.
#
option add *Pushbutton.borderWidth 2 widgetDefault

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Pushbutton::constructor {args} {
    #
    # Reconfigure the hull to act as the outer sunken ring of
    # the pushbutton, complete with focus ring.
    #
    itk_option add hull.borderwidth hull.relief
    itk_option add hull.highlightcolor
    itk_option add hull.highlightbackground
    
    component hull configure \
	    -borderwidth [$this cget -borderwidth]
    
    pack propagate $itk_component(hull) no

    itk_component add pushbutton {
	button $itk_component(hull).pushbutton \
    } {
        usual
	keep -underline -wraplength -state -command 
    }
    pack $itk_component(pushbutton) -expand 1 -fill both 
    
    #
    # Initialize the widget based on the command line options.
    #
    eval itk_initialize $args

    #
    # Layout the pushbutton.
    #
    _relayout
}

# ------------------------------------------------------------------
#                           DESTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Pushbutton::destructor {} {
    if {$_reposition != ""} {after cancel $_reposition}
}

# ------------------------------------------------------------------
#                             OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -padx
#
# Specifies the extra space surrounding the label in the x direction.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::padx {
    $itk_component(pushbutton) configure -padx $itk_option(-padx)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -pady
#
# Specifies the extra space surrounding the label in the y direction.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::pady {
    $itk_component(pushbutton) configure -pady $itk_option(-pady)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -font
#
# Specifies the label font.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::font {
    $itk_component(pushbutton) configure -font $itk_option(-font)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -text
#
# Specifies the label text.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::text {
    $itk_component(pushbutton) configure -text $itk_option(-text)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -bitmap
#
# Specifies the label bitmap.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::bitmap {
    $itk_component(pushbutton) configure -bitmap $itk_option(-bitmap)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -image
#
# Specifies the label image.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::image {
    $itk_component(pushbutton) configure -image $itk_option(-image)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -highlightthickness
#
# Specifies the thickness of the highlight ring.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::highlightthickness {
    $itk_component(pushbutton) configure \
	    -highlightthickness $itk_option(-highlightthickness)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -borderwidth
#
# Specifies the width of the relief border.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::borderwidth {
    $itk_component(pushbutton) configure -borderwidth $itk_option(-borderwidth)

    _relayout
}

# ------------------------------------------------------------------
# OPTION: -defaultring
#
# Boolean describing whether the button displays its default ring.  
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::defaultring {
    _relayout
}

# ------------------------------------------------------------------
# OPTION: -defaultringpad
#
# The size of the padded default ring around the button.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::defaultringpad {
    pack $itk_component(pushbutton) \
	    -padx $itk_option(-defaultringpad) \
	    -pady $itk_option(-defaultringpad)
}

# ------------------------------------------------------------------
# OPTION: -height
#
# Specifies the height of the button inclusive of any default ring.
# A value of zero lets the push button determine the height based
# on the requested height plus highlightring and defaultringpad.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::height {
    _relayout
}

# ------------------------------------------------------------------
# OPTION: -width
#
# Specifies the width of the button inclusive of any default ring.
# A value of zero lets the push button determine the width based
# on the requested width plus highlightring and defaultringpad.
# ------------------------------------------------------------------
configbody iwidgets::Pushbutton::width {
    _relayout
}

# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# METHOD: flash
#
# Thin wrap of standard button widget flash method.
# ------------------------------------------------------------------
body iwidgets::Pushbutton::flash {} {
    $itk_component(pushbutton) flash
}

# ------------------------------------------------------------------
# METHOD: invoke
#
# Thin wrap of standard button widget invoke method.
# ------------------------------------------------------------------
body iwidgets::Pushbutton::invoke {} {
    $itk_component(pushbutton) invoke
}

# ------------------------------------------------------------------
# PROTECTED METHOD: _relayout ?when?
#
# Adjust the width and height of the Pushbutton to accomadate all the
# current options settings.  Add back in the highlightthickness to 
# the button such that the correct reqwidth and reqheight are computed.  
# Set the width and height based on the reqwidth/reqheight, 
# highlightthickness, and ringpad.  Finally, configure the defaultring
# properly. If "when" is "now", the change is applied immediately.  If 
# it is "later" or it is not specified, then the change is applied later,
# when the application is idle.
# ------------------------------------------------------------------
body iwidgets::Pushbutton::_relayout {{when later}} {
    if {$when == "later"} {
	if {$_reposition == ""} {
	    set _reposition [after idle [code $this _relayout now]]
	}
	return
    } elseif {$when != "now"} {
	error "bad option \"$when\": should be now or later"
    }

    set _reposition ""

    if {$itk_option(-width) == 0} {
	set w [expr [winfo reqwidth $itk_component(pushbutton)] \
		+ 2 * $itk_option(-highlightthickness) \
		+ 2 * $itk_option(-borderwidth) \
		+ 2 * $itk_option(-defaultringpad)]
    } else {
	set w $itk_option(-width)
    }
    
    if {$itk_option(-height) == 0} {
	set h [expr [winfo reqheight $itk_component(pushbutton)] \
		+ 2 * $itk_option(-highlightthickness) \
		+ 2 * $itk_option(-borderwidth) \
		+ 2 * $itk_option(-defaultringpad)]
    } else {
	set h $itk_option(-height)
    }
    
    component hull configure -width $w -height $h
    
    if {$itk_option(-defaultring)} {
	component hull configure -relief sunken \
		-highlightthickness [$this cget -highlightthickness] \
		-takefocus 1

	configure -takefocus 1
	
	component pushbutton configure \
		-highlightthickness 0 -takefocus 0
	
    } else {
	component hull configure -relief flat \
		-highlightthickness 0 -takefocus 0
	
	component pushbutton configure \
		-highlightthickness [$this cget -highlightthickness] \
		-takefocus 1

	configure -takefocus 0
    }
}