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