Don't use hardcoded strings in the configuration message box. Use string resources...
[reactos.git] / reactos / base / applications / screensavers / scrnsave / scrnsave.c
1 /*
2 * Copyright 2003 J Brown
3 * Copyright 2006 Eric Kohl
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19
20 #include <windows.h>
21 #include <tchar.h>
22 #include "resource.h"
23
24 #define APPNAME _T("Scrnsave")
25
26
27 HINSTANCE hInstance;
28
29 BOOL fullscreen = FALSE;
30
31
32 LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
33 {
34 static POINT ptLast;
35 static POINT ptCursor;
36 static BOOL fFirstTime = TRUE;
37
38 switch (msg)
39 {
40 case WM_DESTROY:
41 ShowCursor(TRUE);
42 PostQuitMessage(0);
43 break;
44
45 // break out of screen-saver if any keyboard activity
46 case WM_NOTIFY:
47 case WM_SYSKEYDOWN:
48 PostMessage(hwnd, WM_CLOSE, 0, 0);
49 break;
50
51 // break out of screen-saver if any mouse activity
52 case WM_LBUTTONDOWN:
53 case WM_LBUTTONUP:
54 case WM_RBUTTONDOWN:
55 case WM_RBUTTONUP:
56 case WM_MBUTTONDOWN:
57 case WM_MBUTTONUP:
58 case WM_MOUSEMOVE:
59 // If we've got a parent then we must be a preview
60 if(GetParent(hwnd) != 0)
61 return 0;
62
63 if(fFirstTime)
64 {
65 GetCursorPos(&ptLast);
66 fFirstTime = FALSE;
67 }
68
69 GetCursorPos(&ptCursor);
70
71 // if the mouse has moved more than 3 pixels then exit
72 if(abs(ptCursor.x - ptLast.x) >= 3 || abs(ptCursor.y - ptLast.y) >= 3)
73 PostMessage(hwnd, WM_CLOSE, 0, 0);
74
75 ptLast = ptCursor;
76
77 return 0;
78 }
79
80 return DefWindowProc(hwnd, msg, wParam, lParam);
81 }
82
83 void InitSaver(HWND hwndParent)
84 {
85 WNDCLASS wc;
86 ZeroMemory(&wc, sizeof(wc));
87 wc.style = CS_HREDRAW | CS_VREDRAW;
88 wc.lpfnWndProc = WndProc;
89 wc.lpszClassName = APPNAME;
90 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
91 RegisterClass(&wc);
92
93 if (hwndParent != 0)
94 {
95 RECT rect;
96 GetClientRect(hwndParent, &rect);
97 CreateWindow(APPNAME, APPNAME,
98 WS_VISIBLE | WS_CHILD,
99 0, 0,
100 rect.right,
101 rect.bottom,
102 hwndParent, 0,
103 hInstance, NULL);
104 fullscreen = FALSE;
105 }
106 else
107 {
108 HWND hwnd;
109 hwnd = CreateWindowEx(WS_EX_TOPMOST,
110 APPNAME,
111 APPNAME,
112 WS_VISIBLE | WS_POPUP,
113 0, 0,
114 GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
115 HWND_DESKTOP, 0,
116 hInstance, NULL);
117
118 SetWindowPos(hwnd,
119 0, 0, 0, 0, 0,
120 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_SHOWWINDOW);
121
122 ShowCursor(FALSE);
123 fullscreen = TRUE;
124 }
125 }
126
127 void ParseCommandLine(PSTR szCmdLine, int *chOption, HWND *hwndParent)
128 {
129 int ch = *szCmdLine++;
130
131 if(ch == '-' || ch == '/')
132 ch = *szCmdLine++;
133
134 if(ch >= 'A' && ch <= 'Z')
135 ch += 'a' - 'A';
136
137 *chOption = ch;
138 ch = *szCmdLine++;
139
140 if(ch == ':')
141 ch = *szCmdLine++;
142
143 while(ch == ' ' || ch == '\t')
144 ch = *szCmdLine++;
145
146 if(isdigit(ch))
147 {
148 unsigned int i = atoi(szCmdLine - 1);
149 *hwndParent = (HWND)i;
150 }
151 else
152 *hwndParent = 0;
153 }
154
155 void Configure(void)
156 {
157 TCHAR szTitle[256];
158 TCHAR szText[256];
159
160 LoadString(hInstance,
161 IDS_TITLE,
162 szTitle,
163 256);
164
165 LoadString(hInstance,
166 IDS_TEXT,
167 szText,
168 256);
169
170 MessageBox(0,
171 szText,
172 szTitle,
173 MB_OK | MB_ICONWARNING);
174 }
175
176
177 int WINAPI WinMain (HINSTANCE hInst,
178 HINSTANCE hPrev,
179 LPSTR lpCmdLine,
180 int iCmdShow)
181 {
182 HWND hwndParent;
183 UINT nPreviousState;
184 int chOption;
185 MSG Message;
186
187 hInstance = hInst;
188
189 ParseCommandLine(lpCmdLine, &chOption, &hwndParent);
190
191 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);
192
193 switch (chOption)
194 {
195 case 's':
196 InitSaver(0);
197 break;
198
199 case 'p':
200 InitSaver(hwndParent);
201 break;
202
203 case 'c':
204 default:
205 Configure();
206 return 0;
207 }
208
209 while (GetMessage(&Message, 0, 0, 0))
210 DispatchMessage(&Message);
211
212 SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);
213
214 return Message.wParam;
215 }