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/filedlg.h"
35 #include "wx/colordlg.h"
36 #include "wx/srchctrl.h"
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
46 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
47 #error You need to enable XPM support to use XPM bitmaps with toolbar!
48 #endif // USE_XPM_BITMAPS
50 // If this is 1, the sample will test an extra toolbar identical to the
51 // main one, but not managed by the frame. This can test subtle differences
52 // in the way toolbars are handled, especially on Mac where there is one
53 // native, 'installed' toolbar.
54 #define USE_UNMANAGED_TOOLBAR 0
56 // Define this as 0 for the platforms not supporting controls in toolbars
57 #define USE_CONTROLS_IN_TOOLBAR 1
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #if !defined(__WXMSW__) && !defined(__WXPM__)
64 #include "mondrian.xpm"
68 #include "bitmaps/new.xpm"
69 #include "bitmaps/open.xpm"
70 #include "bitmaps/save.xpm"
71 #include "bitmaps/copy.xpm"
72 #include "bitmaps/cut.xpm"
73 #include "bitmaps/preview.xpm" // paste XPM
74 #include "bitmaps/print.xpm"
75 #include "bitmaps/help.xpm"
76 #endif // USE_XPM_BITMAPS
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // Define a new application
91 class MyApp
: public wxApp
98 class MyFrame
: public wxFrame
101 MyFrame(wxFrame
*parent
,
102 wxWindowID id
= wxID_ANY
,
103 const wxString
& title
= wxT("wxToolBar Sample"),
104 const wxPoint
& pos
= wxDefaultPosition
,
105 const wxSize
& size
= wxDefaultSize
,
106 long style
= wxDEFAULT_FRAME_STYLE
|wxCLIP_CHILDREN
|wxNO_FULL_REPAINT_ON_RESIZE
);
109 void PopulateToolbar(wxToolBarBase
* toolBar
);
110 void RecreateToolbar();
112 void OnQuit(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
115 void OnSize(wxSizeEvent
& event
);
117 void OnToggleToolbar(wxCommandEvent
& event
);
118 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
119 void OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
));
121 void OnToggleToolbarSize(wxCommandEvent
& event
);
122 void OnChangeOrientation(wxCommandEvent
& event
);
123 void OnToggleToolbarRows(wxCommandEvent
& event
);
124 void OnToggleTooltips(wxCommandEvent
& event
);
125 void OnToggleCustomDisabled(wxCommandEvent
& event
);
127 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
128 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
129 void OnInsertPrint(wxCommandEvent
& event
);
130 void OnChangeToolTip(wxCommandEvent
& event
);
131 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
132 void OnToggleSearch(wxCommandEvent
& event
);
133 void OnToggleRadioBtn(wxCommandEvent
& event
);
135 void OnToolbarStyle(wxCommandEvent
& event
);
136 void OnToolbarBgCol(wxCommandEvent
& event
);
137 void OnToolbarCustomBitmap(wxCommandEvent
& event
);
139 void OnToolLeftClick(wxCommandEvent
& event
);
140 void OnToolRightClick(wxCommandEvent
& event
);
141 void OnToolDropdown(wxCommandEvent
& event
);
143 void OnCombo(wxCommandEvent
& event
);
145 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
146 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
147 void OnUpdateToggleRadioBtn(wxUpdateUIEvent
& event
)
148 { event
.Enable( m_tbar
!= NULL
); }
151 void DoEnablePrint();
152 void DoDeletePrint();
155 void LayoutChildren();
161 size_t m_rows
; // 1 or 2 only
163 // the number of print buttons we have (they're added/removed dynamically)
166 // store toolbar position for future use
167 Positions m_toolbarPosition
;
169 wxTextCtrl
*m_textWindow
;
172 #if USE_UNMANAGED_TOOLBAR
173 wxToolBar
*m_extraToolBar
;
178 // the path to the custom bitmap for the test toolbar tool
181 // the search tool, initially NULL
182 wxToolBarToolBase
*m_searchTool
;
184 DECLARE_EVENT_TABLE()
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 const int ID_TOOLBAR
= 500;
193 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
197 // toolbar menu items
198 IDM_TOOLBAR_TOGGLE_TOOLBAR
= 200,
199 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
200 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
201 IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
202 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
203 IDM_TOOLBAR_TOGGLETOOLTIPS
,
204 IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
205 IDM_TOOLBAR_SHOW_TEXT
,
206 IDM_TOOLBAR_SHOW_ICONS
,
207 IDM_TOOLBAR_SHOW_BOTH
,
209 IDM_TOOLBAR_CUSTOM_PATH
,
210 IDM_TOOLBAR_TOP_ORIENTATION
,
211 IDM_TOOLBAR_LEFT_ORIENTATION
,
212 IDM_TOOLBAR_BOTTOM_ORIENTATION
,
213 IDM_TOOLBAR_RIGHT_ORIENTATION
,
219 IDM_TOOLBAR_ENABLEPRINT
,
220 IDM_TOOLBAR_DELETEPRINT
,
221 IDM_TOOLBAR_INSERTPRINT
,
222 IDM_TOOLBAR_TOGGLEHELP
,
223 IDM_TOOLBAR_TOGGLESEARCH
,
224 IDM_TOOLBAR_TOGGLERADIOBTN1
,
225 IDM_TOOLBAR_TOGGLERADIOBTN2
,
226 IDM_TOOLBAR_TOGGLERADIOBTN3
,
227 IDM_TOOLBAR_CHANGE_TOOLTIP
,
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
239 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
240 EVT_SIZE(MyFrame::OnSize
)
242 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
243 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
245 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
246 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
247 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame::OnToggleHorizontalText
)
249 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION
, IDM_TOOLBAR_RIGHT_ORIENTATION
, MyFrame::OnChangeOrientation
)
250 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
251 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
252 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS
, MyFrame::OnToggleTooltips
)
253 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
, MyFrame::OnToggleCustomDisabled
)
255 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
256 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
257 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
258 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
259 EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH
, MyFrame::OnToggleSearch
)
260 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
, IDM_TOOLBAR_TOGGLERADIOBTN3
,
261 MyFrame::OnToggleRadioBtn
)
262 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
264 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
265 MyFrame::OnToolbarStyle
)
266 EVT_MENU(IDM_TOOLBAR_BG_COL
, MyFrame::OnToolbarBgCol
)
268 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH
, MyFrame::OnToolbarCustomBitmap
)
270 EVT_MENU(wxID_ANY
, MyFrame::OnToolLeftClick
)
272 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
274 EVT_TOOL_RCLICKED(wxID_ANY
, MyFrame::OnToolRightClick
)
276 EVT_TOOL_DROPDOWN(wxID_ANY
, MyFrame::OnToolDropdown
)
278 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
279 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
281 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
,
282 IDM_TOOLBAR_TOGGLERADIOBTN3
,
283 MyFrame::OnUpdateToggleRadioBtn
)
284 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
285 MyFrame::OnUpdateToggleHorzText
)
288 // ============================================================================
290 // ============================================================================
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
298 // The `main program' equivalent, creating the windows and returning the
302 if ( !wxApp::OnInit() )
305 // Create the main frame window
306 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxID_ANY
,
307 wxT("wxToolBar Sample"),
308 wxPoint(100, 100), wxSize(550, 300));
313 frame
->SetStatusText(wxT("Hello, wxWidgets"));
316 wxInitAllImageHandlers();
323 void MyFrame::RecreateToolbar()
326 // On Windows CE, we should not delete the
327 // previous toolbar in case it contains the menubar.
328 // We'll try to accommodate this usage in due course.
329 wxToolBar
* toolBar
= CreateToolBar();
331 // delete and recreate the toolbar
332 wxToolBarBase
*toolBar
= GetToolBar();
333 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
335 if (toolBar
&& m_searchTool
&& m_searchTool
->GetToolBar() == NULL
)
338 toolBar
->AddTool(m_searchTool
);
346 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_BOTTOM
| wxTB_RIGHT
| wxTB_HORZ_LAYOUT
);
347 switch( m_toolbarPosition
)
359 style
|= wxTB_BOTTOM
;
363 if ( m_showTooltips
)
364 style
&= ~wxTB_NO_TOOLTIPS
;
366 style
|= wxTB_NO_TOOLTIPS
;
368 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
369 style
|= wxTB_HORZ_LAYOUT
;
371 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
374 PopulateToolbar(toolBar
);
377 void MyFrame::PopulateToolbar(wxToolBarBase
* toolBar
)
393 wxBitmap toolBarBitmaps
[Tool_Max
];
396 #define INIT_TOOL_BMP(bmp) \
397 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
398 #else // !USE_XPM_BITMAPS
399 #define INIT_TOOL_BMP(bmp) \
400 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
401 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
408 INIT_TOOL_BMP(paste
);
409 INIT_TOOL_BMP(print
);
412 int w
= toolBarBitmaps
[Tool_new
].GetWidth(),
413 h
= toolBarBitmaps
[Tool_new
].GetHeight();
415 if ( !m_smallToolbar
)
420 for ( size_t n
= Tool_new
; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
423 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
427 // this call is actually unnecessary as the toolbar will adjust its tools
428 // size to fit the biggest icon used anyhow but it doesn't hurt neither
429 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
431 toolBar
->AddTool(wxID_NEW
, wxT("New"),
432 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
433 wxT("New file"), wxT("This is help for new file tool"));
435 wxMenu
* menu
= new wxMenu
;
436 menu
->Append(wxID_ANY
, wxT("&First dummy item"));
437 menu
->Append(wxID_ANY
, wxT("&Second dummy item"));
438 menu
->AppendSeparator();
439 menu
->Append(wxID_EXIT
, wxT("Exit"));
440 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
442 toolBar
->AddTool(wxID_OPEN
, wxT("Open"),
443 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
444 wxT("Open file"), wxT("This is help for open file tool"));
446 #if USE_CONTROLS_IN_TOOLBAR
447 // adding a combo to a vertical toolbar is not very smart
448 if ( !toolBar
->IsVertical() )
450 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, wxEmptyString
, wxDefaultPosition
, wxSize(100,-1) );
451 combo
->Append(wxT("This"));
452 combo
->Append(wxT("is a"));
453 combo
->Append(wxT("combobox"));
454 combo
->Append(wxT("in a"));
455 combo
->Append(wxT("toolbar"));
456 toolBar
->AddControl(combo
, wxT("Combo Label"));
458 #endif // USE_CONTROLS_IN_TOOLBAR
460 toolBar
->AddTool(wxID_SAVE
, wxT("Save"), toolBarBitmaps
[Tool_save
], wxT("Toggle button 1"), wxITEM_CHECK
);
461 toolBar
->AddTool(wxID_COPY
, wxT("Copy"), toolBarBitmaps
[Tool_copy
], wxT("Toggle button 2"), wxITEM_CHECK
);
462 toolBar
->AddTool(wxID_CUT
, wxT("Cut"), toolBarBitmaps
[Tool_cut
], wxT("Toggle/Untoggle help button"));
463 toolBar
->AddTool(wxID_PASTE
, wxT("Paste"), toolBarBitmaps
[Tool_paste
], wxT("Paste"));
465 if ( m_useCustomDisabled
)
467 wxBitmap
bmpDisabled(w
, h
);
470 dc
.SelectObject(bmpDisabled
);
471 dc
.DrawBitmap(toolBarBitmaps
[Tool_print
], 0, 0);
473 wxPen
pen(*wxRED
, 5);
475 dc
.DrawLine(0, 0, w
, h
);
478 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
483 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
484 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."));
487 toolBar
->AddSeparator();
488 toolBar
->AddTool(wxID_HELP
, wxT("Help"), toolBarBitmaps
[Tool_help
], wxT("Help button"), wxITEM_CHECK
);
490 if ( !m_pathBmp
.empty() )
492 // create a tool with a custom bitmap for testing
493 wxImage
img(m_pathBmp
);
496 if ( img
.GetWidth() > w
&& img
.GetHeight() > h
)
497 img
= img
.GetSubImage(wxRect(0, 0, w
, h
));
499 toolBar
->AddSeparator();
500 toolBar
->AddTool(wxID_ANY
, wxT("Custom"), img
);
504 // after adding the buttons to the toolbar, must call Realize() to reflect
508 toolBar
->SetRows(!(toolBar
->IsVertical()) ? m_rows
: 10 / m_rows
);
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 // Define my frame constructor
516 MyFrame::MyFrame(wxFrame
* parent
,
518 const wxString
& title
,
522 : wxFrame(parent
, id
, title
, pos
, size
, style
)
526 m_smallToolbar
= true;
528 m_useCustomDisabled
= false;
529 m_showTooltips
= true;
536 // Give it a status line
541 SetIcon(wxICON(mondrian
));
544 wxMenu
*tbarMenu
= new wxMenu
;
545 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
546 wxT("Toggle &toolbar\tCtrl-Z"),
547 wxT("Show or hide the toolbar"));
549 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
550 wxT("Toggle &another toolbar\tCtrl-A"),
551 wxT("Show/hide another test toolbar"));
553 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
554 wxT("Toggle hori&zontal text\tCtrl-H"),
555 wxT("Show text under/alongside the icon"));
557 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
558 wxT("&Toggle toolbar size\tCtrl-S"),
559 wxT("Toggle between big/small toolbar"));
561 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
562 wxT("Toggle number of &rows\tCtrl-R"),
563 wxT("Toggle number of toolbar rows between 1 and 2"));
565 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
566 wxT("Show &tooltips\tCtrl-L"),
567 wxT("Show tooltips for the toolbar tools"));
569 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
570 wxT("Use c&ustom disabled images\tCtrl-U"),
571 wxT("Switch between using system-generated and custom disabled images"));
574 tbarMenu
->AppendSeparator();
575 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
576 wxT("Set toolbar at the top of the window"),
577 wxT("Set toolbar at the top of the window"));
578 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
579 wxT("Set toolbar at the left of the window"),
580 wxT("Set toolbar at the left of the window"));
581 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
582 wxT("Set toolbar at the bottom of the window"),
583 wxT("Set toolbar at the bottom of the window"));
584 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
585 wxT("Set toolbar at the right edge of the window"),
586 wxT("Set toolbar at the right edge of the window"));
587 tbarMenu
->AppendSeparator();
589 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, wxT("Show &text\tCtrl-Alt-T"));
590 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, wxT("Show &icons\tCtrl-Alt-I"));
591 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, wxT("Show &both\tCtrl-Alt-B"));
592 tbarMenu
->AppendSeparator();
593 tbarMenu
->Append(IDM_TOOLBAR_BG_COL
, wxT("Choose bac&kground colour..."));
594 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, wxT("Custom &bitmap...\tCtrl-B"));
596 wxMenu
*toolMenu
= new wxMenu
;
597 toolMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, wxT("&Enable print button\tCtrl-E"));
598 toolMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, wxT("&Delete print button\tCtrl-D"));
599 toolMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, wxT("&Insert print button\tCtrl-I"));
600 toolMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, wxT("Toggle &help button\tCtrl-T"));
601 toolMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH
, wxT("Toggle &search field\tCtrl-F"));
602 toolMenu
->AppendSeparator();
603 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, wxT("Toggle &1st radio button\tCtrl-1"));
604 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, wxT("Toggle &2nd radio button\tCtrl-2"));
605 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, wxT("Toggle &3rd radio button\tCtrl-3"));
606 toolMenu
->AppendSeparator();
607 toolMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, wxT("Change tooltip of \"New\""));
609 wxMenu
*fileMenu
= new wxMenu
;
610 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
612 wxMenu
*helpMenu
= new wxMenu
;
613 helpMenu
->Append(wxID_HELP
, wxT("&About"), wxT("About toolbar sample"));
615 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
617 menuBar
->Append(fileMenu
, wxT("&File"));
618 menuBar
->Append(tbarMenu
, wxT("&Toolbar"));
619 menuBar
->Append(toolMenu
, wxT("Tool&s"));
620 menuBar
->Append(helpMenu
, wxT("&Help"));
622 // Associate the menu bar with the frame
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();
709 void MyFrame::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
711 m_horzText
= !m_horzText
;
716 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
725 long style
= GetToolBar() ? GetToolBar()->GetWindowStyle()
727 style
&= ~wxTB_HORIZONTAL
;
728 style
|= wxTB_VERTICAL
;
730 m_tbar
= new wxToolBar(this, wxID_ANY
,
731 wxDefaultPosition
, wxDefaultSize
,
734 m_tbar
->SetMargins(4, 4);
736 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_1
, wxT("First"), wxBITMAP(new));
737 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, wxT("Second"), wxBITMAP(open
));
738 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, wxT("Third"), wxBITMAP(save
));
739 m_tbar
->AddSeparator();
740 m_tbar
->AddTool(wxID_HELP
, wxT("Help"), wxBITMAP(help
));
748 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
750 m_smallToolbar
= !m_smallToolbar
;
755 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
757 // m_rows may be only 1 or 2
760 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows
: 10 / m_rows
);
762 //RecreateToolbar(); -- this is unneeded
765 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
767 m_showTooltips
= !m_showTooltips
;
772 void MyFrame::OnToggleCustomDisabled(wxCommandEvent
& WXUNUSED(event
))
774 m_useCustomDisabled
= !m_useCustomDisabled
;
779 void MyFrame::OnChangeOrientation(wxCommandEvent
& event
)
781 switch( event
.GetId() )
783 case IDM_TOOLBAR_LEFT_ORIENTATION
:
784 m_toolbarPosition
= TOOLBAR_LEFT
;
786 case IDM_TOOLBAR_TOP_ORIENTATION
:
787 m_toolbarPosition
= TOOLBAR_TOP
;
789 case IDM_TOOLBAR_RIGHT_ORIENTATION
:
790 m_toolbarPosition
= TOOLBAR_RIGHT
;
792 case IDM_TOOLBAR_BOTTOM_ORIENTATION
:
793 m_toolbarPosition
= TOOLBAR_BOTTOM
;
799 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
804 void MyFrame::OnAbout(wxCommandEvent
& event
)
806 if ( event
.IsChecked() )
807 m_textWindow
->WriteText( wxT("Help button down now.\n") );
809 m_textWindow
->WriteText( wxT("Help button up now.\n") );
811 (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
814 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
817 str
.Printf( wxT("Clicked on tool %d\n"), event
.GetId());
818 m_textWindow
->WriteText( str
);
820 if (event
.GetId() == wxID_COPY
)
825 if (event
.GetId() == wxID_CUT
)
830 if (event
.GetId() == wxID_PRINT
)
836 void MyFrame::OnToolRightClick(wxCommandEvent
& event
)
838 m_textWindow
->AppendText(
839 wxString::Format(wxT("Tool %d right clicked.\n"),
840 (int) event
.GetInt()));
843 void MyFrame::OnCombo(wxCommandEvent
& event
)
845 wxLogStatus(wxT("Combobox string '%s' selected"), event
.GetString().c_str());
848 void MyFrame::DoEnablePrint()
853 wxToolBarBase
*tb
= GetToolBar();
854 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
857 void MyFrame::DoDeletePrint()
862 wxToolBarBase
*tb
= GetToolBar();
863 tb
->DeleteTool( wxID_PRINT
);
868 void MyFrame::DoToggleHelp()
870 wxToolBarBase
*tb
= GetToolBar();
871 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
874 void MyFrame::OnToggleSearch(wxCommandEvent
& WXUNUSED(event
))
876 static const int searchPos
= 3;
878 wxToolBarBase
* const tb
= GetToolBar();
881 wxSearchCtrl
* const srch
= new wxSearchCtrl(tb
, wxID_ANY
, "needle");
882 srch
->SetMinSize(wxSize(80, -1));
883 m_searchTool
= tb
->InsertControl(searchPos
, srch
);
885 else // tool already exists
887 wxControl
* const win
= m_searchTool
->GetControl();
888 if ( m_searchTool
->GetToolBar() )
890 // attached now, remove it
892 tb
->RemoveTool(m_searchTool
->GetId());
894 else // tool exists in detached state, attach it back
896 tb
->InsertTool(searchPos
, m_searchTool
);
904 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
906 event
.Enable( m_textWindow
->CanCopy() );
909 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
911 wxToolBar
*tbar
= GetToolBar();
912 event
.Enable( tbar
&&
913 tbar
->HasFlag(wxTB_TEXT
) &&
914 !tbar
->HasFlag(wxTB_NOICONS
) );
917 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
919 GetToolBar()->SetToolShortHelp(wxID_NEW
, wxT("New toolbar button"));
922 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
924 long style
= GetToolBar()->GetWindowStyle();
925 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
927 switch ( event
.GetId() )
929 case IDM_TOOLBAR_SHOW_TEXT
:
930 style
|= wxTB_NOICONS
| wxTB_TEXT
;
933 case IDM_TOOLBAR_SHOW_ICONS
:
937 case IDM_TOOLBAR_SHOW_BOTH
:
941 GetToolBar()->SetWindowStyle(style
);
944 void MyFrame::OnToolbarBgCol(wxCommandEvent
& WXUNUSED(event
))
946 wxColour col
= wxGetColourFromUser
949 GetToolBar()->GetBackgroundColour(),
950 "Toolbar background colour"
954 GetToolBar()->SetBackgroundColour(col
);
955 GetToolBar()->Refresh();
959 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
961 m_pathBmp
= wxLoadFileSelector("custom bitmap", "");
966 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
970 wxToolBarBase
*tb
= GetToolBar();
971 tb
->InsertTool(0, wxID_PRINT
, wxT("New print"),
972 wxBITMAP(print
), wxNullBitmap
,
974 wxT("Delete this tool"),
975 wxT("This button was inserted into the toolbar"));
977 // must call Realize() after adding a new button
981 void MyFrame::OnToggleRadioBtn(wxCommandEvent
& event
)
985 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
986 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
990 void MyFrame::OnToolDropdown(wxCommandEvent
& event
)
993 str
.Printf( wxT("Dropdown on tool %d\n"), event
.GetId());
994 m_textWindow
->WriteText( str
);