summaryrefslogtreecommitdiff
path: root/itcl/iwidgets/generic/labeledwidget.itk
blob: 2e0faa5d21dfaa72d30fae1da78282111f68e393 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#
# Labeledwidget
# ----------------------------------------------------------------------
# Implements a labeled widget which contains a label and child site.
# The child site is a frame which can filled with any widget via a 
# derived class or though the use of the childsite method.  This class
# was designed to be a general purpose base class for supporting the 
# combination of label widget and a childsite, where a label may be 
# text, bitmap or image.  The options include the ability to position 
# the label around the childsite widget, modify the font and margin, 
# and control the display of the label.  
#
# ----------------------------------------------------------------------
#  AUTHOR: Mark L. Ulferts             EMAIL: mulferts@austin.dsccc.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 Labeledwidget {
    keep -background -cursor -foreground -labelfont
}

# ------------------------------------------------------------------
#                            LABELEDWIDGET
# ------------------------------------------------------------------
itcl::class iwidgets::Labeledwidget {
    inherit itk::Widget

    constructor {args} {}
    destructor {}

    itk_option define -disabledforeground disabledForeground \
	DisabledForeground \#a3a3a3
    itk_option define -labelpos labelPos Position w
    itk_option define -labelmargin labelMargin Margin 2
    itk_option define -labeltext labelText Text {}
    itk_option define -labelvariable labelVariable Variable {}
    itk_option define -labelbitmap labelBitmap Bitmap {}
    itk_option define -labelimage labelImage Image {}
    itk_option define -state state State normal
    itk_option define -sticky sticky Sticky nsew

    public method childsite
    
    private method _positionLabel {{when later}}

    proc alignlabels {args} {}

    protected variable _reposition ""  ;# non-null => _positionLabel pending
}
    
#
# Provide a lowercased access method for the Labeledwidget class.
# 
proc ::iwidgets::labeledwidget {pathName args} {
    uplevel ::iwidgets::Labeledwidget $pathName $args
}

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Labeledwidget::constructor {args} {
    #
    # Create a frame for the childsite widget.
    #
    itk_component add -protected lwchildsite {
	frame $itk_interior.lwchildsite
    } 
    
    #
    # Create label.
    #
    itk_component add label {
	label $itk_interior.label
    } {
	usual
	
	rename -font -labelfont labelFont Font
	ignore -highlightcolor -highlightthickness
    }
    
    #
    # Set the interior to be the childsite for derived classes.
    #
    set itk_interior $itk_component(lwchildsite)

    #
    # Initialize the widget based on the command line options.
    #
    eval itk_initialize $args

    # 
    # When idle, position the label.
    #
    _positionLabel
}

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

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

# ------------------------------------------------------------------
# OPTION: -disabledforeground
#
# Specified the foreground to be used on the label when disabled.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::disabledforeground {}

# ------------------------------------------------------------------
# OPTION: -labelpos
#
# Set the position of the label on the labeled widget.  The margin
# between the label and childsite comes along for the ride.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labelpos {
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -labelmargin
#
# Specifies the distance between the widget and label.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labelmargin {
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -labeltext
#
# Specifies the label text.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labeltext {
    $itk_component(label) configure -text $itk_option(-labeltext)
    
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -labelvariable
#
# Specifies the label text variable.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labelvariable {
    $itk_component(label) configure -textvariable $itk_option(-labelvariable)
    
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -labelbitmap
#
# Specifies the label bitmap.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labelbitmap {
    $itk_component(label) configure -bitmap $itk_option(-labelbitmap)
    
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -labelimage
#
# Specifies the label image.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::labelimage {
    $itk_component(label) configure -image $itk_option(-labelimage)
    
    _positionLabel
}

# ------------------------------------------------------------------
# OPTION: -sticky
#
# Specifies the stickyness of the child site. This option was added
# by James Bonfield (committed by Chad Smith 8/20/01).
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::sticky {
    grid $itk_component(lwchildsite) -sticky $itk_option(-sticky)
}

