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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [itcl/] [iwidgets3.0.0/] [generic/] [labeledwidget.itk] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#
2
# Labeledwidget
3
# ----------------------------------------------------------------------
4
# Implements a labeled widget which contains a label and child site.
5
# The child site is a frame which can filled with any widget via a
6
# derived class or though the use of the childsite method.  This class
7
# was designed to be a general purpose base class for supporting the
8
# combination of label widget and a childsite, where a label may be
9
# text, bitmap or image.  The options include the ability to position
10
# the label around the childsite widget, modify the font and margin,
11
# and control the display of the label.
12
#
13
# ----------------------------------------------------------------------
14
#  AUTHOR: Mark L. Ulferts             EMAIL: mulferts@austin.dsccc.com
15
#
16
#  @(#) $Id: labeledwidget.itk,v 1.1.1.1 2002-01-16 10:24:50 markom Exp $
17
# ----------------------------------------------------------------------
18
#            Copyright (c) 1995 DSC Technologies Corporation
19
# ======================================================================
20
# Permission to use, copy, modify, distribute and license this software
21
# and its documentation for any purpose, and without fee or written
22
# agreement with DSC, is hereby granted, provided that the above copyright
23
# notice appears in all copies and that both the copyright notice and
24
# warranty disclaimer below appear in supporting documentation, and that
25
# the names of DSC Technologies Corporation or DSC Communications
26
# Corporation not be used in advertising or publicity pertaining to the
27
# software without specific, written prior permission.
28
#
29
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
30
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
31
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
32
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
33
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL
34
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
35
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
36
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
37
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
38
# SOFTWARE.
39
# ======================================================================
40
 
41
#
42
# Usual options.
43
#
44
itk::usual Labeledwidget {
45
    keep -background -cursor -foreground -labelfont
46
}
47
 
48
# ------------------------------------------------------------------
49
#                            LABELEDWIDGET
50
# ------------------------------------------------------------------
51
class iwidgets::Labeledwidget {
52
    inherit itk::Widget
53
 
54
    constructor {args} {}
55
    destructor {}
56
 
57
    itk_option define -disabledforeground disabledForeground \
58
        DisabledForeground \#a3a3a3
59
    itk_option define -labelpos labelPos Position w
60
    itk_option define -labelmargin labelMargin Margin 2
61
    itk_option define -labeltext labelText Text {}
62
    itk_option define -labelvariable labelVariable Variable {}
63
    itk_option define -labelbitmap labelBitmap Bitmap {}
64
    itk_option define -labelimage labelImage Image {}
65
    itk_option define -state state State normal
66
 
67
    public method childsite
68
 
69
    protected method _positionLabel {{when later}}
70
 
71
    proc alignlabels {args} {}
72
 
73
    protected variable _reposition ""  ;# non-null => _positionLabel pending
74
}
75
 
76
#
77
# Provide a lowercased access method for the Labeledwidget class.
78
#
79
proc ::iwidgets::labeledwidget {pathName args} {
80
    uplevel ::iwidgets::Labeledwidget $pathName $args
81
}
82
 
83
# ------------------------------------------------------------------
84
#                        CONSTRUCTOR
85
# ------------------------------------------------------------------
86
body iwidgets::Labeledwidget::constructor {args} {
87
    #
88
    # Create a frame for the childsite widget.
89
    #
90
    itk_component add -protected lwchildsite {
91
        frame $itk_interior.lwchildsite
92
    }
93
 
94
    #
95
    # Create label.
96
    #
97
    itk_component add label {
98
        label $itk_interior.label
99
    } {
100
        usual
101
 
102
        rename -font -labelfont labelFont Font
103
        ignore -highlightcolor -highlightthickness
104
    }
105
 
106
    #
107
    # Set the interior to be the childsite for derived classes.
108
    #
109
    set itk_interior $itk_component(lwchildsite)
110
 
111
    #
112
    # Initialize the widget based on the command line options.
113
    #
114
    eval itk_initialize $args
115
 
116
    #
117
    # When idle, position the label.
118
    #
119
    _positionLabel
120
}
121
 
