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
39 #if !wxUSE_TOOLBAR_SIMPLE
40 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
41 to 1 in setup.h and recompile the library.
43 #include <wx/tbarsmpl.h>
45 #endif // USE_GENERIC_TBAR
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #if defined(__WXGTK__) || defined(__WXMOTIF__)
52 #include "mondrian.xpm"
53 #include "bitmaps/new.xpm"
54 #include "bitmaps/open.xpm"
55 #include "bitmaps/save.xpm"
56 #include "bitmaps/copy.xpm"
57 #include "bitmaps/cut.xpm"
58 #include "bitmaps/preview.xpm" // paste XPM
59 #include "bitmaps/print.xpm"
60 #include "bitmaps/help.xpm"
61 #endif // GTK or Motif
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // Define a new application
68 class MyApp
: public wxApp
75 class MyFrame
: public wxFrame
78 MyFrame(wxFrame
*parent
,
80 const wxString
& title
= "wxToolBar Sample",
81 const wxPoint
& pos
= wxDefaultPosition
,
82 const wxSize
& size
= wxDefaultSize
,
83 long style
= wxDEFAULT_FRAME_STYLE
);
85 void RecreateToolbar();
87 void OnQuit(wxCommandEvent
& event
);
88 void OnAbout(wxCommandEvent
& event
);
90 void OnToggleToolbarSize(wxCommandEvent
& event
);
91 void OnToggleToolbarOrient(wxCommandEvent
& event
);
92 void OnToggleToolbarRows(wxCommandEvent
& event
);
94 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
95 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
96 void OnInsertPrint(wxCommandEvent
& event
);
97 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
99 void OnToolLeftClick(wxCommandEvent
& event
);
100 void OnToolEnter(wxCommandEvent
& event
);
102 void OnCombo(wxCommandEvent
& event
);
104 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
106 void OnToggleFullScreen(wxCommandEvent
& event
);
109 virtual wxToolBar
*OnCreateToolBar(long style
,
111 const wxString
& name
);
112 #endif // USE_GENERIC_TBAR
115 void DoEnablePrint();
116 void DoDeletePrint();
121 size_t m_rows
; // 1 or 2 only
123 wxTextCtrl
* m_textWindow
;
125 DECLARE_EVENT_TABLE()
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 const int ID_TOOLBAR
= 500;
136 IDM_TOOLBAR_TOGGLETOOLBARSIZE
= 200,
137 IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
138 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
139 IDM_TOOLBAR_ENABLEPRINT
,
140 IDM_TOOLBAR_DELETEPRINT
,
141 IDM_TOOLBAR_INSERTPRINT
,
142 IDM_TOOLBAR_TOGGLEHELP
,
143 IDM_TOOLBAR_TOGGLEFULLSCREEN
,
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
155 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
156 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
157 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
159 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
160 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT
, MyFrame::OnToggleToolbarOrient
)
161 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
163 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
164 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
165 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
166 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
167 EVT_MENU(IDM_TOOLBAR_TOGGLEFULLSCREEN
, MyFrame::OnToggleFullScreen
)
169 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
171 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
173 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
175 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
176 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
179 // ============================================================================
181 // ============================================================================
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
189 // The `main program' equivalent, creating the windows and returning the
193 // Create the main frame window
194 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
196 wxPoint(100, 100), wxSize(450, 300));
200 frame
->SetStatusText("Hello, wxWindows");
207 void MyFrame::RecreateToolbar()
209 // delete and recreate the toolbar
210 wxToolBarBase
*toolBar
= GetToolBar();
215 long style
= wxNO_BORDER
| wxTB_FLAT
| wxTB_DOCKABLE
;
216 style
|= m_horzToolbar
? wxTB_HORIZONTAL
: wxTB_VERTICAL
;
218 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
219 toolBar
->SetMargins( 4, 4 );
222 wxBitmap toolBarBitmaps
[8];
224 toolBarBitmaps
[0] = wxBITMAP(new);
225 toolBarBitmaps
[1] = wxBITMAP(open
);
226 toolBarBitmaps
[2] = wxBITMAP(save
);
227 toolBarBitmaps
[3] = wxBITMAP(copy
);
228 toolBarBitmaps
[4] = wxBITMAP(cut
);
229 toolBarBitmaps
[5] = wxBITMAP(paste
);
230 toolBarBitmaps
[6] = wxBITMAP(print
);
231 toolBarBitmaps
[7] = wxBITMAP(help
);
233 if ( !m_smallToolbar
)
235 int w
= 2*toolBarBitmaps
[0].GetWidth(),
236 h
= 2*toolBarBitmaps
[0].GetHeight();
237 for ( size_t n
= 0; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
240 wxImage(toolBarBitmaps
[n
]).Scale(w
, h
).ConvertToBitmap();
243 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
254 toolBar
->AddTool(wxID_NEW
, toolBarBitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
255 currentX
+= width
+ 5;
256 toolBar
->AddTool(wxID_OPEN
, toolBarBitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
258 // neither the generic nor Motif native toolbars really support this
259 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
260 // adding a combo to a vertical toolbar is not very smart
263 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
264 combo
->Append("This");
265 combo
->Append("is a");
266 combo
->Append("combobox");
267 combo
->Append("in a");
268 combo
->Append("toolbar");
269 toolBar
->AddControl(combo
);
271 #endif // toolbars which don't support controls
273 currentX
+= width
+ 5;
274 toolBar
->AddTool(wxID_SAVE
, toolBarBitmaps
[2], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
275 currentX
+= width
+ 5;
276 toolBar
->AddTool(wxID_COPY
, toolBarBitmaps
[3], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
277 currentX
+= width
+ 5;
278 toolBar
->AddTool(wxID_CUT
, toolBarBitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
279 currentX
+= width
+ 5;
280 toolBar
->AddTool(wxID_PASTE
, toolBarBitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
281 currentX
+= width
+ 5;
282 toolBar
->AddTool(wxID_PRINT
, toolBarBitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
283 currentX
+= width
+ 5;
284 toolBar
->AddSeparator();
285 toolBar
->AddTool(wxID_HELP
, toolBarBitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
287 // after adding the buttons to the toolbar, must call Realize() to reflect
291 toolBar
->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 // Define my frame constructor
299 MyFrame::MyFrame(wxFrame
* parent
,
301 const wxString
& title
,
305 : wxFrame(parent
, id
, title
, pos
, size
, style
)
307 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
309 m_smallToolbar
= TRUE
;
310 m_horzToolbar
= TRUE
;
313 // Give it a status line
317 SetIcon(wxICON(mondrian
));
320 wxMenu
*tbarMenu
= new wxMenu
;
321 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
322 "&Toggle toolbar size\tCtrl-S",
323 "Toggle between big/small toolbar",
325 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT
,
326 "Toggle toolbar &orientation\tCtrl-O",
327 "Toggle toolbar orientation",
329 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
330 "Toggle number of &rows\tCtrl-R",
331 "Toggle number of toolbar rows between 1 and 2",
334 tbarMenu
->AppendSeparator();
336 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button\tCtrl-E", "");
337 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button\tCtrl-D", "");
338 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button\tCtrl-I", "");
339 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button\tCtrl-T", "");
342 tbarMenu
->AppendSeparator();
343 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN
, "Toggle &full screen mode\tCtrl-F", "");
346 wxMenu
*fileMenu
= new wxMenu
;
347 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
349 wxMenu
*helpMenu
= new wxMenu
;
350 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
352 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
354 menuBar
->Append(fileMenu
, "&File");
355 menuBar
->Append(tbarMenu
, "&Toolbar");
356 menuBar
->Append(helpMenu
, "&Help");
358 // Associate the menu bar with the frame
361 // Create the toolbar
367 wxToolBar
* MyFrame::OnCreateToolBar(long style
,
369 const wxString
& name
)
371 return (wxToolBar
*)new wxToolBarSimple(this, id
,
372 wxDefaultPosition
, wxDefaultSize
,
376 #endif // USE_GENERIC_TBAR
378 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
380 m_smallToolbar
= !m_smallToolbar
;
385 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
387 // m_rows may be only 1 or 2
390 GetToolBar()->SetRows(m_horzToolbar
? m_rows
: 10 / m_rows
);
392 //RecreateToolbar(); -- this is unneeded
395 void MyFrame::OnToggleToolbarOrient(wxCommandEvent
& WXUNUSED(event
))
397 m_horzToolbar
= !m_horzToolbar
;
402 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
407 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
409 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
412 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
415 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
416 m_textWindow
->WriteText( str
);
418 if (event
.GetId() == wxID_HELP
)
420 if ( event
.GetExtraLong() != 0 )
421 m_textWindow
->WriteText( _T("Help button down now.\n") );
423 m_textWindow
->WriteText( _T("Help button up now.\n") );
426 if (event
.GetId() == wxID_COPY
)
431 if (event
.GetId() == wxID_CUT
)
436 if (event
.GetId() == wxID_PRINT
)
442 void MyFrame::OnCombo(wxCommandEvent
& event
)
444 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
447 void MyFrame::DoEnablePrint()
449 wxToolBarBase
*tb
= GetToolBar();
450 if (tb
->GetToolEnabled(wxID_PRINT
))
451 tb
->EnableTool( wxID_PRINT
, FALSE
);
453 tb
->EnableTool( wxID_PRINT
, TRUE
);
456 void MyFrame::DoDeletePrint()
458 wxToolBarBase
*tb
= GetToolBar();
460 tb
->DeleteTool( wxID_PRINT
);
463 void MyFrame::DoToggleHelp()
465 wxToolBarBase
*tb
= GetToolBar();
466 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
469 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
471 event
.Enable( m_textWindow
->CanCopy() );
474 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
476 wxBitmap bmp
= wxBITMAP(print
);
478 GetToolBar()->InsertTool(0, wxID_PRINT
, bmp
, wxNullBitmap
,
479 FALSE
, (wxObject
*) NULL
,
481 "This button was inserted into the toolbar");
483 GetToolBar()->Realize();
486 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
488 if (event
.GetSelection() > -1)
491 str
.Printf(_T("This is tool number %d"), event
.GetSelection());
498 void MyFrame::OnToggleFullScreen(wxCommandEvent
& event
)
501 ShowFullScreen(!IsFullScreen());