sync with trunk r49322
[reactos.git] / base / applications / magnify / magnifier.c
1 /*
2 * PROJECT: ReactOS Magnify
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/magnify/magnifier.c
5 * PURPOSE:
6 * COPYRIGHT: Copyright 2007 Marc Piulachs <marc.piulachs@codexchange.net>
7 *
8 */
9
10 #include <windows.h>
11 #include <shellapi.h>
12 #include "magnifier.h"
13 #include "resource.h"
14
15 const TCHAR szWindowClass[] = TEXT("MAGNIFIER");
16
17 #define MAX_LOADSTRING 100
18
19 // Global Variables:
20 HINSTANCE hInst; // current instance
21 HWND hMainWnd;
22
23 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
24
25 #define REPAINT_SPEED 100
26
27 HWND hDesktopWindow = NULL;
28
29 //Current magnified area
30 POINT cp;
31
32 //Last positions
33 POINT pMouse;
34 POINT pCaret;
35 POINT pFocus;
36
37 // Forward declarations of functions included in this code module:
38 ATOM MyRegisterClass(HINSTANCE hInstance);
39 BOOL InitInstance(HINSTANCE, int);
40 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
41 INT_PTR CALLBACK AboutProc(HWND, UINT, WPARAM, LPARAM);
42 INT_PTR CALLBACK OptionsProc(HWND, UINT, WPARAM, LPARAM);
43 INT_PTR CALLBACK WarningProc(HWND, UINT, WPARAM, LPARAM);
44
45 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
46 {
47 // TODO: Place code here.
48 MSG msg;
49 HACCEL hAccelTable;
50
51 UNREFERENCED_PARAMETER(hPrevInstance);
52 UNREFERENCED_PARAMETER(lpCmdLine);
53
54 // Initialize global strings
55 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
56 MyRegisterClass(hInstance);
57
58 // Perform application initialization:
59 if (!InitInstance (hInstance, nCmdShow))
60 {
61 return FALSE;
62 }
63
64 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MAGNIFIER));
65
66 // Main message loop:
67 while (GetMessage(&msg, NULL, 0, 0))
68 {
69 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
70 {
71 TranslateMessage(&msg);
72 DispatchMessage(&msg);
73 }
74 }
75
76 return (int) msg.wParam;
77 }
78
79
80
81 //
82 // FUNCTION: MyRegisterClass()
83 //
84 // PURPOSE: Registers the window class.
85 //
86 // COMMENTS:
87 //
88 // This function and its usage are only necessary if you want this code
89 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
90 // function that was added to Windows 95. It is important to call this function
91 // so that the application will get 'well formed' small icons associated
92 // with it.
93 //
94 ATOM MyRegisterClass(HINSTANCE hInstance)
95 {
96 WNDCLASS wc;
97
98 wc.style = CS_HREDRAW | CS_VREDRAW;
99 wc.lpfnWndProc = WndProc;
100 wc.cbClsExtra = 0;
101 wc.cbWndExtra = 0;
102 wc.hInstance = hInstance;
103 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
104 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
105 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
106 wc.lpszMenuName = MAKEINTRESOURCE(IDC_MAGNIFIER);
107 wc.lpszClassName = szWindowClass;
108
109 return RegisterClass(&wc);
110 }
111
112 //
113 // FUNCTION: InitInstance(HINSTANCE, int)
114 //
115 // PURPOSE: Saves instance handle and creates main window
116 //
117 // COMMENTS:
118 //
119 // In this function, we save the instance handle in a global variable and
120 // create and display the main program window.
121 //
122 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
123 {
124 hInst = hInstance; // Store instance handle in our global variable
125
126 // Create the Window
127 hMainWnd = CreateWindowEx(
128 WS_EX_TOPMOST,
129 szWindowClass,
130 szTitle,
131 WS_OVERLAPPEDWINDOW,
132 CW_USEDEFAULT,
133 CW_USEDEFAULT,
134 CW_USEDEFAULT,
135 CW_USEDEFAULT,
136 NULL,
137 NULL,
138 hInstance,
139 NULL);
140
141 if (!hMainWnd)
142 {
143 return FALSE;
144 }
145
146 ShowWindow(hMainWnd, (bStartMinimized) ? SW_MINIMIZE : nCmdShow);
147 UpdateWindow(hMainWnd);
148
149 if (bShowWarning)
150 {
151 DialogBox (hInstance, MAKEINTRESOURCE(IDD_WARNINGDIALOG), hMainWnd, (DLGPROC)WarningProc);
152 }
153
154 return TRUE;
155 }
156
157 void Refresh ()
158 {
159 if (!IsIconic(hMainWnd))
160 {
161 // Invalidate the client area forcing a WM_PAINT message
162 InvalidateRgn(hMainWnd, NULL, TRUE);
163 }
164 }
165
166 void Draw(HDC aDc)
167 {
168 HDC desktopHdc = NULL;
169 HDC HdcStrech;
170 HANDLE hOld;
171 HBITMAP HbmpStrech;
172
173 RECT R;
174 RECT appRect;
175 DWORD rop = SRCCOPY;
176 CURSORINFO cinfo;
177 ICONINFO iinfo;
178
179 int Width, Height, AppWidth, AppHeight;
180 LONG blitAreaWidth, blitAreaHeight, blitAreaX, blitAreaY;
181
182 desktopHdc = GetWindowDC (hDesktopWindow);
183
184 GetClientRect(hMainWnd, &appRect);
185 GetWindowRect(hDesktopWindow, &R);
186
187 ZeroMemory(&cinfo, sizeof(CURSORINFO));
188 ZeroMemory(&iinfo, sizeof(ICONINFO));
189 cinfo.cbSize = sizeof(cinfo);
190 GetCursorInfo(&cinfo);
191 GetIconInfo(cinfo.hCursor, &iinfo);
192
193 /* Create a memory DC compatible with client area DC.*/
194 HdcStrech = CreateCompatibleDC(desktopHdc);
195
196 /* Create a bitmap compatible with the client area DC.*/
197 HbmpStrech = CreateCompatibleBitmap(
198 desktopHdc,
199 R.right,
200 R.bottom);
201
202 /* Select our bitmap in memory DC and save the old one.*/
203 hOld = SelectObject (HdcStrech , HbmpStrech);
204
205 /* Paint the screen bitmap to our in memory DC */
206 BitBlt(
207 HdcStrech,
208 0,
209 0,
210 R.right,
211 R.bottom,
212 desktopHdc,
213 0,
214 0,
215 SRCCOPY);
216
217 /* Draw the mouse pointer in the right position */
218 DrawIcon(
219 HdcStrech ,
220 pMouse.x - iinfo.xHotspot, // - 10,
221 pMouse.y - iinfo.yHotspot, // - 10,
222 cinfo.hCursor);
223
224 Width = (R.right - R.left);
225 Height = (R.bottom - R.top);
226
227 AppWidth = (appRect.right - appRect.left);
228 AppHeight = (appRect.bottom - appRect.top);
229
230 blitAreaWidth = AppWidth / iZoom;
231 blitAreaHeight = AppHeight / iZoom;
232
233 blitAreaX = (cp.x) - (blitAreaWidth /2);
234 blitAreaY = (cp.y) - (blitAreaHeight /2);
235
236 if (blitAreaX < 0)
237 {
238 blitAreaX = 0;
239 }
240
241 if (blitAreaY < 0)
242 {
243 blitAreaY = 0;
244 }
245
246 if (blitAreaX > (Width - blitAreaWidth))
247 {
248 blitAreaX = (Width - blitAreaWidth);
249 }
250
251 if (blitAreaY > (Height - blitAreaHeight))
252 {
253 blitAreaY = (Height - blitAreaHeight);
254 }
255
256 if (bInvertColors)
257 {
258 rop = NOTSRCCOPY;
259 }
260
261 /* Blast the stretched image from memory DC to window DC.*/
262 StretchBlt(
263 aDc,
264 0,
265 0,
266 AppWidth,
267 AppHeight,
268 HdcStrech,
269 blitAreaX,
270 blitAreaY,
271 blitAreaWidth,
272 blitAreaHeight,
273 rop);
274
275 /* Cleanup.*/
276 if (iinfo.hbmMask)
277 DeleteObject(iinfo.hbmMask);
278 if (iinfo.hbmColor)
279 DeleteObject(iinfo.hbmColor);
280 SelectObject (HdcStrech, hOld);
281 DeleteObject (HbmpStrech);
282 DeleteDC (HdcStrech);
283 ReleaseDC(hDesktopWindow, desktopHdc);
284 }
285
286 //
287 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
288 //
289 // PURPOSE: Processes messages for the main window.
290 //
291 // WM_COMMAND - process the application menu
292 // WM_PAINT - Paint the main window
293 // WM_DESTROY - post a quit message and return
294 //
295 //
296 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
297 {
298 int wmId;
299
300 switch (message)
301 {
302 case WM_TIMER:
303 {
304 POINT pNewMouse;
305 POINT pNewCaret;
306 POINT pNewFocus;
307 HWND hwnd1, hwnd2, hwnd3;
308 DWORD a, b;
309 RECT controlRect;
310
311 //Get current mouse position
312 GetCursorPos (&pNewMouse);
313
314 //Get caret position
315 hwnd1 = GetForegroundWindow ();
316 a = GetWindowThreadProcessId(hwnd1, NULL);
317 b = GetCurrentThreadId();
318 AttachThreadInput (a, b, TRUE);
319 hwnd2 = GetFocus();
320
321 GetCaretPos( &pNewCaret);
322 ClientToScreen (hwnd2, (LPPOINT) &pNewCaret);
323 AttachThreadInput (a, b, FALSE);
324
325 //Get current control focus
326 hwnd3 = GetFocus ();
327 GetWindowRect (hwnd3 , &controlRect);
328 pNewFocus.x = controlRect.left;
329 pNewFocus.y = controlRect.top;
330
331 //If mouse has moved ....
332 if (((pMouse.x != pNewMouse.x) || (pMouse.y != pNewMouse.y)) && bFollowMouse)
333 {
334 //Update to new position
335 pMouse = pNewMouse;
336 cp = pNewMouse;
337 Refresh();
338 }
339 else if (((pCaret.x != pNewCaret.x) || (pCaret.y != pNewCaret.y)) && bFollowCaret)
340 {
341 //Update to new position
342 pCaret = pNewCaret;
343 cp = pNewCaret;
344 Refresh();
345 }
346 else if (((pFocus.x != pNewFocus.x) || (pFocus.y != pNewFocus.y)) && bFollowFocus)
347 {
348 //Update to new position
349 pFocus = pNewFocus;
350 cp = pNewFocus;
351 Refresh();
352 }
353 }
354 break;
355 case WM_COMMAND:
356 wmId = LOWORD(wParam);
357 // Parse the menu selections:
358 switch (wmId)
359 {
360 case IDM_OPTIONS:
361 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOGOPTIONS), hWnd, (DLGPROC)OptionsProc);
362 break;
363 case IDM_ABOUT:
364 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)AboutProc);
365 break;
366 case IDM_EXIT:
367 DestroyWindow(hWnd);
368 break;
369 default:
370 return DefWindowProc(hWnd, message, wParam, lParam);
371 }
372 break;
373 case WM_PAINT:
374 {
375 PAINTSTRUCT PaintStruct;
376 HDC dc;
377 dc = BeginPaint(hWnd, &PaintStruct);
378 Draw(dc);
379 EndPaint(hWnd, &PaintStruct);
380 }
381 break;
382 case WM_ERASEBKGND:
383 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
384 break;
385 case WM_DESTROY:
386 //Save settings to registry
387 SaveSettings ();
388 KillTimer (hWnd , 1);
389 PostQuitMessage(0);
390 break;
391 case WM_CREATE:
392 //Load settings from registry
393 LoadSettings ();
394
395 //Get the desktop window
396 hDesktopWindow = GetDesktopWindow();
397
398 //Set the timer
399 SetTimer (hWnd , 1, REPAINT_SPEED , NULL);
400 break;
401 default:
402 return DefWindowProc(hWnd, message, wParam, lParam);
403 }
404 return 0;
405 }
406
407 // Message handler for about box.
408 INT_PTR CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
409 {
410 UNREFERENCED_PARAMETER(lParam);
411 switch (message)
412 {
413 case WM_INITDIALOG:
414 return (INT_PTR)TRUE;
415
416 case WM_COMMAND:
417 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
418 {
419 EndDialog(hDlg, LOWORD(wParam));
420 return (INT_PTR)TRUE;
421 }
422 break;
423 }
424 return (INT_PTR)FALSE;
425 }
426
427 // Message handler for options box.
428 INT_PTR CALLBACK OptionsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
429 {
430 UNREFERENCED_PARAMETER(lParam);
431 switch (message)
432 {
433 case WM_INITDIALOG:
434 {
435 //Add the zoom items....
436 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("1"));
437 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("2"));
438 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("3"));
439 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("4"));
440 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("5"));
441 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("6"));
442 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("7"));
443 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("8"));
444
445 //
446 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_SETCURSEL, iZoom - 1, 0);
447
448 if (bFollowMouse)
449 SendDlgItemMessage(hDlg,IDC_FOLLOWMOUSECHECK,BM_SETCHECK , wParam ,0);
450
451 if (bFollowFocus)
452 SendDlgItemMessage(hDlg,IDC_FOLLOWKEYBOARDCHECK,BM_SETCHECK , wParam ,0);
453
454 if (bFollowCaret)
455 SendDlgItemMessage(hDlg,IDC_FOLLOWTEXTEDITINGCHECK,BM_SETCHECK , wParam ,0);
456
457 if (bInvertColors)
458 SendDlgItemMessage(hDlg,IDC_INVERTCOLORSCHECK,BM_SETCHECK , wParam ,0);
459
460 if (bStartMinimized)
461 SendDlgItemMessage(hDlg,IDC_STARTMINIMIZEDCHECK,BM_SETCHECK , wParam ,0);
462
463 if (bShowMagnifier)
464 SendDlgItemMessage(hDlg,IDC_SHOWMAGNIFIERCHECK,BM_SETCHECK , wParam ,0);
465
466 return (INT_PTR)TRUE;
467 }
468 case WM_COMMAND:
469 switch(LOWORD(wParam))
470 {
471 case IDOK:
472 case IDCANCEL:
473 EndDialog(hDlg, LOWORD(wParam));
474 return (INT_PTR)TRUE;
475
476 case IDC_BUTTON_HELP:
477 /* unimplemented */
478 MessageBox(hDlg , TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK);
479 break;
480 case IDC_ZOOM:
481 if(HIWORD(wParam) == CBN_SELCHANGE)
482 {
483 HWND hCombo = GetDlgItem(hDlg,IDC_ZOOM);
484
485 /* Get index of current selection and the text of that selection. */
486 iZoom = SendMessage( hCombo, CB_GETCURSEL, (WPARAM) wParam, (LPARAM) lParam ) + 1;
487
488 //Update the magnigier UI
489 Refresh ();
490 }
491 break;
492 case IDC_INVERTCOLORSCHECK:
493 bInvertColors = IsDlgButtonChecked (hDlg, IDC_INVERTCOLORSCHECK);
494 Refresh ();
495 break;
496 case IDC_FOLLOWMOUSECHECK:
497 bFollowMouse = IsDlgButtonChecked (hDlg, IDC_FOLLOWMOUSECHECK);
498 break;
499 case IDC_FOLLOWKEYBOARDCHECK:
500 bFollowFocus = IsDlgButtonChecked (hDlg, IDC_FOLLOWKEYBOARDCHECK);
501 break;
502 case IDC_FOLLOWTEXTEDITINGCHECK:
503 bFollowCaret = IsDlgButtonChecked (hDlg, IDC_FOLLOWTEXTEDITINGCHECK);
504 break;
505 case IDC_STARTMINIMIZEDCHECK:
506 bStartMinimized = IsDlgButtonChecked (hDlg, IDC_STARTMINIMIZEDCHECK);
507 break;
508 case IDC_SHOWMAGNIFIER:
509 bShowMagnifier = IsDlgButtonChecked (hDlg, IDC_SHOWMAGNIFIERCHECK);
510 if (bShowMagnifier){
511 ShowWindow (hMainWnd , SW_SHOW);
512 }else{
513 ShowWindow (hMainWnd , SW_HIDE);
514 }
515 break;
516 }
517 }
518 return (INT_PTR)FALSE;
519 }
520
521 // Message handler for warning box.
522 INT_PTR CALLBACK WarningProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
523 {
524 UNREFERENCED_PARAMETER(lParam);
525
526 switch (message)
527 {
528 case WM_INITDIALOG:
529 return (INT_PTR)TRUE;
530 case WM_COMMAND:
531 switch(LOWORD(wParam))
532 {
533 case IDC_SHOWWARNINGCHECK:
534 bShowWarning = !IsDlgButtonChecked (hDlg, IDC_SHOWWARNINGCHECK);
535 break;
536 }
537 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
538 {
539 EndDialog(hDlg, LOWORD(wParam));
540 return (INT_PTR)TRUE;
541 }
542 break;
543 }
544 return (INT_PTR)FALSE;
545 }