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