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