]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import wx | |
3 | import images | |
4 | ||
5 | #--------------------------------------------------------------------------- | |
6 | ||
7 | class TestToolBar(wx.Frame): | |
8 | def __init__(self, parent, log): | |
9 | wx.Frame.__init__(self, parent, -1, 'Test ToolBar', size=(500, 300)) | |
10 | self.log = log | |
11 | self.timer = None | |
12 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) | |
13 | ||
14 | wx.Window(self, -1).SetBackgroundColour(wx.NamedColour("WHITE")) | |
15 | ||
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 | |
22 | ) | |
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) | |
27 | #self.SetToolBar(tb) | |
28 | # But we're doing it a different way here. | |
29 | ||
30 | log.write("Default toolbar tool size: %s\n" % tb.GetToolBitmapSize()) | |
31 | ||
32 | self.CreateStatusBar() | |
33 | ||
34 | tb.AddSimpleTool(10, images.getNewBitmap(), "New", "Long help for 'New'") | |
35 | #tb.AddLabelTool(10, "New", images.getNewBitmap(), shortHelp="New", longHelp="Long help for 'New'") | |
36 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=10) | |
37 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=10) | |
38 | ||
39 | tb.AddSimpleTool(20, images.getOpenBitmap(), "Open", "Long help for 'Open'") | |
40 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=20) | |
41 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=20) | |
42 | ||
43 | tb.AddSeparator() | |
44 | tb.AddSimpleTool(30, images.getCopyBitmap(), "Copy", "Long help for 'Copy'") | |
45 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=30) | |
46 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=30) | |
47 | ||
48 | tb.AddSimpleTool(40, images.getPasteBitmap(), "Paste", "Long help for 'Paste'") | |
49 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=40) | |
50 | self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=40) | |
51 | ||
52 | tb.AddSeparator() | |
53 | ||
54 | tool = tb.AddCheckTool(50, images.getTog1Bitmap(), | |
55 | shortHelp="Toggle this") | |
56 | self.Bind(wx.EVT_TOOL, self.OnToolClick, id=50) | |
57 | ||
58 | ## tb.AddCheckTool(60, images.getTog1Bitmap(), images.getTog2Bitmap(), | |
59 | ## shortHelp="Toggle with 2 bitmaps") | |
60 | ## self.Bind(EVT_TOOL, self.OnToolClick, id=60) | |
61 | ||
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) | |
65 | ||
66 | tb.AddSeparator() | |
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). | |
80 | tb.Realize() | |
81 | ||
82 | ||
83 | def OnToolClick(self, event): | |
84 | self.log.WriteText("tool %s clicked\n" % event.GetId()) | |
85 | tb = self.GetToolBar() | |
86 | tb.EnableTool(10, not tb.GetToolEnabled(10)) | |
87 | ||
88 | def OnToolRClick(self, event): | |
89 | self.log.WriteText("tool %s right-clicked\n" % event.GetId()) | |
90 | ||
91 | def OnCombo(self, event): | |
92 | self.log.WriteText("combobox item selected: %s\n" % event.GetString()) | |
93 | ||
94 | def OnToolEnter(self, event): | |
95 | self.log.WriteText('OnToolEnter: %s, %s\n' % (event.GetId(), event.GetInt())) | |
96 | ||
97 | if self.timer is None: | |
98 | self.timer = wx.Timer(self) | |
99 | ||
100 | if self.timer.IsRunning(): | |
101 | self.timer.Stop() | |
102 | ||
103 | self.timer.Start(2000) | |
104 | event.Skip() | |
105 | ||
106 | ||
107 | def OnClearSB(self, event): # called for the timer event handler | |
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() | |
118 | ||
119 | #--------------------------------------------------------------------------- | |
120 | ||
121 | def runTest(frame, nb, log): | |
122 | win = TestToolBar(frame, log) | |
123 | frame.otherWin = win | |
124 | win.Show(True) | |
125 | ||
126 | #--------------------------------------------------------------------------- | |
127 | ||
128 | ||
129 | ||
130 | overview = """\ | |
131 | wx.ToolBar is a narrow strip of icons on one side of a frame (top, bottom, sides) | |
132 | that acts much like a menu does, except it is always visible. Additionally, actual | |
133 | wxWindows controls, such as wx.TextCtrl or wx.ComboBox, can be added to the toolbar | |
134 | and used from within it. | |
135 | ||
136 | Toolbar creation is a two-step process. First, the toolbar is defined using the | |
137 | various Add* methods of wx.ToolBar. Once all is set up, then wx.Toolbar.Realize() | |
138 | must be called to render it. | |
139 | ||
140 | wx.Toolbar events are also propogated as Menu events; this is especially handy when | |
141 | you have a menu bar that contains items that carry out the same function. For example, | |
142 | it is not uncommon to have a little 'floppy' toolbar icon to 'save' the current file | |
143 | (whatever it is) as well as a FILE/SAVE menu item that does the same thing. In this | |
144 | case, both events can be captured and acted upon using the same event handler | |
145 | with no ill effects. | |
146 | ||
147 | If there are cases where a toolbar icon should *not* be associated with a menu item, | |
148 | use a unique ID to trap it. | |
149 | ||
150 | There are a number of ways to create a toolbar for a wx.Frame. wx.Frame.CreateToolBar() | |
151 | does all the work except it adds no buttons at all unless you override the virtual method | |
152 | OnCreateToolBar(). On the other hand, you can just subclass wx.ToolBar and then use | |
153 | wx.Frame.SetToolBar() instead. | |
154 | ||
155 | Note that wx.TB_DOCKABLE is only supported under GTK. An attempt to alleviate this | |
156 | is provided in wx.lib.floatbar, but it is not formally supported. | |
157 | """ | |
158 | ||
159 | ||
160 | if __name__ == '__main__': | |
161 | import sys,os | |
162 | import run | |
163 | run.main(['', os.path.basename(sys.argv[0])]) | |
164 |