removed the menu item which could be used to remove the icon and leave the user witho...
[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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 // the application icon (under Windows and OS/2 it is in resources)
24 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
25 #include "../sample.xpm"
26 #endif
27
28 #include "smile.xpm"
29
30 #include "wx/taskbar.h"
31 #include "tbtest.h"
32
33 // Declare two frames
34 MyDialog *dialog = NULL;
35
36 IMPLEMENT_APP(MyApp)
37
38 bool MyApp::OnInit(void)
39 {
40 if ( !wxApp::OnInit() )
41 return false;
42
43 // Create the main frame window
44 dialog = new MyDialog(NULL, wxID_ANY, wxT("wxTaskBarIcon Test Dialog"), wxDefaultPosition, wxSize(365, 290));
45
46 dialog->Show(true);
47
48 return true;
49 }
50
51
52 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
53 EVT_BUTTON(wxID_OK, MyDialog::OnOK)
54 EVT_BUTTON(wxID_EXIT, MyDialog::OnExit)
55 EVT_CLOSE(MyDialog::OnCloseWindow)
56 END_EVENT_TABLE()
57
58
59 MyDialog::MyDialog(wxWindow* parent, const wxWindowID id, const wxString& title,
60 const wxPoint& pos, const wxSize& size, const long windowStyle):
61 wxDialog(parent, id, title, pos, size, windowStyle)
62 {
63 Init();
64 }
65
66 MyDialog::~MyDialog()
67 {
68 delete m_taskBarIcon;
69 #if defined(__WXCOCOA__)
70 delete m_dockIcon;
71 #endif
72 }
73
74 void MyDialog::OnOK(wxCommandEvent& WXUNUSED(event))
75 {
76 Show(false);
77 }
78
79 void MyDialog::OnExit(wxCommandEvent& WXUNUSED(event))
80 {
81 Close(true);
82 }
83
84 void MyDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
85 {
86 Destroy();
87 }
88
89 void MyDialog::Init(void)
90 {
91 (void)new wxStaticText(this, wxID_ANY, _T("Press 'Hide me' to hide me, Exit to quit."),
92 wxPoint(10, 20));
93
94 (void)new wxStaticText(this, wxID_ANY, _T("Double-click on the taskbar icon to show me again."),
95 wxPoint(10, 40));
96
97 (void)new wxButton(this, wxID_EXIT, _T("Exit"), wxPoint(185, 230), wxSize(80, 25));
98 (new wxButton(this, wxID_OK, _T("Hide me"), wxPoint(100, 230), wxSize(80, 25)))->SetDefault();
99 Centre(wxBOTH);
100
101 m_taskBarIcon = new MyTaskBarIcon();
102 #if defined(__WXCOCOA__)
103 m_dockIcon = new MyTaskBarIcon(wxTaskBarIcon::DOCK);
104 #endif
105 // we should be able to show up to 128 characters on recent Windows versions
106 // (and 64 on Win9x)
107 if (!m_taskBarIcon->SetIcon(wxICON(sample),
108 "wxTaskBarIcon Sample\n"
109 "With a very, very, very, very\n"
110 "long tooltip whose length is\n"
111 "greater than 64 characters."))
112 {
113 wxMessageBox(wxT("Could not set icon."));
114 }
115 }
116
117
118 enum
119 {
120 PU_RESTORE = 10001,
121 PU_NEW_ICON,
122 PU_EXIT,
123 PU_CHECKMARK,
124 PU_SUB1,
125 PU_SUB2,
126 PU_SUBMAIN
127 };
128
129
130 BEGIN_EVENT_TABLE(MyTaskBarIcon, wxTaskBarIcon)
131 EVT_MENU(PU_RESTORE, MyTaskBarIcon::OnMenuRestore)
132 EVT_MENU(PU_EXIT, MyTaskBarIcon::OnMenuExit)
133 EVT_MENU(PU_NEW_ICON,MyTaskBarIcon::OnMenuSetNewIcon)
134 EVT_MENU(PU_CHECKMARK,MyTaskBarIcon::OnMenuCheckmark)
135 EVT_UPDATE_UI(PU_CHECKMARK,MyTaskBarIcon::OnMenuUICheckmark)
136 EVT_TASKBAR_LEFT_DCLICK (MyTaskBarIcon::OnLeftButtonDClick)
137 EVT_MENU(PU_SUB1, MyTaskBarIcon::OnMenuSub)
138 EVT_MENU(PU_SUB2, MyTaskBarIcon::OnMenuSub)
139 END_EVENT_TABLE()
140
141 void MyTaskBarIcon::OnMenuRestore(wxCommandEvent& )
142 {
143 dialog->Show(true);
144 }
145
146 void MyTaskBarIcon::OnMenuExit(wxCommandEvent& )
147 {
148 dialog->Close(true);
149 }
150
151 static bool check = true;
152
153 void MyTaskBarIcon::OnMenuCheckmark(wxCommandEvent& )
154 {
155 check = !check;
156 }
157
158 void MyTaskBarIcon::OnMenuUICheckmark(wxUpdateUIEvent &event)
159 {
160 event.Check(check);
161 }
162
163 void MyTaskBarIcon::OnMenuSetNewIcon(wxCommandEvent&)
164 {
165 wxIcon icon(smile_xpm);
166
167 if (!SetIcon(icon, wxT("wxTaskBarIcon Sample - a different icon")))
168 wxMessageBox(wxT("Could not set new icon."));
169 }
170
171 void MyTaskBarIcon::OnMenuSub(wxCommandEvent&)
172 {
173 wxMessageBox(wxT("You clicked on a submenu!"));
174 }
175
176 // Overridables
177 wxMenu *MyTaskBarIcon::CreatePopupMenu()
178 {
179 wxMenu *menu = new wxMenu;
180 menu->Append(PU_RESTORE, _T("&Restore main window"));
181 menu->AppendSeparator();
182 menu->Append(PU_NEW_ICON, _T("&Set New Icon"));
183 menu->AppendSeparator();
184 menu->AppendCheckItem(PU_CHECKMARK, _T("Test &check mark"));
185 menu->AppendSeparator();
186 wxMenu *submenu = new wxMenu;
187 submenu->Append(PU_SUB1, _T("One submenu"));
188 submenu->AppendSeparator();
189 submenu->Append(PU_SUB2, _T("Another submenu"));
190 menu->Append(PU_SUBMAIN, _T("Submenu"), submenu);
191 #ifndef __WXMAC_OSX__ /*Mac has built-in quit menu*/
192 menu->AppendSeparator();
193 menu->Append(PU_EXIT, _T("E&xit"));
194 #endif
195 return menu;
196 }
197
198 void MyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent&)
199 {
200 dialog->Show(true);
201 }