1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic events wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/clntdata.h"
26 #include "mondrian.xpm"
29 // Define a new application type
30 class MyApp
: public wxApp
36 // Define a new frame type
37 class MyFrame
: public wxFrame
40 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
43 void OnQuit(wxCommandEvent
& event
);
44 void OnTest(wxCommandEvent
& event
);
45 void OnAbout(wxCommandEvent
& event
);
48 wxShadowObject m_shadow
;
51 // Define another new frame type
52 class MySecondFrame
: public MyFrame
55 MySecondFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
58 // ID for the menu commands
59 #define DYNAMIC_QUIT wxID_EXIT
60 #define DYNAMIC_TEST 101
61 #define DYNAMIC_ABOUT wxID_ABOUT
63 // Create a new application object
66 // `Main program' equivalent, creating windows and returning main app frame
67 bool MyApp::OnInit(void)
69 // Create the main frame window
70 MyFrame
*frame
= new MyFrame(NULL
, _T("Dynamic wxWidgets App"), 50, 50, 450, 340);
75 // Create the main frame window
76 MySecondFrame
*frame2
= new MySecondFrame(NULL
, _T("Dynamic wxWidgets App"), 150, 150, 450, 340);
86 // -------------------------------------
88 // -------------------------------------
90 // Callback from wxShadowObject
92 int cb_MyFrame_InitStatusbar( void* window
, void* WXUNUSED(param
) )
94 MyFrame
*frame
= (MyFrame
*) window
;
95 frame
->SetStatusText( wxT("Hello from MyFrame"), 0 );
99 // My frame constructor
100 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
101 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
105 SetIcon(wxIcon(_T("mondrian")));
107 SetIcon(wxIcon(mondrian_xpm
));
111 wxMenu
*file_menu
= new wxMenu
;
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
);
120 // Make a panel with a message
121 wxPanel
*panel
= new wxPanel(this, wxID_ANY
, wxPoint(0, 0), wxSize(400, 200), wxTAB_TRAVERSAL
);
123 (void)new wxStaticText(panel
, 311, _T("Hello!"), wxPoint(10, 10), wxDefaultSize
, 0);
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
) );
134 m_shadow
.AddMethod( wxT("OnTest"), &cb_MyFrame_InitStatusbar
);
137 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
142 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
) )
144 m_shadow
.InvokeMethod( wxT("OnTest"), this, NULL
, NULL
);
147 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
149 wxMessageDialog
dialog(this, _T("This demonstrates dynamic event handling"),
150 _T("About Dynamic"), wxYES_NO
|wxCANCEL
);
156 // -------------------------------------
158 // -------------------------------------
160 // Callback from wxShadowObject
162 int cb_MySecondFrame_InitStatusbar( void* window
, void* WXUNUSED(param
) )
164 MySecondFrame
*frame
= (MySecondFrame
*) window
;
165 frame
->SetStatusText( wxT("Hello from MySecondFrame"), 0 );
169 // My frame constructor
170 MySecondFrame::MySecondFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
171 MyFrame(frame
, title
, x
, y
, w
, h
)
173 m_shadow
.AddMethod( wxT("OnTest"), &cb_MySecondFrame_InitStatusbar
);