created bakefile and generated all makefiles from it; renamed minimal.cpp to popup.cpp
[wxWidgets.git] / samples / popup / popup.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Popup wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 2005-02-04
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/popupwin.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
39 // the application icon (under Windows and OS/2 it is in resources and even
40 // though we could still include the XPM here it would be unused)
41 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "../sample.xpm"
43 #endif
44
45 class SimpleTransientPopup: public wxPopupTransientWindow
46 {
47 public:
48 SimpleTransientPopup( wxWindow *parent );
49 virtual ~SimpleTransientPopup();
50
51 void OnDismiss();
52
53 private:
54 wxPanel *m_panel;
55
56 private:
57 void OnMouse( wxMouseEvent &event );
58 void OnSize( wxSizeEvent &event );
59 void OnSetFocus( wxFocusEvent &event );
60 void OnKillFocus( wxFocusEvent &event );
61
62 private:
63 DECLARE_CLASS(SimpleTransientPopup)
64 DECLARE_EVENT_TABLE()
65 };
66
67
68 //----------------------------------------------------------------------------
69 // SimpleTransientPopup
70 //----------------------------------------------------------------------------
71
72 IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
73
74 BEGIN_EVENT_TABLE(SimpleTransientPopup,wxPopupTransientWindow)
75 EVT_MOUSE_EVENTS( SimpleTransientPopup::OnMouse )
76 EVT_SIZE( SimpleTransientPopup::OnSize )
77 EVT_SET_FOCUS( SimpleTransientPopup::OnSetFocus )
78 EVT_KILL_FOCUS( SimpleTransientPopup::OnKillFocus )
79 END_EVENT_TABLE()
80
81 SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent ) :
82 wxPopupTransientWindow( parent )
83 {
84 m_panel = new wxPanel( this, -1 );
85 m_panel->SetBackgroundColour( *wxLIGHT_GREY );
86 wxStaticText *text = new wxStaticText( m_panel, -1,
87 wxT("wx.PopupTransientWindow is a\n")
88 wxT("wx.PopupWindow which disappears\n")
89 wxT("automatically when the user\n")
90 wxT("clicks the mouse outside it or if it\n")
91 wxT("(or its first child) loses focus in \n")
92 wxT("any other way."), wxPoint( 10,10) );
93 wxSize size = text->GetBestSize();
94 m_panel->SetSize( size.x+20, size.y+20 );
95 SetClientSize( size.x+20, size.y+20 );
96 }
97
98 SimpleTransientPopup::~SimpleTransientPopup()
99 {
100 }
101
102 void SimpleTransientPopup::OnDismiss()
103 {
104 }
105
106 void SimpleTransientPopup::OnSize(wxSizeEvent &event)
107 {
108 event.Skip();
109 }
110
111 void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
112 {
113 event.Skip();
114 }
115
116 void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
117 {
118 event.Skip();
119 }
120
121 void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
122 {
123 event.Skip();
124 }
125
126 // ----------------------------------------------------------------------------
127 // private classes
128 // ----------------------------------------------------------------------------
129
130 class MyApp : public wxApp
131 {
132 public:
133 virtual bool OnInit();
134 };
135
136 class MyDialog : public wxDialog
137 {
138 public:
139 MyDialog(const wxString& title);
140
141 void OnStartSimplePopup(wxCommandEvent& event);
142 void OnStartComplexPopup(wxCommandEvent& event);
143
144 private:
145 DECLARE_EVENT_TABLE()
146 };
147
148 class MyFrame : public wxFrame
149 {
150 public:
151 MyFrame(const wxString& title);
152
153 void OnQuit(wxCommandEvent& event);
154 void OnAbout(wxCommandEvent& event);
155 void OnTestDialog(wxCommandEvent& event);
156 void OnStartSimplePopup(wxCommandEvent& event);
157 void OnStartComplexPopup(wxCommandEvent& event);
158
159 private:
160 DECLARE_EVENT_TABLE()
161 };
162
163 // ----------------------------------------------------------------------------
164 // constants
165 // ----------------------------------------------------------------------------
166
167 // IDs for the controls and the menu commands
168 enum
169 {
170 Minimal_Quit = wxID_EXIT,
171 Minimal_About = wxID_ABOUT,
172 Minimal_TestDialog,
173 Minimal_StartSimplePopup,
174 Minimal_StartComplexPopup,
175 };
176
177 // ----------------------------------------------------------------------------
178 // event tables and other macros for wxWidgets
179 // ----------------------------------------------------------------------------
180
181
182 IMPLEMENT_APP(MyApp)
183
184 // 'Main program' equivalent: the program execution "starts" here
185 bool MyApp::OnInit()
186 {
187 // create the main application window
188 MyFrame *frame = new MyFrame(_T("Popup wxWidgets App"));
189
190 // and show it (the frames, unlike simple controls, are not shown when
191 // created initially)
192 frame->Show(true);
193
194 // success: wxApp::OnRun() will be called which will enter the main message
195 // loop and the application will run. If we returned false here, the
196 // application would exit immediately.
197 return true;
198 }
199
200 // ----------------------------------------------------------------------------
201 // main frame
202 // ----------------------------------------------------------------------------
203
204 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
205 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
206 EVT_MENU(Minimal_About, MyFrame::OnAbout)
207 EVT_MENU(Minimal_TestDialog, MyFrame::OnTestDialog)
208 EVT_BUTTON(Minimal_StartSimplePopup, MyFrame::OnStartSimplePopup)
209 EVT_BUTTON(Minimal_StartComplexPopup, MyFrame::OnStartComplexPopup)
210 END_EVENT_TABLE()
211
212 MyFrame::MyFrame(const wxString& title)
213 : wxFrame(NULL, wxID_ANY, title)
214 {
215 SetIcon(wxICON(sample));
216
217 #if wxUSE_MENUS
218 wxMenu *menuFile = new wxMenu;
219
220 // the "About" item should be in the help menu
221 wxMenu *helpMenu = new wxMenu;
222 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
223
224 menuFile->Append(Minimal_TestDialog, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
225 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
226
227 // now append the freshly created menu to the menu bar...
228 wxMenuBar *menuBar = new wxMenuBar();
229 menuBar->Append(menuFile, _T("&File"));
230 menuBar->Append(helpMenu, _T("&Help"));
231
232 // ... and attach this menu bar to the frame
233 SetMenuBar(menuBar);
234 #endif // wxUSE_MENUS
235
236 new wxButton( this, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
237 new wxButton( this, Minimal_StartComplexPopup, wxT("Show complex popup"), wxPoint(20,120) );
238
239
240 #if wxUSE_STATUSBAR
241 // create a status bar just for fun (by default with 1 pane only)
242 CreateStatusBar(2);
243 SetStatusText(_T("Welcome to wxWidgets!"));
244 #endif // wxUSE_STATUSBAR
245 }
246
247 // event handlers
248
249 void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
250 {
251 SimpleTransientPopup* popup = new SimpleTransientPopup( this );
252 wxWindow *btn = (wxWindow*) event.GetEventObject();
253 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
254 wxSize sz = btn->GetSize();
255 popup->Position( pos, sz );
256 popup->Popup();
257 }
258
259 void MyFrame::OnStartComplexPopup(wxCommandEvent& WXUNUSED(event))
260 {
261 }
262
263 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
264 {
265 MyDialog dialog( wxT("Test") );
266 dialog.ShowModal();
267 }
268
269 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
270 {
271 // true is to force the frame to close
272 Close(true);
273 }
274
275 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
276 {
277 wxString msg;
278 msg.Printf( _T("This is the About dialog of the popup sample.\n")
279 _T("Welcome to %s"), wxVERSION_STRING);
280
281 wxMessageBox(msg, _T("About Popup"), wxOK | wxICON_INFORMATION, this);
282 }
283
284 // ----------------------------------------------------------------------------
285 // test dialog
286 // ----------------------------------------------------------------------------
287
288 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
289 EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
290 EVT_BUTTON(Minimal_StartComplexPopup, MyDialog::OnStartComplexPopup)
291 END_EVENT_TABLE()
292
293 MyDialog::MyDialog(const wxString& title)
294 : wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
295 {
296
297 new wxButton( this, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
298 new wxButton( this, Minimal_StartComplexPopup, wxT("Show complex popup"), wxPoint(20,80) );
299
300 new wxButton( this, wxID_OK, wxT("OK"), wxPoint(20,200) );
301 }
302
303 void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
304 {
305 SimpleTransientPopup* popup = new SimpleTransientPopup( this );
306 wxWindow *btn = (wxWindow*) event.GetEventObject();
307 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
308 wxSize sz = btn->GetSize();
309 popup->Position( pos, sz );
310 popup->Popup();
311 }
312
313 void MyDialog::OnStartComplexPopup(wxCommandEvent& WXUNUSED(event))
314 {
315 }
316