]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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$ | |
1fded56b | 6 | |
d14a1e28 RD |
7 | from xxx import * # xxx imports globals and params |
8 | from tree import ID_NEW | |
1fded56b | 9 | |
d14a1e28 RD |
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 | |
c032d94e | 18 | STATE_ROOT, STATE_MENUBAR, STATE_TOOLBAR, STATE_MENU, STATE_STDDLGBTN, STATE_ELSE = range(6) |
d14a1e28 RD |
19 | |
20 | # Left toolbar for GUI elements | |
29a41103 | 21 | class Tools(wx.Panel): |
d14a1e28 RD |
22 | TOOL_SIZE = (30, 30) |
23 | def __init__(self, parent): | |
29a41103 RD |
24 | if wx.Platform == '__WXGTK__': |
25 | wx.Panel.__init__(self, parent, -1, | |
26 | style=wx.RAISED_BORDER|wx.WANTS_CHARS) | |
d14a1e28 | 27 | else: |
29a41103 | 28 | wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS) |
d14a1e28 | 29 | # Create sizer for groups |
29a41103 | 30 | self.sizer = wx.BoxSizer(wx.VERTICAL) |
d14a1e28 RD |
31 | # Data to create buttons |
32 | pullDownMenu = g.pullDownMenu | |
33 | self.groups = [] | |
34 | self.ctrl = self.shift = False | |
35 | # Current state (what to enable/disable) | |
36 | self.state = None | |
37 | groups = [ | |
38 | ["Windows", | |
39 | (ID_NEW.FRAME, images.getToolFrameBitmap()), | |
40 | (ID_NEW.DIALOG, images.getToolDialogBitmap()), | |
41 | (ID_NEW.PANEL, images.getToolPanelBitmap())], | |
42 | ["Menus", | |
43 | (ID_NEW.TOOL_BAR, images.getToolToolBarBitmap()), | |
44 | (ID_NEW.MENU_BAR, images.getToolMenuBarBitmap()), | |
45 | (ID_NEW.MENU, images.getToolMenuBitmap()), | |
46 | (ID_NEW.TOOL, images.getToolToolBitmap()), | |
47 | (ID_NEW.MENU_ITEM, images.getToolMenuItemBitmap()), | |
48 | (ID_NEW.SEPARATOR, images.getToolSeparatorBitmap())], | |
49 | ["Sizers", | |
50 | (ID_NEW.BOX_SIZER, images.getToolBoxSizerBitmap()), | |
51 | (ID_NEW.STATIC_BOX_SIZER, images.getToolStaticBoxSizerBitmap()), | |
52 | (ID_NEW.GRID_SIZER, images.getToolGridSizerBitmap()), | |
53 | (ID_NEW.FLEX_GRID_SIZER, images.getToolFlexGridSizerBitmap()), | |
a4c013b2 | 54 | (ID_NEW.GRID_BAG_SIZER, images.getToolGridBagSizerBitmap()), |
d14a1e28 RD |
55 | (ID_NEW.SPACER, images.getToolSpacerBitmap())], |
56 | ["Controls", | |
57 | (ID_NEW.STATIC_TEXT, images.getToolStaticTextBitmap()), | |
58 | (ID_NEW.STATIC_BITMAP, images.getToolStaticBitmapBitmap()), | |
59 | (ID_NEW.STATIC_LINE, images.getToolStaticLineBitmap()), | |
60 | ||
61 | (ID_NEW.BUTTON, images.getToolButtonBitmap()), | |
62 | (ID_NEW.BITMAP_BUTTON, images.getToolBitmapButtonBitmap()), | |
63 | (ID_NEW.STATIC_BOX, images.getToolStaticBoxBitmap()), | |
64 | ||
65 | (ID_NEW.TEXT_CTRL, images.getToolTextCtrlBitmap()), | |
66 | (ID_NEW.COMBO_BOX, images.getToolComboBoxBitmap()), | |
67 | (ID_NEW.CHOICE, images.getToolChoiceBitmap()), | |
68 | ||
69 | (ID_NEW.RADIO_BUTTON, images.getToolRadioButtonBitmap()), | |
70 | (ID_NEW.CHECK_BOX, images.getToolCheckBoxBitmap()), | |
71 | (ID_NEW.RADIO_BOX, images.getToolRadioBoxBitmap()), | |
72 | ||
73 | (ID_NEW.SPIN_CTRL, images.getToolSpinCtrlBitmap()), | |
74 | (ID_NEW.SPIN_BUTTON, images.getToolSpinButtonBitmap()), | |
75 | (ID_NEW.SCROLL_BAR, images.getToolScrollBarBitmap()), | |
76 | ||
77 | (ID_NEW.SLIDER, images.getToolSliderBitmap()), | |
78 | (ID_NEW.GAUGE, images.getToolGaugeBitmap()), | |
79 | (ID_NEW.TREE_CTRL, images.getToolTreeCtrlBitmap()), | |
80 | ||
81 | (ID_NEW.LIST_BOX, images.getToolListBoxBitmap()), | |
82 | (ID_NEW.CHECK_LIST, images.getToolCheckListBitmap()), | |
83 | (ID_NEW.LIST_CTRL, images.getToolListCtrlBitmap()), | |
84 | ||
85 | (ID_NEW.NOTEBOOK, images.getToolNotebookBitmap()), | |
68ae5821 | 86 | (ID_NEW.SPLITTER_WINDOW, images.getToolSplitterWindowBitmap()), |
d14a1e28 RD |
87 | |
88 | (ID_NEW.UNKNOWN, images.getToolUnknownBitmap())] | |
89 | ] | |
552240ce | 90 | self.boxes = {} |
d14a1e28 RD |
91 | for grp in groups: |
92 | self.AddGroup(grp[0]) | |
93 | for b in grp[1:]: | |
94 | self.AddButton(b[0], b[1], g.pullDownMenu.createMap[b[0]]) | |
d14a1e28 RD |
95 | self.SetSizerAndFit(self.sizer) |
96 | # Allow to be resized in vertical direction only | |
97 | self.SetSizeHints(self.GetSize()[0], -1) | |
98 | # Events | |
29a41103 RD |
99 | wx.EVT_COMMAND_RANGE(self, ID_NEW.PANEL, ID_NEW.LAST, |
100 | wx.wxEVT_COMMAND_BUTTON_CLICKED, g.frame.OnCreate) | |
101 | wx.EVT_KEY_DOWN(self, self.OnKeyDown) | |
102 | wx.EVT_KEY_UP(self, self.OnKeyUp) | |
d14a1e28 RD |
103 | |
104 | def AddButton(self, id, image, text): | |
29a41103 RD |
105 | from wx.lib import buttons |
106 | button = buttons.GenBitmapButton(self, id, image, size=self.TOOL_SIZE, | |
107 | style=wx.NO_BORDER|wx.WANTS_CHARS) | |
5e8b10e9 | 108 | button.SetBezelWidth(0) |
29a41103 RD |
109 | wx.EVT_KEY_DOWN(button, self.OnKeyDown) |
110 | wx.EVT_KEY_UP(button, self.OnKeyUp) | |
d14a1e28 RD |
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 | |
552240ce RR |
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) | |
289128a4 | 122 | box.SetFont(g.smallerFont()) |
552240ce | 123 | box.Bind(wx.EVT_LEFT_DOWN, self.OnClickBox) |
29a41103 | 124 | boxSizer = wx.StaticBoxSizer(box, wx.VERTICAL) |
d14a1e28 | 125 | boxSizer.Add((0, 4)) |
552240ce | 126 | self.boxes[id] = box |
29a41103 | 127 | self.curSizer = wx.GridSizer(0, 3) |
d14a1e28 | 128 | boxSizer.Add(self.curSizer) |
552240ce | 129 | self.sizer.Add(boxSizer, 0, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4) |
d14a1e28 RD |
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 | ||
552240ce RR |
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 | ||
d14a1e28 RD |
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 | ||
552240ce RR |
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 | ||
d14a1e28 RD |
163 | # Process key events |
164 | def OnKeyDown(self, evt): | |
29a41103 | 165 | if evt.GetKeyCode() == wx.WXK_CONTROL: |
d14a1e28 | 166 | g.tree.ctrl = True |
29a41103 | 167 | elif evt.GetKeyCode() == wx.WXK_SHIFT: |
d14a1e28 RD |
168 | g.tree.shift = True |
169 | self.UpdateIfNeeded() | |
170 | evt.Skip() | |
171 | ||
172 | def OnKeyUp(self, evt): | |
29a41103 | 173 | if evt.GetKeyCode() == wx.WXK_CONTROL: |
d14a1e28 | 174 | g.tree.ctrl = False |
29a41103 | 175 | elif evt.GetKeyCode() == wx.WXK_SHIFT: |
d14a1e28 RD |
176 | g.tree.shift = False |
177 | self.UpdateIfNeeded() | |
178 | evt.Skip() | |
179 | ||
180 | def OnMouse(self, evt): | |
181 | # Update control and shift states | |
182 | g.tree.ctrl = evt.ControlDown() | |
183 | g.tree.shift = evt.ShiftDown() | |
184 | self.UpdateIfNeeded() | |
185 | evt.Skip() | |
186 | ||
187 | # Update UI after key presses, if necessary | |
188 | def UpdateIfNeeded(self): | |
189 | tree = g.tree | |
190 | if self.ctrl != tree.ctrl or self.shift != tree.shift: | |
191 | # Enabling is needed only for ctrl | |
192 | if self.ctrl != tree.ctrl: self.UpdateUI() | |
193 | self.ctrl = tree.ctrl | |
194 | self.shift = tree.shift | |
195 | if tree.ctrl: | |
196 | status = 'SBL' | |
197 | elif tree.shift: | |
198 | status = 'INS' | |
199 | else: | |
200 | status = '' | |
201 | g.frame.SetStatusText(status, 1) | |
202 | ||
203 | # Update interface | |
204 | def UpdateUI(self): | |
205 | if not self.IsShown(): return | |
206 | # Update status bar | |
207 | pullDownMenu = g.pullDownMenu | |
208 | tree = g.tree | |
209 | item = tree.selection | |
210 | # If nothing selected, disable everything and return | |
211 | if not item: | |
212 | # Disable everything | |
213 | for grp in range(GROUPNUM): | |
214 | self.EnableGroup(grp, False) | |
215 | self.state = None | |
216 | return | |
217 | if tree.ctrl: needInsert = True | |
218 | else: needInsert = tree.NeedInsert(item) | |
219 | # Enable depending on selection | |
220 | if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root: | |
221 | state = STATE_ROOT | |
222 | else: | |
223 | xxx = tree.GetPyData(item).treeObject() | |
224 | # Check parent for possible child nodes if inserting sibling | |
225 | if needInsert: xxx = xxx.parent | |
226 | if xxx.__class__ == xxxMenuBar: | |
227 | state = STATE_MENUBAR | |
228 | elif xxx.__class__ in [xxxToolBar, xxxTool] or \ | |
229 | xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar: | |
230 | state = STATE_TOOLBAR | |
231 | elif xxx.__class__ in [xxxMenu, xxxMenuItem]: | |
232 | state = STATE_MENU | |
c032d94e RR |
233 | elif xxx.__class__ == xxxStdDialogButtonSizer: |
234 | state = STATE_STDDLGBTN | |
d14a1e28 RD |
235 | else: |
236 | state = STATE_ELSE | |
237 | ||
238 | # Enable depending on selection | |
239 | if state != self.state: | |
240 | # Disable everything | |
241 | for grp in range(GROUPNUM): | |
242 | self.EnableGroup(grp, False) | |
243 | # Enable some | |
244 | if state == STATE_ROOT: | |
245 | self.EnableGroup(GROUP_WINDOWS, True) | |
246 | self.EnableGroup(GROUP_MENUS, True) | |
247 | # But disable items | |
248 | self.EnableGroupItems(GROUP_MENUS, | |
249 | [ ID_NEW.TOOL, | |
250 | ID_NEW.MENU_ITEM, | |
251 | ID_NEW.SEPARATOR ], | |
252 | False) | |
306b6fe9 | 253 | elif state == STATE_STDDLGBTN: |
c032d94e | 254 | pass # nothing can be added from toolbar |
d14a1e28 RD |
255 | elif state == STATE_MENUBAR: |
256 | self.EnableGroup(GROUP_MENUS) | |
257 | self.EnableGroupItems(GROUP_MENUS, | |
258 | [ ID_NEW.TOOL_BAR, | |
259 | ID_NEW.MENU_BAR, | |
260 | ID_NEW.TOOL ], | |
261 | False) | |
262 | elif state == STATE_TOOLBAR: | |
263 | self.EnableGroup(GROUP_MENUS) | |
264 | self.EnableGroupItems(GROUP_MENUS, | |
265 | [ ID_NEW.TOOL_BAR, | |
266 | ID_NEW.MENU, | |
267 | ID_NEW.MENU_BAR, | |
268 | ID_NEW.MENU_ITEM ], | |
269 | False) | |
270 | self.EnableGroup(GROUP_CONTROLS) | |
271 | self.EnableGroupItems(GROUP_CONTROLS, | |
272 | [ ID_NEW.TREE_CTRL, | |
68ae5821 RD |
273 | ID_NEW.NOTEBOOK, |
274 | ID_NEW.SPLITTER_WINDOW ], | |
d14a1e28 RD |
275 | False) |
276 | elif state == STATE_MENU: | |
277 | self.EnableGroup(GROUP_MENUS) | |
278 | self.EnableGroupItems(GROUP_MENUS, | |
279 | [ ID_NEW.TOOL_BAR, | |
280 | ID_NEW.MENU_BAR, | |
281 | ID_NEW.TOOL ], | |
282 | False) | |
283 | else: | |
284 | self.EnableGroup(GROUP_WINDOWS) | |
285 | self.EnableGroupItems(GROUP_WINDOWS, | |
286 | [ ID_NEW.FRAME, | |
287 | ID_NEW.DIALOG ], | |
288 | False) | |
289 | self.EnableGroup(GROUP_MENUS) | |
290 | self.EnableGroupItems(GROUP_MENUS, | |
291 | [ ID_NEW.MENU_BAR, | |
292 | ID_NEW.MENU_BAR, | |
293 | ID_NEW.MENU, | |
294 | ID_NEW.MENU_ITEM, | |
295 | ID_NEW.TOOL, | |
296 | ID_NEW.SEPARATOR ], | |
297 | False) | |
298 | self.EnableGroup(GROUP_SIZERS) | |
299 | self.EnableGroup(GROUP_CONTROLS) | |
306b6fe9 | 300 | # Special case for *book (always executed) |
d14a1e28 | 301 | if state == STATE_ELSE: |
306b6fe9 | 302 | if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]: |
d14a1e28 RD |
303 | self.EnableGroup(GROUP_SIZERS, False) |
304 | else: | |
305 | self.EnableGroup(GROUP_SIZERS) | |
306 | if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer): | |
307 | self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False) | |
f65bb0f8 RD |
308 | if xxx.__class__ == xxxFrame: |
309 | self.EnableGroupItem(GROUP_MENUS, ID_NEW.MENU_BAR) | |
d14a1e28 RD |
310 | # Save state |
311 | self.state = state |