menu.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/menu.cpp
  3. // Purpose: wxMenu/wxMenuBar sample
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 01.11.99
  7. // Copyright: (c) 1999 Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/app.h"
  23. #include "wx/frame.h"
  24. #include "wx/menu.h"
  25. #include "wx/msgdlg.h"
  26. #include "wx/log.h"
  27. #include "wx/textctrl.h"
  28. #include "wx/textdlg.h"
  29. #endif
  30. #if !wxUSE_MENUS
  31. // nice try...
  32. #error "menu sample requires wxUSE_MENUS=1"
  33. #endif // wxUSE_MENUS
  34. // not all ports have support for EVT_CONTEXT_MENU yet, don't define
  35. // USE_CONTEXT_MENU for those which don't
  36. #if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__)
  37. #define USE_CONTEXT_MENU 0
  38. #else
  39. #define USE_CONTEXT_MENU 1
  40. #endif
  41. // this sample is useful when a new port is developed
  42. // and usually a new port has majority of flags turned off
  43. #if wxUSE_LOG && wxUSE_TEXTCTRL
  44. #define USE_LOG_WINDOW 1
  45. #else
  46. #define USE_LOG_WINDOW 0
  47. #endif
  48. #include "copy.xpm"
  49. #ifndef wxHAS_IMAGES_IN_RESOURCES
  50. #include "../sample.xpm"
  51. #endif
  52. // ----------------------------------------------------------------------------
  53. // classes
  54. // ----------------------------------------------------------------------------
  55. // Define a new application
  56. class MyApp: public wxApp
  57. {
  58. public:
  59. bool OnInit();
  60. };
  61. // Define a new frame
  62. class MyFrame: public wxFrame
  63. {
  64. public:
  65. MyFrame();
  66. virtual ~MyFrame();
  67. void LogMenuEvent(const wxCommandEvent& event);
  68. protected:
  69. void OnQuit(wxCommandEvent& event);
  70. #if USE_LOG_WINDOW
  71. void OnClearLog(wxCommandEvent& event);
  72. void OnClearLogUpdateUI(wxUpdateUIEvent& event);
  73. #endif // USE_LOG_WINDOW
  74. void OnShowDialog(wxCommandEvent& event);
  75. void OnAbout(wxCommandEvent& event);
  76. void OnDummy(wxCommandEvent& event);
  77. void OnAppendMenuItem(wxCommandEvent& event);
  78. void OnAppendSubMenu(wxCommandEvent& event);
  79. void OnDeleteMenuItem(wxCommandEvent& event);
  80. void OnDeleteSubMenu(wxCommandEvent& event);
  81. void OnInsertMenuItem(wxCommandEvent& event);
  82. void OnCheckMenuItem(wxCommandEvent& event);
  83. void OnEnableMenuItem(wxCommandEvent& event);
  84. void OnGetLabelMenuItem(wxCommandEvent& event);
  85. #if wxUSE_TEXTDLG
  86. void OnSetLabelMenuItem(wxCommandEvent& event);
  87. #endif
  88. void OnGetMenuItemInfo(wxCommandEvent& event);
  89. #if wxUSE_TEXTDLG
  90. void OnFindMenuItem(wxCommandEvent& event);
  91. #endif
  92. void OnAppendMenu(wxCommandEvent& event);
  93. void OnInsertMenu(wxCommandEvent& event);
  94. void OnDeleteMenu(wxCommandEvent& event);
  95. void OnToggleMenu(wxCommandEvent& event);
  96. void OnEnableMenu(wxCommandEvent& event);
  97. void OnGetLabelMenu(wxCommandEvent& event);
  98. void OnSetLabelMenu(wxCommandEvent& event);
  99. #if wxUSE_TEXTDLG
  100. void OnFindMenu(wxCommandEvent& event);
  101. #endif
  102. void OnTestNormal(wxCommandEvent& event);
  103. void OnTestCheck(wxCommandEvent& event);
  104. void OnTestRadio(wxCommandEvent& event);
  105. void OnUpdateSubMenuNormal(wxUpdateUIEvent& event);
  106. void OnUpdateSubMenuCheck(wxUpdateUIEvent& event);
  107. void OnUpdateSubMenuRadio(wxUpdateUIEvent& event);
  108. #if USE_CONTEXT_MENU
  109. void OnContextMenu(wxContextMenuEvent& event);
  110. #else
  111. void OnRightUp(wxMouseEvent& event)
  112. { ShowContextMenu(event.GetPosition()); }
  113. #endif
  114. void OnMenuOpen(wxMenuEvent& event)
  115. {
  116. #if USE_LOG_WINDOW
  117. LogMenuOpenCloseOrHighlight(event, wxT("opened")); event.Skip();
  118. #endif
  119. }
  120. void OnMenuClose(wxMenuEvent& event)
  121. {
  122. #if USE_LOG_WINDOW
  123. LogMenuOpenCloseOrHighlight(event, wxT("closed")); event.Skip();
  124. #endif
  125. }
  126. void OnMenuHighlight(wxMenuEvent& event)
  127. {
  128. #if USE_LOG_WINDOW
  129. LogMenuOpenCloseOrHighlight(event, wxT("highlighted")); event.Skip();
  130. #endif
  131. }
  132. void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
  133. void OnSize(wxSizeEvent& event);
  134. private:
  135. #if USE_LOG_WINDOW
  136. void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what);
  137. #endif
  138. void ShowContextMenu(const wxPoint& pos);
  139. wxMenu *CreateDummyMenu(wxString *title);
  140. wxMenuItem *GetLastMenuItem() const;
  141. // the menu previously detached from the menubar (may be NULL)
  142. wxMenu *m_menu;
  143. // the count of dummy menus already created
  144. size_t m_countDummy;
  145. #if USE_LOG_WINDOW
  146. // the control used for logging
  147. wxTextCtrl *m_textctrl;
  148. #endif
  149. // the previous log target
  150. wxLog *m_logOld;
  151. wxDECLARE_EVENT_TABLE();
  152. };
  153. class MyDialog : public wxDialog
  154. {
  155. public:
  156. MyDialog(wxWindow* parent);
  157. #if USE_CONTEXT_MENU
  158. void OnContextMenu(wxContextMenuEvent& event);
  159. #else
  160. void OnRightUp(wxMouseEvent& event)
  161. { ShowContextMenu(event.GetPosition()); }
  162. #endif
  163. void OnMenuOpen(wxMenuEvent& event)
  164. {
  165. #if USE_LOG_WINDOW
  166. LogMenuOpenCloseOrHighlight(event, wxT("opened")); event.Skip();
  167. #endif
  168. }
  169. void OnMenuClose(wxMenuEvent& event)
  170. {
  171. #if USE_LOG_WINDOW
  172. LogMenuOpenCloseOrHighlight(event, wxT("closed")); event.Skip();
  173. #endif
  174. }
  175. void OnMenuHighlight(wxMenuEvent& event)
  176. {
  177. #if USE_LOG_WINDOW
  178. LogMenuOpenCloseOrHighlight(event, wxT("highlighted")); event.Skip();
  179. #endif
  180. }
  181. private:
  182. #if USE_LOG_WINDOW
  183. void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what);
  184. #endif
  185. void ShowContextMenu(const wxPoint& pos);
  186. #if USE_LOG_WINDOW
  187. // the control used for logging
  188. wxTextCtrl *m_textctrl;
  189. #endif
  190. wxDECLARE_EVENT_TABLE();
  191. };
  192. // A small helper class which intercepts all menu events and logs them
  193. class MyEvtHandler : public wxEvtHandler
  194. {
  195. public:
  196. MyEvtHandler(MyFrame *frame) { m_frame = frame; }
  197. void OnMenuEvent(wxCommandEvent& event)
  198. {
  199. m_frame->LogMenuEvent(event);
  200. event.Skip();
  201. }
  202. private:
  203. MyFrame *m_frame;
  204. wxDECLARE_EVENT_TABLE();
  205. };
  206. // ----------------------------------------------------------------------------
  207. // constants
  208. // ----------------------------------------------------------------------------
  209. enum
  210. {
  211. Menu_File_Quit = wxID_EXIT,
  212. #if USE_LOG_WINDOW
  213. Menu_File_ClearLog = 100,
  214. #endif
  215. Menu_File_ShowDialog,
  216. Menu_MenuBar_Toggle = 200,
  217. Menu_MenuBar_Append,
  218. Menu_MenuBar_Insert,
  219. Menu_MenuBar_Delete,
  220. Menu_MenuBar_Enable,
  221. Menu_MenuBar_GetLabel,
  222. #if wxUSE_TEXTDLG
  223. Menu_MenuBar_SetLabel,
  224. Menu_MenuBar_FindMenu,
  225. #endif
  226. Menu_Menu_Append = 300,
  227. Menu_Menu_AppendSub,
  228. Menu_Menu_Insert,
  229. Menu_Menu_Delete,
  230. Menu_Menu_DeleteSub,
  231. Menu_Menu_Enable,
  232. Menu_Menu_Check,
  233. Menu_Menu_GetLabel,
  234. #if wxUSE_TEXTDLG
  235. Menu_Menu_SetLabel,
  236. #endif
  237. Menu_Menu_GetInfo,
  238. #if wxUSE_TEXTDLG
  239. Menu_Menu_FindItem,
  240. #endif
  241. Menu_Test_Normal = 400,
  242. Menu_Test_Check,
  243. Menu_Test_Radio1,
  244. Menu_Test_Radio2,
  245. Menu_Test_Radio3,
  246. Menu_SubMenu = 450,
  247. Menu_SubMenu_Normal,
  248. Menu_SubMenu_Check,
  249. Menu_SubMenu_Radio1,
  250. Menu_SubMenu_Radio2,
  251. Menu_SubMenu_Radio3,
  252. Menu_Dummy_First = 500,
  253. Menu_Dummy_Second,
  254. Menu_Dummy_Third,
  255. Menu_Dummy_Fourth,
  256. Menu_Dummy_Last,
  257. Menu_Help_About = wxID_ABOUT,
  258. Menu_Popup_ToBeDeleted = 2000,
  259. Menu_Popup_ToBeGreyed,
  260. Menu_Popup_ToBeChecked,
  261. Menu_Popup_Submenu,
  262. Menu_PopupChoice,
  263. Menu_Max
  264. };
  265. // ----------------------------------------------------------------------------
  266. // event tables
  267. // ----------------------------------------------------------------------------
  268. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  269. EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
  270. #if USE_LOG_WINDOW
  271. EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
  272. EVT_UPDATE_UI(Menu_File_ClearLog, MyFrame::OnClearLogUpdateUI)
  273. #endif
  274. EVT_MENU(Menu_File_ShowDialog, MyFrame::OnShowDialog)
  275. EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
  276. EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu)
  277. EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu)
  278. EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
  279. EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
  280. EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
  281. EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
  282. #if wxUSE_TEXTDLG
  283. EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
  284. EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu)
  285. #endif
  286. EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem)
  287. EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu)
  288. EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem)
  289. EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem)
  290. EVT_MENU(Menu_Menu_DeleteSub, MyFrame::OnDeleteSubMenu)
  291. EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
  292. EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
  293. EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
  294. #if wxUSE_TEXTDLG
  295. EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
  296. #endif
  297. EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
  298. #if wxUSE_TEXTDLG
  299. EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem)
  300. #endif
  301. EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal)
  302. EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck)
  303. EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio)
  304. EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio)
  305. EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio)
  306. EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal)
  307. EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck)
  308. EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio)
  309. EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio)
  310. EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio)
  311. EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
  312. EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
  313. #if USE_CONTEXT_MENU
  314. EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
  315. #else
  316. EVT_RIGHT_UP(MyFrame::OnRightUp)
  317. #endif
  318. EVT_MENU_OPEN(MyFrame::OnMenuOpen)
  319. EVT_MENU_CLOSE(MyFrame::OnMenuClose)
  320. EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight)
  321. EVT_SIZE(MyFrame::OnSize)
  322. wxEND_EVENT_TABLE()
  323. wxBEGIN_EVENT_TABLE(MyDialog, wxDialog)
  324. #if USE_CONTEXT_MENU
  325. EVT_CONTEXT_MENU(MyDialog::OnContextMenu)
  326. #else
  327. EVT_RIGHT_UP(MyDialog::OnRightUp)
  328. #endif
  329. EVT_MENU_OPEN(MyDialog::OnMenuOpen)
  330. EVT_MENU_CLOSE(MyDialog::OnMenuClose)
  331. EVT_MENU_HIGHLIGHT_ALL(MyDialog::OnMenuHighlight)
  332. wxEND_EVENT_TABLE()
  333. wxBEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
  334. EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent)
  335. wxEND_EVENT_TABLE()
  336. // ============================================================================
  337. // implementation
  338. // ============================================================================
  339. // ----------------------------------------------------------------------------
  340. // MyApp
  341. // ----------------------------------------------------------------------------
  342. IMPLEMENT_APP(MyApp)
  343. // The `main program' equivalent, creating the windows and returning the
  344. // main frame
  345. bool MyApp::OnInit()
  346. {
  347. if ( !wxApp::OnInit() )
  348. return false;
  349. // Create the main frame window
  350. MyFrame* frame = new MyFrame;
  351. frame->Show(true);
  352. #if wxUSE_STATUSBAR
  353. frame->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
  354. #endif // wxUSE_STATUSBAR
  355. return true;
  356. }
  357. // ----------------------------------------------------------------------------
  358. // MyFrame
  359. // ----------------------------------------------------------------------------
  360. // Define my frame constructor
  361. MyFrame::MyFrame()
  362. : wxFrame((wxFrame *)NULL, wxID_ANY, wxT("wxWidgets menu sample"))
  363. {
  364. SetIcon(wxICON(sample));
  365. #if USE_LOG_WINDOW
  366. m_textctrl = NULL;
  367. #endif
  368. m_menu = NULL;
  369. m_countDummy = 0;
  370. m_logOld = NULL;
  371. #if wxUSE_STATUSBAR
  372. CreateStatusBar();
  373. #endif // wxUSE_STATUSBAR
  374. // create the menubar
  375. wxMenu *fileMenu = new wxMenu;
  376. wxMenu *stockSubMenu = new wxMenu;
  377. stockSubMenu->Append(wxID_ADD);
  378. stockSubMenu->Append(wxID_APPLY);
  379. stockSubMenu->Append(wxID_BACKWARD);
  380. stockSubMenu->Append(wxID_BOLD);
  381. stockSubMenu->Append(wxID_BOTTOM);
  382. stockSubMenu->Append(wxID_CANCEL);
  383. stockSubMenu->Append(wxID_CDROM);
  384. stockSubMenu->Append(wxID_CLEAR);
  385. stockSubMenu->Append(wxID_CLOSE);
  386. stockSubMenu->Append(wxID_CONVERT);
  387. stockSubMenu->Append(wxID_COPY);
  388. stockSubMenu->Append(wxID_CUT);
  389. stockSubMenu->Append(wxID_DELETE);
  390. stockSubMenu->Append(wxID_DOWN);
  391. stockSubMenu->Append(wxID_EXECUTE);
  392. stockSubMenu->Append(wxID_EXIT);
  393. stockSubMenu->Append(wxID_FIND);
  394. stockSubMenu->Append(wxID_FIRST);
  395. stockSubMenu->Append(wxID_FLOPPY);
  396. stockSubMenu->Append(wxID_FORWARD);
  397. stockSubMenu->Append(wxID_HARDDISK);
  398. stockSubMenu->Append(wxID_HELP);
  399. stockSubMenu->Append(wxID_HOME);
  400. stockSubMenu->Append(wxID_INDENT);
  401. stockSubMenu->Append(wxID_INDEX);
  402. stockSubMenu->Append(wxID_INFO);
  403. stockSubMenu->Append(wxID_ITALIC);
  404. stockSubMenu->Append(wxID_JUMP_TO);
  405. stockSubMenu->Append(wxID_JUSTIFY_CENTER);
  406. stockSubMenu->Append(wxID_JUSTIFY_FILL);
  407. stockSubMenu->Append(wxID_JUSTIFY_LEFT);
  408. stockSubMenu->Append(wxID_JUSTIFY_RIGHT);
  409. stockSubMenu->Append(wxID_LAST);
  410. stockSubMenu->Append(wxID_NETWORK);
  411. stockSubMenu->Append(wxID_NEW);
  412. stockSubMenu->Append(wxID_NO);
  413. stockSubMenu->Append(wxID_OK);
  414. stockSubMenu->Append(wxID_OPEN);
  415. stockSubMenu->Append(wxID_PASTE);
  416. stockSubMenu->Append(wxID_PREFERENCES);
  417. stockSubMenu->Append(wxID_PREVIEW);
  418. stockSubMenu->Append(wxID_PRINT);
  419. stockSubMenu->Append(wxID_PROPERTIES);
  420. stockSubMenu->Append(wxID_REDO);
  421. stockSubMenu->Append(wxID_REFRESH);
  422. stockSubMenu->Append(wxID_REMOVE);
  423. stockSubMenu->Append(wxID_REPLACE);
  424. stockSubMenu->Append(wxID_REVERT_TO_SAVED);
  425. stockSubMenu->Append(wxID_SAVE);
  426. stockSubMenu->Append(wxID_SAVEAS);
  427. stockSubMenu->Append(wxID_SELECT_COLOR);
  428. stockSubMenu->Append(wxID_SELECT_FONT);
  429. stockSubMenu->Append(wxID_SORT_ASCENDING);
  430. stockSubMenu->Append(wxID_SORT_DESCENDING);
  431. stockSubMenu->Append(wxID_SPELL_CHECK);
  432. stockSubMenu->Append(wxID_STOP);
  433. stockSubMenu->Append(wxID_STRIKETHROUGH);
  434. stockSubMenu->Append(wxID_TOP);
  435. stockSubMenu->Append(wxID_UNDELETE);
  436. stockSubMenu->Append(wxID_UNDERLINE);
  437. stockSubMenu->Append(wxID_UNDO);
  438. stockSubMenu->Append(wxID_UNINDENT);
  439. stockSubMenu->Append(wxID_UP);
  440. stockSubMenu->Append(wxID_YES);
  441. stockSubMenu->Append(wxID_ZOOM_100);
  442. stockSubMenu->Append(wxID_ZOOM_FIT);
  443. stockSubMenu->Append(wxID_ZOOM_IN);
  444. stockSubMenu->Append(wxID_ZOOM_OUT);
  445. fileMenu->AppendSubMenu(stockSubMenu, wxT("&Standard items demo"));
  446. #if USE_LOG_WINDOW
  447. wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
  448. wxT("Clear &log\tCtrl-L"));
  449. item->SetBitmap(copy_xpm);
  450. fileMenu->Append(item);
  451. fileMenu->AppendSeparator();
  452. #endif // USE_LOG_WINDOW
  453. fileMenu->Append(Menu_File_ShowDialog, wxT("Show &Dialog\tCtrl-D"),
  454. wxT("Show a dialog"));
  455. fileMenu->AppendSeparator();
  456. fileMenu->Append(Menu_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
  457. wxMenu *menubarMenu = new wxMenu;
  458. menubarMenu->Append(Menu_MenuBar_Append, wxT("&Append menu\tCtrl-A"),
  459. wxT("Append a menu to the menubar"));
  460. menubarMenu->Append(Menu_MenuBar_Insert, wxT("&Insert menu\tCtrl-I"),
  461. wxT("Insert a menu into the menubar"));
  462. menubarMenu->Append(Menu_MenuBar_Delete, wxT("&Delete menu\tCtrl-D"),
  463. wxT("Delete the last menu from the menubar"));
  464. menubarMenu->Append(Menu_MenuBar_Toggle, wxT("&Toggle menu\tCtrl-T"),
  465. wxT("Toggle the first menu in the menubar"), true);
  466. menubarMenu->AppendSeparator();
  467. menubarMenu->Append(Menu_MenuBar_Enable, wxT("&Enable menu\tCtrl-E"),
  468. wxT("Enable or disable the last menu"), true);
  469. menubarMenu->AppendSeparator();
  470. menubarMenu->Append(Menu_MenuBar_GetLabel, wxT("&Get menu label\tCtrl-G"),
  471. wxT("Get the label of the last menu"));
  472. #if wxUSE_TEXTDLG
  473. menubarMenu->Append(Menu_MenuBar_SetLabel, wxT("&Set menu label\tCtrl-S"),
  474. wxT("Change the label of the last menu"));
  475. menubarMenu->AppendSeparator();
  476. menubarMenu->Append(Menu_MenuBar_FindMenu, wxT("&Find menu from label\tCtrl-F"),
  477. wxT("Find a menu by searching for its label"));
  478. #endif
  479. wxMenu* subMenu = new wxMenu;
  480. subMenu->Append(Menu_SubMenu_Normal, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
  481. subMenu->AppendCheckItem(Menu_SubMenu_Check, wxT("&Check submenu item"), wxT("Check submenu item"));
  482. subMenu->AppendRadioItem(Menu_SubMenu_Radio1, wxT("Radio item &1"), wxT("Radio item"));
  483. subMenu->AppendRadioItem(Menu_SubMenu_Radio2, wxT("Radio item &2"), wxT("Radio item"));
  484. subMenu->AppendRadioItem(Menu_SubMenu_Radio3, wxT("Radio item &3"), wxT("Radio item"));
  485. menubarMenu->Append(Menu_SubMenu, wxT("Submenu"), subMenu);
  486. wxMenu *menuMenu = new wxMenu;
  487. menuMenu->Append(Menu_Menu_Append, wxT("&Append menu item\tAlt-A"),
  488. wxT("Append a menu item to the 'Test' menu"));
  489. menuMenu->Append(Menu_Menu_AppendSub, wxT("&Append sub menu\tAlt-S"),
  490. wxT("Append a sub menu to the 'Test' menu"));
  491. menuMenu->Append(Menu_Menu_Insert, wxT("&Insert menu item\tAlt-I"),
  492. wxT("Insert a menu item in head of the 'Test' menu"));
  493. menuMenu->Append(Menu_Menu_Delete, wxT("&Delete menu item\tAlt-D"),
  494. wxT("Delete the last menu item from the 'Test' menu"));
  495. menuMenu->Append(Menu_Menu_DeleteSub, wxT("Delete last &submenu\tAlt-K"),
  496. wxT("Delete the last submenu from the 'Test' menu"));
  497. menuMenu->AppendSeparator();
  498. menuMenu->Append(Menu_Menu_Enable, wxT("&Enable menu item\tAlt-E"),
  499. wxT("Enable or disable the last menu item"), true);
  500. menuMenu->Append(Menu_Menu_Check, wxT("&Check menu item\tAlt-C"),
  501. wxT("Check or uncheck the last menu item"), true);
  502. menuMenu->AppendSeparator();
  503. menuMenu->Append(Menu_Menu_GetInfo, wxT("Get menu item in&fo\tAlt-F"),
  504. wxT("Show the state of the last menu item"));
  505. #if wxUSE_TEXTDLG
  506. menuMenu->Append(Menu_Menu_SetLabel, wxT("Set menu item label\tAlt-L"),
  507. wxT("Set the label of a menu item"));
  508. #endif
  509. #if wxUSE_TEXTDLG
  510. menuMenu->AppendSeparator();
  511. menuMenu->Append(Menu_Menu_FindItem, wxT("Find menu item from label"),
  512. wxT("Find a menu item by searching for its label"));
  513. #endif
  514. wxMenu *testMenu = new wxMenu;
  515. testMenu->Append(Menu_Test_Normal, wxT("&Normal item"));
  516. testMenu->AppendSeparator();
  517. testMenu->AppendCheckItem(Menu_Test_Check, wxT("&Check item"));
  518. testMenu->AppendSeparator();
  519. testMenu->AppendRadioItem(Menu_Test_Radio1, wxT("Radio item &1"));
  520. testMenu->AppendRadioItem(Menu_Test_Radio2, wxT("Radio item &2"));
  521. testMenu->AppendRadioItem(Menu_Test_Radio3, wxT("Radio item &3"));
  522. wxMenu *helpMenu = new wxMenu;
  523. helpMenu->Append(Menu_Help_About, wxT("&About\tF1"), wxT("About menu sample"));
  524. wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
  525. menuBar->Append(fileMenu, wxT("&File"));
  526. menuBar->Append(menubarMenu, wxT("Menu&bar"));
  527. menuBar->Append(menuMenu, wxT("&Menu"));
  528. menuBar->Append(testMenu, wxT("&Test"));
  529. menuBar->Append(helpMenu, wxT("&Help"));
  530. // these items should be initially checked
  531. menuBar->Check(Menu_MenuBar_Toggle, true);
  532. menuBar->Check(Menu_MenuBar_Enable, true);
  533. menuBar->Check(Menu_Menu_Enable, true);
  534. menuBar->Check(Menu_Menu_Check, false);
  535. // associate the menu bar with the frame
  536. SetMenuBar(menuBar);
  537. // intercept all menu events and log them in this custom event handler
  538. PushEventHandler(new MyEvtHandler(this));
  539. #if USE_LOG_WINDOW
  540. // create the log text window
  541. m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  542. wxDefaultPosition, wxDefaultSize,
  543. wxTE_MULTILINE);
  544. m_textctrl->SetEditable(false);
  545. wxLog::DisableTimestamp();
  546. m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
  547. wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
  548. wxT("append/insert/delete items to/from the \"Test\" menu.\n")
  549. wxT("The commands in the \"Menubar\" menu work with the ")
  550. wxT("menubar itself.\n\n")
  551. wxT("Right click the band below to test popup menus.\n"));
  552. #endif
  553. #ifdef __POCKETPC__
  554. EnableContextMenu();
  555. #endif
  556. }
  557. MyFrame::~MyFrame()
  558. {
  559. delete m_menu;
  560. // delete the event handler installed in ctor
  561. PopEventHandler(true);
  562. #if USE_LOG_WINDOW
  563. // restore old logger
  564. delete wxLog::SetActiveTarget(m_logOld);
  565. #endif
  566. }
  567. wxMenu *MyFrame::CreateDummyMenu(wxString *title)
  568. {
  569. wxMenu *menu = new wxMenu;
  570. menu->Append(Menu_Dummy_First, wxT("&First item\tCtrl-F1"));
  571. menu->AppendSeparator();
  572. menu->AppendCheckItem(Menu_Dummy_Second, wxT("&Second item\tCtrl-F2"));
  573. if ( title )
  574. {
  575. title->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy);
  576. }
  577. return menu;
  578. }
  579. wxMenuItem *MyFrame::GetLastMenuItem() const
  580. {
  581. wxMenuBar *menubar = GetMenuBar();
  582. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  583. wxCHECK_MSG( menu, NULL, wxT("no 'Test' menu?") );
  584. wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
  585. if ( !node )
  586. {
  587. wxLogWarning(wxT("No last item in the 'Test' menu!"));
  588. return NULL;
  589. }
  590. else
  591. {
  592. return node->GetData();
  593. }
  594. }
  595. void MyFrame::LogMenuEvent(const wxCommandEvent& event)
  596. {
  597. int id = event.GetId();
  598. wxString msg = wxString::Format(wxT("Menu command %d"), id);
  599. // catch all checkable menubar items and also the check item from the popup
  600. // menu
  601. wxMenuItem *item = GetMenuBar()->FindItem(id);
  602. if ( (item && item->IsCheckable()) || id == Menu_Popup_ToBeChecked )
  603. {
  604. msg += wxString::Format(wxT(" (the item is currently %schecked)"),
  605. event.IsChecked() ? wxT("") : wxT("not "));
  606. }
  607. wxLogMessage(msg);
  608. }
  609. // ----------------------------------------------------------------------------
  610. // menu callbacks
  611. // ----------------------------------------------------------------------------
  612. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  613. {
  614. Close(true);
  615. }
  616. #if USE_LOG_WINDOW
  617. void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
  618. {
  619. m_textctrl->Clear();
  620. }
  621. void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent& event)
  622. {
  623. // if we only enable this item when the log window is empty, we never see
  624. // it in the disable state as a message is logged whenever the menu is
  625. // opened, so we disable it if there is not "much" text in the window
  626. event.Enable( m_textctrl->GetNumberOfLines() > 5 );
  627. }
  628. #endif // USE_LOG_WINDOW
  629. void MyFrame::OnShowDialog(wxCommandEvent& WXUNUSED(event))
  630. {
  631. MyDialog dlg(this);
  632. dlg.ShowModal();
  633. }
  634. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  635. {
  636. (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
  637. wxT("About wxWidgets menu sample"),
  638. wxOK | wxICON_INFORMATION);
  639. }
  640. void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
  641. {
  642. wxMenuBar *mbar = GetMenuBar();
  643. size_t count = mbar->GetMenuCount();
  644. if ( count == 4 )
  645. {
  646. // don't let delete the first 4 menus
  647. wxLogError(wxT("Can't delete any more menus"));
  648. }
  649. else
  650. {
  651. delete mbar->Remove(count - 1);
  652. }
  653. }
  654. void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
  655. {
  656. wxString title;
  657. wxMenu *menu = CreateDummyMenu(&title);
  658. // Insert before the 'Help' menu
  659. // Otherwise repeated Deletes will remove the 'Test' menu
  660. GetMenuBar()->Insert(4, menu, title);
  661. }
  662. void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
  663. {
  664. wxString title;
  665. wxMenu *menu = CreateDummyMenu(&title);
  666. GetMenuBar()->Append(menu, title);
  667. }
  668. void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
  669. {
  670. wxMenuBar *mbar = GetMenuBar();
  671. if ( !m_menu )
  672. {
  673. // hide the menu
  674. m_menu = mbar->Remove(0);
  675. }
  676. else
  677. {
  678. // restore it
  679. mbar->Insert(0, m_menu, wxT("&File"));
  680. m_menu = NULL;
  681. }
  682. }
  683. void MyFrame::OnEnableMenu(wxCommandEvent& event)
  684. {
  685. wxMenuBar *mbar = GetMenuBar();
  686. size_t count = mbar->GetMenuCount();
  687. mbar->EnableTop(count - 1, event.IsChecked());
  688. }
  689. void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
  690. {
  691. wxMenuBar *mbar = GetMenuBar();
  692. size_t count = mbar->GetMenuCount();
  693. wxCHECK_RET( count, wxT("no last menu?") );
  694. wxLogMessage(wxT("The label of the last menu item is '%s'"),
  695. mbar->GetMenuLabel(count - 1).c_str());
  696. }
  697. #if wxUSE_TEXTDLG
  698. void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
  699. {
  700. wxMenuBar *mbar = GetMenuBar();
  701. size_t count = mbar->GetMenuCount();
  702. wxCHECK_RET( count, wxT("no last menu?") );
  703. wxString label = wxGetTextFromUser
  704. (
  705. wxT("Enter new label: "),
  706. wxT("Change last menu text"),
  707. mbar->GetMenuLabel(count - 1),
  708. this
  709. );
  710. if ( !label.empty() )
  711. {
  712. mbar->SetMenuLabel(count - 1, label);
  713. }
  714. }
  715. void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event))
  716. {
  717. wxMenuBar *mbar = GetMenuBar();
  718. size_t count = mbar->GetMenuCount();
  719. wxCHECK_RET( count, wxT("no last menu?") );
  720. wxString label = wxGetTextFromUser
  721. (
  722. wxT("Enter label to search for: "),
  723. wxT("Find menu"),
  724. wxEmptyString,
  725. this
  726. );
  727. if ( !label.empty() )
  728. {
  729. int index = mbar->FindMenu(label);
  730. if (index == wxNOT_FOUND)
  731. {
  732. wxLogWarning(wxT("No menu with label '%s'"), label.c_str());
  733. }
  734. else
  735. {
  736. wxLogMessage(wxT("Menu %d has label '%s'"), index, label.c_str());
  737. }
  738. }
  739. }
  740. #endif
  741. void MyFrame::OnDummy(wxCommandEvent& event)
  742. {
  743. wxLogMessage(wxT("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1);
  744. }
  745. void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
  746. {
  747. wxMenuBar *menubar = GetMenuBar();
  748. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  749. wxCHECK_RET( menu, wxT("no 'Test' menu?") );
  750. menu->AppendSeparator();
  751. menu->Append(Menu_Dummy_Third, wxT("&Third dummy item\tCtrl-F3"),
  752. wxT("Checkable item"), true);
  753. }
  754. void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
  755. {
  756. wxMenuBar *menubar = GetMenuBar();
  757. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  758. wxCHECK_RET( menu, wxT("no 'Test' menu?") );
  759. menu->Append(Menu_Dummy_Last, wxT("&Dummy sub menu"),
  760. CreateDummyMenu(NULL), wxT("Dummy sub menu help"));
  761. }
  762. void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
  763. {
  764. wxMenuBar *menubar = GetMenuBar();
  765. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  766. wxCHECK_RET( menu, wxT("no 'Test' menu?") );
  767. size_t count = menu->GetMenuItemCount();
  768. if ( !count )
  769. {
  770. wxLogWarning(wxT("No items to delete!"));
  771. }
  772. else
  773. {
  774. menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
  775. }
  776. }
  777. void MyFrame::OnDeleteSubMenu(wxCommandEvent& WXUNUSED(event))
  778. {
  779. wxMenuBar *menubar = GetMenuBar();
  780. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  781. wxCHECK_RET( menu, wxT("no 'Test' menu?") );
  782. for ( int n = menu->GetMenuItemCount() - 1; n >=0 ; --n )
  783. {
  784. wxMenuItem* item = menu->FindItemByPosition(n);
  785. if (item->IsSubMenu())
  786. {
  787. menu->Destroy(item);
  788. return;
  789. }
  790. }
  791. wxLogWarning(wxT("No submenu to delete!"));
  792. }
  793. void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
  794. {
  795. wxMenuBar *menubar = GetMenuBar();
  796. wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
  797. wxCHECK_RET( menu, wxT("no 'Test' menu?") );
  798. menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
  799. wxT("Fourth dummy item\tCtrl-F4")));
  800. menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR));
  801. }
  802. void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
  803. {
  804. wxMenuItem *item = GetLastMenuItem();
  805. if ( item )
  806. {
  807. item->Enable(!item->IsEnabled());
  808. }
  809. }
  810. void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
  811. {
  812. wxMenuItem *item = GetLastMenuItem();
  813. if (item && item->IsCheckable())
  814. {
  815. item->Toggle();
  816. }
  817. }
  818. void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
  819. {
  820. wxLogNull nolog;
  821. wxMenuItem *item = GetLastMenuItem();
  822. event.Enable(item && item->IsCheckable());
  823. }
  824. void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
  825. {
  826. wxMenuItem *item = GetLastMenuItem();
  827. if ( item )
  828. {
  829. wxString label = item->GetItemLabel();
  830. wxLogMessage(wxT("The label of the last menu item is '%s'"),
  831. label.c_str());
  832. }
  833. }
  834. #if wxUSE_TEXTDLG
  835. void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
  836. {
  837. wxMenuItem *item = GetLastMenuItem();
  838. if ( item )
  839. {
  840. wxString label = wxGetTextFromUser
  841. (
  842. wxT("Enter new label: "),
  843. wxT("Change last menu item text"),
  844. item->GetItemLabel(),
  845. this
  846. );
  847. label.Replace( wxT("\\t"), wxT("\t") );
  848. if ( !label.empty() )
  849. {
  850. item->SetItemLabel(label);
  851. }
  852. }
  853. }
  854. #endif
  855. void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
  856. {
  857. wxMenuItem *item = GetLastMenuItem();
  858. if ( item )
  859. {
  860. wxString msg;
  861. msg << wxT("The item is ") << (item->IsEnabled() ? wxT("enabled")
  862. : wxT("disabled"))
  863. << wxT('\n');
  864. if ( item->IsCheckable() )
  865. {
  866. msg << wxT("It is checkable and ") << (item->IsChecked() ? wxT("") : wxT("un"))
  867. << wxT("checked\n");
  868. }
  869. #if wxUSE_ACCEL
  870. wxAcceleratorEntry *accel = item->GetAccel();
  871. if ( accel )
  872. {
  873. msg << wxT("Its accelerator is ");
  874. int flags = accel->GetFlags();
  875. if ( flags & wxACCEL_ALT )
  876. msg << wxT("Alt-");
  877. if ( flags & wxACCEL_CTRL )
  878. msg << wxT("Ctrl-");
  879. if ( flags & wxACCEL_SHIFT )
  880. msg << wxT("Shift-");
  881. int code = accel->GetKeyCode();
  882. switch ( code )
  883. {
  884. case WXK_F1:
  885. case WXK_F2:
  886. case WXK_F3:
  887. case WXK_F4:
  888. case WXK_F5:
  889. case WXK_F6:
  890. case WXK_F7:
  891. case WXK_F8:
  892. case WXK_F9:
  893. case WXK_F10:
  894. case WXK_F11:
  895. case WXK_F12:
  896. msg << wxT('F') << code - WXK_F1 + 1;
  897. break;
  898. // if there are any other keys wxGetAccelFromString() may return,
  899. // we should process them here
  900. default:
  901. if ( wxIsalnum(code) )
  902. {
  903. msg << (wxChar)code;
  904. break;
  905. }
  906. wxFAIL_MSG( wxT("unknown keyboard accel") );
  907. }
  908. delete accel;
  909. }
  910. else
  911. {
  912. msg << wxT("It doesn't have an accelerator");
  913. }
  914. #endif // wxUSE_ACCEL
  915. wxLogMessage(msg);
  916. }
  917. }
  918. #if wxUSE_TEXTDLG
  919. void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event))
  920. {
  921. wxMenuBar *mbar = GetMenuBar();
  922. size_t count = mbar->GetMenuCount();
  923. wxCHECK_RET( count, wxT("no last menu?") );
  924. wxString label = wxGetTextFromUser
  925. (
  926. wxT("Enter label to search for: "),
  927. wxT("Find menu item"),
  928. wxEmptyString,
  929. this
  930. );
  931. if ( !label.empty() )
  932. {
  933. size_t menuindex;
  934. int index = wxNOT_FOUND;
  935. for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex)
  936. {
  937. index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label);
  938. }
  939. if (index == wxNOT_FOUND)
  940. {
  941. wxLogWarning(wxT("No menu item with label '%s'"), label.c_str());
  942. }
  943. else
  944. {
  945. wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
  946. index, (unsigned long)menuindex, label.c_str());
  947. }
  948. }
  949. }
  950. #endif
  951. void MyFrame::ShowContextMenu(const wxPoint& pos)
  952. {
  953. wxMenu menu;
  954. if ( wxGetKeyState(WXK_SHIFT) )
  955. {
  956. // when Shift is pressed, demonstrate the use of a simple function
  957. // returning the id of the item selected in the popup menu
  958. menu.SetTitle("Choose one of:");
  959. static const char *choices[] = { "Apple", "Banana", "Cherry" };
  960. for ( size_t n = 0; n < WXSIZEOF(choices); n++ )
  961. menu.Append(Menu_PopupChoice + n, choices[n]);
  962. const int rc = GetPopupMenuSelectionFromUser(menu, pos);
  963. if ( rc == wxID_NONE )
  964. {
  965. wxLogMessage("No selection");
  966. }
  967. else
  968. {
  969. wxLogMessage("You have selected \"%s\"",
  970. choices[rc - Menu_PopupChoice]);
  971. }
  972. }
  973. else // normal case, shift not pressed
  974. {
  975. menu.Append(Menu_Help_About, wxT("&About"));
  976. menu.Append(Menu_Popup_Submenu, wxT("&Submenu"), CreateDummyMenu(NULL));
  977. menu.Append(Menu_Popup_ToBeDeleted, wxT("To be &deleted"));
  978. menu.AppendCheckItem(Menu_Popup_ToBeChecked, wxT("To be &checked"));
  979. menu.Append(Menu_Popup_ToBeGreyed, wxT("To be &greyed"),
  980. wxT("This menu item should be initially greyed out"));
  981. menu.AppendSeparator();
  982. menu.Append(Menu_File_Quit, wxT("E&xit"));
  983. menu.Delete(Menu_Popup_ToBeDeleted);
  984. menu.Check(Menu_Popup_ToBeChecked, true);
  985. menu.Enable(Menu_Popup_ToBeGreyed, false);
  986. PopupMenu(&menu, pos);
  987. // test for destroying items in popup menus
  988. #if 0 // doesn't work in wxGTK!
  989. menu.Destroy(Menu_Popup_Submenu);
  990. PopupMenu( &menu, event.GetX(), event.GetY() );
  991. #endif // 0
  992. }
  993. }
  994. void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
  995. {
  996. wxLogMessage(wxT("Normal item selected"));
  997. }
  998. void MyFrame::OnTestCheck(wxCommandEvent& event)
  999. {
  1000. wxLogMessage(wxT("Check item %schecked"),
  1001. event.IsChecked() ? wxT("") : wxT("un"));
  1002. }
  1003. void MyFrame::OnTestRadio(wxCommandEvent& event)
  1004. {
  1005. wxLogMessage(wxT("Radio item %d selected"),
  1006. event.GetId() - Menu_Test_Radio1 + 1);
  1007. }
  1008. #if USE_LOG_WINDOW
  1009. void MyFrame::LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what)
  1010. {
  1011. wxString msg;
  1012. msg << wxT("A ")
  1013. << ( event.IsPopup() ? wxT("popup ") : wxT("") )
  1014. << wxT("menu has been ")
  1015. << what;
  1016. if ( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
  1017. {
  1018. msg << wxT(" (id=") << event.GetId() << wxT(")");
  1019. }
  1020. msg << wxT(".");
  1021. wxLogStatus(this, msg.c_str());
  1022. }
  1023. #endif
  1024. void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event)
  1025. {
  1026. event.Enable(false);
  1027. }
  1028. void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event)
  1029. {
  1030. event.Enable(true);
  1031. }
  1032. void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event)
  1033. {
  1034. int which = (event.GetId() - Menu_SubMenu_Radio1 + 1);
  1035. if (which == 2)
  1036. event.Check(true);
  1037. else
  1038. event.Check(false);
  1039. }
  1040. #if USE_CONTEXT_MENU
  1041. void MyFrame::OnContextMenu(wxContextMenuEvent& event)
  1042. {
  1043. wxPoint point = event.GetPosition();
  1044. // If from keyboard
  1045. if (point.x == -1 && point.y == -1) {
  1046. wxSize size = GetSize();
  1047. point.x = size.x / 2;
  1048. point.y = size.y / 2;
  1049. } else {
  1050. point = ScreenToClient(point);
  1051. }
  1052. ShowContextMenu(point);
  1053. }
  1054. #endif
  1055. void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
  1056. {
  1057. #if USE_LOG_WINDOW
  1058. if ( !m_textctrl )
  1059. return;
  1060. // leave a band below for popup menu testing
  1061. wxSize size = GetClientSize();
  1062. m_textctrl->SetSize(0, 0, size.x, (3*size.y)/4);
  1063. #endif
  1064. // this is really ugly but we have to do it as we can't just call
  1065. // event.Skip() because wxFrameBase would make the text control fill the
  1066. // entire frame then
  1067. #ifdef __WXUNIVERSAL__
  1068. PositionMenuBar();
  1069. #endif // __WXUNIVERSAL__
  1070. }
  1071. // ----------------------------------------------------------------------------
  1072. // MyDialog
  1073. // ----------------------------------------------------------------------------
  1074. MyDialog::MyDialog(wxWindow* parent)
  1075. : wxDialog(parent, wxID_ANY, "Test Dialog")
  1076. {
  1077. #if USE_LOG_WINDOW
  1078. // create the log text window
  1079. m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  1080. wxDefaultPosition, wxDefaultSize,
  1081. wxTE_MULTILINE);
  1082. m_textctrl->SetEditable(false);
  1083. m_textctrl->AppendText(wxT("Dialogs do not have menus, but popup menus should function the same\n\n")
  1084. wxT("Right click this text ctrl to test popup menus.\n"));
  1085. #endif
  1086. #ifdef __POCKETPC__
  1087. EnableContextMenu();
  1088. #endif
  1089. }
  1090. #if USE_LOG_WINDOW
  1091. void MyDialog::LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what)
  1092. {
  1093. wxString msg;
  1094. msg << wxT("A ")
  1095. << ( event.IsPopup() ? wxT("popup ") : wxT("") )
  1096. << wxT("menu has been ")
  1097. << what;
  1098. if ( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
  1099. {
  1100. msg << wxT(" (id=") << event.GetId() << wxT(")");
  1101. }
  1102. msg << wxT(".\n");
  1103. m_textctrl->AppendText(msg);
  1104. }
  1105. #endif // USE_LOG_WINDOW
  1106. #if USE_CONTEXT_MENU
  1107. void MyDialog::OnContextMenu(wxContextMenuEvent& event)
  1108. {
  1109. wxPoint point = event.GetPosition();
  1110. // If from keyboard
  1111. if (point.x == -1 && point.y == -1) {
  1112. wxSize size = GetSize();
  1113. point.x = size.x / 2;
  1114. point.y = size.y / 2;
  1115. } else {
  1116. point = ScreenToClient(point);
  1117. }
  1118. ShowContextMenu(point);
  1119. }
  1120. #endif
  1121. void MyDialog::ShowContextMenu(const wxPoint& pos)
  1122. {
  1123. wxMenu menu;
  1124. menu.Append(Menu_Help_About, wxT("&About"));
  1125. menu.Append(Menu_Popup_ToBeDeleted, wxT("To be &deleted"));
  1126. menu.AppendCheckItem(Menu_Popup_ToBeChecked, wxT("To be &checked"));
  1127. menu.Append(Menu_Popup_ToBeGreyed, wxT("To be &greyed"),
  1128. wxT("This menu item should be initially greyed out"));
  1129. menu.AppendSeparator();
  1130. menu.Append(Menu_File_Quit, wxT("E&xit"));
  1131. menu.Delete(Menu_Popup_ToBeDeleted);
  1132. menu.Check(Menu_Popup_ToBeChecked, true);
  1133. menu.Enable(Menu_Popup_ToBeGreyed, false);
  1134. PopupMenu(&menu, pos);
  1135. }