1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include <wx/wxprec.h>
31 #include <wx/toolbar.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 #if defined(__WXGTK__) || defined(__WXMOTIF__)
39 #include "mondrian.xpm"
40 #include "bitmaps/new.xpm"
41 #include "bitmaps/open.xpm"
42 #include "bitmaps/save.xpm"
43 #include "bitmaps/copy.xpm"
44 #include "bitmaps/cut.xpm"
45 #include "bitmaps/preview.xpm" // paste XPM
46 #include "bitmaps/print.xpm"
47 #include "bitmaps/help.xpm"
48 #endif // GTK or Motif
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // Define a new application
55 class MyApp
: public wxApp
62 class MyFrame
: public wxFrame
65 MyFrame(wxFrame
*parent
,
67 const wxString
& title
= "wxToolBar Sample",
68 const wxPoint
& pos
= wxDefaultPosition
,
69 const wxSize
& size
= wxDefaultSize
,
70 long style
= wxDEFAULT_FRAME_STYLE
);
72 void RecreateToolbar();
74 void OnQuit(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
77 void OnToggleToolbarSize(wxCommandEvent
& event
);
78 void OnToggleToolbarOrient(wxCommandEvent
& event
);
80 void OnEnablePrint(wxCommandEvent
& event
) { DoEnablePrint(); }
81 void OnDeletePrint(wxCommandEvent
& event
) { DoDeletePrint(); }
82 void OnInsertPrint(wxCommandEvent
& event
);
83 void OnToggleHelp(wxCommandEvent
& event
) { DoToggleHelp(); }
85 void OnToolLeftClick(wxCommandEvent
& event
);
86 void OnToolEnter(wxCommandEvent
& event
);
88 void OnCombo(wxCommandEvent
& event
);
90 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
99 wxTextCtrl
* m_textWindow
;
101 DECLARE_EVENT_TABLE()
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 const int ID_TOOLBAR
= 500;
112 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
113 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
114 IDM_TOOLBAR_ENABLEPRINT
,
115 IDM_TOOLBAR_DELETEPRINT
,
116 IDM_TOOLBAR_INSERTPRINT
,
117 IDM_TOOLBAR_TOGGLEHELP
,
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
129 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
130 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
131 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
133 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
134 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
136 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
137 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
138 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
139 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
141 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
143 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
145 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
147 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
148 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
161 // The `main program' equivalent, creating the windows and returning the
165 // Create the main frame window
166 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
168 wxPoint(100, 100), wxSize(450, 300));
170 // VZ: what's this for??
172 // Force a resize. This should probably be replaced by a call to a wxFrame
173 // function that lays out default decorations and the remaining content window.
174 wxSizeEvent
event(wxSize(-1, -1), frame
->GetId());
175 frame
->OnSize(event
);
180 frame
->SetStatusText("Hello, wxWindows");
187 void MyFrame::RecreateToolbar()
189 // delete and recreate the toolbar
190 wxToolBar
*toolBar
= GetToolBar();
195 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
196 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
198 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
199 toolBar
->SetMargins( 4, 4 );
202 wxBitmap toolBarBitmaps
[8];
204 toolBarBitmaps
[0] = wxBITMAP(new);
205 toolBarBitmaps
[1] = wxBITMAP(open
);
206 if ( !m_smallToolbar
)
208 toolBarBitmaps
[2] = wxBITMAP(save
);
209 toolBarBitmaps
[3] = wxBITMAP(copy
);
210 toolBarBitmaps
[4] = wxBITMAP(cut
);
211 toolBarBitmaps
[5] = wxBITMAP(paste
);
212 toolBarBitmaps
[6] = wxBITMAP(print
);
213 toolBarBitmaps
[7] = wxBITMAP(help
);
224 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
225 currentX
+= width
+ 5;
226 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
228 // adding a combo to a vertical toolbar is not very smart
231 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
232 combo
->Append("This");
233 combo
->Append("is a");
234 combo
->Append("combobox");
235 combo
->Append("in a");
236 combo
->Append("toolbar");
237 toolBar
->AddControl(combo
);
240 if ( !m_smallToolbar
)
242 currentX
+= width
+ 5;
243 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
244 currentX
+= width
+ 5;
245 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
246 currentX
+= width
+ 5;
247 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
248 currentX
+= width
+ 5;
249 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
250 currentX
+= width
+ 5;
251 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
252 currentX
+= width
+ 5;
253 toolBar
->AddSeparator();
254 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
257 // after adding the buttons to the toolbar, must call Realize() to reflect
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 // Define my frame constructor
267 MyFrame::MyFrame(wxFrame
* parent
,
269 const wxString
& title
,
273 : wxFrame(parent
, id
, title
, pos
, size
, style
)
275 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
276 m_smallToolbar
= FALSE
;
277 m_horzToolbar
= FALSE
;
279 // Give it a status line
283 SetIcon(wxICON(mondrian
));
286 wxMenu
*tbarMenu
= new wxMenu
;
287 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
288 "&Toggle toolbar size\tCtrl-S",
289 "Toggle between big/small toolbar",
291 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
292 "Toggle toolbar &orientation\tCtrl-O",
293 "Toggle toolbar orientation",
296 tbarMenu
->AppendSeparator();
298 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
299 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
300 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
301 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
303 wxMenu
*fileMenu
= new wxMenu
;
304 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
306 wxMenu
*helpMenu
= new wxMenu
;
307 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
309 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
311 menuBar
->Append(fileMenu
, "&File");
312 menuBar
->Append(tbarMenu
, "&Toolbar");
313 menuBar
->Append(helpMenu
, "&Help");
315 // Associate the menu bar with the frame
318 // Create the toolbar
322 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
324 m_smallToolbar
= !m_smallToolbar
;
329 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
331 m_horzToolbar
= !m_horzToolbar
;
336 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
341 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
343 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
346 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
349 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
350 m_textWindow
->WriteText( str
);
352 if (event
.GetId() == wxID_HELP
)
354 if ( event
.GetExtraLong() != 0 )
355 m_textWindow
->WriteText( _T("Help button down now.\n") );
357 m_textWindow
->WriteText( _T("Help button up now.\n") );
360 if (event
.GetId() == wxID_COPY
)
365 if (event
.GetId() == wxID_CUT
)
370 if (event
.GetId() == wxID_PRINT
)
376 void MyFrame::OnCombo(wxCommandEvent
& event
)
378 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
381 void MyFrame::DoEnablePrint()
383 wxToolBar
*tb
= GetToolBar();
384 if (tb
->GetToolEnabled(wxID_PRINT
))
385 tb
->EnableTool( wxID_PRINT
, FALSE
);
387 tb
->EnableTool( wxID_PRINT
, TRUE
);
390 void MyFrame::DoDeletePrint()
392 wxToolBar
*tb
= GetToolBar();
394 tb
->DeleteTool( wxID_PRINT
);
397 void MyFrame::DoToggleHelp()
399 wxToolBar
*tb
= GetToolBar();
400 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
403 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
405 event
.Enable( m_textWindow
->CanCopy() );
408 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
410 wxBitmap bmp
= wxBITMAP(print
);
412 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
413 FALSE
, (wxObject
*) NULL
,
415 "This button was inserted into the toolbar");
417 GetToolBar()->Realize();
420 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
422 if (event
.GetSelection() > -1)
425 str
.Printf(_T("This is tool number %d"), event
.GetSelection());