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