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>
35 // define this to 1 to use wxToolBarSimple instead of the native one
36 #define USE_GENERIC_TBAR 0
38 // define this to use XPMs everywhere (by default, BMPs are used under Win)
40 #define USE_XPM_BITMAPS 0
42 #define USE_XPM_BITMAPS 1
46 #if !wxUSE_TOOLBAR_SIMPLE
47 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
48 to 1 in setup.h and recompile the library.
50 #include <wx/tbarsmpl.h>
52 #endif // USE_GENERIC_TBAR
54 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
55 #error You need to enable XPM support to use XPM bitmaps with toolbar!
56 #endif // USE_XPM_BITMAPS
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
63 #include "mondrian.xpm"
64 #include "bitmaps/new.xpm"
65 #include "bitmaps/open.xpm"
66 #include "bitmaps/save.xpm"
67 #include "bitmaps/copy.xpm"
68 #include "bitmaps/cut.xpm"
69 #include "bitmaps/preview.xpm" // paste XPM
70 #include "bitmaps/print.xpm"
71 #include "bitmaps/help.xpm"
72 #endif // USE_XPM_BITMAPS
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // Define a new application
79 class MyApp
: public wxApp
86 class MyFrame
: public wxFrame
89 MyFrame(wxFrame
*parent
,
91 const wxString
& title
= "wxToolBar Sample",
92 const wxPoint
& pos
= wxDefaultPosition
,
93 const wxSize
& size
= wxDefaultSize
,
94 long style
= wxDEFAULT_FRAME_STYLE
);
96 void RecreateToolbar();
98 void OnQuit(wxCommandEvent
& event
);
99 void OnAbout(wxCommandEvent
& event
);
101 void OnSize(wxSizeEvent
& event
);
103 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
105 void OnToggleToolbarSize(wxCommandEvent
& event
);
106 void OnToggleToolbarOrient(wxCommandEvent
& event
);
107 void OnToggleToolbarRows(wxCommandEvent
& event
);
109 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
110 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
111 void OnInsertPrint(wxCommandEvent
& event
);
112 void OnChangeToolTip(wxCommandEvent
& event
);
113 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
115 void OnToolLeftClick(wxCommandEvent
& event
);
116 void OnToolEnter(wxCommandEvent
& event
);
118 void OnCombo(wxCommandEvent
& event
);
120 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
122 void OnToggleFullScreen(wxCommandEvent
& event
);
125 virtual wxToolBar
*OnCreateToolBar(long style
,
127 const wxString
& name
);
128 #endif // USE_GENERIC_TBAR
131 void DoEnablePrint();
132 void DoDeletePrint();
135 void LayoutChildren();
139 size_t m_rows
; // 1 or 2 only
141 wxTextCtrl
*m_textWindow
;
145 DECLARE_EVENT_TABLE()
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 const int ID_TOOLBAR
= 500;
156 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
157 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
158 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
159 IDM_TOOLBAR_ENABLEPRINT
,
160 IDM_TOOLBAR_DELETEPRINT
,
161 IDM_TOOLBAR_INSERTPRINT
,
162 IDM_TOOLBAR_TOGGLEHELP
,
163 IDM_TOOLBAR_TOGGLEFULLSCREEN
,
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_TOGGLEFULLSCREEN
, MyFrame::OnToggleFullScreen
)
194 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
196 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
198 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
200 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
202 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
203 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
206 // ============================================================================
208 // ============================================================================
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
216 // The `main program' equivalent, creating the windows and returning the
220 // Create the main frame window
221 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
223 wxPoint(100, 100), wxSize(450, 300));
227 frame
->SetStatusText("Hello, wxWindows");
234 void MyFrame::RecreateToolbar()
236 // delete and recreate the toolbar
237 wxToolBarBase
*toolBar
= GetToolBar();
242 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
243 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
245 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
246 toolBar
->SetMargins( 4, 4 );
249 wxBitmap toolBarBitmaps
[8];
252 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
253 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
254 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
255 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
256 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
257 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
258 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
259 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
260 #else // !USE_XPM_BITMAPS
261 toolBarBitmaps
[0] = wxBITMAP(new);
262 toolBarBitmaps
[1] = wxBITMAP(open
);
263 toolBarBitmaps
[2] = wxBITMAP(save
);
264 toolBarBitmaps
[3] = wxBITMAP(copy
);
265 toolBarBitmaps
[4] = wxBITMAP(cut
);
266 toolBarBitmaps
[5] = wxBITMAP(paste
);
267 toolBarBitmaps
[6] = wxBITMAP(print
);
268 toolBarBitmaps
[7] = wxBITMAP(help
);
269 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
271 if ( !m_smallToolbar
)
273 int w
= 2*toolBarBitmaps
[0].GetWidth(),
274 h
= 2*toolBarBitmaps
[0].GetHeight();
275 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
278 wxImage(toolBarBitmaps
[n
]).Scale(w
, h
).ConvertToBitmap();
281 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
292 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
293 currentX
+= width
+ 5;
294 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
296 // neither the generic nor Motif native toolbars really support this
297 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
298 // adding a combo to a vertical toolbar is not very smart
301 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
302 combo
->Append("This");
303 combo
->Append("is a");
304 combo
->Append("combobox");
305 combo
->Append("in a");
306 combo
->Append("toolbar");
307 toolBar
->AddControl(combo
);
309 #endif // toolbars which don't support controls
311 currentX
+= width
+ 5;
312 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
313 currentX
+= width
+ 5;
314 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
315 currentX
+= width
+ 5;
316 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
317 currentX
+= width
+ 5;
318 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
319 currentX
+= width
+ 5;
320 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
321 currentX
+= width
+ 5;
322 toolBar
->AddSeparator();
323 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
325 // after adding the buttons to the toolbar, must call Realize() to reflect
329 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 // Define my frame constructor
337 MyFrame::MyFrame(wxFrame
* parent
,
339 const wxString
& title
,
343 : wxFrame(parent
, id
, title
, pos
, size
, style
)
346 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
348 m_smallToolbar
= TRUE
;
349 m_horzToolbar
= TRUE
;
352 // Give it a status line
356 SetIcon(wxICON(mondrian
));
359 wxMenu
*tbarMenu
= new wxMenu
;
360 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
361 "Toggle &another toolbar\tCtrl-A",
362 "Show/hide another test toolbar",
365 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
366 "&Toggle toolbar size\tCtrl-S",
367 "Toggle between big/small toolbar",
369 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
370 "Toggle toolbar &orientation\tCtrl-O",
371 "Toggle toolbar orientation",
373 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
374 "Toggle number of &rows\tCtrl-R",
375 "Toggle number of toolbar rows between 1 and 2",
378 tbarMenu
->AppendSeparator();
380 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
381 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
382 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
383 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
384 tbarMenu
->AppendSeparator();
385 tbarMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, "Change tool tip", "");
386 tbarMenu
->AppendSeparator();
387 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN
, "Toggle &full screen mode\tCtrl-F", "");
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
,
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());
601 void MyFrame::OnToggleFullScreen(wxCommandEvent
& event
)
604 ShowFullScreen(!IsFullScreen());