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)
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 OnToggleAnotherToolbar(wxCommandEvent
& event
);
106 void OnToggleToolbarSize(wxCommandEvent
& event
);
107 void OnToggleToolbarOrient(wxCommandEvent
& event
);
108 void OnToggleToolbarRows(wxCommandEvent
& event
);
110 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
111 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
112 void OnInsertPrint(wxCommandEvent
& event
);
113 void OnChangeToolTip(wxCommandEvent
& event
);
114 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
116 void OnToolbarStyle(wxCommandEvent
& event
);
118 void OnToolLeftClick(wxCommandEvent
& event
);
119 void OnToolEnter(wxCommandEvent
& event
);
121 void OnCombo(wxCommandEvent
& event
);
123 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
126 virtual wxToolBar
*OnCreateToolBar(long style
,
128 const wxString
& name
);
129 #endif // USE_GENERIC_TBAR
132 void DoEnablePrint();
133 void DoDeletePrint();
136 void LayoutChildren();
140 size_t m_rows
; // 1 or 2 only
142 wxTextCtrl
*m_textWindow
;
146 DECLARE_EVENT_TABLE()
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 const int ID_TOOLBAR
= 500;
155 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
159 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
160 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
161 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
162 IDM_TOOLBAR_ENABLEPRINT
,
163 IDM_TOOLBAR_DELETEPRINT
,
164 IDM_TOOLBAR_INSERTPRINT
,
165 IDM_TOOLBAR_TOGGLEHELP
,
166 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
167 IDM_TOOLBAR_CHANGE_TOOLTIP
,
168 IDM_TOOLBAR_SHOW_TEXT
,
169 IDM_TOOLBAR_SHOW_ICONS
,
170 IDM_TOOLBAR_SHOW_BOTH
,
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
182 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
183 EVT_SIZE(MyFrame::OnSize
)
185 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
186 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
188 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
190 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
191 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
192 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
194 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
195 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
196 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
197 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
198 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
200 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
201 MyFrame::OnToolbarStyle
)
203 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
205 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
207 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
209 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
210 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
213 // ============================================================================
215 // ============================================================================
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
223 // The `main program' equivalent, creating the windows and returning the
227 // Create the main frame window
228 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
230 wxPoint(100, 100), wxSize(550, 300));
234 frame
->SetStatusText("Hello, wxWindows");
241 void MyFrame::RecreateToolbar()
243 // delete and recreate the toolbar
244 wxToolBarBase
*toolBar
= GetToolBar();
245 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
251 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
);
252 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
254 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
255 //toolBar->SetMargins( 4, 4 );
258 wxBitmap toolBarBitmaps
[8];
261 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
262 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
263 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
264 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
265 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
266 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
267 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
268 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
269 #else // !USE_XPM_BITMAPS
270 toolBarBitmaps
[0] = wxBITMAP(new);
271 toolBarBitmaps
[1] = wxBITMAP(open
);
272 toolBarBitmaps
[2] = wxBITMAP(save
);
273 toolBarBitmaps
[3] = wxBITMAP(copy
);
274 toolBarBitmaps
[4] = wxBITMAP(cut
);
275 toolBarBitmaps
[5] = wxBITMAP(paste
);
276 toolBarBitmaps
[6] = wxBITMAP(print
);
277 toolBarBitmaps
[7] = wxBITMAP(help
);
278 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
280 if ( !m_smallToolbar
)
282 int w
= 2*toolBarBitmaps
[0].GetWidth(),
283 h
= 2*toolBarBitmaps
[0].GetHeight();
284 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
287 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
290 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
293 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
294 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
296 // neither the generic nor Motif native toolbars really support this
297 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
298 // adding a combo to a vertical toolbar is not very smart
301 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
302 combo
->Append("This");
303 combo
->Append("is a");
304 combo
->Append("combobox");
305 combo
->Append("in a");
306 combo
->Append("toolbar");
307 toolBar
->AddControl(combo
);
309 #endif // toolbars which don't support controls
311 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
312 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
313 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
314 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
315 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool"));
316 toolBar
->AddSeparator();
317 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
319 // after adding the buttons to the toolbar, must call Realize() to reflect
323 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 // Define my frame constructor
331 MyFrame::MyFrame(wxFrame
* parent
,
333 const wxString
& title
,
337 : wxFrame(parent
, id
, title
, pos
, size
, style
)
340 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
342 m_smallToolbar
= TRUE
;
343 m_horzToolbar
= TRUE
;
346 // Give it a status line
350 SetIcon(wxICON(mondrian
));
353 wxMenu
*tbarMenu
= new wxMenu
;
354 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
355 "Toggle &another toolbar\tCtrl-A",
356 "Show/hide another test toolbar",
359 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
360 "&Toggle toolbar size\tCtrl-S",
361 "Toggle between big/small toolbar",
363 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
364 "Toggle toolbar &orientation\tCtrl-O",
365 "Toggle toolbar orientation",
367 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
368 "Toggle number of &rows\tCtrl-R",
369 "Toggle number of toolbar rows between 1 and 2",
372 tbarMenu
->AppendSeparator();
374 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
375 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
376 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
377 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
378 tbarMenu
->AppendSeparator();
379 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
380 tbarMenu
->AppendSeparator();
381 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, "Show &text\tAlt-T");
382 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, "Show &icons\tAlt-I");
383 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, "Show &both\tAlt-B");
385 wxMenu
*fileMenu
= new wxMenu
;
386 fileMenu
->Append(wxID_EXIT
, "E&xit\tAlt-X", "Quit toolbar sample" );
388 wxMenu
*helpMenu
= new wxMenu
;
389 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
391 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
393 menuBar
->Append(fileMenu
, "&File");
394 menuBar
->Append(tbarMenu
, "&Toolbar");
395 menuBar
->Append(helpMenu
, "&Help");
397 // Associate the menu bar with the frame
400 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
402 // Create the toolbar
408 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
410 const wxString
& name
)
412 return (wxToolBar
*)new wxToolBarSimple(this, id
,
413 wxDefaultPosition
, wxDefaultSize
,
417 #endif // USE_GENERIC_TBAR
419 void MyFrame::LayoutChildren()
421 wxSize size
= GetClientSize();
426 m_tbar
->SetSize(-1, size
.y
);
429 offset
= m_tbar
->GetSize().x
;
436 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
439 void MyFrame::OnSize(wxSizeEvent
& event
)
451 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
460 long style
= GetToolBar()->GetWindowStyle();
461 style
&= ~wxTB_HORIZONTAL
;
462 style
|= wxTB_VERTICAL
;
464 m_tbar
= new wxToolBar(this, -1,
465 wxDefaultPosition
, wxDefaultSize
,
468 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
469 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
470 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
471 m_tbar
->AddSeparator();
472 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
480 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
482 m_smallToolbar
= !m_smallToolbar
;
487 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
489 // m_rows may be only 1 or 2
492 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
494 //RecreateToolbar(); -- this is unneeded
497 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
499 m_horzToolbar
= !m_horzToolbar
;
504 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
509 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
511 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
514 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
517 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
518 m_textWindow
->WriteText( str
);
520 if (event
.GetId() == wxID_HELP
)
522 if ( event
.GetExtraLong() != 0 )
523 m_textWindow
->WriteText( _T("Help button down now.\n") );
525 m_textWindow
->WriteText( _T("Help button up now.\n") );
528 if (event
.GetId() == wxID_COPY
)
533 if (event
.GetId() == wxID_CUT
)
538 if (event
.GetId() == wxID_PRINT
)
544 void MyFrame::OnCombo(wxCommandEvent
& event
)
546 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
549 void MyFrame::DoEnablePrint()
551 wxToolBarBase
*tb
= GetToolBar();
552 if (tb
->GetToolEnabled(wxID_PRINT
))
553 tb
->EnableTool( wxID_PRINT
, FALSE
);
555 tb
->EnableTool( wxID_PRINT
, TRUE
);
558 void MyFrame::DoDeletePrint()
560 wxToolBarBase
*tb
= GetToolBar();
562 tb
->DeleteTool( wxID_PRINT
);
565 void MyFrame::DoToggleHelp()
567 wxToolBarBase
*tb
= GetToolBar();
568 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
571 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
573 event
.Enable( m_textWindow
->CanCopy() );
576 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
578 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
581 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
583 long style
= GetToolBar()->GetWindowStyle();
584 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
586 switch ( event
.GetId() )
588 case IDM_TOOLBAR_SHOW_TEXT
:
589 style
|= wxTB_NOICONS
| wxTB_TEXT
;
592 case IDM_TOOLBAR_SHOW_ICONS
:
596 case IDM_TOOLBAR_SHOW_BOTH
:
600 GetToolBar()->SetWindowStyle(style
);
603 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
605 wxBitmap bmp
= wxBITMAP(print
);
607 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
608 FALSE
, (wxObject
*) NULL
,
610 "This button was inserted into the toolbar");
612 GetToolBar()->Realize();
615 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
617 if (event
.GetSelection() > -1)
620 str
.Printf(_T("This is tool number %d"), event
.GetSelection());