]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tools.py
cursor image type fix
[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 # wxGTK requires wxIcon cursor, wxWIN and wxMAC require wxCursor
169 if wx.Platform == '__WXGTK__':
170 icon = wx.EmptyIcon()
171 icon.CopyFromBitmap(bm)
172 dragSource = wx.DropSource(self, icon)
173 else:
174 curs = wx.CursorFromImage(wx.ImageFromBitmap(bm))
175 dragSource = wx.DropSource(self, curs)
176 dragSource.SetData(do)
177 g.frame.SetStatusText('Release the mouse button over the test window')
178 dragSource.DoDragDrop()
179
180 # Process key events
181 def OnKeyDown(self, evt):
182 if evt.GetKeyCode() == wx.WXK_CONTROL:
183 g.tree.ctrl = True
184 elif evt.GetKeyCode() == wx.WXK_SHIFT:
185 g.tree.shift = True
186 self.UpdateIfNeeded()
187 evt.Skip()
188
189 def OnKeyUp(self, evt):
190 if evt.GetKeyCode() == wx.WXK_CONTROL:
191 g.tree.ctrl = False
192 elif evt.GetKeyCode() == wx.WXK_SHIFT:
193 g.tree.shift = False
194 self.UpdateIfNeeded()
195 evt.Skip()
196
197 def OnMouse(self, evt):
198 # Update control and shift states
199 g.tree.ctrl = evt.ControlDown()
200 g.tree.shift = evt.ShiftDown()
201 self.UpdateIfNeeded()
202 evt.Skip()
203
204 # Update UI after key presses, if necessary
205 def UpdateIfNeeded(self):
206 tree = g.tree
207 if self.ctrl != tree.ctrl or self.shift != tree.shift:
208 # Enabling is needed only for ctrl
209 if self.ctrl != tree.ctrl: self.UpdateUI()
210 self.ctrl = tree.ctrl
211 self.shift = tree.shift
212 if tree.ctrl:
213 status = 'SBL'
214 elif tree.shift:
215 status = 'INS'
216 else:
217 status = ''
218 g.frame.SetStatusText(status, 1)
219
220 # Update interface
221 def UpdateUI(self):
222 if not self.IsShown(): return
223 # Update status bar
224 pullDownMenu = g.pullDownMenu
225 tree = g.tree
226 item = tree.selection
227 # If nothing selected, disable everything and return
228 if not item:
229 # Disable everything
230 for grp in range(GROUPNUM):
231 self.EnableGroup(grp, False)
232 self.state = None
233 return
234 if tree.ctrl: needInsert = True
235 else: needInsert = tree.NeedInsert(item)
236 # Enable depending on selection
237 if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root:
238 state = STATE_ROOT
239 else:
240 xxx = tree.GetPyData(item).treeObject()
241 # Check parent for possible child nodes if inserting sibling
242 if needInsert: xxx = xxx.parent
243 if xxx.__class__ == xxxMenuBar:
244 state = STATE_MENUBAR
245 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
246 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
247 state = STATE_TOOLBAR
248 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
249 state = STATE_MENU
250 elif xxx.__class__ == xxxStdDialogButtonSizer:
251 state = STATE_STDDLGBTN
252 else:
253 state = STATE_ELSE
254
255 # Enable depending on selection
256 if state != self.state:
257 # Disable everything
258 for grp in range(GROUPNUM):
259 self.EnableGroup(grp, False)
260 # Enable some
261 if state == STATE_ROOT:
262 self.EnableGroup(GROUP_WINDOWS, True)
263 self.EnableGroup(GROUP_MENUS, True)
264 # But disable items
265 self.EnableGroupItems(GROUP_MENUS,
266 [ ID_NEW.TOOL,
267 ID_NEW.MENU_ITEM,
268 ID_NEW.SEPARATOR ],
269 False)
270 elif state == STATE_STDDLGBTN:
271 pass # nothing can be added from toolbar
272 elif state == STATE_MENUBAR:
273 self.EnableGroup(GROUP_MENUS)
274 self.EnableGroupItems(GROUP_MENUS,
275 [ ID_NEW.TOOL_BAR,
276 ID_NEW.MENU_BAR,
277 ID_NEW.TOOL ],
278 False)
279 elif state == STATE_TOOLBAR:
280 self.EnableGroup(GROUP_MENUS)
281 self.EnableGroupItems(GROUP_MENUS,
282 [ ID_NEW.TOOL_BAR,
283 ID_NEW.MENU,
284 ID_NEW.MENU_BAR,
285 ID_NEW.MENU_ITEM ],
286 False)
287 self.EnableGroup(GROUP_CONTROLS)
288 self.EnableGroupItems(GROUP_CONTROLS,
289 [ ID_NEW.TREE_CTRL,
290 ID_NEW.NOTEBOOK,
291 ID_NEW.SPLITTER_WINDOW ],
292 False)
293 elif state == STATE_MENU:
294 self.EnableGroup(GROUP_MENUS)
295 self.EnableGroupItems(GROUP_MENUS,
296 [ ID_NEW.TOOL_BAR,
297 ID_NEW.MENU_BAR,
298 ID_NEW.TOOL ],
299 False)
300 else:
301 self.EnableGroup(GROUP_WINDOWS)
302 self.EnableGroupItems(GROUP_WINDOWS,
303 [ ID_NEW.FRAME,
304 ID_NEW.DIALOG ],
305 False)
306 self.EnableGroup(GROUP_MENUS)
307 self.EnableGroupItems(GROUP_MENUS,
308 [ ID_NEW.MENU_BAR,
309 ID_NEW.MENU_BAR,
310 ID_NEW.MENU,
311 ID_NEW.MENU_ITEM,
312 ID_NEW.TOOL,
313 ID_NEW.SEPARATOR ],
314 False)
315 self.EnableGroup(GROUP_SIZERS)
316 self.EnableGroup(GROUP_CONTROLS)
317 # Special case for *book (always executed)
318 if state == STATE_ELSE:
319 if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
320 self.EnableGroup(GROUP_SIZERS, False)
321 else:
322 self.EnableGroup(GROUP_SIZERS)
323 if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
324 self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False)
325 if xxx.__class__ == xxxFrame:
326 self.EnableGroupItem(GROUP_MENUS, ID_NEW.MENU_BAR)
327 # Save state
328 self.state = state