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 // define this to 1 to use wxToolBarSimple instead of the native one
35 #define USE_GENERIC_TBAR 0
38 #if !wxUSE_TOOLBAR_SIMPLE
39 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
40 to 1 in setup.h and recompile the library.
42 #include <wx/tbarsmpl.h>
44 #endif // USE_GENERIC_TBAR
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #if defined(__WXGTK__) || defined(__WXMOTIF__)
51 #include "mondrian.xpm"
52 #include "bitmaps/new.xpm"
53 #include "bitmaps/open.xpm"
54 #include "bitmaps/save.xpm"
55 #include "bitmaps/copy.xpm"
56 #include "bitmaps/cut.xpm"
57 #include "bitmaps/preview.xpm" // paste XPM
58 #include "bitmaps/print.xpm"
59 #include "bitmaps/help.xpm"
60 #endif // GTK or Motif
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Define a new application
67 class MyApp
: public wxApp
74 class MyFrame
: public wxFrame
77 MyFrame(wxFrame
*parent
,
79 const wxString
& title
= "wxToolBar Sample",
80 const wxPoint
& pos
= wxDefaultPosition
,
81 const wxSize
& size
= wxDefaultSize
,
82 long style
= wxDEFAULT_FRAME_STYLE
);
84 void RecreateToolbar();
86 void OnQuit(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
89 void OnToggleToolbarSize(wxCommandEvent
& event
);
90 void OnToggleToolbarOrient(wxCommandEvent
& event
);
91 void OnToggleToolbarRows(wxCommandEvent
& event
);
93 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
94 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
95 void OnInsertPrint(wxCommandEvent
& event
);
96 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
98 void OnToolLeftClick(wxCommandEvent
& event
);
99 void OnToolEnter(wxCommandEvent
& event
);
101 void OnCombo(wxCommandEvent
& event
);
103 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
106 virtual wxToolBar
*OnCreateToolBar(long style
,
108 const wxString
& name
);
109 #endif // USE_GENERIC_TBAR
112 void DoEnablePrint();
113 void DoDeletePrint();
118 size_t m_rows
; // 1 or 2 only
120 wxTextCtrl
* m_textWindow
;
122 DECLARE_EVENT_TABLE()
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 const int ID_TOOLBAR
= 500;
133 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
134 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
135 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
136 IDM_TOOLBAR_ENABLEPRINT
,
137 IDM_TOOLBAR_DELETEPRINT
,
138 IDM_TOOLBAR_INSERTPRINT
,
139 IDM_TOOLBAR_TOGGLEHELP
,
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
151 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
152 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
153 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
155 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
156 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
157 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
159 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
160 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
161 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
162 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
164 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
166 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
168 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
170 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
171 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
174 // ============================================================================
176 // ============================================================================
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
184 // The `main program' equivalent, creating the windows and returning the
188 // Create the main frame window
189 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
191 wxPoint(100, 100), wxSize(450, 300));
195 frame
->SetStatusText("Hello, wxWindows");
202 void MyFrame::RecreateToolbar()
204 // delete and recreate the toolbar
205 wxToolBarBase
*toolBar
= GetToolBar();
210 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
211 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
213 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
214 toolBar
->SetMargins( 4, 4 );
217 wxBitmap toolBarBitmaps
[8];
219 toolBarBitmaps
[0] = wxBITMAP(new);
220 toolBarBitmaps
[1] = wxBITMAP(open
);
221 if ( !m_smallToolbar
)
223 toolBarBitmaps
[2] = wxBITMAP(save
);
224 toolBarBitmaps
[3] = wxBITMAP(copy
);
225 toolBarBitmaps
[4] = wxBITMAP(cut
);
226 toolBarBitmaps
[5] = wxBITMAP(paste
);
227 toolBarBitmaps
[6] = wxBITMAP(print
);
228 toolBarBitmaps
[7] = wxBITMAP(help
);
239 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
240 currentX
+= width
+ 5;
241 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
243 // neither the generic nor Motif native toolbars really support this
244 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
245 // adding a combo to a vertical toolbar is not very smart
248 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
249 combo
->Append("This");
250 combo
->Append("is a");
251 combo
->Append("combobox");
252 combo
->Append("in a");
253 combo
->Append("toolbar");
254 toolBar
->AddControl(combo
);
256 #endif // toolbars which don't support controls
258 if ( !m_smallToolbar
)
260 currentX
+= width
+ 5;
261 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
262 currentX
+= width
+ 5;
263 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
264 currentX
+= width
+ 5;
265 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
266 currentX
+= width
+ 5;
267 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
268 currentX
+= width
+ 5;
269 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
270 currentX
+= width
+ 5;
271 toolBar
->AddSeparator();
272 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
275 // after adding the buttons to the toolbar, must call Realize() to reflect
279 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 // Define my frame constructor
287 MyFrame::MyFrame(wxFrame
* parent
,
289 const wxString
& title
,
293 : wxFrame(parent
, id
, title
, pos
, size
, style
)
295 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
297 m_smallToolbar
= FALSE
;
298 m_horzToolbar
= TRUE
;
301 // Give it a status line
305 SetIcon(wxICON(mondrian
));
308 wxMenu
*tbarMenu
= new wxMenu
;
309 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
310 "&Toggle toolbar size\tCtrl-S",
311 "Toggle between big/small toolbar",
313 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
314 "Toggle toolbar &orientation\tCtrl-O",
315 "Toggle toolbar orientation",
317 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
318 "Toggle number of &rows\tCtrl-R",
319 "Toggle number of toolbar rows between 1 and 2",
322 tbarMenu
->AppendSeparator();
324 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
325 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
326 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
327 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
329 wxMenu
*fileMenu
= new wxMenu
;
330 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
332 wxMenu
*helpMenu
= new wxMenu
;
333 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
335 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
337 menuBar
->Append(fileMenu
, "&File");
338 menuBar
->Append(tbarMenu
, "&Toolbar");
339 menuBar
->Append(helpMenu
, "&Help");
341 // Associate the menu bar with the frame
344 // Create the toolbar
350 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
352 const wxString
& name
)
354 return (wxToolBar
*)new wxToolBarSimple(this, id
,
355 wxDefaultPosition
, wxDefaultSize
,
359 #endif // USE_GENERIC_TBAR
361 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
363 m_smallToolbar
= !m_smallToolbar
;
368 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
370 // m_rows may be only 1 or 2
373 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
375 //RecreateToolbar(); -- this is unneeded
378 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
380 m_horzToolbar
= !m_horzToolbar
;
385 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
390 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
392 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
395 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
398 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
399 m_textWindow
->WriteText( str
);
401 if (event
.GetId() == wxID_HELP
)
403 if ( event
.GetExtraLong() != 0 )
404 m_textWindow
->WriteText( _T("Help button down now.\n") );
406 m_textWindow
->WriteText( _T("Help button up now.\n") );
409 if (event
.GetId() == wxID_COPY
)
414 if (event
.GetId() == wxID_CUT
)
419 if (event
.GetId() == wxID_PRINT
)
425 void MyFrame::OnCombo(wxCommandEvent
& event
)
427 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
430 void MyFrame::DoEnablePrint()
432 wxToolBarBase
*tb
= GetToolBar();
433 if (tb
->GetToolEnabled(wxID_PRINT
))
434 tb
->EnableTool( wxID_PRINT
, FALSE
);
436 tb
->EnableTool( wxID_PRINT
, TRUE
);
439 void MyFrame::DoDeletePrint()
441 wxToolBarBase
*tb
= GetToolBar();
443 tb
->DeleteTool( wxID_PRINT
);
446 void MyFrame::DoToggleHelp()
448 wxToolBarBase
*tb
= GetToolBar();
449 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
452 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
454 event
.Enable( m_textWindow
->CanCopy() );
457 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
459 wxBitmap bmp
= wxBITMAP(print
);
461 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
462 FALSE
, (wxObject
*) NULL
,
464 "This button was inserted into the toolbar");
466 GetToolBar()->Realize();
469 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
471 if (event
.GetSelection() > -1)
474 str
.Printf(_T("This is tool number %d"), event
.GetSelection());