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 OnToolbarCustomBg(wxCommandEvent
& event
);
138 void OnToolbarCustomBitmap(wxCommandEvent
& event
);
140 void OnToolLeftClick(wxCommandEvent
& event
);
141 void OnToolRightClick(wxCommandEvent
& event
);
142 void OnToolDropdown(wxCommandEvent
& event
);
144 void OnCombo(wxCommandEvent
& event
);
146 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
147 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
148 void OnUpdateToggleRadioBtn(wxUpdateUIEvent
& event
)
149 { event
.Enable( m_tbar
!= NULL
); }
152 void OnEraseToolBarBackground(wxEraseEvent
& event
);
154 void DoEnablePrint();
155 void DoDeletePrint();
158 void LayoutChildren();
164 size_t m_rows
; // 1 or 2 only
166 // the number of print buttons we have (they're added/removed dynamically)
169 // store toolbar position for future use
170 Positions m_toolbarPosition
;
172 wxTextCtrl
*m_textWindow
;
175 #if USE_UNMANAGED_TOOLBAR
176 wxToolBar
*m_extraToolBar
;
181 // the path to the custom bitmap for the test toolbar tool
184 // the search tool, initially NULL
185 wxToolBarToolBase
*m_searchTool
;
187 DECLARE_EVENT_TABLE()
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 const int ID_TOOLBAR
= 500;
196 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
200 // toolbar menu items
201 IDM_TOOLBAR_TOGGLE_TOOLBAR
= 200,
202 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
203 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
204 IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
205 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
206 IDM_TOOLBAR_TOGGLETOOLTIPS
,
207 IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
208 IDM_TOOLBAR_SHOW_TEXT
,
209 IDM_TOOLBAR_SHOW_ICONS
,
210 IDM_TOOLBAR_SHOW_BOTH
,
212 IDM_TOOLBAR_CUSTOM_BG
,
213 IDM_TOOLBAR_CUSTOM_PATH
,
214 IDM_TOOLBAR_TOP_ORIENTATION
,
215 IDM_TOOLBAR_LEFT_ORIENTATION
,
216 IDM_TOOLBAR_BOTTOM_ORIENTATION
,
217 IDM_TOOLBAR_RIGHT_ORIENTATION
,
223 IDM_TOOLBAR_ENABLEPRINT
,
224 IDM_TOOLBAR_DELETEPRINT
,
225 IDM_TOOLBAR_INSERTPRINT
,
226 IDM_TOOLBAR_TOGGLEHELP
,
227 IDM_TOOLBAR_TOGGLESEARCH
,
228 IDM_TOOLBAR_TOGGLERADIOBTN1
,
229 IDM_TOOLBAR_TOGGLERADIOBTN2
,
230 IDM_TOOLBAR_TOGGLERADIOBTN3
,
231 IDM_TOOLBAR_CHANGE_TOOLTIP
,
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
243 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
244 EVT_SIZE(MyFrame::OnSize
)
246 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
247 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
249 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
250 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
251 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame::OnToggleHorizontalText
)
253 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION
, IDM_TOOLBAR_RIGHT_ORIENTATION
, MyFrame::OnChangeOrientation
)
254 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
255 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
256 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS
, MyFrame::OnToggleTooltips
)
257 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
, MyFrame::OnToggleCustomDisabled
)
259 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
260 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
261 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
262 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
263 EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH
, MyFrame::OnToggleSearch
)
264 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
, IDM_TOOLBAR_TOGGLERADIOBTN3
,
265 MyFrame::OnToggleRadioBtn
)
266 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
268 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
269 MyFrame::OnToolbarStyle
)
270 EVT_MENU(IDM_TOOLBAR_BG_COL
, MyFrame::OnToolbarBgCol
)
271 EVT_MENU(IDM_TOOLBAR_CUSTOM_BG
, MyFrame::OnToolbarCustomBg
)
273 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH
, MyFrame::OnToolbarCustomBitmap
)
275 EVT_MENU(wxID_ANY
, MyFrame::OnToolLeftClick
)
277 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
279 EVT_TOOL_RCLICKED(wxID_ANY
, MyFrame::OnToolRightClick
)
281 EVT_TOOL_DROPDOWN(wxID_ANY
, MyFrame::OnToolDropdown
)
283 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
284 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
286 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
,
287 IDM_TOOLBAR_TOGGLERADIOBTN3
,
288 MyFrame::OnUpdateToggleRadioBtn
)
289 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
290 MyFrame::OnUpdateToggleHorzText
)
293 // ============================================================================
295 // ============================================================================
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
303 // The `main program' equivalent, creating the windows and returning the
307 if ( !wxApp::OnInit() )
310 // Create the main frame window
311 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxID_ANY
,
312 wxT("wxToolBar Sample"),
313 wxPoint(100, 100), wxSize(550, 300));
318 frame
->SetStatusText(wxT("Hello, wxWidgets"));
321 wxInitAllImageHandlers();
328 void MyFrame::RecreateToolbar()
331 // On Windows CE, we should not delete the
332 // previous toolbar in case it contains the menubar.
333 // We'll try to accommodate this usage in due course.
334 wxToolBar
* toolBar
= CreateToolBar();
336 // delete and recreate the toolbar
337 wxToolBarBase
*toolBar
= GetToolBar();
338 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
340 if (toolBar
&& m_searchTool
&& m_searchTool
->GetToolBar() == NULL
)
343 toolBar
->AddTool(m_searchTool
);
351 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_BOTTOM
| wxTB_RIGHT
| wxTB_HORZ_LAYOUT
);
352 switch( m_toolbarPosition
)
364 style
|= wxTB_BOTTOM
;
368 if ( m_showTooltips
)
369 style
&= ~wxTB_NO_TOOLTIPS
;
371 style
|= wxTB_NO_TOOLTIPS
;
373 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
374 style
|= wxTB_HORZ_LAYOUT
;
376 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
379 if ( GetMenuBar()->IsChecked(IDM_TOOLBAR_CUSTOM_BG
) )
383 wxEVT_ERASE_BACKGROUND
,
384 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
390 PopulateToolbar(toolBar
);
393 void MyFrame::PopulateToolbar(wxToolBarBase
* toolBar
)
409 wxBitmap toolBarBitmaps
[Tool_Max
];
412 #define INIT_TOOL_BMP(bmp) \
413 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
414 #else // !USE_XPM_BITMAPS
415 #define INIT_TOOL_BMP(bmp) \
416 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
417 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
424 INIT_TOOL_BMP(paste
);
425 INIT_TOOL_BMP(print
);
428 int w
= toolBarBitmaps
[Tool_new
].GetWidth(),
429 h
= toolBarBitmaps
[Tool_new
].GetHeight();
431 if ( !m_smallToolbar
)
436 for ( size_t n
= Tool_new
; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
439 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
443 // this call is actually unnecessary as the toolbar will adjust its tools
444 // size to fit the biggest icon used anyhow but it doesn't hurt neither
445 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
447 toolBar
->AddTool(wxID_NEW
, wxT("New"),
448 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
449 wxT("New file"), wxT("This is help for new file tool"));
451 wxMenu
* menu
= new wxMenu
;
452 menu
->Append(wxID_ANY
, wxT("&First dummy item"));
453 menu
->Append(wxID_ANY
, wxT("&Second dummy item"));
454 menu
->AppendSeparator();
455 menu
->Append(wxID_EXIT
, wxT("Exit"));
456 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
458 toolBar
->AddTool(wxID_OPEN
, wxT("Open"),
459 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
460 wxT("Open file"), wxT("This is help for open file tool"));
462 #if USE_CONTROLS_IN_TOOLBAR
463 // adding a combo to a vertical toolbar is not very smart
464 if ( !toolBar
->IsVertical() )
466 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, wxEmptyString
, wxDefaultPosition
, wxSize(100,-1) );
467 combo
->Append(wxT("This"));
468 combo
->Append(wxT("is a"));
469 combo
->Append(wxT("combobox"));
470 combo
->Append(wxT("in a"));
471 combo
->Append(wxT("toolbar"));
472 toolBar
->AddControl(combo
, wxT("Combo Label"));
474 #endif // USE_CONTROLS_IN_TOOLBAR
476 toolBar
->AddTool(wxID_SAVE
, wxT("Save"), toolBarBitmaps
[Tool_save
], wxT("Toggle button 1"), wxITEM_CHECK
);
477 toolBar
->AddTool(wxID_COPY
, wxT("Copy"), toolBarBitmaps
[Tool_copy
], wxT("Toggle button 2"), wxITEM_CHECK
);
478 toolBar
->AddTool(wxID_CUT
, wxT("Cut"), toolBarBitmaps
[Tool_cut
], wxT("Toggle/Untoggle help button"));
479 toolBar
->AddTool(wxID_PASTE
, wxT("Paste"), toolBarBitmaps
[Tool_paste
], wxT("Paste"));
481 if ( m_useCustomDisabled
)
483 wxBitmap
bmpDisabled(w
, h
);
486 dc
.SelectObject(bmpDisabled
);
487 dc
.DrawBitmap(toolBarBitmaps
[Tool_print
], 0, 0);
489 wxPen
pen(*wxRED
, 5);
491 dc
.DrawLine(0, 0, w
, h
);
494 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
499 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
500 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."));
503 toolBar
->AddSeparator();
504 toolBar
->AddTool(wxID_HELP
, wxT("Help"), toolBarBitmaps
[Tool_help
], wxT("Help button"), wxITEM_CHECK
);
506 if ( !m_pathBmp
.empty() )
508 // create a tool with a custom bitmap for testing
509 wxImage
img(m_pathBmp
);
512 if ( img
.GetWidth() > w
&& img
.GetHeight() > h
)
513 img
= img
.GetSubImage(wxRect(0, 0, w
, h
));
515 toolBar
->AddSeparator();
516 toolBar
->AddTool(wxID_ANY
, wxT("Custom"), img
);
520 // after adding the buttons to the toolbar, must call Realize() to reflect
524 toolBar
->SetRows(!(toolBar
->IsVertical()) ? m_rows
: 10 / m_rows
);
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 // Define my frame constructor
532 MyFrame::MyFrame(wxFrame
* parent
,
534 const wxString
& title
,
538 : wxFrame(parent
, id
, title
, pos
, size
, style
)
542 m_smallToolbar
= true;
544 m_useCustomDisabled
= false;
545 m_showTooltips
= true;
552 // Give it a status line
557 SetIcon(wxICON(mondrian
));
560 wxMenu
*tbarMenu
= new wxMenu
;
561 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
562 wxT("Toggle &toolbar\tCtrl-Z"),
563 wxT("Show or hide the toolbar"));
565 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
566 wxT("Toggle &another toolbar\tCtrl-A"),
567 wxT("Show/hide another test toolbar"));
569 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
570 wxT("Toggle hori&zontal text\tCtrl-H"),
571 wxT("Show text under/alongside the icon"));
573 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
574 wxT("&Toggle toolbar size\tCtrl-S"),
575 wxT("Toggle between big/small toolbar"));
577 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
578 wxT("Toggle number of &rows\tCtrl-R"),
579 wxT("Toggle number of toolbar rows between 1 and 2"));
581 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
582 wxT("Show &tooltips\tCtrl-L"),
583 wxT("Show tooltips for the toolbar tools"));
585 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
586 wxT("Use c&ustom disabled images\tCtrl-U"),
587 wxT("Switch between using system-generated and custom disabled images"));
590 tbarMenu
->AppendSeparator();
591 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
592 wxT("Set toolbar at the top of the window"),
593 wxT("Set toolbar at the top of the window"));
594 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
595 wxT("Set toolbar at the left of the window"),
596 wxT("Set toolbar at the left of the window"));
597 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
598 wxT("Set toolbar at the bottom of the window"),
599 wxT("Set toolbar at the bottom of the window"));
600 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
601 wxT("Set toolbar at the right edge of the window"),
602 wxT("Set toolbar at the right edge of the window"));
603 tbarMenu
->AppendSeparator();
605 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, wxT("Show &text\tCtrl-Alt-T"));
606 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, wxT("Show &icons\tCtrl-Alt-I"));
607 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, wxT("Show &both\tCtrl-Alt-B"));
608 tbarMenu
->AppendSeparator();
609 tbarMenu
->Append(IDM_TOOLBAR_BG_COL
, wxT("Choose bac&kground colour..."));
610 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_CUSTOM_BG
, wxT("Draw custom back&ground"));
611 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, wxT("Custom &bitmap...\tCtrl-B"));
613 wxMenu
*toolMenu
= new wxMenu
;
614 toolMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, wxT("&Enable print button\tCtrl-E"));
615 toolMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, wxT("&Delete print button\tCtrl-D"));
616 toolMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, wxT("&Insert print button\tCtrl-I"));
617 toolMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, wxT("Toggle &help button\tCtrl-T"));
618 toolMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH
, wxT("Toggle &search field\tCtrl-F"));
619 toolMenu
->AppendSeparator();
620 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, wxT("Toggle &1st radio button\tCtrl-1"));
621 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, wxT("Toggle &2nd radio button\tCtrl-2"));
622 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, wxT("Toggle &3rd radio button\tCtrl-3"));
623 toolMenu
->AppendSeparator();
624 toolMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, wxT("Change tooltip of \"New\""));
626 wxMenu
*fileMenu
= new wxMenu
;
627 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
629 wxMenu
*helpMenu
= new wxMenu
;
630 helpMenu
->Append(wxID_HELP
, wxT("&About"), wxT("About toolbar sample"));
632 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
634 menuBar
->Append(fileMenu
, wxT("&File"));
635 menuBar
->Append(tbarMenu
, wxT("&Toolbar"));
636 menuBar
->Append(toolMenu
, wxT("Tool&s"));
637 menuBar
->Append(helpMenu
, wxT("&Help"));
639 // Associate the menu bar with the frame
642 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, true);
643 menuBar
->Check(IDM_TOOLBAR_TOGGLETOOLTIPS
, true);
645 menuBar
->Check(IDM_TOOLBAR_TOP_ORIENTATION
, true );
646 m_toolbarPosition
= TOOLBAR_TOP
;
648 // Create the toolbar
651 m_panel
= new wxPanel(this, wxID_ANY
);
652 #if USE_UNMANAGED_TOOLBAR
653 m_extraToolBar
= new wxToolBar(m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_TEXT
|wxTB_FLAT
|wxTB_TOP
);
654 PopulateToolbar(m_extraToolBar
);
657 m_textWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
659 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
660 m_panel
->SetSizer(sizer
);
661 #if USE_UNMANAGED_TOOLBAR
663 sizer
->Add(m_extraToolBar
, 0, wxEXPAND
, 0);
665 sizer
->Add(m_textWindow
, 1, wxEXPAND
, 0);
670 if ( m_searchTool
&& !m_searchTool
->GetToolBar() )
672 // we currently can't delete a toolbar tool ourselves, so we have to
673 // attach it to the toolbar just for it to be deleted, this is pretty
674 // ugly and will need to be changed
675 GetToolBar()->AddTool(m_searchTool
);
679 void MyFrame::LayoutChildren()
681 wxSize size
= GetClientSize();
686 m_tbar
->SetSize(0, 0, wxDefaultCoord
, size
.y
);
688 offset
= m_tbar
->GetSize().x
;
695 m_panel
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
698 void MyFrame::OnSize(wxSizeEvent
& event
)
710 void MyFrame::OnEraseToolBarBackground(wxEraseEvent
& event
)
712 wxDC
& dc
= *event
.GetDC();
713 const wxSize size
= dc
.GetSize();
714 dc
.SetPen(*wxRED_PEN
);
715 dc
.DrawLine(0, 0, size
.x
, size
.y
);
716 dc
.DrawLine(0, size
.y
, size
.x
, 0);
719 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
721 wxToolBar
*tbar
= GetToolBar();
735 void MyFrame::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
737 m_horzText
= !m_horzText
;
742 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
751 long style
= GetToolBar() ? GetToolBar()->GetWindowStyle()
753 style
&= ~wxTB_HORIZONTAL
;
754 style
|= wxTB_VERTICAL
;
756 m_tbar
= new wxToolBar(this, wxID_ANY
,
757 wxDefaultPosition
, wxDefaultSize
,
760 m_tbar
->SetMargins(4, 4);
762 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_1
, wxT("First"), wxBITMAP(new));
763 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, wxT("Second"), wxBITMAP(open
));
764 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, wxT("Third"), wxBITMAP(save
));
765 m_tbar
->AddSeparator();
766 m_tbar
->AddTool(wxID_HELP
, wxT("Help"), wxBITMAP(help
));
774 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
776 m_smallToolbar
= !m_smallToolbar
;
781 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
783 // m_rows may be only 1 or 2
786 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows
: 10 / m_rows
);
788 //RecreateToolbar(); -- this is unneeded
791 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
793 m_showTooltips
= !m_showTooltips
;
798 void MyFrame::OnToggleCustomDisabled(wxCommandEvent
& WXUNUSED(event
))
800 m_useCustomDisabled
= !m_useCustomDisabled
;
805 void MyFrame::OnChangeOrientation(wxCommandEvent
& event
)
807 switch( event
.GetId() )
809 case IDM_TOOLBAR_LEFT_ORIENTATION
:
810 m_toolbarPosition
= TOOLBAR_LEFT
;
812 case IDM_TOOLBAR_TOP_ORIENTATION
:
813 m_toolbarPosition
= TOOLBAR_TOP
;
815 case IDM_TOOLBAR_RIGHT_ORIENTATION
:
816 m_toolbarPosition
= TOOLBAR_RIGHT
;
818 case IDM_TOOLBAR_BOTTOM_ORIENTATION
:
819 m_toolbarPosition
= TOOLBAR_BOTTOM
;
825 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
830 void MyFrame::OnAbout(wxCommandEvent
& event
)
832 if ( event
.IsChecked() )
833 m_textWindow
->WriteText( wxT("Help button down now.\n") );
835 m_textWindow
->WriteText( wxT("Help button up now.\n") );
837 (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
840 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
843 str
.Printf( wxT("Clicked on tool %d\n"), event
.GetId());
844 m_textWindow
->WriteText( str
);
846 if (event
.GetId() == wxID_COPY
)
851 if (event
.GetId() == wxID_CUT
)
856 if (event
.GetId() == wxID_PRINT
)
862 void MyFrame::OnToolRightClick(wxCommandEvent
& event
)
864 m_textWindow
->AppendText(
865 wxString::Format(wxT("Tool %d right clicked.\n"),
866 (int) event
.GetInt()));
869 void MyFrame::OnCombo(wxCommandEvent
& event
)
871 wxLogStatus(wxT("Combobox string '%s' selected"), event
.GetString().c_str());
874 void MyFrame::DoEnablePrint()
879 wxToolBarBase
*tb
= GetToolBar();
880 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
883 void MyFrame::DoDeletePrint()
888 wxToolBarBase
*tb
= GetToolBar();
889 tb
->DeleteTool( wxID_PRINT
);
894 void MyFrame::DoToggleHelp()
896 wxToolBarBase
*tb
= GetToolBar();
897 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
900 void MyFrame::OnToggleSearch(wxCommandEvent
& WXUNUSED(event
))
902 static const int searchPos
= 3;
904 wxToolBarBase
* const tb
= GetToolBar();
907 wxSearchCtrl
* const srch
= new wxSearchCtrl(tb
, wxID_ANY
, "needle");
908 srch
->SetMinSize(wxSize(80, -1));
909 m_searchTool
= tb
->InsertControl(searchPos
, srch
);
911 else // tool already exists
913 wxControl
* const win
= m_searchTool
->GetControl();
914 if ( m_searchTool
->GetToolBar() )
916 // attached now, remove it
918 tb
->RemoveTool(m_searchTool
->GetId());
920 else // tool exists in detached state, attach it back
922 tb
->InsertTool(searchPos
, m_searchTool
);
930 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
932 event
.Enable( m_textWindow
->CanCopy() );
935 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
937 wxToolBar
*tbar
= GetToolBar();
938 event
.Enable( tbar
&&
939 tbar
->HasFlag(wxTB_TEXT
) &&
940 !tbar
->HasFlag(wxTB_NOICONS
) );
943 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
945 GetToolBar()->SetToolShortHelp(wxID_NEW
, wxT("New toolbar button"));
948 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
950 long style
= GetToolBar()->GetWindowStyle();
951 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
953 switch ( event
.GetId() )
955 case IDM_TOOLBAR_SHOW_TEXT
:
956 style
|= wxTB_NOICONS
| wxTB_TEXT
;
959 case IDM_TOOLBAR_SHOW_ICONS
:
963 case IDM_TOOLBAR_SHOW_BOTH
:
967 GetToolBar()->SetWindowStyle(style
);
970 void MyFrame::OnToolbarBgCol(wxCommandEvent
& WXUNUSED(event
))
972 wxColour col
= wxGetColourFromUser
975 GetToolBar()->GetBackgroundColour(),
976 "Toolbar background colour"
980 GetToolBar()->SetBackgroundColour(col
);
981 GetToolBar()->Refresh();
985 void MyFrame::OnToolbarCustomBg(wxCommandEvent
& event
)
987 wxToolBarBase
*tb
= GetToolBar();
989 if ( event
.IsChecked() )
993 wxEVT_ERASE_BACKGROUND
,
994 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
1003 wxEVT_ERASE_BACKGROUND
,
1004 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
1013 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
1015 m_pathBmp
= wxLoadFileSelector("custom bitmap", "");
1020 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
1024 wxToolBarBase
*tb
= GetToolBar();
1025 tb
->InsertTool(0, wxID_PRINT
, wxT("New print"),
1026 wxBITMAP(print
), wxNullBitmap
,
1028 wxT("Delete this tool"),
1029 wxT("This button was inserted into the toolbar"));
1031 // must call Realize() after adding a new button
1035 void MyFrame::OnToggleRadioBtn(wxCommandEvent
& event
)
1039 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
1040 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
1044 void MyFrame::OnToolDropdown(wxCommandEvent
& event
)
1047 str
.Printf( wxT("Dropdown on tool %d\n"), event
.GetId());
1048 m_textWindow
->WriteText( str
);