]>
Commit | Line | Data |
---|---|---|
3e0f9228 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynamic.cpp | |
be5a51fb | 3 | // Purpose: Dynamic events wxWidgets sample |
3e0f9228 JS |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
f5d01a1c | 9 | // Licence: wxWindows license |
3e0f9228 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
788233da | 12 | #if defined(__GNUG__) && !defined(__APPLE__) |
11fdee42 WS |
13 | #pragma implementation |
14 | #pragma interface | |
3e0f9228 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
619f45aa RR |
28 | #include "wx/clntdata.h" |
29 | ||
116a877a | 30 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXCOCOA__) |
3e0f9228 JS |
31 | #include "mondrian.xpm" |
32 | #endif | |
33 | ||
34 | // Define a new application type | |
35 | class MyApp: public wxApp | |
619f45aa RR |
36 | { |
37 | public: | |
3e0f9228 JS |
38 | bool OnInit(void); |
39 | }; | |
40 | ||
41 | // Define a new frame type | |
42 | class MyFrame: public wxFrame | |
619f45aa RR |
43 | { |
44 | public: | |
ab1ca7b3 | 45 | MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); |
f5d01a1c | 46 | |
619f45aa | 47 | public: |
3e0f9228 | 48 | void OnQuit(wxCommandEvent& event); |
619f45aa | 49 | void OnTest(wxCommandEvent& event); |
3e0f9228 | 50 | void OnAbout(wxCommandEvent& event); |
619f45aa RR |
51 | |
52 | protected: | |
53 | wxShadowObject m_shadow; | |
54 | }; | |
55 | ||
56 | // Define another new frame type | |
57 | class MySecondFrame: public MyFrame | |
58 | { | |
59 | public: | |
60 | MySecondFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); | |
3e0f9228 JS |
61 | }; |
62 | ||
63 | // ID for the menu commands | |
91b07357 | 64 | #define DYNAMIC_QUIT wxID_EXIT |
619f45aa | 65 | #define DYNAMIC_TEST 101 |
91b07357 | 66 | #define DYNAMIC_ABOUT wxID_ABOUT |
3e0f9228 JS |
67 | |
68 | // Create a new application object | |
f5d01a1c | 69 | IMPLEMENT_APP (MyApp) |
3e0f9228 JS |
70 | |
71 | // `Main program' equivalent, creating windows and returning main app frame | |
72 | bool MyApp::OnInit(void) | |
73 | { | |
619f45aa RR |
74 | // Create the main frame window |
75 | MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 450, 340); | |
3e0f9228 | 76 | |
619f45aa RR |
77 | // Show the frame |
78 | frame->Show(true); | |
79 | ||
80 | // Create the main frame window | |
81 | MySecondFrame *frame2 = new MySecondFrame(NULL, _T("Dynamic wxWidgets App"), 150, 150, 450, 340); | |
82 | ||
83 | // Show the frame | |
84 | frame2->Show(true); | |
3e0f9228 | 85 | |
619f45aa | 86 | SetTopWindow(frame); |
3e0f9228 | 87 | |
619f45aa RR |
88 | return true; |
89 | } | |
3e0f9228 | 90 | |
619f45aa RR |
91 | // ------------------------------------- |
92 | // MyFrame | |
93 | // ------------------------------------- | |
f5d01a1c | 94 | |
619f45aa | 95 | // Callback from wxShadowObject |
3e0f9228 | 96 | |
619f45aa RR |
97 | int cb_MyFrame_InitStatusbar( void* window, void* param ) |
98 | { | |
99 | MyFrame *frame = (MyFrame*) window; | |
100 | frame->SetStatusText( wxT("Hello from MyFrame"), 0 ); | |
101 | return 0; | |
3e0f9228 JS |
102 | } |
103 | ||
104 | // My frame constructor | |
ab1ca7b3 | 105 | MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): |
07850a49 | 106 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) |
619f45aa RR |
107 | { |
108 | // Give it an icon | |
109 | #ifdef __WXMSW__ | |
110 | SetIcon(wxIcon(_T("mondrian"))); | |
111 | #else | |
112 | SetIcon(wxIcon(mondrian_xpm)); | |
113 | #endif | |
114 | ||
115 | // Make a menubar | |
116 | wxMenu *file_menu = new wxMenu; | |
117 | ||
118 | file_menu->Append(DYNAMIC_ABOUT, _T("&About")); | |
119 | file_menu->Append(DYNAMIC_TEST, _T("&Test")); | |
120 | file_menu->Append(DYNAMIC_QUIT, _T("E&xit")); | |
121 | wxMenuBar *menu_bar = new wxMenuBar; | |
122 | menu_bar->Append(file_menu, _T("&File")); | |
123 | SetMenuBar(menu_bar); | |
124 | ||
125 | // Make a panel with a message | |
126 | wxPanel *panel = new wxPanel(this, wxID_ANY, wxPoint(0, 0), wxSize(400, 200), wxTAB_TRAVERSAL); | |
127 | ||
128 | (void)new wxStaticText(panel, 311, _T("Hello!"), wxPoint(10, 10), wxDefaultSize, 0); | |
129 | ||
130 | // You used to have to do some casting for param 4, but now there are type-safe handlers | |
131 | Connect( DYNAMIC_QUIT, wxID_ANY, | |
132 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) ); | |
133 | Connect( DYNAMIC_TEST, wxID_ANY, | |
134 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnTest) ); | |
135 | Connect( DYNAMIC_ABOUT, wxID_ANY, | |
136 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) ); | |
137 | ||
138 | CreateStatusBar(); | |
139 | m_shadow.AddMethod( wxT("OnTest"), &cb_MyFrame_InitStatusbar ); | |
140 | } | |
3e0f9228 JS |
141 | |
142 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
143 | { | |
619f45aa RR |
144 | Close(true); |
145 | } | |
146 | ||
147 | void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event) ) | |
148 | { | |
149 | m_shadow.InvokeMethod( wxT("OnTest"), this, NULL, NULL ); | |
3e0f9228 JS |
150 | } |
151 | ||
152 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
153 | { | |
619f45aa RR |
154 | wxMessageDialog dialog(this, _T("This demonstrates dynamic event handling"), |
155 | _T("About Dynamic"), wxYES_NO|wxCANCEL); | |
3e0f9228 | 156 | |
619f45aa | 157 | dialog.ShowModal(); |
3e0f9228 JS |
158 | } |
159 | ||
160 | ||
619f45aa RR |
161 | // ------------------------------------- |
162 | // MySecondFrame | |
163 | // ------------------------------------- | |
164 | ||
165 | // Callback from wxShadowObject | |
166 | ||
167 | int cb_MySecondFrame_InitStatusbar( void* window, void* param ) | |
168 | { | |
169 | MySecondFrame *frame = (MySecondFrame*) window; | |
170 | frame->SetStatusText( wxT("Hello from MySecondFrame"), 0 ); | |
171 | return 0; | |
172 | } | |
173 | ||
174 | // My frame constructor | |
175 | MySecondFrame::MySecondFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): | |
176 | MyFrame(frame, title, x, y, w, h ) | |
177 | { | |
178 | m_shadow.AddMethod( wxT("OnTest"), &cb_MySecondFrame_InitStatusbar ); | |
179 | } | |
180 |