source: nscp/trunk/modules/SysTray/TrayIcon.cpp @ 2eeb220

Last change on this file since 2eeb220 was 2eeb220, checked in by Michael Medin <michael@…>, 8 years ago

Changed dialog code and log view to work better

  • Property mode set to 100644
File size: 3.8 KB
Line 
1#include "stdafx.h"
2#include ".\trayicon.h"
3#include "resource.h"
4#include <strEx.h>
5#include <ShellAPI.h>
6
7namespace TrayIcon
8{
9        HWND ghDlgWnd = NULL;
10        HANDLE ghMutex;
11        std::string defaultCommand;
12}
13
14BOOL CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) {
15        switch (uMsg)
16        {
17        case WM_INITDIALOG:
18                SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str());
19        case WM_COMMAND:
20                switch (LOWORD(wParam))
21                {
22                case IDOK:
23                        {
24                                char *c=new char[1024];
25                                if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023))
26                                        TrayIcon::defaultCommand = c;
27                                delete [] c;
28                        }
29
30                case IDCANCEL:
31                        EndDialog(hwndDlg, wParam);
32                        return TRUE;
33                }
34        }
35        return FALSE;
36}
37
38INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
39{
40        DWORD tmp = 0;
41        switch (uMsg)
42        {
43        case WM_INITDIALOG:
44                addIcon(hwndDlg);
45//              return TRUE;
46                break;
47
48        case WM_ICON_NOTIFY:
49                if (lParam==WM_RBUTTONDOWN) {
50                        HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP));
51                        HMENU hSubMenu = GetSubMenu(hMenu, 0);
52                        const RECT r = {0, 0, 0, 0};
53                        POINT pt;
54                        GetCursorPos(&pt);
55                        SetForegroundWindow(ghDlgWnd);
56                        int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, ghDlgWnd, &r);
57                        DestroyMenu(hMenu);
58                        switch (cmd) {
59                        case ID_POPUP_STOPSERVICE:
60                                NSCModuleHelper::StopService();
61                                break;
62                        case ID_POPUP_INJECTCOMMAND:
63                                if (TrayIcon::defaultCommand.empty())
64                                        TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", "");
65                                if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) {
66                                        NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand);
67                                }
68                                break;
69                        case ID_POPUP_SHOWLOG:
70//                              SE_ERR_ACCESSDENIED
71                                if ((tmp = (INT)ShellExecute(ghDlgWnd, "open", (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(), NULL, NULL, SW_SHOWNORMAL))<=32) {
72                                        NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos((INT)tmp));
73                                }
74                        }
75                        return TRUE;
76                }
77                break;
78        }
79        return FALSE;
80}
81void TrayIcon::addIcon(HWND hWnd) {
82        assert(NSCModuleWrapper::getModule() != NULL);
83        assert(hWnd != NULL);
84
85        NOTIFYICONDATA ndata;
86        ndata.cbSize=sizeof(NOTIFYICONDATA);
87        ndata.hWnd=hWnd;
88        ndata.uID=2000;
89        ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
90        ndata.uCallbackMessage=WM_ICON_NOTIFY;
91        ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY));
92        strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63);
93        Shell_NotifyIcon(NIM_ADD,&ndata);
94}
95
96void TrayIcon::removeIcon(void) {
97        NOTIFYICONDATA ndata;
98        ndata.hWnd=ghDlgWnd;
99        ndata.uID=2000;
100        Shell_NotifyIcon(NIM_DELETE,&ndata);
101}
102
103void TrayIcon::createDialog(void) {
104        ghMutex = ::CreateMutex(NULL, TRUE, NULL);
105        if (!ghMutex)
106                throw std::string("Could not create mutex.");
107
108        ghDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,DialogProc);
109
110        MSG Msg;
111        while(::GetMessage(&Msg, ghDlgWnd, 0, 0))
112        {
113                if (Msg.message == WM_MY_CLOSE)
114                        break;
115                if (!::IsWindow(ghDlgWnd) || !::IsDialogMessage(ghDlgWnd, &Msg)) {
116                        ::TranslateMessage(&Msg);
117                        ::DispatchMessage(&Msg);
118                }
119        }
120
121        ::DestroyWindow(ghDlgWnd);
122        ::ReleaseMutex(ghMutex);
123}
124void TrayIcon::destroyDialog(void) {
125        ::PostMessage(ghDlgWnd, WM_MY_CLOSE, NULL, NULL);
126}
127bool TrayIcon::waitForTermination(DWORD timeout /* = 5000L */) {
128        DWORD dwWaitResult = WaitForSingleObject(ghMutex, timeout);
129        switch (dwWaitResult) {
130                // The thread got mutex ownership.
131        case WAIT_OBJECT_0:
132                ReleaseMutex(ghMutex);
133                CloseHandle(ghMutex);
134                return true;
135                // Cannot get mutex ownership due to time-out.
136        case WAIT_TIMEOUT:
137                return false;
138
139                // Got ownership of the abandoned mutex object.
140        case WAIT_ABANDONED:
141                return false;
142        }
143        return false;
144}
Note: See TracBrowser for help on using the repository browser.