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
;
86 void OnMouse( wxMouseEvent
&event
);
87 void OnSize( wxSizeEvent
&event
);
88 void OnSetFocus( wxFocusEvent
&event
);
89 void OnKillFocus( wxFocusEvent
&event
);
90 void OnButton( wxCommandEvent
& event
);
91 void OnSpinCtrl( wxSpinEvent
& event
);
94 DECLARE_CLASS(SimpleTransientPopup
)
98 //----------------------------------------------------------------------------
99 // SimpleTransientPopup
100 //----------------------------------------------------------------------------
101 IMPLEMENT_CLASS(SimpleTransientPopup
,wxPopupTransientWindow
)
103 BEGIN_EVENT_TABLE(SimpleTransientPopup
,wxPopupTransientWindow
)
104 EVT_MOUSE_EVENTS( SimpleTransientPopup
::OnMouse
)
105 EVT_SIZE( SimpleTransientPopup
::OnSize
)
106 EVT_SET_FOCUS( SimpleTransientPopup
::OnSetFocus
)
107 EVT_KILL_FOCUS( SimpleTransientPopup
::OnKillFocus
)
108 EVT_BUTTON( Minimal_PopupButton
, SimpleTransientPopup
::OnButton
)
109 EVT_SPINCTRL( Minimal_PopupSpinctrl
, SimpleTransientPopup
::OnSpinCtrl
)
112 SimpleTransientPopup
::SimpleTransientPopup( wxWindow
*parent
)
113 :wxPopupTransientWindow( parent
)
115 m_panel
= new wxScrolledWindow( this, wxID_ANY
);
116 m_panel
->SetBackgroundColour( *wxLIGHT_GREY
);
118 // Keep this code to verify if mouse events work, they're required if
119 // you're making a control like a combobox where the items are highlighted
120 // under the cursor, the m_panel is set focus in the Popup() function
121 //m_panel->Connect(wxEVT_MOTION,
122 // wxMouseEventHandler(SimpleTransientPopup::OnMouse),
125 wxStaticText
*text
= new wxStaticText( m_panel
, wxID_ANY
,
126 wxT("wx.PopupTransientWindow is a\n")
127 wxT("wx.PopupWindow which disappears\n")
128 wxT("automatically when the user\n")
129 wxT("clicks the mouse outside it or if it\n")
130 wxT("(or its first child) loses focus in \n")
131 wxT("any other way."), wxPoint( 10,10) );
132 wxSize size
= text
->GetBestSize();
134 m_button
= new wxButton(m_panel
, Minimal_PopupButton
, wxT("Press Me"), wxPoint(0, size
.y
+ 10));
135 size
.y
= m_button
->GetRect().GetBottom();
136 m_spinCtrl
= new wxSpinCtrl(m_panel
, Minimal_PopupSpinctrl
, wxT("Hello"), wxPoint(0, size
.y
+ 5));
137 size
.y
= m_spinCtrl
->GetRect().GetBottom();
139 m_panel
->SetSize( size
.x
+20, size
.y
+20 );
140 SetClientSize( size
.x
+20, size
.y
+20 );
143 SimpleTransientPopup
::~SimpleTransientPopup()
147 void SimpleTransientPopup
::Popup(wxWindow
*focus
)
149 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
150 wxPopupTransientWindow
::Popup(focus ? focus
: m_panel
);
153 void SimpleTransientPopup
::OnDismiss()
155 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
156 wxPopupTransientWindow
::OnDismiss();
159 bool SimpleTransientPopup
::ProcessLeftDown(wxMouseEvent
& event
)
161 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
162 return wxPopupTransientWindow
::ProcessLeftDown(event
);
164 bool SimpleTransientPopup
::Show( bool show
)
166 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show
));
167 return wxPopupTransientWindow
::Show(show
);
170 void SimpleTransientPopup
::OnSize(wxSizeEvent
&event
)
172 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
176 void SimpleTransientPopup
::OnSetFocus(wxFocusEvent
&event
)
178 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
182 void SimpleTransientPopup
::OnKillFocus(wxFocusEvent
&event
)
184 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
188 void SimpleTransientPopup
::OnMouse(wxMouseEvent
&event
)
190 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"), long(event
.GetEventObject()), event
.GetX(), event
.GetY());
194 void SimpleTransientPopup
::OnButton(wxCommandEvent
& event
)
196 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event
.GetId());
198 wxButton
*button
= wxDynamicCast(event
.GetEventObject(), wxButton
);
199 if (button
->GetLabel() == wxT("Press Me"))
200 button
->SetLabel(wxT("Pressed"));
202 button
->SetLabel(wxT("Press Me"));
207 void SimpleTransientPopup
::OnSpinCtrl(wxSpinEvent
& event
)
209 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %ld"), long(this), event
.GetId(), event
.GetInt());
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 class MyDialog
: public wxDialog
220 MyDialog(const wxString
& title
);
222 void OnStartSimplePopup(wxCommandEvent
& event
);
223 void OnStartScrolledPopup(wxCommandEvent
& event
);
226 SimpleTransientPopup
*m_simplePopup
;
227 SimpleTransientPopup
*m_scrolledPopup
;
228 DECLARE_EVENT_TABLE()
231 class MyFrame
: public wxFrame
234 MyFrame(const wxString
& title
);
237 void OnQuit(wxCommandEvent
& event
);
238 void OnAbout(wxCommandEvent
& event
);
239 void OnTestDialog(wxCommandEvent
& event
);
240 void OnStartSimplePopup(wxCommandEvent
& event
);
241 void OnStartScrolledPopup(wxCommandEvent
& event
);
244 SimpleTransientPopup
*m_simplePopup
;
245 SimpleTransientPopup
*m_scrolledPopup
;
246 wxTextCtrl
*m_logWin
;
248 DECLARE_EVENT_TABLE()
251 class MyApp
: public wxApp
254 virtual bool OnInit();
259 // ----------------------------------------------------------------------------
260 // event tables and other macros for wxWidgets
261 // ----------------------------------------------------------------------------
266 // 'Main program' equivalent: the program execution "starts" here
269 // create the main application window
270 m_frame
= new MyFrame(_T("Popup wxWidgets App"));
272 // and show it (the frames, unlike simple controls, are not shown when
273 // created initially)
276 // success: wxApp::OnRun() will be called which will enter the main message
277 // loop and the application will run. If we returned false here, the
278 // application would exit immediately.
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
287 EVT_MENU(Minimal_Quit
, MyFrame
::OnQuit
)
288 EVT_MENU(Minimal_About
, MyFrame
::OnAbout
)
289 EVT_MENU(Minimal_TestDialog
, MyFrame
::OnTestDialog
)
290 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame
::OnStartSimplePopup
)
291 EVT_BUTTON(Minimal_StartScrolledPopup
, MyFrame
::OnStartScrolledPopup
)
294 MyFrame
::MyFrame(const wxString
& title
)
295 : wxFrame(NULL
, wxID_ANY
, title
)
297 m_simplePopup
= m_scrolledPopup
= NULL
;
299 SetIcon(wxICON(sample
));
302 wxMenu
*menuFile
= new wxMenu
;
304 // the "About" item should be in the help menu
305 wxMenu
*helpMenu
= new wxMenu
;
306 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
308 menuFile
->Append(Minimal_TestDialog
, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
309 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
311 // now append the freshly created menu to the menu bar...
312 wxMenuBar
*menuBar
= new wxMenuBar();
313 menuBar
->Append(menuFile
, _T("&File"));
314 menuBar
->Append(helpMenu
, _T("&Help"));
316 // ... and attach this menu bar to the frame
318 #endif // wxUSE_MENUS
320 wxPanel
*panel
= new wxPanel(this, -1);
321 wxButton
*button1
= new wxButton( panel
, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
322 wxButton
*button2
= new wxButton( panel
, Minimal_StartScrolledPopup
, wxT("Show scrolled popup"), wxPoint(20,70) );
324 m_logWin
= new wxTextCtrl( panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
325 wxDefaultSize
, wxTE_MULTILINE
);
326 m_logWin
->SetEditable(false);
327 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
328 m_logOld
= logger
->SetActiveTarget( logger
);
329 logger
->SetTimestamp( NULL
);
331 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
332 topSizer
->Add( button1
, 0, wxALL
, 5 );
333 topSizer
->Add( button2
, 0, wxALL
, 5 );
334 topSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 5 );
336 panel
->SetAutoLayout( true );
337 panel
->SetSizer( topSizer
);
340 // create a status bar just for fun (by default with 1 pane only)
342 SetStatusText(_T("Welcome to wxWidgets!"));
343 #endif // wxUSE_STATUSBAR
348 delete wxLog
::SetActiveTarget(m_logOld
);
354 void MyFrame
::OnStartSimplePopup(wxCommandEvent
& event
)
356 wxLogMessage( wxT("================================================") );
357 delete m_simplePopup
;
358 m_simplePopup
= new SimpleTransientPopup( this );
359 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
360 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
361 wxSize sz
= btn
->GetSize();
362 m_simplePopup
->Position( pos
, sz
);
363 wxLogMessage( wxT("0x%lx Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(m_simplePopup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);
364 m_simplePopup
->Popup();
367 void MyFrame
::OnStartScrolledPopup(wxCommandEvent
& event
)
369 wxLogMessage( wxT("================================================") );
370 delete m_scrolledPopup
;
371 m_scrolledPopup
= new SimpleTransientPopup( this );
372 m_scrolledPopup
->GetChild()->SetScrollbars(1, 1, 1000, 1000);
373 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
374 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
375 wxSize sz
= btn
->GetSize();
376 m_scrolledPopup
->Position( pos
, sz
);
377 wxLogMessage( wxT("0x%lx Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(m_scrolledPopup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);
378 m_scrolledPopup
->Popup();
381 void MyFrame
::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
383 MyDialog
dialog( wxT("Test") );
387 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
389 // true is to force the frame to close
393 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
396 msg
.Printf( _T("This is the About dialog of the popup sample.\n")
397 _T("Welcome to %s"), wxVERSION_STRING
);
399 wxMessageBox(msg
, _T("About Popup"), wxOK
| wxICON_INFORMATION
, this);
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
407 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog
::OnStartSimplePopup
)
408 EVT_BUTTON(Minimal_StartScrolledPopup
, MyDialog
::OnStartScrolledPopup
)
411 MyDialog
::MyDialog(const wxString
& title
)
412 :wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
414 m_simplePopup
= m_scrolledPopup
= NULL
;
415 wxPanel
*panel
= new wxPanel(this, -1);
417 wxButton
*button1
= new wxButton( panel
, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
418 wxButton
*button2
= new wxButton( panel
, Minimal_StartScrolledPopup
, wxT("Show scrolled popup"), wxPoint(20,60) );
420 wxButton
*okButton
= new wxButton( panel
, wxID_OK
, wxT("OK"), wxPoint(20,200) );
422 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
423 topSizer
->Add( button1
, 0, wxALL
, 5 );
424 topSizer
->Add( button2
, 0, wxALL
, 5 );
425 topSizer
->AddSpacer(40);
426 topSizer
->Add( okButton
, 0, wxALL
, 5 );
428 panel
->SetAutoLayout( true );
429 panel
->SetSizer( topSizer
);
433 void MyDialog
::OnStartSimplePopup(wxCommandEvent
& event
)
435 wxLogMessage( wxT("================================================") );
436 delete m_simplePopup
;
437 m_simplePopup
= new SimpleTransientPopup( this );
438 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
439 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
440 wxSize sz
= btn
->GetSize();
441 m_simplePopup
->Position( pos
, sz
);
442 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
);
443 m_simplePopup
->Popup();
446 void MyDialog
::OnStartScrolledPopup(wxCommandEvent
& event
)
448 wxLogMessage( wxT("================================================") );
449 delete m_scrolledPopup
;
450 m_scrolledPopup
= new SimpleTransientPopup( this );
451 m_scrolledPopup
->GetChild()->SetScrollbars(1, 1, 1000, 1000);
452 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
453 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
454 wxSize sz
= btn
->GetSize();
455 m_scrolledPopup
->Position( pos
, sz
);
456 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
);
457 m_scrolledPopup
->Popup();