122
# ------------------------------------------------------------------
123
#                           DESTURCTOR
124
# ------------------------------------------------------------------
125
body iwidgets::Labeledwidget::destructor {} {
126
    if {$_reposition != ""} {after cancel $_reposition}
127
}
128
 
129
# ------------------------------------------------------------------
130
#                             OPTIONS
131
# ------------------------------------------------------------------
132
 
133
# ------------------------------------------------------------------
134
# OPTION: -disabledforeground
135
#
136
# Specified the foreground to be used on the label when disabled.
137
# ------------------------------------------------------------------
138
configbody iwidgets::Labeledwidget::disabledforeground {}
139
 
140
# ------------------------------------------------------------------
141
# OPTION: -labelpos
142
#
143
# Set the position of the label on the labeled widget.  The margin
144
# between the label and childsite comes along for the ride.
145
# ------------------------------------------------------------------
146
configbody iwidgets::Labeledwidget::labelpos {
147
    _positionLabel
148
}
149
 
150
# ------------------------------------------------------------------
151
# OPTION: -labelmargin
152
#
153
# Specifies the distance between the widget and label.
154
# ------------------------------------------------------------------
155
configbody iwidgets::Labeledwidget::labelmargin {
156
    _positionLabel
157
}
158
 
159
# ------------------------------------------------------------------
160
# OPTION: -labeltext
161
#
162
# Specifies the label text.
163
# ------------------------------------------------------------------
164
configbody iwidgets::Labeledwidget::labeltext {
165
    $itk_component(label) configure -text $itk_option(-labeltext)
166
 
167
    _positionLabel
168
}
169
 
170
# ------------------------------------------------------------------
171
# OPTION: -labelvariable
172
#
173
# Specifies the label text variable.
174
# ------------------------------------------------------------------
175
configbody iwidgets::Labeledwidget::labelvariable {
176
    $itk_component(label) configure -textvariable $itk_option(-labelvariable)
177
 
178
    uplevel [list trace variable \
179
                 $itk_option(-labelvariable) w [code _positionLabel]]
180
 
181
    _positionLabel
182
}
183
 
184
# ------------------------------------------------------------------
185
# OPTION: -labelbitmap
186
#
187
# Specifies the label bitmap.
188
# ------------------------------------------------------------------
189
configbody iwidgets::Labeledwidget::labelbitmap {
190
    $itk_component(label) configure -bitmap $itk_option(-labelbitmap)
191
 
192
    _positionLabel
193
}
194
 
195
# ------------------------------------------------------------------
196
# OPTION: -labelimage
197
#
198
# Specifies the label image.
199
# ------------------------------------------------------------------
200
configbody iwidgets::Labeledwidget::labelimage {
201
    $itk_component(label) configure -image $itk_option(-labelimage)
202
 
203
    _positionLabel
204
}
205
 
206
# ------------------------------------------------------------------
207
# OPTION: -state
208
#
209
# Specifies the state of the label.
210
# ------------------------------------------------------------------
211
configbody iwidgets::Labeledwidget::state {
212
    _positionLabel
213
}
214
 
215
# ------------------------------------------------------------------
216
#                            METHODS
217
# ------------------------------------------------------------------
218
 
219
# ------------------------------------------------------------------
220
# METHOD: childsite
221
#
222
# Returns the path name of the child site widget.
223
# ------------------------------------------------------------------
224
body iwidgets::Labeledwidget::childsite {} {
225
    return $itk_component(lwchildsite)
226
}
227
 
