1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: dropdown - base class for drop-down panels |
---|
3 | # |
---|
4 | # This is the base class for a family of drop-down widget panels. |
---|
5 | # They might be used, for example, to build the drop-down list for |
---|
6 | # a combobox. |
---|
7 | # |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Michael McLennan, Purdue University |
---|
10 | # Copyright (c) 2004-2005 |
---|
11 | # Purdue Research Foundation, West Lafayette, IN |
---|
12 | # ====================================================================== |
---|
13 | package require Itk |
---|
14 | |
---|
15 | option add *Dropdown.textBackground white widgetDefault |
---|
16 | option add *Dropdown.outline black widgetDefault |
---|
17 | option add *Dropdown.borderwidth 1 widgetDefault |
---|
18 | option add *Dropdown.font -*-helvetica-medium-r-normal-*-*-120-* widgetDefault |
---|
19 | |
---|
20 | itcl::class Rappture::Dropdown { |
---|
21 | inherit itk::Toplevel |
---|
22 | |
---|
23 | itk_option define -outline outline Outline "" |
---|
24 | itk_option define -postcommand postCommand PostCommand "" |
---|
25 | itk_option define -unpostcommand unpostCommand UnpostCommand "" |
---|
26 | |
---|
27 | constructor {args} { # defined below } |
---|
28 | |
---|
29 | public method post {where args} |
---|
30 | public method unpost {} |
---|
31 | |
---|
32 | protected method _adjust {{widget ""}} |
---|
33 | |
---|
34 | public proc outside {w x y} |
---|
35 | |
---|
36 | bind RapptureDropdown <ButtonPress> \ |
---|
37 | {if {[Rappture::Dropdown::outside %W %X %Y]} {%W unpost}} |
---|
38 | } |
---|
39 | |
---|
40 | itk::usual Dropdown { |
---|
41 | keep -background -outline -cursor -font |
---|
42 | } |
---|
43 | |
---|
44 | # ---------------------------------------------------------------------- |
---|
45 | # CONSTRUCTOR |
---|
46 | # ---------------------------------------------------------------------- |
---|
47 | itcl::body Rappture::Dropdown::constructor {args} { |
---|
48 | wm overrideredirect $itk_component(hull) yes |
---|
49 | wm withdraw $itk_component(hull) |
---|
50 | |
---|
51 | component hull configure -borderwidth 1 -background black |
---|
52 | itk_option remove hull.background hull.borderwidth |
---|
53 | |
---|
54 | # add bindings to release the grab |
---|
55 | set btags [bindtags $itk_component(hull)] |
---|
56 | bindtags $itk_component(hull) [linsert $btags 1 RapptureDropdown] |
---|
57 | |
---|
58 | eval itk_initialize $args |
---|
59 | } |
---|
60 | |
---|
61 | # ---------------------------------------------------------------------- |
---|
62 | # USAGE: post @<x>,<y> |
---|
63 | # USAGE: post <widget> <justify> |
---|
64 | # |
---|
65 | # Clients use this to pop up the dropdown on the screen. The position |
---|
66 | # should be either a specific location "@x,y", or a <widget> and its |
---|
67 | # justification. |
---|
68 | # ---------------------------------------------------------------------- |
---|
69 | itcl::body Rappture::Dropdown::post {where args} { |
---|
70 | set owner [expr {([winfo exists $where]) ? $where : ""}] |
---|
71 | _adjust $owner ;# make sure contents are up-to-date |
---|
72 | update idletasks ;# fix size info |
---|
73 | |
---|
74 | if {[string length $itk_option(-postcommand)] > 0} { |
---|
75 | set cmd [list uplevel #0 $itk_option(-postcommand)] |
---|
76 | if {[catch $cmd result]} { |
---|
77 | bgerror $result |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | set w [winfo width $itk_component(hull)] |
---|
82 | set h [winfo height $itk_component(hull)] |
---|
83 | set sw [winfo screenwidth $itk_component(hull)] |
---|
84 | set sh [winfo screenwidth $itk_component(hull)] |
---|
85 | |
---|
86 | if {[regexp {^@([0-9]+),([0-9]+)$} $where match x y]} { |
---|
87 | set xpos $x |
---|
88 | set ypos $y |
---|
89 | } elseif {[winfo exists $where]} { |
---|
90 | set x0 [winfo rootx $where] |
---|
91 | switch -- $args { |
---|
92 | left { set xpos $x0 } |
---|
93 | right { set xpos [expr {$x0 + [winfo width $where] - $sw}] } |
---|
94 | default { |
---|
95 | error "bad option \"$args\": should be left, right" |
---|
96 | } |
---|
97 | } |
---|
98 | set ypos [expr {[winfo rooty $where]+[winfo height $where]}] |
---|
99 | } else { |
---|
100 | error "bad position \"$where\": should be widget name or @x,y" |
---|
101 | } |
---|
102 | |
---|
103 | # make sure the dropdown doesn't go off screen |
---|
104 | if {$xpos > 0} { |
---|
105 | # left-justified positions |
---|
106 | if {$xpos + $w > $sw} { |
---|
107 | set xpos [expr {$sw-$w}] |
---|
108 | if {$xpos < 0} { set xpos 0 } |
---|
109 | } |
---|
110 | set xpos "+$xpos" |
---|
111 | } else { |
---|
112 | # right-justified positions |
---|
113 | if {$xpos - $w < -$sw} { |
---|
114 | set xpos [expr {-$sw+$w}] |
---|
115 | if {$xpos > 0} { set xpos -1 } |
---|
116 | } |
---|
117 | } |
---|
118 | if {$ypos + $h > $sh} { |
---|
119 | set ypos [expr {$sh-$h}] |
---|
120 | if {$ypos < 0} { set ypos 0 } |
---|
121 | } |
---|
122 | |
---|
123 | # post the dropdown on the screen |
---|
124 | wm geometry $itk_component(hull) "$xpos+$ypos" |
---|
125 | update |
---|
126 | |
---|
127 | wm deiconify $itk_component(hull) |
---|
128 | raise $itk_component(hull) |
---|
129 | |
---|
130 | # grab the mouse pointer |
---|
131 | update |
---|
132 | while {[catch {grab set -global $itk_component(hull)}]} { |
---|
133 | after 100 |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | # ---------------------------------------------------------------------- |
---|
138 | # USAGE: unpost |
---|
139 | # |
---|
140 | # Takes down the dropdown, if it is showing on the screen. |
---|
141 | # ---------------------------------------------------------------------- |
---|
142 | itcl::body Rappture::Dropdown::unpost {} { |
---|
143 | grab release $itk_component(hull) |
---|
144 | wm withdraw $itk_component(hull) |
---|
145 | |
---|
146 | if {[string length $itk_option(-unpostcommand)] > 0} { |
---|
147 | set cmd [list uplevel #0 $itk_option(-unpostcommand)] |
---|
148 | if {[catch $cmd result]} { |
---|
149 | bgerror $result |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | # ---------------------------------------------------------------------- |
---|
155 | # USAGE: _adjust |
---|
156 | # |
---|
157 | # This method is invoked each time the dropdown is posted to adjust |
---|
158 | # its size and contents. |
---|
159 | # ---------------------------------------------------------------------- |
---|
160 | itcl::body Rappture::Dropdown::_adjust {{widget ""}} { |
---|
161 | # derived classes redefine this to do something useful |
---|
162 | } |
---|
163 | |
---|
164 | # ---------------------------------------------------------------------- |
---|
165 | # USAGE: outside <widget> <x> <y> |
---|
166 | # |
---|
167 | # Checks to see if the root coordinate <x>,<y> is outside of the |
---|
168 | # area for the <widget>. Returns 1 if so, and 0 otherwise. |
---|
169 | # ---------------------------------------------------------------------- |
---|
170 | itcl::body Rappture::Dropdown::outside {widget x y} { |
---|
171 | return [expr {$x < [winfo rootx $widget] |
---|
172 | || $x > [winfo rootx $widget]+[winfo width $widget] |
---|
173 | || $y < [winfo rooty $widget] |
---|
174 | || $y > [winfo rooty $widget]+[winfo height $widget]}] |
---|
175 | } |
---|
176 | |
---|
177 | # ---------------------------------------------------------------------- |
---|
178 | # CONFIGURATION OPTION: -outline |
---|
179 | # ---------------------------------------------------------------------- |
---|
180 | itcl::configbody Rappture::Dropdown::outline { |
---|
181 | component hull configure -background $itk_option(-outline) |
---|
182 | } |
---|