1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon demo
4 // Author: Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 // the application icon (under Windows and OS/2 it is in resources)
31 #ifndef wxHAS_IMAGES_IN_RESOURCES
32 #include "../sample.xpm"
37 #include "wx/taskbar.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 static MyDialog
*gs_dialog
= NULL
;
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
59 if ( !wxApp::OnInit() )
62 if ( !wxTaskBarIcon::IsAvailable() )
66 "There appears to be no system tray support in your current environment. This sample may not behave as expected.",
68 wxOK
| wxICON_EXCLAMATION
72 // Create the main window
73 gs_dialog
= new MyDialog(wxT("wxTaskBarIcon Test Dialog"));
75 gs_dialog
->Show(true);
81 // ----------------------------------------------------------------------------
82 // MyDialog implementation
83 // ----------------------------------------------------------------------------
85 BEGIN_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
)
93 MyDialog::MyDialog(const wxString
& title
)
94 : wxDialog(NULL
, wxID_ANY
, title
)
96 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
99 flags
.Border(wxALL
, 10);
101 sizerTop
->Add(new wxStaticText
105 wxT("Press 'Hide me' to hide this window, Exit to quit.")
108 sizerTop
->Add(new wxStaticText
112 wxT("Double-click on the taskbar icon to show me again.")
115 sizerTop
->AddStretchSpacer()->SetMinSize(200, 50);
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
);
122 sizerTop
->Add(sizerBtns
, flags
.Align(wxALIGN_CENTER_HORIZONTAL
));
123 SetSizerAndFit(sizerTop
);
126 m_taskBarIcon
= new MyTaskBarIcon();
128 // we should be able to show up to 128 characters on recent Windows versions
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.") )
136 wxLogError(wxT("Could not set icon."));
139 #if defined(__WXOSX__) && wxOSX_USE_COCOA
140 m_dockIcon
= new MyTaskBarIcon(wxTBI_DOCK
);
141 if ( !m_dockIcon
->SetIcon(wxICON(sample
)) )
143 wxLogError(wxT("Could not set icon."));
148 MyDialog::~MyDialog()
150 delete m_taskBarIcon
;
151 #if defined(__WXCOCOA__)
156 void MyDialog::OnAbout(wxCommandEvent
& WXUNUSED(event
))
158 static const char * const title
= "About wxWidgets Taskbar Sample";
159 static const char * const message
160 = "wxWidgets sample showing wxTaskBarIcon class\n"
162 "(C) 1997 Julian Smart\n"
163 "(C) 2007 Vadim Zeitlin";
165 #if defined(__WXMSW__) && wxUSE_TASKBARICON_BALLOONS
166 m_taskBarIcon
->ShowBalloon(title
, message
, 15000, wxICON_INFORMATION
);
168 wxMessageBox(message
, title
, wxICON_INFORMATION
|wxOK
, this);
169 #endif // __WXMSW__/!__WXMSW__
172 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
177 void MyDialog::OnExit(wxCommandEvent
& WXUNUSED(event
))
182 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
188 // ----------------------------------------------------------------------------
189 // MyTaskBarIcon implementation
190 // ----------------------------------------------------------------------------
204 BEGIN_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
)
215 void MyTaskBarIcon::OnMenuRestore(wxCommandEvent
& )
217 gs_dialog
->Show(true);
220 void MyTaskBarIcon::OnMenuExit(wxCommandEvent
& )
222 gs_dialog
->Close(true);
225 static bool check
= true;
227 void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent
& )
232 void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent
&event
)
237 void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent
&)
239 wxIcon
icon(smile_xpm
);
241 if (!SetIcon(icon
, wxT("wxTaskBarIcon Sample - a different icon")))
242 wxMessageBox(wxT("Could not set new icon."));
245 void MyTaskBarIcon::OnMenuSub(wxCommandEvent
&)
247 wxMessageBox(wxT("You clicked on a submenu!"));
251 wxMenu
*MyTaskBarIcon::CreatePopupMenu()
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 */
267 if ( OSXIsStatusItem() )
270 menu
->AppendSeparator();
271 menu
->Append(PU_EXIT
, wxT("E&xit"));
276 void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent
&)
278 gs_dialog
->Show(true);