228
# ------------------------------------------------------------------
229
# PROCEDURE: alignlabels widget ?widget ...?
230
#
231
# The alignlabels procedure takes a list of widgets derived from
232
# the Labeledwidget class and adjusts the label margin to align
233
# the labels.
234
# ------------------------------------------------------------------
235
body iwidgets::Labeledwidget::alignlabels {args} {
236
    update
237
    set maxLabelWidth 0
238
 
239
    #
240
    # Verify that all the widgets are of type Labeledwidget and
241
    # determine the size of the maximum length label string.
242
    #
243
    foreach iwid $args {
244
        set objcmd [itcl::find objects -isa Labeledwidget *::$iwid]
245
 
246
        if {$objcmd == ""} {
247
            error "$iwid is not a \"Labeledwidget\""
248
        }
249
 
250
        set csWidth [winfo reqwidth $iwid.lwchildsite]
251
        set shellWidth [winfo reqwidth $iwid]
252
 
253
        if {[expr $shellWidth - $csWidth] > $maxLabelWidth} {
254
            set maxLabelWidth [expr $shellWidth - $csWidth]
255
        }
256
    }
257
 
258
    #
259
    # Adjust the margins for the labels such that the child sites and
260
    # labels line up.
261
    #
262
    foreach iwid $args {
263
        set csWidth [winfo reqwidth $iwid.lwchildsite]
264
        set shellWidth [winfo reqwidth $iwid]
265
 
266
        set labelSize [expr $shellWidth - $csWidth]
267
 
268
        if {$maxLabelWidth > $labelSize} {
269
            set objcmd [itcl::find objects -isa Labeledwidget *::$iwid]
270
            set dist [expr $maxLabelWidth - \
271
                    ($labelSize - [$objcmd cget -labelmargin])]
272
 
273
            $objcmd configure -labelmargin $dist
274
        }
275
    }
276
}
277
 
