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/paste.xpm"
46 #include "bitmaps/print.xpm"
47 #include "bitmaps/preview.xpm"
48 #include "bitmaps/help.xpm"
49 #endif // GTK or Motif
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // Define a new application
56 class MyApp
: public wxApp
60 bool InitToolbar(wxToolBar
* toolBar
, bool smallicons
= FALSE
);
64 class MyFrame
: public wxFrame
67 MyFrame(wxFrame
*parent
,
69 const wxString
& title
= "wxToolBar Sample",
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
72 long style
= wxDEFAULT_FRAME_STYLE
);
74 void OnQuit(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
77 void OnToggleToolbar(wxCommandEvent
& event
);
78 void OnEnablePrint(wxCommandEvent
& event
) { DoEnablePrint(); }
79 void OnDeletePrint(wxCommandEvent
& event
) { DoDeletePrint(); }
80 void OnInsertPrint(wxCommandEvent
& event
);
81 void OnToggleHelp(wxCommandEvent
& event
) { DoToggleHelp(); }
83 void OnToolLeftClick(wxCommandEvent
& event
);
84 void OnToolEnter(wxCommandEvent
& event
);
86 void OnCombo(wxCommandEvent
& event
);
94 wxTextCtrl
* m_textWindow
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 const int ID_TOOLBAR
= 500;
107 IDM_TOOLBAR_TOGGLETOOLBAR
= 200,
108 IDM_TOOLBAR_ENABLEPRINT
,
109 IDM_TOOLBAR_DELETEPRINT
,
110 IDM_TOOLBAR_INSERTPRINT
,
111 IDM_TOOLBAR_TOGGLEHELP
,
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
123 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
124 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
125 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
127 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR
, MyFrame::OnToggleToolbar
)
128 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
129 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
130 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
131 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
133 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
135 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
137 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
150 // The `main program' equivalent, creating the windows and returning the
154 // Create the main frame window
155 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
157 wxPoint(100, 100), wxSize(450, 300));
159 // VZ: what's this for??
161 // Force a resize. This should probably be replaced by a call to a wxFrame
162 // function that lays out default decorations and the remaining content window.
163 wxSizeEvent
event(wxSize(-1, -1), frame
->GetId());
164 frame
->OnSize(event
);
169 frame
->SetStatusText("Hello, wxWindows");
176 bool MyApp::InitToolbar(wxToolBar
* toolBar
, bool smallicons
)
179 wxBitmap
* toolBarBitmaps
[8];
182 toolBarBitmaps
[0] = new wxBitmap("icon1");
183 toolBarBitmaps
[1] = new wxBitmap("icon2");
186 toolBarBitmaps
[2] = new wxBitmap("icon3");
187 toolBarBitmaps
[3] = new wxBitmap("icon4");
188 toolBarBitmaps
[4] = new wxBitmap("icon5");
189 toolBarBitmaps
[5] = new wxBitmap("icon6");
190 toolBarBitmaps
[6] = new wxBitmap("icon7");
191 toolBarBitmaps
[7] = new wxBitmap("icon8");
194 toolBarBitmaps
[0] = new wxBitmap( new_xpm
);
195 toolBarBitmaps
[1] = new wxBitmap( open_xpm
);
198 toolBarBitmaps
[2] = new wxBitmap( save_xpm
);
199 toolBarBitmaps
[3] = new wxBitmap( copy_xpm
);
200 toolBarBitmaps
[4] = new wxBitmap( cut_xpm
);
201 toolBarBitmaps
[5] = new wxBitmap( preview_xpm
);
202 toolBarBitmaps
[6] = new wxBitmap( print_xpm
);
203 toolBarBitmaps
[7] = new wxBitmap( help_xpm
);
214 toolBar
->AddTool(wxID_NEW
, *(toolBarBitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
215 currentX
+= width
+ 5;
216 toolBar
->AddTool(wxID_OPEN
, *(toolBarBitmaps
[1]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
217 currentX
+= width
+ 5;
218 toolBar
->AddTool(wxID_SAVE
, *(toolBarBitmaps
[2]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
220 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
221 combo
->Append("This");
222 combo
->Append("is a");
223 combo
->Append("combobox");
224 combo
->Append("in a");
225 combo
->Append("toolbar");
226 toolBar
->AddControl(combo
);
230 currentX
+= width
+ 5;
231 toolBar
->AddTool(wxID_COPY
, *(toolBarBitmaps
[3]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
232 currentX
+= width
+ 5;
233 toolBar
->AddTool(wxID_CUT
, *(toolBarBitmaps
[4]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
234 currentX
+= width
+ 5;
235 toolBar
->AddTool(wxID_PASTE
, *(toolBarBitmaps
[5]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
236 currentX
+= width
+ 5;
237 toolBar
->AddTool(wxID_PRINT
, *(toolBarBitmaps
[6]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
238 currentX
+= width
+ 5;
239 toolBar
->AddSeparator();
240 toolBar
->AddTool(wxID_HELP
, *(toolBarBitmaps
[7]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
245 // Can delete the bitmaps since they're reference counted
246 int i
, max
= smallicons
? 3 : WXSIZEOF(toolBarBitmaps
);
247 for (i
= 0; i
< max
; i
++)
248 delete toolBarBitmaps
[i
];
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 // Define my frame constructor
258 MyFrame::MyFrame(wxFrame
* parent
,
260 const wxString
& title
,
264 : wxFrame(parent
, id
, title
, pos
, size
, style
)
266 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
267 m_smallToolbar
= FALSE
;
269 // Give it a status line
273 SetIcon(wxICON(mondrian
));
276 wxMenu
*tbarMenu
= new wxMenu
;
277 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBAR
, "&Toggle toolbar", "Change the toolbar kind");
278 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button", "");
279 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button", "");
280 tbarMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, "&Insert print button", "");
281 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button", "");
283 wxMenu
*fileMenu
= new wxMenu
;
284 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
286 wxMenu
*helpMenu
= new wxMenu
;
287 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
289 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
291 menuBar
->Append(fileMenu
, "&File");
292 menuBar
->Append(tbarMenu
, "&Toolbar");
293 menuBar
->Append(helpMenu
, "&Help");
295 // Associate the menu bar with the frame
298 // Create the toolbar
299 wxToolBar
*tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
300 wxTB_FLAT
| wxTB_DOCKABLE
,
303 tbar
->SetMargins( 4, 4 );
305 wxGetApp().InitToolbar(tbar
);
308 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
310 // delete and recreate the toolbar
311 wxToolBar
*tbar
= GetToolBar();
315 tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
316 wxTB_FLAT
| wxTB_DOCKABLE
,
319 m_smallToolbar
= !m_smallToolbar
;
320 wxGetApp().InitToolbar(tbar
, m_smallToolbar
);
323 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
328 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
330 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
333 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
336 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
337 m_textWindow
->WriteText( str
);
339 if (event
.GetId() == wxID_HELP
)
341 if ( event
.GetExtraLong() != 0 )
342 m_textWindow
->WriteText( _T("Help button down now.\n") );
344 m_textWindow
->WriteText( _T("Help button up now.\n") );
347 if (event
.GetId() == wxID_COPY
)
352 if (event
.GetId() == wxID_CUT
)
357 if (event
.GetId() == wxID_PRINT
)
363 void MyFrame::OnCombo(wxCommandEvent
& event
)
365 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
368 void MyFrame::DoEnablePrint()
370 wxToolBar
*tb
= GetToolBar();
371 if (tb
->GetToolEnabled(wxID_PRINT
))
372 tb
->EnableTool( wxID_PRINT
, FALSE
);
374 tb
->EnableTool( wxID_PRINT
, TRUE
);
377 void MyFrame::DoDeletePrint()
379 wxToolBar
*tb
= GetToolBar();
381 // only implemented in wxGTK and wxMSW for now
382 #if !defined(__WXGTK__) && !defined(__WXMSW__)
383 wxMessageBox("Sorry, wxToolBar::DeleteTool is not implemented.");
385 tb
->DeleteTool( wxID_PRINT
);
389 void MyFrame::DoToggleHelp()
391 wxToolBar
*tb
= GetToolBar();
392 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
395 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
398 wxBitmap
bmp("icon7");
400 wxBitmap
bmp(print_xpm
);
403 GetToolBar()->AddTool(wxID_PRINT
, bmp
, wxNullBitmap
,
405 (wxObject
*) NULL
, "Delete this tool");
407 GetToolBar()->Realize();
410 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
412 if (event
.GetSelection() > -1)
415 str
.Printf(_T("This is tool number %d"), event
.GetSelection());