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
);
123 void OnToggleFullScreen(wxCommandEvent
& event
);
126 virtual wxToolBar
*OnCreateToolBar(long style
,
128 const wxString
& name
);
129 #endif // USE_GENERIC_TBAR
132 void DoEnablePrint();
133 void DoDeletePrint();
136 void LayoutChildren();
140 size_t m_rows
; // 1 or 2 only
142 wxTextCtrl
*m_textWindow
;
146 DECLARE_EVENT_TABLE()
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 const int ID_TOOLBAR
= 500;
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_TOGGLEFULLSCREEN
,
165 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
166 IDM_TOOLBAR_CHANGE_TOOLTIP
,
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
178 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
179 EVT_SIZE(MyFrame::OnSize
)
181 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
182 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
184 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
186 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
187 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
188 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
190 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
191 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
192 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
193 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
194 EVT_MENU(IDM_TOOLBAR_TOGGLEFULLSCREEN
, MyFrame::OnToggleFullScreen
)
195 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
197 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
199 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
201 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
203 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
204 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
207 // ============================================================================
209 // ============================================================================
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
217 // The `main program' equivalent, creating the windows and returning the
221 // Create the main frame window
222 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
224 wxPoint(100, 100), wxSize(450, 300));
228 frame
->SetStatusText("Hello, wxWindows");
235 void MyFrame::RecreateToolbar()
237 // delete and recreate the toolbar
238 wxToolBarBase
*toolBar
= GetToolBar();
243 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
244 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
246 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
247 toolBar
->SetMargins( 4, 4 );
250 wxBitmap toolBarBitmaps
[8];
253 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
254 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
255 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
256 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
257 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
258 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
259 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
260 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
261 #else // !USE_XPM_BITMAPS
262 toolBarBitmaps
[0] = wxBITMAP(new);
263 toolBarBitmaps
[1] = wxBITMAP(open
);
264 toolBarBitmaps
[2] = wxBITMAP(save
);
265 toolBarBitmaps
[3] = wxBITMAP(copy
);
266 toolBarBitmaps
[4] = wxBITMAP(cut
);
267 toolBarBitmaps
[5] = wxBITMAP(paste
);
268 toolBarBitmaps
[6] = wxBITMAP(print
);
269 toolBarBitmaps
[7] = wxBITMAP(help
);
270 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
272 if ( !m_smallToolbar
)
274 int w
= 2*toolBarBitmaps
[0].GetWidth(),
275 h
= 2*toolBarBitmaps
[0].GetHeight();
276 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
279 wxImage(toolBarBitmaps
[n
]).Scale(w
, h
).ConvertToBitmap();
282 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
293 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
294 currentX
+= width
+ 5;
295 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
297 // neither the generic nor Motif native toolbars really support this
298 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
299 // adding a combo to a vertical toolbar is not very smart
302 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, "", wxDefaultPosition
, wxSize(200,-1) );
303 combo
->Append("This");
304 combo
->Append("is a");
305 combo
->Append("combobox");
306 combo
->Append("in a");
307 combo
->Append("toolbar");
309 wxTextCtrl *combo = new wxTextCtrl( toolBar, -1, "", wxDefaultPosition, wxSize(80,-1) );
311 toolBar
->AddControl(combo
);
313 #endif // toolbars which don't support controls
315 currentX
+= width
+ 5;
316 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
317 currentX
+= width
+ 5;
318 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
319 currentX
+= width
+ 5;
320 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
321 currentX
+= width
+ 5;
322 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
323 currentX
+= width
+ 5;
324 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
325 currentX
+= width
+ 5;
326 toolBar
->AddSeparator();
327 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
329 // after adding the buttons to the toolbar, must call Realize() to reflect
333 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 // Define my frame constructor
341 MyFrame::MyFrame(wxFrame
* parent
,
343 const wxString
& title
,
347 : wxFrame(parent
, id
, title
, pos
, size
, style
)
350 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
352 m_smallToolbar
= TRUE
;
353 m_horzToolbar
= TRUE
;
356 // Give it a status line
360 SetIcon(wxICON(mondrian
));
363 wxMenu
*tbarMenu
= new wxMenu
;
364 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
365 "Toggle &another toolbar\tCtrl-A",
366 "Show/hide another test toolbar",
369 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
370 "&Toggle toolbar size\tCtrl-S",
371 "Toggle between big/small toolbar",
373 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
374 "Toggle toolbar &orientation\tCtrl-O",
375 "Toggle toolbar orientation",
377 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
378 "Toggle number of &rows\tCtrl-R",
379 "Toggle number of toolbar rows between 1 and 2",
382 tbarMenu
->AppendSeparator();
384 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
385 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
386 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
387 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
388 tbarMenu
->AppendSeparator();
389 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
390 tbarMenu
->AppendSeparator();
391 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN
, "Toggle &full screen mode\tCtrl-F", "");
393 wxMenu
*fileMenu
= new wxMenu
;
394 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
396 wxMenu
*helpMenu
= new wxMenu
;
397 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
399 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
401 menuBar
->Append(fileMenu
, "&File");
402 menuBar
->Append(tbarMenu
, "&Toolbar");
403 menuBar
->Append(helpMenu
, "&Help");
405 // Associate the menu bar with the frame
408 // Create the toolbar
414 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
416 const wxString
& name
)
418 return (wxToolBar
*)new wxToolBarSimple(this, id
,
419 wxDefaultPosition
, wxDefaultSize
,
423 #endif // USE_GENERIC_TBAR
425 void MyFrame::LayoutChildren()
427 wxSize size
= GetClientSize();
432 m_tbar
->SetSize(-1, size
.y
);
435 offset
= m_tbar
->GetSize().x
;
442 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
445 void MyFrame::OnSize(wxSizeEvent
& event
)
457 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
466 m_tbar
= new wxToolBar(this, -1,
467 wxDefaultPosition
, wxDefaultSize
,
469 m_tbar
->AddTool(wxID_HELP
, wxBITMAP(help
),
472 "This is the help button",
473 "This is the long help for the help button");
480 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
482 m_smallToolbar
= !m_smallToolbar
;
487 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
489 // m_rows may be only 1 or 2
492 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
494 //RecreateToolbar(); -- this is unneeded
497 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
499 m_horzToolbar
= !m_horzToolbar
;
504 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
509 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
511 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
514 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
517 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
518 m_textWindow
->WriteText( str
);
520 if (event
.GetId() == wxID_HELP
)
522 if ( event
.GetExtraLong() != 0 )
523 m_textWindow
->WriteText( _T("Help button down now.\n") );
525 m_textWindow
->WriteText( _T("Help button up now.\n") );
528 if (event
.GetId() == wxID_COPY
)
533 if (event
.GetId() == wxID_CUT
)
538 if (event
.GetId() == wxID_PRINT
)
544 void MyFrame::OnCombo(wxCommandEvent
& event
)
546 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
549 void MyFrame::DoEnablePrint()
551 wxToolBarBase
*tb
= GetToolBar();
552 if (tb
->GetToolEnabled(wxID_PRINT
))
553 tb
->EnableTool( wxID_PRINT
, FALSE
);
555 tb
->EnableTool( wxID_PRINT
, TRUE
);
558 void MyFrame::DoDeletePrint()
560 wxToolBarBase
*tb
= GetToolBar();
562 tb
->DeleteTool( wxID_PRINT
);
565 void MyFrame::DoToggleHelp()
567 wxToolBarBase
*tb
= GetToolBar();
568 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
571 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
573 event
.Enable( m_textWindow
->CanCopy() );
576 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
578 GetToolBar()->SetToolShortHelp(wxID_NEW
, _T("New toolbar button"));
581 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
583 wxBitmap bmp
= wxBITMAP(print
);
585 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
586 FALSE
, (wxObject
*) NULL
,
588 "This button was inserted into the toolbar");
590 GetToolBar()->Realize();
593 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
595 if (event
.GetSelection() > -1)
598 str
.Printf(_T("This is tool number %d"), event
.GetSelection());
605 void MyFrame::OnToggleFullScreen(wxCommandEvent
& event
)
608 ShowFullScreen(!IsFullScreen());