278
# ------------------------------------------------------------------
279
# PROTECTED METHOD: _positionLabel ?when?
280
#
281
# Packs the label and label margin.  If "when" is "now", the
282
# change is applied immediately.  If it is "later" or it is not
283
# specified, then the change is applied later, when the application
284
# is idle.
285
# ------------------------------------------------------------------
286
body iwidgets::Labeledwidget::_positionLabel {{when later}} {
287
    if {$when == "later"} {
288
        if {$_reposition == ""} {
289
            set _reposition [after idle [code $this _positionLabel now]]
290
        }
291
        return
292
 
293
    } elseif {$when != "now"} {
294
        error "bad option \"$when\": should be now or later"
295
    }
296
 
297
    #
298
    # If we have a label, be it text, bitmap, or image continue.
299
    #
300
    if {($itk_option(-labeltext) != {}) || \
301
        ($itk_option(-labelbitmap) != {}) || \
302
        ($itk_option(-labelimage) != {}) || \
303
        ($itk_option(-labelvariable) != {})} {
304
 
305
        #
306
        # Set the foreground color based on the state.
307
        #
308
        if {[info exists itk_option(-state)]} {
309
            switch -- $itk_option(-state) {
310
                disabled {
311
                    $itk_component(label) configure \
312
                        -foreground $itk_option(-disabledforeground)
313
                }
314
                normal {
315
                    $itk_component(label) configure \
316
                        -foreground $itk_option(-foreground)
317
                }
318
            }
319
        }
320
 
321
        set parent [winfo parent $itk_component(lwchildsite)]
322
 
323
        #
324
        # Switch on the label position option.  Using the grid,
325
        # adjust the row/column setting of the label, margin, and
326
        # and childsite.  The margin height/width is adjust based
327
        # on the orientation as well.  Finally, set the weights such
328
        # that the childsite takes the heat on expansion and shrinkage.
329
        #
330
        switch $itk_option(-labelpos) {
331
            nw -
332
            n -
333
            ne {
334
                grid $itk_component(label) -row 0 -column 0 \
335
                        -sticky $itk_option(-labelpos)
336
                grid $itk_component(lwchildsite) -row 2 -column 0 \
337
                        -sticky nsew
338
 
339
                grid rowconfigure $parent 0 -weight 0 -minsize 0
340
                grid rowconfigure $parent 1 -weight 0 -minsize \
341
                        [winfo pixels $itk_component(label) \
342
                         $itk_option(-labelmargin)]
343
                grid rowconfigure $parent 2 -weight 1 -minsize 0
344
 
345
                grid columnconfigure $parent 0 -weight 1 -minsize 0
346
                grid columnconfigure $parent 1 -weight 0 -minsize 0
347
                grid columnconfigure $parent 2 -weight 0 -minsize 0
348
            }
349
 
350
            en -
351
            e -
352
            es {
353
                grid $itk_component(lwchildsite) -row 0 -column 0 \
354
                        -sticky nsew
355
                grid $itk_component(label) -row 0 -column 2 \
356
                        -sticky $itk_option(-labelpos)
357
 
358
                grid rowconfigure $parent 0 -weight 1 -minsize 0
359
                grid rowconfigure $parent 1 -weight 0 -minsize 0
360
                grid rowconfigure $parent 2 -weight 0 -minsize 0
361
 
362
                grid columnconfigure $parent 0 -weight 1 -minsize 0
363
                grid columnconfigure $parent 1 -weight 0 -minsize \
364
                        [winfo pixels $itk_component(label) \
365
                        $itk_option(-labelmargin)]
366
                grid columnconfigure $parent 2 -weight 0 -minsize 0
367
            }
368
 
369
            se -
370
            s -
371
            sw {
372
                grid $itk_component(lwchildsite) -row 0 -column 0 \
373
                        -sticky nsew
374
                grid $itk_component(label) -row 2 -column 0 \
375
                        -sticky $itk_option(-labelpos)
376
 
377
                grid rowconfigure $parent 0 -weight 1 -minsize 0
378
                grid rowconfigure $parent 1 -weight 0 -minsize \
379
                        [winfo pixels $itk_component(label) \
380
                        $itk_option(-labelmargin)]
381
                grid rowconfigure $parent 2 -weight 0 -minsize 0
382
 
383
                grid columnconfigure $parent 0 -weight 1 -minsize 0
384
                grid columnconfigure $parent 1 -weight 0 -minsize 0
385
                grid columnconfigure $parent 2 -weight 0 -minsize 0
386
            }
387
 
388
            wn -
389
            w -
390
            ws {
391
                grid $itk_component(lwchildsite) -row 0 -column 2 \
392
                        -sticky nsew
393
                grid $itk_component(label) -row 0 -column 0 \
394
                        -sticky $itk_option(-labelpos)
395
 
396
                grid rowconfigure $parent 0 -weight 1 -minsize 0
397
                grid rowconfigure $parent 1 -weight 0 -minsize 0
398
                grid rowconfigure $parent 2 -weight 0 -minsize 0
399
 
400
                grid columnconfigure $parent 0 -weight 0 -minsize 0
401
                grid columnconfigure $parent 1 -weight 0 -minsize \
402
                        [winfo pixels $itk_component(label) \
403
                        $itk_option(-labelmargin)]
404
                grid columnconfigure $parent 2 -weight 1 -minsize 0
405
            }
406
 
407
            default {
408
                error "bad labelpos option\
409
                        \"$itk_option(-labelpos)\": should be\
410
                        nw, n, ne, sw, s, se, en, e, es, wn, w, or ws"
411
            }
412
        }
413
 
414
    #
415
    # Else, neither the  label text, bitmap, or image have a value, so
416
    # forget them so they don't appear and manage only the childsite.
417
    #
418
    } else {
419
        grid forget $itk_component(label)
420
 
421
        grid $itk_component(lwchildsite) -row 0 -column 0 -sticky nsew
422
 
423
        set parent [winfo parent $itk_component(lwchildsite)]
424
 
425
        grid rowconfigure $parent 0 -weight 1 -minsize 0
426
        grid rowconfigure $parent 1 -weight 0 -minsize 0
427
        grid rowconfigure $parent 2 -weight 0 -minsize 0
428
        grid columnconfigure $parent 0 -weight 1 -minsize 0
429
        grid columnconfigure $parent 1 -weight 0 -minsize 0
430
        grid columnconfigure $parent 2 -weight 0 -minsize 0
431
    }
432
 
433
    #
434
    # Reset the resposition flag.
435
    #
436
    set _reposition ""
437
}

powered by: WebSVN 2.1.0

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