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/spinctrl.h>
36 // define this to 1 to use wxToolBarSimple instead of the native one
37 #define USE_GENERIC_TBAR 0
39 // define this to use XPMs everywhere (by default, BMPs are used under Win)
41 #define USE_XPM_BITMAPS 0
43 #define USE_XPM_BITMAPS 1
47 #if !wxUSE_TOOLBAR_SIMPLE
48 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
49 to 1 in setup.h and recompile the library.
51 #include <wx/tbarsmpl.h>
53 #endif // USE_GENERIC_TBAR
55 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
56 #error You need to enable XPM support to use XPM bitmaps with toolbar!
57 #endif // USE_XPM_BITMAPS
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 #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
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application
80 class MyApp
: public wxApp
87 class MyFrame
: public wxFrame
90 MyFrame(wxFrame
*parent
,
92 const wxString
& title
= "wxToolBar Sample",
93 const wxPoint
& pos
= wxDefaultPosition
,
94 const wxSize
& size
= wxDefaultSize
,
95 long style
= wxDEFAULT_FRAME_STYLE
);
97 void RecreateToolbar();
99 void OnQuit(wxCommandEvent
& event
);
100 void OnAbout(wxCommandEvent
& event
);
102 void OnSize(wxSizeEvent
& event
);
104 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
106 void OnToggleToolbarSize(wxCommandEvent
& event
);
107 void OnToggleToolbarOrient(wxCommandEvent
& event
);
108 void OnToggleToolbarRows(wxCommandEvent
& event
);
110 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
111 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
112 void OnInsertPrint(wxCommandEvent
& event
);
113 void OnChangeToolTip(wxCommandEvent
& event
);
114 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
116 void OnToolLeftClick(wxCommandEvent
& event
);
117 void OnToolEnter(wxCommandEvent
& event
);
119 void OnCombo(wxCommandEvent
& event
);
121 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
124 virtual wxToolBar
*OnCreateToolBar(long style
,
126 const wxString
& name
);
127 #endif // USE_GENERIC_TBAR
130 void DoEnablePrint();
131 void DoDeletePrint();
134 void LayoutChildren();
138 size_t m_rows
; // 1 or 2 only
140 wxTextCtrl
*m_textWindow
;
144 DECLARE_EVENT_TABLE()
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 const int ID_TOOLBAR
= 500;
153 static const long TOOLBAR_STYLE
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
157 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
158 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
159 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
160 IDM_TOOLBAR_ENABLEPRINT
,
161 IDM_TOOLBAR_DELETEPRINT
,
162 IDM_TOOLBAR_INSERTPRINT
,
163 IDM_TOOLBAR_TOGGLEHELP
,
164 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
165 IDM_TOOLBAR_CHANGE_TOOLTIP
,
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
177 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
178 EVT_SIZE(MyFrame::OnSize
)
180 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
181 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
183 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
185 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
186 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
187 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
189 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
190 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
191 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
192 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
193 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
195 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
197 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
199 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
201 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
202 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
205 // ============================================================================
207 // ============================================================================
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
215 // The `main program' equivalent, creating the windows and returning the
219 // Create the main frame window
220 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
222 wxPoint(100, 100), wxSize(450, 300));
226 frame
->SetStatusText("Hello, wxWindows");
233 void MyFrame::RecreateToolbar()
235 // delete and recreate the toolbar
236 wxToolBarBase
*toolBar
= GetToolBar();
241 long style
= TOOLBAR_STYLE
;
242 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
244 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
245 //toolBar->SetMargins( 4, 4 );
248 wxBitmap toolBarBitmaps
[8];
251 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
252 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
253 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
254 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
255 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
256 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
257 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
258 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
259 #else // !USE_XPM_BITMAPS
260 toolBarBitmaps
[0] = wxBITMAP(new);
261 toolBarBitmaps
[1] = wxBITMAP(open
);
262 toolBarBitmaps
[2] = wxBITMAP(save
);
263 toolBarBitmaps
[3] = wxBITMAP(copy
);
264 toolBarBitmaps
[4] = wxBITMAP(cut
);
265 toolBarBitmaps
[5] = wxBITMAP(paste
);
266 toolBarBitmaps
[6] = wxBITMAP(print
);
267 toolBarBitmaps
[7] = wxBITMAP(help
);
268 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
270 if ( !m_smallToolbar
)
272 int w
= 2*toolBarBitmaps
[0].GetWidth(),
273 h
= 2*toolBarBitmaps
[0].GetHeight();
274 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
277 wxImage(toolBarBitmaps
[n
]).Scale(w
, h
).ConvertToBitmap();
280 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
291 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
292 currentX
+= width
+ 5;
293 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
295 // neither the generic nor Motif native toolbars really support this
296 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
297 // adding a combo to a vertical toolbar is not very smart
300 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
301 combo
->Append("This");
302 combo
->Append("is a");
303 combo
->Append("combobox");
304 combo
->Append("in a");
305 combo
->Append("toolbar");
307 wxTextCtrl *combo = new wxTextCtrl( toolBar, -1, "", wxDefaultPosition, wxSize(80,-1) );
309 toolBar
->AddControl(combo
);
311 #endif // toolbars which don't support controls
313 currentX
+= width
+ 5;
314 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
315 currentX
+= width
+ 5;
316 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
317 currentX
+= width
+ 5;
318 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
319 currentX
+= width
+ 5;
320 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
321 currentX
+= width
+ 5;
322 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
323 currentX
+= width
+ 5;
324 toolBar
->AddSeparator();
325 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
327 // after adding the buttons to the toolbar, must call Realize() to reflect
331 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 // Define my frame constructor
339 MyFrame::MyFrame(wxFrame
* parent
,
341 const wxString
& title
,
345 : wxFrame(parent
, id
, title
, pos
, size
, style
)
348 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
350 m_smallToolbar
= TRUE
;
351 m_horzToolbar
= TRUE
;
354 // Give it a status line
358 SetIcon(wxICON(mondrian
));
361 wxMenu
*tbarMenu
= new wxMenu
;
362 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
363 "Toggle &another toolbar\tCtrl-A",
364 "Show/hide another test toolbar",
367 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
368 "&Toggle toolbar size\tCtrl-S",
369 "Toggle between big/small toolbar",
371 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
372 "Toggle toolbar &orientation\tCtrl-O",
373 "Toggle toolbar orientation",
375 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
376 "Toggle number of &rows\tCtrl-R",
377 "Toggle number of toolbar rows between 1 and 2",
380 tbarMenu
->AppendSeparator();
382 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
383 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
384 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
385 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
386 tbarMenu
->AppendSeparator();
387 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
389 wxMenu
*fileMenu
= new wxMenu
;
390 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
392 wxMenu
*helpMenu
= new wxMenu
;
393 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
395 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
397 menuBar
->Append(fileMenu
, "&File");
398 menuBar
->Append(tbarMenu
, "&Toolbar");
399 menuBar
->Append(helpMenu
, "&Help");
401 // Associate the menu bar with the frame
404 // Create the toolbar
410 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
412 const wxString
& name
)
414 return (wxToolBar
*)new wxToolBarSimple(this, id
,
415 wxDefaultPosition
, wxDefaultSize
,
419 #endif // USE_GENERIC_TBAR
421 void MyFrame::LayoutChildren()
423 wxSize size
= GetClientSize();
428 m_tbar
->SetSize(-1, size
.y
);
431 offset
= m_tbar
->GetSize().x
;
438 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
441 void MyFrame::OnSize(wxSizeEvent
& event
)
453 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
462 m_tbar
= new wxToolBar(this, -1,
463 wxDefaultPosition
, wxDefaultSize
,
464 TOOLBAR_STYLE
| wxTB_VERTICAL
);
465 m_tbar
->AddTool(wxID_HELP
, wxBITMAP(help
),
468 "This is the help button",
469 "This is the long help for the help button");
476 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
478 m_smallToolbar
= !m_smallToolbar
;
483 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
485 // m_rows may be only 1 or 2
488 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
490 //RecreateToolbar(); -- this is unneeded
493 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
495 m_horzToolbar
= !m_horzToolbar
;
500 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
505 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
507 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
510 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
513 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
514 m_textWindow
->WriteText( str
);
516 if (event
.GetId() == wxID_HELP
)
518 if ( event
.GetExtraLong() != 0 )
519 m_textWindow
->WriteText( _T("Help button down now.\n") );
521 m_textWindow
->WriteText( _T("Help button up now.\n") );
524 if (event
.GetId() == wxID_COPY
)
529 if (event
.GetId() == wxID_CUT
)
534 if (event
.GetId() == wxID_PRINT
)
540 void MyFrame::OnCombo(wxCommandEvent
& event
)
542 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
545 void MyFrame::DoEnablePrint()
547 wxToolBarBase
*tb
= GetToolBar();
548 if (tb
->GetToolEnabled(wxID_PRINT
))
549 tb
->EnableTool( wxID_PRINT
, FALSE
);
551 tb
->EnableTool( wxID_PRINT
, TRUE
);
554 void MyFrame::DoDeletePrint()
556 wxToolBarBase
*tb
= GetToolBar();
558 tb
->DeleteTool( wxID_PRINT
);
561 void MyFrame::DoToggleHelp()
563 wxToolBarBase
*tb
= GetToolBar();
564 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
567 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
569 event
.Enable( m_textWindow
->CanCopy() );
572 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
574 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
577 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
579 wxBitmap bmp
= wxBITMAP(print
);
581 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
582 FALSE
, (wxObject
*) NULL
,
584 "This button was inserted into the toolbar");
586 GetToolBar()->Realize();
589 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
591 if (event
.GetSelection() > -1)
594 str
.Printf(_T("This is tool number %d"), event
.GetSelection());