1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/toolbar.h"
33 #include "wx/filedlg.h"
34 #include "wx/colordlg.h"
35 #include "wx/srchctrl.h"
37 // define this to use XPMs everywhere (by default, BMPs are used under Win)
38 // BMPs use less space, but aren't compiled into the executable on other platforms
40 #define USE_XPM_BITMAPS 0
42 #define USE_XPM_BITMAPS 1
45 // If this is 1, the sample will test an extra toolbar identical to the
46 // main one, but not managed by the frame. This can test subtle differences
47 // in the way toolbars are handled, especially on Mac where there is one
48 // native, 'installed' toolbar.
49 #define USE_UNMANAGED_TOOLBAR 0
51 // Define this as 0 for the platforms not supporting controls in toolbars
52 #define USE_CONTROLS_IN_TOOLBAR 1
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 #ifndef wxHAS_IMAGES_IN_RESOURCES
59 #include "../sample.xpm"
63 #include "bitmaps/new.xpm"
64 #include "bitmaps/open.xpm"
65 #include "bitmaps/save.xpm"
66 #include "bitmaps/copy.xpm"
67 #include "bitmaps/cut.xpm"
68 #include "bitmaps/preview.xpm" // paste XPM
69 #include "bitmaps/print.xpm"
70 #include "bitmaps/help.xpm"
71 #endif // USE_XPM_BITMAPS
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // Define a new application
86 class MyApp
: public wxApp
93 class MyFrame
: public wxFrame
96 MyFrame(wxFrame
*parent
,
97 wxWindowID id
= wxID_ANY
,
98 const wxString
& title
= wxT("wxToolBar Sample"),
99 const wxPoint
& pos
= wxDefaultPosition
,
100 const wxSize
& size
= wxDefaultSize
,
101 long style
= wxDEFAULT_FRAME_STYLE
|wxCLIP_CHILDREN
|wxNO_FULL_REPAINT_ON_RESIZE
);
104 void PopulateToolbar(wxToolBarBase
* toolBar
);
105 void RecreateToolbar();
107 void OnQuit(wxCommandEvent
& event
);
108 void OnAbout(wxCommandEvent
& event
);
110 void OnSize(wxSizeEvent
& event
);
112 void OnToggleToolbar(wxCommandEvent
& event
);
113 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
114 void OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
));
116 void OnToggleToolbarSize(wxCommandEvent
& event
);
117 void OnChangeOrientation(wxCommandEvent
& event
);
118 void OnToggleToolbarRows(wxCommandEvent
& event
);
119 void OnToggleTooltips(wxCommandEvent
& event
);
120 void OnToggleCustomDisabled(wxCommandEvent
& event
);
122 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
123 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
124 void OnInsertPrint(wxCommandEvent
& event
);
125 void OnChangeToolTip(wxCommandEvent
& event
);
126 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
127 void OnToggleSearch(wxCommandEvent
& event
);
128 void OnToggleRadioBtn(wxCommandEvent
& event
);
130 void OnToolbarStyle(wxCommandEvent
& event
);
131 void OnToolbarBgCol(wxCommandEvent
& event
);
132 void OnToolbarCustomBitmap(wxCommandEvent
& event
);
134 void OnToolLeftClick(wxCommandEvent
& event
);
135 void OnToolRightClick(wxCommandEvent
& event
);
136 void OnToolDropdown(wxCommandEvent
& event
);
138 void OnCombo(wxCommandEvent
& event
);
140 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
141 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
142 void OnUpdateToggleRadioBtn(wxUpdateUIEvent
& event
)
143 { event
.Enable( m_tbar
!= NULL
); }
146 void DoEnablePrint();
147 void DoDeletePrint();
150 void LayoutChildren();
156 size_t m_rows
; // 1 or 2 only
158 // the number of print buttons we have (they're added/removed dynamically)
161 // store toolbar position for future use
162 Positions m_toolbarPosition
;
164 wxTextCtrl
*m_textWindow
;
167 #if USE_UNMANAGED_TOOLBAR
168 wxToolBar
*m_extraToolBar
;
173 // the path to the custom bitmap for the test toolbar tool
176 // the search tool, initially NULL
177 wxToolBarToolBase
*m_searchTool
;
179 DECLARE_EVENT_TABLE()
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 const int ID_TOOLBAR
= 500;
188 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
192 // toolbar menu items
193 IDM_TOOLBAR_TOGGLE_TOOLBAR
= 200,
194 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
195 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
196 IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
197 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
198 IDM_TOOLBAR_TOGGLETOOLTIPS
,
199 IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
200 IDM_TOOLBAR_SHOW_TEXT
,
201 IDM_TOOLBAR_SHOW_ICONS
,
202 IDM_TOOLBAR_SHOW_BOTH
,
204 IDM_TOOLBAR_CUSTOM_PATH
,
205 IDM_TOOLBAR_TOP_ORIENTATION
,
206 IDM_TOOLBAR_LEFT_ORIENTATION
,
207 IDM_TOOLBAR_BOTTOM_ORIENTATION
,
208 IDM_TOOLBAR_RIGHT_ORIENTATION
,
214 IDM_TOOLBAR_ENABLEPRINT
,
215 IDM_TOOLBAR_DELETEPRINT
,
216 IDM_TOOLBAR_INSERTPRINT
,
217 IDM_TOOLBAR_TOGGLEHELP
,
218 IDM_TOOLBAR_TOGGLESEARCH
,
219 IDM_TOOLBAR_TOGGLERADIOBTN1
,
220 IDM_TOOLBAR_TOGGLERADIOBTN2
,
221 IDM_TOOLBAR_TOGGLERADIOBTN3
,
222 IDM_TOOLBAR_CHANGE_TOOLTIP
,
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
234 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
235 EVT_SIZE(MyFrame::OnSize
)
237 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
238 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
240 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
241 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
242 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame::OnToggleHorizontalText
)
244 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION
, IDM_TOOLBAR_RIGHT_ORIENTATION
, MyFrame::OnChangeOrientation
)
245 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
246 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
247 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS
, MyFrame::OnToggleTooltips
)
248 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
, MyFrame::OnToggleCustomDisabled
)
250 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
251 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
252 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
253 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
254 EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH
, MyFrame::OnToggleSearch
)
255 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
, IDM_TOOLBAR_TOGGLERADIOBTN3
,
256 MyFrame::OnToggleRadioBtn
)
257 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
259 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
260 MyFrame::OnToolbarStyle
)
261 EVT_MENU(IDM_TOOLBAR_BG_COL
, MyFrame::OnToolbarBgCol
)
263 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH
, MyFrame::OnToolbarCustomBitmap
)
265 EVT_MENU(wxID_ANY
, MyFrame::OnToolLeftClick
)
267 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
269 EVT_TOOL_RCLICKED(wxID_ANY
, MyFrame::OnToolRightClick
)
271 EVT_TOOL_DROPDOWN(wxID_ANY
, MyFrame::OnToolDropdown
)
273 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
274 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
276 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
,
277 IDM_TOOLBAR_TOGGLERADIOBTN3
,
278 MyFrame::OnUpdateToggleRadioBtn
)
279 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
280 MyFrame::OnUpdateToggleHorzText
)
283 // ============================================================================
285 // ============================================================================
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
293 // The `main program' equivalent, creating the windows and returning the
297 if ( !wxApp::OnInit() )
300 // Create the main frame window
301 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxID_ANY
,
302 wxT("wxToolBar Sample"),
303 wxPoint(100, 100), wxSize(650, 300));
308 frame
->SetStatusText(wxT("Hello, wxWidgets"));
311 wxInitAllImageHandlers();
316 void MyFrame::RecreateToolbar()
319 // On Windows CE, we should not delete the
320 // previous toolbar in case it contains the menubar.
321 // We'll try to accommodate this usage in due course.
322 wxToolBar
* toolBar
= CreateToolBar();
324 // delete and recreate the toolbar
325 wxToolBarBase
*toolBar
= GetToolBar();
326 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
328 if (toolBar
&& m_searchTool
&& m_searchTool
->GetToolBar() == NULL
)
331 toolBar
->AddTool(m_searchTool
);
339 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_BOTTOM
| wxTB_RIGHT
| wxTB_HORZ_LAYOUT
);
340 switch( m_toolbarPosition
)
352 style
|= wxTB_BOTTOM
;
356 if ( m_showTooltips
)
357 style
&= ~wxTB_NO_TOOLTIPS
;
359 style
|= wxTB_NO_TOOLTIPS
;
361 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
362 style
|= wxTB_HORZ_LAYOUT
;
364 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
367 PopulateToolbar(toolBar
);
370 void MyFrame::PopulateToolbar(wxToolBarBase
* toolBar
)
386 wxBitmap toolBarBitmaps
[Tool_Max
];
389 #define INIT_TOOL_BMP(bmp) \
390 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
391 #else // !USE_XPM_BITMAPS
392 #define INIT_TOOL_BMP(bmp) \
393 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
394 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
401 INIT_TOOL_BMP(paste
);
402 INIT_TOOL_BMP(print
);
405 int w
= toolBarBitmaps
[Tool_new
].GetWidth(),
406 h
= toolBarBitmaps
[Tool_new
].GetHeight();
408 if ( !m_smallToolbar
)
413 for ( size_t n
= Tool_new
; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
416 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
420 // this call is actually unnecessary as the toolbar will adjust its tools
421 // size to fit the biggest icon used anyhow but it doesn't hurt neither
422 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
424 toolBar
->AddTool(wxID_NEW
, wxT("New"),
425 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
426 wxT("New file"), wxT("This is help for new file tool"));
428 wxMenu
* menu
= new wxMenu
;
429 menu
->Append(wxID_ANY
, wxT("&First dummy item"));
430 menu
->Append(wxID_ANY
, wxT("&Second dummy item"));
431 menu
->AppendSeparator();
432 menu
->Append(wxID_EXIT
, wxT("Exit"));
433 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
435 toolBar
->AddTool(wxID_OPEN
, wxT("Open"),
436 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
437 wxT("Open file"), wxT("This is help for open file tool"));
439 #if USE_CONTROLS_IN_TOOLBAR
440 // adding a combo to a vertical toolbar is not very smart
441 if ( !toolBar
->IsVertical() )
443 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, wxEmptyString
, wxDefaultPosition
, wxSize(100,-1) );
444 combo
->Append(wxT("This"));
445 combo
->Append(wxT("is a"));
446 combo
->Append(wxT("combobox"));
447 combo
->Append(wxT("in a"));
448 combo
->Append(wxT("toolbar"));
449 toolBar
->AddControl(combo
, wxT("Combo Label"));
451 #endif // USE_CONTROLS_IN_TOOLBAR
453 toolBar
->AddTool(wxID_SAVE
, wxT("Save"), toolBarBitmaps
[Tool_save
], wxT("Toggle button 1"), wxITEM_CHECK
);
455 toolBar
->AddSeparator();
456 toolBar
->AddTool(wxID_COPY
, wxT("Copy"), toolBarBitmaps
[Tool_copy
], wxT("Toggle button 2"), wxITEM_CHECK
);
457 toolBar
->AddTool(wxID_CUT
, wxT("Cut"), toolBarBitmaps
[Tool_cut
], wxT("Toggle/Untoggle help button"));
458 toolBar
->AddTool(wxID_PASTE
, wxT("Paste"), toolBarBitmaps
[Tool_paste
], wxT("Paste"));
459 toolBar
->AddSeparator();
461 if ( m_useCustomDisabled
)
463 wxBitmap
bmpDisabled(w
, h
);
466 dc
.SelectObject(bmpDisabled
);
467 dc
.DrawBitmap(toolBarBitmaps
[Tool_print
], 0, 0);
469 wxPen
pen(*wxRED
, 5);
471 dc
.DrawLine(0, 0, w
, h
);
474 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
479 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
480 wxT("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."));
483 // add a stretchable space before the "Help" button to make it
485 toolBar
->AddStretchableSpace();
486 toolBar
->AddTool(wxID_HELP
, wxT("Help"), toolBarBitmaps
[Tool_help
], wxT("Help button"), wxITEM_CHECK
);
488 if ( !m_pathBmp
.empty() )
490 // create a tool with a custom bitmap for testing
491 wxImage
img(m_pathBmp
);
494 if ( img
.GetWidth() > w
&& img
.GetHeight() > h
)
495 img
= img
.GetSubImage(wxRect(0, 0, w
, h
));
497 toolBar
->AddSeparator();
498 toolBar
->AddTool(wxID_ANY
, wxT("Custom"), img
);
502 // after adding the buttons to the toolbar, must call Realize() to reflect
506 toolBar
->SetRows(toolBar
->IsVertical() ? toolBar
->GetToolsCount() / m_rows
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 // Define my frame constructor
515 MyFrame::MyFrame(wxFrame
* parent
,
517 const wxString
& title
,
521 : wxFrame(parent
, id
, title
, pos
, size
, style
)
525 m_smallToolbar
= true;
527 m_useCustomDisabled
= false;
528 m_showTooltips
= true;
535 // Give it a status line
540 SetIcon(wxICON(sample
));
543 wxMenu
*tbarMenu
= new wxMenu
;
544 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
545 wxT("Toggle &toolbar\tCtrl-Z"),
546 wxT("Show or hide the toolbar"));
548 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
549 wxT("Toggle &another toolbar\tCtrl-A"),
550 wxT("Show/hide another test toolbar"));
552 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
553 wxT("Toggle hori&zontal text\tCtrl-H"),
554 wxT("Show text under/alongside the icon"));
556 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
557 wxT("&Toggle toolbar size\tCtrl-S"),
558 wxT("Toggle between big/small toolbar"));
560 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
561 wxT("Toggle number of &rows\tCtrl-R"),
562 wxT("Toggle number of toolbar rows between 1 and 2"));
564 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
565 wxT("Show &tooltips\tCtrl-L"),
566 wxT("Show tooltips for the toolbar tools"));
568 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
569 wxT("Use c&ustom disabled images\tCtrl-U"),
570 wxT("Switch between using system-generated and custom disabled images"));
573 tbarMenu
->AppendSeparator();
574 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
575 wxT("Set toolbar at the top of the window"),
576 wxT("Set toolbar at the top of the window"));
577 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
578 wxT("Set toolbar at the left of the window"),
579 wxT("Set toolbar at the left of the window"));
580 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
581 wxT("Set toolbar at the bottom of the window"),
582 wxT("Set toolbar at the bottom of the window"));
583 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
584 wxT("Set toolbar at the right edge of the window"),
585 wxT("Set toolbar at the right edge of the window"));
586 tbarMenu
->AppendSeparator();
588 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, wxT("Show &text\tCtrl-Alt-T"));
589 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, wxT("Show &icons\tCtrl-Alt-I"));
590 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, wxT("Show &both\tCtrl-Alt-B"));
591 tbarMenu
->AppendSeparator();
592 tbarMenu
->Append(IDM_TOOLBAR_BG_COL
, wxT("Choose bac&kground colour..."));
593 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, wxT("Custom &bitmap...\tCtrl-B"));
595 wxMenu
*toolMenu
= new wxMenu
;
596 toolMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, wxT("&Enable print button\tCtrl-E"));
597 toolMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, wxT("&Delete print button\tCtrl-D"));
598 toolMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, wxT("&Insert print button\tCtrl-I"));
599 toolMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, wxT("Toggle &help button\tCtrl-T"));
600 toolMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH
, wxT("Toggle &search field\tCtrl-F"));
601 toolMenu
->AppendSeparator();
602 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, wxT("Toggle &1st radio button\tCtrl-1"));
603 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, wxT("Toggle &2nd radio button\tCtrl-2"));
604 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, wxT("Toggle &3rd radio button\tCtrl-3"));
605 toolMenu
->AppendSeparator();
606 toolMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, wxT("Change tooltip of \"New\""));
608 wxMenu
*fileMenu
= new wxMenu
;
609 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
611 wxMenu
*helpMenu
= new wxMenu
;
612 helpMenu
->Append(wxID_HELP
, wxT("&About"), wxT("About toolbar sample"));
614 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
616 menuBar
->Append(fileMenu
, wxT("&File"));
617 menuBar
->Append(tbarMenu
, wxT("&Toolbar"));
618 menuBar
->Append(toolMenu
, wxT("Tool&s"));
619 menuBar
->Append(helpMenu
, wxT("&Help"));
621 // Associate the menu bar with the frame
624 menuBar
->Check(IDM_TOOLBAR_TOGGLE_TOOLBAR
, true);
625 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, true);
626 menuBar
->Check(IDM_TOOLBAR_TOGGLETOOLTIPS
, true);
628 menuBar
->Check(IDM_TOOLBAR_TOP_ORIENTATION
, true );
629 m_toolbarPosition
= TOOLBAR_TOP
;
631 // Create the toolbar
634 m_panel
= new wxPanel(this, wxID_ANY
);
635 #if USE_UNMANAGED_TOOLBAR
636 m_extraToolBar
= new wxToolBar(m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_TEXT
|wxTB_FLAT
|wxTB_TOP
);
637 PopulateToolbar(m_extraToolBar
);
640 m_textWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
642 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
643 m_panel
->SetSizer(sizer
);
644 #if USE_UNMANAGED_TOOLBAR
646 sizer
->Add(m_extraToolBar
, 0, wxEXPAND
, 0);
648 sizer
->Add(m_textWindow
, 1, wxEXPAND
, 0);
653 if ( m_searchTool
&& !m_searchTool
->GetToolBar() )
655 // we currently can't delete a toolbar tool ourselves, so we have to
656 // attach it to the toolbar just for it to be deleted, this is pretty
657 // ugly and will need to be changed
658 GetToolBar()->AddTool(m_searchTool
);
662 void MyFrame::LayoutChildren()
664 wxSize size
= GetClientSize();
669 m_tbar
->SetSize(0, 0, wxDefaultCoord
, size
.y
);
671 offset
= m_tbar
->GetSize().x
;
678 m_panel
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
681 void MyFrame::OnSize(wxSizeEvent
& event
)
693 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
695 wxToolBar
*tbar
= GetToolBar();
703 // notice that there is no need to call SetToolBar(NULL) here (although
704 // this it is harmless to do and it must be called if you do not delete
705 // the toolbar but keep it for later reuse), just delete the toolbar
706 // directly and it will reset the associated frame toolbar pointer
711 void MyFrame::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
713 m_horzText
= !m_horzText
;
718 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
726 long style
= GetToolBar() ? GetToolBar()->GetWindowStyle()
728 style
&= ~wxTB_HORIZONTAL
;
729 style
|= wxTB_VERTICAL
;
731 m_tbar
= new wxToolBar(this, wxID_ANY
,
732 wxDefaultPosition
, wxDefaultSize
,
735 m_tbar
->SetMargins(4, 4);
737 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_1
, wxT("First"), wxBITMAP(new));
738 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, wxT("Second"), wxBITMAP(open
));
739 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, wxT("Third"), wxBITMAP(save
));
740 m_tbar
->AddSeparator();
741 m_tbar
->AddTool(wxID_HELP
, wxT("Help"), wxBITMAP(help
));
749 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
751 m_smallToolbar
= !m_smallToolbar
;
756 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
758 // m_rows may be only 1 or 2
761 wxToolBar
* const toolBar
= GetToolBar();
762 toolBar
->SetRows(toolBar
->IsVertical() ? toolBar
->GetToolsCount() / m_rows
765 //RecreateToolbar(); -- this is unneeded
768 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
770 m_showTooltips
= !m_showTooltips
;
775 void MyFrame::OnToggleCustomDisabled(wxCommandEvent
& WXUNUSED(event
))
777 m_useCustomDisabled
= !m_useCustomDisabled
;
782 void MyFrame::OnChangeOrientation(wxCommandEvent
& event
)
784 switch( event
.GetId() )
786 case IDM_TOOLBAR_LEFT_ORIENTATION
:
787 m_toolbarPosition
= TOOLBAR_LEFT
;
789 case IDM_TOOLBAR_TOP_ORIENTATION
:
790 m_toolbarPosition
= TOOLBAR_TOP
;
792 case IDM_TOOLBAR_RIGHT_ORIENTATION
:
793 m_toolbarPosition
= TOOLBAR_RIGHT
;
795 case IDM_TOOLBAR_BOTTOM_ORIENTATION
:
796 m_toolbarPosition
= TOOLBAR_BOTTOM
;
802 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
807 void MyFrame::OnAbout(wxCommandEvent
& event
)
809 if ( event
.IsChecked() )
810 m_textWindow
->WriteText( wxT("Help button down now.\n") );
812 m_textWindow
->WriteText( wxT("Help button up now.\n") );
814 (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
817 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
820 str
.Printf( wxT("Clicked on tool %d\n"), event
.GetId());
821 m_textWindow
->WriteText( str
);
823 if (event
.GetId() == wxID_COPY
)
828 if (event
.GetId() == wxID_CUT
)
833 if (event
.GetId() == wxID_PRINT
)
839 void MyFrame::OnToolRightClick(wxCommandEvent
& event
)
841 m_textWindow
->AppendText(
842 wxString::Format(wxT("Tool %d right clicked.\n"),
843 (int) event
.GetInt()));
846 void MyFrame::OnCombo(wxCommandEvent
& event
)
848 wxLogStatus(wxT("Combobox string '%s' selected"), event
.GetString().c_str());
851 void MyFrame::DoEnablePrint()
856 wxToolBarBase
*tb
= GetToolBar();
857 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
860 void MyFrame::DoDeletePrint()
865 wxToolBarBase
*tb
= GetToolBar();
866 tb
->DeleteTool( wxID_PRINT
);
871 void MyFrame::DoToggleHelp()
873 wxToolBarBase
*tb
= GetToolBar();
874 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
877 void MyFrame::OnToggleSearch(wxCommandEvent
& WXUNUSED(event
))
879 wxToolBarBase
* const tb
= GetToolBar();
882 wxSearchCtrl
* const srch
= new wxSearchCtrl(tb
, wxID_ANY
, "needle");
883 srch
->SetMinSize(wxSize(80, -1));
884 m_searchTool
= tb
->AddControl(srch
);
886 else // tool already exists
888 wxControl
* const win
= m_searchTool
->GetControl();
889 if ( m_searchTool
->GetToolBar() )
891 // attached now, remove it
893 tb
->RemoveTool(m_searchTool
->GetId());
895 else // tool exists in detached state, attach it back
897 tb
->AddTool(m_searchTool
);
905 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
907 event
.Enable( m_textWindow
->CanCopy() );
910 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
912 wxToolBar
*tbar
= GetToolBar();
913 event
.Enable( tbar
&&
914 tbar
->HasFlag(wxTB_TEXT
) &&
915 !tbar
->HasFlag(wxTB_NOICONS
) );
918 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
920 GetToolBar()->SetToolShortHelp(wxID_NEW
, wxT("New toolbar button"));
923 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
925 long style
= GetToolBar()->GetWindowStyle();
926 style
&= ~(wxTB_NOICONS
| wxTB_HORZ_TEXT
);
928 switch ( event
.GetId() )
930 case IDM_TOOLBAR_SHOW_TEXT
:
931 style
|= wxTB_NOICONS
| (m_horzText
? wxTB_HORZ_TEXT
: wxTB_TEXT
);
934 case IDM_TOOLBAR_SHOW_ICONS
:
938 case IDM_TOOLBAR_SHOW_BOTH
:
939 style
|= (m_horzText
? wxTB_HORZ_TEXT
: wxTB_TEXT
);
942 GetToolBar()->SetWindowStyle(style
);
945 void MyFrame::OnToolbarBgCol(wxCommandEvent
& WXUNUSED(event
))
947 wxColour col
= wxGetColourFromUser
950 GetToolBar()->GetBackgroundColour(),
951 "Toolbar background colour"
955 GetToolBar()->SetBackgroundColour(col
);
956 GetToolBar()->Refresh();
960 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
962 m_pathBmp
= wxLoadFileSelector("custom bitmap", "");
967 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
971 wxToolBarBase
*tb
= GetToolBar();
972 tb
->InsertTool(0, wxID_PRINT
, wxT("New print"),
973 wxBITMAP(print
), wxNullBitmap
,
975 wxT("Delete this tool"),
976 wxT("This button was inserted into the toolbar"));
978 // must call Realize() after adding a new button
982 void MyFrame::OnToggleRadioBtn(wxCommandEvent
& event
)
986 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
987 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
991 void MyFrame::OnToolDropdown(wxCommandEvent
& event
)
994 str
.Printf( wxT("Dropdown on tool %d\n"), event
.GetId());
995 m_textWindow
->WriteText( str
);