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
= "wxToolBar Sample",
93 const wxPoint
& pos
= wxDefaultPosition
,
94 const wxSize
& size
= wxDefaultSize
,
95 long style
= wxDEFAULT_FRAME_STYLE
);
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 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_TOOLBAR
,
169 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
170 IDM_TOOLBAR_CHANGE_TOOLTIP
,
171 IDM_TOOLBAR_SHOW_TEXT
,
172 IDM_TOOLBAR_SHOW_ICONS
,
173 IDM_TOOLBAR_SHOW_BOTH
,
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
185 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
186 EVT_SIZE(MyFrame::OnSize
)
188 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
189 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
191 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
192 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
194 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
195 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
196 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
198 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
199 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
200 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
201 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
202 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
204 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
205 MyFrame::OnToolbarStyle
)
207 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
209 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
211 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
213 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
214 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
217 // ============================================================================
219 // ============================================================================
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
227 // The `main program' equivalent, creating the windows and returning the
231 // Create the main frame window
232 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
234 wxPoint(100, 100), wxSize(550, 300));
238 frame
->SetStatusText("Hello, wxWindows");
245 void MyFrame::RecreateToolbar()
247 // delete and recreate the toolbar
248 wxToolBarBase
*toolBar
= GetToolBar();
249 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
255 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
);
256 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
258 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
259 //toolBar->SetMargins( 4, 4 );
262 wxBitmap toolBarBitmaps
[8];
265 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
266 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
267 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
268 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
269 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
270 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
271 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
272 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
273 #else // !USE_XPM_BITMAPS
274 toolBarBitmaps
[0] = wxBITMAP(new);
275 toolBarBitmaps
[1] = wxBITMAP(open
);
276 toolBarBitmaps
[2] = wxBITMAP(save
);
277 toolBarBitmaps
[3] = wxBITMAP(copy
);
278 toolBarBitmaps
[4] = wxBITMAP(cut
);
279 toolBarBitmaps
[5] = wxBITMAP(paste
);
280 toolBarBitmaps
[6] = wxBITMAP(print
);
281 toolBarBitmaps
[7] = wxBITMAP(help
);
282 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
284 if ( !m_smallToolbar
)
286 int w
= 2*toolBarBitmaps
[0].GetWidth(),
287 h
= 2*toolBarBitmaps
[0].GetHeight();
288 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
291 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
294 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
297 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
298 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
300 // neither the generic nor Motif native toolbars really support this
301 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
302 // adding a combo to a vertical toolbar is not very smart
305 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
306 combo
->Append("This");
307 combo
->Append("is a");
308 combo
->Append("combobox");
309 combo
->Append("in a");
310 combo
->Append("toolbar");
311 toolBar
->AddControl(combo
);
313 #endif // toolbars which don't support controls
315 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
316 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
317 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
318 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
319 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool"));
320 toolBar
->AddSeparator();
321 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
323 // after adding the buttons to the toolbar, must call Realize() to reflect
327 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 // Define my frame constructor
335 MyFrame::MyFrame(wxFrame
* parent
,
337 const wxString
& title
,
341 : wxFrame(parent
, id
, title
, pos
, size
, style
)
344 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
346 m_smallToolbar
= TRUE
;
347 m_horzToolbar
= TRUE
;
350 // Give it a status line
354 SetIcon(wxICON(mondrian
));
357 wxMenu
*tbarMenu
= new wxMenu
;
358 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
359 "Toggle &toolbar\tCtrl-Z",
360 "Show or hide the toolbar");
362 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
363 "Toggle &another toolbar\tCtrl-A",
364 "Show/hide another test toolbar");
366 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
367 "&Toggle toolbar size\tCtrl-S",
368 "Toggle between big/small toolbar");
370 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
371 "Toggle toolbar &orientation\tCtrl-O",
372 "Toggle toolbar orientation");
374 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
375 "Toggle number of &rows\tCtrl-R",
376 "Toggle number of toolbar rows between 1 and 2");
378 tbarMenu
->AppendSeparator();
380 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
381 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
382 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
383 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
384 tbarMenu
->AppendSeparator();
385 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
386 tbarMenu
->AppendSeparator();
387 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, "Show &text\tAlt-T");
388 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, "Show &icons\tAlt-I");
389 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, "Show &both\tAlt-B");
391 wxMenu
*fileMenu
= new wxMenu
;
392 fileMenu
->Append(wxID_EXIT
, "E&xit\tAlt-X", "Quit toolbar sample" );
394 wxMenu
*helpMenu
= new wxMenu
;
395 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
397 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
399 menuBar
->Append(fileMenu
, "&File");
400 menuBar
->Append(tbarMenu
, "&Toolbar");
401 menuBar
->Append(helpMenu
, "&Help");
403 // Associate the menu bar with the frame
406 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
408 // Create the toolbar
414 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
416 const wxString
& name
)
418 return (wxToolBar
*)new wxToolBarSimple(this, id
,
419 wxDefaultPosition
, wxDefaultSize
,
423 #endif // USE_GENERIC_TBAR
425 void MyFrame::LayoutChildren()
427 wxSize size
= GetClientSize();
432 m_tbar
->SetSize(-1, size
.y
);
435 offset
= m_tbar
->GetSize().x
;
442 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
445 void MyFrame::OnSize(wxSizeEvent
& event
)
457 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
459 wxToolBar
*tbar
= GetToolBar();
473 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
482 long style
= GetToolBar()->GetWindowStyle();
483 style
&= ~wxTB_HORIZONTAL
;
484 style
|= wxTB_VERTICAL
;
486 m_tbar
= new wxToolBar(this, -1,
487 wxDefaultPosition
, wxDefaultSize
,
490 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
491 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
492 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
493 m_tbar
->AddSeparator();
494 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
502 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
504 m_smallToolbar
= !m_smallToolbar
;
509 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
511 // m_rows may be only 1 or 2
514 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
516 //RecreateToolbar(); -- this is unneeded
519 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
521 m_horzToolbar
= !m_horzToolbar
;
526 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
531 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
533 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
536 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
539 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
540 m_textWindow
->WriteText( str
);
542 if (event
.GetId() == wxID_HELP
)
544 if ( event
.GetExtraLong() != 0 )
545 m_textWindow
->WriteText( _T("Help button down now.\n") );
547 m_textWindow
->WriteText( _T("Help button up now.\n") );
550 if (event
.GetId() == wxID_COPY
)
555 if (event
.GetId() == wxID_CUT
)
560 if (event
.GetId() == wxID_PRINT
)
566 void MyFrame::OnCombo(wxCommandEvent
& event
)
568 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
571 void MyFrame::DoEnablePrint()
573 wxToolBarBase
*tb
= GetToolBar();
574 if (tb
->GetToolEnabled(wxID_PRINT
))
575 tb
->EnableTool( wxID_PRINT
, FALSE
);
577 tb
->EnableTool( wxID_PRINT
, TRUE
);
580 void MyFrame::DoDeletePrint()
582 wxToolBarBase
*tb
= GetToolBar();
584 tb
->DeleteTool( wxID_PRINT
);
587 void MyFrame::DoToggleHelp()
589 wxToolBarBase
*tb
= GetToolBar();
590 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
593 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
595 event
.Enable( m_textWindow
->CanCopy() );
598 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
600 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
603 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
605 long style
= GetToolBar()->GetWindowStyle();
606 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
608 switch ( event
.GetId() )
610 case IDM_TOOLBAR_SHOW_TEXT
:
611 style
|= wxTB_NOICONS
| wxTB_TEXT
;
614 case IDM_TOOLBAR_SHOW_ICONS
:
618 case IDM_TOOLBAR_SHOW_BOTH
:
622 GetToolBar()->SetWindowStyle(style
);
625 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
627 wxBitmap bmp
= wxBITMAP(print
);
629 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
630 FALSE
, (wxObject
*) NULL
,
632 "This button was inserted into the toolbar");
634 GetToolBar()->Realize();
637 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
639 if (event
.GetSelection() > -1)
642 str
.Printf(_T("This is tool number %d"), event
.GetSelection());