]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tools.py
Fix or add some DoGetBestSize's
[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_ELSE = range(5)
19
20 # Left toolbar for GUI elements
21 class Tools(wxPanel):
22 TOOL_SIZE = (30, 30)
23 def __init__(self, parent):
24 if wxPlatform == '__WXGTK__':
25 wxPanel.__init__(self, parent, -1,
26 style=wxRAISED_BORDER|wxWANTS_CHARS)
27 else:
28 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
29 # Create sizer for groups
30 self.sizer = wxBoxSizer(wxVERTICAL)
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()),
54 (ID_NEW.GRID_BAG_SIZER, images.getToolGridBagSizerBitmap()),
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()),
86
87 (ID_NEW.UNKNOWN, images.getToolUnknownBitmap())]
88 ]
89 for grp in groups:
90 self.AddGroup(grp[0])
91 for b in grp[1:]:
92 self.AddButton(b[0], b[1], g.pullDownMenu.createMap[b[0]])
93 self.SetAutoLayout(True)
94 self.SetSizerAndFit(self.sizer)
95 # Allow to be resized in vertical direction only
96 self.SetSizeHints(self.GetSize()[0], -1)
97 # Events
98 EVT_COMMAND_RANGE(self, ID_NEW.PANEL, ID_NEW.LAST,
99 wxEVT_COMMAND_BUTTON_CLICKED, g.frame.OnCreate)
100 EVT_KEY_DOWN(self, self.OnKeyDown)
101 EVT_KEY_UP(self, self.OnKeyUp)
102
103 def AddButton(self, id, image, text):
104 from wxPython.lib import buttons
105 button = buttons.wxGenBitmapButton(self, id, image, size=self.TOOL_SIZE,
106 style=wxNO_BORDER|wxWANTS_CHARS)
107 button.SetBezelWidth(0)
108 EVT_KEY_DOWN(button, self.OnKeyDown)
109 EVT_KEY_UP(button, self.OnKeyUp)
110 button.SetToolTipString(text)
111 self.curSizer.Add(button)
112 self.groups[-1][1][id] = button
113
114 def AddGroup(self, name):
115 # Each group is inside box
116 box = wxStaticBox(self, -1, name, style=wxWANTS_CHARS)
117 box.SetFont(g.smallerFont())
118 boxSizer = wxStaticBoxSizer(box, wxVERTICAL)
119 boxSizer.Add((0, 4))
120 self.curSizer = wxGridSizer(0, 3)
121 boxSizer.Add(self.curSizer)
122 self.sizer.Add(boxSizer, 0, wxTOP | wxLEFT | wxRIGHT, 4)
123 self.groups.append((box,{}))
124
125 # Enable/disable group
126 def EnableGroup(self, gnum, enable = True):
127 grp = self.groups[gnum]
128 grp[0].Enable(enable)
129 for b in grp[1].values(): b.Enable(enable)
130
131 # Enable/disable group item
132 def EnableGroupItem(self, gnum, id, enable = True):
133 grp = self.groups[gnum]
134 grp[1][id].Enable(enable)
135
136 # Enable/disable group items
137 def EnableGroupItems(self, gnum, ids, enable = True):
138 grp = self.groups[gnum]
139 for id in ids:
140 grp[1][id].Enable(enable)
141
142 # Process key events
143 def OnKeyDown(self, evt):
144 if evt.GetKeyCode() == WXK_CONTROL:
145 g.tree.ctrl = True
146 elif evt.GetKeyCode() == WXK_SHIFT:
147 g.tree.shift = True
148 self.UpdateIfNeeded()
149 evt.Skip()
150
151 def OnKeyUp(self, evt):
152 if evt.GetKeyCode() == WXK_CONTROL:
153 g.tree.ctrl = False
154 elif evt.GetKeyCode() == WXK_SHIFT:
155 g.tree.shift = False
156 self.UpdateIfNeeded()
157 evt.Skip()
158
159 def OnMouse(self, evt):
160 # Update control and shift states
161 g.tree.ctrl = evt.ControlDown()
162 g.tree.shift = evt.ShiftDown()
163 self.UpdateIfNeeded()
164 evt.Skip()
165
166 # Update UI after key presses, if necessary
167 def UpdateIfNeeded(self):
168 tree = g.tree
169 if self.ctrl != tree.ctrl or self.shift != tree.shift:
170 # Enabling is needed only for ctrl
171 if self.ctrl != tree.ctrl: self.UpdateUI()
172 self.ctrl = tree.ctrl
173 self.shift = tree.shift
174 if tree.ctrl:
175 status = 'SBL'
176 elif tree.shift:
177 status = 'INS'
178 else:
179 status = ''
180 g.frame.SetStatusText(status, 1)
181
182 # Update interface
183 def UpdateUI(self):
184 if not self.IsShown(): return
185 # Update status bar
186 pullDownMenu = g.pullDownMenu
187 tree = g.tree
188 item = tree.selection
189 # If nothing selected, disable everything and return
190 if not item:
191 # Disable everything
192 for grp in range(GROUPNUM):
193 self.EnableGroup(grp, False)
194 self.state = None
195 return
196 if tree.ctrl: needInsert = True
197 else: needInsert = tree.NeedInsert(item)
198 # Enable depending on selection
199 if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root:
200 state = STATE_ROOT
201 else:
202 xxx = tree.GetPyData(item).treeObject()
203 # Check parent for possible child nodes if inserting sibling
204 if needInsert: xxx = xxx.parent
205 if xxx.__class__ == xxxMenuBar:
206 state = STATE_MENUBAR
207 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
208 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
209 state = STATE_TOOLBAR
210 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
211 state = STATE_MENU
212 else:
213 state = STATE_ELSE
214
215 # Enable depending on selection
216 if state != self.state:
217 # Disable everything
218 for grp in range(GROUPNUM):
219 self.EnableGroup(grp, False)
220 # Enable some
221 if state == STATE_ROOT:
222 self.EnableGroup(GROUP_WINDOWS, True)
223 self.EnableGroup(GROUP_MENUS, True)
224 # But disable items
225 self.EnableGroupItems(GROUP_MENUS,
226 [ ID_NEW.TOOL,
227 ID_NEW.MENU_ITEM,
228 ID_NEW.SEPARATOR ],
229 False)
230 elif state == STATE_MENUBAR:
231 self.EnableGroup(GROUP_MENUS)
232 self.EnableGroupItems(GROUP_MENUS,
233 [ ID_NEW.TOOL_BAR,
234 ID_NEW.MENU_BAR,
235 ID_NEW.TOOL ],
236 False)
237 elif state == STATE_TOOLBAR:
238 self.EnableGroup(GROUP_MENUS)
239 self.EnableGroupItems(GROUP_MENUS,
240 [ ID_NEW.TOOL_BAR,
241 ID_NEW.MENU,
242 ID_NEW.MENU_BAR,
243 ID_NEW.MENU_ITEM ],
244 False)
245 self.EnableGroup(GROUP_CONTROLS)
246 self.EnableGroupItems(GROUP_CONTROLS,
247 [ ID_NEW.TREE_CTRL,
248 ID_NEW.NOTEBOOK ],
249 False)
250 elif state == STATE_MENU:
251 self.EnableGroup(GROUP_MENUS)
252 self.EnableGroupItems(GROUP_MENUS,
253 [ ID_NEW.TOOL_BAR,
254 ID_NEW.MENU_BAR,
255 ID_NEW.TOOL ],
256 False)
257 else:
258 self.EnableGroup(GROUP_WINDOWS)
259 self.EnableGroupItems(GROUP_WINDOWS,
260 [ ID_NEW.FRAME,
261 ID_NEW.DIALOG ],
262 False)
263 self.EnableGroup(GROUP_MENUS)
264 self.EnableGroupItems(GROUP_MENUS,
265 [ ID_NEW.MENU_BAR,
266 ID_NEW.MENU_BAR,
267 ID_NEW.MENU,
268 ID_NEW.MENU_ITEM,
269 ID_NEW.TOOL,
270 ID_NEW.SEPARATOR ],
271 False)
272 self.EnableGroup(GROUP_SIZERS)
273 self.EnableGroup(GROUP_CONTROLS)
274 # Special case for notebook (always executed)
275 if state == STATE_ELSE:
276 if xxx.__class__ == xxxNotebook:
277 self.EnableGroup(GROUP_SIZERS, False)
278 else:
279 self.EnableGroup(GROUP_SIZERS)
280 if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
281 self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False)
282 # Save state
283 self.state = state