]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/taskbar/tbtest.cpp
Change version to 3.0.0.
[wxWidgets.git] / samples / taskbar / tbtest.cpp
... / ...
CommitLineData
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// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
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
27 #include "wx/wx.h"
28#endif
29
30// the application icon (under Windows and OS/2 it is in resources)
31#ifndef wxHAS_IMAGES_IN_RESOURCES
32 #include "../sample.xpm"
33#endif
34
35#include "smile.xpm"
36
37#include "wx/taskbar.h"
38
39#include "tbtest.h"
40
41// ----------------------------------------------------------------------------
42// global variables
43// ----------------------------------------------------------------------------
44
45static MyDialog *gs_dialog = NULL;
46
47// ============================================================================
48// implementation
49// ============================================================================
50
51// ----------------------------------------------------------------------------
52// MyApp
53// ----------------------------------------------------------------------------
54
55IMPLEMENT_APP(MyApp)
56
57bool MyApp::OnInit()
58{
59 if ( !wxApp::OnInit() )
60 return false;
61
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
72 // Create the main window
73 gs_dialog = new MyDialog(wxT("wxTaskBarIcon Test Dialog"));
74
75 gs_dialog->Show(true);
76
77 return true;
78}
79
80
81// ----------------------------------------------------------------------------
82// MyDialog implementation
83// ----------------------------------------------------------------------------
84
85BEGIN_EVENT_TABLE(MyDialog, wxDialog)
86 EVT_BUTTON(wxID_ABOUT, MyDialog::OnAbout)
87 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
88 EVT_BUTTON(wxID_EXIT, MyDialog::OnExit)
89 EVT_CLOSE(MyDialog::OnCloseWindow)
90END_EVENT_TABLE()
91
92
93MyDialog::MyDialog(const wxString& title)
94 : wxDialog(NULL, wxID_ANY, title)
95{
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,
105 wxT("Press 'Hide me' to hide this window, Exit to quit.")
106 ), flags);
107
108 sizerTop->Add(new wxStaticText
109 (
110 this,
111 wxID_ANY,
112 wxT("Double-click on the taskbar icon to show me again.")
113 ), flags);
114
115 sizerTop->AddStretchSpacer()->SetMinSize(200, 50);
116
117 wxSizer * const sizerBtns = new wxBoxSizer(wxHORIZONTAL);
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);
121
122 sizerTop->Add(sizerBtns, flags.Align(wxALIGN_CENTER_HORIZONTAL));
123 SetSizerAndFit(sizerTop);
124 Centre();
125
126 m_taskBarIcon = new MyTaskBarIcon();
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 }
138
139#if defined(__WXOSX__) && wxOSX_USE_COCOA
140 m_dockIcon = new MyTaskBarIcon(wxTBI_DOCK);
141 if ( !m_dockIcon->SetIcon(wxICON(sample)) )
142 {
143 wxLogError(wxT("Could not set icon."));
144 }
145#endif
146}
147
148MyDialog::~MyDialog()
149{
150 delete m_taskBarIcon;
151#if defined(__WXCOCOA__)
152 delete m_dockIcon;
153#endif
154}
155
156void 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
165#if defined(__WXMSW__) && wxUSE_TASKBARICON_BALLOONS
166 m_taskBarIcon->ShowBalloon(title, message, 15000, wxICON_INFORMATION);
167#else // !__WXMSW__
168 wxMessageBox(message, title, wxICON_INFORMATION|wxOK, this);
169#endif // __WXMSW__/!__WXMSW__
170}
171
172void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event))
173{
174 Show(false);
175}
176
177void MyDialog::OnExit(wxCommandEvent& WXUNUSED(event))
178{
179 Close(true);
180}
181
182void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
183{
184 Destroy();
185}
186
187
188// ----------------------------------------------------------------------------
189// MyTaskBarIcon implementation
190// ----------------------------------------------------------------------------
191
192enum
193{
194 PU_RESTORE = 10001,
195 PU_NEW_ICON,
196 PU_EXIT,
197 PU_CHECKMARK,
198 PU_SUB1,
199 PU_SUB2,
200 PU_SUBMAIN
201};
202
203
204BEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon)
205 EVT_MENU(PU_RESTORE, MyTaskBarIcon::OnMenuRestore)
206 EVT_MENU(PU_EXIT, MyTaskBarIcon::OnMenuExit)
207 EVT_MENU(PU_NEW_ICON,MyTaskBarIcon::OnMenuSetNewIcon)
208 EVT_MENU(PU_CHECKMARK,MyTaskBarIcon::OnMenuCheckmark)
209 EVT_UPDATE_UI(PU_CHECKMARK,MyTaskBarIcon::OnMenuUICheckmark)
210 EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick)
211 EVT_MENU(PU_SUB1, MyTaskBarIcon::OnMenuSub)
212 EVT_MENU(PU_SUB2, MyTaskBarIcon::OnMenuSub)
213END_EVENT_TABLE()
214
215void MyTaskBarIcon::OnMenuRestore(wxCommandEvent& )
216{
217 gs_dialog->Show(true);
218}
219
220void MyTaskBarIcon::OnMenuExit(wxCommandEvent& )
221{
222 gs_dialog->Close(true);
223}
224
225static bool check = true;
226
227void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent& )
228{
229 check = !check;
230}
231
232void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent &event)
233{
234 event.Check(check);
235}
236
237void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent&)
238{
239 wxIcon icon(smile_xpm);
240
241 if (!SetIcon(icon, wxT("wxTaskBarIcon Sample - a different icon")))
242 wxMessageBox(wxT("Could not set new icon."));
243}
244
245void MyTaskBarIcon::OnMenuSub(wxCommandEvent&)
246{
247 wxMessageBox(wxT("You clicked on a submenu!"));
248}
249
250// Overridables
251wxMenu *MyTaskBarIcon::CreatePopupMenu()
252{
253 wxMenu *menu = new wxMenu;
254 menu->Append(PU_RESTORE, wxT("&Restore main window"));
255 menu->AppendSeparator();
256 menu->Append(PU_NEW_ICON, wxT("&Set New Icon"));
257 menu->AppendSeparator();
258 menu->AppendCheckItem(PU_CHECKMARK, wxT("Test &check mark"));
259 menu->AppendSeparator();
260 wxMenu *submenu = new wxMenu;
261 submenu->Append(PU_SUB1, wxT("One submenu"));
262 submenu->AppendSeparator();
263 submenu->Append(PU_SUB2, wxT("Another submenu"));
264 menu->Append(PU_SUBMAIN, wxT("Submenu"), submenu);
265 /* OSX has built-in quit menu for the dock menu, but not for the status item */
266#ifdef __WXOSX__
267 if ( OSXIsStatusItem() )
268#endif
269 {
270 menu->AppendSeparator();
271 menu->Append(PU_EXIT, wxT("E&xit"));
272 }
273 return menu;
274}
275
276void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&)
277{
278 gs_dialog->Show(true);
279}