source: nscp/trunk/modules/SysTray/TrayIcon.cpp @ 2172776

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

2006-03-05 MickeM

  • Fixed -v FILEAGE check_nt (NSClient) check.
  • Added licence agreement header to all files
  • Property mode set to 100644
File size: 4.9 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#include "stdafx.h"
22#include ".\trayicon.h"
23#include "resource.h"
24#include <strEx.h>
25#include <ShellAPI.h>
26
27unsigned IconWidget_::threadProc(LPVOID lpParameter)
28{
29        createDialog();
30        return 0;
31}
32
33
34void IconWidget_::createDialog(void) {
35        hDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,TrayIcon::DialogProc);
36
37        MSG Msg;
38        while(::GetMessage(&Msg, hDlgWnd, 0, 0))
39        {
40                if (Msg.message == WM_MY_CLOSE)
41                        break;
42                if (!::IsWindow(hDlgWnd) || !::IsDialogMessage(hDlgWnd, &Msg)) {
43                        ::TranslateMessage(&Msg);
44                        ::DispatchMessage(&Msg);
45                }
46        }
47        TrayIcon::removeIcon(hDlgWnd);
48
49        ::DestroyWindow(hDlgWnd);
50}
51void IconWidget_::exitThread(void) {
52        ::PostMessage(hDlgWnd, WM_MY_CLOSE, NULL, NULL);
53
54}
55
56namespace TrayIcon
57{
58        std::string defaultCommand;
59}
60
61BOOL CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) {
62        switch (uMsg)
63        {
64        case WM_INITDIALOG:
65                SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str());
66        case WM_COMMAND:
67                switch (LOWORD(wParam))
68                {
69                case IDOK:
70                        {
71                                char *c=new char[1024];
72                                if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023))
73                                        TrayIcon::defaultCommand = c;
74                                delete [] c;
75                        }
76
77                case IDCANCEL:
78                        EndDialog(hwndDlg, wParam);
79                        return TRUE;
80                }
81        }
82        return FALSE;
83}
84
85INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
86{
87        switch (uMsg)
88        {
89        case WM_INITDIALOG:
90                addIcon(hwndDlg);
91                break;
92
93        case WM_ICON_NOTIFY:
94                if (lParam==WM_RBUTTONDOWN) {
95                        HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP));
96                        HMENU hSubMenu = GetSubMenu(hMenu, 0);
97                        const RECT r = {0, 0, 0, 0};
98                        POINT pt;
99                        GetCursorPos(&pt);
100                        SetForegroundWindow(hwndDlg);
101                        int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, hwndDlg, &r);
102                        DestroyMenu(hMenu);
103                        switch (cmd) {
104                        case ID_POPUP_STOPSERVICE:
105                                NSCModuleHelper::StopService();
106                                break;
107                        case ID_POPUP_INJECTCOMMAND:
108                                if (TrayIcon::defaultCommand.empty())
109                                        TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", "");
110                                if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) {
111                                        // @todo NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand);
112                                }
113                                break;
114                        case ID_POPUP_SHOWLOG:
115                                {
116                                        long long err = reinterpret_cast<long long>(ShellExecute(hwndDlg, "open",
117                                                (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(),
118                                                NULL, NULL, SW_SHOWNORMAL));
119                                        if (err <=32) {
120                                                        NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos(err));
121                                                }
122                                }
123                        }
124                        return TRUE;
125                }
126                break;
127        }
128        return FALSE;
129}
130void TrayIcon::addIcon(HWND hWnd) {
131        assert(NSCModuleWrapper::getModule() != NULL);
132        assert(hWnd != NULL);
133
134        NOTIFYICONDATA ndata;
135        ndata.cbSize=sizeof(NOTIFYICONDATA);
136        ndata.hWnd=hWnd;
137        ndata.uID=2000;
138        ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
139        ndata.uCallbackMessage=WM_ICON_NOTIFY;
140        ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY));
141        strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63);
142        Shell_NotifyIcon(NIM_ADD,&ndata);
143}
144
145void TrayIcon::removeIcon(HWND hWnd) {
146        NOTIFYICONDATA ndata;
147        ndata.cbSize=sizeof(NOTIFYICONDATA);
148        ndata.hWnd=hWnd;
149        ndata.uID=2000;
150        Shell_NotifyIcon(NIM_DELETE,&ndata);
151}
Note: See TracBrowser for help on using the repository browser.