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
;
156 // static const long TOOLBAR_STYLE = 0;
160 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
161 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
162 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
163 IDM_TOOLBAR_ENABLEPRINT
,
164 IDM_TOOLBAR_DELETEPRINT
,
165 IDM_TOOLBAR_INSERTPRINT
,
166 IDM_TOOLBAR_TOGGLEHELP
,
167 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
168 IDM_TOOLBAR_CHANGE_TOOLTIP
,
169 IDM_TOOLBAR_SHOW_TEXT
,
170 IDM_TOOLBAR_SHOW_ICONS
,
171 IDM_TOOLBAR_SHOW_BOTH
,
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
183 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
184 EVT_SIZE(MyFrame::OnSize
)
186 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
187 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
189 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
191 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
192 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
193 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
195 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
196 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
197 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
198 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
199 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
201 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
202 MyFrame::OnToolbarStyle
)
204 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
206 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
208 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
210 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
211 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
214 // ============================================================================
216 // ============================================================================
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
224 // The `main program' equivalent, creating the windows and returning the
228 // Create the main frame window
229 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
231 wxPoint(100, 100), wxSize(550, 300));
235 frame
->SetStatusText("Hello, wxWindows");
242 void MyFrame::RecreateToolbar()
244 // delete and recreate the toolbar
245 wxToolBarBase
*toolBar
= GetToolBar();
246 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
252 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
);
253 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
255 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
256 //toolBar->SetMargins( 4, 4 );
259 wxBitmap toolBarBitmaps
[8];
262 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
263 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
264 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
265 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
266 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
267 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
268 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
269 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
270 #else // !USE_XPM_BITMAPS
271 toolBarBitmaps
[0] = wxBITMAP(new);
272 toolBarBitmaps
[1] = wxBITMAP(open
);
273 toolBarBitmaps
[2] = wxBITMAP(save
);
274 toolBarBitmaps
[3] = wxBITMAP(copy
);
275 toolBarBitmaps
[4] = wxBITMAP(cut
);
276 toolBarBitmaps
[5] = wxBITMAP(paste
);
277 toolBarBitmaps
[6] = wxBITMAP(print
);
278 toolBarBitmaps
[7] = wxBITMAP(help
);
279 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
281 if ( !m_smallToolbar
)
283 int w
= 2*toolBarBitmaps
[0].GetWidth(),
284 h
= 2*toolBarBitmaps
[0].GetHeight();
285 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
288 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
291 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
294 toolBar
->AddTool(wxID_NEW
, _T("New"), toolBarBitmaps
[0], _T("New file"));
295 toolBar
->AddTool(wxID_OPEN
, _T("Open"), toolBarBitmaps
[1], _T("Open file"));
297 // neither the generic nor Motif native toolbars really support this
298 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
299 // adding a combo to a vertical toolbar is not very smart
302 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
303 combo
->Append("This");
304 combo
->Append("is a");
305 combo
->Append("combobox");
306 combo
->Append("in a");
307 combo
->Append("toolbar");
308 toolBar
->AddControl(combo
);
310 #endif // toolbars which don't support controls
312 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[2], _T("Toggle button 1"), wxITEM_CHECK
);
313 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[3], _T("Toggle button 2"), wxITEM_CHECK
);
314 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[4], _T("Toggle/Untoggle help button"));
315 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[5], _T("Paste"));
316 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[6], _T("Delete this tool"));
317 toolBar
->AddSeparator();
318 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[7], _T("Help button"), wxITEM_CHECK
);
320 // after adding the buttons to the toolbar, must call Realize() to reflect
324 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 // Define my frame constructor
332 MyFrame::MyFrame(wxFrame
* parent
,
334 const wxString
& title
,
338 : wxFrame(parent
, id
, title
, pos
, size
, style
)
341 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
343 m_smallToolbar
= TRUE
;
344 m_horzToolbar
= TRUE
;
347 // Give it a status line
351 SetIcon(wxICON(mondrian
));
354 wxMenu
*tbarMenu
= new wxMenu
;
355 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
356 "Toggle &another toolbar\tCtrl-A",
357 "Show/hide another test toolbar",
360 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
361 "&Toggle toolbar size\tCtrl-S",
362 "Toggle between big/small toolbar",
364 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
365 "Toggle toolbar &orientation\tCtrl-O",
366 "Toggle toolbar orientation",
368 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
369 "Toggle number of &rows\tCtrl-R",
370 "Toggle number of toolbar rows between 1 and 2",
373 tbarMenu
->AppendSeparator();
375 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
376 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
377 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
378 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
379 tbarMenu
->AppendSeparator();
380 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
381 tbarMenu
->AppendSeparator();
382 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, "Show &text\tAlt-T");
383 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, "Show &icons\tAlt-I");
384 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, "Show &both\tAlt-B");
386 wxMenu
*fileMenu
= new wxMenu
;
387 fileMenu
->Append(wxID_EXIT
, "E&xit\tAlt-X", "Quit toolbar sample" );
389 wxMenu
*helpMenu
= new wxMenu
;
390 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
392 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
394 menuBar
->Append(fileMenu
, "&File");
395 menuBar
->Append(tbarMenu
, "&Toolbar");
396 menuBar
->Append(helpMenu
, "&Help");
398 // Associate the menu bar with the frame
401 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, TRUE
);
403 // Create the toolbar
409 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
411 const wxString
& name
)
413 return (wxToolBar
*)new wxToolBarSimple(this, id
,
414 wxDefaultPosition
, wxDefaultSize
,
418 #endif // USE_GENERIC_TBAR
420 void MyFrame::LayoutChildren()
422 wxSize size
= GetClientSize();
427 m_tbar
->SetSize(-1, size
.y
);
430 offset
= m_tbar
->GetSize().x
;
437 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
440 void MyFrame::OnSize(wxSizeEvent
& event
)
452 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
461 long style
= GetToolBar()->GetWindowStyle();
462 style
&= ~wxTB_HORIZONTAL
;
463 style
|= wxTB_VERTICAL
;
465 m_tbar
= new wxToolBar(this, -1,
466 wxDefaultPosition
, wxDefaultSize
,
469 m_tbar
->AddRadioTool(wxID_NEW
, _T("First"), wxBITMAP(new));
470 m_tbar
->AddRadioTool(wxID_OPEN
, _T("Second"), wxBITMAP(open
));
471 m_tbar
->AddRadioTool(wxID_SAVE
, _T("Third"), wxBITMAP(save
));
472 m_tbar
->AddSeparator();
473 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
481 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
483 m_smallToolbar
= !m_smallToolbar
;
488 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
490 // m_rows may be only 1 or 2
493 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
495 //RecreateToolbar(); -- this is unneeded
498 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
500 m_horzToolbar
= !m_horzToolbar
;
505 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
510 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
512 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
515 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
518 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
519 m_textWindow
->WriteText( str
);
521 if (event
.GetId() == wxID_HELP
)
523 if ( event
.GetExtraLong() != 0 )
524 m_textWindow
->WriteText( _T("Help button down now.\n") );
526 m_textWindow
->WriteText( _T("Help button up now.\n") );
529 if (event
.GetId() == wxID_COPY
)
534 if (event
.GetId() == wxID_CUT
)
539 if (event
.GetId() == wxID_PRINT
)
545 void MyFrame::OnCombo(wxCommandEvent
& event
)
547 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
550 void MyFrame::DoEnablePrint()
552 wxToolBarBase
*tb
= GetToolBar();
553 if (tb
->GetToolEnabled(wxID_PRINT
))
554 tb
->EnableTool( wxID_PRINT
, FALSE
);
556 tb
->EnableTool( wxID_PRINT
, TRUE
);
559 void MyFrame::DoDeletePrint()
561 wxToolBarBase
*tb
= GetToolBar();
563 tb
->DeleteTool( wxID_PRINT
);
566 void MyFrame::DoToggleHelp()
568 wxToolBarBase
*tb
= GetToolBar();
569 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
572 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
574 event
.Enable( m_textWindow
->CanCopy() );
577 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
579 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
582 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
584 long style
= GetToolBar()->GetWindowStyle();
585 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
587 switch ( event
.GetId() )
589 case IDM_TOOLBAR_SHOW_TEXT
:
590 style
|= wxTB_NOICONS
| wxTB_TEXT
;
593 case IDM_TOOLBAR_SHOW_ICONS
:
597 case IDM_TOOLBAR_SHOW_BOTH
:
601 GetToolBar()->SetWindowStyle(style
);
604 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
606 wxBitmap bmp
= wxBITMAP(print
);
608 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
609 FALSE
, (wxObject
*) NULL
,
611 "This button was inserted into the toolbar");
613 GetToolBar()->Realize();
616 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
618 if (event
.GetSelection() > -1)
621 str
.Printf(_T("This is tool number %d"), event
.GetSelection());