📄 toolbar.py
字号:
# toolbar.py: wxToolBar objects# $Id: toolbar.py,v 1.26 2007/08/07 12:18:33 agriggio Exp $## Copyright (c) 2002-2007 Alberto Griggio <agriggio@users.sourceforge.net># License: MIT (see license.txt)# THIS PROGRAM COMES WITH NO WARRANTYimport wx#from wxPython.lib.filebrowsebutton import FileBrowseButtonfrom wx.lib.filebrowsebutton import FileBrowseButtonimport common, math, misc, osfrom tree import Treefrom tool import *from widget_properties import *from edit_windows import EditBase, TopLevelBase, PreviewMixinclass _MyBrowseButton(FileBrowseButton): def createBrowseButton( self): """Create the browse-button control""" ID = wx.NewId() button =wx.Button(self, ID, misc.wxstr(self.buttonText)) button.SetToolTipString(misc.wxstr(self.toolTip)) w = button.GetTextExtent(self.buttonText)[0] + 10 if not misc.check_wx_version(2, 5, 2): button.SetSize((w, -1)) else: button.SetMinSize((w, -1)) wx.EVT_BUTTON(button, ID, self.OnBrowse) return button def OnBrowse (self, event=None): """ Going to browse for file... """ current = self.GetValue() directory = os.path.split(current) if os.path.isdir(current): directory = current current = '' elif directory and os.path.isdir(directory[0]): current = directory[1] directory = directory [0] else: directory = self.startDirectory value = misc.FileSelector(self.dialogTitle, directory, current, wildcard=self.fileMask, flags=self.fileMode) if value: self.SetValue(value)# end of class _MyBrowseButtonclass ToolsDialog(wx.Dialog): def __init__(self, parent, owner, items=None): wx.Dialog.__init__(self, parent, -1, _("Toolbar editor"), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) ADD_ID, REMOVE_ID, NAME_ID, LABEL_ID, ID_ID, CHECK_RADIO_ID, LIST_ID, \ ADD_SEP_ID, MOVE_UP_ID, MOVE_DOWN_ID, HELP_STR_ID, \ LONG_HELP_STR_ID, BITMAP1_ID, BITMAP2_ID \ = [wx.NewId() for i in range(14)] self._staticbox = wx.StaticBox(self, -1, _("Tool:")) self.owner = owner self.tool_items = wx.ListCtrl(self, LIST_ID, style=wx.LC_REPORT | \ wx.LC_SINGLE_SEL|wx.SUNKEN_BORDER, size=(300, -1)) self.selected_index = -1 # index of the selected element in the # wxListCtrl self.tool_items.InsertColumn(0, _("Label")) self.tool_items.InsertColumn(1, _("Id")) self.tool_items.InsertColumn(2, _("Normal Bitmap")) self.tool_items.InsertColumn(3, _("Second Bitmap")) self.tool_items.InsertColumn(4, _("Short Help")) self.tool_items.InsertColumn(5, _("Long Help")) self.tool_items.InsertColumn(6, _("Type")) # ALB 2004-12-05 self.tool_items.InsertColumn(7, _("Event Handler")) self.tool_items.SetColumnWidth(0, 100) self.tool_items.SetColumnWidth(2, 100) self.tool_items.SetColumnWidth(3, 150) self.tool_items.SetColumnWidth(4, 150) self.tool_items.SetColumnWidth(5, 100) self.tool_items.SetColumnWidth(6, 150) self.tool_items.SetColumnWidth(7, 150) # tool fields self.id = wx.TextCtrl(self, ID_ID) self.label = wx.TextCtrl(self, LABEL_ID) self.help_str = wx.TextCtrl(self, HELP_STR_ID) self.long_help_str = wx.TextCtrl(self, LONG_HELP_STR_ID) # ALB 2004-12-05 self.event_handler = wx.TextCtrl(self, -1) import re self.handler_re = re.compile(r'^\s*\w*\s*$') self.bitmap1 = _MyBrowseButton( self, BITMAP1_ID, labelText=_('Normal Bitmap'), buttonText='...', changeCallback=self.update_tool) self.bitmap2 = _MyBrowseButton( self, BITMAP2_ID, labelText=_('Second Bitmap'), buttonText='...', changeCallback=self.update_tool) self.check_radio = wx.RadioBox( self, CHECK_RADIO_ID, _("Type"), choices=['Normal', 'Checkable', 'Radio'], majorDimension=3) self.add = wx.Button(self, ADD_ID, _("Add")) self.remove = wx.Button(self, REMOVE_ID, _("Remove")) self.add_sep = wx.Button(self, ADD_SEP_ID, _("Add separator")) # tools navigation self.move_up = wx.Button(self, MOVE_UP_ID, _("Up")) self.move_down = wx.Button(self, MOVE_DOWN_ID, _("Down")) self.ok = wx.Button(self, wx.ID_OK, _("OK")) self.apply = wx.Button(self, wx.ID_APPLY, _("Apply")) self.cancel = wx.Button(self, wx.ID_CANCEL, _("Cancel")) self.do_layout() # event handlers wx.EVT_BUTTON(self, ADD_ID, self.add_tool) wx.EVT_BUTTON(self, REMOVE_ID, self.remove_tool) wx.EVT_BUTTON(self, ADD_SEP_ID, self.add_separator) wx.EVT_BUTTON(self, MOVE_UP_ID, self.move_item_up) wx.EVT_BUTTON(self, MOVE_DOWN_ID, self.move_item_down) wx.EVT_BUTTON(self, wx.ID_APPLY, self.on_apply) wx.EVT_KILL_FOCUS(self.label, self.update_tool) wx.EVT_KILL_FOCUS(self.id, self.update_tool) wx.EVT_KILL_FOCUS(self.help_str, self.update_tool) wx.EVT_KILL_FOCUS(self.long_help_str, self.update_tool) wx.EVT_KILL_FOCUS(self.event_handler, self.update_tool) wx.EVT_RADIOBOX(self, CHECK_RADIO_ID, self.update_tool) wx.EVT_LIST_ITEM_SELECTED(self, LIST_ID, self.show_tool) if items: self.add_tools(items) def do_layout(self): self.label.Enable(False) self.id.Enable(False) self.help_str.Enable(False) self.long_help_str.Enable(False) self.event_handler.Enable(False) self.bitmap1.Enable(False) self.bitmap2.Enable(False) self.check_radio.Enable(False) sizer = wx.BoxSizer(wx.VERTICAL) sizer2 = wx.StaticBoxSizer(self._staticbox, wx.VERTICAL) self.label.SetSize((150, -1)) self.id.SetSize((150, -1)) self.help_str.SetSize((150, -1)) self.long_help_str.SetSize((150, -1)) self.event_handler.SetSize((150, -1)) szr = wx.FlexGridSizer(0, 2) if misc.check_wx_version(2, 5, 2): flag = wx.FIXED_MINSIZE else: flag = 0 label_flag = wx.ALIGN_CENTER_VERTICAL szr.Add(wx.StaticText(self, -1, _("Id ")), flag=label_flag) szr.Add(self.id, flag=flag) szr.Add(wx.StaticText(self, -1, _("Label ")), flag=label_flag) szr.Add(self.label, flag=flag) szr.Add(wx.StaticText(self, -1, _("Short Help ")), flag=label_flag) szr.Add(self.help_str, flag=flag) szr.Add(wx.StaticText(self, -1, _("Long Help ")), flag=label_flag) szr.Add(self.long_help_str, flag=flag) szr.Add(wx.StaticText(self, -1, _("Event Handler ")), flag=label_flag) szr.Add(self.event_handler, flag=flag) sizer2.Add(szr, 1, wx.ALL|wx.EXPAND, 5) label_w = self.bitmap1.browseButton.GetTextExtent('...')[0] sizer2.Add(self.bitmap1, 0, wx.EXPAND) sizer2.Add(self.bitmap2, 0, wx.EXPAND) sizer2.Add(self.check_radio, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM, 4) szr = wx.GridSizer(0, 2, 3, 3) szr.Add(self.add, 0, wx.EXPAND); szr.Add(self.remove, 0, wx.EXPAND) sizer2.Add(szr, 0, wx.EXPAND) sizer2.Add(self.add_sep, 0, wx.TOP|wx.EXPAND, 3) sizer3 = wx.BoxSizer(wx.VERTICAL) sizer3.Add(self.tool_items, 1, wx.ALL|wx.EXPAND, 5) sizer4 = wx.BoxSizer(wx.HORIZONTAL) sizer4.Add(self.move_up, 0, wx.LEFT|wx.RIGHT, 3) sizer4.Add(self.move_down, 0, wx.LEFT|wx.RIGHT, 5) sizer3.Add(sizer4, 0, wx.ALIGN_CENTER|wx.ALL, 5) szr = wx.BoxSizer(wx.HORIZONTAL) szr.Add(sizer3, 1, wx.ALL|wx.EXPAND, 5) szr.Add(sizer2, 0, wx.TOP|wx.BOTTOM|wx.RIGHT, 5) sizer.Add(szr, 1, wx.EXPAND) sizer2 = wx.BoxSizer(wx.HORIZONTAL) sizer2.Add(self.ok, 0, wx.ALL, 5) sizer2.Add(self.apply, 0, wx.ALL, 5) sizer2.Add(self.cancel, 0, wx.ALL, 5) sizer.Add(sizer2, 0, wx.ALL|wx.ALIGN_CENTER, 3) self.SetAutoLayout(1) self.SetSizer(sizer) sizer.Fit(self) #self.SetSize((-1, 350)) self.CenterOnScreen() def add_tool(self, event): """\ Event handler called when the Add button is clicked """ index = self.selected_index = self.selected_index+1 if not self.tool_items.GetItemCount(): for s in (self.label, self.id, self.help_str, self.long_help_str, self.bitmap1, self.bitmap2, self.check_radio, self.event_handler): s.Enable(True) if index < 0: index = self.tool_items.GetItemCount() name, label, id, check_radio = "", "item", "", "0" bitmap1, bitmap2, help_str, long_help_str = [""] * 4 self.tool_items.InsertStringItem(index, label) self.tool_items.SetStringItem(index, 1, id) self.tool_items.SetStringItem(index, 2, bitmap1) self.tool_items.SetStringItem(index, 3, bitmap2) self.tool_items.SetStringItem(index, 4, help_str) self.tool_items.SetStringItem(index, 5, long_help_str) self.tool_items.SetStringItem(index, 6, check_radio) self.tool_items.SetItemState(index, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED) self.label.SetValue(label) self.id.SetValue(id) self.check_radio.SetSelection(int(check_radio)) self.bitmap1.SetValue(bitmap1, False) self.bitmap2.SetValue(bitmap2, False) self.help_str.SetValue(help_str) self.long_help_str.SetValue(long_help_str) self.event_handler.SetValue("") def add_separator(self, event): """\ Event handler called when the Add Separator button is clicked """ index = self.selected_index+1 if not self.tool_items.GetItemCount(): for s in (self.label, self.id, self.help_str, self.long_help_str, self.bitmap1, self.bitmap2, self.check_radio, self.event_handler): s.Enable(True) if index < 0: index = self.tool_items.GetItemCount() self.tool_items.InsertStringItem(index, '---')#label) for i in range(1, 5): self.tool_items.SetStringItem(index, i, '---') self.tool_items.SetItemState(index, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED) def show_tool(self, event): """\ Event handler called when a tool in the list is selected """ self.selected_index = index = event.GetIndex() get_item = self.tool_items.GetItem if not self.tool_items.GetItem(index, 2).m_text == '---': # skip if the selected item is a separator for (s, i) in ((self.label, 0), (self.id, 1), (self.help_str, 4), (self.long_help_str, 5), (self.event_handler, 7)): s.SetValue(get_item(index, i).m_text) self.bitmap1.SetValue(get_item(index, 2).m_text, False) self.bitmap2.SetValue(get_item(index, 3).m_text, False) try: self.check_radio.SetSelection( int(self.tool_items.GetItem(index, 6).m_text)) except: self.check_radio.SetSelection(0) event.Skip() def update_tool(self, event): """\ Event handler called when some of the properties of the current tool changes """ set_item = self.tool_items.SetStringItem index = self.selected_index handler = self.event_handler.GetValue() if not self.handler_re.match(handler): event.GetEventObject().SetFocus() return if index < 0: return event.Skip() set_item(index, 0, self.label.GetValue()) set_item(index, 1, self.id.GetValue()) set_item(index, 2, self.bitmap1.GetValue()) set_item(index, 3, self.bitmap2.GetValue()) set_item(index, 4, self.help_str.GetValue()) set_item(index, 5, self.long_help_str.GetValue()) set_item(index, 6, str(self.check_radio.GetSelection())) set_item(index, 7, self.event_handler.GetValue()) try: event.Skip() except AttributeError: # this happens on wx2.4.0.1 for FileBrowseButton events pass # update the directory of the browse buttons directory = os.path.split(self.bitmap1.GetValue())[0] if not os.path.isdir(directory): directory = os.path.split(self.bitmap2.GetValue())[0] if os.path.isdir(directory): self.bitmap1.startDirectory = directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -