2 import Wbase
, Wcontrols
3 from Carbon
import Ctl
, Controls
, Qd
, Res
7 #_arrowright = Qd.GetPicture(472)
8 #_arrowdown = Qd.GetPicture(473)
10 _arrowright
= Res
.Resource(
11 '\x00I\x00\x00\x00\x00\x00\n\x00\n\x11\x01\x01\x00\n\x00\x00\x00'
12 '\x00\x00\n\x00\n\x90\x00\x02\x00\x00\x00\x00\x00\n\x00\n\x00\x00'
13 '\x00\x00\x00\n\x00\n\x00\x00\x00\x00\x00\n\x00\n\x00\x00\x10\x00'
14 '\x18\x00\x1c\x00\x1e\x00\x1f\x00\x1f\x00\x1e\x00\x1c\x00\x18\x00'
18 class PopupControl(Wcontrols
.ControlWidget
):
20 def __init__(self
, possize
, items
=None, callback
=None):
23 procID
= Controls
.popupMenuProc|Controls
.popupFixedWidth|Controls
.useWFont
24 Wcontrols
.ControlWidget
.__init
__(self
, possize
, "", procID
, callback
, 0, 0, 0)
25 self
._items
= items
[:]
28 self
._control
.SetControlValue(value
+1)
31 return self
._control
.GetControlValue() - 1
34 self
.menu
= menu
= FrameWork
.Menu(self
._parentwindow
.parent
.menubar
, 'Foo', -1)
36 for i
in range(len(self
._items
)):
38 if type(item
) == StringType
:
39 menuitemtext
= object = item
40 elif type(item
) == TupleType
and len(item
) == 2:
41 menuitemtext
, object = item
42 self
._items
[i
] = object
44 raise Wbase
.WidgetsError
, "illegal itemlist for popup menu"
45 menuitem
= FrameWork
.MenuItem(menu
, menuitemtext
, None, None)
48 self
._control
= Ctl
.NewControl(self
._parentwindow
.wid
,
58 self
.enable(self
._enabled
)
62 return Wcontrols
.ControlWidget
.close(self
)
64 def click(self
, point
, modifiers
):
67 part
= self
._control
.TrackControl(point
, -1)
70 Wbase
.CallbackCall(self
._callback
, 0, self
._items
[self
.get()])
73 class PopupWidget(Wbase
.ClickableWidget
):
75 """Simple title-less popup widget. Should be 16x16 pixels.
76 Builds the menu items on the fly, good for dynamic popup menus."""
78 def __init__(self
, possize
, items
=None, callback
=None):
79 Wbase
.ClickableWidget
.__init
__(self
, possize
)
84 self
._callback
= callback
88 Wbase
.ClickableWidget
.close(self
)
92 def draw(self
, visRgn
= None):
94 Qd
.FrameRect(self
._bounds
)
95 Qd
.EraseRect(Qd
.InsetRect(self
._bounds
, 1, 1))
96 l
, t
, r
, b
= self
._bounds
99 pictframe
= (l
, t
, l
+ 10, t
+ 10)
100 Qd
.DrawPicture(_arrowright
, pictframe
)
102 def click(self
, point
, modifiers
):
103 if not self
._enabled
:
105 self
.menu
= FrameWork
.Menu(self
._parentwindow
.parent
.menubar
, 'Foo', -1)
106 self
._additems
(self
._items
, self
.menu
)
108 l
, t
, r
, b
= self
._bounds
109 l
, t
= Qd
.LocalToGlobal((l
+1, t
+1))
110 Wbase
.SetCursor("arrow")
111 self
.menu
.menu
.EnableMenuItem(0)
112 reply
= self
.menu
.menu
.PopUpMenuSelect(t
, l
, 1)
115 item
= reply
& 0xffff
116 self
._menu
_callback
(id, item
)
119 def set(self
, items
):
125 def _additems(self
, items
, menu
):
126 from FrameWork
import SubMenu
, MenuItem
132 elif type(item
) == ListType
:
133 submenu
= SubMenu(menu
, item
[0])
134 self
._additems
(item
[1:], submenu
)
136 elif type(item
) == StringType
:
137 menuitemtext
= object = item
138 elif type(item
) == TupleType
and len(item
) == 2:
139 menuitemtext
, object = item
141 raise Wbase
.WidgetsError
, "illegal itemlist for popup menu"
143 if menuitemtext
[:1] == '\0':
144 check
= ord(menuitemtext
[1])
145 menuitemtext
= menuitemtext
[2:]
148 menuitem
= MenuItem(menu
, menuitemtext
, None, None)
151 self
._itemsdict
[(menu_id
, menuitem
.item
)] = object
153 def _emptymenu(self
):
154 menus
= self
._parentwindow
.parent
.menubar
.menus
155 for id, item
in self
._itemsdict
.keys():
156 if menus
.has_key(id):
157 self
.menu
= menus
[id]
161 def _menu_callback(self
, id, item
):
162 thing
= self
._itemsdict
[(id, item
)]
166 Wbase
.CallbackCall(self
._callback
, 0, thing
)
169 class PopupMenu(PopupWidget
):
171 """Simple title-less popup widget. Should be 16x16 pixels.
172 Prebuilds the menu items, good for static (non changing) popup menus."""
176 self
.menu
= Wapplication
.Menu(self
._parentwindow
.parent
.menubar
, 'Foo', -1)
177 self
._additems
(self
._items
, self
.menu
)
181 Wbase
.Widget
.close(self
)
186 def set(self
, items
):
189 self
.menu
= Wapplication
.Menu(self
._parentwindow
.parent
.menubar
, 'Foo', -1)
191 self
._additems
(self
._items
, self
.menu
)
193 def click(self
, point
, modifiers
):
194 if not self
._enabled
:
197 l
, t
, r
, b
= self
._bounds
198 l
, t
= Qd
.LocalToGlobal((l
+1, t
+1))
199 Wbase
.SetCursor("arrow")
200 self
.menu
.menu
.EnableMenuItem(0)
201 reply
= self
.menu
.menu
.PopUpMenuSelect(t
, l
, 1)
204 item
= reply
& 0xffff
205 self
._menu
_callback
(id, item
)
208 class FontMenu(PopupMenu
):
210 """A font popup menu."""
214 def __init__(self
, possize
, callback
):
215 PopupMenu
.__init
__(self
, possize
)
217 self
._callback
= callback
227 raise Wbase
.WidgetsError
, "can't change font menu widget"
229 def _menu_callback(self
, id, item
):
230 fontname
= self
.menu
.menu
.GetMenuItemText(item
)
232 Wbase
.CallbackCall(self
._callback
, 0, fontname
)
234 def click(self
, point
, modifiers
):
235 if not self
._enabled
:
238 return PopupMenu
.click(self
, point
, modifiers
)
242 """helper for font menu"""
243 if FontMenu
.menu
is not None:
246 FontMenu
.menu
= Wapplication
.Menu(W
.getapplication().menubar
, 'Foo', -1)
248 for i
in range(FontMenu
.menu
.menu
.CountMenuItems(), 0, -1):
249 FontMenu
.menu
.menu
.DeleteMenuItem(i
)
250 FontMenu
.menu
.menu
.AppendResMenu('FOND')
254 from Carbon
import Res
256 for i
in range(1, Res
.CountResources('FOND') + 1):
257 r
= Res
.GetIndResource('FOND', i
)
258 fontnames
.append(r
.GetResInfo()[2])