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