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