# ------------------------------------------------------------------
# OPTION: -state
#
# Specifies the state of the label.  
# ------------------------------------------------------------------
itcl::configbody iwidgets::Labeledwidget::state {
    _positionLabel
}

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

# ------------------------------------------------------------------
# METHOD: childsite
#
# Returns the path name of the child site widget.
# ------------------------------------------------------------------
itcl::body iwidgets::Labeledwidget::childsite {} {
    return $itk_component(lwchildsite)
}

# ------------------------------------------------------------------
# PROCEDURE: alignlabels widget ?widget ...?
#
# The alignlabels procedure takes a list of widgets derived from
# the Labeledwidget class and adjusts the label margin to align 
# the labels.
# ------------------------------------------------------------------
itcl::body iwidgets::Labeledwidget::alignlabels {args} {
    update
    set maxLabelWidth 0
    
    #
    # Verify that all the widgets are of type Labeledwidget and 
    # determine the size of the maximum length label string.
    #
    foreach iwid $args {
	set objcmd [itcl::find objects -isa Labeledwidget *::$iwid]

	if {$objcmd == ""} {
	    error "$iwid is not a \"Labeledwidget\""
	}
	
	set csWidth [winfo reqwidth $iwid.lwchildsite]
	set shellWidth [winfo reqwidth $iwid]
	    
	if {($shellWidth - $csWidth) > $maxLabelWidth} {
	    set maxLabelWidth [expr {$shellWidth - $csWidth}]
	}
    }
    
    #
    # Adjust the margins for the labels such that the child sites and
    # labels line up.
    #
    foreach iwid $args {
	set csWidth [winfo reqwidth $iwid.lwchildsite]
	set shellWidth [winfo reqwidth $iwid]
	
	set labelSize [expr {$shellWidth - $csWidth}]
	
	if {$maxLabelWidth > $labelSize} {
	    set objcmd [itcl::find objects -isa Labeledwidget *::$iwid]
	    set dist [expr {$maxLabelWidth - \
		    ($labelSize - [$objcmd cget -labelmargin])}]
	    
	    $objcmd configure -labelmargin $dist 
	}
    }	
}

