]> git.saurik.com Git - wxWidgets.git/blame - samples/popup/popup.cpp
reworded a bit
[wxWidgets.git] / samples / popup / popup.cpp
CommitLineData
98d5b42b
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: minimal.cpp
3// Purpose: Popup wxWidgets sample
43f813d1 4// Author: Robert Roebling
98d5b42b 5// Modified by:
43f813d1 6// Created: 2005-02-04
98d5b42b 7// RCS-ID: $Id$
43f813d1 8// Copyright: (c) 2005 Robert Roebling
98d5b42b
RR
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"
b6de8445 34#include "wx/spinctrl.h"
98d5b42b
RR
35
36// ----------------------------------------------------------------------------
37// resources
38// ----------------------------------------------------------------------------
39
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"
44#endif
45
b6de8445
RR
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// IDs for the controls and the menu commands
51enum
52{
53 Minimal_Quit = wxID_EXIT,
54 Minimal_About = wxID_ABOUT,
55 Minimal_TestDialog,
56 Minimal_StartSimplePopup,
57 Minimal_StartScrolledPopup,
58 Minimal_LogWindow,
59 Minimal_PopupButton,
60 Minimal_PopupSpinctrl
61};
62
34786588
VZ
63//----------------------------------------------------------------------------
64// SimpleTransientPopup
65//----------------------------------------------------------------------------
98d5b42b
RR
66class SimpleTransientPopup: public wxPopupTransientWindow
67{
68public:
69 SimpleTransientPopup( wxWindow *parent );
70 virtual ~SimpleTransientPopup();
34786588
VZ
71
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 );
dd6a7fb4 77
34786588 78 wxScrolledWindow* GetChild() { return m_panel; }
dd6a7fb4 79
98d5b42b 80private:
34786588 81 wxScrolledWindow *m_panel;
b6de8445
RR
82 wxButton *m_button;
83 wxSpinCtrl *m_spinCtrl;
dd6a7fb4 84
98d5b42b
RR
85private:
86 void OnMouse( wxMouseEvent &event );
87 void OnSize( wxSizeEvent &event );
88 void OnSetFocus( wxFocusEvent &event );
89 void OnKillFocus( wxFocusEvent &event );
b6de8445
RR
90 void OnButton( wxCommandEvent& event );
91 void OnSpinCtrl( wxSpinEvent& event );
98d5b42b
RR
92
93private:
94 DECLARE_CLASS(SimpleTransientPopup)
95 DECLARE_EVENT_TABLE()
96};
97
98d5b42b
RR
98//----------------------------------------------------------------------------
99// SimpleTransientPopup
100//----------------------------------------------------------------------------
98d5b42b
RR
101IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
102
103BEGIN_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 )
b6de8445
RR
108 EVT_BUTTON( Minimal_PopupButton, SimpleTransientPopup::OnButton )
109 EVT_SPINCTRL( Minimal_PopupSpinctrl, SimpleTransientPopup::OnSpinCtrl )
98d5b42b
RR
110END_EVENT_TABLE()
111
112SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent ) :
113 wxPopupTransientWindow( parent )
114{
dd6a7fb4 115 m_panel = new wxScrolledWindow( this, wxID_ANY );
98d5b42b 116 m_panel->SetBackgroundColour( *wxLIGHT_GREY );
dd6a7fb4 117 wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
98d5b42b
RR
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();
b6de8445
RR
125
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();
21fe01e2 130
98d5b42b
RR
131 m_panel->SetSize( size.x+20, size.y+20 );
132 SetClientSize( size.x+20, size.y+20 );
133}
134
135SimpleTransientPopup::~SimpleTransientPopup()
136{
137}
138
34786588
VZ
139void SimpleTransientPopup::Popup(wxWindow *focus)
140{
21fe01e2 141 wxLogMessage( wxT("0x%x SimpleTransientPopup::Popup"), int(this) );
34786588
VZ
142 wxPopupTransientWindow::Popup(focus);
143}
144
98d5b42b
RR
145void SimpleTransientPopup::OnDismiss()
146{
21fe01e2 147 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnDismiss"), int(this) );
34786588
VZ
148 wxPopupTransientWindow::OnDismiss();
149}
150
151bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent& event)
152{
21fe01e2 153 wxLogMessage( wxT("0x%x SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), int(this), event.GetX(), event.GetY());
34786588
VZ
154 return wxPopupTransientWindow::ProcessLeftDown(event);
155}
156bool SimpleTransientPopup::Show( bool show )
157{
21fe01e2 158 wxLogMessage( wxT("0x%x SimpleTransientPopup::Show %d"), int(this), int(show));
34786588 159 return wxPopupTransientWindow::Show(show);
98d5b42b
RR
160}
161
162void SimpleTransientPopup::OnSize(wxSizeEvent &event)
163{
21fe01e2 164 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnSize"), int(this) );
98d5b42b
RR
165 event.Skip();
166}
167
168void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
169{
21fe01e2 170 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnSetFocus"), int(this) );
43f813d1 171 event.Skip();
98d5b42b
RR
172}
173
174void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
175{
21fe01e2 176 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnKillFocus"), int(this) );
43f813d1 177 event.Skip();
98d5b42b
RR
178}
179
180void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
181{
21fe01e2 182 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnMouse pos(%d, %d)"), int(this), event.GetX(), event.GetY());
34786588
VZ
183 event.Skip();
184}
185
b6de8445 186void SimpleTransientPopup::OnButton(wxCommandEvent& event)
34786588 187{
21fe01e2 188 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnButton ID %d"), int(this), event.GetId());
dd6a7fb4 189
b6de8445
RR
190 wxButton *button = wxDynamicCast(event.GetEventObject(), wxButton);
191 if (button->GetLabel() == wxT("Press Me"))
192 button->SetLabel(wxT("Pressed"));
dd6a7fb4 193 else
b6de8445 194 button->SetLabel(wxT("Press Me"));
34786588 195
34786588
VZ
196 event.Skip();
197}
34786588 198
b6de8445 199void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent& event)
34786588 200{
21fe01e2 201 wxLogMessage( wxT("0x%x SimpleTransientPopup::OnSpinCtrl ID %d Value %d"), int(this), event.GetId(), event.GetInt());
98d5b42b
RR
202 event.Skip();
203}
204
205// ----------------------------------------------------------------------------
206// private classes
207// ----------------------------------------------------------------------------
208
98d5b42b
RR
209class MyDialog : public wxDialog
210{
211public:
212 MyDialog(const wxString& title);
213
214 void OnStartSimplePopup(wxCommandEvent& event);
34786588 215 void OnStartScrolledPopup(wxCommandEvent& event);
98d5b42b
RR
216
217private:
218 DECLARE_EVENT_TABLE()
219};
220
221class MyFrame : public wxFrame
222{
223public:
224 MyFrame(const wxString& title);
34786588
VZ
225 virtual ~MyFrame();
226
98d5b42b
RR
227 void OnQuit(wxCommandEvent& event);
228 void OnAbout(wxCommandEvent& event);
229 void OnTestDialog(wxCommandEvent& event);
230 void OnStartSimplePopup(wxCommandEvent& event);
34786588 231 void OnStartScrolledPopup(wxCommandEvent& event);
98d5b42b
RR
232
233private:
b6de8445 234 wxTextCtrl *m_logWin;
34786588 235 wxLog *m_logOld;
98d5b42b
RR
236 DECLARE_EVENT_TABLE()
237};
238
b6de8445 239class MyApp : public wxApp
98d5b42b 240{
b6de8445
RR
241public:
242 virtual bool OnInit();
243
244 MyFrame *m_frame;
98d5b42b
RR
245};
246
247// ----------------------------------------------------------------------------
248// event tables and other macros for wxWidgets
249// ----------------------------------------------------------------------------
250
251
252IMPLEMENT_APP(MyApp)
253
254// 'Main program' equivalent: the program execution "starts" here
255bool MyApp::OnInit()
256{
257 // create the main application window
b6de8445 258 m_frame = new MyFrame(_T("Popup wxWidgets App"));
98d5b42b
RR
259
260 // and show it (the frames, unlike simple controls, are not shown when
261 // created initially)
b6de8445 262 m_frame->Show(true);
98d5b42b
RR
263
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.
267 return true;
268}
269
270// ----------------------------------------------------------------------------
271// main frame
272// ----------------------------------------------------------------------------
273
274BEGIN_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)
34786588 279 EVT_BUTTON(Minimal_StartScrolledPopup, MyFrame::OnStartScrolledPopup)
98d5b42b
RR
280END_EVENT_TABLE()
281
282MyFrame::MyFrame(const wxString& title)
283 : wxFrame(NULL, wxID_ANY, title)
284{
285 SetIcon(wxICON(sample));
286
287#if wxUSE_MENUS
288 wxMenu *menuFile = new wxMenu;
289
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"));
293
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"));
296
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"));
301
302 // ... and attach this menu bar to the frame
303 SetMenuBar(menuBar);
304#endif // wxUSE_MENUS
305
34786588
VZ
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) );
34786588 308
b6de8445 309 m_logWin = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
34786588 310 wxDefaultSize, wxTE_MULTILINE );
b6de8445
RR
311 m_logWin->SetEditable(false);
312 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
34786588
VZ
313 m_logOld = logger->SetActiveTarget( logger );
314 logger->SetTimestamp( NULL );
dd6a7fb4 315
34786588
VZ
316 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
317 topSizer->Add( button1, 0 );
318 topSizer->Add( button2, 0 );
b6de8445 319 topSizer->Add( m_logWin, 1, wxEXPAND );
98d5b42b 320
34786588
VZ
321 SetAutoLayout( true );
322 SetSizer( topSizer );
98d5b42b
RR
323
324#if wxUSE_STATUSBAR
325 // create a status bar just for fun (by default with 1 pane only)
326 CreateStatusBar(2);
327 SetStatusText(_T("Welcome to wxWidgets!"));
328#endif // wxUSE_STATUSBAR
329}
330
dd6a7fb4
WS
331MyFrame::~MyFrame()
332{
333 delete wxLog::SetActiveTarget(m_logOld);
334}
34786588
VZ
335
336
98d5b42b
RR
337// event handlers
338
339void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
340{
b6de8445 341 wxLogMessage( wxT("================================================") );
98d5b42b
RR
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 );
b6de8445 347 wxLogMessage( wxT("0x%x Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(popup), pos.x, pos.y, sz.x, sz.y );
98d5b42b
RR
348 popup->Popup();
349}
350
34786588 351void MyFrame::OnStartScrolledPopup(wxCommandEvent& event)
98d5b42b 352{
34786588 353 wxLogMessage( wxT("================================================") );
b6de8445 354 SimpleTransientPopup* popup = new SimpleTransientPopup( this );
34786588
VZ
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 );
b6de8445 360 wxLogMessage( wxT("0x%x Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(popup), pos.x, pos.y, sz.x, sz.y );
34786588 361 popup->Popup();
98d5b42b
RR
362}
363
364void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
365{
366 MyDialog dialog( wxT("Test") );
367 dialog.ShowModal();
368}
369
370void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
371{
372 // true is to force the frame to close
373 Close(true);
374}
375
376void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
377{
378 wxString msg;
379 msg.Printf( _T("This is the About dialog of the popup sample.\n")
380 _T("Welcome to %s"), wxVERSION_STRING);
381
382 wxMessageBox(msg, _T("About Popup"), wxOK | wxICON_INFORMATION, this);
383}
384
385// ----------------------------------------------------------------------------
386// test dialog
387// ----------------------------------------------------------------------------
388
389BEGIN_EVENT_TABLE(MyDialog, wxDialog)
390 EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
34786588 391 EVT_BUTTON(Minimal_StartScrolledPopup, MyDialog::OnStartScrolledPopup)
98d5b42b
RR
392END_EVENT_TABLE()
393
394MyDialog::MyDialog(const wxString& title)
395 : wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
396{
397
398 new wxButton( this, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
34786588 399 new wxButton( this, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,60) );
98d5b42b
RR
400
401 new wxButton( this, wxID_OK, wxT("OK"), wxPoint(20,200) );
402}
403
404void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
405{
b6de8445 406 wxLogMessage( wxT("================================================") );
98d5b42b
RR
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 );
b6de8445 412 wxLogMessage( wxT("0x%x Dialog Simple Popup Shown pos(%d, %d) size(%d, %d)"), long(popup), pos.x, pos.y, sz.x, sz.y );
98d5b42b
RR
413 popup->Popup();
414}
415
34786588 416void MyDialog::OnStartScrolledPopup(wxCommandEvent& event)
98d5b42b 417{
34786588 418 wxLogMessage( wxT("================================================") );
b6de8445 419 SimpleTransientPopup* popup = new SimpleTransientPopup( this );
34786588
VZ
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 );
b6de8445 425 wxLogMessage( wxT("0x%x Dialog Scrolled Popup Shown pos(%d, %d) size(%d, %d)"), long(popup), pos.x, pos.y, sz.x, sz.y );
34786588 426 popup->Popup();
98d5b42b 427}