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