1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Popup wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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"
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 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "../sample.xpm"
45 class SimpleTransientPopup
: public wxPopupTransientWindow
48 SimpleTransientPopup( wxWindow
*parent
);
49 virtual ~SimpleTransientPopup();
57 void OnMouse( wxMouseEvent
&event
);
58 void OnSize( wxSizeEvent
&event
);
59 void OnSetFocus( wxFocusEvent
&event
);
60 void OnKillFocus( wxFocusEvent
&event
);
63 DECLARE_CLASS(SimpleTransientPopup
)
68 //----------------------------------------------------------------------------
69 // SimpleTransientPopup
70 //----------------------------------------------------------------------------
72 IMPLEMENT_CLASS(SimpleTransientPopup
,wxPopupTransientWindow
)
74 BEGIN_EVENT_TABLE(SimpleTransientPopup
,wxPopupTransientWindow
)
75 EVT_MOUSE_EVENTS( SimpleTransientPopup::OnMouse
)
76 EVT_SIZE( SimpleTransientPopup::OnSize
)
77 EVT_SET_FOCUS( SimpleTransientPopup::OnSetFocus
)
78 EVT_KILL_FOCUS( SimpleTransientPopup::OnKillFocus
)
81 SimpleTransientPopup::SimpleTransientPopup( wxWindow
*parent
) :
82 wxPopupTransientWindow( parent
)
84 m_panel
= new wxPanel( this, -1 );
85 m_panel
->SetBackgroundColour( *wxLIGHT_GREY
);
86 wxStaticText
*text
= new wxStaticText( m_panel
, -1,
87 wxT("wx.PopupTransientWindow is a\n")
88 wxT("wx.PopupWindow which disappears\n")
89 wxT("automatically when the user\n")
90 wxT("clicks the mouse outside it or if it\n")
91 wxT("(or its first child) loses focus in \n")
92 wxT("any other way."), wxPoint( 10,10) );
93 wxSize size
= text
->GetBestSize();
94 m_panel
->SetSize( size
.x
+20, size
.y
+20 );
95 SetClientSize( size
.x
+20, size
.y
+20 );
98 SimpleTransientPopup::~SimpleTransientPopup()
102 void SimpleTransientPopup::OnDismiss()
106 void SimpleTransientPopup::OnSize(wxSizeEvent
&event
)
111 void SimpleTransientPopup::OnSetFocus(wxFocusEvent
&event
)
115 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
119 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class MyApp
: public wxApp
131 virtual bool OnInit();
134 class MyDialog
: public wxDialog
137 MyDialog(const wxString
& title
);
139 void OnStartSimplePopup(wxCommandEvent
& event
);
140 void OnStartComplexPopup(wxCommandEvent
& event
);
143 DECLARE_EVENT_TABLE()
146 class MyFrame
: public wxFrame
149 MyFrame(const wxString
& title
);
151 void OnQuit(wxCommandEvent
& event
);
152 void OnAbout(wxCommandEvent
& event
);
153 void OnTestDialog(wxCommandEvent
& event
);
154 void OnStartSimplePopup(wxCommandEvent
& event
);
155 void OnStartComplexPopup(wxCommandEvent
& event
);
158 DECLARE_EVENT_TABLE()
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 // IDs for the controls and the menu commands
168 Minimal_Quit
= wxID_EXIT
,
169 Minimal_About
= wxID_ABOUT
,
171 Minimal_StartSimplePopup
,
172 Minimal_StartComplexPopup
,
175 // ----------------------------------------------------------------------------
176 // event tables and other macros for wxWidgets
177 // ----------------------------------------------------------------------------
182 // 'Main program' equivalent: the program execution "starts" here
185 // create the main application window
186 MyFrame
*frame
= new MyFrame(_T("Popup wxWidgets App"));
188 // and show it (the frames, unlike simple controls, are not shown when
189 // created initially)
192 // success: wxApp::OnRun() will be called which will enter the main message
193 // loop and the application will run. If we returned false here, the
194 // application would exit immediately.
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
203 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
204 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
205 EVT_MENU(Minimal_TestDialog
, MyFrame::OnTestDialog
)
206 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame::OnStartSimplePopup
)
207 EVT_BUTTON(Minimal_StartComplexPopup
, MyFrame::OnStartComplexPopup
)
210 MyFrame::MyFrame(const wxString
& title
)
211 : wxFrame(NULL
, wxID_ANY
, title
)
213 SetIcon(wxICON(sample
));
216 wxMenu
*menuFile
= new wxMenu
;
218 // the "About" item should be in the help menu
219 wxMenu
*helpMenu
= new wxMenu
;
220 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
222 menuFile
->Append(Minimal_TestDialog
, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
223 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
225 // now append the freshly created menu to the menu bar...
226 wxMenuBar
*menuBar
= new wxMenuBar();
227 menuBar
->Append(menuFile
, _T("&File"));
228 menuBar
->Append(helpMenu
, _T("&Help"));
230 // ... and attach this menu bar to the frame
232 #endif // wxUSE_MENUS
234 new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
235 new wxButton( this, Minimal_StartComplexPopup
, wxT("Show complex popup"), wxPoint(20,120) );
239 // create a status bar just for fun (by default with 1 pane only)
241 SetStatusText(_T("Welcome to wxWidgets!"));
242 #endif // wxUSE_STATUSBAR
247 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
249 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
250 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
251 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
252 wxSize sz
= btn
->GetSize();
253 popup
->Position( pos
, sz
);
257 void MyFrame::OnStartComplexPopup(wxCommandEvent
& WXUNUSED(event
))
261 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
263 MyDialog
dialog( wxT("Test") );
267 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
269 // true is to force the frame to close
273 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
276 msg
.Printf( _T("This is the About dialog of the popup sample.\n")
277 _T("Welcome to %s"), wxVERSION_STRING
);
279 wxMessageBox(msg
, _T("About Popup"), wxOK
| wxICON_INFORMATION
, this);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
287 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
288 EVT_BUTTON(Minimal_StartComplexPopup
, MyDialog::OnStartComplexPopup
)
291 MyDialog::MyDialog(const wxString
& title
)
292 : wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
295 new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
296 new wxButton( this, Minimal_StartComplexPopup
, wxT("Show complex popup"), wxPoint(20,80) );
298 new wxButton( this, wxID_OK
, wxT("OK"), wxPoint(20,200) );
301 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
303 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
304 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
305 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
306 wxSize sz
= btn
->GetSize();
307 popup
->Position( pos
, sz
);
311 void MyDialog::OnStartComplexPopup(wxCommandEvent
& WXUNUSED(event
))