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 OnToggleHelp(wxCommandEvent
& event
) { DoToggleHelp(); }
81 void OnAppendMenu(wxCommandEvent
& event
);
82 void OnDeleteMenu(wxCommandEvent
& event
);
83 void OnToggleMenu(wxCommandEvent
& event
);
85 void OnToolLeftClick(wxCommandEvent
& event
);
86 void OnToolEnter(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_TOGGLEHELP
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
118 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
119 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
120 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
122 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR
, MyFrame::OnToggleToolbar
)
123 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
124 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
126 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
128 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
141 // The `main program' equivalent, creating the windows and returning the
145 // Create the main frame window
146 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, -1,
148 wxPoint(100, 100), wxSize(450, 300));
150 // VZ: what's this for??
152 // Force a resize. This should probably be replaced by a call to a wxFrame
153 // function that lays out default decorations and the remaining content window.
154 wxSizeEvent
event(wxSize(-1, -1), frame
->GetId());
155 frame
->OnSize(event
);
160 frame
->SetStatusText("Hello, wxWindows");
167 bool MyApp::InitToolbar(wxToolBar
* toolBar
, bool smallicons
)
170 wxBitmap
* toolBarBitmaps
[8];
173 toolBarBitmaps
[0] = new wxBitmap("icon1");
174 toolBarBitmaps
[1] = new wxBitmap("icon2");
177 toolBarBitmaps
[2] = new wxBitmap("icon3");
178 toolBarBitmaps
[3] = new wxBitmap("icon4");
179 toolBarBitmaps
[4] = new wxBitmap("icon5");
180 toolBarBitmaps
[5] = new wxBitmap("icon6");
181 toolBarBitmaps
[6] = new wxBitmap("icon7");
182 toolBarBitmaps
[7] = new wxBitmap("icon8");
185 toolBarBitmaps
[0] = new wxBitmap( new_xpm
);
186 toolBarBitmaps
[1] = new wxBitmap( open_xpm
);
189 toolBarBitmaps
[2] = new wxBitmap( save_xpm
);
190 toolBarBitmaps
[3] = new wxBitmap( copy_xpm
);
191 toolBarBitmaps
[4] = new wxBitmap( cut_xpm
);
192 toolBarBitmaps
[5] = new wxBitmap( preview_xpm
);
193 toolBarBitmaps
[6] = new wxBitmap( print_xpm
);
194 toolBarBitmaps
[7] = new wxBitmap( help_xpm
);
205 toolBar
->AddTool(wxID_NEW
, *(toolBarBitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
206 currentX
+= width
+ 5;
207 toolBar
->AddTool(wxID_OPEN
, *(toolBarBitmaps
[1]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
211 currentX
+= width
+ 5;
212 toolBar
->AddTool(wxID_SAVE
, *(toolBarBitmaps
[2]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Save file");
213 currentX
+= width
+ 5;
214 toolBar
->AddSeparator();
215 toolBar
->AddTool(wxID_COPY
, *(toolBarBitmaps
[3]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Disable/Enable print button");
216 currentX
+= width
+ 5;
217 toolBar
->AddTool(wxID_CUT
, *(toolBarBitmaps
[4]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Toggle/Untoggle help button");
218 currentX
+= width
+ 5;
219 toolBar
->AddTool(wxID_PASTE
, *(toolBarBitmaps
[5]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
220 currentX
+= width
+ 5;
221 toolBar
->AddSeparator();
222 toolBar
->AddTool(wxID_PRINT
, *(toolBarBitmaps
[6]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Print");
223 currentX
+= width
+ 5;
224 toolBar
->AddSeparator();
225 toolBar
->AddTool(wxID_HELP
, *(toolBarBitmaps
[7]), *(toolBarBitmaps
[6]), TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help");
227 toolBar
->EnableTool( wxID_PRINT
, FALSE
);
232 // Can delete the bitmaps since they're reference counted
233 int i
, max
= smallicons
? 2 : WXSIZEOF(toolBarBitmaps
);
234 for (i
= 0; i
< max
; i
++)
235 delete toolBarBitmaps
[i
];
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 // Define my frame constructor
245 MyFrame::MyFrame(wxFrame
* parent
,
247 const wxString
& title
,
251 : wxFrame(parent
, id
, title
, pos
, size
, style
)
253 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
254 m_smallToolbar
= FALSE
;
256 // Give it a status line
260 SetIcon(wxICON(mondrian
));
263 wxMenu
*tbarMenu
= new wxMenu
;
264 tbarMenu
->Append(IDM_TOOLBAR_TOGGLETOOLBAR
, "&Toggle toolbar", "Change the toolbar kind");
265 tbarMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, "&Enable print button", "");
266 tbarMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, "Toggle &help button", "");
268 wxMenu
*fileMenu
= new wxMenu
;
269 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
271 wxMenu
*helpMenu
= new wxMenu
;
272 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
274 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
276 menuBar
->Append(fileMenu
, "&File");
277 menuBar
->Append(tbarMenu
, "&Toolbar");
278 menuBar
->Append(helpMenu
, "&Help");
280 // Associate the menu bar with the frame
283 // Create the toolbar
284 wxToolBar
*tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
285 wxTB_FLAT
| wxTB_DOCKABLE
,
288 tbar
->SetMargins( 2, 2 );
290 wxGetApp().InitToolbar(tbar
);
293 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
295 // delete and recreate the toolbar
296 wxToolBar
*tbar
= GetToolBar();
300 tbar
= CreateToolBar(wxNO_BORDER
| wxTB_HORIZONTAL
|
301 wxTB_FLAT
| wxTB_DOCKABLE
,
304 m_smallToolbar
= !m_smallToolbar
;
305 wxGetApp().InitToolbar(tbar
, m_smallToolbar
);
308 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
313 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
315 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
318 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
321 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
322 m_textWindow
->WriteText( str
);
324 if (event
.GetId() == wxID_HELP
)
326 if ( event
.GetExtraLong() != 0 )
327 m_textWindow
->WriteText( _T("Help button down now.\n") );
329 m_textWindow
->WriteText( _T("Help button up now.\n") );
332 if (event
.GetId() == wxID_COPY
)
337 if (event
.GetId() == wxID_CUT
)
343 void MyFrame::DoEnablePrint()
345 wxToolBar
*tb
= GetToolBar();
346 if (tb
->GetToolEnabled(wxID_PRINT
))
347 tb
->EnableTool( wxID_PRINT
, FALSE
);
349 tb
->EnableTool( wxID_PRINT
, TRUE
);
352 void MyFrame::DoToggleHelp()
354 wxToolBar
*tb
= GetToolBar();
355 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
358 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
360 if (event
.GetSelection() > -1)
363 str
.Printf(_T("This is tool number %d"), event
.GetSelection());