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