fixed resource leak (DestroyMenu before destroying main window)
[reactos.git] / rosapps / devutils / zoomin / framewnd.c
1 /*
2 * ReactOS zoomin
3 *
4 * framewnd.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 * Copyright (C) 2005 Martin Fuchs <martin-fuchs@gmx.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
25 #include <windows.h>
26 #include <windowsx.h>
27 #include <tchar.h>
28
29 #include "main.h"
30 #include "framewnd.h"
31
32 extern void ExitInstance();
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // Global Variables:
36 //
37
38 static int s_factor = 2; // zoom factor
39
40 static POINT s_srcPos = {0, 0}; // source rectangle position
41 static RECT s_lastSrc = {-1,-1,-1,-1}; // last cursor position
42
43 BOOL s_dragging = FALSE;
44
45
46 // zoom range
47
48 #define MIN_ZOOM 1
49 #define MAX_ZOOM 16
50
51
52 ////////////////////////////////////////////////////////////////////////////////
53 //
54 // FUNCTION: SetZoom()
55 //
56 // PURPOSE: Change zoom level
57 //
58
59 static void SetZoom(HWND hWnd, int factor)
60 {
61 TCHAR buffer[MAX_LOADSTRING];
62
63 if (factor>=MIN_ZOOM && factor<=MAX_ZOOM) {
64 s_factor = factor;
65
66 SetScrollPos(hWnd, SB_VERT, s_factor, TRUE);
67
68 wsprintf(buffer, TEXT("%s %dx"), szTitle, s_factor);
69 SetWindowText(hWnd, buffer);
70 }
71 }
72
73
74 ////////////////////////////////////////////////////////////////////////////////
75 //
76 // FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
77 //
78 // PURPOSE: Processes WM_COMMAND messages for the main frame window.
79 //
80 //
81
82 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
83 {
84 switch (LOWORD(wParam)) {
85 // Parse the menu selections:
86 case ID_EDIT_EXIT:
87 DestroyWindow(hWnd);
88 break;
89
90 case ID_EDIT_COPY:
91 case ID_EDIT_REFRESH:
92 case ID_OPTIONS_REFRESH_RATE:
93 case ID_HELP_ABOUT:
94 // TODO:
95 break;
96
97 case ID_REFRESH:
98 InvalidateRect(hWnd, NULL, FALSE);
99 break;
100
101 default:
102 return FALSE;
103 }
104
105 return TRUE;
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////
109 //
110 // FUNCTION: FrameWndProc(HWND, unsigned, WORD, LONG)
111 //
112 // PURPOSE: Processes messages for the main window.
113 //
114
115 LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
116 {
117 switch (message) {
118 case WM_CREATE:
119 SetTimer(hWnd, 0, 200, NULL); // refresh display all 200 ms
120 SetScrollRange(hWnd, SB_VERT, 1, MAX_ZOOM, FALSE);
121 SetZoom(hWnd, s_factor);
122 break;
123
124 case WM_COMMAND:
125 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
126 return DefWindowProc(hWnd, message, wParam, lParam);
127 }
128 break;
129
130 case WM_PAINT: {
131 PAINTSTRUCT ps;
132 HDC hdcMem;
133 RECT clnt;
134 SIZE size;
135
136 BeginPaint(hWnd, &ps);
137 hdcMem = GetDC(GetDesktopWindow());
138
139 GetClientRect(hWnd, &clnt);
140 size.cx = (clnt.right + s_factor-1) / s_factor;
141 size.cy = (clnt.bottom + s_factor-1) / s_factor;
142
143 StretchBlt(ps.hdc, 0, 0, size.cx*s_factor, size.cy*s_factor,
144 hdcMem, s_srcPos.x, s_srcPos.y, size.cx, size.cy, SRCCOPY);
145
146 ReleaseDC(GetDesktopWindow(), hdcMem);
147 EndPaint(hWnd, &ps);
148 break;}
149
150 case WM_TIMER:
151 if (s_dragging && GetCapture()==hWnd) {
152 RECT clnt, rect;
153
154 int width = GetSystemMetrics(SM_CXSCREEN);
155 int height = GetSystemMetrics(SM_CYSCREEN);
156
157 GetCursorPos(&s_srcPos);
158
159 GetClientRect(hWnd, &clnt);
160
161 s_srcPos.x -= clnt.right / s_factor / 2;
162 s_srcPos.y -= clnt.bottom / s_factor / 2;
163
164 if (s_srcPos.x < 0)
165 s_srcPos.x = 0;
166 else if (s_srcPos.x+clnt.right/s_factor > width)
167 s_srcPos.x = width - clnt.right/s_factor;
168
169 if (s_srcPos.y < 0)
170 s_srcPos.y = 0;
171 else if (s_srcPos.y+clnt.bottom/s_factor > height)
172 s_srcPos.y = height - clnt.bottom/s_factor;
173
174 if (memcmp(&rect, &s_lastSrc, sizeof(RECT))) {
175 HDC hdc = GetDC(0);
176
177 if (s_lastSrc.bottom != -1)
178 DrawFocusRect(hdc, &s_lastSrc);
179
180 rect.left = s_srcPos.x - 1;
181 rect.top = s_srcPos.y - 1;
182 rect.right = rect.left + clnt.right/s_factor + 2;
183 rect.bottom = rect.top + clnt.bottom/s_factor + 2;
184 DrawFocusRect(hdc, &rect);
185
186 ReleaseDC(0, hdc);
187
188 s_lastSrc = rect;
189 }
190 }
191
192 InvalidateRect(hWnd, NULL, FALSE);
193 UpdateWindow(hWnd);
194 break;
195
196 case WM_LBUTTONDOWN:
197 s_dragging = TRUE;
198 SetCapture(hWnd);
199 break;
200
201 case WM_LBUTTONUP:
202 case WM_CANCELMODE:
203 if (s_dragging) {
204 HDC hdc = GetDC(0);
205 DrawFocusRect(hdc, &s_lastSrc);
206 ReleaseDC(0, hdc);
207
208 s_lastSrc.bottom = -1;
209
210 s_dragging = FALSE;
211 ReleaseCapture();
212 }
213 break;
214
215 case WM_VSCROLL:
216 switch(wParam) {
217 case SB_LINEUP:
218 case SB_PAGEUP:
219 SetZoom(hWnd, s_factor-1);
220 break;
221
222 case SB_LINEDOWN:
223 case SB_PAGEDOWN:
224 SetZoom(hWnd, s_factor+1);
225 break;
226 }
227 break;
228
229 case WM_DESTROY:
230 KillTimer(hWnd, 0);
231 PostQuitMessage(0);
232 ExitInstance();
233 break;
234
235 default:
236 return DefWindowProc(hWnd, message, wParam, lParam);
237 }
238
239 return 0;
240 }