Carlo Bramini (carlo DOT bramix AT libero DOT it)
[reactos.git] / rosapps / 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 memset(&cinfo, 0, sizeof(cinfo));
188 cinfo.cbSize = sizeof(cinfo);
189 GetCursorInfo(&cinfo);
190 GetIconInfo(cinfo.hCursor, &iinfo);
191
192 /* Create a memory DC compatible with client area DC.*/
193 HdcStrech = CreateCompatibleDC(desktopHdc);
194
195 /* Create a bitmap compatible with the client area DC.*/
196 HbmpStrech = CreateCompatibleBitmap(
197 desktopHdc,
198 R.right,
199 R.bottom);
200
201 /* Select our bitmap in memory DC and save the old one.*/
202 hOld = SelectObject (HdcStrech , HbmpStrech);
203
204 /* Paint the screen bitmap to our in memory DC */
205 BitBlt(
206 HdcStrech,
207 0,
208 0,
209 R.right,
210 R.bottom,
211 desktopHdc,
212 0,
213 0,
214 SRCCOPY);
215
216 /* Draw the mouse pointer in the right position */
217 DrawIcon(
218 HdcStrech ,
219 pMouse.x - iinfo.xHotspot, // - 10,
220 pMouse.y - iinfo.yHotspot, // - 10,
221 cinfo.hCursor);
222
223 Width = (R.right - R.left);
224 Height = (R.bottom - R.top);
225
226 AppWidth = (appRect.right - appRect.left);
227 AppHeight = (appRect.bottom - appRect.top);
228
229 blitAreaWidth = AppWidth / iZoom;
230 blitAreaHeight = AppHeight / iZoom;
231
232 blitAreaX = (cp.x) - (blitAreaWidth /2);
233 blitAreaY = (cp.y) - (blitAreaHeight /2);
234
235 if (blitAreaX < 0)
236 {
237 blitAreaX = 0;
238 }
239
240 if (blitAreaY < 0)
241 {
242 blitAreaY = 0;
243 }
244
245 if (blitAreaX > (Width - blitAreaWidth))
246 {
247 blitAreaX = (Width - blitAreaWidth);
248 }
249
250 if (blitAreaY > (Height - blitAreaHeight))
251 {
252 blitAreaY = (Height - blitAreaHeight);
253 }
254
255 if (bInvertColors)
256 {
257 rop = NOTSRCCOPY;
258 }
259
260 StretchBlt(
261 HdcStrech,
262 0,
263 0,
264 AppWidth,
265 AppHeight,
266 HdcStrech,
267 blitAreaX,
268 blitAreaY,
269 blitAreaWidth,
270 blitAreaHeight,
271 rop);
272
273 /* Blast the image from memory DC to client DC.*/
274 BitBlt (
275 aDc,
276 0 ,
277 0 ,
278 AppWidth ,
279 AppHeight ,
280 HdcStrech ,
281 0 ,
282 0 ,
283 SRCCOPY);
284
285 /* Cleanup.*/
286 SelectObject (HdcStrech, hOld);
287 DeleteObject (HbmpStrech);
288 DeleteDC (HdcStrech);
289 ReleaseDC(hDesktopWindow, desktopHdc);
290 }
291
292 //
293 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
294 //
295 // PURPOSE: Processes messages for the main window.
296 //
297 // WM_COMMAND - process the application menu
298 // WM_PAINT - Paint the main window
299 // WM_DESTROY - post a quit message and return
300 //
301 //
302 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
303 {
304 int wmId, wmEvent;
305
306 switch (message)
307 {
308 case WM_TIMER:
309 {
310 POINT pNewMouse;
311 POINT pNewCaret;
312 POINT pNewFocus;
313 HWND hwnd1, hwnd2, hwnd3;
314 DWORD a, b;
315 RECT controlRect;
316
317 //Get current mouse position
318 GetCursorPos (&pNewMouse);
319
320 //Get caret position
321 hwnd1 = GetForegroundWindow ();
322 a = GetWindowThreadProcessId(hwnd1, NULL);
323 b = GetCurrentThreadId();
324 AttachThreadInput (a, b, TRUE);
325 hwnd2 = GetFocus();
326
327 GetCaretPos( &pNewCaret);
328 ClientToScreen (hwnd2, (LPPOINT) &pNewCaret);
329 AttachThreadInput (a, b, FALSE);
330
331 //Get current control focus
332 hwnd3 = GetFocus ();
333 GetWindowRect (hwnd3 , &controlRect);
334 pNewFocus.x = controlRect.left;
335 pNewFocus.y = controlRect.top;
336
337 //If mouse has moved ....
338 if (((pMouse.x != pNewMouse.x) || (pMouse.y != pNewMouse.y)) && bFollowMouse)
339 {
340 //Update to new position
341 pMouse = pNewMouse;
342 cp = pNewMouse;
343 Refresh();
344 }
345 else if (((pCaret.x != pNewCaret.x) || (pCaret.y != pNewCaret.y)) && bFollowCaret)
346 {
347 //Update to new position
348 pCaret = pNewCaret;
349 cp = pNewCaret;
350 Refresh();
351 }
352 else if (((pFocus.x != pNewFocus.x) || (pFocus.y != pNewFocus.y)) && bFollowFocus)
353 {
354 //Update to new position
355 pFocus = pNewFocus;
356 cp = pNewFocus;
357 Refresh();
358 }
359 }
360 break;
361 case WM_COMMAND:
362 wmId = LOWORD(wParam);
363 wmEvent = HIWORD(wParam);
364 // Parse the menu selections:
365 switch (wmId)
366 {
367 case IDM_OPTIONS:
368 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOGOPTIONS), hWnd, (DLGPROC)OptionsProc);
369 break;
370 case IDM_ABOUT:
371 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)AboutProc);
372 break;
373 case IDM_EXIT:
374 DestroyWindow(hWnd);
375 break;
376 default:
377 return DefWindowProc(hWnd, message, wParam, lParam);
378 }
379 break;
380 case WM_PAINT:
381 {
382 PAINTSTRUCT PaintStruct;
383 HDC dc;
384 dc = BeginPaint(hWnd, &PaintStruct);
385 Draw(dc);
386 EndPaint(hWnd, &PaintStruct);
387 }
388 break;
389 case WM_ERASEBKGND:
390 //handle WM_ERASEBKGND by simply returning non-zero because we did all the drawing in WM_PAINT.
391 break;
392 case WM_DESTROY:
393 //Save settings to registry
394 SaveSettings ();
395 KillTimer (hWnd , 1);
396 PostQuitMessage(0);
397 break;
398 case WM_CREATE:
399 //Load settings from registry
400 LoadSettings ();
401
402 //Get the desktop window
403 hDesktopWindow = GetDesktopWindow();
404
405 //Set the timer
406 SetTimer (hWnd , 1, REPAINT_SPEED , NULL);
407 break;
408 default:
409 return DefWindowProc(hWnd, message, wParam, lParam);
410 }
411 return 0;
412 }
413
414 // Message handler for about box.
415 INT_PTR CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
416 {
417 UNREFERENCED_PARAMETER(lParam);
418 switch (message)
419 {
420 case WM_INITDIALOG:
421 return (INT_PTR)TRUE;
422
423 case WM_COMMAND:
424 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
425 {
426 EndDialog(hDlg, LOWORD(wParam));
427 return (INT_PTR)TRUE;
428 }
429 break;
430 }
431 return (INT_PTR)FALSE;
432 }
433
434 // Message handler for options box.
435 INT_PTR CALLBACK OptionsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
436 {
437 UNREFERENCED_PARAMETER(lParam);
438 switch (message)
439 {
440 case WM_INITDIALOG:
441 {
442 //Add the zoom items....
443 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("1"));
444 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("2"));
445 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("3"));
446 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("4"));
447 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("5"));
448 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("6"));
449 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("7"));
450 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_ADDSTRING, 0, (LPARAM)("8"));
451
452 //
453 SendDlgItemMessage(hDlg, IDC_ZOOM, CB_SETCURSEL, iZoom - 1, 0);
454
455 if (bFollowMouse)
456 SendDlgItemMessage(hDlg,IDC_FOLLOWMOUSECHECK,BM_SETCHECK , wParam ,0);
457
458 if (bFollowFocus)
459 SendDlgItemMessage(hDlg,IDC_FOLLOWKEYBOARDCHECK,BM_SETCHECK , wParam ,0);
460
461 if (bFollowCaret)
462 SendDlgItemMessage(hDlg,IDC_FOLLOWTEXTEDITINGCHECK,BM_SETCHECK , wParam ,0);
463
464 if (bInvertColors)
465 SendDlgItemMessage(hDlg,IDC_INVERTCOLORSCHECK,BM_SETCHECK , wParam ,0);
466
467 if (bStartMinimized)
468 SendDlgItemMessage(hDlg,IDC_STARTMINIMIZEDCHECK,BM_SETCHECK , wParam ,0);
469
470 if (bShowMagnifier)
471 SendDlgItemMessage(hDlg,IDC_SHOWMAGNIFIERCHECK,BM_SETCHECK , wParam ,0);
472
473 return (INT_PTR)TRUE;
474 }
475 case WM_COMMAND:
476 switch(LOWORD(wParam))
477 {
478 case IDOK:
479 case IDCANCEL:
480 EndDialog(hDlg, LOWORD(wParam));
481 return (INT_PTR)TRUE;
482
483 case IDC_BUTTON_HELP:
484 /* unimplemented */
485 MessageBox(hDlg , TEXT("Magnifier help not available yet!") , TEXT("Help") , MB_OK);
486 break;
487 case IDC_ZOOM:
488 if(HIWORD(wParam) == CBN_SELCHANGE)
489 {
490 HWND hCombo = GetDlgItem(hDlg,IDC_ZOOM);
491
492 /* Get index of current selection and the text of that selection. */
493 iZoom = SendMessage( hCombo, CB_GETCURSEL, (WPARAM) wParam, (LPARAM) lParam ) + 1;
494
495 //Update the magnigier UI
496 Refresh ();
497 }
498 break;
499 case IDC_INVERTCOLORSCHECK:
500 bInvertColors = IsDlgButtonChecked (hDlg, IDC_INVERTCOLORSCHECK);
501 Refresh ();
502 break;
503 case IDC_FOLLOWMOUSECHECK:
504 bFollowMouse = IsDlgButtonChecked (hDlg, IDC_FOLLOWMOUSECHECK);
505 break;
506 case IDC_FOLLOWKEYBOARDCHECK:
507 bFollowFocus = IsDlgButtonChecked (hDlg, IDC_FOLLOWKEYBOARDCHECK);
508 break;
509 case IDC_FOLLOWTEXTEDITINGCHECK:
510 bFollowCaret = IsDlgButtonChecked (hDlg, IDC_FOLLOWTEXTEDITINGCHECK);
511 break;
512 case IDC_STARTMINIMIZEDCHECK:
513 bStartMinimized = IsDlgButtonChecked (hDlg, IDC_STARTMINIMIZEDCHECK);
514 break;
515 case IDC_SHOWMAGNIFIER:
516 bShowMagnifier = IsDlgButtonChecked (hDlg, IDC_SHOWMAGNIFIERCHECK);
517 if (bShowMagnifier){
518 ShowWindow (hMainWnd , SW_SHOW);
519 }else{
520 ShowWindow (hMainWnd , SW_HIDE);
521 }
522 break;
523 }
524 }
525 return (INT_PTR)FALSE;
526 }
527
528 // Message handler for warning box.
529 INT_PTR CALLBACK WarningProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
530 {
531 UNREFERENCED_PARAMETER(lParam);
532
533 switch (message)
534 {
535 case WM_INITDIALOG:
536 return (INT_PTR)TRUE;
537 case WM_COMMAND:
538 switch(LOWORD(wParam))
539 {
540 case IDC_SHOWWARNINGCHECK:
541 bShowWarning = !IsDlgButtonChecked (hDlg, IDC_SHOWWARNINGCHECK);
542 break;
543 }
544 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
545 {
546 EndDialog(hDlg, LOWORD(wParam));
547 return (INT_PTR)TRUE;
548 }
549 break;
550 }
551 return (INT_PTR)FALSE;
552 }