OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
trayicon.cpp
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2// ObjectWindows
3// Copyright (c) 1998 by Bidus Yura
4//
5/// \file
6/// This is a conglomeration of ideas from the MSJ "Webster" application,
7/// sniffing round the online docs, and from other implementations such
8/// as PJ Naughter's "CTrayIconifyIcon" (http://indigo.ie/~pjn/ntray.html)
9/// especially the "CTrayIcon::OnTrayNotification" member function.
10///
11/// This class is a light wrapper around the windows system tray stuff. It
12/// adds an icon to the system tray with the specified ToolTip text and
13/// callback notification value, which is sent back to the Parent window.
14///
15/// The tray icon can be instantiated using either the constructor or by
16/// declaring the object and creating (and displaying) it later on in the
17/// program. eg.
18/// \code
19/// TTrayIcon* Trayicon; // Member variable of some class
20///
21/// ...
22/// // in some member function maybe...
23/// Trayicon = new TTrayIcon(... ... ... ... );
24/// ...
25/// //in SetupWindow() or after where hWnd is valid
26/// Trayicon->Create();
27/// \endcode
28/// or
29/// \code
30/// TTrayIcon Trayicon; // Member variable of some class
31/// ...
32/// // in some member function maybe...
33/// Trayicon.Init(........);
34/// ...
35/// //in SetupWindow() or after where hWnd is valid
36/// Trayicon.Create();
37/// \endcode
38//
39// Clobbered together by Chris Maunder.
40//
41// Rewrited for OWL by Bidus Yura;
42//
43////////////////////////////////////////////////////////////////////////////////
44#include <owl/pch.h>
45#include <owl/shellitm.h>
46
47#include <owl/trayicon.h>
48#include <owl/window.h>
49
50namespace owl {
51
52//
53// TTrayIcon construction/creation/destruction
54//
56:
57 Module(module),
58 Icon(nullptr),
59 ShouldDelete(false)
60{
61 tstring tos(_T(""));
62 Init(nullptr, 0, tos, nullptr, 0);
63}
64
65//
66//
67//
69 uint id, TModule* module)
70:
71 Module(module),
72 Icon(nullptr),
73 ShouldDelete(false)
74{
75 Init(parent, msg, tip, &icon, id);
76 bHidden = false;
77}
78//
80 TResId icon, uint id, TModule* module)
81:
82 Module(module),
83 Icon(nullptr),
84 ShouldDelete(false)
85{
86
87 Init(parent,msg,tip,new TIcon(Module?(HINSTANCE)*Module:HINSTANCE(nullptr),icon),id);
88 ShouldDelete = true;
89 bHidden = false;
90}
91//
92//
93//
94//
96 TIcon* icon, uint id)
97{
98 //Make sure we avoid conflict with other messages
99 bEnabled = false;
100 bHidden = false;
101 Parent = parent;
102
103 if(Icon && ShouldDelete)
104 delete Icon;
105 Icon = icon;
106
107 cbSize = sizeof(NOTIFYICONDATA);
108 hWnd = nullptr;
109 uFlags = 0;//NIF_MESSAGE|NIF_ICON|NIF_TIP;
110 uID = id;
112 if(msg){
116 }
117 hIcon = nullptr;
118 if(Icon){
119 hIcon = *Icon;
120 uFlags |= NIF_ICON;
121 }
122 memset(szTip, 0, sizeof(szTip));
123 if(::_tcslen(tip.c_str())){
124 if(::_tcslen(tip.c_str()) > 64){
125 _tcsncpy(szTip, tip.c_str(), 64);
126 szTip[64] = 0;
127 }
128 else
129 ::_tcscpy(szTip, tip.c_str());
130 uFlags |= NIF_TIP;
131 }
132 CHECK(uFlags);
133}
134
135//
136//
137//
139{
140 //Make sure Notification window is valid
141 if(!Parent->GetHandle() || !Parent->IsWindow())
142 return false;
143
144 hWnd = *Parent;
145
146 // Set the tray icon
148 return false;;
149 bEnabled = true;
150 return true;
151}
152
153//
154//
155//
157{
158 RemoveIcon();
159 if(ShouldDelete)
160 delete Icon;
161}
162
163//
164// CTrayIcon icon manipulation
165//
167{
168 HideIcon();
169 ShowIcon();
170}
171
172//
173//
174//
176{
177 if(!bEnabled)
178 return;
179
180 uFlags = 0;
182 bEnabled = false;
183}
184
185//
186//
187//
189{
190 if(bEnabled && !bHidden) {
191 uFlags &= ~NIF_ICON;
193 bHidden = true;
194 }
195}
196
197//
198//
200{
201 return tstring(szTip);
202}
203
204//
205//
206//
208{
209 if (bEnabled && bHidden){
210 uFlags = 0;
213 if(Icon)
214 uFlags |= NIF_ICON;
215 if(::_tcslen(szTip))
216 uFlags |= NIF_TIP;
218 bHidden = false;
219 }
220}
221
222//
223//
224//
226{
227 if(Icon && ShouldDelete)
228 delete Icon;
229 ShouldDelete = shouldDelete == AutoDelete;
230 Icon = &icon;
231
232 if(!bEnabled)
233 return true;
234
235 uFlags |= NIF_ICON;
236 hIcon = *Icon;
237
239}
240
241//
242// TTrayIcon tooltip text manipulation
243//
245{
246 if(_tcscmp(text.c_str(),szTip)!= 0){
247 uFlags |= NIF_TIP;
248 if(::_tcslen(text.c_str()) > 64){
249 _tcsncpy(szTip, text.c_str(), 64);
250 szTip[64] = 0;
251 }
252 else if(::_tcslen(text.c_str()))
253 ::_tcscpy(szTip, text.c_str());
254 else{
255 szTip[0] = 0;
256 uFlags &= ~NIF_TIP;
257 }
258 if (!bEnabled)
259 return true;
261 }
262 return true;
263}
264
265//
266// TTrayIcon notification window stuff
267//
269{
270 Parent = window;
271
272 if (!bEnabled)
273 return true;
274
275 //Make sure Notification window is valid
276 if(!Parent->GetHandle() || !Parent->IsWindow())
277 return true;
278
279 hWnd = *Parent;
280
282}
283
284//
285// TTrayIcon implentation of EvTrayNotification
286// call this functionon
288{
289 //Return quickly if its not for this tray icon
290 if (uID != id)
291 return 0L;
292
293 // Clicking with right button brings up a context menu
294 if (LOWORD(event) == WM_RBUTTONUP){
295 TMenu menu(*Module, uID);
296 if(!menu.IsOK())
297 return 0;
298 //Display and track the popup menu
299 TPoint pos;
300 GetCursorPos(&pos);
302 TPopupMenu popup(menu);
303 // Make first menu item the default (bold font)
304 popup.SetDefaultItem(0, true);
305 popup.TrackPopupMenu(TPM_LEFTBUTTON, pos, 0, *Parent);
306 }
307 else if (LOWORD(event) == WM_LBUTTONDBLCLK){
308 TMenu menu(*Module, uID);
309 if(!menu.IsOK())
310 return 0;
311 // double click received, the default action is to execute first menu item
314 }
315 return 1;
316}
317
318} // OWL namespace
319/* ========================================================================== */
320
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
TIcon, derived from TGdiObject, represents the GDI object icon class.
Definition gdiobjec.h:670
The TMenu class encapsulates window menus.
Definition menu.h:77
uint GetMenuItemID(int posItem) const
Returns the ID of the menu item at the position specified by posItem.
Definition menu.cpp:174
bool IsOK() const
Returns true if the menu has a valid handle.
Definition menu.h:333
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TPopupMenu creates an empty pop-up menu to add to an existing window or pop-up menu.
Definition menu.h:189
static BOOL Shell_NotifyIcon(DWORD, PNOTIFYICONDATA)
Invokes 'Shell_NotifyIcon' indirectly.
Definition shellitm.cpp:227
virtual ~TTrayIcon()
Definition trayicon.cpp:156
bool SetParent(TWindow *wnd)
Definition trayicon.cpp:268
bool SetText(const tstring &text)
Definition trayicon.cpp:244
TWindow * Parent
Definition trayicon.h:75
void MoveToRight()
Definition trayicon.cpp:166
bool bEnabled
does O/S support tray icon?
Definition trayicon.h:73
tstring GetText() const
Definition trayicon.cpp:199
bool bHidden
Has the icon been hidden?
Definition trayicon.h:74
void RemoveIcon()
Definition trayicon.cpp:175
virtual void Init(TWindow *parent, TMsgId, const tstring &tip, TIcon *icon=0, uint id=0)
Definition trayicon.cpp:95
TResult EvTrayNotification(TParam1, TParam2)
Default handler for tray notification message.
Definition trayicon.cpp:287
bool ShouldDelete
Definition trayicon.h:78
bool SetIcon(TIcon &icon, TAutoDelete=NoAutoDelete)
Definition trayicon.cpp:225
TIcon * Icon
Definition trayicon.h:77
bool Create()
Create the tray icon => must have valid hWnd => call in SetupWindow();.
Definition trayicon.cpp:138
TTrayIcon(TModule *module=&GetGlobalModule())
Definition trayicon.cpp:55
TModule * Module
Definition trayicon.h:76
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
bool IsWindow() const
Returns true if an HWND is being used.
Definition window.h:2040
TResult SendMessage(TMsgId, TParam1=0, TParam2=0) const
Sends a message (msg) to a specified window or windows.
Definition window.cpp:3288
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
#define _tcscmp
Definition cygwin.h:75
#define _tcscpy
Definition cygwin.h:79
#define _tcsncpy
Definition cygwin.h:80
#define _tcslen
Definition cygwin.h:74
#define _T(x)
Definition cygwin.h:51
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
@ AutoDelete
Definition gdibase.h:70
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
UINT TMsgId
Message ID type.
Definition dispatch.h:53
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
WPARAM TParam1
First parameter type.
Definition dispatch.h:54
TModule * Module
Definition global.cpp:34
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
Definitions of Win95 Shell Clases: TShellItem, TShellItemIterator, TPidl, TShellMalloc.
Base window class TWindow definition, including HWND encapsulation.