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