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/spinctrl.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 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 #if !defined(__WXMSW__) && !defined(__WXPM__)
61 #include "mondrian.xpm"
65 #include "bitmaps/new.xpm"
66 #include "bitmaps/open.xpm"
67 #include "bitmaps/save.xpm"
68 #include "bitmaps/copy.xpm"
69 #include "bitmaps/cut.xpm"
70 #include "bitmaps/preview.xpm" // paste XPM
71 #include "bitmaps/print.xpm"
72 #include "bitmaps/help.xpm"
73 #endif // USE_XPM_BITMAPS
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class MyMiniControl
: public wxControl
90 MyMiniControl( wxWindow
*parent
) :
91 wxControl( parent
, -1, wxDefaultPosition
, wxSize(70,22), wxBORDER_SUNKEN
, wxDefaultValidator
, "MyMiniControl" )
94 void OnPaint(wxPaintEvent
&WXUNUSED(event
))
97 dc
.SetPen( *wxWHITE_PEN
);
98 dc
.SetBrush( *wxGREEN_BRUSH
);
99 wxSize size
= GetClientSize();
100 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
102 virtual wxSize
GetBestSize()
104 return wxSize(70,22);
108 DECLARE_EVENT_TABLE()
111 BEGIN_EVENT_TABLE(MyMiniControl
, wxControl
)
112 EVT_PAINT(MyMiniControl
::OnPaint
)
117 // Define a new application
118 class MyApp
: public wxApp
124 // Define a new frame
125 class MyFrame
: public wxFrame
128 MyFrame(wxFrame
*parent
,
129 wxWindowID id
= wxID_ANY
,
130 const wxString
& title
= _T("wxToolBar Sample"),
131 const wxPoint
& pos
= wxDefaultPosition
,
132 const wxSize
& size
= wxDefaultSize
,
133 long style
= wxDEFAULT_FRAME_STYLE
|wxCLIP_CHILDREN
|wxNO_FULL_REPAINT_ON_RESIZE
);
135 void PopulateToolbar(wxToolBarBase
* toolBar
);
136 void RecreateToolbar();
138 void OnQuit(wxCommandEvent
& event
);
139 void OnAbout(wxCommandEvent
& event
);
141 void OnSize(wxSizeEvent
& event
);
143 void OnToggleToolbar(wxCommandEvent
& event
);
144 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
145 void OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
));
147 void OnToggleToolbarSize(wxCommandEvent
& event
);
148 void OnChangeOrientation(wxCommandEvent
& event
);
149 void OnToggleToolbarRows(wxCommandEvent
& event
);
150 void OnToggleTooltips(wxCommandEvent
& event
);
151 void OnToggleCustomDisabled(wxCommandEvent
& event
);
153 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
154 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
155 void OnInsertPrint(wxCommandEvent
& event
);
156 void OnChangeToolTip(wxCommandEvent
& event
);
157 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
158 void OnToggleRadioBtn(wxCommandEvent
& event
);
160 void OnToolbarStyle(wxCommandEvent
& event
);
161 void OnToolbarCustomBitmap(wxCommandEvent
& event
);
163 void OnToolLeftClick(wxCommandEvent
& event
);
164 void OnToolRightClick(wxCommandEvent
& event
);
165 void OnToolDropdown(wxCommandEvent
& event
);
167 void OnCombo(wxCommandEvent
& event
);
169 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
170 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
171 void OnUpdateToggleRadioBtn(wxUpdateUIEvent
& event
)
172 { event
.Enable( m_tbar
!= NULL
); }
175 void DoEnablePrint();
176 void DoDeletePrint();
179 void LayoutChildren();
185 size_t m_rows
; // 1 or 2 only
187 // the number of print buttons we have (they're added/removed dynamically)
190 // store toolbar position for future use
191 Positions m_toolbarPosition
;
193 wxTextCtrl
*m_textWindow
;
196 wxToolBar
*m_extraToolBar
;
200 // the path to the custom bitmap for the test toolbar tool
203 DECLARE_EVENT_TABLE()
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 const int ID_TOOLBAR
= 500;
212 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
216 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
217 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
218 IDM_TOOLBAR_TOGGLETOOLTIPS
,
219 IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
220 IDM_TOOLBAR_ENABLEPRINT
,
221 IDM_TOOLBAR_DELETEPRINT
,
222 IDM_TOOLBAR_INSERTPRINT
,
223 IDM_TOOLBAR_TOGGLEHELP
,
224 IDM_TOOLBAR_TOGGLERADIOBTN1
,
225 IDM_TOOLBAR_TOGGLERADIOBTN2
,
226 IDM_TOOLBAR_TOGGLERADIOBTN3
,
227 IDM_TOOLBAR_TOGGLE_TOOLBAR
,
228 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
229 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
230 IDM_TOOLBAR_CHANGE_TOOLTIP
,
231 IDM_TOOLBAR_SHOW_TEXT
,
232 IDM_TOOLBAR_SHOW_ICONS
,
233 IDM_TOOLBAR_SHOW_BOTH
,
234 IDM_TOOLBAR_CUSTOM_PATH
,
235 IDM_TOOLBAR_TOP_ORIENTATION
,
236 IDM_TOOLBAR_LEFT_ORIENTATION
,
237 IDM_TOOLBAR_BOTTOM_ORIENTATION
,
238 IDM_TOOLBAR_RIGHT_ORIENTATION
,
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
254 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
255 EVT_SIZE(MyFrame
::OnSize
)
257 EVT_MENU(wxID_EXIT
, MyFrame
::OnQuit
)
258 EVT_MENU(wxID_HELP
, MyFrame
::OnAbout
)
260 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame
::OnToggleToolbar
)
261 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame
::OnToggleAnotherToolbar
)
262 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame
::OnToggleHorizontalText
)
264 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION
, IDM_TOOLBAR_RIGHT_ORIENTATION
, MyFrame
::OnChangeOrientation
)
265 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame
::OnToggleToolbarSize
)
266 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame
::OnToggleToolbarRows
)
267 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS
, MyFrame
::OnToggleTooltips
)
268 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
, MyFrame
::OnToggleCustomDisabled
)
270 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame
::OnEnablePrint
)
271 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame
::OnDeletePrint
)
272 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame
::OnInsertPrint
)
273 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame
::OnToggleHelp
)
274 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
, IDM_TOOLBAR_TOGGLERADIOBTN3
,
275 MyFrame
::OnToggleRadioBtn
)
276 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame
::OnChangeToolTip
)
278 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
279 MyFrame
::OnToolbarStyle
)
281 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH
, MyFrame
::OnToolbarCustomBitmap
)
283 EVT_MENU(wxID_ANY
, MyFrame
::OnToolLeftClick
)
285 EVT_COMBOBOX(ID_COMBO
, MyFrame
::OnCombo
)
287 EVT_TOOL_RCLICKED(wxID_ANY
, MyFrame
::OnToolRightClick
)
289 EVT_TOOL_DROPDOWN(wxID_ANY
, MyFrame
::OnToolDropdown
)
291 EVT_UPDATE_UI(wxID_COPY
, MyFrame
::OnUpdateCopyAndCut
)
292 EVT_UPDATE_UI(wxID_CUT
, MyFrame
::OnUpdateCopyAndCut
)
294 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
,
295 IDM_TOOLBAR_TOGGLERADIOBTN3
,
296 MyFrame
::OnUpdateToggleRadioBtn
)
297 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
298 MyFrame
::OnUpdateToggleHorzText
)
301 // ============================================================================
303 // ============================================================================
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
311 // The `main program' equivalent, creating the windows and returning the
315 if ( !wxApp
::OnInit() )
318 // Create the main frame window
319 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxID_ANY
,
320 _T("wxToolBar Sample"),
321 wxPoint(100, 100), wxSize(550, 300));
326 frame
->SetStatusText(_T("Hello, wxWidgets"));
329 wxInitAllImageHandlers();
336 void MyFrame
::RecreateToolbar()
339 // On Windows CE, we should not delete the
340 // previous toolbar in case it contains the menubar.
341 // We'll try to accommodate this usage in due course.
342 wxToolBar
* toolBar
= CreateToolBar();
344 // delete and recreate the toolbar
345 wxToolBarBase
*toolBar
= GetToolBar();
346 long style
= toolBar ? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
352 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_BOTTOM
| wxTB_RIGHT
| wxTB_HORZ_LAYOUT
);
353 switch( m_toolbarPosition
)
365 style
|= wxTB_BOTTOM
;
369 if ( m_showTooltips
)
370 style
&= ~wxTB_NO_TOOLTIPS
;
372 style
|= wxTB_NO_TOOLTIPS
;
374 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
375 style
|= wxTB_HORZ_LAYOUT
;
377 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
380 PopulateToolbar(toolBar
);
383 void MyFrame
::PopulateToolbar(wxToolBarBase
* toolBar
)
399 wxBitmap toolBarBitmaps
[Tool_Max
];
402 #define INIT_TOOL_BMP(bmp) \
403 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
404 #else // !USE_XPM_BITMAPS
405 #define INIT_TOOL_BMP(bmp) \
406 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
407 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
414 INIT_TOOL_BMP(paste
);
415 INIT_TOOL_BMP(print
);
418 int w
= toolBarBitmaps
[Tool_new
].GetWidth(),
419 h
= toolBarBitmaps
[Tool_new
].GetHeight();
421 if ( !m_smallToolbar
)
426 for ( size_t n
= Tool_new
; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
429 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
433 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
435 toolBar
->AddTool(wxID_NEW
, _T("New"),
436 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
437 _T("New file"), _T("This is help for new file tool"));
439 wxMenu
* menu
= new wxMenu
;
440 menu
->Append(wxID_ANY
, _T("&First dummy item"));
441 menu
->Append(wxID_ANY
, _T("&Second dummy item"));
442 menu
->AppendSeparator();
443 menu
->Append(wxID_EXIT
, _T("Exit"));
444 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
446 toolBar
->AddTool(wxID_OPEN
, _T("Open"),
447 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
448 _T("Open file"), _T("This is help for open file tool"));
450 // the generic toolbar doesn't really support this
451 #if wxUSE_TOOLBAR_NATIVE && !defined(__WXX11__) || defined(__WXUNIVERSAL__)
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 wxSpinCtrl
*spin
= new wxSpinCtrl( toolBar
, ID_SPIN
, wxT("0"), wxDefaultPosition
, wxSize(80,wxDefaultCoord
), 0, 0, 100 );
464 toolBar
->AddControl( spin
);
466 wxTextCtrl
*text
= new wxTextCtrl( toolBar
, -1, wxT("text"), wxDefaultPosition
, wxSize(80,wxDefaultCoord
) );
467 toolBar
->AddControl( text
);
469 wxSearchCtrl
*srch
= new wxSearchCtrl( toolBar
, -1, wxT("xx"), wxDefaultPosition
, wxSize(80,wxDefaultCoord
), wxSUNKEN_BORDER
);
470 toolBar
->AddControl( srch
);
472 toolBar
->AddControl( new MyMiniControl( toolBar
) );
474 #endif // toolbars which don't support controls
476 toolBar
->AddTool(wxID_SAVE
, _T("Save"), toolBarBitmaps
[Tool_save
], _T("Toggle button 1"), wxITEM_CHECK
);
477 toolBar
->AddTool(wxID_COPY
, _T("Copy"), toolBarBitmaps
[Tool_copy
], _T("Toggle button 2"), wxITEM_CHECK
);
478 toolBar
->AddTool(wxID_CUT
, _T("Cut"), toolBarBitmaps
[Tool_cut
], _T("Toggle/Untoggle help button"));
479 toolBar
->AddTool(wxID_PASTE
, _T("Paste"), toolBarBitmaps
[Tool_paste
], _T("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
, _T("Print"), toolBarBitmaps
[Tool_print
],
499 toolBar
->AddTool(wxID_PRINT
, _T("Print"), toolBarBitmaps
[Tool_print
],
500 _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."));
503 toolBar
->AddSeparator();
504 toolBar
->AddTool(wxID_HELP
, _T("Help"), toolBarBitmaps
[Tool_help
], _T("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
, _T("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;
551 // Give it a status line
556 SetIcon(wxICON(mondrian
));
559 wxMenu
*tbarMenu
= new wxMenu
;
560 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
561 _T("Toggle &toolbar\tCtrl-Z"),
562 _T("Show or hide the toolbar"));
564 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
565 _T("Toggle &another toolbar\tCtrl-A"),
566 _T("Show/hide another test toolbar"));
568 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
569 _T("Toggle hori&zontal text\tCtrl-H"),
570 _T("Show text under/alongside the icon"));
572 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
573 _T("&Toggle toolbar size\tCtrl-S"),
574 _T("Toggle between big/small toolbar"));
576 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
577 _T("Toggle number of &rows\tCtrl-R"),
578 _T("Toggle number of toolbar rows between 1 and 2"));
580 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
581 _T("Show &tooltips\tCtrl-L"),
582 _T("Show tooltips for the toolbar tools"));
584 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
585 _T("Use c&ustom disabled images\tCtrl-U"),
586 _T("Switch between using system-generated and custom disabled images"));
589 tbarMenu
->AppendSeparator();
590 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
591 _T("Set toolbar at the top of the window"),
592 _T("Set toolbar at the top of the window"));
593 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
594 _T("Set toolbar at the left of the window"),
595 _T("Set toolbar at the left of the window"));
596 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
597 _T("Set toolbar at the bottom of the window"),
598 _T("Set toolbar at the bottom of the window"));
599 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
600 _T("Set toolbar at the right edge of the window"),
601 _T("Set toolbar at the right edge of the window"));
602 tbarMenu
->AppendSeparator();
604 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, _T("&Enable print button\tCtrl-E"));
605 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, _T("&Delete print button\tCtrl-D"));
606 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, _T("&Insert print button\tCtrl-I"));
607 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, _T("Toggle &help button\tCtrl-T"));
608 tbarMenu
->AppendSeparator();
609 tbarMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, _T("Toggle &1st radio button\tCtrl-1"));
610 tbarMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, _T("Toggle &2nd radio button\tCtrl-2"));
611 tbarMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, _T("Toggle &3rd radio button\tCtrl-3"));
612 tbarMenu
->AppendSeparator();
613 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, _T("Change tool tip"));
614 tbarMenu
->AppendSeparator();
615 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, _T("Show &text\tCtrl-Alt-T"));
616 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, _T("Show &icons\tCtrl-Alt-I"));
617 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, _T("Show &both\tCtrl-Alt-B"));
618 tbarMenu
->AppendSeparator();
619 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, _T("Custom &bitmap...\tCtrl-B"));
621 wxMenu
*fileMenu
= new wxMenu
;
622 fileMenu
->Append(wxID_EXIT
, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
624 wxMenu
*helpMenu
= new wxMenu
;
625 helpMenu
->Append(wxID_HELP
, _T("&About"), _T("About toolbar sample"));
627 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
629 menuBar
->Append(fileMenu
, _T("&File"));
630 menuBar
->Append(tbarMenu
, _T("&Toolbar"));
631 menuBar
->Append(helpMenu
, _T("&Help"));
633 // Associate the menu bar with the frame
636 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, true);
637 menuBar
->Check(IDM_TOOLBAR_TOGGLETOOLTIPS
, true);
639 menuBar
->Check(IDM_TOOLBAR_TOP_ORIENTATION
, true );
640 m_toolbarPosition
= TOOLBAR_TOP
;
642 // Create the toolbar
645 m_panel
= new wxPanel(this, wxID_ANY
);
646 #if USE_UNMANAGED_TOOLBAR
647 m_extraToolBar
= new wxToolBar(m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_TEXT
|wxTB_FLAT
|wxTB_TOP
);
648 PopulateToolbar(m_extraToolBar
);
650 m_extraToolBar
= NULL
;
653 m_textWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
655 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
656 m_panel
->SetSizer(sizer
);
658 sizer
->Add(m_extraToolBar
, 0, wxEXPAND
, 0);
659 sizer
->Add(m_textWindow
, 1, wxEXPAND
, 0);
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
, _T("First"), wxBITMAP(new));
737 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, _T("Second"), wxBITMAP(open
));
738 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, _T("Third"), wxBITMAP(save
));
739 m_tbar
->AddSeparator();
740 m_tbar
->AddTool(wxID_HELP
, _T("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( _T("Help button down now.\n") );
809 m_textWindow
->WriteText( _T("Help button up now.\n") );
811 (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar"));
814 void MyFrame
::OnToolLeftClick(wxCommandEvent
& event
)
817 str
.Printf( _T("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(_T("Tool %d right clicked.\n"),
840 (int) event
.GetInt()));
843 void MyFrame
::OnCombo(wxCommandEvent
& event
)
845 wxLogStatus(_T("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
::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
876 event
.Enable( m_textWindow
->CanCopy() );
879 void MyFrame
::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
881 wxToolBar
*tbar
= GetToolBar();
882 event
.Enable( tbar
&&
883 tbar
->HasFlag(wxTB_TEXT
) &&
884 !tbar
->HasFlag(wxTB_NOICONS
) );
887 void MyFrame
::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
889 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
892 void MyFrame
::OnToolbarStyle(wxCommandEvent
& event
)
894 long style
= GetToolBar()->GetWindowStyle();
895 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
897 switch ( event
.GetId() )
899 case IDM_TOOLBAR_SHOW_TEXT
:
900 style
|= wxTB_NOICONS
| wxTB_TEXT
;
903 case IDM_TOOLBAR_SHOW_ICONS
:
907 case IDM_TOOLBAR_SHOW_BOTH
:
911 GetToolBar()->SetWindowStyle(style
);
914 void MyFrame
::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
916 m_pathBmp
= wxFileSelector(_T("Custom bitmap path"));
921 void MyFrame
::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
925 wxToolBarBase
*tb
= GetToolBar();
926 tb
->InsertTool(0, wxID_PRINT
, _T("New print"),
927 wxBITMAP(print
), wxNullBitmap
,
929 _T("Delete this tool"),
930 _T("This button was inserted into the toolbar"));
932 // must call Realize() after adding a new button
936 void MyFrame
::OnToggleRadioBtn(wxCommandEvent
& event
)
940 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
941 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
945 void MyFrame
::OnToolDropdown(wxCommandEvent
& event
)
948 str
.Printf( _T("Dropdown on tool %d\n"), event
.GetId());
949 m_textWindow
->WriteText( str
);