1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Popup wxWidgets sample
4 // Author: Robert Roebling
8 // Copyright: (c) 2005 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/popupwin.h"
34 #include "wx/spinctrl.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
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"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // IDs for the controls and the menu commands
53 Minimal_Quit
= wxID_EXIT
,
54 Minimal_About
= wxID_ABOUT
,
56 Minimal_StartSimplePopup
,
57 Minimal_StartScrolledPopup
,
63 //----------------------------------------------------------------------------
64 // SimpleTransientPopup
65 //----------------------------------------------------------------------------
66 class SimpleTransientPopup
: public wxPopupTransientWindow
69 SimpleTransientPopup( wxWindow
*parent
);
70 virtual ~SimpleTransientPopup();
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 );
78 wxScrolledWindow
* GetChild() { return m_panel
; }
81 wxScrolledWindow
*m_panel
;
83 wxSpinCtrl
*m_spinCtrl
;
84 wxStaticText
*m_mouseText
;
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
);
95 DECLARE_CLASS(SimpleTransientPopup
)
99 //----------------------------------------------------------------------------
100 // SimpleTransientPopup
101 //----------------------------------------------------------------------------
102 IMPLEMENT_CLASS(SimpleTransientPopup
,wxPopupTransientWindow
)
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
)
113 SimpleTransientPopup::SimpleTransientPopup( wxWindow
*parent
)
114 :wxPopupTransientWindow( parent
)
116 m_panel
= new wxScrolledWindow( this, wxID_ANY
);
117 m_panel
->SetBackgroundColour( *wxLIGHT_GREY
);
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
),
126 wxStaticText
*text
= new wxStaticText( m_panel
, wxID_ANY
,
127 wxT("wxPopupTransientWindow is a\n")
128 wxT("wxPopupWindow 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.") );
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 ->"));
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 );
145 m_panel
->SetSizer( topSizer
);
146 topSizer
->Fit(m_panel
);
150 SimpleTransientPopup::~SimpleTransientPopup()
154 void SimpleTransientPopup::Popup(wxWindow
*focus
)
156 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
157 wxPopupTransientWindow::Popup(focus
? focus
: m_panel
);
160 void SimpleTransientPopup::OnDismiss()
162 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
163 wxPopupTransientWindow::OnDismiss();
166 bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent
& event
)
168 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
169 return wxPopupTransientWindow::ProcessLeftDown(event
);
171 bool SimpleTransientPopup::Show( bool show
)
173 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show
));
174 return wxPopupTransientWindow::Show(show
);
177 void SimpleTransientPopup::OnSize(wxSizeEvent
&event
)
179 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
183 void SimpleTransientPopup::OnSetFocus(wxFocusEvent
&event
)
185 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
189 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
191 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
195 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
197 wxRect
rect(m_mouseText
->GetRect());
199 rect
.SetWidth(1000000);
200 wxColour
colour(*wxLIGHT_GREY
);
202 if (rect
.Contains(event
.GetPosition()))
204 colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
205 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"),
206 long(event
.GetEventObject()), event
.GetX(), event
.GetY());
209 if (colour
!= m_mouseText
->GetBackgroundColour())
211 m_mouseText
->SetBackgroundColour(colour
);
212 m_mouseText
->Refresh();
217 void SimpleTransientPopup::OnButton(wxCommandEvent
& event
)
219 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event
.GetId());
221 wxButton
*button
= wxDynamicCast(event
.GetEventObject(), wxButton
);
222 if (button
->GetLabel() == wxT("Press Me"))
223 button
->SetLabel(wxT("Pressed"));
225 button
->SetLabel(wxT("Press Me"));
230 void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent
& event
)
232 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %d"),
233 long(this), event
.GetId(), event
.GetInt());
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 class MyDialog
: public wxDialog
244 MyDialog(const wxString
& title
);
246 void OnStartSimplePopup(wxCommandEvent
& event
);
247 void OnStartScrolledPopup(wxCommandEvent
& event
);
250 SimpleTransientPopup
*m_simplePopup
;
251 SimpleTransientPopup
*m_scrolledPopup
;
252 DECLARE_EVENT_TABLE()
255 class MyFrame
: public wxFrame
258 MyFrame(const wxString
& title
);
261 void OnQuit(wxCommandEvent
& event
);
262 void OnAbout(wxCommandEvent
& event
);
263 void OnTestDialog(wxCommandEvent
& event
);
264 void OnStartSimplePopup(wxCommandEvent
& event
);
265 void OnStartScrolledPopup(wxCommandEvent
& event
);
268 SimpleTransientPopup
*m_simplePopup
;
269 SimpleTransientPopup
*m_scrolledPopup
;
270 wxTextCtrl
*m_logWin
;
272 DECLARE_EVENT_TABLE()
275 class MyApp
: public wxApp
278 virtual bool OnInit();
283 // ----------------------------------------------------------------------------
284 // event tables and other macros for wxWidgets
285 // ----------------------------------------------------------------------------
290 // 'Main program' equivalent: the program execution "starts" here
293 if ( !wxApp::OnInit() )
296 // create the main application window
297 m_frame
= new MyFrame(_T("Popup wxWidgets App"));
299 // and show it (the frames, unlike simple controls, are not shown when
300 // created initially)
303 // success: wxApp::OnRun() will be called which will enter the main message
304 // loop and the application will run. If we returned false here, the
305 // application would exit immediately.
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
314 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
315 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
316 EVT_MENU(Minimal_TestDialog
, MyFrame::OnTestDialog
)
317 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame::OnStartSimplePopup
)
318 EVT_BUTTON(Minimal_StartScrolledPopup
, MyFrame::OnStartScrolledPopup
)
321 MyFrame::MyFrame(const wxString
& title
)
322 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,300))
324 m_simplePopup
= m_scrolledPopup
= NULL
;
326 SetIcon(wxICON(sample
));
329 wxMenu
*menuFile
= new wxMenu
;
331 // the "About" item should be in the help menu
332 wxMenu
*helpMenu
= new wxMenu
;
333 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
335 menuFile
->Append(Minimal_TestDialog
, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
336 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
338 // now append the freshly created menu to the menu bar...
339 wxMenuBar
*menuBar
= new wxMenuBar();
340 menuBar
->Append(menuFile
, _T("&File"));
341 menuBar
->Append(helpMenu
, _T("&Help"));
343 // ... and attach this menu bar to the frame
345 #endif // wxUSE_MENUS
348 // create a status bar just for fun (by default with 1 pane only)
350 SetStatusText(_T("Welcome to wxWidgets!"));
351 #endif // wxUSE_STATUSBAR
353 wxPanel
*panel
= new wxPanel(this, -1);
354 wxButton
*button1
= new wxButton( panel
, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
355 wxButton
*button2
= new wxButton( panel
, Minimal_StartScrolledPopup
, wxT("Show scrolled popup"), wxPoint(20,70) );
357 m_logWin
= new wxTextCtrl( panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
358 wxDefaultSize
, wxTE_MULTILINE
);
359 m_logWin
->SetEditable(false);
360 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
361 m_logOld
= logger
->SetActiveTarget( logger
);
362 logger
->DisableTimestamp();
364 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
365 topSizer
->Add( button1
, 0, wxALL
, 5 );
366 topSizer
->Add( button2
, 0, wxALL
, 5 );
367 topSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 5 );
369 panel
->SetSizer( topSizer
);
375 delete wxLog::SetActiveTarget(m_logOld
);
381 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
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();
394 void MyFrame::OnStartScrolledPopup(wxCommandEvent
& event
)
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();
408 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
410 MyDialog
dialog( wxT("Test") );
414 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
416 // true is to force the frame to close
420 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
423 msg
.Printf( _T("This is the About dialog of the popup sample.\n")
424 _T("Welcome to %s"), wxVERSION_STRING
);
426 wxMessageBox(msg
, _T("About Popup"), wxOK
| wxICON_INFORMATION
, this);
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
434 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
435 EVT_BUTTON(Minimal_StartScrolledPopup
, MyDialog::OnStartScrolledPopup
)
438 MyDialog::MyDialog(const wxString
& title
)
439 :wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
441 m_simplePopup
= m_scrolledPopup
= NULL
;
442 wxPanel
*panel
= new wxPanel(this, -1);
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) );
447 wxButton
*okButton
= new wxButton( panel
, wxID_OK
, wxT("OK"), wxPoint(20,200) );
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 );
455 panel
->SetSizerAndFit( topSizer
);
458 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
460 wxLogMessage( wxT("================================================") );
461 delete m_simplePopup
;
462 m_simplePopup
= new SimpleTransientPopup( this );
463 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
464 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
465 wxSize sz
= btn
->GetSize();
466 m_simplePopup
->Position( pos
, sz
);
467 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
);
468 m_simplePopup
->Popup();
471 void MyDialog::OnStartScrolledPopup(wxCommandEvent
& event
)
473 wxLogMessage( wxT("================================================") );
474 delete m_scrolledPopup
;
475 m_scrolledPopup
= new SimpleTransientPopup( this );
476 m_scrolledPopup
->GetChild()->SetScrollbars(1, 1, 1000, 1000);
477 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
478 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
479 wxSize sz
= btn
->GetSize();
480 m_scrolledPopup
->Position( pos
, sz
);
481 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
);
482 m_scrolledPopup
->Popup();