popup.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: popup.cpp
  3. // Purpose: Popup wxWidgets sample
  4. // Author: Robert Roebling
  5. // Modified by:
  6. // Created: 2005-02-04
  7. // Copyright: (c) 2005 Robert Roebling
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include "wx/popupwin.h"
  27. #include "wx/spinctrl.h"
  28. // ----------------------------------------------------------------------------
  29. // resources
  30. // ----------------------------------------------------------------------------
  31. // the application icon (under Windows and OS/2 it is in resources and even
  32. // though we could still include the XPM here it would be unused)
  33. #ifndef wxHAS_IMAGES_IN_RESOURCES
  34. #include "../sample.xpm"
  35. #endif
  36. // ----------------------------------------------------------------------------
  37. // constants
  38. // ----------------------------------------------------------------------------
  39. // IDs for the controls and the menu commands
  40. enum
  41. {
  42. Minimal_Quit = wxID_EXIT,
  43. Minimal_About = wxID_ABOUT,
  44. Minimal_TestDialog,
  45. Minimal_StartSimplePopup,
  46. Minimal_StartScrolledPopup,
  47. Minimal_LogWindow,
  48. Minimal_PopupButton,
  49. Minimal_PopupSpinctrl
  50. };
  51. //----------------------------------------------------------------------------
  52. // SimpleTransientPopup
  53. //----------------------------------------------------------------------------
  54. class SimpleTransientPopup: public wxPopupTransientWindow
  55. {
  56. public:
  57. SimpleTransientPopup( wxWindow *parent, bool scrolled );
  58. virtual ~SimpleTransientPopup();
  59. // wxPopupTransientWindow virtual methods are all overridden to log them
  60. virtual void Popup(wxWindow *focus = NULL);
  61. virtual void OnDismiss();
  62. virtual bool ProcessLeftDown(wxMouseEvent& event);
  63. virtual bool Show( bool show = true );
  64. private:
  65. wxScrolledWindow *m_panel;
  66. wxButton *m_button;
  67. wxSpinCtrl *m_spinCtrl;
  68. wxStaticText *m_mouseText;
  69. private:
  70. void OnMouse( wxMouseEvent &event );
  71. void OnSize( wxSizeEvent &event );
  72. void OnSetFocus( wxFocusEvent &event );
  73. void OnKillFocus( wxFocusEvent &event );
  74. void OnButton( wxCommandEvent& event );
  75. void OnSpinCtrl( wxSpinEvent& event );
  76. private:
  77. wxDECLARE_ABSTRACT_CLASS(SimpleTransientPopup);
  78. wxDECLARE_EVENT_TABLE();
  79. };
  80. //----------------------------------------------------------------------------
  81. // SimpleTransientPopup
  82. //----------------------------------------------------------------------------
  83. IMPLEMENT_CLASS(SimpleTransientPopup,wxPopupTransientWindow)
  84. wxBEGIN_EVENT_TABLE(SimpleTransientPopup,wxPopupTransientWindow)
  85. EVT_MOUSE_EVENTS( SimpleTransientPopup::OnMouse )
  86. EVT_SIZE( SimpleTransientPopup::OnSize )
  87. EVT_SET_FOCUS( SimpleTransientPopup::OnSetFocus )
  88. EVT_KILL_FOCUS( SimpleTransientPopup::OnKillFocus )
  89. EVT_BUTTON( Minimal_PopupButton, SimpleTransientPopup::OnButton )
  90. EVT_SPINCTRL( Minimal_PopupSpinctrl, SimpleTransientPopup::OnSpinCtrl )
  91. wxEND_EVENT_TABLE()
  92. SimpleTransientPopup::SimpleTransientPopup( wxWindow *parent, bool scrolled )
  93. :wxPopupTransientWindow( parent )
  94. {
  95. m_panel = new wxScrolledWindow( this, wxID_ANY );
  96. m_panel->SetBackgroundColour( *wxLIGHT_GREY );
  97. // Keep this code to verify if mouse events work, they're required if
  98. // you're making a control like a combobox where the items are highlighted
  99. // under the cursor, the m_panel is set focus in the Popup() function
  100. m_panel->Connect(wxEVT_MOTION,
  101. wxMouseEventHandler(SimpleTransientPopup::OnMouse),
  102. NULL, this);
  103. wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
  104. wxT("wxPopupTransientWindow is a\n")
  105. wxT("wxPopupWindow which disappears\n")
  106. wxT("automatically when the user\n")
  107. wxT("clicks the mouse outside it or if it\n")
  108. wxT("(or its first child) loses focus in \n")
  109. wxT("any other way.") );
  110. m_button = new wxButton(m_panel, Minimal_PopupButton, wxT("Press Me"));
  111. m_spinCtrl = new wxSpinCtrl(m_panel, Minimal_PopupSpinctrl, wxT("Hello"));
  112. m_mouseText = new wxStaticText(m_panel, wxID_ANY,
  113. wxT("<- Test Mouse ->"));
  114. wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
  115. topSizer->Add( text, 0, wxALL, 5 );
  116. topSizer->Add( m_button, 0, wxALL, 5 );
  117. topSizer->Add( m_spinCtrl, 0, wxALL, 5 );
  118. topSizer->Add( m_mouseText, 0, wxCENTRE|wxALL, 5 );
  119. if ( scrolled )
  120. {
  121. // Add a big window to ensure that scrollbars are shown when we set the
  122. // panel size to a lesser size below.
  123. topSizer->Add(new wxPanel(m_panel, wxID_ANY, wxDefaultPosition,
  124. wxSize(600, 900)));
  125. }
  126. m_panel->SetSizer( topSizer );
  127. if ( scrolled )
  128. {
  129. // Set the fixed size to ensure that the scrollbars are shown.
  130. m_panel->SetSize(300, 300);
  131. // And also actually enable them.
  132. m_panel->SetScrollRate(10, 10);
  133. }
  134. else
  135. {
  136. // Use the fitting size for the panel if we don't need scrollbars.
  137. topSizer->Fit(m_panel);
  138. }
  139. SetClientSize(m_panel->GetSize());
  140. }
  141. SimpleTransientPopup::~SimpleTransientPopup()
  142. {
  143. }
  144. void SimpleTransientPopup::Popup(wxWindow* WXUNUSED(focus))
  145. {
  146. wxLogMessage( "%p SimpleTransientPopup::Popup", this );
  147. wxPopupTransientWindow::Popup();
  148. }
  149. void SimpleTransientPopup::OnDismiss()
  150. {
  151. wxLogMessage( "%p SimpleTransientPopup::OnDismiss", this );
  152. wxPopupTransientWindow::OnDismiss();
  153. }
  154. bool SimpleTransientPopup::ProcessLeftDown(wxMouseEvent& event)
  155. {
  156. wxLogMessage( "%p SimpleTransientPopup::ProcessLeftDown pos(%d, %d)", this, event.GetX(), event.GetY());
  157. return wxPopupTransientWindow::ProcessLeftDown(event);
  158. }
  159. bool SimpleTransientPopup::Show( bool show )
  160. {
  161. wxLogMessage( "%p SimpleTransientPopup::Show %d", this, int(show));
  162. return wxPopupTransientWindow::Show(show);
  163. }
  164. void SimpleTransientPopup::OnSize(wxSizeEvent &event)
  165. {
  166. wxLogMessage( "%p SimpleTransientPopup::OnSize", this );
  167. event.Skip();
  168. }
  169. void SimpleTransientPopup::OnSetFocus(wxFocusEvent &event)
  170. {
  171. wxLogMessage( "%p SimpleTransientPopup::OnSetFocus", this );
  172. event.Skip();
  173. }
  174. void SimpleTransientPopup::OnKillFocus(wxFocusEvent &event)
  175. {
  176. wxLogMessage( "%p SimpleTransientPopup::OnKillFocus", this );
  177. event.Skip();
  178. }
  179. void SimpleTransientPopup::OnMouse(wxMouseEvent &event)
  180. {
  181. wxRect rect(m_mouseText->GetRect());
  182. rect.SetX(-100000);
  183. rect.SetWidth(1000000);
  184. wxColour colour(*wxLIGHT_GREY);
  185. if (rect.Contains(event.GetPosition()))
  186. {
  187. colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
  188. wxLogMessage( "%p SimpleTransientPopup::OnMouse pos(%d, %d)",
  189. event.GetEventObject(), event.GetX(), event.GetY());
  190. }
  191. if (colour != m_mouseText->GetBackgroundColour())
  192. {
  193. m_mouseText->SetBackgroundColour(colour);
  194. m_mouseText->Refresh();
  195. }
  196. event.Skip();
  197. }
  198. void SimpleTransientPopup::OnButton(wxCommandEvent& event)
  199. {
  200. wxLogMessage( "%p SimpleTransientPopup::OnButton ID %d", this, event.GetId());
  201. wxButton *button = wxDynamicCast(event.GetEventObject(), wxButton);
  202. if (button->GetLabel() == wxT("Press Me"))
  203. button->SetLabel(wxT("Pressed"));
  204. else
  205. button->SetLabel(wxT("Press Me"));
  206. event.Skip();
  207. }
  208. void SimpleTransientPopup::OnSpinCtrl(wxSpinEvent& event)
  209. {
  210. wxLogMessage( "%p SimpleTransientPopup::OnSpinCtrl ID %d Value %d",
  211. this, event.GetId(), event.GetInt());
  212. event.Skip();
  213. }
  214. // ----------------------------------------------------------------------------
  215. // private classes
  216. // ----------------------------------------------------------------------------
  217. class MyDialog : public wxDialog
  218. {
  219. public:
  220. MyDialog(const wxString& title);
  221. void OnStartSimplePopup(wxCommandEvent& event);
  222. void OnStartScrolledPopup(wxCommandEvent& event);
  223. private:
  224. SimpleTransientPopup *m_simplePopup;
  225. SimpleTransientPopup *m_scrolledPopup;
  226. wxDECLARE_EVENT_TABLE();
  227. };
  228. class MyFrame : public wxFrame
  229. {
  230. public:
  231. MyFrame(const wxString& title);
  232. virtual ~MyFrame();
  233. void OnQuit(wxCommandEvent& event);
  234. void OnAbout(wxCommandEvent& event);
  235. void OnTestDialog(wxCommandEvent& event);
  236. void OnStartSimplePopup(wxCommandEvent& event);
  237. void OnStartScrolledPopup(wxCommandEvent& event);
  238. void OnActivate(wxActivateEvent& event);
  239. private:
  240. SimpleTransientPopup *m_simplePopup;
  241. SimpleTransientPopup *m_scrolledPopup;
  242. wxTextCtrl *m_logWin;
  243. wxLog *m_logOld;
  244. wxDECLARE_EVENT_TABLE();
  245. };
  246. class MyApp : public wxApp
  247. {
  248. public:
  249. virtual bool OnInit();
  250. MyFrame *m_frame;
  251. };
  252. // ----------------------------------------------------------------------------
  253. // event tables and other macros for wxWidgets
  254. // ----------------------------------------------------------------------------
  255. IMPLEMENT_APP(MyApp)
  256. // 'Main program' equivalent: the program execution "starts" here
  257. bool MyApp::OnInit()
  258. {
  259. if ( !wxApp::OnInit() )
  260. return false;
  261. // create the main application window
  262. m_frame = new MyFrame(wxT("Popup wxWidgets App"));
  263. // and show it (the frames, unlike simple controls, are not shown when
  264. // created initially)
  265. m_frame->Show(true);
  266. // success: wxApp::OnRun() will be called which will enter the main message
  267. // loop and the application will run. If we returned false here, the
  268. // application would exit immediately.
  269. return true;
  270. }
  271. // ----------------------------------------------------------------------------
  272. // main frame
  273. // ----------------------------------------------------------------------------
  274. wxBEGIN_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_ACTIVATE(MyFrame::OnActivate)
  279. EVT_BUTTON(Minimal_StartSimplePopup, MyFrame::OnStartSimplePopup)
  280. EVT_BUTTON(Minimal_StartScrolledPopup, MyFrame::OnStartScrolledPopup)
  281. wxEND_EVENT_TABLE()
  282. MyFrame::MyFrame(const wxString& title)
  283. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500,300))
  284. {
  285. m_simplePopup = m_scrolledPopup = NULL;
  286. SetIcon(wxICON(sample));
  287. #if wxUSE_MENUS
  288. wxMenu *menuFile = new wxMenu;
  289. // the "About" item should be in the help menu
  290. wxMenu *helpMenu = new wxMenu;
  291. helpMenu->Append(Minimal_About, wxT("&About\tF1"), wxT("Show about dialog"));
  292. menuFile->Append(Minimal_TestDialog, wxT("&Test dialog\tAlt-T"), wxT("Test dialog"));
  293. menuFile->Append(Minimal_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
  294. // now append the freshly created menu to the menu bar...
  295. wxMenuBar *menuBar = new wxMenuBar();
  296. menuBar->Append(menuFile, wxT("&File"));
  297. menuBar->Append(helpMenu, wxT("&Help"));
  298. // ... and attach this menu bar to the frame
  299. SetMenuBar(menuBar);
  300. #endif // wxUSE_MENUS
  301. #if wxUSE_STATUSBAR
  302. // create a status bar just for fun (by default with 1 pane only)
  303. CreateStatusBar(2);
  304. SetStatusText(wxT("Welcome to wxWidgets!"));
  305. #endif // wxUSE_STATUSBAR
  306. wxPanel *panel = new wxPanel(this, -1);
  307. wxButton *button1 = new wxButton( panel, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
  308. wxButton *button2 = new wxButton( panel, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,70) );
  309. m_logWin = new wxTextCtrl( panel, 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->DisableTimestamp();
  315. wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
  316. topSizer->Add( button1, 0, wxALL, 5 );
  317. topSizer->Add( button2, 0, wxALL, 5 );
  318. topSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
  319. panel->SetSizer( topSizer );
  320. }
  321. MyFrame::~MyFrame()
  322. {
  323. delete wxLog::SetActiveTarget(m_logOld);
  324. }
  325. // event handlers
  326. void MyFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
  327. {
  328. wxLogMessage( wxT("In activate...") );
  329. }
  330. void MyFrame::OnStartSimplePopup(wxCommandEvent& event)
  331. {
  332. wxLogMessage( wxT("================================================") );
  333. delete m_simplePopup;
  334. m_simplePopup = new SimpleTransientPopup( this, false );
  335. wxWindow *btn = (wxWindow*) event.GetEventObject();
  336. wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
  337. wxSize sz = btn->GetSize();
  338. m_simplePopup->Position( pos, sz );
  339. wxLogMessage( "%p Simple Popup Shown pos(%d, %d) size(%d, %d)", m_simplePopup, pos.x, pos.y, sz.x, sz.y );
  340. m_simplePopup->Popup();
  341. }
  342. void MyFrame::OnStartScrolledPopup(wxCommandEvent& event)
  343. {
  344. wxLogMessage( wxT("================================================") );
  345. delete m_scrolledPopup;
  346. m_scrolledPopup = new SimpleTransientPopup( this, true );
  347. wxWindow *btn = (wxWindow*) event.GetEventObject();
  348. wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
  349. wxSize sz = btn->GetSize();
  350. m_scrolledPopup->Position( pos, sz );
  351. wxLogMessage( "%p Scrolled Popup Shown pos(%d, %d) size(%d, %d)", m_scrolledPopup, pos.x, pos.y, sz.x, sz.y );
  352. m_scrolledPopup->Popup();
  353. }
  354. void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
  355. {
  356. MyDialog dialog( wxT("Test") );
  357. dialog.ShowModal();
  358. }
  359. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  360. {
  361. // true is to force the frame to close
  362. Close(true);
  363. }
  364. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  365. {
  366. wxString msg;
  367. msg.Printf( wxT("This is the About dialog of the popup sample.\n")
  368. wxT("Welcome to %s"), wxVERSION_STRING);
  369. wxMessageBox(msg, wxT("About Popup"), wxOK | wxICON_INFORMATION, this);
  370. }
  371. // ----------------------------------------------------------------------------
  372. // test dialog
  373. // ----------------------------------------------------------------------------
  374. wxBEGIN_EVENT_TABLE(MyDialog, wxDialog)
  375. EVT_BUTTON(Minimal_StartSimplePopup, MyDialog::OnStartSimplePopup)
  376. EVT_BUTTON(Minimal_StartScrolledPopup, MyDialog::OnStartScrolledPopup)
  377. wxEND_EVENT_TABLE()
  378. MyDialog::MyDialog(const wxString& title)
  379. :wxDialog(NULL, wxID_ANY, title, wxPoint(50,50), wxSize(400,300))
  380. {
  381. m_simplePopup = m_scrolledPopup = NULL;
  382. wxPanel *panel = new wxPanel(this, -1);
  383. wxButton *button1 = new wxButton( panel, Minimal_StartSimplePopup, wxT("Show simple popup"), wxPoint(20,20) );
  384. wxButton *button2 = new wxButton( panel, Minimal_StartScrolledPopup, wxT("Show scrolled popup"), wxPoint(20,60) );
  385. wxButton *okButton = new wxButton( panel, wxID_OK, wxT("OK"), wxPoint(20,200) );
  386. wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
  387. topSizer->Add( button1, 0, wxALL, 5 );
  388. topSizer->Add( button2, 0, wxALL, 5 );
  389. topSizer->AddSpacer(40);
  390. topSizer->Add( okButton, 0, wxALL, 5 );
  391. panel->SetSizerAndFit( topSizer );
  392. }
  393. void MyDialog::OnStartSimplePopup(wxCommandEvent& event)
  394. {
  395. wxLogMessage( wxT("================================================") );
  396. delete m_simplePopup;
  397. m_simplePopup = new SimpleTransientPopup( this, false );
  398. wxWindow *btn = (wxWindow*) event.GetEventObject();
  399. wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
  400. wxSize sz = btn->GetSize();
  401. m_simplePopup->Position( pos, sz );
  402. wxLogMessage( "%p Dialog Simple Popup Shown pos(%d, %d) size(%d, %d)", m_simplePopup, pos.x, pos.y, sz.x, sz.y );
  403. m_simplePopup->Popup();
  404. }
  405. void MyDialog::OnStartScrolledPopup(wxCommandEvent& event)
  406. {
  407. wxLogMessage( wxT("================================================") );
  408. delete m_scrolledPopup;
  409. m_scrolledPopup = new SimpleTransientPopup( this, true );
  410. wxWindow *btn = (wxWindow*) event.GetEventObject();
  411. wxPoint pos = btn->ClientToScreen( wxPoint(0,0) );
  412. wxSize sz = btn->GetSize();
  413. m_scrolledPopup->Position( pos, sz );
  414. wxLogMessage( "%p Dialog Scrolled Popup Shown pos(%d, %d) size(%d, %d)", m_scrolledPopup, pos.x, pos.y, sz.x, sz.y );
  415. m_scrolledPopup->Popup();
  416. }