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