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 OnToggleHelp(wxCommandEvent
& event
) { DoToggleHelp(); }
82 void OnToolLeftClick(wxCommandEvent
& event
);
83 void OnToolEnter(wxCommandEvent
& event
);
85 void OnCombo(wxCommandEvent
& event
);
93 wxTextCtrl
* m_textWindow
;
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 const int ID_TOOLBAR
= 500;
106 IDM_TOOLBAR_TOGGLETOOLBAR
= 200,
107 IDM_TOOLBAR_ENABLEPRINT
,
108 IDM_TOOLBAR_DELETEPRINT
,
109 IDM_TOOLBAR_TOGGLEHELP
,
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
121 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
122 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
123 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
125 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR
, MyFrame::OnToggleToolbar
)
126 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
127 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
128 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
130 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
132 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
134 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
137 // ============================================================================
139 // ============================================================================
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
147 // The `main program' equivalent, creating the windows and returning the
151 // Create the main frame window
152 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
154 wxPoint(100, 100), wxSize(450, 300));
156 // VZ: what's this for??
158 // Force a resize. This should probably be replaced by a call to a wxFrame
159 // function that lays out default decorations and the remaining content window.
160 wxSizeEvent
event(wxSize(-1, -1), frame
->GetId());
161 frame
->OnSize(event
);
166 frame
->SetStatusText("Hello, wxWindows");
173 bool MyApp::InitToolbar(wxToolBar
* toolBar
, bool smallicons
)
176 wxBitmap
* toolBarBitmaps
[8];
179 toolBarBitmaps
[0] = new wxBitmap("icon1");
180 toolBarBitmaps
[1] = new wxBitmap("icon2");
183 toolBarBitmaps
[2] = new wxBitmap("icon3");
184 toolBarBitmaps
[3] = new wxBitmap("icon4");
185 toolBarBitmaps
[4] = new wxBitmap("icon5");
186 toolBarBitmaps
[5] = new wxBitmap("icon6");
187 toolBarBitmaps
[6] = new wxBitmap("icon7");
188 toolBarBitmaps
[7] = new wxBitmap("icon8");
191 toolBarBitmaps
[0] = new wxBitmap( new_xpm
);
192 toolBarBitmaps
[1] = new wxBitmap( open_xpm
);
195 toolBarBitmaps
[2] = new wxBitmap( save_xpm
);
196 toolBarBitmaps
[3] = new wxBitmap( copy_xpm
);
197 toolBarBitmaps
[4] = new wxBitmap( cut_xpm
);
198 toolBarBitmaps
[5] = new wxBitmap( preview_xpm
);
199 toolBarBitmaps
[6] = new wxBitmap( print_xpm
);
200 toolBarBitmaps
[7] = new wxBitmap( help_xpm
);
211 toolBar
->AddTool(wxID_NEW
, *(toolBarBitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
212 currentX
+= width
+ 5;
213 toolBar
->AddTool(wxID_OPEN
, *(toolBarBitmaps
[1]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
214 currentX
+= width
+ 5;
215 toolBar
->AddTool(wxID_SAVE
, *(toolBarBitmaps
[2]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 1");
217 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
);
218 combo
->Append("This");
219 combo
->Append("is a");
220 combo
->Append("combobox");
221 combo
->Append("in a");
222 combo
->Append("toolbar");
223 toolBar
->AddControl(combo
);
227 currentX
+= width
+ 5;
228 toolBar
->AddTool(wxID_COPY
, *(toolBarBitmaps
[3]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Toggle button 2");
229 currentX
+= width
+ 5;
230 toolBar
->AddTool(wxID_CUT
, *(toolBarBitmaps
[4]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
231 currentX
+= width
+ 5;
232 toolBar
->AddTool(wxID_PASTE
, *(toolBarBitmaps
[5]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
233 currentX
+= width
+ 5;
234 toolBar
->AddTool(wxID_PRINT
, *(toolBarBitmaps
[6]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Delete this tool");
235 currentX
+= width
+ 5;
236 toolBar
->AddSeparator();
237 toolBar
->AddTool(wxID_HELP
, *(toolBarBitmaps
[7]), wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help button");
242 // Can delete the bitmaps since they're reference counted
243 int i
, max
= smallicons
? 3 : WXSIZEOF(toolBarBitmaps
);
244 for (i
= 0; i
< max
; i
++)
245 delete toolBarBitmaps
[i
];
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 // Define my frame constructor
255 MyFrame::MyFrame(wxFrame
* parent
,
257 const wxString
& title
,
261 : wxFrame(parent
, id
, title
, pos
, size
, style
)
263 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
264 m_smallToolbar
= FALSE
;
266 // Give it a status line
270 SetIcon(wxICON(mondrian
));
273 wxMenu
*tbarMenu
= new wxMenu
;
274 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBAR
, "&Toggle toolbar", "Change the toolbar kind");
275 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button", "");
276 tbarMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, "&Delete print button", "");
277 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button", "");
279 wxMenu
*fileMenu
= new wxMenu
;
280 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
282 wxMenu
*helpMenu
= new wxMenu
;
283 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
285 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
287 menuBar
->Append(fileMenu
, "&File");
288 menuBar
->Append(tbarMenu
, "&Toolbar");
289 menuBar
->Append(helpMenu
, "&Help");
291 // Associate the menu bar with the frame
294 // Create the toolbar
295 wxToolBar
*tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
296 wxTB_FLAT
| wxTB_DOCKABLE
,
299 tbar
->SetMargins( 4, 4 );
301 wxGetApp().InitToolbar(tbar
);
304 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
306 // delete and recreate the toolbar
307 wxToolBar
*tbar
= GetToolBar();
311 tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
312 wxTB_FLAT
| wxTB_DOCKABLE
,
315 m_smallToolbar
= !m_smallToolbar
;
316 wxGetApp().InitToolbar(tbar
, m_smallToolbar
);
319 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
324 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
326 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
329 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
332 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
333 m_textWindow
->WriteText( str
);
335 if (event
.GetId() == wxID_HELP
)
337 if ( event
.GetExtraLong() != 0 )
338 m_textWindow
->WriteText( _T("Help button down now.\n") );
340 m_textWindow
->WriteText( _T("Help button up now.\n") );
343 if (event
.GetId() == wxID_COPY
)
348 if (event
.GetId() == wxID_CUT
)
353 if (event
.GetId() == wxID_PRINT
)
359 void MyFrame::OnCombo(wxCommandEvent
& event
)
361 wxLogStatus(_T("Combobox string '%s' selected"), event
.GetString().c_str());
364 void MyFrame::DoEnablePrint()
366 wxToolBar
*tb
= GetToolBar();
367 if (tb
->GetToolEnabled(wxID_PRINT
))
368 tb
->EnableTool( wxID_PRINT
, FALSE
);
370 tb
->EnableTool( wxID_PRINT
, TRUE
);
373 void MyFrame::DoDeletePrint()
375 wxToolBar
*tb
= GetToolBar();
377 // only implemented in wxGTK for now
379 wxMessageBox("Sorry, wxToolBar::DeleteTool is not implemented under Windows.");
381 tb
->DeleteTool( wxID_PRINT
);
385 void MyFrame::DoToggleHelp()
387 wxToolBar
*tb
= GetToolBar();
388 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
391 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
393 if (event
.GetSelection() > -1)
396 str
.Printf(_T("This is tool number %d"), event
.GetSelection());