]>
Commit | Line | Data |
---|---|---|
14d1ccd8 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: wxToolBar sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/toolbar.h" | |
8bbe427f VZ |
24 | #include <wx/log.h> |
25 | ||
14d1ccd8 JS |
26 | #include "test.h" |
27 | ||
a4294b78 | 28 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
47908e25 RR |
29 | #include "mondrian.xpm" |
30 | #include "bitmaps/new.xpm" | |
31 | #include "bitmaps/open.xpm" | |
32 | #include "bitmaps/save.xpm" | |
33 | #include "bitmaps/copy.xpm" | |
34 | #include "bitmaps/cut.xpm" | |
35 | // #include "bitmaps/paste.xpm" | |
36 | #include "bitmaps/print.xpm" | |
37 | #include "bitmaps/preview.xpm" | |
38 | #include "bitmaps/help.xpm" | |
39 | #endif | |
40 | ||
14d1ccd8 JS |
41 | IMPLEMENT_APP(MyApp) |
42 | ||
14d1ccd8 JS |
43 | // The `main program' equivalent, creating the windows and returning the |
44 | // main frame | |
45 | bool MyApp::OnInit(void) | |
46 | { | |
47 | // Create the main frame window | |
c67daf87 | 48 | MyFrame* frame = new MyFrame((wxFrame *) NULL, -1, (const wxString) "wxToolBar Sample", |
81d66cf3 | 49 | wxPoint(100, 100), wxSize(450, 300)); |
14d1ccd8 JS |
50 | |
51 | // Give it a status line | |
52 | frame->CreateStatusBar(); | |
53 | ||
54 | // Give it an icon | |
8bbe427f | 55 | frame->SetIcon(wxICON(mondrian)); |
14d1ccd8 JS |
56 | |
57 | // Make a menubar | |
58 | wxMenu *fileMenu = new wxMenu; | |
342b6a2f | 59 | fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" ); |
14d1ccd8 JS |
60 | |
61 | wxMenu *helpMenu = new wxMenu; | |
342b6a2f | 62 | helpMenu->Append(wxID_HELP, "&About", "About toolbar sample"); |
14d1ccd8 | 63 | |
3502e687 | 64 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); |
14d1ccd8 JS |
65 | |
66 | menuBar->Append(fileMenu, "&File"); | |
67 | menuBar->Append(helpMenu, "&Help"); | |
68 | ||
69 | // Associate the menu bar with the frame | |
70 | frame->SetMenuBar(menuBar); | |
71 | ||
72 | // Create the toolbar | |
2a47d3c1 | 73 | frame->CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT|wxTB_DOCKABLE, ID_TOOLBAR); |
1144d24d RR |
74 | |
75 | frame->GetToolBar()->SetMargins( 2, 2 ); | |
14d1ccd8 | 76 | |
81d66cf3 | 77 | InitToolbar(frame->GetToolBar()); |
14d1ccd8 | 78 | |
13437238 JS |
79 | // Force a resize. This should probably be replaced by a call to a wxFrame |
80 | // function that lays out default decorations and the remaining content window. | |
cb43b372 RR |
81 | wxSizeEvent event(wxSize(-1, -1), frame->GetId()); |
82 | frame->OnSize(event); | |
14d1ccd8 JS |
83 | frame->Show(TRUE); |
84 | ||
85 | frame->SetStatusText("Hello, wxWindows"); | |
86 | ||
87 | SetTopWindow(frame); | |
88 | ||
89 | return TRUE; | |
90 | } | |
91 | ||
13437238 | 92 | bool MyApp::InitToolbar(wxToolBar* toolBar) |
14d1ccd8 JS |
93 | { |
94 | // Set up toolbar | |
95 | wxBitmap* toolBarBitmaps[8]; | |
96 | ||
97 | #ifdef __WXMSW__ | |
98 | toolBarBitmaps[0] = new wxBitmap("icon1"); | |
99 | toolBarBitmaps[1] = new wxBitmap("icon2"); | |
100 | toolBarBitmaps[2] = new wxBitmap("icon3"); | |
101 | toolBarBitmaps[3] = new wxBitmap("icon4"); | |
102 | toolBarBitmaps[4] = new wxBitmap("icon5"); | |
103 | toolBarBitmaps[5] = new wxBitmap("icon6"); | |
104 | toolBarBitmaps[6] = new wxBitmap("icon7"); | |
105 | toolBarBitmaps[7] = new wxBitmap("icon8"); | |
47908e25 RR |
106 | #else |
107 | toolBarBitmaps[0] = new wxBitmap( new_xpm ); | |
108 | toolBarBitmaps[1] = new wxBitmap( open_xpm ); | |
109 | toolBarBitmaps[2] = new wxBitmap( save_xpm ); | |
110 | toolBarBitmaps[3] = new wxBitmap( copy_xpm ); | |
111 | toolBarBitmaps[4] = new wxBitmap( cut_xpm ); | |
112 | // toolBarBitmaps[5] = new wxBitmap( paste_xpm ); | |
113 | toolBarBitmaps[5] = new wxBitmap( preview_xpm ); | |
114 | toolBarBitmaps[6] = new wxBitmap( print_xpm ); | |
115 | toolBarBitmaps[7] = new wxBitmap( help_xpm ); | |
14d1ccd8 JS |
116 | #endif |
117 | ||
118 | #ifdef __WXMSW__ | |
119 | int width = 24; | |
120 | #else | |
121 | int width = 16; | |
122 | #endif | |
14d1ccd8 JS |
123 | int currentX = 5; |
124 | ||
a4294b78 | 125 | toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file"); |
14d1ccd8 | 126 | currentX += width + 5; |
a4294b78 | 127 | toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file"); |
14d1ccd8 | 128 | currentX += width + 5; |
a4294b78 | 129 | toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file"); |
14d1ccd8 | 130 | currentX += width + 5; |
13437238 | 131 | toolBar->AddSeparator(); |
e179bd65 | 132 | toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Disable/Enable print button"); |
14d1ccd8 | 133 | currentX += width + 5; |
e179bd65 | 134 | toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button"); |
14d1ccd8 | 135 | currentX += width + 5; |
a4294b78 | 136 | toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste"); |
14d1ccd8 | 137 | currentX += width + 5; |
13437238 | 138 | toolBar->AddSeparator(); |
a4294b78 | 139 | toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print"); |
14d1ccd8 | 140 | currentX += width + 5; |
13437238 | 141 | toolBar->AddSeparator(); |
85eb36c2 | 142 | toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), *(toolBarBitmaps[6]), TRUE, currentX, -1, (wxObject *) NULL, "Help"); |
b98d804b RR |
143 | |
144 | toolBar->EnableTool( wxID_PRINT, FALSE ); | |
14d1ccd8 | 145 | |
81d66cf3 | 146 | toolBar->Realize(); |
14d1ccd8 JS |
147 | |
148 | // Can delete the bitmaps since they're reference counted | |
149 | int i; | |
150 | for (i = 0; i < 8; i++) | |
151 | delete toolBarBitmaps[i]; | |
13437238 JS |
152 | |
153 | return TRUE; | |
14d1ccd8 JS |
154 | } |
155 | ||
13437238 JS |
156 | // wxID_HELP will be processed for the 'About' menu and the toolbar help button. |
157 | ||
158 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
159 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
13437238 | 160 | EVT_CLOSE(MyFrame::OnCloseWindow) |
e179bd65 | 161 | EVT_MENU(-1, MyFrame::OnToolLeftClick) |
777553d2 | 162 | EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter) |
13437238 JS |
163 | END_EVENT_TABLE() |
164 | ||
165 | // Define my frame constructor | |
166 | MyFrame::MyFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos, | |
167 | const wxSize& size, long style): | |
1d5b7a0b | 168 | wxFrame(parent, id, title, pos, size, style) |
14d1ccd8 | 169 | { |
e179bd65 | 170 | m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE); |
14d1ccd8 JS |
171 | } |
172 | ||
47908e25 | 173 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
14d1ccd8 | 174 | { |
13437238 | 175 | Close(TRUE); |
14d1ccd8 JS |
176 | } |
177 | ||
47908e25 | 178 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
14d1ccd8 | 179 | { |
1d5b7a0b | 180 | (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar"); |
13437238 | 181 | } |
14d1ccd8 | 182 | |
13437238 JS |
183 | // Define the behaviour for the frame closing |
184 | // - must delete all frames except for the main one. | |
47908e25 | 185 | void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
13437238 | 186 | { |
e179bd65 | 187 | Destroy(); |
13437238 JS |
188 | } |
189 | ||
190 | void MyFrame::OnToolLeftClick(wxCommandEvent& event) | |
191 | { | |
e179bd65 RR |
192 | wxString str; |
193 | str.Printf( _T("Clicked on tool %d\n"), event.GetId()); | |
194 | m_textWindow->WriteText( str ); | |
195 | ||
196 | if (event.GetId() == wxID_HELP) | |
197 | { | |
198 | if ((bool)event.GetExtraLong()) | |
199 | m_textWindow->WriteText( _T("Help button down now.\n") ); | |
200 | else | |
201 | m_textWindow->WriteText( _T("Help button up now.\n") ); | |
202 | } | |
203 | ||
204 | if (event.GetId() == wxID_COPY) | |
205 | { | |
206 | wxToolBar *tb = GetToolBar(); | |
207 | if (tb->GetToolEnabled(wxID_PRINT)) | |
208 | tb->EnableTool( wxID_PRINT, FALSE ); | |
209 | else | |
210 | tb->EnableTool( wxID_PRINT, TRUE ); | |
211 | } | |
212 | ||
213 | if (event.GetId() == wxID_CUT) | |
214 | { | |
215 | wxToolBar *tb = GetToolBar(); | |
216 | tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) ); | |
217 | } | |
14d1ccd8 JS |
218 | } |
219 | ||
13437238 JS |
220 | void MyFrame::OnToolEnter(wxCommandEvent& event) |
221 | { | |
81d66cf3 | 222 | if (event.GetSelection() > -1) |
13437238 JS |
223 | { |
224 | wxString str; | |
58dea4b0 | 225 | str.Printf(_T("This is tool number %d"), event.GetSelection()); |
13437238 JS |
226 | SetStatusText(str); |
227 | } | |
228 | else | |
229 | SetStatusText(""); | |
230 | } | |
14d1ccd8 | 231 |