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 #include <wx/spinctrl.h>
36 // define this to 1 to use wxToolBarSimple instead of the native one
37 #define USE_GENERIC_TBAR 0
39 // define this to use XPMs everywhere (by default, BMPs are used under Win)
40 // BMPs use less space, but aren't compiled into the executable on other platforms
42 #define USE_XPM_BITMAPS 0
44 #define USE_XPM_BITMAPS 1
48 #if !wxUSE_TOOLBAR_SIMPLE
49 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
50 to 1 in setup.h and recompile the library.
52 #include <wx/tbarsmpl.h>
54 #endif // USE_GENERIC_TBAR
56 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
57 #error You need to enable XPM support to use XPM bitmaps with toolbar!
58 #endif // USE_XPM_BITMAPS
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
65 #include "mondrian.xpm"
66 #include "bitmaps/new.xpm"
67 #include "bitmaps/open.xpm"
68 #include "bitmaps/save.xpm"
69 #include "bitmaps/copy.xpm"
70 #include "bitmaps/cut.xpm"
71 #include "bitmaps/preview.xpm" // paste XPM
72 #include "bitmaps/print.xpm"
73 #include "bitmaps/help.xpm"
74 #endif // USE_XPM_BITMAPS
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // Define a new application
81 class MyApp
: public wxApp
88 class MyFrame
: public wxFrame
91 MyFrame(wxFrame
*parent
,
93 const wxString
& title
= "wxToolBar Sample",
94 const wxPoint
& pos
= wxDefaultPosition
,
95 const wxSize
& size
= wxDefaultSize
,
96 long style
= wxDEFAULT_FRAME_STYLE
);
98 void RecreateToolbar();
100 void OnQuit(wxCommandEvent
& event
);
101 void OnAbout(wxCommandEvent
& event
);
103 void OnSize(wxSizeEvent
& event
);
105 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
107 void OnToggleToolbarSize(wxCommandEvent
& event
);
108 void OnToggleToolbarOrient(wxCommandEvent
& event
);
109 void OnToggleToolbarRows(wxCommandEvent
& event
);
111 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
112 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
113 void OnInsertPrint(wxCommandEvent
& event
);
114 void OnChangeToolTip(wxCommandEvent
& event
);
115 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
117 void OnToolbarStyle(wxCommandEvent
& event
);
119 void OnToolLeftClick(wxCommandEvent
& event
);
120 void OnToolEnter(wxCommandEvent
& event
);
122 void OnCombo(wxCommandEvent
& event
);
124 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
127 virtual wxToolBar
*OnCreateToolBar(long style
,
129 const wxString
& name
);
130 #endif // USE_GENERIC_TBAR
133 void DoEnablePrint();
134 void DoDeletePrint();
137 void LayoutChildren();
141 size_t m_rows
; // 1 or 2 only
143 wxTextCtrl
*m_textWindow
;
147 DECLARE_EVENT_TABLE()
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 const int ID_TOOLBAR
= 500;
156 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
157 // static const long TOOLBAR_STYLE = 0;
161 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
162 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
163 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
164 IDM_TOOLBAR_ENABLEPRINT
,
165 IDM_TOOLBAR_DELETEPRINT
,
166 IDM_TOOLBAR_INSERTPRINT
,
167 IDM_TOOLBAR_TOGGLEHELP
,
168 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
169 IDM_TOOLBAR_CHANGE_TOOLTIP
,
170 IDM_TOOLBAR_SHOW_TEXT
,
171 IDM_TOOLBAR_SHOW_ICONS
,
172 IDM_TOOLBAR_SHOW_BOTH
,
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
184 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
185 EVT_SIZE(MyFrame::OnSize
)
187 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
188 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
190 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
192 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
193 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
194 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
196 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
197 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
198 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
199 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
200 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
202 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
203 MyFrame::OnToolbarStyle
)
205 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
207 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
209 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
211 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
212 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
215 // ============================================================================
217 // ============================================================================
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
225 // The `main program' equivalent, creating the windows and returning the
229 // Create the main frame window
230 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
232 wxPoint(100, 100), wxSize(550, 300));
236 frame
->SetStatusText("Hello, wxWindows");
243 void MyFrame::RecreateToolbar()
245 // delete and recreate the toolbar
246 wxToolBarBase
*toolBar
= GetToolBar();
247 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
253 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
);
254 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
256 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
257 //toolBar->SetMargins( 4, 4 );
260 wxBitmap toolBarBitmaps
[8];
263 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
264 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
265 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
266 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
267 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
268 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
269 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
270 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
271 #else // !USE_XPM_BITMAPS
272 toolBarBitmaps
[0] = wxBITMAP(new);
273 toolBarBitmaps
[1] = wxBITMAP(open
);
274 toolBarBitmaps
[2] = wxBITMAP(save
);
275 toolBarBitmaps
[3] = wxBITMAP(copy
);
276 toolBarBitmaps
[4] = wxBITMAP(cut
);
277 toolBarBitmaps
[5] = wxBITMAP(paste
);
278 toolBarBitmaps
[6] = wxBITMAP(print
);
279 toolBarBitmaps
[7] = wxBITMAP(help
);
280 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
282 if ( !m_smallToolbar
)
284 int w
= 2*toolBarBitmaps
[0].GetWidth(),
285 h
= 2*toolBarBitmaps
[0].GetHeight();
286 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
289 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
292 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
295 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
296 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
298 // neither the generic nor Motif native toolbars really support this
299 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
300 // adding a combo to a vertical toolbar is not very smart
303 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
304 combo
->Append("This");
305 combo
->Append("is a");
306 combo
->Append("combobox");
307 combo
->Append("in a");
308 combo
->Append("toolbar");
309 toolBar
->AddControl(combo
);
311 #endif // toolbars which don't support controls
313 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
314 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
315 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
316 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
317 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool"));
318 toolBar
->AddSeparator();
319 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
321 // after adding the buttons to the toolbar, must call Realize() to reflect
325 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 // Define my frame constructor
333 MyFrame::MyFrame(wxFrame
* parent
,
335 const wxString
& title
,
339 : wxFrame(parent
, id
, title
, pos
, size
, style
)
342 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
344 m_smallToolbar
= TRUE
;
345 m_horzToolbar
= TRUE
;
348 // Give it a status line
352 SetIcon(wxICON(mondrian
));
355 wxMenu
*tbarMenu
= new wxMenu
;
356 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
357 "Toggle &another toolbar\tCtrl-A",
358 "Show/hide another test toolbar",
361 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
362 "&Toggle toolbar size\tCtrl-S",
363 "Toggle between big/small toolbar",
365 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
366 "Toggle toolbar &orientation\tCtrl-O",
367 "Toggle toolbar orientation",
369 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
370 "Toggle number of &rows\tCtrl-R",
371 "Toggle number of toolbar rows between 1 and 2",
374 tbarMenu
->AppendSeparator();
376 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
377 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
378 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
379 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
380 tbarMenu
->AppendSeparator();
381 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
382 tbarMenu
->AppendSeparator();
383 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, "Show &text\tAlt-T");
384 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, "Show &icons\tAlt-I");
385 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, "Show &both\tAlt-B");
387 wxMenu
*fileMenu
= new wxMenu
;
388 fileMenu
->Append(wxID_EXIT
, "E&xit\tAlt-X", "Quit toolbar sample" );
390 wxMenu
*helpMenu
= new wxMenu
;
391 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
393 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
395 menuBar
->Append(fileMenu
, "&File");
396 menuBar
->Append(tbarMenu
, "&Toolbar");
397 menuBar
->Append(helpMenu
, "&Help");
399 // Associate the menu bar with the frame
402 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
404 // Create the toolbar
410 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
412 const wxString
& name
)
414 return (wxToolBar
*)new wxToolBarSimple(this, id
,
415 wxDefaultPosition
, wxDefaultSize
,
419 #endif // USE_GENERIC_TBAR
421 void MyFrame::LayoutChildren()
423 wxSize size
= GetClientSize();
428 m_tbar
->SetSize(-1, size
.y
);
431 offset
= m_tbar
->GetSize().x
;
438 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
441 void MyFrame::OnSize(wxSizeEvent
& event
)
453 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
462 long style
= GetToolBar()->GetWindowStyle();
463 style
&= ~wxTB_HORIZONTAL
;
464 style
|= wxTB_VERTICAL
;
466 m_tbar
= new wxToolBar(this, -1,
467 wxDefaultPosition
, wxDefaultSize
,
470 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
471 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
472 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
473 m_tbar
->AddSeparator();
474 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
482 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
484 m_smallToolbar
= !m_smallToolbar
;
489 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
491 // m_rows may be only 1 or 2
494 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
496 //RecreateToolbar(); -- this is unneeded
499 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
501 m_horzToolbar
= !m_horzToolbar
;
506 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
511 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
513 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
516 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
519 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
520 m_textWindow
->WriteText( str
);
522 if (event
.GetId() == wxID_HELP
)
524 if ( event
.GetExtraLong() != 0 )
525 m_textWindow
->WriteText( _T("Help button down now.\n") );
527 m_textWindow
->WriteText( _T("Help button up now.\n") );
530 if (event
.GetId() == wxID_COPY
)
535 if (event
.GetId() == wxID_CUT
)
540 if (event
.GetId() == wxID_PRINT
)
546 void MyFrame::OnCombo(wxCommandEvent
& event
)
548 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
551 void MyFrame::DoEnablePrint()
553 wxToolBarBase
*tb
= GetToolBar();
554 if (tb
->GetToolEnabled(wxID_PRINT
))
555 tb
->EnableTool( wxID_PRINT
, FALSE
);
557 tb
->EnableTool( wxID_PRINT
, TRUE
);
560 void MyFrame::DoDeletePrint()
562 wxToolBarBase
*tb
= GetToolBar();
564 tb
->DeleteTool( wxID_PRINT
);
567 void MyFrame::DoToggleHelp()
569 wxToolBarBase
*tb
= GetToolBar();
570 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
573 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
575 event
.Enable( m_textWindow
->CanCopy() );
578 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
580 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
583 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
585 long style
= GetToolBar()->GetWindowStyle();
586 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
588 switch ( event
.GetId() )
590 case IDM_TOOLBAR_SHOW_TEXT
:
591 style
|= wxTB_NOICONS
| wxTB_TEXT
;
594 case IDM_TOOLBAR_SHOW_ICONS
:
598 case IDM_TOOLBAR_SHOW_BOTH
:
602 GetToolBar()->SetWindowStyle(style
);
605 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
607 wxBitmap bmp
= wxBITMAP(print
);
609 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
610 FALSE
, (wxObject
*) NULL
,
612 "This button was inserted into the toolbar");
614 GetToolBar()->Realize();
617 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
619 if (event
.GetSelection() > -1)
622 str
.Printf(_T("This is tool number %d"), event
.GetSelection());