2 * PROJECT: ReactOS system libraries
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/shellext/stobject/csystray.cpp
5 * PURPOSE: Systray shell service object implementation
6 * PROGRAMMERS: David Quintana <gigaherz@gmail.com>
7 * Shriraj Sawant a.k.a SR13 <sr.official@hotmail.com>
12 WINE_DEFAULT_DEBUG_CHANNEL(stobject
);
14 SysTrayIconHandlers_t g_IconHandlers
[] = {
15 { Volume_Init
, Volume_Shutdown
, Volume_Update
, Volume_Message
},
16 { Hotplug_Init
, Hotplug_Shutdown
, Hotplug_Update
, Hotplug_Message
},
17 { Power_Init
, Power_Shutdown
, Power_Update
, Power_Message
}
19 const int g_NumIcons
= _countof(g_IconHandlers
);
21 CSysTray::CSysTray() {}
22 CSysTray::~CSysTray() {}
24 HRESULT
CSysTray::InitNetShell()
26 HRESULT hr
= CoCreateInstance(CLSID_ConnectionTray
, 0, 1u, IID_PPV_ARG(IOleCommandTarget
, &pctNetShell
));
30 return pctNetShell
->Exec(&CGID_ShellServiceObject
,
32 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
35 HRESULT
CSysTray::ShutdownNetShell()
39 HRESULT hr
= pctNetShell
->Exec(&CGID_ShellServiceObject
,
41 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
42 pctNetShell
.Release();
46 HRESULT
CSysTray::InitIcons()
48 TRACE("Initializing Notification icons...\n");
49 for (int i
= 0; i
< g_NumIcons
; i
++)
51 HRESULT hr
= g_IconHandlers
[i
].pfnInit(this);
56 return InitNetShell();
59 HRESULT
CSysTray::ShutdownIcons()
61 TRACE("Shutting down Notification icons...\n");
62 for (int i
= 0; i
< g_NumIcons
; i
++)
64 HRESULT hr
= g_IconHandlers
[i
].pfnShutdown(this);
69 return ShutdownNetShell();
72 HRESULT
CSysTray::UpdateIcons()
74 TRACE("Updating Notification icons...\n");
75 for (int i
= 0; i
< g_NumIcons
; i
++)
77 HRESULT hr
= g_IconHandlers
[i
].pfnUpdate(this);
85 HRESULT
CSysTray::ProcessIconMessage(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
&lResult
)
87 for (int i
= 0; i
< g_NumIcons
; i
++)
89 HRESULT hr
= g_IconHandlers
[i
].pfnMessage(this, uMsg
, wParam
, lParam
, lResult
);
97 // Not handled by anyone, so return accordingly.
104 * Basically a Shell_NotifyIcon wrapper.
105 * Based on the parameters provided, it changes the current state of the notification icon.
108 * Determines whether to add, delete or modify the notification icon (represented by uId).
110 * Represents the particular notification icon.
112 * A handle to an icon for the notification object.
114 * A string for the tooltip of the notification.
116 * Determines whether to show or hide the notification icon.
118 * @return The error code.
121 HRESULT
CSysTray::NotifyIcon(INT code
, UINT uId
, HICON hIcon
, LPCWSTR szTip
, DWORD dwstate
)
123 NOTIFYICONDATA nim
= { 0 };
125 TRACE("NotifyIcon code=%d, uId=%d, hIcon=%p, szTip=%S\n", code
, uId
, hIcon
, szTip
);
127 nim
.cbSize
= sizeof(NOTIFYICONDATA
);
128 nim
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_STATE
| NIF_TIP
;
131 nim
.uCallbackMessage
= uId
;
132 nim
.dwState
= dwstate
;
133 nim
.dwStateMask
= NIS_HIDDEN
;
135 nim
.uVersion
= NOTIFYICON_VERSION
;
137 StringCchCopy(nim
.szTip
, _countof(nim
.szTip
), szTip
);
140 BOOL ret
= Shell_NotifyIcon(code
, &nim
);
141 return ret
? S_OK
: E_FAIL
;
144 DWORD WINAPI
CSysTray::s_SysTrayThreadProc(PVOID param
)
146 CSysTray
* st
= (CSysTray
*) param
;
147 return st
->SysTrayThreadProc();
150 HRESULT
CSysTray::SysTrayMessageLoop()
155 while ((ret
= GetMessage(&msg
, NULL
, 0, 0)) != 0)
160 TranslateMessage(&msg
);
161 DispatchMessage(&msg
);
167 HRESULT
CSysTray::SysTrayThreadProc()
169 WCHAR strFileName
[MAX_PATH
];
170 GetModuleFileNameW(g_hInstance
, strFileName
, MAX_PATH
);
171 HMODULE hLib
= LoadLibraryW(strFileName
);
173 CoInitializeEx(NULL
, COINIT_DISABLE_OLE1DDE
| COINIT_APARTMENTTHREADED
);
177 HRESULT ret
= SysTrayMessageLoop();
181 FreeLibraryAndExitThread(hLib
, ret
);
184 HRESULT
CSysTray::CreateSysTrayThread()
186 TRACE("CSysTray Init TODO: Initialize tray icon handlers.\n");
188 HANDLE hThread
= CreateThread(NULL
, 0, s_SysTrayThreadProc
, this, 0, NULL
);
190 CloseHandle(hThread
);
195 HRESULT
CSysTray::DestroySysTrayWindow()
202 // *** IOleCommandTarget methods ***
203 HRESULT STDMETHODCALLTYPE
CSysTray::QueryStatus(const GUID
*pguidCmdGroup
, ULONG cCmds
, OLECMD prgCmds
[], OLECMDTEXT
*pCmdText
)
209 HRESULT STDMETHODCALLTYPE
CSysTray::Exec(const GUID
*pguidCmdGroup
, DWORD nCmdID
, DWORD nCmdexecopt
, VARIANT
*pvaIn
, VARIANT
*pvaOut
)
211 if (!IsEqualGUID(*pguidCmdGroup
, CGID_ShellServiceObject
))
216 case OLECMDID_NEW
: // init
217 return CreateSysTrayThread();
218 case OLECMDID_SAVE
: // shutdown
219 return DestroySysTrayWindow();
224 BOOL
CSysTray::ProcessWindowMessage(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
&lResult
, DWORD dwMsgMapID
)
239 SetTimer(1, 2000, NULL
);
251 TRACE("SysTray message received %u (%08p %08p)\n", uMsg
, wParam
, lParam
);
253 hr
= ProcessIconMessage(uMsg
, wParam
, lParam
, lResult
);