]> git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
fixed documentation of wxList::Clear behavior after DeleteContents(TRUE)
[wxWidgets.git] / samples / minimal / minimal.cpp
1 /*
2 Test for menu swapping bug (?) with GTK 2.1.13
3 */
4
5
6
7 /////////////////////////////////////////////////////////////////////////////
8 // Name: minimal.cpp
9 // Purpose: Minimal wxWindows sample
10 // Author: Julian Smart
11 // Modified by:
12 // Created: 04/01/98
13 // RCS-ID: $Id$
14 // Copyright: (c) Julian Smart
15 // Licence: wxWindows licence
16 /////////////////////////////////////////////////////////////////////////////
17
18 // ============================================================================
19 // declarations
20 // ============================================================================
21
22 // ----------------------------------------------------------------------------
23 // headers
24 // ----------------------------------------------------------------------------
25 #ifdef __GNUG__
26 #pragma implementation "minimal.cpp"
27 #pragma interface "minimal.cpp"
28 #endif
29
30 // For compilers that support precompilation, includes "wx/wx.h".
31 #include "wx/wxprec.h"
32
33 #ifdef __BORLANDC__
34 #pragma hdrstop
35 #endif
36
37 // for all others, include the necessary headers (this file is usually all you
38 // need because it includes almost all "standard" wxWindows headers
39 #ifndef WX_PRECOMP
40 #include "wx/wx.h"
41 #endif
42
43
44 // ----------------------------------------------------------------------------
45 // Global variables
46 // ----------------------------------------------------------------------------
47 static wxMenuBar *bar[2];
48 static int current_bar;
49
50 // ----------------------------------------------------------------------------
51 // ressources
52 // ----------------------------------------------------------------------------
53 // the application icon
54 #if defined(__WXGTK__) || defined(__WXMOTIF__)
55 #include "mondrian.xpm"
56 #endif
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp : public wxApp
64 {
65 public:
66 // override base class virtuals
67 // ----------------------------
68
69 // this one is called on application startup and is a good place for the app
70 // initialization (doing it here and not in the ctor allows to have an error
71 // return: if OnInit() returns false, the application terminates)
72 virtual bool OnInit();
73 };
74
75 // Define a new frame type: this is going to be our main frame
76 class MyFrame : public wxFrame
77 {
78 public:
79 // ctor(s)
80 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
81
82 // event handlers (these functions should _not_ be virtual)
83 void OnQuit(wxCommandEvent& event);
84 void OnAbout(wxCommandEvent& event);
85 void OnSwapMenus(wxCommandEvent& event);
86 void OnReplaceMenu(wxCommandEvent& event);
87 void OnRemoveInsertMenu(wxCommandEvent& event);
88
89 private:
90 // any class wishing to process wxWindows events must use this macro
91 DECLARE_EVENT_TABLE()
92 };
93
94 // ----------------------------------------------------------------------------
95 // constants
96 // ----------------------------------------------------------------------------
97
98 // IDs for the controls and the menu commands
99 enum
100 {
101 // menu items
102 Minimal_Quit = 1,
103 Minimal_About,
104 Minimal_SwapMenus,
105 Minimal_ReplaceMenu,
106 Minimal_RemoveInsertMenu
107 };
108
109 // ----------------------------------------------------------------------------
110 // event tables and other macros for wxWindows
111 // ----------------------------------------------------------------------------
112
113 // the event tables connect the wxWindows events with the functions (event
114 // handlers) which process them. It can be also done at run-time, but for the
115 // simple menu events like this the static method is much simpler.
116 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
117 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
118 EVT_MENU(Minimal_About, MyFrame::OnAbout)
119 EVT_MENU(Minimal_SwapMenus, MyFrame::OnSwapMenus)
120 EVT_MENU(Minimal_ReplaceMenu, MyFrame::OnReplaceMenu)
121 EVT_MENU(Minimal_RemoveInsertMenu, MyFrame::OnRemoveInsertMenu)
122 END_EVENT_TABLE()
123
124 // Create a new application object: this macro will allow wxWindows to create
125 // the application object during program execution (it's better than using a
126 // static object for many reasons) and also declares the accessor function
127 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
128 // not wxApp)
129 IMPLEMENT_APP(MyApp)
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 wxString IntTowxString(int number)
136 {
137 return(wxString(IntToString(number)));
138 }
139
140 wxMenu *GetFileMenu(int menu_number)
141 {
142 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
143 menuFile->Append(Minimal_Quit, "E&xit" + IntTowxString(menu_number) +
144 "\tAlt-X", "Quit this program");
145 menuFile->Append(Minimal_SwapMenus, "&SwapMenus" + IntTowxString(menu_number)
146 + "\tAlt-S", "Swap Menus");
147 menuFile->Append(Minimal_ReplaceMenu, "&ReplaceMenu" +
148 IntTowxString(menu_number) + "\tAlt-R", "Replace Menu");
149 menuFile->Append(Minimal_RemoveInsertMenu, "&RemoveInsertMenu" +
150 IntTowxString(menu_number) + "\tAlt-I", "Remove Then Insert Menu");
151
152 return(menuFile);
153 }
154
155 // ----------------------------------------------------------------------------
156 // the application class
157 // ----------------------------------------------------------------------------
158
159 // `Main program' equivalent: the program execution "starts" here
160 bool MyApp::OnInit()
161 {
162 // create the main application window
163 MyFrame *frame = new MyFrame("Minimal wxWindows App",
164 wxPoint(50, 50), wxSize(450, 340));
165
166 // and show it (the frames, unlike simple controls, are not shown when
167 // created initially)
168 frame->Show(TRUE);
169
170 // success: wxApp::OnRun() will be called which will enter the main message
171 // loop and the application will run. If we returned FALSE here, the
172 // application would exit immediately.
173 return TRUE;
174 }
175
176 // ----------------------------------------------------------------------------
177 // main frame
178 // ----------------------------------------------------------------------------
179
180 // frame constructor
181 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
182 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
183 {
184 #ifdef __WXMAC__
185 // we need this in order to allow the about menu relocation, since ABOUT is
186 // not the default id of the about menu
187 wxApp::s_macAboutMenuItemId = Minimal_About;
188 #endif
189
190 // set the frame icon
191 SetIcon(wxICON(mondrian));
192
193 bar[0] = new wxMenuBar();
194
195 wxMenu *menuFile0 = GetFileMenu(0);
196
197 wxMenu *helpMenu0 = new wxMenu;
198 helpMenu0->Append(Minimal_About, "&About0...\tCtrl-A",
199 "Show about dialog");
200
201 bar[0]->Append(menuFile0, "&File0");
202 bar[0]->Append(helpMenu0, "&Help0");
203
204
205 bar[1] = new wxMenuBar();
206
207 wxMenu *menuFile1 = GetFileMenu(1);
208
209 wxMenu *helpMenu1 = new wxMenu;
210 helpMenu1->Append(Minimal_About, "&About1...\tCtrl-A",
211 "Show about dialog");
212
213 bar[1]->Append(menuFile1, "&File1");
214 bar[1]->Append(helpMenu1, "&Help1");
215
216 current_bar = 1;
217
218 // ... and attach this menu bar to the frame
219 SetMenuBar(bar[current_bar]);
220
221 #if wxUSE_STATUSBAR
222 // create a status bar just for fun (by default with 1 pane only)
223 CreateStatusBar(2);
224 SetStatusText("Welcome to wxWindows!");
225 #endif // wxUSE_STATUSBAR
226 }
227
228
229 // event handlers
230
231 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
232 {
233 // TRUE is to force the frame to close
234 Close(TRUE);
235 }
236
237 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
238 {
239 wxString msg;
240 msg.Printf( _T("This is the about dialog of minimal sample.\n")
241 _T("Welcome to %s"), wxVERSION_STRING);
242
243 wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
244 }
245
246 void MyFrame::OnSwapMenus(wxCommandEvent& WXUNUSED(event))
247 {
248 // Change the menu set around
249 current_bar = 1 - current_bar;
250 SetMenuBar(bar[current_bar]);
251 }
252
253 void MyFrame::OnReplaceMenu(wxCommandEvent& WXUNUSED(event))
254 {
255 wxMenuBar *curr_bar = bar[current_bar];
256 wxMenu *menu = GetFileMenu(3);
257 wxString title = "&File3";
258
259 // Replace the first menu with the same thing
260 int pos = 1;
261 if (pos != wxNOT_FOUND)
262 {
263 curr_bar->Replace(pos, menu, title);
264 // SetMenuBar(curr_bar);
265 }
266 }
267
268
269 void MyFrame::OnRemoveInsertMenu(wxCommandEvent& WXUNUSED(event))
270 {
271 wxMenuBar *curr_bar = bar[current_bar];
272 wxMenu *menu = GetFileMenu(current_bar);
273 wxString title = "&File3";
274
275 // Remove the first menu then insert it back in
276 int pos = 1;
277 if (pos != wxNOT_FOUND)
278 {
279 curr_bar->Remove(pos);
280 if (curr_bar->GetMenuCount() != 0)
281 curr_bar->Insert(pos, menu, title);
282 else
283 curr_bar->Append(menu, title);
284
285 SetMenuBar(curr_bar);
286 }
287 }
288
289
290