1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTaskBarIcon demo
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
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"
38 #include "wx/taskbar.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 static MyDialog
*gs_dialog
= NULL
;
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
60 if ( !wxApp::OnInit() )
63 if ( !wxTaskBarIcon::IsAvailable() )
67 "There appears to be no system tray support in your current environment. This sample may not behave as expected.",
69 wxOK
| wxICON_EXCLAMATION
73 // Create the main window
74 gs_dialog
= new MyDialog(wxT("wxTaskBarIcon Test Dialog"));
76 gs_dialog
->Show(true);
82 // ----------------------------------------------------------------------------
83 // MyDialog implementation
84 // ----------------------------------------------------------------------------
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
)
94 MyDialog::MyDialog(const wxString
& title
)
95 : wxDialog(NULL
, wxID_ANY
, title
)
97 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
100 flags
.Border(wxALL
, 10);
102 sizerTop
->Add(new wxStaticText
106 wxT("Press 'Hide me' to hide this window, Exit to quit.")
109 sizerTop
->Add(new wxStaticText
113 wxT("Double-click on the taskbar icon to show me again.")
116 sizerTop
->AddStretchSpacer()->SetMinSize(200, 50);
118 wxSizer
* const sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
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
);
123 sizerTop
->Add(sizerBtns
, flags
.Align(wxALIGN_CENTER_HORIZONTAL
));
124 SetSizerAndFit(sizerTop
);
127 m_taskBarIcon
= new MyTaskBarIcon();
129 // we should be able to show up to 128 characters on recent Windows versions
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.") )
137 wxLogError(wxT("Could not set icon."));
140 #if defined(__WXOSX__) && wxOSX_USE_COCOA
141 m_dockIcon
= new MyTaskBarIcon(wxTaskBarIcon::DOCK
);
142 if ( !m_dockIcon
->SetIcon(wxICON(sample
)) )
144 wxLogError(wxT("Could not set icon."));
149 MyDialog::~MyDialog()
151 delete m_taskBarIcon
;
152 #if defined(__WXCOCOA__)
157 void MyDialog::OnAbout(wxCommandEvent
& WXUNUSED(event
))
159 static const char * const title
= "About wxWidgets Taskbar Sample";
160 static const char * const message
161 = "wxWidgets sample showing wxTaskBarIcon class\n"
163 "(C) 1997 Julian Smart\n"
164 "(C) 2007 Vadim Zeitlin";
166 #if defined(__WXMSW__) && wxUSE_TASKBARICON_BALLOONS
167 m_taskBarIcon
->ShowBalloon(title
, message
, 15000, wxICON_INFORMATION
);
169 wxMessageBox(message
, title
, wxICON_INFORMATION
|wxOK
, this);
170 #endif // __WXMSW__/!__WXMSW__
173 void MyDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
178 void MyDialog::OnExit(wxCommandEvent
& WXUNUSED(event
))
183 void MyDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
189 // ----------------------------------------------------------------------------
190 // MyTaskBarIcon implementation
191 // ----------------------------------------------------------------------------
205 BEGIN_EVENT_TABLE(MyTaskBarIcon
, wxTaskBarIcon
)
206 EVT_MENU(PU_RESTORE
, MyTaskBarIcon::OnMenuRestore
)
207 EVT_MENU(PU_EXIT
, MyTaskBarIcon::OnMenuExit
)
208 EVT_MENU(PU_NEW_ICON
,MyTaskBarIcon::OnMenuSetNewIcon
)
209 EVT_MENU(PU_CHECKMARK
,MyTaskBarIcon::OnMenuCheckmark
)
210 EVT_UPDATE_UI(PU_CHECKMARK
,MyTaskBarIcon::OnMenuUICheckmark
)
211 EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick
)
212 EVT_MENU(PU_SUB1
, MyTaskBarIcon::OnMenuSub
)
213 EVT_MENU(PU_SUB2
, MyTaskBarIcon::OnMenuSub
)
216 void MyTaskBarIcon::OnMenuRestore(wxCommandEvent
& )
218 gs_dialog
->Show(true);
221 void MyTaskBarIcon::OnMenuExit(wxCommandEvent
& )
223 gs_dialog
->Close(true);
226 static bool check
= true;
228 void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent
& )
233 void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent
&event
)
238 void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent
&)
240 wxIcon
icon(smile_xpm
);
242 if (!SetIcon(icon
, wxT("wxTaskBarIcon Sample - a different icon")))
243 wxMessageBox(wxT("Could not set new icon."));
246 void MyTaskBarIcon::OnMenuSub(wxCommandEvent
&)
248 wxMessageBox(wxT("You clicked on a submenu!"));
252 wxMenu
*MyTaskBarIcon::CreatePopupMenu()
254 wxMenu
*menu
= new wxMenu
;
255 menu
->Append(PU_RESTORE
, wxT("&Restore main window"));
256 menu
->AppendSeparator();
257 menu
->Append(PU_NEW_ICON
, wxT("&Set New Icon"));
258 menu
->AppendSeparator();
259 menu
->AppendCheckItem(PU_CHECKMARK
, wxT("Test &check mark"));
260 menu
->AppendSeparator();
261 wxMenu
*submenu
= new wxMenu
;
262 submenu
->Append(PU_SUB1
, wxT("One submenu"));
263 submenu
->AppendSeparator();
264 submenu
->Append(PU_SUB2
, wxT("Another submenu"));
265 menu
->Append(PU_SUBMAIN
, wxT("Submenu"), submenu
);
266 /* OSX has built-in quit menu for the dock menu, but not for the status item */
268 if ( OSXIsStatusItem() )
271 menu
->AppendSeparator();
272 menu
->Append(PU_EXIT
, wxT("E&xit"));
277 void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent
&)
279 gs_dialog
->Show(true);