Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 1 | # Module 'Buttons' |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 2 | |
| 3 | |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 4 | # Import module 'rect' renamed as '_rect' to avoid exporting it on |
| 5 | # 'from Buttons import *' |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 6 | # |
| 7 | import rect |
| 8 | _rect = rect |
| 9 | del rect |
| 10 | |
| 11 | |
| 12 | # Field indices in mouse event detail |
| 13 | # |
| 14 | _HV = 0 |
| 15 | _CLICKS = 1 |
| 16 | _BUTTON = 2 |
| 17 | _MASK = 3 |
| 18 | |
| 19 | |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 20 | # LabelAppearance provides defaults for all appearance methods. |
| 21 | # selected state not visible |
| 22 | # disabled --> crossed out |
| 23 | # hilited --> inverted |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 24 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 25 | class LabelAppearance: |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 26 | # |
| 27 | # Initialization |
| 28 | # |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 29 | def init_appearance(self): |
| 30 | self.bounds = _rect.empty |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 31 | self.enabled = 1 |
| 32 | self.hilited = 0 |
| 33 | self.selected = 0 |
| 34 | self.text = '' |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 35 | # |
| 36 | # Size enquiry |
| 37 | # |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 38 | def getminsize(self, m, (width, height)): |
Guido van Rossum | 1e5ad4e | 1991-08-16 13:04:40 +0000 | [diff] [blame] | 39 | width = max(width, m.textwidth(self.text) + 6) |
| 40 | height = max(height, m.lineheight() + 6) |
| 41 | return width, height |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 42 | # |
| 43 | def getbounds(self): |
| 44 | return self.bounds |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 45 | # |
| 46 | # Changing the parameters |
| 47 | # |
| 48 | def settext(self, text): |
| 49 | self.text = text |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 50 | if self.bounds <> _rect.empty: |
| 51 | self.recalctextpos() |
| 52 | self.redraw() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 53 | # |
| 54 | def setbounds(self, bounds): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 55 | self.bounds = bounds |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 56 | if self.bounds <> _rect.empty: |
| 57 | self.recalc() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 58 | # |
Guido van Rossum | efba63b | 1991-04-07 13:32:19 +0000 | [diff] [blame] | 59 | def realize(self): |
| 60 | pass |
| 61 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 62 | # Changing the state bits |
| 63 | # |
| 64 | def enable(self, flag): |
| 65 | if flag <> self.enabled: |
| 66 | self.enabled = flag |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 67 | if self.bounds <> _rect.empty: |
| 68 | self.flipenable(self.parent.begindrawing()) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 69 | # |
| 70 | def hilite(self, flag): |
| 71 | if flag <> self.hilited: |
| 72 | self.hilited = flag |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 73 | if self.bounds <> _rect.empty: |
| 74 | self.fliphilite(self.parent.begindrawing()) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 75 | # |
| 76 | def select(self, flag): |
| 77 | if flag <> self.selected: |
| 78 | self.selected = flag |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 79 | if self.bounds <> _rect.empty: |
| 80 | self.redraw() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 81 | # |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 82 | # Recalculate the box bounds and text position. |
| 83 | # This can be overridden by buttons that draw different boxes |
| 84 | # or want their text in a different position. |
| 85 | # |
| 86 | def recalc(self): |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 87 | if self.bounds <> _rect.empty: |
| 88 | self.recalcbounds() |
| 89 | self.recalctextpos() |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 90 | # |
| 91 | def recalcbounds(self): |
| 92 | self.hilitebounds = _rect.inset(self.bounds, (3, 3)) |
| 93 | self.crossbounds = self.bounds |
| 94 | # |
| 95 | def recalctextpos(self): |
| 96 | (left, top), (right, bottom) = self.bounds |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 97 | m = self.parent.beginmeasuring() |
| 98 | h = (left + right - m.textwidth(self.text)) / 2 |
| 99 | v = (top + bottom - m.lineheight()) / 2 |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 100 | self.textpos = h, v |
| 101 | # |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 102 | # Generic drawing interface. |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 103 | # Do not override redraw() or draw() methods; override drawit() c.s. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 104 | # |
| 105 | def redraw(self): |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 106 | if self.bounds <> _rect.empty: |
Guido van Rossum | 17fca17 | 1991-05-14 12:13:40 +0000 | [diff] [blame] | 107 | d = self.parent.begindrawing() |
| 108 | d.erase(self.bounds) |
| 109 | self.draw(d, self.bounds) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 110 | # |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 111 | def draw(self, d, area): |
| 112 | area = _rect.intersect([area, self.bounds]) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 113 | if area == _rect.empty: |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 114 | return |
| 115 | d.cliprect(area) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 116 | self.drawit(d) |
| 117 | d.noclip() |
| 118 | # |
| 119 | # The drawit() method is fairly generic but may be overridden. |
| 120 | # |
| 121 | def drawit(self, d): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 122 | self.drawpict(d) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 123 | if self.text: |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 124 | d.text(self.textpos, self.text) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 125 | if not self.enabled: |
| 126 | self.flipenable(d) |
| 127 | if self.hilited: |
| 128 | self.fliphilite(d) |
| 129 | # |
| 130 | # Default drawing detail functions. |
| 131 | # Overriding these is normally sufficient to get different |
| 132 | # appearances. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 133 | # |
| 134 | def drawpict(self, d): |
| 135 | pass |
| 136 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 137 | def flipenable(self, d): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 138 | _xorcross(d, self.crossbounds) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 139 | # |
| 140 | def fliphilite(self, d): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 141 | d.invert(self.hilitebounds) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 142 | |
| 143 | |
Guido van Rossum | 1e5ad4e | 1991-08-16 13:04:40 +0000 | [diff] [blame] | 144 | # A Strut is a label with no width of its own. |
| 145 | |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 146 | class StrutAppearance(LabelAppearance): |
Guido van Rossum | 1e5ad4e | 1991-08-16 13:04:40 +0000 | [diff] [blame] | 147 | # |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 148 | def getminsize(self, m, (width, height)): |
Guido van Rossum | 1e5ad4e | 1991-08-16 13:04:40 +0000 | [diff] [blame] | 149 | height = max(height, m.lineheight() + 6) |
| 150 | return width, height |
| 151 | # |
| 152 | |
| 153 | |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 154 | # ButtonAppearance displays a centered string in a box. |
| 155 | # selected --> bold border |
| 156 | # disabled --> crossed out |
| 157 | # hilited --> inverted |
| 158 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 159 | class ButtonAppearance(LabelAppearance): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 160 | # |
| 161 | def drawpict(self, d): |
| 162 | d.box(_rect.inset(self.bounds, (1, 1))) |
| 163 | if self.selected: |
| 164 | # Make a thicker box |
| 165 | d.box(self.bounds) |
| 166 | d.box(_rect.inset(self.bounds, (2, 2))) |
| 167 | d.box(_rect.inset(self.bounds, (3, 3))) |
| 168 | # |
| 169 | |
| 170 | |
| 171 | # CheckAppearance displays a small square box and a left-justified string. |
| 172 | # selected --> a cross appears in the box |
| 173 | # disabled --> whole button crossed out |
| 174 | # hilited --> box is inverted |
| 175 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 176 | class CheckAppearance(LabelAppearance): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 177 | # |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 178 | def getminsize(self, m, (width, height)): |
Guido van Rossum | 1e5ad4e | 1991-08-16 13:04:40 +0000 | [diff] [blame] | 179 | minwidth = m.textwidth(self.text) + 6 |
| 180 | minheight = m.lineheight() + 6 |
| 181 | width = max(width, minwidth + minheight + m.textwidth(' ')) |
| 182 | height = max(height, minheight) |
| 183 | return width, height |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 184 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 185 | def drawpict(self, d): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 186 | d.box(self.boxbounds) |
| 187 | if self.selected: _xorcross(d, self.boxbounds) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 188 | # |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 189 | def recalcbounds(self): |
| 190 | LabelAppearance.recalcbounds(self) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 191 | (left, top), (right, bottom) = self.bounds |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 192 | self.size = bottom - top - 4 |
| 193 | self.boxbounds = (left+2, top+2), (left+2+self.size, bottom-2) |
| 194 | self.hilitebounds = self.boxbounds |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 195 | # |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 196 | def recalctextpos(self): |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 197 | m = self.parent.beginmeasuring() |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 198 | (left, top), (right, bottom) = self.boxbounds |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 199 | h = right + m.textwidth(' ') |
| 200 | v = top + (self.size - m.lineheight()) / 2 |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 201 | self.textpos = h, v |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 202 | # |
| 203 | |
| 204 | |
| 205 | # RadioAppearance displays a round indicator and a left-justified string. |
| 206 | # selected --> a dot appears in the indicator |
| 207 | # disabled --> whole button crossed out |
| 208 | # hilited --> indicator is inverted |
| 209 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 210 | class RadioAppearance(CheckAppearance): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 211 | # |
| 212 | def drawpict(self, d): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 213 | (left, top), (right, bottom) = self.boxbounds |
| 214 | radius = self.size / 2 |
Guido van Rossum | 17fca17 | 1991-05-14 12:13:40 +0000 | [diff] [blame] | 215 | center = left + radius, top + radius |
| 216 | d.circle(center, radius) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 217 | if self.selected: |
Guido van Rossum | 17fca17 | 1991-05-14 12:13:40 +0000 | [diff] [blame] | 218 | d.fillcircle(center, radius*3/5) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 219 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 220 | |
| 221 | |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 222 | # NoReactivity ignores mouse events. |
| 223 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 224 | class NoReactivity: |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 225 | def init_reactivity(self): pass |
| 226 | |
| 227 | |
| 228 | # BaseReactivity defines hooks and asks for mouse events, |
| 229 | # but provides only dummy mouse event handlers. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 230 | # The trigger methods call the corresponding hooks set by the user. |
| 231 | # Hooks (and triggers) mean the following: |
| 232 | # down_hook called on some mouse-down events |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 233 | # move_hook called on some mouse-move events |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 234 | # up_hook called on mouse-up events |
| 235 | # on_hook called for buttons with on/off state, when it goes on |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 236 | # hook called when a button 'fires' or a radiobutton goes on |
| 237 | # There are usually extra conditions, e.g., hooks are only called |
| 238 | # when the button is enabled, or active, or selected (on). |
| 239 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 240 | class BaseReactivity: |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 241 | # |
| 242 | def init_reactivity(self): |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 243 | self.down_hook = self.move_hook = self.up_hook = \ |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 244 | self.on_hook = self.off_hook = \ |
| 245 | self.hook = self.active = 0 |
| 246 | self.parent.need_mouse(self) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 247 | # |
| 248 | def mousetest(self, hv): |
| 249 | return _rect.pointinrect(hv, self.bounds) |
| 250 | # |
| 251 | def mouse_down(self, detail): |
| 252 | pass |
| 253 | # |
| 254 | def mouse_move(self, detail): |
| 255 | pass |
| 256 | # |
| 257 | def mouse_up(self, detail): |
| 258 | pass |
| 259 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 260 | def down_trigger(self): |
| 261 | if self.down_hook: self.down_hook(self) |
| 262 | # |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 263 | def move_trigger(self): |
| 264 | if self.move_hook: self.move_hook(self) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 265 | # |
| 266 | def up_trigger(self): |
| 267 | if self.up_hook: self.up_hook(self) |
| 268 | # |
| 269 | def on_trigger(self): |
| 270 | if self.on_hook: self.on_hook(self) |
| 271 | # |
| 272 | def off_trigger(self): |
| 273 | if self.off_hook: self.off_hook(self) |
| 274 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 275 | def trigger(self): |
| 276 | if self.hook: self.hook(self) |
| 277 | |
| 278 | |
| 279 | # ToggleReactivity acts like a simple pushbutton. |
| 280 | # It toggles its hilite state on mouse down events. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 281 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 282 | class ToggleReactivity(BaseReactivity): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 283 | # |
| 284 | def mouse_down(self, detail): |
| 285 | if self.enabled and self.mousetest(detail[_HV]): |
| 286 | self.active = 1 |
| 287 | self.hilite(not self.hilited) |
| 288 | self.down_trigger() |
| 289 | # |
| 290 | def mouse_move(self, detail): |
| 291 | if self.active: |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 292 | self.move_trigger() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 293 | # |
| 294 | def mouse_up(self, detail): |
| 295 | if self.active: |
| 296 | self.up_trigger() |
| 297 | self.active = 0 |
| 298 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 299 | def down_trigger(self): |
| 300 | if self.hilited: |
| 301 | self.on_trigger() |
| 302 | else: |
| 303 | self.off_trigger() |
| 304 | self.trigger() |
| 305 | # |
| 306 | |
| 307 | |
| 308 | # TriggerReactivity acts like a fancy pushbutton. |
| 309 | # It hilites itself while the mouse is down within its bounds. |
| 310 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 311 | class TriggerReactivity(BaseReactivity): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 312 | # |
| 313 | def mouse_down(self, detail): |
| 314 | if self.enabled and self.mousetest(detail[_HV]): |
| 315 | self.active = 1 |
| 316 | self.hilite(1) |
| 317 | self.down_trigger() |
| 318 | # |
| 319 | def mouse_move(self, detail): |
| 320 | if self.active: |
| 321 | self.hilite(self.mousetest(detail[_HV])) |
| 322 | if self.hilited: |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 323 | self.move_trigger() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 324 | # |
| 325 | def mouse_up(self, detail): |
| 326 | if self.active: |
| 327 | self.hilite(self.mousetest(detail[_HV])) |
| 328 | if self.hilited: |
| 329 | self.up_trigger() |
| 330 | self.trigger() |
| 331 | self.active = 0 |
| 332 | self.hilite(0) |
| 333 | # |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 334 | |
| 335 | |
| 336 | # CheckReactivity handles mouse events like TriggerReactivity, |
| 337 | # It overrides the up_trigger method to flip its selected state. |
| 338 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 339 | class CheckReactivity(TriggerReactivity): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 340 | # |
| 341 | def up_trigger(self): |
| 342 | self.select(not self.selected) |
| 343 | if self.selected: |
| 344 | self.on_trigger() |
| 345 | else: |
| 346 | self.off_trigger() |
| 347 | self.trigger() |
| 348 | |
| 349 | |
| 350 | # RadioReactivity turns itself on and the other buttons in its group |
| 351 | # off when its up_trigger method is called. |
| 352 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 353 | class RadioReactivity(TriggerReactivity): |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 354 | # |
| 355 | def init_reactivity(self): |
| 356 | TriggerReactivity.init_reactivity(self) |
| 357 | self.group = [] |
| 358 | # |
| 359 | def up_trigger(self): |
| 360 | for b in self.group: |
| 361 | if b <> self: |
| 362 | if b.selected: |
| 363 | b.select(0) |
| 364 | b.off_trigger() |
| 365 | self.select(1) |
| 366 | self.on_trigger() |
| 367 | self.trigger() |
| 368 | |
| 369 | |
| 370 | # Auxiliary class for 'define' method. |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 371 | # Call the initializers in the right order. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 372 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 373 | class Define: |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 374 | # |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 375 | def define(self, parent): |
| 376 | self.parent = parent |
| 377 | parent.addchild(self) |
| 378 | self.init_appearance() |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 379 | self.init_reactivity() |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 380 | return self |
| 381 | # |
| 382 | def destroy(self): |
| 383 | self.parent = 0 |
| 384 | # |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 385 | def definetext(self, parent, text): |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 386 | self = self.define(parent) |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 387 | self.settext(text) |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 388 | return self |
| 389 | |
| 390 | |
Guido van Rossum | ceae528 | 1990-10-25 18:50:27 +0000 | [diff] [blame] | 391 | # Subroutine to cross out a rectangle. |
| 392 | # |
| 393 | def _xorcross(d, bounds): |
| 394 | ((left, top), (right, bottom)) = bounds |
| 395 | # This is s bit funny to make it look better |
| 396 | left = left + 2 |
| 397 | right = right - 2 |
| 398 | top = top + 2 |
| 399 | bottom = bottom - 3 |
| 400 | d.xorline(((left, top), (right, bottom))) |
| 401 | d.xorline((left, bottom), (right, top)) |
| 402 | |
| 403 | |
Guido van Rossum | 0c89ec7 | 1990-11-05 19:44:31 +0000 | [diff] [blame] | 404 | # Ready-made button classes. |
Guido van Rossum | 336f281 | 1990-10-24 16:39:18 +0000 | [diff] [blame] | 405 | # |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 406 | class Label(NoReactivity, LabelAppearance, Define): pass |
| 407 | class Strut(NoReactivity, StrutAppearance, Define): pass |
| 408 | class PushButton(TriggerReactivity, ButtonAppearance, Define): pass |
| 409 | class CheckButton(CheckReactivity, CheckAppearance, Define): pass |
| 410 | class RadioButton(RadioReactivity, RadioAppearance, Define): pass |
| 411 | class ToggleButton(ToggleReactivity, ButtonAppearance, Define): pass |