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>
35 // define this to 1 to use wxToolBarSimple instead of the native one
36 #define USE_GENERIC_TBAR 0
38 // define this to use XPMs everywhere (by default, BMPs are used under Win)
39 // BMPs use less space, but aren't compiled into the executable on other platforms
41 #define USE_XPM_BITMAPS 0
43 #define USE_XPM_BITMAPS 1
47 #if !wxUSE_TOOLBAR_SIMPLE
48 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
49 to 1 in setup.h and recompile the library.
51 #include <wx/tbarsmpl.h>
53 #endif // USE_GENERIC_TBAR
55 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
56 #error You need to enable XPM support to use XPM bitmaps with toolbar!
57 #endif // USE_XPM_BITMAPS
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 #include "mondrian.xpm"
65 #include "bitmaps/new.xpm"
66 #include "bitmaps/open.xpm"
67 #include "bitmaps/save.xpm"
68 #include "bitmaps/copy.xpm"
69 #include "bitmaps/cut.xpm"
70 #include "bitmaps/preview.xpm" // paste XPM
71 #include "bitmaps/print.xpm"
72 #include "bitmaps/help.xpm"
73 #endif // USE_XPM_BITMAPS
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application
80 class MyApp
: public wxApp
87 class MyFrame
: public wxFrame
90 MyFrame(wxFrame
*parent
,
92 const wxString
& title
= _T("wxToolBar Sample"),
93 const wxPoint
& pos
= wxDefaultPosition
,
94 const wxSize
& size
= wxDefaultSize
,
95 long style
= wxDEFAULT_FRAME_STYLE
|wxCLIP_CHILDREN
|wxNO_FULL_REPAINT_ON_RESIZE
);
97 void RecreateToolbar();
99 void OnQuit(wxCommandEvent
& event
);
100 void OnAbout(wxCommandEvent
& event
);
102 void OnSize(wxSizeEvent
& event
);
104 void OnToggleToolbar(wxCommandEvent
& event
);
105 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
106 void OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
));
108 void OnToggleToolbarSize(wxCommandEvent
& event
);
109 void OnToggleToolbarOrient(wxCommandEvent
& event
);
110 void OnToggleToolbarRows(wxCommandEvent
& event
);
112 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
113 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
114 void OnInsertPrint(wxCommandEvent
& event
);
115 void OnChangeToolTip(wxCommandEvent
& event
);
116 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
118 void OnToolbarStyle(wxCommandEvent
& event
);
120 void OnToolLeftClick(wxCommandEvent
& event
);
121 void OnToolEnter(wxCommandEvent
& event
);
123 void OnCombo(wxCommandEvent
& event
);
125 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
126 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
129 virtual wxToolBar
*OnCreateToolBar(long style
,
131 const wxString
& name
);
132 #endif // USE_GENERIC_TBAR
135 void DoEnablePrint();
136 void DoDeletePrint();
139 void LayoutChildren();
144 size_t m_rows
; // 1 or 2 only
146 // the number of print buttons we have (they're added/removed dynamically)
149 wxTextCtrl
*m_textWindow
;
153 DECLARE_EVENT_TABLE()
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 const int ID_TOOLBAR
= 500;
162 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
166 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
167 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
168 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
169 IDM_TOOLBAR_ENABLEPRINT
,
170 IDM_TOOLBAR_DELETEPRINT
,
171 IDM_TOOLBAR_INSERTPRINT
,
172 IDM_TOOLBAR_TOGGLEHELP
,
173 IDM_TOOLBAR_TOGGLE_TOOLBAR
,
174 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
175 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
176 IDM_TOOLBAR_CHANGE_TOOLTIP
,
177 IDM_TOOLBAR_SHOW_TEXT
,
178 IDM_TOOLBAR_SHOW_ICONS
,
179 IDM_TOOLBAR_SHOW_BOTH
,
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
191 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
192 EVT_SIZE(MyFrame
::OnSize
)
194 EVT_MENU(wxID_EXIT
, MyFrame
::OnQuit
)
195 EVT_MENU(wxID_HELP
, MyFrame
::OnAbout
)
197 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame
::OnToggleToolbar
)
198 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame
::OnToggleAnotherToolbar
)
199 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame
::OnToggleHorizontalText
)
201 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame
::OnToggleToolbarSize
)
202 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame
::OnToggleToolbarOrient
)
203 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame
::OnToggleToolbarRows
)
205 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame
::OnEnablePrint
)
206 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame
::OnDeletePrint
)
207 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame
::OnInsertPrint
)
208 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame
::OnToggleHelp
)
209 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame
::OnChangeToolTip
)
211 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
212 MyFrame
::OnToolbarStyle
)
214 EVT_MENU(-1, MyFrame
::OnToolLeftClick
)
216 EVT_COMBOBOX(ID_COMBO
, MyFrame
::OnCombo
)
218 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame
::OnToolEnter
)
220 EVT_UPDATE_UI(wxID_COPY
, MyFrame
::OnUpdateCopyAndCut
)
221 EVT_UPDATE_UI(wxID_CUT
, MyFrame
::OnUpdateCopyAndCut
)
223 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
224 MyFrame
::OnUpdateToggleHorzText
)
227 // ============================================================================
229 // ============================================================================
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
237 // The `main program' equivalent, creating the windows and returning the
241 // Create the main frame window
242 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
243 _T("wxToolBar Sample"),
245 wxPoint(0, 0), wxDefaultSize
, wxNO_BORDER
247 wxPoint(100, 100), wxSize(550, 300)
253 frame
->SetStatusText(_T("Hello, wxWindows"));
260 void MyFrame
::RecreateToolbar()
263 // On Windows CE, we should not delete the
264 // previous toolbar in case it contains the menubar.
265 // We'll try to accomodate this usage in due course.
266 wxToolBar
* toolBar
= CreateToolBar();
268 // delete and recreate the toolbar
269 wxToolBarBase
*toolBar
= GetToolBar();
270 long style
= toolBar ? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
276 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_HORZ_LAYOUT
);
277 style
|= m_horzToolbar ? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
279 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
280 style
|= wxTB_HORZ_LAYOUT
;
282 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
286 wxBitmap toolBarBitmaps
[8];
289 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
290 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
291 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
292 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
293 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
294 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
295 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
296 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
297 #else // !USE_XPM_BITMAPS
298 toolBarBitmaps
[0] = wxBITMAP(new);
299 toolBarBitmaps
[1] = wxBITMAP(open
);
300 toolBarBitmaps
[2] = wxBITMAP(save
);
301 toolBarBitmaps
[3] = wxBITMAP(copy
);
302 toolBarBitmaps
[4] = wxBITMAP(cut
);
303 toolBarBitmaps
[5] = wxBITMAP(paste
);
304 toolBarBitmaps
[6] = wxBITMAP(print
);
305 toolBarBitmaps
[7] = wxBITMAP(help
);
306 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
308 if ( !m_smallToolbar
)
310 int w
= 2*toolBarBitmaps
[0].GetWidth(),
311 h
= 2*toolBarBitmaps
[0].GetHeight();
312 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
315 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
318 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
321 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
322 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
324 // the generic toolbar doesn't really support this
325 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXX11__) || defined(__WXUNIVERSAL__)
326 // adding a combo to a vertical toolbar is not very smart
329 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, _T(""), wxDefaultPosition
, wxSize(200,-1) );
330 combo
->Append(_T("This"));
331 combo
->Append(_T("is a"));
332 combo
->Append(_T("combobox"));
333 combo
->Append(_T("in a"));
334 combo
->Append(_T("toolbar"));
335 toolBar
->AddControl(combo
);
337 #endif // toolbars which don't support controls
339 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
340 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
341 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
342 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
343 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool. This is a very long tooltip to test whether it does the right thing when the tooltip is more than Windows can cope with."));
344 toolBar
->AddSeparator();
345 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
347 // after adding the buttons to the toolbar, must call Realize() to reflect
351 toolBar
->SetRows(m_horzToolbar ? m_rows
: 10 / m_rows
);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 // Define my frame constructor
359 MyFrame
::MyFrame(wxFrame
* parent
,
361 const wxString
& title
,
365 : wxFrame(parent
, id
, title
, pos
, size
, style
)
368 m_textWindow
= new wxTextCtrl(this, -1, _T(""), wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
370 m_smallToolbar
= TRUE
;
371 m_horzToolbar
= TRUE
;
377 // Give it a status line
382 SetIcon(wxICON(mondrian
));
385 wxMenu
*tbarMenu
= new wxMenu
;
386 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
387 _T("Toggle &toolbar\tCtrl-Z"),
388 _T("Show or hide the toolbar"));
390 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
391 _T("Toggle &another toolbar\tCtrl-A"),
392 _T("Show/hide another test toolbar"));
394 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
395 _T("Toggle hori&zontal text\tCtrl-H"),
396 _T("Show text under/alongside the icon"));
398 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
399 _T("&Toggle toolbar size\tCtrl-S"),
400 _T("Toggle between big/small toolbar"));
402 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
403 _T("Toggle toolbar &orientation\tCtrl-O"),
404 _T("Toggle toolbar orientation"));
406 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
407 _T("Toggle number of &rows\tCtrl-R"),
408 _T("Toggle number of toolbar rows between 1 and 2"));
410 tbarMenu
->AppendSeparator();
412 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, _T("&Enable print button\tCtrl-E"), _T(""));
413 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, _T("&Delete print button\tCtrl-D"), _T(""));
414 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, _T("&Insert print button\tCtrl-I"), _T(""));
415 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, _T("Toggle &help button\tCtrl-T"), _T(""));
416 tbarMenu
->AppendSeparator();
417 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, _T("Change tool tip"), _T(""));
418 tbarMenu
->AppendSeparator();
419 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, _T("Show &text\tAlt-T"));
420 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, _T("Show &icons\tAlt-I"));
421 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, _T("Show &both\tAlt-B"));
423 wxMenu
*fileMenu
= new wxMenu
;
424 fileMenu
->Append(wxID_EXIT
, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
426 wxMenu
*helpMenu
= new wxMenu
;
427 helpMenu
->Append(wxID_HELP
, _T("&About"), _T("About toolbar sample"));
429 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
431 menuBar
->Append(fileMenu
, _T("&File"));
432 menuBar
->Append(tbarMenu
, _T("&Toolbar"));
433 menuBar
->Append(helpMenu
, _T("&Help"));
435 // Associate the menu bar with the frame
438 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
440 // Create the toolbar
446 wxToolBar
* MyFrame
::OnCreateToolBar(long style
,
448 const wxString
& name
)
450 return (wxToolBar
*)new wxToolBarSimple(this, id
,
451 wxDefaultPosition
, wxDefaultSize
,
455 #endif // USE_GENERIC_TBAR
457 void MyFrame
::LayoutChildren()
459 wxSize size
= GetClientSize();
464 m_tbar
->SetSize(-1, size
.y
);
467 offset
= m_tbar
->GetSize().x
;
474 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
477 void MyFrame
::OnSize(wxSizeEvent
& event
)
489 void MyFrame
::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
491 wxToolBar
*tbar
= GetToolBar();
505 void MyFrame
::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
507 m_horzText
= !m_horzText
;
512 void MyFrame
::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
521 long style
= GetToolBar()->GetWindowStyle();
522 style
&= ~wxTB_HORIZONTAL
;
523 style
|= wxTB_VERTICAL
;
525 m_tbar
= new wxToolBar(this, -1,
526 wxDefaultPosition
, wxDefaultSize
,
529 m_tbar
->SetMargins(4, 4);
531 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
532 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
533 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
534 m_tbar
->AddSeparator();
535 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
543 void MyFrame
::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
545 m_smallToolbar
= !m_smallToolbar
;
550 void MyFrame
::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
552 // m_rows may be only 1 or 2
555 GetToolBar()->SetRows(m_horzToolbar ? m_rows
: 10 / m_rows
);
557 //RecreateToolbar(); -- this is unneeded
560 void MyFrame
::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
562 m_horzToolbar
= !m_horzToolbar
;
567 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
572 void MyFrame
::OnAbout(wxCommandEvent
& event
)
574 if ( event
.IsChecked() )
575 m_textWindow
->WriteText( _T("Help button down now.\n") );
577 m_textWindow
->WriteText( _T("Help button up now.\n") );
579 (void)wxMessageBox(_T("wxWindows toolbar sample"), _T("About wxToolBar"));
582 void MyFrame
::OnToolLeftClick(wxCommandEvent
& event
)
585 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
586 m_textWindow
->WriteText( str
);
588 if (event
.GetId() == wxID_COPY
)
593 if (event
.GetId() == wxID_CUT
)
598 if (event
.GetId() == wxID_PRINT
)
604 void MyFrame
::OnCombo(wxCommandEvent
& event
)
606 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
609 void MyFrame
::DoEnablePrint()
614 wxToolBarBase
*tb
= GetToolBar();
615 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
618 void MyFrame
::DoDeletePrint()
623 wxToolBarBase
*tb
= GetToolBar();
624 tb
->DeleteTool( wxID_PRINT
);
629 void MyFrame
::DoToggleHelp()
631 wxToolBarBase
*tb
= GetToolBar();
632 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
635 void MyFrame
::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
637 event
.Enable( m_textWindow
->CanCopy() );
640 void MyFrame
::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
642 wxToolBar
*tbar
= GetToolBar();
643 event
.Enable( tbar
&&
644 tbar
->HasFlag(wxTB_TEXT
) &&
645 !tbar
->HasFlag(wxTB_NOICONS
) );
648 void MyFrame
::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
650 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
653 void MyFrame
::OnToolbarStyle(wxCommandEvent
& event
)
655 long style
= GetToolBar()->GetWindowStyle();
656 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
658 switch ( event
.GetId() )
660 case IDM_TOOLBAR_SHOW_TEXT
:
661 style
|= wxTB_NOICONS
| wxTB_TEXT
;
664 case IDM_TOOLBAR_SHOW_ICONS
:
668 case IDM_TOOLBAR_SHOW_BOTH
:
672 GetToolBar()->SetWindowStyle(style
);
675 void MyFrame
::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
679 wxToolBarBase
*tb
= GetToolBar();
680 tb
->InsertTool(0, wxID_PRINT
, _T("New print"),
681 wxBITMAP(print
), wxNullBitmap
,
683 _T("Delete this tool"),
684 _T("This button was inserted into the toolbar"));
686 // must call Realize() after adding a new button
690 void MyFrame
::OnToolEnter(wxCommandEvent
& event
)
692 if (event
.GetSelection() > -1)
695 str
.Printf(_T("This is tool number %d"), event
.GetSelection());
699 SetStatusText(_T(""));