1 |
twoaday |
2 |
/* wptTrayPop.cpp - Alternative status box |
2 |
|
|
* Copyright (C) 2002, 2004 Timo Schulz |
3 |
|
|
* Copyright (C) 2002 Prasenjeet Dutta. <http://www.chaoszone.org> |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
* |
21 |
|
|
*/ |
22 |
|
|
#include <windows.h> |
23 |
|
|
|
24 |
twoaday |
23 |
#include "wptW32API.h" |
25 |
|
|
|
26 |
twoaday |
34 |
#define PD_WINDOWNAME "WinPT_TrayPop" |
27 |
twoaday |
24 |
#define PD_TIMER_IDENTIFIER 23 |
28 |
|
|
#define PD_TIMER_TIMEOUT 1500 |
29 |
twoaday |
2 |
|
30 |
twoaday |
24 |
/* XXX: use the given text to figure out these params. */ |
31 |
|
|
#define PD_TRAYPOP_WIDTH 250 |
32 |
|
|
#define PD_TRAYPOP_HEIGHT 80 |
33 |
twoaday |
2 |
|
34 |
|
|
extern HINSTANCE glob_hinst; |
35 |
|
|
extern HWND glob_hwnd; |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
static LPCTSTR |
39 |
twoaday |
24 |
store_text (LPCTSTR alert_text) |
40 |
twoaday |
2 |
{ |
41 |
|
|
static LPCTSTR text; |
42 |
|
|
|
43 |
twoaday |
24 |
if (alert_text) |
44 |
twoaday |
2 |
text = alert_text; |
45 |
|
|
return text; |
46 |
twoaday |
34 |
} |
47 |
twoaday |
2 |
|
48 |
|
|
|
49 |
|
|
static void |
50 |
|
|
paint_alert (HDC *p_hdc, PAINTSTRUCT *p_ps) |
51 |
|
|
{ |
52 |
|
|
SetBkMode(*p_hdc, TRANSPARENT); |
53 |
|
|
HFONT hFont = CreateFont( |
54 |
|
|
13, // height of font (in css px-style) |
55 |
|
|
0, // average character width |
56 |
|
|
0, // angle of escapement |
57 |
|
|
0, // base-line orientation angle |
58 |
|
|
FW_DONTCARE, // font weight |
59 |
|
|
FALSE, // italic attribute option |
60 |
|
|
FALSE, // underline attribute option |
61 |
|
|
FALSE, // strikeout attribute option |
62 |
|
|
// TODO: future i18n problem, see docs |
63 |
|
|
DEFAULT_CHARSET, // character set identifier |
64 |
|
|
OUT_DEFAULT_PRECIS, // output precision |
65 |
|
|
CLIP_DEFAULT_PRECIS, // clipping precision |
66 |
|
|
DEFAULT_QUALITY, // output quality |
67 |
|
|
FF_DONTCARE, // pitch and family |
68 |
|
|
"Tahoma" // typeface name |
69 |
|
|
); |
70 |
twoaday |
34 |
|
71 |
|
|
HFONT hOldFont = (HFONT) SelectObject (*p_hdc, hFont); |
72 |
twoaday |
2 |
RECT recttext; |
73 |
|
|
recttext.top = 0; |
74 |
|
|
recttext.bottom = PD_TRAYPOP_HEIGHT - 2; |
75 |
|
|
recttext.left = 10; |
76 |
|
|
recttext.right = PD_TRAYPOP_WIDTH - 12; |
77 |
|
|
|
78 |
twoaday |
34 |
MoveToEx (*p_hdc, 0, 0, (LPPOINT) NULL); |
79 |
|
|
LineTo (*p_hdc, PD_TRAYPOP_WIDTH - 3, 0); |
80 |
|
|
LineTo (*p_hdc, PD_TRAYPOP_WIDTH - 3, PD_TRAYPOP_HEIGHT - 3); |
81 |
|
|
LineTo (*p_hdc, 0, PD_TRAYPOP_HEIGHT - 3); |
82 |
|
|
LineTo (*p_hdc, 0, 0); |
83 |
twoaday |
2 |
|
84 |
twoaday |
34 |
UINT dtFormat = DT_END_ELLIPSIS | DT_NOPREFIX | DT_WORDBREAK; |
85 |
twoaday |
2 |
|
86 |
twoaday |
34 |
/* determine text height */ |
87 |
|
|
int text_height = DrawText (*p_hdc, store_text (NULL), |
88 |
|
|
-1, &recttext, |
89 |
|
|
dtFormat | DT_CENTER | DT_CALCRECT); |
90 |
twoaday |
2 |
|
91 |
twoaday |
34 |
/* vertically center text in the box */ |
92 |
twoaday |
2 |
recttext.top = PD_TRAYPOP_HEIGHT/2 - text_height/2 - 5; |
93 |
|
|
recttext.bottom = PD_TRAYPOP_HEIGHT/2 + text_height/2 + 5; |
94 |
|
|
recttext.left = 10; |
95 |
|
|
recttext.right = PD_TRAYPOP_WIDTH - 12; |
96 |
|
|
|
97 |
twoaday |
34 |
/* change over to left-justified from centered, for longer lines */ |
98 |
twoaday |
2 |
(text_height > 30)? (dtFormat |= DT_LEFT) : (dtFormat |= DT_CENTER); |
99 |
|
|
|
100 |
twoaday |
34 |
DrawText (*p_hdc, store_text (NULL), |
101 |
|
|
-1, &recttext, dtFormat); |
102 |
twoaday |
2 |
|
103 |
|
|
hFont = (HFONT) SelectObject(*p_hdc, hOldFont); |
104 |
|
|
DeleteObject(hFont); |
105 |
twoaday |
34 |
} |
106 |
twoaday |
2 |
|
107 |
|
|
|
108 |
twoaday |
34 |
/* Window procedure for the message box. */ |
109 |
twoaday |
2 |
static LRESULT CALLBACK |
110 |
twoaday |
34 |
window_proc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
111 |
twoaday |
2 |
{ |
112 |
|
|
PAINTSTRUCT ps; |
113 |
|
|
HDC hdc; |
114 |
|
|
|
115 |
twoaday |
34 |
switch (msg) { |
116 |
twoaday |
2 |
case WM_PAINT: |
117 |
|
|
hdc = BeginPaint (hWnd, &ps); |
118 |
|
|
paint_alert (&hdc, &ps); |
119 |
|
|
EndPaint (hWnd, &ps); |
120 |
|
|
break; |
121 |
|
|
|
122 |
|
|
case WM_LBUTTONUP: |
123 |
|
|
KillTimer (hWnd, PD_TIMER_IDENTIFIER); |
124 |
|
|
PostQuitMessage (1); |
125 |
|
|
break; |
126 |
|
|
|
127 |
|
|
case WM_DESTROY: |
128 |
|
|
KillTimer (hWnd, PD_TIMER_IDENTIFIER); |
129 |
|
|
PostQuitMessage (1); |
130 |
|
|
break; |
131 |
twoaday |
34 |
|
132 |
twoaday |
2 |
default: |
133 |
|
|
return DefWindowProc (hWnd, msg, wParam, lParam); |
134 |
|
|
} |
135 |
twoaday |
34 |
|
136 |
twoaday |
2 |
return 0; |
137 |
twoaday |
34 |
} |
138 |
twoaday |
2 |
|
139 |
|
|
|
140 |
twoaday |
34 |
|
141 |
|
|
/* Simple timer function which destroys the (message) window . */ |
142 |
twoaday |
2 |
static VOID CALLBACK |
143 |
twoaday |
34 |
popup_timer_proc (HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) |
144 |
twoaday |
2 |
{ |
145 |
twoaday |
34 |
DestroyWindow (hwnd); |
146 |
|
|
} |
147 |
twoaday |
2 |
|
148 |
|
|
|
149 |
twoaday |
34 |
|
150 |
|
|
/* Init window class. */ |
151 |
twoaday |
2 |
static void |
152 |
|
|
init_wclass (WNDCLASS *p_wclass, HINSTANCE hInst) |
153 |
|
|
{ |
154 |
|
|
p_wclass->style = CS_HREDRAW | CS_VREDRAW; |
155 |
twoaday |
34 |
p_wclass->lpfnWndProc = window_proc; |
156 |
twoaday |
2 |
p_wclass->cbClsExtra = 0; |
157 |
|
|
p_wclass->cbWndExtra = 0; |
158 |
|
|
p_wclass->hInstance = hInst; |
159 |
|
|
p_wclass->hIcon = LoadIcon(hInst, IDI_APPLICATION); |
160 |
|
|
p_wclass->hCursor = LoadCursor(NULL, IDC_ARROW); |
161 |
|
|
p_wclass->hbrBackground = (HBRUSH) (COLOR_INFOBK + 1); |
162 |
|
|
p_wclass->lpszMenuName = NULL; |
163 |
|
|
p_wclass->lpszClassName = PD_WINDOWNAME; |
164 |
twoaday |
34 |
} |
165 |
twoaday |
2 |
|
166 |
|
|
|
167 |
twoaday |
23 |
/* Show the given message in a pop-up window for millis miliseconds. */ |
168 |
twoaday |
2 |
int |
169 |
|
|
show_msg (HWND hParentWnd, int millis, LPCTSTR string) |
170 |
|
|
{ |
171 |
|
|
RECT parent; |
172 |
|
|
int nWndWidth = PD_TRAYPOP_WIDTH; |
173 |
|
|
int nWndHeight = PD_TRAYPOP_HEIGHT; |
174 |
|
|
MSG msg; |
175 |
|
|
HWND hWnd; |
176 |
|
|
UINT_PTR timer; |
177 |
|
|
WNDCLASS wClass; |
178 |
|
|
|
179 |
|
|
store_text (string); |
180 |
|
|
GetWindowRect (hParentWnd, &parent); |
181 |
|
|
|
182 |
|
|
init_wclass (&wClass, glob_hinst); |
183 |
|
|
RegisterClass (&wClass); |
184 |
|
|
|
185 |
twoaday |
34 |
hWnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TOPMOST, |
186 |
|
|
PD_WINDOWNAME, PD_WINDOWNAME, |
187 |
|
|
WS_POPUP, |
188 |
|
|
(parent.right + parent.left) / 2, |
189 |
|
|
(parent.bottom + parent.top) / 2, |
190 |
|
|
nWndWidth - 2, nWndHeight - 2, |
191 |
|
|
NULL, NULL, glob_hinst, NULL); |
192 |
twoaday |
2 |
if (hWnd == NULL) |
193 |
twoaday |
24 |
return 0; |
194 |
twoaday |
34 |
else { /* Ensure parent never loses focus. */ |
195 |
twoaday |
23 |
ShowWindow (hWnd, SW_SHOW); |
196 |
|
|
UpdateWindow (hWnd); |
197 |
|
|
EnableWindow (hParentWnd, TRUE); |
198 |
|
|
center_window (hWnd, hParentWnd); |
199 |
|
|
SetActiveWindow (NULL); |
200 |
twoaday |
2 |
} |
201 |
|
|
|
202 |
twoaday |
24 |
timer = SetTimer (hWnd, PD_TIMER_IDENTIFIER, millis, |
203 |
|
|
(TIMERPROC) popup_timer_proc); |
204 |
twoaday |
2 |
|
205 |
twoaday |
23 |
while (GetMessage (&msg, NULL, 0, 0)) { |
206 |
|
|
TranslateMessage (&msg); |
207 |
|
|
DispatchMessage (&msg); |
208 |
twoaday |
2 |
} |
209 |
|
|
|
210 |
|
|
return msg.wParam; |
211 |
twoaday |
23 |
} |