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 #ifndef wxHAS_IMAGES_IN_RESOURCES
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
, bool scrolled
);
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 );
79 wxScrolledWindow
*m_panel
;
81 wxSpinCtrl
*m_spinCtrl
;
82 wxStaticText
*m_mouseText
;
85 void OnMouse( wxMouseEvent
&event
);
86 void OnSize( wxSizeEvent
&event
);
87 void OnSetFocus( wxFocusEvent
&event
);
88 void OnKillFocus( wxFocusEvent
&event
);
89 void OnButton( wxCommandEvent
& event
);
90 void OnSpinCtrl( wxSpinEvent
& event
);
93 DECLARE_CLASS(SimpleTransientPopup
)
97 //----------------------------------------------------------------------------
98 // SimpleTransientPopup
99 //----------------------------------------------------------------------------
100 IMPLEMENT_CLASS(SimpleTransientPopup
,wxPopupTransientWindow
)
102 BEGIN_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
)
107 EVT_BUTTON( Minimal_PopupButton
, SimpleTransientPopup::OnButton
)
108 EVT_SPINCTRL( Minimal_PopupSpinctrl
, SimpleTransientPopup::OnSpinCtrl
)
111 SimpleTransientPopup::SimpleTransientPopup( wxWindow
*parent
, bool scrolled
)
112 :wxPopupTransientWindow( parent
)
114 m_panel
= new wxScrolledWindow( this, wxID_ANY
);
115 m_panel
->SetBackgroundColour( *wxLIGHT_GREY
);
117 // Keep this code to verify if mouse events work, they're required if
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
120 m_panel
->Connect(wxEVT_MOTION
,
121 wxMouseEventHandler(SimpleTransientPopup::OnMouse
),
124 wxStaticText
*text
= new wxStaticText( m_panel
, wxID_ANY
,
125 wxT("wxPopupTransientWindow is a\n")
126 wxT("wxPopupWindow which disappears\n")
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")
130 wxT("any other way.") );
132 m_button
= new wxButton(m_panel
, Minimal_PopupButton
, wxT("Press Me"));
133 m_spinCtrl
= new wxSpinCtrl(m_panel
, Minimal_PopupSpinctrl
, wxT("Hello"));
134 m_mouseText
= new wxStaticText(m_panel
, wxID_ANY
,
135 wxT("<- Test Mouse ->"));
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 );
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
,
151 m_panel
->SetSizer( topSizer
);
154 // Set the fixed size to ensure that the scrollbars are shown.
155 m_panel
->SetSize(300, 300);
157 // And also actually enable them.
158 m_panel
->SetScrollRate(10, 10);
162 // Use the fitting size for the panel if we don't need scrollbars.
163 topSizer
->Fit(m_panel
);
166 SetClientSize(m_panel
->GetSize());
169 SimpleTransientPopup::~SimpleTransientPopup()
173 void SimpleTransientPopup::Popup(wxWindow
* WXUNUSED(focus
))
175 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
176 wxPopupTransientWindow::Popup();
179 void SimpleTransientPopup::OnDismiss()
181 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
182 wxPopupTransientWindow::OnDismiss();
185 bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent
& event
)
187 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
188 return wxPopupTransientWindow::ProcessLeftDown(event
);
190 bool SimpleTransientPopup::Show( bool show
)
192 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show
));
193 return wxPopupTransientWindow::Show(show
);
196 void SimpleTransientPopup::OnSize(wxSizeEvent
&event
)
198 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
202 void SimpleTransientPopup::OnSetFocus(wxFocusEvent
&event
)
204 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
208 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
210 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
214 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
216 wxRect
rect(m_mouseText
->GetRect());
218 rect
.SetWidth(1000000);
219 wxColour
colour(*wxLIGHT_GREY
);
221 if (rect
.Contains(event
.GetPosition()))
223 colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
224 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"),
225 long(event
.GetEventObject()), event
.GetX(), event
.GetY());
228 if (colour
!= m_mouseText
->GetBackgroundColour())
230 m_mouseText
->SetBackgroundColour(colour
);
231 m_mouseText
->Refresh();
236 void SimpleTransientPopup::OnButton(wxCommandEvent
& event
)
238 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event
.GetId());
240 wxButton
*button
= wxDynamicCast(event
.GetEventObject(), wxButton
);
241 if (button
->GetLabel() == wxT("Press Me"))
242 button
->SetLabel(wxT("Pressed"));
244 button
->SetLabel(wxT("Press Me"));
249 void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent
& event
)
251 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %d"),
252 long(this), event
.GetId(), event
.GetInt());
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 class MyDialog
: public wxDialog
263 MyDialog(const wxString
& title
);
265 void OnStartSimplePopup(wxCommandEvent
& event
);
266 void OnStartScrolledPopup(wxCommandEvent
& event
);
269 SimpleTransientPopup
*m_simplePopup
;
270 SimpleTransientPopup
*m_scrolledPopup
;
271 DECLARE_EVENT_TABLE()
274 class MyFrame
: public wxFrame
277 MyFrame(const wxString
& title
);
280 void OnQuit(wxCommandEvent
& event
);
281 void OnAbout(wxCommandEvent
& event
);
282 void OnTestDialog(wxCommandEvent
& event
);
283 void OnStartSimplePopup(wxCommandEvent
& event
);
284 void OnStartScrolledPopup(wxCommandEvent
& event
);
285 void OnActivate(wxActivateEvent
& event
);
288 SimpleTransientPopup
*m_simplePopup
;
289 SimpleTransientPopup
*m_scrolledPopup
;
290 wxTextCtrl
*m_logWin
;
292 DECLARE_EVENT_TABLE()
295 class MyApp
: public wxApp
298 virtual bool OnInit();
303 // ----------------------------------------------------------------------------
304 // event tables and other macros for wxWidgets
305 // ----------------------------------------------------------------------------
310 // 'Main program' equivalent: the program execution "starts" here
313 if ( !wxApp::OnInit() )
316 // create the main application window
317 m_frame
= new MyFrame(wxT("Popup wxWidgets App"));
319 // and show it (the frames, unlike simple controls, are not shown when
320 // created initially)
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.
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 BEGIN_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
)
337 EVT_ACTIVATE(MyFrame::OnActivate
)
338 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame::OnStartSimplePopup
)
339 EVT_BUTTON(Minimal_StartScrolledPopup
, MyFrame::OnStartScrolledPopup
)
342 MyFrame::MyFrame(const wxString
& title
)
343 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,300))
345 m_simplePopup
= m_scrolledPopup
= NULL
;
347 SetIcon(wxICON(sample
));
350 wxMenu
*menuFile
= new wxMenu
;
352 // the "About" item should be in the help menu
353 wxMenu
*helpMenu
= new wxMenu
;
354 helpMenu
->Append(Minimal_About
, wxT("&About\tF1"), wxT("Show about dialog"));
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"));
359 // now append the freshly created menu to the menu bar...
360 wxMenuBar
*menuBar
= new wxMenuBar();
361 menuBar
->Append(menuFile
, wxT("&File"));
362 menuBar
->Append(helpMenu
, wxT("&Help"));
364 // ... and attach this menu bar to the frame
366 #endif // wxUSE_MENUS
369 // create a status bar just for fun (by default with 1 pane only)
371 SetStatusText(wxT("Welcome to wxWidgets!"));
372 #endif // wxUSE_STATUSBAR
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) );
378 m_logWin
= new wxTextCtrl( panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
379 wxDefaultSize
, wxTE_MULTILINE
);
380 m_logWin
->SetEditable(false);
381 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
382 m_logOld
= logger
->SetActiveTarget( logger
);
383 logger
->DisableTimestamp();
385 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
386 topSizer
->Add( button1
, 0, wxALL
, 5 );
387 topSizer
->Add( button2
, 0, wxALL
, 5 );
388 topSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 5 );
390 panel
->SetSizer( topSizer
);
396 delete wxLog::SetActiveTarget(m_logOld
);
402 void MyFrame::OnActivate(wxActivateEvent
& WXUNUSED(event
))
404 wxLogMessage( wxT("In activate...") );
407 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
409 wxLogMessage( wxT("================================================") );
410 delete m_simplePopup
;
411 m_simplePopup
= new SimpleTransientPopup( this, false );
412 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
413 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
414 wxSize sz
= btn
->GetSize();
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();
420 void MyFrame::OnStartScrolledPopup(wxCommandEvent
& event
)
422 wxLogMessage( wxT("================================================") );
423 delete m_scrolledPopup
;
424 m_scrolledPopup
= new SimpleTransientPopup( this, true );
425 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
426 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
427 wxSize sz
= btn
->GetSize();
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();
433 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
435 MyDialog
dialog( wxT("Test") );
439 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
441 // true is to force the frame to close
445 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
448 msg
.Printf( wxT("This is the About dialog of the popup sample.\n")
449 wxT("Welcome to %s"), wxVERSION_STRING
);
451 wxMessageBox(msg
, wxT("About Popup"), wxOK
| wxICON_INFORMATION
, this);
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
459 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
460 EVT_BUTTON(Minimal_StartScrolledPopup
, MyDialog::OnStartScrolledPopup
)
463 MyDialog::MyDialog(const wxString
& title
)
464 :wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
466 m_simplePopup
= m_scrolledPopup
= NULL
;
467 wxPanel
*panel
= new wxPanel(this, -1);
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) );
472 wxButton
*okButton
= new wxButton( panel
, wxID_OK
, wxT("OK"), wxPoint(20,200) );
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 );
480 panel
->SetSizerAndFit( topSizer
);
483 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
485 wxLogMessage( wxT("================================================") );
486 delete m_simplePopup
;
487 m_simplePopup
= new SimpleTransientPopup( this, false );
488 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
489 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
490 wxSize sz
= btn
->GetSize();
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();
496 void MyDialog::OnStartScrolledPopup(wxCommandEvent
& event
)
498 wxLogMessage( wxT("================================================") );
499 delete m_scrolledPopup
;
500 m_scrolledPopup
= new SimpleTransientPopup( this, true );
501 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
502 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
503 wxSize sz
= btn
->GetSize();
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();