]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/tools/XRCed/tools.py
Use unbuffered output
[wxWidgets.git] / wxPython / wx / tools / XRCed / tools.py
CommitLineData
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
7from xxx import * # xxx imports globals and params
8from tree import ID_NEW
1fded56b 9
d14a1e28
RD
10# Icons
11import images
12
13# Groups of controls
14GROUPNUM = 4
15GROUP_WINDOWS, GROUP_MENUS, GROUP_SIZERS, GROUP_CONTROLS = range(GROUPNUM)
16
17# States depending on current selection and Control/Shift keys
18STATE_ROOT, STATE_MENUBAR, STATE_TOOLBAR, STATE_MENU, STATE_ELSE = range(5)
19
20# Left toolbar for GUI elements
21class 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()),
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()),
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 button = wxBitmapButton(self, id, image, size=self.TOOL_SIZE,
105 style=wxBU_AUTODRAW|wxNO_BORDER|wxWANTS_CHARS)
106 EVT_KEY_DOWN(button, self.OnKeyDown)
107 EVT_KEY_UP(button, self.OnKeyUp)
108 button.SetToolTipString(text)
109 self.curSizer.Add(button)
110 self.groups[-1][1][id] = button
111
112 def AddGroup(self, name):
113 # Each group is inside box
114 box = wxStaticBox(self, -1, name, style=wxWANTS_CHARS)
115 box.SetFont(smallerFont)
116 boxSizer = wxStaticBoxSizer(box, wxVERTICAL)
117 boxSizer.Add((0, 4))
118 self.curSizer = wxGridSizer(0, 3)
119 boxSizer.Add(self.curSizer)
120 self.sizer.Add(boxSizer, 0, wxTOP | wxLEFT | wxRIGHT, 4)
121 self.groups.append((box,{}))
122
123 # Enable/disable group
124 def EnableGroup(self, gnum, enable = True):
125 grp = self.groups[gnum]
126 grp[0].Enable(enable)
127 for b in grp[1].values(): b.Enable(enable)
128
129 # Enable/disable group item
130 def EnableGroupItem(self, gnum, id, enable = True):
131 grp = self.groups[gnum]
132 grp[1][id].Enable(enable)
133
134 # Enable/disable group items
135 def EnableGroupItems(self, gnum, ids, enable = True):
136 grp = self.groups[gnum]
137 for id in ids:
138 grp[1][id].Enable(enable)
139
140 # Process key events
141 def OnKeyDown(self, evt):
142 if evt.GetKeyCode() == WXK_CONTROL:
143 g.tree.ctrl = True
144 elif evt.GetKeyCode() == WXK_SHIFT:
145 g.tree.shift = True
146 self.UpdateIfNeeded()
147 evt.Skip()
148
149 def OnKeyUp(self, evt):
150 if evt.GetKeyCode() == WXK_CONTROL:
151 g.tree.ctrl = False
152 elif evt.GetKeyCode() == WXK_SHIFT:
153 g.tree.shift = False
154 self.UpdateIfNeeded()
155 evt.Skip()
156
157 def OnMouse(self, evt):
158 # Update control and shift states
159 g.tree.ctrl = evt.ControlDown()
160 g.tree.shift = evt.ShiftDown()
161 self.UpdateIfNeeded()
162 evt.Skip()
163
164 # Update UI after key presses, if necessary
165 def UpdateIfNeeded(self):
166 tree = g.tree
167 if self.ctrl != tree.ctrl or self.shift != tree.shift:
168 # Enabling is needed only for ctrl
169 if self.ctrl != tree.ctrl: self.UpdateUI()
170 self.ctrl = tree.ctrl
171 self.shift = tree.shift
172 if tree.ctrl:
173 status = 'SBL'
174 elif tree.shift:
175 status = 'INS'
176 else:
177 status = ''
178 g.frame.SetStatusText(status, 1)
179
180 # Update interface
181 def UpdateUI(self):
182 if not self.IsShown(): return
183 # Update status bar
184 pullDownMenu = g.pullDownMenu
185 tree = g.tree
186 item = tree.selection
187 # If nothing selected, disable everything and return
188 if not item:
189 # Disable everything
190 for grp in range(GROUPNUM):
191 self.EnableGroup(grp, False)
192 self.state = None
193 return
194 if tree.ctrl: needInsert = True
195 else: needInsert = tree.NeedInsert(item)
196 # Enable depending on selection
197 if item == tree.root or needInsert and tree.GetItemParent(item) == tree.root:
198 state = STATE_ROOT
199 else:
200 xxx = tree.GetPyData(item).treeObject()
201 # Check parent for possible child nodes if inserting sibling
202 if needInsert: xxx = xxx.parent
203 if xxx.__class__ == xxxMenuBar:
204 state = STATE_MENUBAR
205 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
206 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
207 state = STATE_TOOLBAR
208 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
209 state = STATE_MENU
210 else:
211 state = STATE_ELSE
212
213 # Enable depending on selection
214 if state != self.state:
215 # Disable everything
216 for grp in range(GROUPNUM):
217 self.EnableGroup(grp, False)
218 # Enable some
219 if state == STATE_ROOT:
220 self.EnableGroup(GROUP_WINDOWS, True)
221 self.EnableGroup(GROUP_MENUS, True)
222 # But disable items
223 self.EnableGroupItems(GROUP_MENUS,
224 [ ID_NEW.TOOL,
225 ID_NEW.MENU_ITEM,
226 ID_NEW.SEPARATOR ],
227 False)
228 elif state == STATE_MENUBAR:
229 self.EnableGroup(GROUP_MENUS)
230 self.EnableGroupItems(GROUP_MENUS,
231 [ ID_NEW.TOOL_BAR,
232 ID_NEW.MENU_BAR,
233 ID_NEW.TOOL ],
234 False)
235 elif state == STATE_TOOLBAR:
236 self.EnableGroup(GROUP_MENUS)
237 self.EnableGroupItems(GROUP_MENUS,
238 [ ID_NEW.TOOL_BAR,
239 ID_NEW.MENU,
240 ID_NEW.MENU_BAR,
241 ID_NEW.MENU_ITEM ],
242 False)
243 self.EnableGroup(GROUP_CONTROLS)
244 self.EnableGroupItems(GROUP_CONTROLS,
245 [ ID_NEW.TREE_CTRL,
246 ID_NEW.NOTEBOOK ],
247 False)
248 elif state == STATE_MENU:
249 self.EnableGroup(GROUP_MENUS)
250 self.EnableGroupItems(GROUP_MENUS,
251 [ ID_NEW.TOOL_BAR,
252 ID_NEW.MENU_BAR,
253 ID_NEW.TOOL ],
254 False)
255 else:
256 self.EnableGroup(GROUP_WINDOWS)
257 self.EnableGroupItems(GROUP_WINDOWS,
258 [ ID_NEW.FRAME,
259 ID_NEW.DIALOG ],
260 False)
261 self.EnableGroup(GROUP_MENUS)
262 self.EnableGroupItems(GROUP_MENUS,
263 [ ID_NEW.MENU_BAR,
264 ID_NEW.MENU_BAR,
265 ID_NEW.MENU,
266 ID_NEW.MENU_ITEM,
267 ID_NEW.TOOL,
268 ID_NEW.SEPARATOR ],
269 False)
270 self.EnableGroup(GROUP_SIZERS)
271 self.EnableGroup(GROUP_CONTROLS)
272 # Special case for notebook (always executed)
273 if state == STATE_ELSE:
274 if xxx.__class__ == xxxNotebook:
275 self.EnableGroup(GROUP_SIZERS, False)
276 else:
277 self.EnableGroup(GROUP_SIZERS)
278 if not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
279 self.EnableGroupItem(GROUP_SIZERS, ID_NEW.SPACER, False)
280 # Save state
281 self.state = state