01: #include <wx/wx.h>
02:
03: const int ID_SAY_HELLO = 1000;
04: const int ID_SAY_GOODBYE = 1001;
05:
06: /**
07: A frame with a simple menu that adds greetings to a
08: text control.
09: */
10: class EventFrame : public wxFrame
11: {
12: public:
13: /**
14: Constructs the menu and text control.
15: */
16: EventFrame();
17: /**
18: Adds a "Hello, World!" message to the text control.
19: @param event the event descriptor
20: */
21: void OnSayHello(wxCommandEvent& event);
22: /**
23: Adds a "Goodbye, World!" message to the text control.
24: @param event the event descriptor
25: */
26: void OnSayGoodbye(wxCommandEvent& event);
27: private:
28: wxTextCtrl* text;
29: DECLARE_EVENT_TABLE()
30: };
31:
32: /**
33: An application to demonstrate the handling of menu events.
34: */
35: class EventApp : public wxApp
36: {
37: public:
38: /**
39: Constructs the frame.
40: */
41: EventApp();
42: /**
43: Shows the frame.
44: @return true
45: */
46: virtual bool OnInit();
47: private:
48: EventFrame* frame;
49: };
50:
51: DECLARE_APP(EventApp)
52:
53: IMPLEMENT_APP(EventApp)
54:
55: BEGIN_EVENT_TABLE(EventFrame, wxFrame)
56: EVT_MENU(ID_SAY_HELLO, EventFrame::OnSayHello)
57: EVT_MENU(ID_SAY_GOODBYE, EventFrame::OnSayGoodbye)
58: END_EVENT_TABLE()
59:
60: EventFrame::EventFrame()
61: : wxFrame(NULL, -1, "EventFrame")
62: {
63: text = new wxTextCtrl(this, -1, "",
64: wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
65:
66: // initialize menu
67: wxMenu* menu = new wxMenu();
68: menu->Append(ID_SAY_HELLO, "Hello");
69: menu->Append(ID_SAY_GOODBYE, "Goodbye");
70:
71: // add menu to menu bar
72: wxMenuBar* menuBar = new wxMenuBar();
73: SetMenuBar(menuBar);
74: menuBar->Append(menu, "Say");
75: }
76:
77: void EventFrame::OnSayHello(wxCommandEvent& event)
78: {
79: text->AppendText("Hello, World!\n");
80: }
81:
82: void EventFrame::OnSayGoodbye(wxCommandEvent& event)
83: {
84: text->AppendText("Goodbye, World!\n");
85: }
86:
87: EventApp::EventApp()
88: {
89: frame = new EventFrame();
90: }
91:
92: bool EventApp::OnInit()
93: {
94: frame->Show(true);
95: return true;
96: }
97:
98: