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 OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
114 void OnToolLeftClick(wxCommandEvent
& event
);
115 void OnToolEnter(wxCommandEvent
& event
);
117 void OnCombo(wxCommandEvent
& event
);
119 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
121 void OnToggleFullScreen(wxCommandEvent
& 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;
155 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
156 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
157 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
158 IDM_TOOLBAR_ENABLEPRINT
,
159 IDM_TOOLBAR_DELETEPRINT
,
160 IDM_TOOLBAR_INSERTPRINT
,
161 IDM_TOOLBAR_TOGGLEHELP
,
162 IDM_TOOLBAR_TOGGLEFULLSCREEN
,
163 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
175 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
176 EVT_SIZE(MyFrame::OnSize
)
178 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
179 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
181 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
183 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
184 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
185 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
187 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
188 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
189 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
190 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
191 EVT_MENU(IDM_TOOLBAR_TOGGLEFULLSCREEN
, MyFrame::OnToggleFullScreen
)
193 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
195 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
197 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
199 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
200 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
203 // ============================================================================
205 // ============================================================================
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
213 // The `main program' equivalent, creating the windows and returning the
217 // Create the main frame window
218 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
220 wxPoint(100, 100), wxSize(450, 300));
224 frame
->SetStatusText("Hello, wxWindows");
231 void MyFrame::RecreateToolbar()
233 // delete and recreate the toolbar
234 wxToolBarBase
*toolBar
= GetToolBar();
239 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
240 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
242 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
243 toolBar
->SetMargins( 4, 4 );
246 wxBitmap toolBarBitmaps
[8];
249 toolBarBitmaps
[0] = wxBitmap(new_xpm
);
250 toolBarBitmaps
[1] = wxBitmap(open_xpm
);
251 toolBarBitmaps
[2] = wxBitmap(save_xpm
);
252 toolBarBitmaps
[3] = wxBitmap(copy_xpm
);
253 toolBarBitmaps
[4] = wxBitmap(cut_xpm
);
254 toolBarBitmaps
[5] = wxBitmap(paste_xpm
);
255 toolBarBitmaps
[6] = wxBitmap(print_xpm
);
256 toolBarBitmaps
[7] = wxBitmap(help_xpm
);
257 #else // !USE_XPM_BITMAPS
258 toolBarBitmaps
[0] = wxBITMAP(new);
259 toolBarBitmaps
[1] = wxBITMAP(open
);
260 toolBarBitmaps
[2] = wxBITMAP(save
);
261 toolBarBitmaps
[3] = wxBITMAP(copy
);
262 toolBarBitmaps
[4] = wxBITMAP(cut
);
263 toolBarBitmaps
[5] = wxBITMAP(paste
);
264 toolBarBitmaps
[6] = wxBITMAP(print
);
265 toolBarBitmaps
[7] = wxBITMAP(help
);
266 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
268 if ( !m_smallToolbar
)
270 int w
= 2*toolBarBitmaps
[0].GetWidth(),
271 h
= 2*toolBarBitmaps
[0].GetHeight();
272 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
275 wxImage(toolBarBitmaps
[n
]).Scale(w
, h
).ConvertToBitmap();
278 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
289 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
290 currentX
+= width
+ 5;
291 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
293 // neither the generic nor Motif native toolbars really support this
294 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
295 // adding a combo to a vertical toolbar is not very smart
298 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
299 combo
->Append("This");
300 combo
->Append("is a");
301 combo
->Append("combobox");
302 combo
->Append("in a");
303 combo
->Append("toolbar");
304 toolBar
->AddControl(combo
);
306 #endif // toolbars which don't support controls
308 currentX
+= width
+ 5;
309 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
310 currentX
+= width
+ 5;
311 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
312 currentX
+= width
+ 5;
313 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
314 currentX
+= width
+ 5;
315 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
316 currentX
+= width
+ 5;
317 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
318 currentX
+= width
+ 5;
319 toolBar
->AddSeparator();
320 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
322 // after adding the buttons to the toolbar, must call Realize() to reflect
326 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 // Define my frame constructor
334 MyFrame::MyFrame(wxFrame
* parent
,
336 const wxString
& title
,
340 : wxFrame(parent
, id
, title
, pos
, size
, style
)
343 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
345 m_smallToolbar
= TRUE
;
346 m_horzToolbar
= TRUE
;
349 // Give it a status line
353 SetIcon(wxICON(mondrian
));
356 wxMenu
*tbarMenu
= new wxMenu
;
357 tbarMenu
->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
358 "Toggle &another toolbar\tCtrl-A",
359 "Show/hide another test toolbar",
362 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
363 "&Toggle toolbar size\tCtrl-S",
364 "Toggle between big/small toolbar",
366 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
367 "Toggle toolbar &orientation\tCtrl-O",
368 "Toggle toolbar orientation",
370 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
371 "Toggle number of &rows\tCtrl-R",
372 "Toggle number of toolbar rows between 1 and 2",
375 tbarMenu
->AppendSeparator();
377 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
378 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
379 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
380 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
383 tbarMenu
->AppendSeparator();
384 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN
, "Toggle &full screen mode\tCtrl-F", "");
387 wxMenu
*fileMenu
= new wxMenu
;
388 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
390 wxMenu
*helpMenu
= new wxMenu
;
391 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
393 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
395 menuBar
->Append(fileMenu
, "&File");
396 menuBar
->Append(tbarMenu
, "&Toolbar");
397 menuBar
->Append(helpMenu
, "&Help");
399 // Associate the menu bar with the frame
402 // Create the toolbar
408 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
410 const wxString
& name
)
412 return (wxToolBar
*)new wxToolBarSimple(this, id
,
413 wxDefaultPosition
, wxDefaultSize
,
417 #endif // USE_GENERIC_TBAR
419 void MyFrame::LayoutChildren()
421 wxSize size
= GetClientSize();
426 m_tbar
->SetSize(-1, size
.y
);
429 offset
= m_tbar
->GetSize().x
;
436 m_textWindow
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
439 void MyFrame::OnSize(wxSizeEvent
& event
)
451 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
460 m_tbar
= new wxToolBar(this, -1,
461 wxDefaultPosition
, wxDefaultSize
,
463 m_tbar
->AddTool(wxID_HELP
, wxBITMAP(help
),
466 "This is the help button",
467 "This is the long help for the help button");
474 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
476 m_smallToolbar
= !m_smallToolbar
;
481 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
483 // m_rows may be only 1 or 2
486 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
488 //RecreateToolbar(); -- this is unneeded
491 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
493 m_horzToolbar
= !m_horzToolbar
;
498 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
503 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
505 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
508 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
511 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
512 m_textWindow
->WriteText( str
);
514 if (event
.GetId() == wxID_HELP
)
516 if ( event
.GetExtraLong() != 0 )
517 m_textWindow
->WriteText( _T("Help button down now.\n") );
519 m_textWindow
->WriteText( _T("Help button up now.\n") );
522 if (event
.GetId() == wxID_COPY
)
527 if (event
.GetId() == wxID_CUT
)
532 if (event
.GetId() == wxID_PRINT
)
538 void MyFrame::OnCombo(wxCommandEvent
& event
)
540 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
543 void MyFrame::DoEnablePrint()
545 wxToolBarBase
*tb
= GetToolBar();
546 if (tb
->GetToolEnabled(wxID_PRINT
))
547 tb
->EnableTool( wxID_PRINT
, FALSE
);
549 tb
->EnableTool( wxID_PRINT
, TRUE
);
552 void MyFrame::DoDeletePrint()
554 wxToolBarBase
*tb
= GetToolBar();
556 tb
->DeleteTool( wxID_PRINT
);
559 void MyFrame::DoToggleHelp()
561 wxToolBarBase
*tb
= GetToolBar();
562 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
565 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
567 event
.Enable( m_textWindow
->CanCopy() );
570 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
572 wxBitmap bmp
= wxBITMAP(print
);
574 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
575 FALSE
, (wxObject
*) NULL
,
577 "This button was inserted into the toolbar");
579 GetToolBar()->Realize();
582 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
584 if (event
.GetSelection() > -1)
587 str
.Printf(_T("This is tool number %d"), event
.GetSelection());
594 void MyFrame::OnToggleFullScreen(wxCommandEvent
& event
)
597 ShowFullScreen(!IsFullScreen());