]> git.saurik.com Git - wxWidgets.git/blob - samples/popup/minimal.cpp
7af30b78fa2f8c207a4c85b6e140718c7b24560e
[wxWidgets.git] / samples / popup / minimal.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Popup wxWidgets sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/popupwin.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
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"
43 #endif
44
45 class SimpleTransientPopup: public wxPopupTransientWindow
46 {
47 public:
48 SimpleTransientPopup( wxWindow *parent );
49 virtual ~SimpleTransientPopup();
50
51 void OnDismiss();
52
53 private:
54 wxPanel *m_panel;
55
56 private:
57 void OnMouse( wxMouseEvent &event );
58 void OnSize( wxSizeEvent &event );
59 void OnSetFocus( wxFocusEvent &event );
60 void OnKillFocus( wxFocusEvent &event );
61
62 private:
63 DECLARE_CLASS(SimpleTransientPopup)
64 DECLARE_EVENT_TABLE()
65 };
66
67
68 //----------------------------------------------------------------------------
69 // SimpleTransientPopup
70 //----------------------------------------------------------------------------
71
72 IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
73
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 )
79 END_EVENT_TABLE()
80
81 SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent ) :
82 wxPopupTransientWindow( parent )
83 {
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 );
96 }
97
98 SimpleTransientPopup::~SimpleTransientPopup()
99 {
100 }
101
102 void SimpleTransientPopup::OnDismiss()
103 {
104 }
105
106 void SimpleTransientPopup::OnSize(wxSizeEvent &event)
107 {
108 event.Skip();
109 }
110
111 void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
112 {
113 }
114
115 void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
116 {
117 }
118
119 void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
120 {
121 event.Skip();
122 }
123
124 // ----------------------------------------------------------------------------
125 // private classes
126 // ----------------------------------------------------------------------------
127
128 class MyApp : public wxApp
129 {
130 public:
131 virtual bool OnInit();
132 };
133
134 class MyDialog : public wxDialog
135 {
136 public:
137 MyDialog(const wxString& title);
138
139 void OnStartSimplePopup(wxCommandEvent& event);
140 void OnStartComplexPopup(wxCommandEvent& event);
141
142 private:
143 DECLARE_EVENT_TABLE()
144 };
145
146 class MyFrame : public wxFrame
147 {
148 public:
149 MyFrame(const wxString& title);
150
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);
156
157 private:
158 DECLARE_EVENT_TABLE()
159 };
160
161 // ----------------------------------------------------------------------------
162 // constants
163 // ----------------------------------------------------------------------------
164
165 // IDs for the controls and the menu commands
166 enum
167 {
168 Minimal_Quit = wxID_EXIT,
169 Minimal_About = wxID_ABOUT,
170 Minimal_TestDialog,
171 Minimal_StartSimplePopup,
172 Minimal_StartComplexPopup,
173 };
174
175 // ----------------------------------------------------------------------------
176 // event tables and other macros for wxWidgets
177 // ----------------------------------------------------------------------------
178
179
180 IMPLEMENT_APP(MyApp)
181
182 // 'Main program' equivalent: the program execution "starts" here
183 bool MyApp::OnInit()
184 {
185 // create the main application window
186 MyFrame *frame = new MyFrame(_T("Popup wxWidgets App"));
187
188 // and show it (the frames, unlike simple controls, are not shown when
189 // created initially)
190 frame->Show(true);
191
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.
195 return true;
196 }
197
198 // ----------------------------------------------------------------------------
199 // main frame
200 // ----------------------------------------------------------------------------
201
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)
208 END_EVENT_TABLE()
209
210 MyFrame::MyFrame(const wxString& title)
211 : wxFrame(NULL, wxID_ANY, title)
212 {
213 SetIcon(wxICON(sample));
214
215 #if wxUSE_MENUS
216 wxMenu *menuFile = new wxMenu;
217
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"));
221
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"));
224
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"));
229
230 // ... and attach this menu bar to the frame
231 SetMenuBar(menuBar);
232 #endif // wxUSE_MENUS
233
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) );
236
237
238 #if wxUSE_STATUSBAR
239 // create a status bar just for fun (by default with 1 pane only)
240 CreateStatusBar(2);
241 SetStatusText(_T("Welcome to wxWidgets!"));
242 #endif // wxUSE_STATUSBAR
243 }
244
245 // event handlers
246
247 void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
248 {
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 );
254 popup->Popup();
255 }
256
257 void MyFrame::OnStartComplexPopup(wxCommandEvent& WXUNUSED(event))
258 {
259 }
260
261 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
262 {
263 MyDialog dialog( wxT("Test") );
264 dialog.ShowModal();
265 }
266
267 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
268 {
269 // true is to force the frame to close
270 Close(true);
271 }
272
273 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
274 {
275 wxString msg;
276 msg.Printf( _T("This is the About dialog of the popup sample.\n")
277 _T("Welcome to %s"), wxVERSION_STRING);
278
279 wxMessageBox(msg, _T("About Popup"), wxOK | wxICON_INFORMATION, this);
280 }
281
282 // ----------------------------------------------------------------------------
283 // test dialog
284 // ----------------------------------------------------------------------------
285
286 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
287 EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
288 EVT_BUTTON(Minimal_StartComplexPopup, MyDialog::OnStartComplexPopup)
289 END_EVENT_TABLE()
290
291 MyDialog::MyDialog(const wxString& title)
292 : wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
293 {
294
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) );
297
298 new wxButton( this, wxID_OK, wxT("OK"), wxPoint(20,200) );
299 }
300
301 void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
302 {
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 );
308 popup->Popup();
309 }
310
311 void MyDialog::OnStartComplexPopup(wxCommandEvent& WXUNUSED(event))
312 {
313 }
314