| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: minimal.cpp |
| 3 | // Purpose: Popup wxWidgets sample |
| 4 | // Author: Robert Roebling |
| 5 | // Modified by: |
| 6 | // Created: 2005-02-04 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2005 Robert Roebling |
| 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 | #include "wx/spinctrl.h" |
| 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 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // constants |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | // IDs for the controls and the menu commands |
| 51 | enum |
| 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 | |
| 63 | //---------------------------------------------------------------------------- |
| 64 | // SimpleTransientPopup |
| 65 | //---------------------------------------------------------------------------- |
| 66 | class SimpleTransientPopup: public wxPopupTransientWindow |
| 67 | { |
| 68 | public: |
| 69 | SimpleTransientPopup( wxWindow *parent ); |
| 70 | virtual ~SimpleTransientPopup(); |
| 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 ); |
| 77 | |
| 78 | wxScrolledWindow* GetChild() { return m_panel; } |
| 79 | |
| 80 | private: |
| 81 | wxScrolledWindow *m_panel; |
| 82 | wxButton *m_button; |
| 83 | wxSpinCtrl *m_spinCtrl; |
| 84 | |
| 85 | private: |
| 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 ); |
| 92 | |
| 93 | private: |
| 94 | DECLARE_CLASS(SimpleTransientPopup) |
| 95 | DECLARE_EVENT_TABLE() |
| 96 | }; |
| 97 | |
| 98 | //---------------------------------------------------------------------------- |
| 99 | // SimpleTransientPopup |
| 100 | //---------------------------------------------------------------------------- |
| 101 | IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow) |
| 102 | |
| 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 ) |
| 110 | END_EVENT_TABLE() |
| 111 | |
| 112 | SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent ) : |
| 113 | wxPopupTransientWindow( parent ) |
| 114 | { |
| 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(); |
| 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(); |
| 130 | |
| 131 | m_panel->SetSize( size.x+20, size.y+20 ); |
| 132 | SetClientSize( size.x+20, size.y+20 ); |
| 133 | } |
| 134 | |
| 135 | SimpleTransientPopup::~SimpleTransientPopup() |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | void SimpleTransientPopup::Popup(wxWindow *focus) |
| 140 | { |
| 141 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::Popup"), long(this) ); |
| 142 | wxPopupTransientWindow::Popup(focus); |
| 143 | } |
| 144 | |
| 145 | void SimpleTransientPopup::OnDismiss() |
| 146 | { |
| 147 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnDismiss"), long(this) ); |
| 148 | wxPopupTransientWindow::OnDismiss(); |
| 149 | } |
| 150 | |
| 151 | bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent& event) |
| 152 | { |
| 153 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::ProcessLeftDown pos(%d, %d)"), long(this), event.GetX(), event.GetY()); |
| 154 | return wxPopupTransientWindow::ProcessLeftDown(event); |
| 155 | } |
| 156 | bool SimpleTransientPopup::Show( bool show ) |
| 157 | { |
| 158 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::Show %d"), long(this), int(show)); |
| 159 | return wxPopupTransientWindow::Show(show); |
| 160 | } |
| 161 | |
| 162 | void SimpleTransientPopup::OnSize(wxSizeEvent &event) |
| 163 | { |
| 164 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSize"), long(this) ); |
| 165 | event.Skip(); |
| 166 | } |
| 167 | |
| 168 | void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event) |
| 169 | { |
| 170 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSetFocus"), long(this) ); |
| 171 | event.Skip(); |
| 172 | } |
| 173 | |
| 174 | void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event) |
| 175 | { |
| 176 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnKillFocus"), long(this) ); |
| 177 | event.Skip(); |
| 178 | } |
| 179 | |
| 180 | void SimpleTransientPopup::OnMouse(wxMouseEvent &event) |
| 181 | { |
| 182 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnMouse pos(%d, %d)"), long(this), event.GetX(), event.GetY()); |
| 183 | event.Skip(); |
| 184 | } |
| 185 | |
| 186 | void SimpleTransientPopup::OnButton(wxCommandEvent& event) |
| 187 | { |
| 188 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnButton ID %d"), long(this), event.GetId()); |
| 189 | |
| 190 | wxButton *button = wxDynamicCast(event.GetEventObject(), wxButton); |
| 191 | if (button->GetLabel() == wxT("Press Me")) |
| 192 | button->SetLabel(wxT("Pressed")); |
| 193 | else |
| 194 | button->SetLabel(wxT("Press Me")); |
| 195 | |
| 196 | event.Skip(); |
| 197 | } |
| 198 | |
| 199 | void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent& event) |
| 200 | { |
| 201 | wxLogMessage( wxT("0x%lx SimpleTransientPopup::OnSpinCtrl ID %d Value %ld"), long(this), event.GetId(), event.GetInt()); |
| 202 | event.Skip(); |
| 203 | } |
| 204 | |
| 205 | // ---------------------------------------------------------------------------- |
| 206 | // private classes |
| 207 | // ---------------------------------------------------------------------------- |
| 208 | |
| 209 | class MyDialog : public wxDialog |
| 210 | { |
| 211 | public: |
| 212 | MyDialog(const wxString& title); |
| 213 | |
| 214 | void OnStartSimplePopup(wxCommandEvent& event); |
| 215 | void OnStartScrolledPopup(wxCommandEvent& event); |
| 216 | |
| 217 | private: |
| 218 | DECLARE_EVENT_TABLE() |
| 219 | }; |
| 220 | |
| 221 | class MyFrame : public wxFrame |
| 222 | { |
| 223 | public: |
| 224 | MyFrame(const wxString& title); |
| 225 | virtual ~MyFrame(); |
| 226 | |
| 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); |
| 232 | |
| 233 | private: |
| 234 | wxTextCtrl *m_logWin; |
| 235 | wxLog *m_logOld; |
| 236 | DECLARE_EVENT_TABLE() |
| 237 | }; |
| 238 | |
| 239 | class MyApp : public wxApp |
| 240 | { |
| 241 | public: |
| 242 | virtual bool OnInit(); |
| 243 | |
| 244 | MyFrame *m_frame; |
| 245 | }; |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // event tables and other macros for wxWidgets |
| 249 | // ---------------------------------------------------------------------------- |
| 250 | |
| 251 | |
| 252 | IMPLEMENT_APP(MyApp) |
| 253 | |
| 254 | // 'Main program' equivalent: the program execution "starts" here |
| 255 | bool MyApp::OnInit() |
| 256 | { |
| 257 | // create the main application window |
| 258 | m_frame = new MyFrame(_T("Popup wxWidgets App")); |
| 259 | |
| 260 | // and show it (the frames, unlike simple controls, are not shown when |
| 261 | // created initially) |
| 262 | m_frame->Show(true); |
| 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 | |
| 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) |
| 280 | END_EVENT_TABLE() |
| 281 | |
| 282 | MyFrame::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 | |
| 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) ); |
| 308 | |
| 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 ); |
| 315 | |
| 316 | wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); |
| 317 | topSizer->Add( button1, 0 ); |
| 318 | topSizer->Add( button2, 0 ); |
| 319 | topSizer->Add( m_logWin, 1, wxEXPAND ); |
| 320 | |
| 321 | SetAutoLayout( true ); |
| 322 | SetSizer( topSizer ); |
| 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 | |
| 331 | MyFrame::~MyFrame() |
| 332 | { |
| 333 | delete wxLog::SetActiveTarget(m_logOld); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | // event handlers |
| 338 | |
| 339 | void MyFrame::OnStartSimplePopup(wxCommandEvent& event) |
| 340 | { |
| 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 ); |
| 348 | popup->Popup(); |
| 349 | } |
| 350 | |
| 351 | void MyFrame::OnStartScrolledPopup(wxCommandEvent& event) |
| 352 | { |
| 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 ); |
| 361 | popup->Popup(); |
| 362 | } |
| 363 | |
| 364 | void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event)) |
| 365 | { |
| 366 | MyDialog dialog( wxT("Test") ); |
| 367 | dialog.ShowModal(); |
| 368 | } |
| 369 | |
| 370 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 371 | { |
| 372 | // true is to force the frame to close |
| 373 | Close(true); |
| 374 | } |
| 375 | |
| 376 | void 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 | |
| 389 | BEGIN_EVENT_TABLE(MyDialog, wxDialog) |
| 390 | EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup) |
| 391 | EVT_BUTTON(Minimal_StartScrolledPopup, MyDialog::OnStartScrolledPopup) |
| 392 | END_EVENT_TABLE() |
| 393 | |
| 394 | MyDialog::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) ); |
| 399 | new wxButton( this, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,60) ); |
| 400 | |
| 401 | new wxButton( this, wxID_OK, wxT("OK"), wxPoint(20,200) ); |
| 402 | } |
| 403 | |
| 404 | void MyDialog::OnStartSimplePopup(wxCommandEvent& event) |
| 405 | { |
| 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 ); |
| 413 | popup->Popup(); |
| 414 | } |
| 415 | |
| 416 | void MyDialog::OnStartScrolledPopup(wxCommandEvent& event) |
| 417 | { |
| 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 ); |
| 426 | popup->Popup(); |
| 427 | } |