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