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
);
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 // the number of print buttons we have (they're added/removed dynamically)
146 wxTextCtrl
*m_textWindow
;
150 DECLARE_EVENT_TABLE()
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 const int ID_TOOLBAR
= 500;
159 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
163 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
164 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
165 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
166 IDM_TOOLBAR_ENABLEPRINT
,
167 IDM_TOOLBAR_DELETEPRINT
,
168 IDM_TOOLBAR_INSERTPRINT
,
169 IDM_TOOLBAR_TOGGLEHELP
,
170 IDM_TOOLBAR_TOGGLE_TOOLBAR
,
171 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
172 IDM_TOOLBAR_CHANGE_TOOLTIP
,
173 IDM_TOOLBAR_SHOW_TEXT
,
174 IDM_TOOLBAR_SHOW_ICONS
,
175 IDM_TOOLBAR_SHOW_BOTH
,
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_SIZE(MyFrame::OnSize
)
190 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
191 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
193 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
194 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
196 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
197 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
198 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
200 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
201 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
202 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
203 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
204 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
206 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
207 MyFrame::OnToolbarStyle
)
209 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
211 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
213 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
215 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
216 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
219 // ============================================================================
221 // ============================================================================
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
229 // The `main program' equivalent, creating the windows and returning the
233 // Create the main frame window
234 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
235 _T("wxToolBar Sample"),
237 wxPoint(0, 0), wxDefaultSize
, wxNO_BORDER
239 wxPoint(100, 100), wxSize(550, 300)
245 frame
->SetStatusText(_T("Hello, wxWindows"));
252 void MyFrame::RecreateToolbar()
255 // On Windows CE, we should not delete the
256 // previous toolbar in case it contains the menubar.
257 // We'll try to accomodate this usage in due course.
258 wxToolBar
* toolBar
= CreateToolBar();
260 // delete and recreate the toolbar
261 wxToolBarBase
*toolBar
= GetToolBar();
262 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
268 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
);
269 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
270 style
|= wxNO_FULL_REPAINT_ON_RESIZE
;
272 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
276 wxBitmap toolBarBitmaps
[8];
279 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
280 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
281 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
282 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
283 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
284 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
285 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
286 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
287 #else // !USE_XPM_BITMAPS
288 toolBarBitmaps
[0] = wxBITMAP(new);
289 toolBarBitmaps
[1] = wxBITMAP(open
);
290 toolBarBitmaps
[2] = wxBITMAP(save
);
291 toolBarBitmaps
[3] = wxBITMAP(copy
);
292 toolBarBitmaps
[4] = wxBITMAP(cut
);
293 toolBarBitmaps
[5] = wxBITMAP(paste
);
294 toolBarBitmaps
[6] = wxBITMAP(print
);
295 toolBarBitmaps
[7] = wxBITMAP(help
);
296 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
298 if ( !m_smallToolbar
)
300 int w
= 2*toolBarBitmaps
[0].GetWidth(),
301 h
= 2*toolBarBitmaps
[0].GetHeight();
302 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
305 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
308 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
311 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
312 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
314 // the generic toolbar doesn't really support this
315 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXX11__) || defined(__WXUNIVERSAL__)
316 // adding a combo to a vertical toolbar is not very smart
319 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, _T(""), wxDefaultPosition
, wxSize(200,-1) );
320 combo
->Append(_T("This"));
321 combo
->Append(_T("is a"));
322 combo
->Append(_T("combobox"));
323 combo
->Append(_T("in a"));
324 combo
->Append(_T("toolbar"));
325 toolBar
->AddControl(combo
);
327 #endif // toolbars which don't support controls
329 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
330 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
331 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
332 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
333 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool"));
334 toolBar
->AddSeparator();
335 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
337 // after adding the buttons to the toolbar, must call Realize() to reflect
341 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 // Define my frame constructor
349 MyFrame::MyFrame(wxFrame
* parent
,
351 const wxString
& title
,
355 : wxFrame(parent
, id
, title
, pos
, size
, style
)
358 m_textWindow
= new wxTextCtrl(this, -1, _T(""), wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
360 m_smallToolbar
= TRUE
;
361 m_horzToolbar
= TRUE
;
366 // Give it a status line
371 SetIcon(wxICON(mondrian
));
374 wxMenu
*tbarMenu
= new wxMenu
;
375 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
376 _T("Toggle &toolbar\tCtrl-Z"),
377 _T("Show or hide the toolbar"));
379 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
380 _T("Toggle &another toolbar\tCtrl-A"),
381 _T("Show/hide another test toolbar"));
383 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
384 _T("&Toggle toolbar size\tCtrl-S"),
385 _T("Toggle between big/small toolbar"));
387 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
388 _T("Toggle toolbar &orientation\tCtrl-O"),
389 _T("Toggle toolbar orientation"));
391 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
392 _T("Toggle number of &rows\tCtrl-R"),
393 _T("Toggle number of toolbar rows between 1 and 2"));
395 tbarMenu
->AppendSeparator();
397 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, _T("&Enable print button\tCtrl-E"), _T(""));
398 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, _T("&Delete print button\tCtrl-D"), _T(""));
399 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, _T("&Insert print button\tCtrl-I"), _T(""));
400 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, _T("Toggle &help button\tCtrl-T"), _T(""));
401 tbarMenu
->AppendSeparator();
402 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, _T("Change tool tip"), _T(""));
403 tbarMenu
->AppendSeparator();
404 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, _T("Show &text\tAlt-T"));
405 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, _T("Show &icons\tAlt-I"));
406 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, _T("Show &both\tAlt-B"));
408 wxMenu
*fileMenu
= new wxMenu
;
409 fileMenu
->Append(wxID_EXIT
, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
411 wxMenu
*helpMenu
= new wxMenu
;
412 helpMenu
->Append(wxID_HELP
, _T("&About"), _T("About toolbar sample"));
414 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
416 menuBar
->Append(fileMenu
, _T("&File"));
417 menuBar
->Append(tbarMenu
, _T("&Toolbar"));
418 menuBar
->Append(helpMenu
, _T("&Help"));
420 // Associate the menu bar with the frame
423 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
425 // Create the toolbar
431 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
433 const wxString
& name
)
435 return (wxToolBar
*)new wxToolBarSimple(this, id
,
436 wxDefaultPosition
, wxDefaultSize
,
440 #endif // USE_GENERIC_TBAR
442 void MyFrame::LayoutChildren()
444 wxSize size
= GetClientSize();
449 m_tbar
->SetSize(-1, size
.y
);
452 offset
= m_tbar
->GetSize().x
;
459 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
462 void MyFrame::OnSize(wxSizeEvent
& event
)
474 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
476 wxToolBar
*tbar
= GetToolBar();
490 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
499 long style
= GetToolBar()->GetWindowStyle();
500 style
&= ~wxTB_HORIZONTAL
;
501 style
|= wxTB_VERTICAL
;
503 m_tbar
= new wxToolBar(this, -1,
504 wxDefaultPosition
, wxDefaultSize
,
507 m_tbar
->SetMargins(4, 4);
509 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
510 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
511 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
512 m_tbar
->AddSeparator();
513 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
521 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
523 m_smallToolbar
= !m_smallToolbar
;
528 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
530 // m_rows may be only 1 or 2
533 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
535 //RecreateToolbar(); -- this is unneeded
538 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
540 m_horzToolbar
= !m_horzToolbar
;
545 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
550 void MyFrame::OnAbout(wxCommandEvent
& event
)
552 if ( event
.IsChecked() )
553 m_textWindow
->WriteText( _T("Help button down now.\n") );
555 m_textWindow
->WriteText( _T("Help button up now.\n") );
557 (void)wxMessageBox(_T("wxWindows toolbar sample"), _T("About wxToolBar"));
560 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
563 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
564 m_textWindow
->WriteText( str
);
566 if (event
.GetId() == wxID_COPY
)
571 if (event
.GetId() == wxID_CUT
)
576 if (event
.GetId() == wxID_PRINT
)
582 void MyFrame::OnCombo(wxCommandEvent
& event
)
584 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
587 void MyFrame::DoEnablePrint()
592 wxToolBarBase
*tb
= GetToolBar();
593 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
596 void MyFrame::DoDeletePrint()
601 wxToolBarBase
*tb
= GetToolBar();
602 tb
->DeleteTool( wxID_PRINT
);
607 void MyFrame::DoToggleHelp()
609 wxToolBarBase
*tb
= GetToolBar();
610 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
613 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
616 event
.Enable( m_textWindow
->CanCopy() );
619 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
621 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
624 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
626 long style
= GetToolBar()->GetWindowStyle();
627 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
629 switch ( event
.GetId() )
631 case IDM_TOOLBAR_SHOW_TEXT
:
632 style
|= wxTB_NOICONS
| wxTB_TEXT
;
635 case IDM_TOOLBAR_SHOW_ICONS
:
639 case IDM_TOOLBAR_SHOW_BOTH
:
643 GetToolBar()->SetWindowStyle(style
);
646 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
650 wxToolBarBase
*tb
= GetToolBar();
651 tb
->InsertTool(0, wxID_PRINT
, _T("New print"),
652 wxBITMAP(print
), wxNullBitmap
,
654 _T("Delete this tool"),
655 _T("This button was inserted into the toolbar"));
657 // must call Realize() after adding a new button
661 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
663 if (event
.GetSelection() > -1)
666 str
.Printf(_T("This is tool number %d"), event
.GetSelection());
670 SetStatusText(_T(""));