| 1 | |
| 2 | import wx |
| 3 | import images |
| 4 | |
| 5 | #--------------------------------------------------------------------------- |
| 6 | |
| 7 | class TestToolBar(wx.Frame): |
| 8 | def __init__(self, parent, log): |
| 9 | wx.Frame.__init__(self, parent, -1, 'Test ToolBar', size=(500, 300)) |
| 10 | self.log = log |
| 11 | self.timer = None |
| 12 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
| 13 | |
| 14 | wx.Window(self, -1).SetBackgroundColour(wx.NamedColour("WHITE")) |
| 15 | |
| 16 | # Use the wxFrame internals to create the toolbar and associate it all |
| 17 | # in one tidy method call. |
| 18 | tb = self.CreateToolBar( wx.TB_HORIZONTAL |
| 19 | | wx.NO_BORDER |
| 20 | | wx.TB_FLAT |
| 21 | | wx.TB_TEXT |
| 22 | ) |
| 23 | |
| 24 | # Here's a 'simple' toolbar example, and how to bind it using SetToolBar() |
| 25 | #tb = wx.ToolBarSimple(self, -1, wx.DefaultPosition, wx.DefaultSize, |
| 26 | # wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT) |
| 27 | #self.SetToolBar(tb) |
| 28 | # But we're doing it a different way here. |
| 29 | |
| 30 | log.write("Default toolbar tool size: %s\n" % tb.GetToolBitmapSize()) |
| 31 | |
| 32 | self.CreateStatusBar() |
| 33 | |
| 34 | tsize = (16,16) |
| 35 | new_bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, tsize) |
| 36 | open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize) |
| 37 | copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR, tsize) |
| 38 | paste_bmp= wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR, tsize) |
| 39 | |
| 40 | tb.AddSimpleTool(10, new_bmp, "New", "Long help for 'New'") |
| 41 | #tb.AddLabelTool(10, "New", new_bmp, shortHelp="New", longHelp="Long help for 'New'") |
| 42 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=10) |
| 43 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=10) |
| 44 | |
| 45 | tb.AddSimpleTool(20, open_bmp, "Open", "Long help for 'Open'") |
| 46 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=20) |
| 47 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=20) |
| 48 | |
| 49 | tb.AddSeparator() |
| 50 | tb.AddSimpleTool(30, copy_bmp, "Copy", "Long help for 'Copy'") |
| 51 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=30) |
| 52 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=30) |
| 53 | |
| 54 | tb.AddSimpleTool(40, paste_bmp, "Paste", "Long help for 'Paste'") |
| 55 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=40) |
| 56 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=40) |
| 57 | |
| 58 | tb.AddSeparator() |
| 59 | |
| 60 | tool = tb.AddCheckTool(50, images.getTog1Bitmap(), |
| 61 | shortHelp="Toggle this") |
| 62 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=50) |
| 63 | |
| 64 | ## tb.AddCheckTool(60, images.getTog1Bitmap(), images.getTog2Bitmap(), |
| 65 | ## shortHelp="Toggle with 2 bitmaps") |
| 66 | ## self.Bind(EVT_TOOL, self.OnToolClick, id=60) |
| 67 | |
| 68 | self.Bind(wx.EVT_TOOL_ENTER, self.OnToolEnter) |
| 69 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick) # Match all |
| 70 | self.Bind(wx.EVT_TIMER, self.OnClearSB) |
| 71 | |
| 72 | tb.AddSeparator() |
| 73 | cbID = wx.NewId() |
| 74 | |
| 75 | tb.AddControl( |
| 76 | wx.ComboBox( |
| 77 | tb, cbID, "", choices=["", "This", "is a", "wxComboBox"], |
| 78 | size=(150,-1), style=wx.CB_DROPDOWN |
| 79 | )) |
| 80 | |
| 81 | self.Bind(wx.EVT_COMBOBOX, self.OnCombo, id=cbID) |
| 82 | tb.AddControl(wx.TextCtrl(tb, -1, "Toolbar controls!!", size=(150, -1))) |
| 83 | |
| 84 | # Final thing to do for a toolbar is call the Realize() method. This |
| 85 | # causes it to render (more or less, that is). |
| 86 | tb.Realize() |
| 87 | |
| 88 | |
| 89 | def OnToolClick(self, event): |
| 90 | self.log.WriteText("tool %s clicked\n" % event.GetId()) |
| 91 | tb = self.GetToolBar() |
| 92 | tb.EnableTool(10, not tb.GetToolEnabled(10)) |
| 93 | |
| 94 | def OnToolRClick(self, event): |
| 95 | self.log.WriteText("tool %s right-clicked\n" % event.GetId()) |
| 96 | |
| 97 | def OnCombo(self, event): |
| 98 | self.log.WriteText("combobox item selected: %s\n" % event.GetString()) |
| 99 | |
| 100 | def OnToolEnter(self, event): |
| 101 | self.log.WriteText('OnToolEnter: %s, %s\n' % (event.GetId(), event.GetInt())) |
| 102 | |
| 103 | if self.timer is None: |
| 104 | self.timer = wx.Timer(self) |
| 105 | |
| 106 | if self.timer.IsRunning(): |
| 107 | self.timer.Stop() |
| 108 | |
| 109 | self.timer.Start(2000) |
| 110 | event.Skip() |
| 111 | |
| 112 | |
| 113 | def OnClearSB(self, event): # called for the timer event handler |
| 114 | self.SetStatusText("") |
| 115 | self.timer.Stop() |
| 116 | self.timer = None |
| 117 | |
| 118 | |
| 119 | def OnCloseWindow(self, event): |
| 120 | if self.timer is not None: |
| 121 | self.timer.Stop() |
| 122 | self.timer = None |
| 123 | self.Destroy() |
| 124 | |
| 125 | #--------------------------------------------------------------------------- |
| 126 | |
| 127 | class TestPanel(wx.Panel): |
| 128 | def __init__(self, parent, log): |
| 129 | self.log = log |
| 130 | wx.Panel.__init__(self, parent, -1) |
| 131 | |
| 132 | b = wx.Button(self, -1, "Show the ToolBar sample", (50,50)) |
| 133 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) |
| 134 | |
| 135 | |
| 136 | def OnButton(self, evt): |
| 137 | win = TestToolBar(self, self.log) |
| 138 | win.Show(True) |
| 139 | |
| 140 | |
| 141 | #--------------------------------------------------------------------------- |
| 142 | |
| 143 | |
| 144 | def runTest(frame, nb, log): |
| 145 | win = TestPanel(nb, log) |
| 146 | return win |
| 147 | |
| 148 | #--------------------------------------------------------------------------- |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | overview = """\ |
| 154 | wx.ToolBar is a narrow strip of icons on one side of a frame (top, bottom, sides) |
| 155 | that acts much like a menu does, except it is always visible. Additionally, actual |
| 156 | wxWindows controls, such as wx.TextCtrl or wx.ComboBox, can be added to the toolbar |
| 157 | and used from within it. |
| 158 | |
| 159 | Toolbar creation is a two-step process. First, the toolbar is defined using the |
| 160 | various Add* methods of wx.ToolBar. Once all is set up, then wx.Toolbar.Realize() |
| 161 | must be called to render it. |
| 162 | |
| 163 | wx.Toolbar events are also propogated as Menu events; this is especially handy when |
| 164 | you have a menu bar that contains items that carry out the same function. For example, |
| 165 | it is not uncommon to have a little 'floppy' toolbar icon to 'save' the current file |
| 166 | (whatever it is) as well as a FILE/SAVE menu item that does the same thing. In this |
| 167 | case, both events can be captured and acted upon using the same event handler |
| 168 | with no ill effects. |
| 169 | |
| 170 | If there are cases where a toolbar icon should *not* be associated with a menu item, |
| 171 | use a unique ID to trap it. |
| 172 | |
| 173 | There are a number of ways to create a toolbar for a wx.Frame. wx.Frame.CreateToolBar() |
| 174 | does all the work except it adds no buttons at all unless you override the virtual method |
| 175 | OnCreateToolBar(). On the other hand, you can just subclass wx.ToolBar and then use |
| 176 | wx.Frame.SetToolBar() instead. |
| 177 | |
| 178 | Note that wx.TB_DOCKABLE is only supported under GTK. An attempt to alleviate this |
| 179 | is provided in wx.lib.floatbar, but it is not formally supported. |
| 180 | """ |
| 181 | |
| 182 | |
| 183 | if __name__ == '__main__': |
| 184 | import sys,os |
| 185 | import run |
| 186 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 187 | |