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
);
117 wxStaticText
*text
= new wxStaticText( m_panel
, wxID_ANY
,
118 wxT("wx.PopupTransientWindow is a\n")
119 wxT("wx.PopupWindow which disappears\n")
120 wxT("automatically when the user\n")
121 wxT("clicks the mouse outside it or if it\n")
122 wxT("(or its first child) loses focus in \n")
123 wxT("any other way."), wxPoint( 10,10) );
124 wxSize size
= text
->GetBestSize();
126 m_button
= new wxButton(m_panel
, Minimal_PopupButton
, wxT("Press Me"), wxPoint(0, size
.y
+ 10));
127 size
.y
= m_button
->GetRect().GetBottom();
128 m_spinCtrl
= new wxSpinCtrl(m_panel
, Minimal_PopupSpinctrl
, wxT("Hello"), wxPoint(0, size
.y
+ 5));
129 size
.y
= m_spinCtrl
->GetRect().GetBottom();
131 m_panel
->SetSize( size
.x
+20, size
.y
+20 );
132 SetClientSize( size
.x
+20, size
.y
+20 );
135 SimpleTransientPopup::~SimpleTransientPopup()
139 void SimpleTransientPopup::Popup(wxWindow
*focus
)
141 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) );
142 wxPopupTransientWindow::Popup(focus
);
145 void SimpleTransientPopup::OnDismiss()
147 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) );
148 wxPopupTransientWindow::OnDismiss();
151 bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent
& event
)
153 wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
154 return wxPopupTransientWindow::ProcessLeftDown(event
);
156 bool SimpleTransientPopup::Show( bool show
)
158 wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show
));
159 return wxPopupTransientWindow::Show(show
);
162 void SimpleTransientPopup::OnSize(wxSizeEvent
&event
)
164 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) );
168 void SimpleTransientPopup::OnSetFocus(wxFocusEvent
&event
)
170 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) );
174 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
176 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) );
180 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
182 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"), long(this), event
.GetX(), event
.GetY());
186 void SimpleTransientPopup::OnButton(wxCommandEvent
& event
)
188 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event
.GetId());
190 wxButton
*button
= wxDynamicCast(event
.GetEventObject(), wxButton
);
191 if (button
->GetLabel() == wxT("Press Me"))
192 button
->SetLabel(wxT("Pressed"));
194 button
->SetLabel(wxT("Press Me"));
199 void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent
& event
)
201 wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %ld"), long(this), event
.GetId(), event
.GetInt());
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 class MyDialog
: public wxDialog
212 MyDialog(const wxString
& title
);
214 void OnStartSimplePopup(wxCommandEvent
& event
);
215 void OnStartScrolledPopup(wxCommandEvent
& event
);
218 DECLARE_EVENT_TABLE()
221 class MyFrame
: public wxFrame
224 MyFrame(const wxString
& title
);
227 void OnQuit(wxCommandEvent
& event
);
228 void OnAbout(wxCommandEvent
& event
);
229 void OnTestDialog(wxCommandEvent
& event
);
230 void OnStartSimplePopup(wxCommandEvent
& event
);
231 void OnStartScrolledPopup(wxCommandEvent
& event
);
234 wxTextCtrl
*m_logWin
;
236 DECLARE_EVENT_TABLE()
239 class MyApp
: public wxApp
242 virtual bool OnInit();
247 // ----------------------------------------------------------------------------
248 // event tables and other macros for wxWidgets
249 // ----------------------------------------------------------------------------
254 // 'Main program' equivalent: the program execution "starts" here
257 // create the main application window
258 m_frame
= new MyFrame(_T("Popup wxWidgets App"));
260 // and show it (the frames, unlike simple controls, are not shown when
261 // created initially)
264 // success: wxApp::OnRun() will be called which will enter the main message
265 // loop and the application will run. If we returned false here, the
266 // application would exit immediately.
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
275 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
276 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
277 EVT_MENU(Minimal_TestDialog
, MyFrame::OnTestDialog
)
278 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame::OnStartSimplePopup
)
279 EVT_BUTTON(Minimal_StartScrolledPopup
, MyFrame::OnStartScrolledPopup
)
282 MyFrame::MyFrame(const wxString
& title
)
283 : wxFrame(NULL
, wxID_ANY
, title
)
285 SetIcon(wxICON(sample
));
288 wxMenu
*menuFile
= new wxMenu
;
290 // the "About" item should be in the help menu
291 wxMenu
*helpMenu
= new wxMenu
;
292 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
294 menuFile
->Append(Minimal_TestDialog
, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
295 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
297 // now append the freshly created menu to the menu bar...
298 wxMenuBar
*menuBar
= new wxMenuBar();
299 menuBar
->Append(menuFile
, _T("&File"));
300 menuBar
->Append(helpMenu
, _T("&Help"));
302 // ... and attach this menu bar to the frame
304 #endif // wxUSE_MENUS
306 wxButton
*button1
= new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
307 wxButton
*button2
= new wxButton( this, Minimal_StartScrolledPopup
, wxT("Show scrolled popup"), wxPoint(20,70) );
309 m_logWin
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
310 wxDefaultSize
, wxTE_MULTILINE
);
311 m_logWin
->SetEditable(false);
312 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
313 m_logOld
= logger
->SetActiveTarget( logger
);
314 logger
->SetTimestamp( NULL
);
316 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
317 topSizer
->Add( button1
, 0 );
318 topSizer
->Add( button2
, 0 );
319 topSizer
->Add( m_logWin
, 1, wxEXPAND
);
321 SetAutoLayout( true );
322 SetSizer( topSizer
);
325 // create a status bar just for fun (by default with 1 pane only)
327 SetStatusText(_T("Welcome to wxWidgets!"));
328 #endif // wxUSE_STATUSBAR
333 delete wxLog::SetActiveTarget(m_logOld
);
339 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
341 wxLogMessage( wxT("================================================") );
342 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
343 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
344 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
345 wxSize sz
= btn
->GetSize();
346 popup
->Position( pos
, sz
);
347 wxLogMessage( wxT("0x%lx Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(popup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);
351 void MyFrame::OnStartScrolledPopup(wxCommandEvent
& event
)
353 wxLogMessage( wxT("================================================") );
354 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
355 popup
->GetChild()->SetScrollbars(1, 1, 1000, 1000);
356 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
357 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
358 wxSize sz
= btn
->GetSize();
359 popup
->Position( pos
, sz
);
360 wxLogMessage( wxT("0x%lx Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(popup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);
364 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
366 MyDialog
dialog( wxT("Test") );
370 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
372 // true is to force the frame to close
376 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
379 msg
.Printf( _T("This is the About dialog of the popup sample.\n")
380 _T("Welcome to %s"), wxVERSION_STRING
);
382 wxMessageBox(msg
, _T("About Popup"), wxOK
| wxICON_INFORMATION
, this);
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
390 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
391 EVT_BUTTON(Minimal_StartScrolledPopup
, MyDialog::OnStartScrolledPopup
)
394 MyDialog::MyDialog(const wxString
& title
)
395 : wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
398 new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
399 new wxButton( this, Minimal_StartScrolledPopup
, wxT("Show scrolled popup"), wxPoint(20,60) );
401 new wxButton( this, wxID_OK
, wxT("OK"), wxPoint(20,200) );
404 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
406 wxLogMessage( wxT("================================================") );
407 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
408 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
409 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
410 wxSize sz
= btn
->GetSize();
411 popup
->Position( pos
, sz
);
412 wxLogMessage( wxT("0x%lx Dialog Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(popup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);
416 void MyDialog::OnStartScrolledPopup(wxCommandEvent
& event
)
418 wxLogMessage( wxT("================================================") );
419 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
420 popup
->GetChild()->SetScrollbars(1, 1, 1000, 1000);
421 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
422 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
423 wxSize sz
= btn
->GetSize();
424 popup
->Position( pos
, sz
);
425 wxLogMessage( wxT("0x%lx Dialog Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(popup
), pos
.x
, pos
.y
, sz
.x
, sz
.y
);