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