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"
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
)
116 void SimpleTransientPopup::OnKillFocus(wxFocusEvent
&event
)
121 void SimpleTransientPopup::OnMouse(wxMouseEvent
&event
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 class MyApp
: public wxApp
133 virtual bool OnInit();
136 class MyDialog
: public wxDialog
139 MyDialog(const wxString
& title
);
141 void OnStartSimplePopup(wxCommandEvent
& event
);
142 void OnStartComplexPopup(wxCommandEvent
& event
);
145 DECLARE_EVENT_TABLE()
148 class MyFrame
: public wxFrame
151 MyFrame(const wxString
& title
);
153 void OnQuit(wxCommandEvent
& event
);
154 void OnAbout(wxCommandEvent
& event
);
155 void OnTestDialog(wxCommandEvent
& event
);
156 void OnStartSimplePopup(wxCommandEvent
& event
);
157 void OnStartComplexPopup(wxCommandEvent
& event
);
160 DECLARE_EVENT_TABLE()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // IDs for the controls and the menu commands
170 Minimal_Quit
= wxID_EXIT
,
171 Minimal_About
= wxID_ABOUT
,
173 Minimal_StartSimplePopup
,
174 Minimal_StartComplexPopup
,
177 // ----------------------------------------------------------------------------
178 // event tables and other macros for wxWidgets
179 // ----------------------------------------------------------------------------
184 // 'Main program' equivalent: the program execution "starts" here
187 // create the main application window
188 MyFrame
*frame
= new MyFrame(_T("Popup wxWidgets App"));
190 // and show it (the frames, unlike simple controls, are not shown when
191 // created initially)
194 // success: wxApp::OnRun() will be called which will enter the main message
195 // loop and the application will run. If we returned false here, the
196 // application would exit immediately.
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
205 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
206 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
207 EVT_MENU(Minimal_TestDialog
, MyFrame::OnTestDialog
)
208 EVT_BUTTON(Minimal_StartSimplePopup
, MyFrame::OnStartSimplePopup
)
209 EVT_BUTTON(Minimal_StartComplexPopup
, MyFrame::OnStartComplexPopup
)
212 MyFrame::MyFrame(const wxString
& title
)
213 : wxFrame(NULL
, wxID_ANY
, title
)
215 SetIcon(wxICON(sample
));
218 wxMenu
*menuFile
= new wxMenu
;
220 // the "About" item should be in the help menu
221 wxMenu
*helpMenu
= new wxMenu
;
222 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
224 menuFile
->Append(Minimal_TestDialog
, _T("&Test dialog\tAlt-T"), _T("Test dialog"));
225 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
227 // now append the freshly created menu to the menu bar...
228 wxMenuBar
*menuBar
= new wxMenuBar();
229 menuBar
->Append(menuFile
, _T("&File"));
230 menuBar
->Append(helpMenu
, _T("&Help"));
232 // ... and attach this menu bar to the frame
234 #endif // wxUSE_MENUS
236 new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
237 new wxButton( this, Minimal_StartComplexPopup
, wxT("Show complex popup"), wxPoint(20,120) );
241 // create a status bar just for fun (by default with 1 pane only)
243 SetStatusText(_T("Welcome to wxWidgets!"));
244 #endif // wxUSE_STATUSBAR
249 void MyFrame::OnStartSimplePopup(wxCommandEvent
& event
)
251 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
252 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
253 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
254 wxSize sz
= btn
->GetSize();
255 popup
->Position( pos
, sz
);
259 void MyFrame::OnStartComplexPopup(wxCommandEvent
& WXUNUSED(event
))
263 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
265 MyDialog
dialog( wxT("Test") );
269 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
271 // true is to force the frame to close
275 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
278 msg
.Printf( _T("This is the About dialog of the popup sample.\n")
279 _T("Welcome to %s"), wxVERSION_STRING
);
281 wxMessageBox(msg
, _T("About Popup"), wxOK
| wxICON_INFORMATION
, this);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
289 EVT_BUTTON(Minimal_StartSimplePopup
, MyDialog::OnStartSimplePopup
)
290 EVT_BUTTON(Minimal_StartComplexPopup
, MyDialog::OnStartComplexPopup
)
293 MyDialog::MyDialog(const wxString
& title
)
294 : wxDialog(NULL
, wxID_ANY
, title
, wxPoint(50,50), wxSize(400,300))
297 new wxButton( this, Minimal_StartSimplePopup
, wxT("Show simple popup"), wxPoint(20,20) );
298 new wxButton( this, Minimal_StartComplexPopup
, wxT("Show complex popup"), wxPoint(20,80) );
300 new wxButton( this, wxID_OK
, wxT("OK"), wxPoint(20,200) );
303 void MyDialog::OnStartSimplePopup(wxCommandEvent
& event
)
305 SimpleTransientPopup
* popup
= new SimpleTransientPopup( this );
306 wxWindow
*btn
= (wxWindow
*) event
.GetEventObject();
307 wxPoint pos
= btn
->ClientToScreen( wxPoint(0,0) );
308 wxSize sz
= btn
->GetSize();
309 popup
->Position( pos
, sz
);
313 void MyDialog::OnStartComplexPopup(wxCommandEvent
& WXUNUSED(event
))