]> git.saurik.com Git - wxWidgets.git/blame - samples/popup/popup.cpp
Compilation fix to propgrid sample after r74628.
[wxWidgets.git] / samples / popup / popup.cpp
CommitLineData
98d5b42b 1/////////////////////////////////////////////////////////////////////////////
5d4cca7f 2// Name: popup.cpp
98d5b42b 3// Purpose: Popup wxWidgets sample
43f813d1 4// Author: Robert Roebling
98d5b42b 5// Modified by:
43f813d1 6// Created: 2005-02-04
43f813d1 7// Copyright: (c) 2005 Robert Roebling
98d5b42b
RR
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"
b6de8445 33#include "wx/spinctrl.h"
98d5b42b
RR
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)
e7092398 41#ifndef wxHAS_IMAGES_IN_RESOURCES
98d5b42b
RR
42 #include "../sample.xpm"
43#endif
44
b6de8445
RR
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// IDs for the controls and the menu commands
50enum
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
34786588
VZ
62//----------------------------------------------------------------------------
63// SimpleTransientPopup
64//----------------------------------------------------------------------------
98d5b42b
RR
65class SimpleTransientPopup: public wxPopupTransientWindow
66{
67public:
b7bd58d0 68 SimpleTransientPopup( wxWindow *parent, bool scrolled );
98d5b42b 69 virtual ~SimpleTransientPopup();
34786588
VZ
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 );
dd6a7fb4 76
98d5b42b 77private:
34786588 78 wxScrolledWindow *m_panel;
b6de8445
RR
79 wxButton *m_button;
80 wxSpinCtrl *m_spinCtrl;
414f2513 81 wxStaticText *m_mouseText;
dd6a7fb4 82
98d5b42b
RR
83private:
84 void OnMouse( wxMouseEvent &event );
85 void OnSize( wxSizeEvent &event );
86 void OnSetFocus( wxFocusEvent &event );
87 void OnKillFocus( wxFocusEvent &event );
b6de8445
RR
88 void OnButton( wxCommandEvent& event );
89 void OnSpinCtrl( wxSpinEvent& event );
98d5b42b
RR
90
91private:
92 DECLARE_CLASS(SimpleTransientPopup)
93 DECLARE_EVENT_TABLE()
94};
95
98d5b42b
RR
96//----------------------------------------------------------------------------
97// SimpleTransientPopup
98//----------------------------------------------------------------------------
98d5b42b
RR
99IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
100
101BEGIN_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 )
b6de8445
RR
106 EVT_BUTTON( Minimal_PopupButton, SimpleTransientPopup::OnButton )
107 EVT_SPINCTRL( Minimal_PopupSpinctrl, SimpleTransientPopup::OnSpinCtrl )
98d5b42b
RR
108END_EVENT_TABLE()
109
b7bd58d0 110SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent, bool scrolled )
82ceade7 111 :wxPopupTransientWindow( parent )
98d5b42b 112{
dd6a7fb4 113 m_panel = new wxScrolledWindow( this, wxID_ANY );
98d5b42b 114 m_panel->SetBackgroundColour( *wxLIGHT_GREY );
82ceade7 115
57b2bba4 116 // Keep this code to verify if mouse events work, they're required if
82ceade7
RD
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
414f2513
RD
119 m_panel->Connect(wxEVT_MOTION,
120 wxMouseEventHandler(SimpleTransientPopup::OnMouse),
121 NULL, this);
82ceade7 122
dd6a7fb4 123 wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
befc36f8
RD
124 wxT("wxPopupTransientWindow is a\n")
125 wxT("wxPopupWindow which disappears\n")
98d5b42b
RR
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")
414f2513 129 wxT("any other way.") );
b6de8445 130
414f2513
RD
131 m_button = new wxButton(m_panel, Minimal_PopupButton, wxT("Press Me"));
132 m_spinCtrl = new wxSpinCtrl(m_panel, Minimal_PopupSpinctrl, wxT("Hello"));
57b2bba4 133 m_mouseText = new wxStaticText(m_panel, wxID_ANY,
414f2513 134 wxT("<- Test Mouse ->"));
21fe01e2 135
414f2513
RD
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
b7bd58d0
VZ
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
414f2513 150 m_panel->SetSizer( topSizer );
b7bd58d0
VZ
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());
98d5b42b
RR
166}
167
168SimpleTransientPopup::~SimpleTransientPopup()
169{
170}
171
49449290 172void SimpleTransientPopup::Popup(wxWindow* WXUNUSED(focus))
34786588 173{
5c95f4b2 174 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
509f339a 175 wxPopupTransientWindow::Popup();
34786588
VZ
176}
177
98d5b42b
RR
178void SimpleTransientPopup::OnDismiss()
179{
5c95f4b2 180 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
34786588
VZ
181 wxPopupTransientWindow::OnDismiss();
182}
183
184bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent& event)
185{
5c95f4b2 186 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event.GetX(), event.GetY());
34786588
VZ
187 return wxPopupTransientWindow::ProcessLeftDown(event);
188}
189bool SimpleTransientPopup::Show( bool show )
190{
5c95f4b2 191 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show));
34786588 192 return wxPopupTransientWindow::Show(show);
98d5b42b
RR
193}
194
195void SimpleTransientPopup::OnSize(wxSizeEvent &event)
196{
5c95f4b2 197 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
98d5b42b
RR
198 event.Skip();
199}
200
201void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
202{
5c95f4b2 203 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
43f813d1 204 event.Skip();
98d5b42b
RR
205}
206
207void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
208{
5c95f4b2 209 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
43f813d1 210 event.Skip();
98d5b42b
RR
211}
212
213void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
214{
414f2513
RD
215 wxRect rect(m_mouseText->GetRect());
216 rect.SetX(-100000);
217 rect.SetWidth(1000000);
218 wxColour colour(*wxLIGHT_GREY);
219
50dd4ca9 220 if (rect.Contains(event.GetPosition()))
57b2bba4 221 {
414f2513 222 colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
57b2bba4
BP
223 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"),
224 long(event.GetEventObject()), event.GetX(), event.GetY());
414f2513
RD
225 }
226
227 if (colour != m_mouseText->GetBackgroundColour())
228 {
229 m_mouseText->SetBackgroundColour(colour);
230 m_mouseText->Refresh();
231 }
34786588
VZ
232 event.Skip();
233}
234
b6de8445 235void SimpleTransientPopup::OnButton(wxCommandEvent& event)
34786588 236{
5c95f4b2 237 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event.GetId());
dd6a7fb4 238
b6de8445
RR
239 wxButton *button = wxDynamicCast(event.GetEventObject(), wxButton);
240 if (button->GetLabel() == wxT("Press Me"))
241 button->SetLabel(wxT("Pressed"));
dd6a7fb4 242 else
b6de8445 243 button->SetLabel(wxT("Press Me"));
34786588 244
34786588
VZ
245 event.Skip();
246}
34786588 247
b6de8445 248void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent& event)
34786588 249{
57b2bba4
BP
250 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %d"),
251 long(this), event.GetId(), event.GetInt());
98d5b42b
RR
252 event.Skip();
253}
254
255// ----------------------------------------------------------------------------
256// private classes
257// ----------------------------------------------------------------------------
258
98d5b42b
RR
259class MyDialog : public wxDialog
260{
261public:
262 MyDialog(const wxString& title);
263
264 void OnStartSimplePopup(wxCommandEvent& event);
34786588 265 void OnStartScrolledPopup(wxCommandEvent& event);
98d5b42b
RR
266
267private:
82ceade7
RD
268 SimpleTransientPopup *m_simplePopup;
269 SimpleTransientPopup *m_scrolledPopup;
98d5b42b
RR
270 DECLARE_EVENT_TABLE()
271};
272
273class MyFrame : public wxFrame
274{
275public:
276 MyFrame(const wxString& title);
34786588
VZ
277 virtual ~MyFrame();
278
98d5b42b
RR
279 void OnQuit(wxCommandEvent& event);
280 void OnAbout(wxCommandEvent& event);
281 void OnTestDialog(wxCommandEvent& event);
282 void OnStartSimplePopup(wxCommandEvent& event);
34786588 283 void OnStartScrolledPopup(wxCommandEvent& event);
509f339a 284 void OnActivate(wxActivateEvent& event);
98d5b42b
RR
285
286private:
82ceade7
RD
287 SimpleTransientPopup *m_simplePopup;
288 SimpleTransientPopup *m_scrolledPopup;
b6de8445 289 wxTextCtrl *m_logWin;
34786588 290 wxLog *m_logOld;
98d5b42b
RR
291 DECLARE_EVENT_TABLE()
292};
293
b6de8445 294class MyApp : public wxApp
98d5b42b 295{
b6de8445
RR
296public:
297 virtual bool OnInit();
298
299 MyFrame *m_frame;
98d5b42b
RR
300};
301
302// ----------------------------------------------------------------------------
303// event tables and other macros for wxWidgets
304// ----------------------------------------------------------------------------
305
306
307IMPLEMENT_APP(MyApp)
308
309// 'Main program' equivalent: the program execution "starts" here
310bool MyApp::OnInit()
311{
45e6e6f8
VZ
312 if ( !wxApp::OnInit() )
313 return false;
314
98d5b42b 315 // create the main application window
9a83f860 316 m_frame = new MyFrame(wxT("Popup wxWidgets App"));
98d5b42b
RR
317
318 // and show it (the frames, unlike simple controls, are not shown when
319 // created initially)
b6de8445 320 m_frame->Show(true);
98d5b42b
RR
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
332BEGIN_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)
509f339a 336 EVT_ACTIVATE(MyFrame::OnActivate)
98d5b42b 337 EVT_BUTTON(Minimal_StartSimplePopup, MyFrame::OnStartSimplePopup)
34786588 338 EVT_BUTTON(Minimal_StartScrolledPopup, MyFrame::OnStartScrolledPopup)
98d5b42b
RR
339END_EVENT_TABLE()
340
341MyFrame::MyFrame(const wxString& title)
befc36f8 342: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500,300))
98d5b42b 343{
82ceade7
RD
344 m_simplePopup = m_scrolledPopup = NULL;
345
98d5b42b
RR
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;
2d143b66 353 helpMenu->Append(Minimal_About, wxT("&About\tF1"), wxT("Show about dialog"));
98d5b42b 354
9a83f860
VZ
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"));
98d5b42b
RR
357
358 // now append the freshly created menu to the menu bar...
359 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
360 menuBar->Append(menuFile, wxT("&File"));
361 menuBar->Append(helpMenu, wxT("&Help"));
98d5b42b
RR
362
363 // ... and attach this menu bar to the frame
364 SetMenuBar(menuBar);
365#endif // wxUSE_MENUS
366
414f2513
RD
367#if wxUSE_STATUSBAR
368 // create a status bar just for fun (by default with 1 pane only)
369 CreateStatusBar(2);
9a83f860 370 SetStatusText(wxT("Welcome to wxWidgets!"));
414f2513
RD
371#endif // wxUSE_STATUSBAR
372
82ceade7
RD
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) );
34786588 376
82ceade7
RD
377 m_logWin = new wxTextCtrl( panel, wxID_ANY, wxEmptyString, wxDefaultPosition,
378 wxDefaultSize, wxTE_MULTILINE );
b6de8445
RR
379 m_logWin->SetEditable(false);
380 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
34786588 381 m_logOld = logger->SetActiveTarget( logger );
bd0b594d 382 logger->DisableTimestamp();
dd6a7fb4 383
34786588 384 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
82ceade7
RD
385 topSizer->Add( button1, 0, wxALL, 5 );
386 topSizer->Add( button2, 0, wxALL, 5 );
387 topSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
98d5b42b 388
82ceade7 389 panel->SetSizer( topSizer );
98d5b42b 390
98d5b42b
RR
391}
392
dd6a7fb4
WS
393MyFrame::~MyFrame()
394{
395 delete wxLog::SetActiveTarget(m_logOld);
396}
34786588
VZ
397
398
98d5b42b
RR
399// event handlers
400
49449290 401void MyFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
509f339a
KO
402{
403 wxLogMessage( wxT("In activate...") );
404}
405
98d5b42b
RR
406void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
407{
b6de8445 408 wxLogMessage( wxT("================================================") );
82ceade7 409 delete m_simplePopup;
b7bd58d0 410 m_simplePopup = new SimpleTransientPopup( this, false );
98d5b42b
RR
411 wxWindow *btn = (wxWindow*) event.GetEventObject();
412 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
413 wxSize sz = btn->GetSize();
82ceade7
RD
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();
98d5b42b
RR
417}
418
34786588 419void MyFrame::OnStartScrolledPopup(wxCommandEvent& event)
98d5b42b 420{
34786588 421 wxLogMessage( wxT("================================================") );
82ceade7 422 delete m_scrolledPopup;
b7bd58d0 423 m_scrolledPopup = new SimpleTransientPopup( this, true );
34786588
VZ
424 wxWindow *btn = (wxWindow*) event.GetEventObject();
425 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
426 wxSize sz = btn->GetSize();
82ceade7
RD
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();
98d5b42b
RR
430}
431
432void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
433{
434 MyDialog dialog( wxT("Test") );
435 dialog.ShowModal();
436}
437
438void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
439{
440 // true is to force the frame to close
441 Close(true);
442}
443
444void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
445{
446 wxString msg;
9a83f860
VZ
447 msg.Printf( wxT("This is the About dialog of the popup sample.\n")
448 wxT("Welcome to %s"), wxVERSION_STRING);
98d5b42b 449
9a83f860 450 wxMessageBox(msg, wxT("About Popup"), wxOK | wxICON_INFORMATION, this);
98d5b42b
RR
451}
452
453// ----------------------------------------------------------------------------
454// test dialog
455// ----------------------------------------------------------------------------
456
457BEGIN_EVENT_TABLE(MyDialog, wxDialog)
458 EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
34786588 459 EVT_BUTTON(Minimal_StartScrolledPopup, MyDialog::OnStartScrolledPopup)
98d5b42b
RR
460END_EVENT_TABLE()
461
462MyDialog::MyDialog(const wxString& title)
82ceade7 463 :wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
98d5b42b 464{
82ceade7
RD
465 m_simplePopup = m_scrolledPopup = NULL;
466 wxPanel *panel = new wxPanel(this, -1);
98d5b42b 467
82ceade7
RD
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) );
98d5b42b 470
82ceade7
RD
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
92c01615 479 panel->SetSizerAndFit( topSizer );
98d5b42b
RR
480}
481
482void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
483{
b6de8445 484 wxLogMessage( wxT("================================================") );
82ceade7 485 delete m_simplePopup;
b7bd58d0 486 m_simplePopup = new SimpleTransientPopup( this, false );
98d5b42b
RR
487 wxWindow *btn = (wxWindow*) event.GetEventObject();
488 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
489 wxSize sz = btn->GetSize();
82ceade7
RD
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();
98d5b42b
RR
493}
494
34786588 495void MyDialog::OnStartScrolledPopup(wxCommandEvent& event)
98d5b42b 496{
34786588 497 wxLogMessage( wxT("================================================") );
82ceade7 498 delete m_scrolledPopup;
b7bd58d0 499 m_scrolledPopup = new SimpleTransientPopup( this, true );
34786588
VZ
500 wxWindow *btn = (wxWindow*) event.GetEventObject();
501 wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
502 wxSize sz = btn->GetSize();
82ceade7
RD
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();
98d5b42b 506}