1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Popup wxWidgets sample
4 // Author: Robert Roebling
7 // Copyright: (c) 2005 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers)
32 #include "wx/popupwin.h"
33 #include "wx/spinctrl.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
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"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // IDs for the controls and the menu commands
52 Minimal_Quit
= wxID_EXIT
,
53 Minimal_About
= wxID_ABOUT
,
55 Minimal_StartSimplePopup
,
56 Minimal_StartScrolledPopup
,
62 //----------------------------------------------------------------------------
63 // SimpleTransientPopup
64 //----------------------------------------------------------------------------
65 class SimpleTransientPopup
: public wxPopupTransientWindow
68 SimpleTransientPopup( wxWindow
*parent
, bool scrolled
);
69 virtual ~SimpleTransientPopup();
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 );
78 wxScrolledWindow
*m_panel
;
80 wxSpinCtrl
*m_spinCtrl
;
81 wxStaticText
*m_mouseText
;
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
);
92 DECLARE_CLASS(SimpleTransientPopup
)
96 //----------------------------------------------------------------------------
97 // SimpleTransientPopup
98 //----------------------------------------------------------------------------
99 IMPLEMENT_CLASS(SimpleTransientPopup
,wxPopupTransientWindow
)
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
)
110 SimpleTransientPopup::SimpleTransientPopup( wxWindow
*parent
, bool scrolled
)
111 :wxPopupTransientWindow( parent
)
113 m_panel
= new wxScrolledWindow( this, wxID_ANY
);
114 m_panel
->SetBackgroundColour( *wxLIGHT_GREY
);
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
),
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.") );
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 ->"));
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 );
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
,
150 m_panel
->SetSizer( topSizer
);
153 // Set the fixed size to ensure that the scrollbars are shown.
154 m_panel
->SetSize(300, 300);
156 // And also actually enable them.
157 m_panel
->SetScrollRate(10, 10);
161 // Use the fitting size for the panel if we don't need scrollbars.
162 topSizer
->Fit(m_panel
);
165 SetClientSize(m_panel
->GetSize());
168 SimpleTransientPopup::~SimpleTransientPopup()
172 void SimpleTransientPopup::Popup(wxWindow
* WXUNUSED(focus
))
174 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
175 wxPopupTransientWindow::Popup();
178 void SimpleTransientPopup::OnDismiss()
180 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
181 wxPopupTransientWindow::OnDismiss();
184 bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent
& event
)
186 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
187 return wxPopupTransientWindow::ProcessLeftDown(event
);
189 bool SimpleTransientPopup::Show( bool show
)
191 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show
));
192 return wxPopupTransientWindow::Show(show
);
195 void SimpleTransientPopup::OnSize(wxSizeEvent
&event
)
197 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
201 void SimpleTransientPopup::OnSetFocus(wxFocusEvent
&event
)
203 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
207 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
209 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
213 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
215 wxRect
rect(m_mouseText
->GetRect());
217 rect
.SetWidth(1000000);
218 wxColour
colour(*wxLIGHT_GREY
);
220 if (rect
.Contains(event
.GetPosition()))
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());
227 if (colour
!= m_mouseText
->GetBackgroundColour())
229 m_mouseText
->SetBackgroundColour(colour
);
230 m_mouseText
->Refresh();
235 void SimpleTransientPopup::OnButton(wxCommandEvent
& event
)
237 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event
.GetId());
239 wxButton
*button
= wxDynamicCast(event
.GetEventObject(), wxButton
);
240 if (button
->GetLabel() == wxT("Press Me"))
241 button
->SetLabel(wxT("Pressed"));
243 button
->SetLabel(wxT("Press Me"));
248 void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent
& event
)
250 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %d"),
251 long(this), event
.GetId(), event
.GetInt());
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 class MyDialog
: public wxDialog
262 MyDialog(const wxString
& title
);
264 void OnStartSimplePopup(wxCommandEvent
& event
);
265 void OnStartScrolledPopup(wxCommandEvent
& event
);
268 SimpleTransientPopup
*m_simplePopup
;
269 SimpleTransientPopup
*m_scrolledPopup
;
270 DECLARE_EVENT_TABLE()
273 class MyFrame
: public wxFrame
276 MyFrame(const wxString
& title
);
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
);
287 SimpleTransientPopup
*m_simplePopup
;
288 SimpleTransientPopup
*m_scrolledPopup
;
289 wxTextCtrl
*m_logWin
;
291 DECLARE_EVENT_TABLE()
294 class MyApp
: public wxApp
297 virtual bool OnInit();
302 // ----------------------------------------------------------------------------
303 // event tables and other macros for wxWidgets
304 // ----------------------------------------------------------------------------
309 // 'Main program' equivalent: the program execution "starts" here
312 if ( !wxApp::OnInit() )
315 // create the main application window
316 m_frame
= new MyFrame(wxT("Popup wxWidgets App"));
318 // and show it (the frames, unlike simple controls, are not shown when
319 // created initially)
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.
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
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
)
341 MyFrame::MyFrame(const wxString
& title
)
342 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,300))
344 m_simplePopup
= m_scrolledPopup
= NULL
;
346 SetIcon(wxICON(sample
));
349 wxMenu
*menuFile
= new wxMenu
;
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"));
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"));
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"));
363 // ... and attach this menu bar to the frame
365 #endif // wxUSE_MENUS
368 // create a status bar just for fun (by default with 1 pane only)
370 SetStatusText(wxT("Welcome to wxWidgets!"));
371 #endif // wxUSE_STATUSBAR
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) );
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();
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 );
389 panel
->SetSizer( topSizer
);
395 delete wxLog::SetActiveTarget(m_logOld
);
401 void MyFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
403 wxLogMessage( wxT("In activate...") );
406 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
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();
419 void MyFrame::OnStartScrolledPopup(wxCommandEvent
& event
)
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();
432 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
434 MyDialog
dialog( wxT("Test") );
438 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
440 // true is to force the frame to close
444 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
447 msg
.Printf( wxT("This is the About dialog of the popup sample.\n")
448 wxT("Welcome to %s"), wxVERSION_STRING
);
450 wxMessageBox(msg
, wxT("About Popup"), wxOK
| wxICON_INFORMATION
, this);
453 // ----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
458 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
459 EVT_BUTTON(Minimal_StartScrolledPopup
, MyDialog::OnStartScrolledPopup
)
462 MyDialog::MyDialog(const wxString
& title
)
463 :wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
465 m_simplePopup
= m_scrolledPopup
= NULL
;
466 wxPanel
*panel
= new wxPanel(this, -1);
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) );
471 wxButton
*okButton
= new wxButton( panel
, wxID_OK
, wxT("OK"), wxPoint(20,200) );
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 );
479 panel
->SetSizerAndFit( topSizer
);
482 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
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();
495 void MyDialog::OnStartScrolledPopup(wxCommandEvent
& event
)
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();