]> git.saurik.com Git - wxWidgets.git/blame - samples/taskbar/tbtest.cpp
supporting NO_BORDER for Cocoa as well
[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
VS
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__)
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
cdcfde5d
VZ
63 // Create the main window
64 gs_dialog = new MyDialog(wxT("wxTaskBarIcon Test Dialog"));
bbf1f0e5 65
cdcfde5d 66 gs_dialog->Show(true);
bbf1f0e5 67
27301f26 68 return true;
bbf1f0e5
KB
69}
70
69ecd30f 71
cdcfde5d
VZ
72// ----------------------------------------------------------------------------
73// MyDialog implementation
74// ----------------------------------------------------------------------------
75
bbf1f0e5 76BEGIN_EVENT_TABLE(MyDialog, wxDialog)
cdcfde5d 77 EVT_BUTTON(wxID_ABOUT, MyDialog::OnAbout)
bbf1f0e5
KB
78 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
79 EVT_BUTTON(wxID_EXIT, MyDialog::OnExit)
d66d9d5b 80 EVT_CLOSE(MyDialog::OnCloseWindow)
bbf1f0e5
KB
81END_EVENT_TABLE()
82
69ecd30f 83
cdcfde5d
VZ
84MyDialog::MyDialog(const wxString& title)
85 : wxDialog(NULL, wxID_ANY, title)
bbf1f0e5 86{
cdcfde5d
VZ
87 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
88
89 wxSizerFlags flags;
90 flags.Border(wxALL, 10);
91
92 sizerTop->Add(new wxStaticText
93 (
94 this,
95 wxID_ANY,
96 _T("Press 'Hide me' to hide this window, Exit to quit.")
97 ), flags);
98
99 sizerTop->Add(new wxStaticText
100 (
101 this,
102 wxID_ANY,
103 _T("Double-click on the taskbar icon to show me again.")
104 ), flags);
105
106 sizerTop->AddStretchSpacer()->SetMinSize(200, 50);
107
108 wxSizer * const sizerBtns = new wxBoxSizer(wxHORIZONTAL);
109 sizerBtns->Add(new wxButton(this, wxID_ABOUT, _T("&About")), flags);
110 sizerBtns->Add(new wxButton(this, wxID_OK, _T("&Hide")), flags);
111 sizerBtns->Add(new wxButton(this, wxID_EXIT, _T("E&xit")), flags);
112
113 sizerTop->Add(sizerBtns, flags.Align(wxALIGN_CENTER_HORIZONTAL));
114 SetSizerAndFit(sizerTop);
115 Centre();
116
117 m_taskBarIcon = new MyTaskBarIcon();
118#if defined(__WXCOCOA__)
119 m_dockIcon = new MyTaskBarIcon(wxTaskBarIcon::DOCK);
120#endif
121
122 // we should be able to show up to 128 characters on recent Windows versions
123 // (and 64 on Win9x)
124 if ( !m_taskBarIcon->SetIcon(wxICON(sample),
125 "wxTaskBarIcon Sample\n"
126 "With a very, very, very, very\n"
127 "long tooltip whose length is\n"
128 "greater than 64 characters.") )
129 {
130 wxLogError(wxT("Could not set icon."));
131 }
bbf1f0e5
KB
132}
133
1e6d9c20
VS
134MyDialog::~MyDialog()
135{
136 delete m_taskBarIcon;
a5593369
DE
137#if defined(__WXCOCOA__)
138 delete m_dockIcon;
139#endif
1e6d9c20
VS
140}
141
cdcfde5d
VZ
142void MyDialog::OnAbout(wxCommandEvent& WXUNUSED(event))
143{
144 static const char * const title = "About wxWidgets Taskbar Sample";
145 static const char * const message
146 = "wxWidgets sample showing wxTaskBarIcon class\n"
147 "\n"
148 "(C) 1997 Julian Smart\n"
149 "(C) 2007 Vadim Zeitlin";
150
8f00347a 151#if defined(__WXMSW__) && wxUSE_TASKBARICON_BALLOONS
cdcfde5d
VZ
152 m_taskBarIcon->ShowBalloon(title, message, 15000, wxICON_INFORMATION);
153#else // !__WXMSW__
154 wxMessageBox(message, title, wxICON_INFORMATION, this);
155#endif // __WXMSW__/!__WXMSW__
156}
157
256b8649 158void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 159{
27301f26 160 Show(false);
bbf1f0e5
KB
161}
162
256b8649 163void MyDialog::OnExit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 164{
27301f26 165 Close(true);
bbf1f0e5
KB
166}
167
256b8649 168void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
bbf1f0e5
KB
169{
170 Destroy();
171}
172
bbf1f0e5 173
cdcfde5d
VZ
174// ----------------------------------------------------------------------------
175// MyTaskBarIcon implementation
176// ----------------------------------------------------------------------------
69ecd30f 177
3ecee463
VZ
178enum
179{
69ecd30f 180 PU_RESTORE = 10001,
ab85e6cd 181 PU_NEW_ICON,
69ecd30f 182 PU_EXIT,
d806d30a
DS
183 PU_CHECKMARK,
184 PU_SUB1,
185 PU_SUB2,
186 PU_SUBMAIN
69ecd30f
RD
187};
188
189
190BEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon)
191 EVT_MENU(PU_RESTORE, MyTaskBarIcon::OnMenuRestore)
192 EVT_MENU(PU_EXIT, MyTaskBarIcon::OnMenuExit)
ab85e6cd 193 EVT_MENU(PU_NEW_ICON,MyTaskBarIcon::OnMenuSetNewIcon)
2afa9fa7
RR
194 EVT_MENU(PU_CHECKMARK,MyTaskBarIcon::OnMenuCheckmark)
195 EVT_UPDATE_UI(PU_CHECKMARK,MyTaskBarIcon::OnMenuUICheckmark)
329fa151 196 EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick)
d806d30a
DS
197 EVT_MENU(PU_SUB1, MyTaskBarIcon::OnMenuSub)
198 EVT_MENU(PU_SUB2, MyTaskBarIcon::OnMenuSub)
69ecd30f
RD
199END_EVENT_TABLE()
200
a42b93aa 201void MyTaskBarIcon::OnMenuRestore(wxCommandEvent& )
69ecd30f 202{
cdcfde5d 203 gs_dialog->Show(true);
69ecd30f
RD
204}
205
a42b93aa 206void MyTaskBarIcon::OnMenuExit(wxCommandEvent& )
69ecd30f 207{
cdcfde5d 208 gs_dialog->Close(true);
69ecd30f
RD
209}
210
2afa9fa7
RR
211static bool check = true;
212
213void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent& )
214{
3ecee463 215 check = !check;
2afa9fa7 216}
d806d30a 217
2afa9fa7
RR
218void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent &event)
219{
3ecee463 220 event.Check(check);
2afa9fa7
RR
221}
222
ab85e6cd
JS
223void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent&)
224{
0dc6da2d 225 wxIcon icon(smile_xpm);
ab85e6cd 226
0dc6da2d 227 if (!SetIcon(icon, wxT("wxTaskBarIcon Sample - a different icon")))
ab85e6cd 228 wxMessageBox(wxT("Could not set new icon."));
ab85e6cd 229}
69ecd30f 230
d806d30a
DS
231void MyTaskBarIcon::OnMenuSub(wxCommandEvent&)
232{
233 wxMessageBox(wxT("You clicked on a submenu!"));
234}
235
bbf1f0e5 236// Overridables
dae73d74 237wxMenu *MyTaskBarIcon::CreatePopupMenu()
bbf1f0e5 238{
dae73d74 239 wxMenu *menu = new wxMenu;
3ecee463 240 menu->Append(PU_RESTORE, _T("&Restore main window"));
d806d30a 241 menu->AppendSeparator();
d806d30a
DS
242 menu->Append(PU_NEW_ICON, _T("&Set New Icon"));
243 menu->AppendSeparator();
3ecee463 244 menu->AppendCheckItem(PU_CHECKMARK, _T("Test &check mark"));
d806d30a
DS
245 menu->AppendSeparator();
246 wxMenu *submenu = new wxMenu;
247 submenu->Append(PU_SUB1, _T("One submenu"));
248 submenu->AppendSeparator();
249 submenu->Append(PU_SUB2, _T("Another submenu"));
250 menu->Append(PU_SUBMAIN, _T("Submenu"), submenu);
251#ifndef __WXMAC_OSX__ /*Mac has built-in quit menu*/
252 menu->AppendSeparator();
dae73d74 253 menu->Append(PU_EXIT, _T("E&xit"));
d806d30a 254#endif
dae73d74 255 return menu;
bbf1f0e5
KB
256}
257
329fa151 258void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&)
bbf1f0e5 259{
cdcfde5d 260 gs_dialog->Show(true);
bbf1f0e5 261}