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