tbtest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tbtest.cpp
  3. // Purpose: wxTaskBarIcon demo
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c)
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/wx.h"
  23. #endif
  24. // the application icon (under Windows and OS/2 it is in resources)
  25. #ifndef wxHAS_IMAGES_IN_RESOURCES
  26. #include "../sample.xpm"
  27. #endif
  28. #include "smile.xpm"
  29. #include "wx/taskbar.h"
  30. #include "tbtest.h"
  31. // ----------------------------------------------------------------------------
  32. // global variables
  33. // ----------------------------------------------------------------------------
  34. static MyDialog *gs_dialog = NULL;
  35. // ============================================================================
  36. // implementation
  37. // ============================================================================
  38. // ----------------------------------------------------------------------------
  39. // MyApp
  40. // ----------------------------------------------------------------------------
  41. IMPLEMENT_APP(MyApp)
  42. bool MyApp::OnInit()
  43. {
  44. if ( !wxApp::OnInit() )
  45. return false;
  46. if ( !wxTaskBarIcon::IsAvailable() )
  47. {
  48. wxMessageBox
  49. (
  50. "There appears to be no system tray support in your current environment. This sample may not behave as expected.",
  51. "Warning",
  52. wxOK | wxICON_EXCLAMATION
  53. );
  54. }
  55. // Create the main window
  56. gs_dialog = new MyDialog(wxT("wxTaskBarIcon Test Dialog"));
  57. gs_dialog->Show(true);
  58. return true;
  59. }
  60. // ----------------------------------------------------------------------------
  61. // MyDialog implementation
  62. // ----------------------------------------------------------------------------
  63. wxBEGIN_EVENT_TABLE(MyDialog, wxDialog)
  64. EVT_BUTTON(wxID_ABOUT, MyDialog::OnAbout)
  65. EVT_BUTTON(wxID_OK, MyDialog::OnOK)
  66. EVT_BUTTON(wxID_EXIT, MyDialog::OnExit)
  67. EVT_CLOSE(MyDialog::OnCloseWindow)
  68. wxEND_EVENT_TABLE()
  69. MyDialog::MyDialog(const wxString& title)
  70. : wxDialog(NULL, wxID_ANY, title)
  71. {
  72. wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
  73. wxSizerFlags flags;
  74. flags.Border(wxALL, 10);
  75. sizerTop->Add(new wxStaticText
  76. (
  77. this,
  78. wxID_ANY,
  79. wxT("Press 'Hide me' to hide this window, Exit to quit.")
  80. ), flags);
  81. sizerTop->Add(new wxStaticText
  82. (
  83. this,
  84. wxID_ANY,
  85. wxT("Double-click on the taskbar icon to show me again.")
  86. ), flags);
  87. sizerTop->AddStretchSpacer()->SetMinSize(200, 50);
  88. wxSizer * const sizerBtns = new wxBoxSizer(wxHORIZONTAL);
  89. sizerBtns->Add(new wxButton(this, wxID_ABOUT, wxT("&About")), flags);
  90. sizerBtns->Add(new wxButton(this, wxID_OK, wxT("&Hide")), flags);
  91. sizerBtns->Add(new wxButton(this, wxID_EXIT, wxT("E&xit")), flags);
  92. sizerTop->Add(sizerBtns, flags.Align(wxALIGN_CENTER_HORIZONTAL));
  93. SetSizerAndFit(sizerTop);
  94. Centre();
  95. m_taskBarIcon = new MyTaskBarIcon();
  96. // we should be able to show up to 128 characters on recent Windows versions
  97. // (and 64 on Win9x)
  98. if ( !m_taskBarIcon->SetIcon(wxICON(sample),
  99. "wxTaskBarIcon Sample\n"
  100. "With a very, very, very, very\n"
  101. "long tooltip whose length is\n"
  102. "greater than 64 characters.") )
  103. {
  104. wxLogError(wxT("Could not set icon."));
  105. }
  106. #if defined(__WXOSX__) && wxOSX_USE_COCOA
  107. m_dockIcon = new MyTaskBarIcon(wxTBI_DOCK);
  108. if ( !m_dockIcon->SetIcon(wxICON(sample)) )
  109. {
  110. wxLogError(wxT("Could not set icon."));
  111. }
  112. #endif
  113. }
  114. MyDialog::~MyDialog()
  115. {
  116. delete m_taskBarIcon;
  117. #if defined(__WXCOCOA__)
  118. delete m_dockIcon;
  119. #endif
  120. }
  121. void MyDialog::OnAbout(wxCommandEvent& WXUNUSED(event))
  122. {
  123. static const char * const title = "About wxWidgets Taskbar Sample";
  124. static const char * const message
  125. = "wxWidgets sample showing wxTaskBarIcon class\n"
  126. "\n"
  127. "(C) 1997 Julian Smart\n"
  128. "(C) 2007 Vadim Zeitlin";
  129. #if defined(__WXMSW__) && wxUSE_TASKBARICON_BALLOONS
  130. m_taskBarIcon->ShowBalloon(title, message, 15000, wxICON_INFORMATION);
  131. #else // !__WXMSW__
  132. wxMessageBox(message, title, wxICON_INFORMATION|wxOK, this);
  133. #endif // __WXMSW__/!__WXMSW__
  134. }
  135. void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event))
  136. {
  137. Show(false);
  138. }
  139. void MyDialog::OnExit(wxCommandEvent& WXUNUSED(event))
  140. {
  141. Close(true);
  142. }
  143. void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
  144. {
  145. Destroy();
  146. }
  147. // ----------------------------------------------------------------------------
  148. // MyTaskBarIcon implementation
  149. // ----------------------------------------------------------------------------
  150. enum
  151. {
  152. PU_RESTORE = 10001,
  153. PU_NEW_ICON,
  154. PU_EXIT,
  155. PU_CHECKMARK,
  156. PU_SUB1,
  157. PU_SUB2,
  158. PU_SUBMAIN
  159. };
  160. wxBEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon)
  161. EVT_MENU(PU_RESTORE, MyTaskBarIcon::OnMenuRestore)
  162. EVT_MENU(PU_EXIT, MyTaskBarIcon::OnMenuExit)
  163. EVT_MENU(PU_NEW_ICON,MyTaskBarIcon::OnMenuSetNewIcon)
  164. EVT_MENU(PU_CHECKMARK,MyTaskBarIcon::OnMenuCheckmark)
  165. EVT_UPDATE_UI(PU_CHECKMARK,MyTaskBarIcon::OnMenuUICheckmark)
  166. EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick)
  167. EVT_MENU(PU_SUB1, MyTaskBarIcon::OnMenuSub)
  168. EVT_MENU(PU_SUB2, MyTaskBarIcon::OnMenuSub)
  169. wxEND_EVENT_TABLE()
  170. void MyTaskBarIcon::OnMenuRestore(wxCommandEvent& )
  171. {
  172. gs_dialog->Show(true);
  173. }
  174. void MyTaskBarIcon::OnMenuExit(wxCommandEvent& )
  175. {
  176. gs_dialog->Close(true);
  177. }
  178. static bool check = true;
  179. void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent& )
  180. {
  181. check = !check;
  182. }
  183. void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent &event)
  184. {
  185. event.Check(check);
  186. }
  187. void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent&)
  188. {
  189. wxIcon icon(smile_xpm);
  190. if (!SetIcon(icon, wxT("wxTaskBarIcon Sample - a different icon")))
  191. wxMessageBox(wxT("Could not set new icon."));
  192. }
  193. void MyTaskBarIcon::OnMenuSub(wxCommandEvent&)
  194. {
  195. wxMessageBox(wxT("You clicked on a submenu!"));
  196. }
  197. // Overridables
  198. wxMenu *MyTaskBarIcon::CreatePopupMenu()
  199. {
  200. wxMenu *menu = new wxMenu;
  201. menu->Append(PU_RESTORE, wxT("&Restore main window"));
  202. menu->AppendSeparator();
  203. menu->Append(PU_NEW_ICON, wxT("&Set New Icon"));
  204. menu->AppendSeparator();
  205. menu->AppendCheckItem(PU_CHECKMARK, wxT("Test &check mark"));
  206. menu->AppendSeparator();
  207. wxMenu *submenu = new wxMenu;
  208. submenu->Append(PU_SUB1, wxT("One submenu"));
  209. submenu->AppendSeparator();
  210. submenu->Append(PU_SUB2, wxT("Another submenu"));
  211. menu->Append(PU_SUBMAIN, wxT("Submenu"), submenu);
  212. /* OSX has built-in quit menu for the dock menu, but not for the status item */
  213. #ifdef __WXOSX__
  214. if ( OSXIsStatusItem() )
  215. #endif
  216. {
  217. menu->AppendSeparator();
  218. menu->Append(PU_EXIT, wxT("E&xit"));
  219. }
  220. return menu;
  221. }
  222. void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&)
  223. {
  224. gs_dialog->Show(true);
  225. }