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