# ------------------------------------------------------------------
# PROTECTED METHOD: _positionLabel ?when?
#
# Packs the label and label margin.  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.
# ------------------------------------------------------------------
itcl::body iwidgets::Labeledwidget::_positionLabel {{when later}} {
    if {$when == "later"} {
	if {$_reposition == ""} {
	    set _reposition [after idle [itcl::code $this _positionLabel now]]
	}
	return

    } elseif {$when != "now"} {
	error "bad option \"$when\": should be now or later"
    }

    #
    # If we have a label, be it text, bitmap, or image continue.
    #
    if {($itk_option(-labeltext) != {}) || \
	($itk_option(-labelbitmap) != {}) || \
	($itk_option(-labelimage) != {}) || \
	($itk_option(-labelvariable) != {})} {

	#
	# Set the foreground color based on the state.
	#
	if {[info exists itk_option(-state)]} {
	    switch -- $itk_option(-state) {
		disabled {
		    $itk_component(label) configure \
			-foreground $itk_option(-disabledforeground)
		}
		normal {
		    $itk_component(label) configure \
			-foreground $itk_option(-foreground)
		}
	    }
	}

	set parent [winfo parent $itk_component(lwchildsite)]

	#
	# Switch on the label position option.  Using the grid,
	# adjust the row/column setting of the label, margin, and
	# and childsite.  The margin height/width is adjust based
        # on the orientation as well.  Finally, set the weights such
        # that the childsite takes the heat on expansion and shrinkage.
	#
	switch $itk_option(-labelpos) {
	    nw -
	    n -
	    ne {
		grid $itk_component(label) -row 0 -column 0 \
			-sticky $itk_option(-labelpos)
		grid $itk_component(lwchildsite) -row 2 -column 0 \
			-sticky $itk_option(-sticky)
		
		grid rowconfigure $parent 0 -weight 0 -minsize 0
		grid rowconfigure $parent 1 -weight 0 -minsize \
			[winfo pixels $itk_component(label) \
			 $itk_option(-labelmargin)]
		grid rowconfigure $parent 2 -weight 1 -minsize 0

		grid columnconfigure $parent 0 -weight 1 -minsize 0
		grid columnconfigure $parent 1 -weight 0 -minsize 0
		grid columnconfigure $parent 2 -weight 0 -minsize 0
	    }

	    en -
	    e -
	    es {
		grid $itk_component(lwchildsite) -row 0 -column 0 \
			-sticky $itk_option(-sticky)
		grid $itk_component(label) -row 0 -column 2 \
			-sticky $itk_option(-labelpos)
		
		grid rowconfigure $parent 0 -weight 1 -minsize 0
		grid rowconfigure $parent 1 -weight 0 -minsize 0
		grid rowconfigure $parent 2 -weight 0 -minsize 0

		grid columnconfigure $parent 0 -weight 1 -minsize 0
		grid columnconfigure $parent 1 -weight 0 -minsize \
			[winfo pixels $itk_component(label) \
			$itk_option(-labelmargin)]
		grid columnconfigure $parent 2 -weight 0 -minsize 0
	    }
	    
	    se -
	    s -
	    sw {
		grid $itk_component(lwchildsite) -row 0 -column 0 \
			-sticky $itk_option(-sticky)
		grid $itk_component(label) -row 2 -column 0 \
			-sticky $itk_option(-labelpos)
		
		grid rowconfigure $parent 0 -weight 1 -minsize 0
		grid rowconfigure $parent 1 -weight 0 -minsize \
			[winfo pixels $itk_component(label) \
			$itk_option(-labelmargin)]
		grid rowconfigure $parent 2 -weight 0 -minsize 0

		grid columnconfigure $parent 0 -weight 1 -minsize 0
		grid columnconfigure $parent 1 -weight 0 -minsize 0
		grid columnconfigure $parent 2 -weight 0 -minsize 0
	    }
	    
	    wn -
	    w -
	    ws {
		grid $itk_component(lwchildsite) -row 0 -column 2 \
			-sticky $itk_option(-sticky)
		grid $itk_component(label) -row 0 -column 0 \
			-sticky $itk_option(-labelpos)
		
		grid rowconfigure $parent 0 -weight 1 -minsize 0
		grid rowconfigure $parent 1 -weight 0 -minsize 0
		grid rowconfigure $parent 2 -weight 0 -minsize 0

		grid columnconfigure $parent 0 -weight 0 -minsize 0
		grid columnconfigure $parent 1 -weight 0 -minsize \
			[winfo pixels $itk_component(label) \
			$itk_option(-labelmargin)]
		grid columnconfigure $parent 2 -weight 1 -minsize 0
	    }

	    default {
		error "bad labelpos option\
			\"$itk_option(-labelpos)\": should be\
			nw, n, ne, sw, s, se, en, e, es, wn, w, or ws"
	    }
	}

    #
    # Else, neither the  label text, bitmap, or image have a value, so
    # forget them so they don't appear and manage only the childsite.
    #
    } else {
	grid forget $itk_component(label)

	grid $itk_component(lwchildsite) -row 0 -column 0 -sticky $itk_option(-sticky)

	set parent [winfo parent $itk_component(lwchildsite)]

	grid rowconfigure $parent 0 -weight 1 -minsize 0
	grid rowconfigure $parent 1 -weight 0 -minsize 0
	grid rowconfigure $parent 2 -weight 0 -minsize 0
	grid columnconfigure $parent 0 -weight 1 -minsize 0
	grid columnconfigure $parent 1 -weight 0 -minsize 0
	grid columnconfigure $parent 2 -weight 0 -minsize 0
    }

    #
    # Reset the resposition flag.
    #
    set _reposition ""
}