]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tools.py
f2db30528c5355102e57728ea007c6e398822a92
[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 def AddButton(self, id, image, text):
104 from wx.lib import buttons
105 button = buttons.GenBitmapButton(self, id, image, size=self.TOOL_SIZE,
106 style=wx.NO_BORDER|wx.WANTS_CHARS)
107 button.SetBezelWidth(0)
108 wx.EVT_KEY_DOWN(button, self.OnKeyDown)
109 wx.EVT_KEY_UP(button, self.OnKeyUp)
110 wx.EVT_RIGHT_DOWN(button, self.OnRightClick)
111 button.SetToolTipString(text)
112 self.curSizer.Add(button)
113 self.groups[-1][1][id] = button
114
115 def AddGroup(self, name):
116 # Each group is inside box
117 id = wx.NewId()
118 box = wx.StaticBox(self, id, '[+] '+name, style=wx.WANTS_CHARS)
119 box.show = True
120 box.name = name
121 box.gnum = len(self.groups)
122 box.SetFont(g.smallerFont())
123 box.Bind(wx.EVT_LEFT_DOWN, self.OnClickBox)
124 boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL)
125 boxSizer.Add((0, 4))
126 self.boxes[id] = box
127 self.curSizer = wx.GridSizer(0, 3)
128 boxSizer.Add(self.curSizer)
129 self.sizer.Add(boxSizer, 0, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4)
130 self.groups.append((box,{}))
131
132 # Enable/disable group
133 def EnableGroup(self, gnum, enable = True):
134 grp = self.groups[gnum]
135 grp[0].Enable(enable)
136 for b in grp[1].values(): b.Enable(enable)
137
138 # Show/hide group
139 def ShowGroup(self, gnum, show = True):
140 grp = self.groups[gnum]
141 grp[0].show = show
142 for b in grp[1].values(): b.Show(show)
143
144 # Enable/disable group item
145 def EnableGroupItem(self, gnum, id, enable = True):
146 grp = self.groups[gnum]
147 grp[1][id].Enable(enable)
148
149 # Enable/disable group items
150 def EnableGroupItems(self, gnum, ids, enable = True):
151 grp = self.groups[gnum]
152 for id in ids:
153 grp[1][id].Enable(enable)
154
155 def OnClickBox(self, evt):
156 box = self.boxes[evt.GetId()]
157 # Collapse/restore static box, change label
158 self.ShowGroup(box.gnum, not box.show)
159 if box.show: box.SetLabel('[+] ' + box.name)
160 else: box.SetLabel('[-] ' + box.name)
161 self.Layout()
162
163 # Drag
164 def OnRightClick(self, evt):
165 do = MyDataObject()
166 do.SetData(str(evt.GetId()))
167 bm = evt.GetEventObject().GetBitmapLabel()
168 if wx.Platform != '__WXMAC__':
169 icon = wx.EmptyIcon()
170 icon.CopyFromBitmap(bm)
171 dragSource = wx.DropSource(self, icon)
172 else: # on Mac DragSource requires cursor (but does not work anyway)
173 curs = wx.CursorFromImage(wx.ImageFromBitmap(bm))
174 dragSource = wx.DropSource(self, curs)
175 dragSource.SetData(do)
176 g.frame.SetStatusText('Release the mouse button over the test window')
177 dragSource.DoDragDrop()
178
179 # Process key events
180 def OnKeyDown(self, evt):
181 if evt.GetKeyCode() == wx.WXK_CONTROL:
182 g.tree.ctrl = True
183 elif evt.GetKeyCode() == wx.WXK_SHIFT:
184 g.tree.shift = True
185 self.UpdateIfNeeded()
186 evt.Skip()
187
188 def OnKeyUp(self, evt):
189 if evt.GetKeyCode() == wx.WXK_CONTROL:
190 g.tree.ctrl = False
191 elif evt.GetKeyCode() == wx.WXK_SHIFT:
192 g.tree.shift = False
193 self.UpdateIfNeeded()
194 evt.Skip()
195
196 def OnMouse(self, evt):
197 # Update control and shift states
198 g.tree.ctrl = evt.ControlDown()
199 g.tree.shift = evt.ShiftDown()
200 self.UpdateIfNeeded()
201 evt.Skip()
202
203 # Update UI after key presses, if necessary
204 def UpdateIfNeeded(self):
205 tree = g.tree
206 if self.ctrl != tree.ctrl or self.shift != tree.shift:
207 # Enabling is needed only for ctrl
208 if self.ctrl != tree.ctrl: self.UpdateUI()
209 self.ctrl = tree.ctrl
210 self.shift = tree.shift
211 if tree.ctrl:
212 status = 'SBL'
213 elif tree.shift:
214 status = 'INS'
215 else:
216 status = ''
217 g.frame.SetStatusText(status, 1)
218
219 # Update interface
220 def UpdateUI(self):
221 if not self.IsShown(): return
222 # Update status bar
223 pullDownMenu = g.pullDownMenu
224 tree = g.tree
225 item = tree.selection
226 # If nothing selected, disable everything and return
227 if not item:
228 # Disable everything
229 for grp in range(GROUPNUM):
230 self.EnableGroup(grp, False)
231 self.state = None
232 return
233 if tree.ctrl: needInsert = True
234 else: needInsert = tree.NeedInsert(item)
235 # Enable depending on selection
236 if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root:
237 state = STATE_ROOT
238 else:
239 xxx = tree.GetPyData(item).treeObject()
240 # Check parent for possible child nodes if inserting sibling
241 if needInsert: xxx = xxx.parent
242 if xxx.__class__ == xxxMenuBar:
243 state = STATE_MENUBAR
244 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
245 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
246 state = STATE_TOOLBAR
247 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
248 state = STATE_MENU
249 elif xxx.__class__ == xxxStdDialogButtonSizer:
250 state = STATE_STDDLGBTN
251 else:
252 state = STATE_ELSE
253
254 # Enable depending on selection
255 if state != self.state:
256 # Disable everything
257 for grp in range(GROUPNUM):
258 self.EnableGroup(grp, False)
259 # Enable some
260 if state == STATE_ROOT:
261 self.EnableGroup(GROUP_WINDOWS, True)
262 self.EnableGroup(GROUP_MENUS, True)
263 # But disable items
264 self.EnableGroupItems(GROUP_MENUS,
265 [ ID_NEW.TOOL,
266 ID_NEW.MENU_ITEM,
267 ID_NEW.SEPARATOR ],
268 False)
269 elif state == STATE_STDDLGBTN:
270 pass # nothing can be added from toolbar
271 elif state == STATE_MENUBAR:
272 self.EnableGroup(GROUP_MENUS)
273 self.EnableGroupItems(GROUP_MENUS,
274 [ ID_NEW.TOOL_BAR,
275 ID_NEW.MENU_BAR,
276 ID_NEW.TOOL ],
277 False)
278 elif state == STATE_TOOLBAR:
279 self.EnableGroup(GROUP_MENUS)
280 self.EnableGroupItems(GROUP_MENUS,
281 [ ID_NEW.TOOL_BAR,
282 ID_NEW.MENU,
283 ID_NEW.MENU_BAR,
284 ID_NEW.MENU_ITEM ],
285 False)
286 self.EnableGroup(GROUP_CONTROLS)
287 self.EnableGroupItems(GROUP_CONTROLS,
288 [ ID_NEW.TREE_CTRL,
289 ID_NEW.NOTEBOOK,
290 ID_NEW.SPLITTER_WINDOW ],
291 False)
292 elif state == STATE_MENU:
293 self.EnableGroup(GROUP_MENUS)
294 self.EnableGroupItems(GROUP_MENUS,
295 [ ID_NEW.TOOL_BAR,
296 ID_NEW.MENU_BAR,
297 ID_NEW.TOOL ],
298 False)
299 else:
300 self.EnableGroup(GROUP_WINDOWS)
301 self.EnableGroupItems(GROUP_WINDOWS,
302 [ ID_NEW.FRAME,
303 ID_NEW.DIALOG ],
304 False)
305 self.EnableGroup(GROUP_MENUS)
306 self.EnableGroupItems(GROUP_MENUS,
307 [ ID_NEW.MENU_BAR,
308 ID_NEW.MENU_BAR,
309 ID_NEW.MENU,
310 ID_NEW.MENU_ITEM,
311 ID_NEW.TOOL,
312 ID_NEW.SEPARATOR ],
313 False)
314 self.EnableGroup(GROUP_SIZERS)
315 self.EnableGroup(GROUP_CONTROLS)
316 # Special case for *book (always executed)
317 if state == STATE_ELSE:
318 if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
319 self.EnableGroup(GROUP_SIZERS, False)
320 else:
321 self.EnableGroup(GROUP_SIZERS)
322 if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
323 self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False)
324 if xxx.__class__ == xxxFrame:
325 self.EnableGroupItem(GROUP_MENUS, ID_NEW.MENU_BAR)
326 # Save state
327 self.state = state