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