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
= _T("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 _T("wxToolBar Sample"),
308 wxPoint(100, 100), wxSize(550, 300));
313 frame
->SetStatusText(_T("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
;
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 source
= toolBarBitmaps
[n
];
417 wxBitmap
target( w
, h
);
418 wxMemoryDC
dc( target
);
419 dc
.SetPen( wxPen( wxColour(100,100,100), 1, wxSOLID
) );
420 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
421 dc
.DrawRectangle( 0, 0, w
, h
);
422 wxRect
inner(1,1,w
-2,h
-2);
423 dc
.GradientFillLinear( inner
, wxColour(240,240,240), wxColour(150,150,150), wxSOUTH
);
425 dc
.DrawBitmap( source
, w
/4, h
/4, true );
426 toolBarBitmaps
[n
] = target
;
429 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
434 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
436 toolBar
->AddTool(wxID_NEW
, _T("New"),
437 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
438 _T("New file"), _T("This is help for new file tool"));
440 wxMenu
* menu
= new wxMenu
;
441 menu
->Append(wxID_ANY
, _T("&First dummy item"));
442 menu
->Append(wxID_ANY
, _T("&Second dummy item"));
443 menu
->AppendSeparator();
444 menu
->Append(wxID_EXIT
, _T("Exit"));
445 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
447 toolBar
->AddTool(wxID_OPEN
, _T("Open"),
448 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
449 _T("Open file"), _T("This is help for open file tool"));
451 #if USE_CONTROLS_IN_TOOLBAR
452 // adding a combo to a vertical toolbar is not very smart
453 if ( !toolBar
->IsVertical() )
455 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, wxEmptyString
, wxDefaultPosition
, wxSize(100,-1) );
456 combo
->Append(_T("This"));
457 combo
->Append(_T("is a"));
458 combo
->Append(_T("combobox"));
459 combo
->Append(_T("in a"));
460 combo
->Append(_T("toolbar"));
461 toolBar
->AddControl(combo
, _T("Combo Label"));
463 #endif // USE_CONTROLS_IN_TOOLBAR
465 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[Tool_save
], _T("Toggle button 1"), wxITEM_CHECK
);
466 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[Tool_copy
], _T("Toggle button 2"), wxITEM_CHECK
);
467 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[Tool_cut
], _T("Toggle/Untoggle help button"));
468 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[Tool_paste
], _T("Paste"));
470 if ( m_useCustomDisabled
)
472 wxBitmap
bmpDisabled(w
, h
);
475 dc
.SelectObject(bmpDisabled
);
476 dc
.DrawBitmap(toolBarBitmaps
[Tool_print
], 0, 0);
478 wxPen
pen(*wxRED
, 5);
480 dc
.DrawLine(0, 0, w
, h
);
483 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[Tool_print
],
488 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[Tool_print
],
489 _T("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."));
492 toolBar
->AddSeparator();
493 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[Tool_help
], _T("Help button"), wxITEM_CHECK
);
495 if ( !m_pathBmp
.empty() )
497 // create a tool with a custom bitmap for testing
498 wxImage
img(m_pathBmp
);
501 if ( img
.GetWidth() > w
&& img
.GetHeight() > h
)
502 img
= img
.GetSubImage(wxRect(0, 0, w
, h
));
504 toolBar
->AddSeparator();
505 toolBar
->AddTool(wxID_ANY
, _T("Custom"), img
);
509 // after adding the buttons to the toolbar, must call Realize() to reflect
513 toolBar
->SetRows(!(toolBar
->IsVertical()) ? m_rows
: 10 / m_rows
);
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 // Define my frame constructor
521 MyFrame::MyFrame(wxFrame
* parent
,
523 const wxString
& title
,
527 : wxFrame(parent
, id
, title
, pos
, size
, style
)
531 m_smallToolbar
= true;
533 m_useCustomDisabled
= false;
534 m_showTooltips
= true;
541 // Give it a status line
546 SetIcon(wxICON(mondrian
));
549 wxMenu
*tbarMenu
= new wxMenu
;
550 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
551 _T("Toggle &toolbar\tCtrl-Z"),
552 _T("Show or hide the toolbar"));
554 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
555 _T("Toggle &another toolbar\tCtrl-A"),
556 _T("Show/hide another test toolbar"));
558 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
559 _T("Toggle hori&zontal text\tCtrl-H"),
560 _T("Show text under/alongside the icon"));
562 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
563 _T("&Toggle toolbar size\tCtrl-S"),
564 _T("Toggle between big/small toolbar"));
566 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
567 _T("Toggle number of &rows\tCtrl-R"),
568 _T("Toggle number of toolbar rows between 1 and 2"));
570 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
571 _T("Show &tooltips\tCtrl-L"),
572 _T("Show tooltips for the toolbar tools"));
574 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
575 _T("Use c&ustom disabled images\tCtrl-U"),
576 _T("Switch between using system-generated and custom disabled images"));
579 tbarMenu
->AppendSeparator();
580 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
581 _T("Set toolbar at the top of the window"),
582 _T("Set toolbar at the top of the window"));
583 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
584 _T("Set toolbar at the left of the window"),
585 _T("Set toolbar at the left of the window"));
586 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
587 _T("Set toolbar at the bottom of the window"),
588 _T("Set toolbar at the bottom of the window"));
589 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
590 _T("Set toolbar at the right edge of the window"),
591 _T("Set toolbar at the right edge of the window"));
592 tbarMenu
->AppendSeparator();
594 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, _T("Show &text\tCtrl-Alt-T"));
595 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, _T("Show &icons\tCtrl-Alt-I"));
596 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, _T("Show &both\tCtrl-Alt-B"));
597 tbarMenu
->AppendSeparator();
598 tbarMenu
->Append(IDM_TOOLBAR_BG_COL
, _T("Choose bac&kground colour..."));
599 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, _T("Custom &bitmap...\tCtrl-B"));
601 wxMenu
*toolMenu
= new wxMenu
;
602 toolMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, _T("&Enable print button\tCtrl-E"));
603 toolMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, _T("&Delete print button\tCtrl-D"));
604 toolMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, _T("&Insert print button\tCtrl-I"));
605 toolMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, _T("Toggle &help button\tCtrl-T"));
606 toolMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH
, _T("Toggle &search field\tCtrl-F"));
607 toolMenu
->AppendSeparator();
608 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, _T("Toggle &1st radio button\tCtrl-1"));
609 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, _T("Toggle &2nd radio button\tCtrl-2"));
610 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, _T("Toggle &3rd radio button\tCtrl-3"));
611 toolMenu
->AppendSeparator();
612 toolMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, _T("Change tooltip of \"New\""));
614 wxMenu
*fileMenu
= new wxMenu
;
615 fileMenu
->Append(wxID_EXIT
, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
617 wxMenu
*helpMenu
= new wxMenu
;
618 helpMenu
->Append(wxID_HELP
, _T("&About"), _T("About toolbar sample"));
620 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
622 menuBar
->Append(fileMenu
, _T("&File"));
623 menuBar
->Append(tbarMenu
, _T("&Toolbar"));
624 menuBar
->Append(toolMenu
, _T("Tool&s"));
625 menuBar
->Append(helpMenu
, _T("&Help"));
627 // Associate the menu bar with the frame
630 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, true);
631 menuBar
->Check(IDM_TOOLBAR_TOGGLETOOLTIPS
, true);
633 menuBar
->Check(IDM_TOOLBAR_TOP_ORIENTATION
, true );
634 m_toolbarPosition
= TOOLBAR_TOP
;
636 // Create the toolbar
639 m_panel
= new wxPanel(this, wxID_ANY
);
640 #if USE_UNMANAGED_TOOLBAR
641 m_extraToolBar
= new wxToolBar(m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_TEXT
|wxTB_FLAT
|wxTB_TOP
);
642 PopulateToolbar(m_extraToolBar
);
645 m_textWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
647 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
648 m_panel
->SetSizer(sizer
);
649 #if USE_UNMANAGED_TOOLBAR
651 sizer
->Add(m_extraToolBar
, 0, wxEXPAND
, 0);
653 sizer
->Add(m_textWindow
, 1, wxEXPAND
, 0);
658 if ( m_searchTool
&& !m_searchTool
->GetToolBar() )
660 // we currently can't delete a toolbar tool ourselves, so we have to
661 // attach it to the toolbar just for it to be deleted, this is pretty
662 // ugly and will need to be changed
663 GetToolBar()->AddTool(m_searchTool
);
667 void MyFrame::LayoutChildren()
669 wxSize size
= GetClientSize();
674 m_tbar
->SetSize(0, 0, wxDefaultCoord
, size
.y
);
676 offset
= m_tbar
->GetSize().x
;
683 m_panel
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
686 void MyFrame::OnSize(wxSizeEvent
& event
)
698 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
700 wxToolBar
*tbar
= GetToolBar();
714 void MyFrame::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
716 m_horzText
= !m_horzText
;
721 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
730 long style
= GetToolBar() ? GetToolBar()->GetWindowStyle()
732 style
&= ~wxTB_HORIZONTAL
;
733 style
|= wxTB_VERTICAL
;
735 m_tbar
= new wxToolBar(this, wxID_ANY
,
736 wxDefaultPosition
, wxDefaultSize
,
739 m_tbar
->SetMargins(4, 4);
741 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_1
, _T("First"), wxBITMAP(new));
742 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, _T("Second"), wxBITMAP(open
));
743 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, _T("Third"), wxBITMAP(save
));
744 m_tbar
->AddSeparator();
745 m_tbar
->AddTool(wxID_HELP
, _T("Help"), wxBITMAP(help
));
753 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
755 m_smallToolbar
= !m_smallToolbar
;
760 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
762 // m_rows may be only 1 or 2
765 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows
: 10 / m_rows
);
767 //RecreateToolbar(); -- this is unneeded
770 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
772 m_showTooltips
= !m_showTooltips
;
777 void MyFrame::OnToggleCustomDisabled(wxCommandEvent
& WXUNUSED(event
))
779 m_useCustomDisabled
= !m_useCustomDisabled
;
784 void MyFrame::OnChangeOrientation(wxCommandEvent
& event
)
786 switch( event
.GetId() )
788 case IDM_TOOLBAR_LEFT_ORIENTATION
:
789 m_toolbarPosition
= TOOLBAR_LEFT
;
791 case IDM_TOOLBAR_TOP_ORIENTATION
:
792 m_toolbarPosition
= TOOLBAR_TOP
;
794 case IDM_TOOLBAR_RIGHT_ORIENTATION
:
795 m_toolbarPosition
= TOOLBAR_RIGHT
;
797 case IDM_TOOLBAR_BOTTOM_ORIENTATION
:
798 m_toolbarPosition
= TOOLBAR_BOTTOM
;
804 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
809 void MyFrame::OnAbout(wxCommandEvent
& event
)
811 if ( event
.IsChecked() )
812 m_textWindow
->WriteText( _T("Help button down now.\n") );
814 m_textWindow
->WriteText( _T("Help button up now.\n") );
816 (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar"));
819 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
822 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
823 m_textWindow
->WriteText( str
);
825 if (event
.GetId() == wxID_COPY
)
830 if (event
.GetId() == wxID_CUT
)
835 if (event
.GetId() == wxID_PRINT
)
841 void MyFrame::OnToolRightClick(wxCommandEvent
& event
)
843 m_textWindow
->AppendText(
844 wxString::Format(_T("Tool %d right clicked.\n"),
845 (int) event
.GetInt()));
848 void MyFrame::OnCombo(wxCommandEvent
& event
)
850 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
853 void MyFrame::DoEnablePrint()
858 wxToolBarBase
*tb
= GetToolBar();
859 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
862 void MyFrame::DoDeletePrint()
867 wxToolBarBase
*tb
= GetToolBar();
868 tb
->DeleteTool( wxID_PRINT
);
873 void MyFrame::DoToggleHelp()
875 wxToolBarBase
*tb
= GetToolBar();
876 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
879 void MyFrame::OnToggleSearch(wxCommandEvent
& WXUNUSED(event
))
881 static const int searchPos
= 3;
883 wxToolBarBase
* const tb
= GetToolBar();
886 wxSearchCtrl
* const srch
= new wxSearchCtrl(tb
, wxID_ANY
, "needle");
887 srch
->SetMinSize(wxSize(80, -1));
888 m_searchTool
= tb
->InsertControl(searchPos
, srch
);
890 else // tool already exists
892 wxControl
* const win
= m_searchTool
->GetControl();
893 if ( m_searchTool
->GetToolBar() )
895 // attached now, remove it
897 tb
->RemoveTool(m_searchTool
->GetId());
899 else // tool exists in detached state, attach it back
901 tb
->InsertTool(searchPos
, m_searchTool
);
909 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
911 event
.Enable( m_textWindow
->CanCopy() );
914 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
916 wxToolBar
*tbar
= GetToolBar();
917 event
.Enable( tbar
&&
918 tbar
->HasFlag(wxTB_TEXT
) &&
919 !tbar
->HasFlag(wxTB_NOICONS
) );
922 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
924 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
927 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
929 long style
= GetToolBar()->GetWindowStyle();
930 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
932 switch ( event
.GetId() )
934 case IDM_TOOLBAR_SHOW_TEXT
:
935 style
|= wxTB_NOICONS
| wxTB_TEXT
;
938 case IDM_TOOLBAR_SHOW_ICONS
:
942 case IDM_TOOLBAR_SHOW_BOTH
:
946 GetToolBar()->SetWindowStyle(style
);
949 void MyFrame::OnToolbarBgCol(wxCommandEvent
& WXUNUSED(event
))
951 wxColour col
= wxGetColourFromUser
954 GetToolBar()->GetBackgroundColour(),
955 "Toolbar background colour"
959 GetToolBar()->SetBackgroundColour(col
);
960 GetToolBar()->Refresh();
964 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
966 m_pathBmp
= wxFileSelector(_T("Custom bitmap path"));
971 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
975 wxToolBarBase
*tb
= GetToolBar();
976 tb
->InsertTool(0, wxID_PRINT
, _T("New print"),
977 wxBITMAP(print
), wxNullBitmap
,
979 _T("Delete this tool"),
980 _T("This button was inserted into the toolbar"));
982 // must call Realize() after adding a new button
986 void MyFrame::OnToggleRadioBtn(wxCommandEvent
& event
)
990 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
991 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
995 void MyFrame::OnToolDropdown(wxCommandEvent
& event
)
998 str
.Printf( _T("Dropdown on tool %d\n"), event
.GetId());
999 m_textWindow
->WriteText( str
);