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