]> git.saurik.com Git - wxWidgets.git/blob - samples/popup/popup.cpp
more const correctness (slightly modified patch 1655991)
[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 #include "wx/spinctrl.h"
35
36 // ----------------------------------------------------------------------------
37 // resources
38 // ----------------------------------------------------------------------------
39
40 // the application icon (under Windows and OS/2 it is in resources and even
41 // though we could still include the XPM here it would be unused)
42 #if !defined(__WXMSW__) && !defined(__WXPM__)
43 #include "../sample.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // IDs for the controls and the menu commands
51 enum
52 {
53 Minimal_Quit = wxID_EXIT,
54 Minimal_About = wxID_ABOUT,
55 Minimal_TestDialog,
56 Minimal_StartSimplePopup,
57 Minimal_StartScrolledPopup,
58 Minimal_LogWindow,
59 Minimal_PopupButton,
60 Minimal_PopupSpinctrl
61 };
62
63 //----------------------------------------------------------------------------
64 // SimpleTransientPopup
65 //----------------------------------------------------------------------------
66 class SimpleTransientPopup: public wxPopupTransientWindow
67 {
68 public:
69 SimpleTransientPopup( wxWindow *parent );
70 virtual ~SimpleTransientPopup();
71
72 // wxPopupTransientWindow virtual methods are all overridden to log them
73 virtual void Popup(wxWindow *focus = NULL);
74 virtual void OnDismiss();
75 virtual bool ProcessLeftDown(wxMouseEvent& event);
76 virtual bool Show( bool show = true );
77
78 wxScrolledWindow* GetChild() { return m_panel; }
79
80 private:
81 wxScrolledWindow *m_panel;
82 wxButton *m_button;
83 wxSpinCtrl *m_spinCtrl;
84 wxStaticText *m_mouseText;
85
86 private:
87 void OnMouse( wxMouseEvent &event );
88 void OnSize( wxSizeEvent &event );
89 void OnSetFocus( wxFocusEvent &event );
90 void OnKillFocus( wxFocusEvent &event );
91 void OnButton( wxCommandEvent& event );
92 void OnSpinCtrl( wxSpinEvent& event );
93
94 private:
95 DECLARE_CLASS(SimpleTransientPopup)
96 DECLARE_EVENT_TABLE()
97 };
98
99 //----------------------------------------------------------------------------
100 // SimpleTransientPopup
101 //----------------------------------------------------------------------------
102 IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
103
104 BEGIN_EVENT_TABLE(SimpleTransientPopup,wxPopupTransientWindow)
105 EVT_MOUSE_EVENTS( SimpleTransientPopup::OnMouse )
106 EVT_SIZE( SimpleTransientPopup::OnSize )
107 EVT_SET_FOCUS( SimpleTransientPopup::OnSetFocus )
108 EVT_KILL_FOCUS( SimpleTransientPopup::OnKillFocus )
109 EVT_BUTTON( Minimal_PopupButton, SimpleTransientPopup::OnButton )
110 EVT_SPINCTRL( Minimal_PopupSpinctrl, SimpleTransientPopup::OnSpinCtrl )
111 END_EVENT_TABLE()
112
113 SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent )
114 :wxPopupTransientWindow( parent )
115 {
116 m_panel = new wxScrolledWindow( this, wxID_ANY );
117 m_panel->SetBackgroundColour( *wxLIGHT_GREY );
118
119 // Keep this code to verify if mouse events work, they're required if
120 // you're making a control like a combobox where the items are highlighted
121 // under the cursor, the m_panel is set focus in the Popup() function
122 m_panel->Connect(wxEVT_MOTION,
123 wxMouseEventHandler(SimpleTransientPopup::OnMouse),
124 NULL, this);
125
126 wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
127 wxT("wx.PopupTransientWindow is a\n")
128 wxT("wx.PopupWindow which disappears\n")
129 wxT("automatically when the user\n")
130 wxT("clicks the mouse outside it or if it\n")
131 wxT("(or its first child) loses focus in \n")
132 wxT("any other way.") );
133
134 m_button = new wxButton(m_panel, Minimal_PopupButton, wxT("Press Me"));
135 m_spinCtrl = new wxSpinCtrl(m_panel, Minimal_PopupSpinctrl, wxT("Hello"));
136 m_mouseText = new wxStaticText(m_panel, wxID_ANY,
137 wxT("<- Test Mouse ->"));
138
139 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
140 topSizer->Add( text, 0, wxALL, 5 );
141 topSizer->Add( m_button, 0, wxALL, 5 );
142 topSizer->Add( m_spinCtrl, 0, wxALL, 5 );
143 topSizer->Add( m_mouseText, 0, wxCENTRE|wxALL, 5 );
144
145 m_panel->SetAutoLayout( true );
146 m_panel->SetSizer( topSizer );
147 topSizer->Fit(m_panel);
148 topSizer->Fit(this);
149 }
150
151 SimpleTransientPopup::~SimpleTransientPopup()
152 {
153 }
154
155 void SimpleTransientPopup::Popup(wxWindow *focus)
156 {
157 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
158 wxPopupTransientWindow::Popup(focus ? focus : m_panel);
159 }
160
161 void SimpleTransientPopup::OnDismiss()
162 {
163 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
164 wxPopupTransientWindow::OnDismiss();
165 }
166
167 bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent& event)
168 {
169 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event.GetX(), event.GetY());
170 return wxPopupTransientWindow::ProcessLeftDown(event);
171 }
172 bool SimpleTransientPopup::Show( bool show )
173 {
174 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show));
175 return wxPopupTransientWindow::Show(show);
176 }
177
178 void SimpleTransientPopup::OnSize(wxSizeEvent &event)
179 {
180 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
181 event.Skip();
182 }
183
184 void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
185 {
186 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
187 event.Skip();
188 }
189
190 void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
191 {
192 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
193 event.Skip();
194 }
195
196 void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
197 {
198 wxRect rect(m_mouseText->GetRect());
199 rect.SetX(-100000);
200 rect.SetWidth(1000000);
201 wxColour colour(*wxLIGHT_GREY);
202
203 if (rect.Contains(event.GetPosition()))
204 {
205 colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
206 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"), long(event.GetEventObject()), event.GetX(), event.GetY());
207 }
208
209 if (colour != m_mouseText->GetBackgroundColour())
210 {
211 m_mouseText->SetBackgroundColour(colour);
212 m_mouseText->Refresh();
213 }
214 event.Skip();
215 }
216
217 void SimpleTransientPopup::OnButton(wxCommandEvent& event)
218 {
219 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event.GetId());
220
221 wxButton *button = wxDynamicCast(event.GetEventObject(), wxButton);
222 if (button->GetLabel() == wxT("Press Me"))
223 button->SetLabel(wxT("Pressed"));
224 else
225 button->SetLabel(wxT("Press Me"));
226
227 event.Skip();
228 }
229
230 void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent& event)
231 {
232 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %d"), long(this), event.GetId(), event.GetInt());
233 event.Skip();
234 }
235
236 // ----------------------------------------------------------------------------
237 // private classes
238 // ----------------------------------------------------------------------------
239
240 class MyDialog : public wxDialog
241 {
242 public:
243 MyDialog(const wxString& title);
244
245 void OnStartSimplePopup(wxCommandEvent& event);
246 void OnStartScrolledPopup(wxCommandEvent& event);
247
248 private:
249 SimpleTransientPopup *m_simplePopup;
250 SimpleTransientPopup *m_scrolledPopup;
251 DECLARE_EVENT_TABLE()
252 };
253
254 class MyFrame : public wxFrame
255 {
256 public:
257 MyFrame(const wxString& title);
258 virtual ~MyFrame();
259
260 void OnQuit(wxCommandEvent& event);
261 void OnAbout(wxCommandEvent& event);
262 void OnTestDialog(wxCommandEvent& event);
263 void OnStartSimplePopup(wxCommandEvent& event);
264 void OnStartScrolledPopup(wxCommandEvent& event);
265
266 private:
267 SimpleTransientPopup *m_simplePopup;
268 SimpleTransientPopup *m_scrolledPopup;
269 wxTextCtrl *m_logWin;
270 wxLog *m_logOld;
271 DECLARE_EVENT_TABLE()
272 };
273
274 class MyApp : public wxApp
275 {
276 public:
277 virtual bool OnInit();
278
279 MyFrame *m_frame;
280 };
281
282 // ----------------------------------------------------------------------------
283 // event tables and other macros for wxWidgets
284 // ----------------------------------------------------------------------------
285
286
287 IMPLEMENT_APP(MyApp)
288
289 // 'Main program' equivalent: the program execution "starts" here
290 bool MyApp::OnInit()
291 {
292 if ( !wxApp::OnInit() )
293 return false;
294
295 // create the main application window
296 m_frame = new MyFrame(_T("Popup wxWidgets App"));
297
298 // and show it (the frames, unlike simple controls, are not shown when
299 // created initially)
300 m_frame->Show(true);
301
302 // success: wxApp::OnRun() will be called which will enter the main message
303 // loop and the application will run. If we returned false here, the
304 // application would exit immediately.
305 return true;
306 }
307
308 // ----------------------------------------------------------------------------
309 // main frame
310 // ----------------------------------------------------------------------------
311
312 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
313 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
314 EVT_MENU(Minimal_About, MyFrame::OnAbout)
315 EVT_MENU(Minimal_TestDialog, MyFrame::OnTestDialog)
316 EVT_BUTTON(Minimal_StartSimplePopup, MyFrame::OnStartSimplePopup)
317 EVT_BUTTON(Minimal_StartScrolledPopup, MyFrame::OnStartScrolledPopup)
318 END_EVENT_TABLE()
319
320 MyFrame::MyFrame(const wxString& title)
321 : wxFrame(NULL, wxID_ANY, title)
322 {
323 m_simplePopup = m_scrolledPopup = NULL;
324
325 SetIcon(wxICON(sample));
326
327 #if wxUSE_MENUS
328 wxMenu *menuFile = new wxMenu;
329
330 // the "About" item should be in the help menu
331 wxMenu *helpMenu = new wxMenu;
332 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
333
334 menuFile->Append(Minimal_TestDialog, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
335 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
336
337 // now append the freshly created menu to the menu bar...
338 wxMenuBar *menuBar = new wxMenuBar();
339 menuBar->Append(menuFile, _T("&File"));
340 menuBar->Append(helpMenu, _T("&Help"));
341
342 // ... and attach this menu bar to the frame
343 SetMenuBar(menuBar);
344 #endif // wxUSE_MENUS
345
346 #if wxUSE_STATUSBAR
347 // create a status bar just for fun (by default with 1 pane only)
348 CreateStatusBar(2);
349 SetStatusText(_T("Welcome to wxWidgets!"));
350 #endif // wxUSE_STATUSBAR
351
352 wxPanel *panel = new wxPanel(this, -1);
353 wxButton *button1 = new wxButton( panel, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
354 wxButton *button2 = new wxButton( panel, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,70) );
355
356 m_logWin = new wxTextCtrl( panel, wxID_ANY, wxEmptyString, wxDefaultPosition,
357 wxDefaultSize, wxTE_MULTILINE );
358 m_logWin->SetEditable(false);
359 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
360 m_logOld = logger->SetActiveTarget( logger );
361 logger->SetTimestamp( NULL );
362
363 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
364 topSizer->Add( button1, 0, wxALL, 5 );
365 topSizer->Add( button2, 0, wxALL, 5 );
366 topSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
367
368 panel->SetAutoLayout( true );
369 panel->SetSizer( topSizer );
370
371 }
372
373 MyFrame::~MyFrame()
374 {
375 delete wxLog::SetActiveTarget(m_logOld);
376 }
377
378
379 // event handlers
380
381 void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
382 {
383 wxLogMessage( wxT("================================================") );
384 delete m_simplePopup;
385 m_simplePopup = new SimpleTransientPopup( this );
386 wxWindow *btn = (wxWindow*) event.GetEventObject();
387 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
388 wxSize sz = btn->GetSize();
389 m_simplePopup->Position( pos, sz );
390 wxLogMessage( wxT("0x%lx Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(m_simplePopup), pos.x, pos.y, sz.x, sz.y );
391 m_simplePopup->Popup();
392 }
393
394 void MyFrame::OnStartScrolledPopup(wxCommandEvent& event)
395 {
396 wxLogMessage( wxT("================================================") );
397 delete m_scrolledPopup;
398 m_scrolledPopup = new SimpleTransientPopup( this );
399 m_scrolledPopup->GetChild()->SetScrollbars(1, 1, 1000, 1000);
400 wxWindow *btn = (wxWindow*) event.GetEventObject();
401 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
402 wxSize sz = btn->GetSize();
403 m_scrolledPopup->Position( pos, sz );
404 wxLogMessage( wxT("0x%lx Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(m_scrolledPopup), pos.x, pos.y, sz.x, sz.y );
405 m_scrolledPopup->Popup();
406 }
407
408 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
409 {
410 MyDialog dialog( wxT("Test") );
411 dialog.ShowModal();
412 }
413
414 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
415 {
416 // true is to force the frame to close
417 Close(true);
418 }
419
420 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
421 {
422 wxString msg;
423 msg.Printf( _T("This is the About dialog of the popup sample.\n")
424 _T("Welcome to %s"), wxVERSION_STRING);
425
426 wxMessageBox(msg, _T("About Popup"), wxOK | wxICON_INFORMATION, this);
427 }
428
429 // ----------------------------------------------------------------------------
430 // test dialog
431 // ----------------------------------------------------------------------------
432
433 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
434 EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
435 EVT_BUTTON(Minimal_StartScrolledPopup, MyDialog::OnStartScrolledPopup)
436 END_EVENT_TABLE()
437
438 MyDialog::MyDialog(const wxString& title)
439 :wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
440 {
441 m_simplePopup = m_scrolledPopup = NULL;
442 wxPanel *panel = new wxPanel(this, -1);
443
444 wxButton *button1 = new wxButton( panel, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
445 wxButton *button2 = new wxButton( panel, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,60) );
446
447 wxButton *okButton = new wxButton( panel, wxID_OK, wxT("OK"), wxPoint(20,200) );
448
449 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
450 topSizer->Add( button1, 0, wxALL, 5 );
451 topSizer->Add( button2, 0, wxALL, 5 );
452 topSizer->AddSpacer(40);
453 topSizer->Add( okButton, 0, wxALL, 5 );
454
455 panel->SetAutoLayout( true );
456 panel->SetSizer( topSizer );
457 topSizer->Fit(this);
458 }
459
460 void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
461 {
462 wxLogMessage( wxT("================================================") );
463 delete m_simplePopup;
464 m_simplePopup = new SimpleTransientPopup( this );
465 wxWindow *btn = (wxWindow*) event.GetEventObject();
466 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
467 wxSize sz = btn->GetSize();
468 m_simplePopup->Position( pos, sz );
469 wxLogMessage( wxT("0x%lx Dialog Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(m_simplePopup), pos.x, pos.y, sz.x, sz.y );
470 m_simplePopup->Popup();
471 }
472
473 void MyDialog::OnStartScrolledPopup(wxCommandEvent& event)
474 {
475 wxLogMessage( wxT("================================================") );
476 delete m_scrolledPopup;
477 m_scrolledPopup = new SimpleTransientPopup( this );
478 m_scrolledPopup->GetChild()->SetScrollbars(1, 1, 1000, 1000);
479 wxWindow *btn = (wxWindow*) event.GetEventObject();
480 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
481 wxSize sz = btn->GetSize();
482 m_scrolledPopup->Position( pos, sz );
483 wxLogMessage( wxT("0x%lx Dialog Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(m_scrolledPopup), pos.x, pos.y, sz.x, sz.y );
484 m_scrolledPopup->Popup();
485 }