📄 toolbar.py
字号:
if value[v]: self.style |= self.style_pos[v] if self._tb: self._tb.SetWindowStyleFlag(self.style) if refresh: self._refresh_widget() def get_margins(self): return self.margins def set_margins(self, value, refresh=True): try: margins = [int(t.strip()) for t in value.split(',')] except: self.properties['margins'].set_value(self.margins) else: self.margins = value if self._tb: self._tb.SetMargins(margins) if refresh: self._refresh_widget() def get_packing(self): return self.packing def set_packing(self, value, refresh=True): try: value = int(value) except: self.properties['packing'].set_value(self.packing) else: self.packing = value if self._tb: self._tb.SetToolPacking(self.packing) if refresh: self._refresh_widget() def get_separation(self): return self.separation def set_separation(self, value, refresh=True): try: value = int(value) except: self.properties['separation'].set_value(self.separation) else: self.separation = value if self._tb: self._tb.SetToolSeparation(self.separation) if refresh: self._refresh_widget() def get_bitmapsize(self): return self.bitmapsize def set_bitmapsize(self, value, refresh=True): try: size = [int(t.strip()) for t in value.split(',')] except: self.properties['bitmapsize'].set_value(self.bitmapsize) else: self.bitmapsize = value if self._tb: self._tb.SetToolBitmapSize(size) if refresh: self._refresh_widget() def get_tools(self): return self.tools def set_tools(self, tools): self.tools = tools if not self._tb: return # nothing left to do while self._tb.DeleteToolByPos(0): pass # clear the toolbar # now add all the tools for tool in self.tools: if misc.streq(tool.id, '---'): # the tool is a separator self._tb.AddSeparator() else: if tool.bitmap1: bmp1 = None if not (tool.bitmap1.startswith('var:') or tool.bitmap1.startswith('code:')): bmp1 = wx.Bitmap( misc.get_relative_path(misc.wxstr(tool.bitmap1)), wx.BITMAP_TYPE_ANY) if not bmp1 or not bmp1.Ok(): bmp1 = wx.EmptyBitmap(1, 1) else: bmp1 = wx.NullBitmap if tool.bitmap2: bmp2 = None if not (tool.bitmap2.startswith('var:') or tool.bitmap2.startswith('code:')): bmp2 = wx.Bitmap( misc.get_relative_path(misc.wxstr(tool.bitmap2)), wx.BITMAP_TYPE_ANY) if not bmp2 or not bmp2.Ok(): bmp2 = wx.EmptyBitmap(1, 1) else: bmp2 = wx.NullBitmap # signature of AddTool for 2.3.2.1: # wxToolBarToolBase *AddTool( # int id, const wxBitmap& bitmap, # const wxBitmap& pushedBitmap, bool toggle = FALSE, # wxObject *clientData = NULL, # const wxString& shortHelpString = wxEmptyString, # const wxString& longHelpString = wxEmptyString) if not misc.check_wx_version(2, 3, 3): # use the old signature, some of the entries are ignored self._tb.AddTool(wx.NewId(), bmp1, bmp2, tool.type == 1, shortHelpString=\ misc.wxstr(tool.short_help), longHelpString=misc.wxstr(tool.long_help)) else: kinds = [wx.ITEM_NORMAL, wx.ITEM_CHECK, wx.ITEM_RADIO] try: kind = kinds[int(tool.type)] except (ValueError, IndexError): kind = wx.ITEM_NORMAL self._tb.AddLabelTool(wx.NewId(), misc.wxstr(tool.label), bmp1, bmp2, kind, misc.wxstr(tool.short_help), misc.wxstr(tool.long_help)) # this is required to refresh the toolbar properly self._refresh_widget() def _refresh_widget(self): self._tb.Realize() self._tb.SetSize((-1, self._tb.GetBestSize()[1])) if self.parent: widget = self.parent.widget w, h = widget.GetClientSize() widget.SetClientSize((w, h+1)) widget.SetClientSize((w, h)) else: widget = self.widget w = widget.GetClientSize()[0] h = self._tb.GetSize()[1] / 2 widget.SetClientSize((w, h)) def remove(self, *args, **kwds): if self.parent is not None: self.parent.properties['toolbar'].set_value(0) if kwds.get('do_nothing', False): # and wxPlatform == '__WXGTK__': # this probably leaks memory, but avoids segfaults self.widget = None else: if self.parent.widget: self.parent.widget.SetToolBar(None) else: if self.widget: self.widget.Destroy() self.widget = None EditBase.remove(self) def popup_menu(self, event): if self.parent is not None: return # do nothing in this case if self.widget: if not self._rmenu: REMOVE_ID, HIDE_ID = [wx.NewId() for i in range(2)] self._rmenu = misc.wxGladePopupMenu(self.name) misc.append_item(self._rmenu, REMOVE_ID, _('Remove\tDel'), wx.ART_DELETE) misc.append_item(self._rmenu, HIDE_ID, _('Hide')) def bind(method): return lambda e: misc.wxCallAfter(method) wx.EVT_MENU(self.widget, REMOVE_ID, bind(self.remove)) wx.EVT_MENU(self.widget, HIDE_ID, bind(self.hide_widget)) self.widget.PopupMenu(self._rmenu, event.GetPosition()) def hide_widget(self, *args): if self.widget and self.widget is not self._tb: self.widget.Hide() common.app_tree.expand(self.node, False) common.app_tree.select_item(self.node.parent) common.app_tree.app.show_properties() def set_name(self, name): EditBase.set_name(self, name) if self.widget is not self._tb: self.widget.SetTitle(misc.design_title(misc.wxstr(self.name))) def get_property_handler(self, name): class ToolsHandler: itemattrs = ['label', 'id', 'short_help', 'long_help', 'bitmap1', 'bitmap2', 'type', 'handler'] def __init__(self, owner): self.owner = owner self.tools = [] self.curr_tool = None self.curr_index = -1 def start_elem(self, name, attrs): if name == 'tools': return if name == 'tool': self.curr_tool = Tool() else: try: self.curr_index = self.itemattrs.index(name) except ValueError: self.curr_index = -1 pass # just ignore the attributes we don't know## from xml_parse import XmlParsingError## raise XmlParsingError, _("invalid tool attribute") def end_elem(self, name): if name == 'tool': self.tools.append(self.curr_tool) if name == 'tools': self.owner.set_tools(self.tools) return True def char_data(self, data): if self.curr_index >= 0: setattr(self.curr_tool, self.itemattrs[self.curr_index], data) if name == 'tools': return ToolsHandler(self) return None# end of class EditToolBardef builder(parent, sizer, pos, number=[0]): """\ factory function for EditToolBar objects. """ class Dialog(wx.Dialog): def __init__(self): wx.Dialog.__init__(self, None, -1, _('Select toolbar class')) if common.app_tree.app.get_language().lower() == 'xrc': self.klass = 'wxToolBar' else: if not number[0]: self.klass = 'MyToolBar' else: self.klass = 'MyToolBar%s' % number[0] number[0] += 1 klass_prop = TextProperty(self, 'class', self) szr = wx.BoxSizer(wx.VERTICAL) szr.Add(klass_prop.panel, 0, wx.EXPAND) sz2 = wx.BoxSizer(wx.HORIZONTAL) sz2.Add(wx.Button(self, wx.ID_OK, _('OK')), 0, wx.ALL, 3) sz2.Add(wx.Button(self, wx.ID_CANCEL, _('Cancel')), 0, wx.ALL, 3) szr.Add(sz2, 0, wx.ALL|wx.ALIGN_CENTER, 3) self.SetAutoLayout(True) self.SetSizer(szr) szr.Fit(self) #self.SetSize((150, -1)) def undo(self): if number[0] > 0: number[0] -= 1 def __getitem__(self, value): if value == 'class': def set_klass(c): self.klass = c return (lambda : self.klass, set_klass) # end of inner class dialog = Dialog() if dialog.ShowModal() == wx.ID_CANCEL: # cancel the operation dialog.undo() dialog.Destroy() return name = 'toolbar_%d' % (number[0] or 1) while common.app_tree.has_name(name): number[0] += 1 name = 'toolbar_%d' % number[0] tb = EditToolBar(name, dialog.klass, parent, common.property_panel) tb.node = Tree.Node(tb) common.app_tree.add(tb.node) tb.show_widget(True) tb.show_properties() def xml_builder(attrs, parent, sizer, sizeritem, pos=None): """\ factory to build EditMenuBar objects from an xml file """ name = attrs.get('name') if parent is not None: if name: parent.toolbar.set_name(name) parent.toolbar.name_prop.set_value(name) return parent.toolbar else: tb = EditToolBar(name, attrs.get('class', 'wxMenuBar'), None, common.property_panel) tb.node = Tree.Node(tb) common.app_tree.add(tb.node) return tbdef initialize(): """\ initialization function for the module: returns a wxBitmapButton to be added to the main palette. """ cwx = common.widgets_from_xml cwx['EditToolBar'] = xml_builder common.widgets['EditToolBar'] = builder return common.make_object_button('EditToolBar', 'icons/toolbar.xpm', 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -