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