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