]>
git.saurik.com Git - wxWidgets.git/blob - samples/minimal/minimal.cpp
2 Test for menu swapping bug (?) with GTK 2.1.13
7 /////////////////////////////////////////////////////////////////////////////
9 // Purpose: Minimal wxWindows sample
10 // Author: Julian Smart
14 // Copyright: (c) Julian Smart
15 // Licence: wxWindows licence
16 /////////////////////////////////////////////////////////////////////////////
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #pragma implementation "minimal.cpp"
27 #pragma interface "minimal.cpp"
30 // For compilers that support precompilation, includes "wx/wx.h".
31 #include "wx/wxprec.h"
37 // for all others, include the necessary headers (this file is usually all you
38 // need because it includes almost all "standard" wxWindows headers
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
47 static wxMenuBar
*bar
[2];
48 static int current_bar
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
53 // the application icon
54 #if defined(__WXGTK__) || defined(__WXMOTIF__)
55 #include "mondrian.xpm"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 // ----------------------------
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();
75 // Define a new frame type: this is going to be our main frame
76 class MyFrame
: public wxFrame
80 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
90 // any class wishing to process wxWindows events must use this macro
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // IDs for the controls and the menu commands
106 Minimal_RemoveInsertMenu
109 // ----------------------------------------------------------------------------
110 // event tables and other macros for wxWindows
111 // ----------------------------------------------------------------------------
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
)
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
131 // ============================================================================
133 // ============================================================================
135 wxString
IntTowxString(int number
)
137 return(wxString(IntToString(number
)));
140 wxMenu
*GetFileMenu(int menu_number
)
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");
155 // ----------------------------------------------------------------------------
156 // the application class
157 // ----------------------------------------------------------------------------
159 // `Main program' equivalent: the program execution "starts" here
162 // create the main application window
163 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
164 wxPoint(50, 50), wxSize(450, 340));
166 // and show it (the frames, unlike simple controls, are not shown when
167 // created initially)
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.
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
181 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
182 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
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
;
190 // set the frame icon
191 SetIcon(wxICON(mondrian
));
193 bar
[0] = new wxMenuBar();
195 wxMenu
*menuFile0
= GetFileMenu(0);
197 wxMenu
*helpMenu0
= new wxMenu
;
198 helpMenu0
->Append(Minimal_About
, "&About0...\tCtrl-A",
199 "Show about dialog");
201 bar
[0]->Append(menuFile0
, "&File0");
202 bar
[0]->Append(helpMenu0
, "&Help0");
205 bar
[1] = new wxMenuBar();
207 wxMenu
*menuFile1
= GetFileMenu(1);
209 wxMenu
*helpMenu1
= new wxMenu
;
210 helpMenu1
->Append(Minimal_About
, "&About1...\tCtrl-A",
211 "Show about dialog");
213 bar
[1]->Append(menuFile1
, "&File1");
214 bar
[1]->Append(helpMenu1
, "&Help1");
218 // ... and attach this menu bar to the frame
219 SetMenuBar(bar
[current_bar
]);
222 // create a status bar just for fun (by default with 1 pane only)
224 SetStatusText("Welcome to wxWindows!");
225 #endif // wxUSE_STATUSBAR
231 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
233 // TRUE is to force the frame to close
237 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
240 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
241 _T("Welcome to %s"), wxVERSION_STRING
);
243 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
246 void MyFrame::OnSwapMenus(wxCommandEvent
& WXUNUSED(event
))
248 // Change the menu set around
249 current_bar
= 1 - current_bar
;
250 SetMenuBar(bar
[current_bar
]);
253 void MyFrame::OnReplaceMenu(wxCommandEvent
& WXUNUSED(event
))
255 wxMenuBar
*curr_bar
= bar
[current_bar
];
256 wxMenu
*menu
= GetFileMenu(3);
257 wxString title
= "&File3";
259 // Replace the first menu with the same thing
261 if (pos
!= wxNOT_FOUND
)
263 curr_bar
->Replace(pos
, menu
, title
);
264 // SetMenuBar(curr_bar);
269 void MyFrame::OnRemoveInsertMenu(wxCommandEvent
& WXUNUSED(event
))
271 wxMenuBar
*curr_bar
= bar
[current_bar
];
272 wxMenu
*menu
= GetFileMenu(current_bar
);
273 wxString title
= "&File3";
275 // Remove the first menu then insert it back in
277 if (pos
!= wxNOT_FOUND
)
279 curr_bar
->Remove(pos
);
280 if (curr_bar
->GetMenuCount() != 0)
281 curr_bar
->Insert(pos
, menu
, title
);
283 curr_bar
->Append(menu
, title
);
285 SetMenuBar(curr_bar
);