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 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 #if defined(__WXGTK__) || defined(__WXMOTIF__)
39 #include "mondrian.xpm"
40 #include "bitmaps/new.xpm"
41 #include "bitmaps/open.xpm"
42 #include "bitmaps/save.xpm"
43 #include "bitmaps/copy.xpm"
44 #include "bitmaps/cut.xpm"
45 #include "bitmaps/preview.xpm" // paste XPM
46 #include "bitmaps/print.xpm"
47 #include "bitmaps/help.xpm"
48 #endif // GTK or Motif
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // Define a new application
55 class MyApp
: public wxApp
62 class MyFrame
: public wxFrame
65 MyFrame(wxFrame
*parent
,
67 const wxString
& title
= "wxToolBar Sample",
68 const wxPoint
& pos
= wxDefaultPosition
,
69 const wxSize
& size
= wxDefaultSize
,
70 long style
= wxDEFAULT_FRAME_STYLE
);
72 void RecreateToolbar();
74 void OnQuit(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
77 void OnToggleToolbarSize(wxCommandEvent
& event
);
78 void OnToggleToolbarOrient(wxCommandEvent
& event
);
79 void OnToggleToolbarRows(wxCommandEvent
& event
);
81 void OnEnablePrint(wxCommandEvent
& event
) { DoEnablePrint(); }
82 void OnDeletePrint(wxCommandEvent
& event
) { DoDeletePrint(); }
83 void OnInsertPrint(wxCommandEvent
& event
);
84 void OnToggleHelp(wxCommandEvent
& event
) { DoToggleHelp(); }
86 void OnToolLeftClick(wxCommandEvent
& event
);
87 void OnToolEnter(wxCommandEvent
& event
);
89 void OnCombo(wxCommandEvent
& event
);
91 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
100 size_t m_rows
; // 1 or 2 only
102 wxTextCtrl
* m_textWindow
;
104 DECLARE_EVENT_TABLE()
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 const int ID_TOOLBAR
= 500;
115 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
116 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
117 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
118 IDM_TOOLBAR_ENABLEPRINT
,
119 IDM_TOOLBAR_DELETEPRINT
,
120 IDM_TOOLBAR_INSERTPRINT
,
121 IDM_TOOLBAR_TOGGLEHELP
,
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
133 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
134 EVT_MENU(wxID_EXIT
, MyFrame
::OnQuit
)
135 EVT_MENU(wxID_HELP
, MyFrame
::OnAbout
)
137 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame
::OnToggleToolbarSize
)
138 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame
::OnToggleToolbarOrient
)
139 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame
::OnToggleToolbarRows
)
141 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame
::OnEnablePrint
)
142 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame
::OnDeletePrint
)
143 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame
::OnInsertPrint
)
144 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame
::OnToggleHelp
)
146 EVT_MENU(-1, MyFrame
::OnToolLeftClick
)
148 EVT_COMBOBOX(ID_COMBO
, MyFrame
::OnCombo
)
150 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame
::OnToolEnter
)
152 EVT_UPDATE_UI(wxID_COPY
, MyFrame
::OnUpdateCopyAndCut
)
153 EVT_UPDATE_UI(wxID_CUT
, MyFrame
::OnUpdateCopyAndCut
)
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
166 // The `main program' equivalent, creating the windows and returning the
170 // Create the main frame window
171 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
173 wxPoint(100, 100), wxSize(450, 300));
177 frame
->SetStatusText("Hello, wxWindows");
184 void MyFrame
::RecreateToolbar()
186 // delete and recreate the toolbar
187 wxToolBar
*toolBar
= GetToolBar();
192 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
193 style
|= m_horzToolbar ? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
195 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
196 toolBar
->SetMargins( 4, 4 );
199 wxBitmap toolBarBitmaps
[8];
201 toolBarBitmaps
[0] = wxBITMAP(new);
202 toolBarBitmaps
[1] = wxBITMAP(open
);
203 if ( !m_smallToolbar
)
205 toolBarBitmaps
[2] = wxBITMAP(save
);
206 toolBarBitmaps
[3] = wxBITMAP(copy
);
207 toolBarBitmaps
[4] = wxBITMAP(cut
);
208 toolBarBitmaps
[5] = wxBITMAP(paste
);
209 toolBarBitmaps
[6] = wxBITMAP(print
);
210 toolBarBitmaps
[7] = wxBITMAP(help
);
221 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
222 currentX
+= width
+ 5;
223 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
225 // adding a combo to a vertical toolbar is not very smart
228 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
229 combo
->Append("This");
230 combo
->Append("is a");
231 combo
->Append("combobox");
232 combo
->Append("in a");
233 combo
->Append("toolbar");
234 toolBar
->AddControl(combo
);
237 if ( !m_smallToolbar
)
239 currentX
+= width
+ 5;
240 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
241 currentX
+= width
+ 5;
242 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
243 currentX
+= width
+ 5;
244 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
245 currentX
+= width
+ 5;
246 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
247 currentX
+= width
+ 5;
248 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
249 currentX
+= width
+ 5;
250 toolBar
->AddSeparator();
251 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
254 // after adding the buttons to the toolbar, must call Realize() to reflect
258 toolBar
->SetRows(m_horzToolbar ? m_rows
: 10 / m_rows
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 // Define my frame constructor
266 MyFrame
::MyFrame(wxFrame
* parent
,
268 const wxString
& title
,
272 : wxFrame(parent
, id
, title
, pos
, size
, style
)
274 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
276 m_smallToolbar
= FALSE
;
277 m_horzToolbar
= TRUE
;
280 // Give it a status line
284 SetIcon(wxICON(mondrian
));
287 wxMenu
*tbarMenu
= new wxMenu
;
288 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
289 "&Toggle toolbar size\tCtrl-S",
290 "Toggle between big/small toolbar",
292 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
293 "Toggle toolbar &orientation\tCtrl-O",
294 "Toggle toolbar orientation",
296 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
297 "Toggle number of &rows\tCtrl-R",
298 "Toggle number of toolbar rows between 1 and 2",
301 tbarMenu
->AppendSeparator();
303 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
304 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
305 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
306 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
308 wxMenu
*fileMenu
= new wxMenu
;
309 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
311 wxMenu
*helpMenu
= new wxMenu
;
312 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
314 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
316 menuBar
->Append(fileMenu
, "&File");
317 menuBar
->Append(tbarMenu
, "&Toolbar");
318 menuBar
->Append(helpMenu
, "&Help");
320 // Associate the menu bar with the frame
323 // Create the toolbar
327 void MyFrame
::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
329 m_smallToolbar
= !m_smallToolbar
;
334 void MyFrame
::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
336 // m_rows may be only 1 or 2
339 GetToolBar()->SetRows(m_horzToolbar ? m_rows
: 10 / m_rows
);
344 void MyFrame
::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
346 m_horzToolbar
= !m_horzToolbar
;
351 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
356 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
358 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
361 void MyFrame
::OnToolLeftClick(wxCommandEvent
& event
)
364 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
365 m_textWindow
->WriteText( str
);
367 if (event
.GetId() == wxID_HELP
)
369 if ( event
.GetExtraLong() != 0 )
370 m_textWindow
->WriteText( _T("Help button down now.\n") );
372 m_textWindow
->WriteText( _T("Help button up now.\n") );
375 if (event
.GetId() == wxID_COPY
)
380 if (event
.GetId() == wxID_CUT
)
385 if (event
.GetId() == wxID_PRINT
)
391 void MyFrame
::OnCombo(wxCommandEvent
& event
)
393 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
396 void MyFrame
::DoEnablePrint()
398 wxToolBar
*tb
= GetToolBar();
399 if (tb
->GetToolEnabled(wxID_PRINT
))
400 tb
->EnableTool( wxID_PRINT
, FALSE
);
402 tb
->EnableTool( wxID_PRINT
, TRUE
);
405 void MyFrame
::DoDeletePrint()
407 wxToolBar
*tb
= GetToolBar();
409 tb
->DeleteTool( wxID_PRINT
);
412 void MyFrame
::DoToggleHelp()
414 wxToolBar
*tb
= GetToolBar();
415 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
418 void MyFrame
::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
420 event
.Enable( m_textWindow
->CanCopy() );
423 void MyFrame
::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
425 wxBitmap bmp
= wxBITMAP(print
);
427 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
428 FALSE
, (wxObject
*) NULL
,
430 "This button was inserted into the toolbar");
432 GetToolBar()->Realize();
435 void MyFrame
::OnToolEnter(wxCommandEvent
& event
)
437 if (event
.GetSelection() > -1)
440 str
.Printf(_T("This is tool number %d"), event
.GetSelection());