d326ccdc44cf6dca952860791f80eea54e595338
[olsrd.git] / gui / win32 / Main / TrayIcon.cpp
1 #include "stdafx.h"
2 #include "TrayIcon.h"
3
4 #include <windows.h>
5 #include <shellapi.h>
6
7 #include "Frontend.h"
8 #include "FrontendDlg.h"
9 #include "Resource.h"
10
11 #define TRAYICONID 987435
12
13 HWND s_hWnd;
14 TrayIcon* TrayIcon::instance = NULL;
15
16 LRESULT CALLBACK TrayIconProc( HWND, UINT, WPARAM, LPARAM lParam )
17 {
18         switch (lParam)
19         {
20         case WM_LBUTTONDOWN:
21         case WM_RBUTTONDOWN:
22                 TrayIcon::instance->displayPopup();
23                 break;
24         }
25         
26         return 1;
27 }
28
29 void TrayIcon::displayPopup()
30 {
31         HMENU hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_TRAYMENU));
32         HMENU hSubMenu = GetSubMenu(hMenu, 0);
33
34         EnableMenuItem(hSubMenu, IDM_STOP,
35                 MF_BYCOMMAND | (main_dlg.m_StopButton.IsWindowEnabled() ? MF_ENABLED : MF_GRAYED));
36         
37         EnableMenuItem(hSubMenu, IDM_START,
38                 MF_BYCOMMAND | (main_dlg.m_StartButton.IsWindowEnabled() ? MF_ENABLED : MF_GRAYED));
39
40         POINT pt;
41         
42         GetCursorPos(&pt);
43         
44         SetForegroundWindow( s_hWnd );
45         
46         int cmd = TrackPopupMenu(hSubMenu,
47                 TPM_RIGHTBUTTON | TPM_RETURNCMD, 
48                 pt.x, 
49                 pt.y, 
50                 0, 
51                 s_hWnd, 
52                 NULL);
53         
54         PostMessage(s_hWnd, WM_NULL, 0, 0); 
55         
56         switch(cmd)
57         {
58         case IDM_EXIT:
59                 main_dlg.OnExitButton();
60                 break;
61                 
62         case IDM_WINDOW:
63                 main_dlg.OpenIcon();
64                 main_dlg.SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
65                 break;
66
67         case IDM_SETTINGS:
68                 main_dlg.m_TabCtrl.SetCurSel(0);
69                 main_dlg.m_TabCtrl.DisplayTabDialog();
70                 main_dlg.OpenIcon();
71                 main_dlg.SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
72                 break;
73                 
74         case IDM_OUTPUT:
75                 main_dlg.m_TabCtrl.SetCurSel(1);
76                 main_dlg.m_TabCtrl.DisplayTabDialog();
77                 main_dlg.OpenIcon();
78                 main_dlg.SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
79                 break;
80                 
81         case IDM_STOP:
82                 main_dlg.OnStopButton();
83                 break;
84                 
85         case IDM_START:
86                 main_dlg.OnStartButton();
87                 break;
88         }
89         
90         DestroyMenu(hMenu);
91 }
92
93 void TrayIcon::setStatus( status con_stat, const char* message )
94 {
95         switch( con_stat )
96         {
97         case CONNECTED:
98                 setTrayAppearance( false, IDI_ICON2, message );
99                 break;
100                 
101         case OFF:
102                 setTrayAppearance( false, IDI_ICON3, message );
103                 break;
104                 
105         case ON:
106                 setTrayAppearance( false, IDI_ICON1, message );
107                 break;
108         }
109 }
110
111 void TrayIcon::setTrayAppearance( bool start, unsigned int res_id, const char* message )
112 {
113         NOTIFYICONDATA notifyIconData;
114         
115         notifyIconData.cbSize = sizeof(notifyIconData);
116         notifyIconData.hWnd = s_hWnd;
117         
118         notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
119         notifyIconData.uCallbackMessage = 123456;
120         notifyIconData.uID = TRAYICONID; 
121         notifyIconData.hIcon = (HICON)LoadIcon( hInst, MAKEINTRESOURCE( res_id ) );  
122         strncpy( notifyIconData.szTip, message, sizeof (notifyIconData.szTip) ); 
123         
124         if( start )
125                 Shell_NotifyIcon( NIM_ADD, &notifyIconData );
126         else
127                 Shell_NotifyIcon( NIM_MODIFY, &notifyIconData );
128 }
129
130 TrayIcon::TrayIcon( CFrontendDlg& dlg, HINSTANCE hInst ) : main_dlg( dlg ), hInst( hInst )
131 {
132         instance = this;
133         WNDCLASS wndClass;
134         
135         wndClass.style = 0; 
136         wndClass.lpfnWndProc = TrayIconProc;
137         wndClass.cbClsExtra = 0;
138         wndClass.cbWndExtra = 0;
139         wndClass.hInstance = hInst;
140         wndClass.hIcon = NULL;
141         wndClass.hCursor = NULL;
142         wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
143         wndClass.lpszMenuName = NULL;
144         wndClass.lpszClassName = "OLSRdTrayIcon";
145         
146         if (0 != RegisterClass(&wndClass))
147         {
148                 s_hWnd = CreateWindow( 
149                         "OLSRdTrayIcon", 
150                         "Invisible Message Window", 
151                         WS_OVERLAPPEDWINDOW,
152                         CW_USEDEFAULT,
153                         CW_USEDEFAULT,
154                         CW_USEDEFAULT,
155                         CW_USEDEFAULT,
156                         NULL,
157                         NULL,
158                         hInst,
159                         NULL);
160                 
161                 if (s_hWnd)
162                 {
163                         ShowWindow(s_hWnd, SW_HIDE);
164                         
165                         UpdateWindow(s_hWnd);
166                 }
167         }
168         
169         setTrayAppearance( true, IDI_ICON3, "Starting..." );
170 }
171
172 TrayIcon::~TrayIcon()
173 {
174         NOTIFYICONDATA notifyIconData;
175         
176         notifyIconData.cbSize = sizeof(notifyIconData);
177         notifyIconData.hWnd = s_hWnd;
178         
179         notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
180         notifyIconData.uCallbackMessage = 123456;
181         notifyIconData.uID = TRAYICONID; 
182         notifyIconData.hIcon = NULL;
183         
184         Shell_NotifyIcon( NIM_DELETE, &notifyIconData );
185 }