]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tools.py
fix for testing if a property is in allParams - it must be a sequence
[wxWidgets.git] / wxPython / wx / tools / XRCed / tools.py
1 # Name: tools.py
2 # Purpose: XRC editor, toolbar
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 19.03.2003
5 # RCS-ID: $Id$
6
7 from xxx import * # xxx imports globals and params
8 from tree import ID_NEW
9
10 # Icons
11 import images
12
13 # Groups of controls
14 GROUPNUM = 4
15 GROUP_WINDOWS, GROUP_MENUS, GROUP_SIZERS, GROUP_CONTROLS = range(GROUPNUM)
16
17 # States depending on current selection and Control/Shift keys
18 STATE_ROOT, STATE_MENUBAR, STATE_TOOLBAR, STATE_MENU, STATE_STDDLGBTN, STATE_ELSE = range(6)
19 # Left toolbar for GUI elements
20 class Tools(wx.Panel):
21 TOOL_SIZE = (30, 30)
22 def __init__(self, parent):
23 if wx.Platform == '__WXGTK__':
24 wx.Panel.__init__(self, parent, -1,
25 style=wx.RAISED_BORDER|wx.WANTS_CHARS)
26 else:
27 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
28 # Create sizer for groups
29 self.sizer = wx.BoxSizer(wx.VERTICAL)
30 # Data to create buttons
31 pullDownMenu = g.pullDownMenu
32 self.groups = []
33 self.ctrl = self.shift = False
34 # Current state (what to enable/disable)
35 self.state = None
36 groups = [
37 ["Windows",
38 (ID_NEW.FRAME, images.getToolFrameBitmap()),
39 (ID_NEW.DIALOG, images.getToolDialogBitmap()),
40 (ID_NEW.PANEL, images.getToolPanelBitmap())],
41 ["Menus",
42 (ID_NEW.TOOL_BAR, images.getToolToolBarBitmap()),
43 (ID_NEW.MENU_BAR, images.getToolMenuBarBitmap()),
44 (ID_NEW.MENU, images.getToolMenuBitmap()),
45 (ID_NEW.TOOL, images.getToolToolBitmap()),
46 (ID_NEW.MENU_ITEM, images.getToolMenuItemBitmap()),
47 (ID_NEW.SEPARATOR, images.getToolSeparatorBitmap())],
48 ["Sizers",
49 (ID_NEW.BOX_SIZER, images.getToolBoxSizerBitmap()),
50 (ID_NEW.STATIC_BOX_SIZER, images.getToolStaticBoxSizerBitmap()),
51 (ID_NEW.GRID_SIZER, images.getToolGridSizerBitmap()),
52 (ID_NEW.FLEX_GRID_SIZER, images.getToolFlexGridSizerBitmap()),
53 (ID_NEW.GRID_BAG_SIZER, images.getToolGridBagSizerBitmap()),
54 (ID_NEW.SPACER, images.getToolSpacerBitmap())],
55 ["Controls",
56 (ID_NEW.STATIC_TEXT, images.getToolStaticTextBitmap()),
57 (ID_NEW.STATIC_BITMAP, images.getToolStaticBitmapBitmap()),
58 (ID_NEW.STATIC_LINE, images.getToolStaticLineBitmap()),
59
60 (ID_NEW.BUTTON, images.getToolButtonBitmap()),
61 (ID_NEW.BITMAP_BUTTON, images.getToolBitmapButtonBitmap()),
62 (ID_NEW.STATIC_BOX, images.getToolStaticBoxBitmap()),
63
64 (ID_NEW.TEXT_CTRL, images.getToolTextCtrlBitmap()),
65 (ID_NEW.COMBO_BOX, images.getToolComboBoxBitmap()),
66 (ID_NEW.CHOICE, images.getToolChoiceBitmap()),
67
68 (ID_NEW.RADIO_BUTTON, images.getToolRadioButtonBitmap()),
69 (ID_NEW.CHECK_BOX, images.getToolCheckBoxBitmap()),
70 (ID_NEW.RADIO_BOX, images.getToolRadioBoxBitmap()),
71
72 (ID_NEW.SPIN_CTRL, images.getToolSpinCtrlBitmap()),
73 (ID_NEW.SPIN_BUTTON, images.getToolSpinButtonBitmap()),
74 (ID_NEW.SCROLL_BAR, images.getToolScrollBarBitmap()),
75
76 (ID_NEW.SLIDER, images.getToolSliderBitmap()),
77 (ID_NEW.GAUGE, images.getToolGaugeBitmap()),
78 (ID_NEW.TREE_CTRL, images.getToolTreeCtrlBitmap()),
79
80 (ID_NEW.LIST_BOX, images.getToolListBoxBitmap()),
81 (ID_NEW.CHECK_LIST, images.getToolCheckListBitmap()),
82 (ID_NEW.LIST_CTRL, images.getToolListCtrlBitmap()),
83
84 (ID_NEW.NOTEBOOK, images.getToolNotebookBitmap()),
85 (ID_NEW.SPLITTER_WINDOW, images.getToolSplitterWindowBitmap()),
86
87 (ID_NEW.UNKNOWN, images.getToolUnknownBitmap())]
88 ]
89 self.boxes = {}
90 for grp in groups:
91 self.AddGroup(grp[0])
92 for b in grp[1:]:
93 self.AddButton(b[0], b[1], g.pullDownMenu.createMap[b[0]])
94 self.SetSizerAndFit(self.sizer)
95 # Allow to be resized in vertical direction only
96 self.SetSizeHints(self.GetSize()[0], -1)
97 # Events
98 wx.EVT_COMMAND_RANGE(self, ID_NEW.PANEL, ID_NEW.LAST,
99 wx.wxEVT_COMMAND_BUTTON_CLICKED, g.frame.OnCreate)
100 wx.EVT_KEY_DOWN(self, self.OnKeyDown)
101 wx.EVT_KEY_UP(self, self.OnKeyUp)
102
103 self.drag = None
104
105 def AddButton(self, id, image, text):
106 from wx.lib import buttons
107 button = buttons.GenBitmapButton(self, id, image, size=self.TOOL_SIZE,
108 style=wx.NO_BORDER|wx.WANTS_CHARS)
109 button.SetBezelWidth(0)
110 wx.EVT_KEY_DOWN(button, self.OnKeyDown)
111 wx.EVT_KEY_UP(button, self.OnKeyUp)
112 wx.EVT_LEFT_DOWN(button, self.OnLeftDownOnButton)
113 wx.EVT_MOTION(button, self.OnMotionOnButton)
114 button.SetToolTipString(text)
115 self.curSizer.Add(button)
116 self.groups[-1][1][id] = button
117
118 def AddGroup(self, name):
119 # Each group is inside box
120 id = wx.NewId()
121 box = wx.StaticBox(self, id, '[+] '+name, style=wx.WANTS_CHARS)
122 box.show = True
123 box.name = name
124 box.gnum = len(self.groups)
125 box.SetFont(g.smallerFont())
126 box.Bind(wx.EVT_LEFT_DOWN, self.OnClickBox)
127 boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
128 boxSizer.Add((0, 4))
129 self.boxes[id] = box
130 self.curSizer = wx.GridSizer(0, 3)
131 boxSizer.Add(self.curSizer)
132 self.sizer.Add(boxSizer, 0, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4)
133 self.groups.append((box,{}))
134
135 # Enable/disable group
136 def EnableGroup(self, gnum, enable = True):
137 grp = self.groups[gnum]
138 grp[0].Enable(enable)
139 for b in grp[1].values(): b.Enable(enable)
140
141 # Show/hide group
142 def ShowGroup(self, gnum, show = True):
143 grp = self.groups[gnum]
144 grp[0].show = show
145 for b in grp[1].values(): b.Show(show)
146
147 # Enable/disable group item
148 def EnableGroupItem(self, gnum, id, enable = True):
149 grp = self.groups[gnum]
150 grp[1][id].Enable(enable)
151
152 # Enable/disable group items
153 def EnableGroupItems(self, gnum, ids, enable = True):
154 grp = self.groups[gnum]
155 for id in ids:
156 grp[1][id].Enable(enable)
157
158 def OnClickBox(self, evt):
159 box = self.boxes[evt.GetId()]
160 # Collapse/restore static box, change label
161 self.ShowGroup(box.gnum, not box.show)
162 if box.show: box.SetLabel('[+] ' + box.name)
163 else: box.SetLabel('[-] ' + box.name)
164 self.Layout()
165
166 # DaD
167 def OnLeftDownOnButton(self, evt):
168 self.posDown = evt.GetPosition()
169 self.idDown = evt.GetId()
170 self.btnDown = evt.GetEventObject()
171 evt.Skip()
172
173 def OnMotionOnButton(self, evt):
174 # Detect dragging
175 if evt.Dragging() and evt.LeftIsDown():
176 d = evt.GetPosition() - self.posDown
177 if max(abs(d[0]), abs(d[1])) >= 5:
178 if self.btnDown.HasCapture():
179 # Generate up event to release mouse
180 evt = wx.MouseEvent(wx.EVT_LEFT_UP.typeId)
181 evt.SetId(self.idDown)
182 # Set flag to prevent normal button operation this time
183 self.drag = True
184 self.btnDown.ProcessEvent(evt)
185 self.StartDrag()
186 evt.Skip()
187
188 def StartDrag(self):
189 do = MyDataObject()
190 do.SetData(str(self.idDown))
191 bm = self.btnDown.GetBitmapLabel()
192 # wxGTK requires wxIcon cursor, wxWIN and wxMAC require wxCursor
193 if wx.Platform == '__WXGTK__':
194 icon = wx.EmptyIcon()
195 icon.CopyFromBitmap(bm)
196 dragSource = wx.DropSource(self, icon)
197 else:
198 curs = wx.CursorFromImage(wx.ImageFromBitmap(bm))
199 dragSource = wx.DropSource(self, curs)
200 dragSource.SetData(do)
201 g.frame.SetStatusText('Release the mouse button over the test window')
202 dragSource.DoDragDrop()
203
204 # Process key events
205 def OnKeyDown(self, evt):
206 if evt.GetKeyCode() == wx.WXK_CONTROL:
207 g.tree.ctrl = True
208 elif evt.GetKeyCode() == wx.WXK_SHIFT:
209 g.tree.shift = True
210 self.UpdateIfNeeded()
211 evt.Skip()
212
213 def OnKeyUp(self, evt):
214 if evt.GetKeyCode() == wx.WXK_CONTROL:
215 g.tree.ctrl = False
216 elif evt.GetKeyCode() == wx.WXK_SHIFT:
217 g.tree.shift = False
218 self.UpdateIfNeeded()
219 evt.Skip()
220
221 def OnMouse(self, evt):
222 # Update control and shift states
223 g.tree.ctrl = evt.ControlDown()
224 g.tree.shift = evt.ShiftDown()
225 self.UpdateIfNeeded()
226 evt.Skip()
227
228 # Update UI after key presses, if necessary
229 def UpdateIfNeeded(self):
230 tree = g.tree
231 if self.ctrl != tree.ctrl or self.shift != tree.shift:
232 # Enabling is needed only for ctrl
233 if self.ctrl != tree.ctrl: self.UpdateUI()
234 self.ctrl = tree.ctrl
235 self.shift = tree.shift
236 if tree.ctrl:
237 status = 'SBL'
238 elif tree.shift:
239 status = 'INS'
240 else:
241 status = ''
242 g.frame.SetStatusText(status, 1)
243
244 # Update interface
245 def UpdateUI(self):
246 if not self.IsShown(): return
247 # Update status bar
248 pullDownMenu = g.pullDownMenu
249 tree = g.tree
250 item = tree.selection
251 # If nothing selected, disable everything and return
252 if not item:
253 # Disable everything
254 for grp in range(GROUPNUM):
255 self.EnableGroup(grp, False)
256 self.state = None
257 return
258 if tree.ctrl: needInsert = True
259 else: needInsert = tree.NeedInsert(item)
260 # Enable depending on selection
261 if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root:
262 state = STATE_ROOT
263 else:
264 xxx = tree.GetPyData(item).treeObject()
265 # Check parent for possible child nodes if inserting sibling
266 if needInsert: xxx = xxx.parent
267 if xxx.__class__ == xxxMenuBar:
268 state = STATE_MENUBAR
269 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
270 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
271 state = STATE_TOOLBAR
272 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
273 state = STATE_MENU
274 elif xxx.__class__ == xxxStdDialogButtonSizer:
275 state = STATE_STDDLGBTN
276 else:
277 state = STATE_ELSE
278
279 # Enable depending on selection
280 if state != self.state:
281 # Disable everything
282 for grp in range(GROUPNUM):
283 self.EnableGroup(grp, False)
284 # Enable some
285 if state == STATE_ROOT:
286 self.EnableGroup(GROUP_WINDOWS, True)
287 self.EnableGroup(GROUP_MENUS, True)
288 # But disable items
289 self.EnableGroupItems(GROUP_MENUS,
290 [ ID_NEW.TOOL,
291 ID_NEW.MENU_ITEM,
292 ID_NEW.SEPARATOR ],
293 False)
294 elif state == STATE_STDDLGBTN:
295 pass # nothing can be added from toolbar
296 elif state == STATE_MENUBAR:
297 self.EnableGroup(GROUP_MENUS)
298 self.EnableGroupItems(GROUP_MENUS,
299 [ ID_NEW.TOOL_BAR,
300 ID_NEW.MENU_BAR,
301 ID_NEW.TOOL ],
302 False)
303 elif state == STATE_TOOLBAR:
304 self.EnableGroup(GROUP_MENUS)
305 self.EnableGroupItems(GROUP_MENUS,
306 [ ID_NEW.TOOL_BAR,
307 ID_NEW.MENU,
308 ID_NEW.MENU_BAR,
309 ID_NEW.MENU_ITEM ],
310 False)
311 self.EnableGroup(GROUP_CONTROLS)
312 self.EnableGroupItems(GROUP_CONTROLS,
313 [ ID_NEW.TREE_CTRL,
314 ID_NEW.NOTEBOOK,
315 ID_NEW.SPLITTER_WINDOW ],
316 False)
317 elif state == STATE_MENU:
318 self.EnableGroup(GROUP_MENUS)
319 self.EnableGroupItems(GROUP_MENUS,
320 [ ID_NEW.TOOL_BAR,
321 ID_NEW.MENU_BAR,
322 ID_NEW.TOOL ],
323 False)
324 else:
325 self.EnableGroup(GROUP_WINDOWS)
326 self.EnableGroupItems(GROUP_WINDOWS,
327 [ ID_NEW.FRAME,
328 ID_NEW.DIALOG ],
329 False)
330 self.EnableGroup(GROUP_MENUS)
331 self.EnableGroupItems(GROUP_MENUS,
332 [ ID_NEW.MENU_BAR,
333 ID_NEW.MENU_BAR,
334 ID_NEW.MENU,
335 ID_NEW.MENU_ITEM,
336 ID_NEW.TOOL,
337 ID_NEW.SEPARATOR ],
338 False)
339 self.EnableGroup(GROUP_SIZERS)
340 self.EnableGroup(GROUP_CONTROLS)
341 # Special case for *book (always executed)
342 if state == STATE_ELSE:
343 if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
344 self.EnableGroup(GROUP_SIZERS, False)
345 else:
346 self.EnableGroup(GROUP_SIZERS)
347 if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
348 self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False)
349 if xxx.__class__ == xxxFrame:
350 self.EnableGroupItem(GROUP_MENUS, ID_NEW.MENU_BAR)
351 # Save state
352 self.state = state