1 |
/* 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 |
|
23 |
/* |
24 |
* ChangeLog |
25 |
* Maybe I didn't understand how to use the code, but the original code |
26 |
* always produces a window near the taskbar icon. This is not very useful |
27 |
* because we need it near the dialog. I also removed some code we don't use. |
28 |
* -ts |
29 |
*/ |
30 |
|
31 |
#include <windows.h> |
32 |
|
33 |
#define PD_WINDOWNAME "ClsTrayPop" |
34 |
#define PD_TIMER_IDENTIFIER 23 |
35 |
#define PD_TIMER_TIMEOUT 1500 |
36 |
|
37 |
#define PD_TRAYPOP_WIDTH 200 |
38 |
#define PD_TRAYPOP_HEIGHT 80 |
39 |
|
40 |
extern HINSTANCE glob_hinst; |
41 |
extern HWND glob_hwnd; |
42 |
|
43 |
|
44 |
static LPCTSTR |
45 |
store_text( LPCTSTR alert_text ) |
46 |
{ |
47 |
static LPCTSTR text; |
48 |
|
49 |
if( alert_text ) |
50 |
text = alert_text; |
51 |
return text; |
52 |
} /* store_text */ |
53 |
|
54 |
|
55 |
static void |
56 |
paint_alert (HDC *p_hdc, PAINTSTRUCT *p_ps) |
57 |
{ |
58 |
SetBkMode(*p_hdc, TRANSPARENT); |
59 |
HFONT hFont = CreateFont( |
60 |
13, // height of font (in css px-style) |
61 |
0, // average character width |
62 |
0, // angle of escapement |
63 |
0, // base-line orientation angle |
64 |
FW_DONTCARE, // font weight |
65 |
FALSE, // italic attribute option |
66 |
FALSE, // underline attribute option |
67 |
FALSE, // strikeout attribute option |
68 |
// TODO: future i18n problem, see docs |
69 |
DEFAULT_CHARSET, // character set identifier |
70 |
OUT_DEFAULT_PRECIS, // output precision |
71 |
CLIP_DEFAULT_PRECIS, // clipping precision |
72 |
DEFAULT_QUALITY, // output quality |
73 |
FF_DONTCARE, // pitch and family |
74 |
"Tahoma" // typeface name |
75 |
); |
76 |
|
77 |
HFONT hOldFont = (HFONT) SelectObject(*p_hdc, hFont); |
78 |
RECT recttext; |
79 |
recttext.top = 0; |
80 |
recttext.bottom = PD_TRAYPOP_HEIGHT - 2; |
81 |
recttext.left = 10; |
82 |
recttext.right = PD_TRAYPOP_WIDTH - 12; |
83 |
|
84 |
MoveToEx(*p_hdc, 0, 0, (LPPOINT) NULL); |
85 |
LineTo(*p_hdc, PD_TRAYPOP_WIDTH - 3, 0); |
86 |
LineTo(*p_hdc, PD_TRAYPOP_WIDTH - 3, PD_TRAYPOP_HEIGHT - 3); |
87 |
LineTo(*p_hdc, 0, PD_TRAYPOP_HEIGHT - 3); |
88 |
LineTo(*p_hdc, 0, 0); |
89 |
|
90 |
UINT dtFormat = DT_END_ELLIPSIS | DT_NOPREFIX | DT_WORDBREAK; |
91 |
|
92 |
// determine text height |
93 |
int text_height = DrawText( |
94 |
*p_hdc, // handle to DC |
95 |
store_text(NULL), // text to draw |
96 |
-1, // text length // TODO: future i18n problem, see docs |
97 |
&recttext, // formatting dimensions |
98 |
dtFormat | DT_CENTER | DT_CALCRECT |
99 |
); |
100 |
|
101 |
// vertically center text in the box |
102 |
recttext.top = PD_TRAYPOP_HEIGHT/2 - text_height/2 - 5; |
103 |
recttext.bottom = PD_TRAYPOP_HEIGHT/2 + text_height/2 + 5; |
104 |
recttext.left = 10; |
105 |
recttext.right = PD_TRAYPOP_WIDTH - 12; |
106 |
|
107 |
// change over to left-justified from centered, for longer lines |
108 |
(text_height > 30)? (dtFormat |= DT_LEFT) : (dtFormat |= DT_CENTER); |
109 |
|
110 |
DrawText (*p_hdc, |
111 |
store_text (NULL), |
112 |
-1, // text length // TODO: future i18n problem, see docs |
113 |
&recttext, // formatting dimensions |
114 |
dtFormat); |
115 |
|
116 |
hFont = (HFONT) SelectObject(*p_hdc, hOldFont); |
117 |
DeleteObject(hFont); |
118 |
} /* paint_alert */ |
119 |
|
120 |
|
121 |
static LRESULT CALLBACK |
122 |
WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
123 |
{ |
124 |
PAINTSTRUCT ps; |
125 |
HDC hdc; |
126 |
|
127 |
switch (msg) |
128 |
{ |
129 |
case WM_PAINT: |
130 |
hdc = BeginPaint (hWnd, &ps); |
131 |
paint_alert (&hdc, &ps); |
132 |
EndPaint (hWnd, &ps); |
133 |
break; |
134 |
|
135 |
case WM_LBUTTONUP: |
136 |
KillTimer (hWnd, PD_TIMER_IDENTIFIER); |
137 |
PostQuitMessage (1); |
138 |
break; |
139 |
|
140 |
case WM_DESTROY: |
141 |
KillTimer (hWnd, PD_TIMER_IDENTIFIER); |
142 |
PostQuitMessage (1); |
143 |
break; |
144 |
|
145 |
default: |
146 |
return DefWindowProc (hWnd, msg, wParam, lParam); |
147 |
} |
148 |
|
149 |
return 0; |
150 |
} /* WndProc */ |
151 |
|
152 |
|
153 |
static VOID CALLBACK |
154 |
popup_timer_proc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) |
155 |
{ |
156 |
DestroyWindow(hwnd); |
157 |
} /* popup_timer_proc */ |
158 |
|
159 |
|
160 |
static void |
161 |
init_wclass (WNDCLASS *p_wclass, HINSTANCE hInst) |
162 |
{ |
163 |
p_wclass->style = CS_HREDRAW | CS_VREDRAW; |
164 |
p_wclass->lpfnWndProc = WndProc; |
165 |
p_wclass->cbClsExtra = 0; |
166 |
p_wclass->cbWndExtra = 0; |
167 |
p_wclass->hInstance = hInst; |
168 |
p_wclass->hIcon = LoadIcon(hInst, IDI_APPLICATION); |
169 |
p_wclass->hCursor = LoadCursor(NULL, IDC_ARROW); |
170 |
p_wclass->hbrBackground = (HBRUSH) (COLOR_INFOBK + 1); |
171 |
p_wclass->lpszMenuName = NULL; |
172 |
p_wclass->lpszClassName = PD_WINDOWNAME; |
173 |
} /* init_wclass */ |
174 |
|
175 |
|
176 |
int |
177 |
show_msg (HWND hParentWnd, int millis, LPCTSTR string) |
178 |
{ |
179 |
RECT parent; |
180 |
int nWndWidth = PD_TRAYPOP_WIDTH; |
181 |
int nWndHeight = PD_TRAYPOP_HEIGHT; |
182 |
MSG msg; |
183 |
HWND hWnd; |
184 |
UINT_PTR timer; |
185 |
WNDCLASS wClass; |
186 |
|
187 |
store_text (string); |
188 |
GetWindowRect (hParentWnd, &parent); |
189 |
|
190 |
init_wclass (&wClass, glob_hinst); |
191 |
RegisterClass (&wClass); |
192 |
|
193 |
hWnd = CreateWindowEx( |
194 |
WS_EX_TOOLWINDOW | WS_EX_TOPMOST, // extended style |
195 |
PD_WINDOWNAME, // class name |
196 |
PD_WINDOWNAME, // window name |
197 |
WS_POPUP, // window style |
198 |
(parent.right + parent.left) / 2, (parent.bottom + parent.top) / 2, |
199 |
nWndWidth - 2, nWndHeight - 2, // width and height, with corrections |
200 |
NULL, // parent handle |
201 |
NULL, // menu handle |
202 |
glob_hinst, // instance handle |
203 |
NULL // other parameters |
204 |
); |
205 |
|
206 |
if (hWnd == NULL) |
207 |
return 0; |
208 |
else { |
209 |
ShowWindow(hWnd, SW_SHOW); |
210 |
UpdateWindow(hWnd); |
211 |
// ensure parent never loses focus |
212 |
EnableWindow(hParentWnd, TRUE); |
213 |
SetActiveWindow(NULL); |
214 |
} |
215 |
|
216 |
timer = SetTimer( |
217 |
hWnd, // handle to window |
218 |
PD_TIMER_IDENTIFIER, // timer identifier |
219 |
millis, // time-out value |
220 |
(TIMERPROC) popup_timer_proc // timer procedure |
221 |
); |
222 |
|
223 |
while( GetMessage(&msg, NULL, 0, 0) ) { |
224 |
TranslateMessage(&msg); |
225 |
DispatchMessage(&msg); |
226 |
} |
227 |
|
228 |
return msg.wParam; |
229 |
} /* show_alert */ |