blob: 4a316ad5e18ee0570db34658b607f26a43312ed3 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <windows.h>
20#include <commctrl.h>
21#include <rfb/Configuration.h>
22#include <rfb/LogWriter.h>
23#include <rfb_win32/WMShatter.h>
24#include <rfb_win32/LowLevelKeyEvents.h>
25#include <rfb_win32/MonitorInfo.h>
26#include <rfb_win32/DeviceContext.h>
27#include <rfb_win32/Win32Util.h>
george82fd334ad2006-05-29 14:05:20 +000028#include <rfb_win32/MsgBox.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000029#include <vncviewer/DesktopWindow.h>
30#include <vncviewer/resource.h>
31
32using namespace rfb;
33using namespace rfb::win32;
34
35
36// - Statics & consts
37
38static LogWriter vlog("DesktopWindow");
39
40const int TIMER_BUMPSCROLL = 1;
41const int TIMER_POINTER_INTERVAL = 2;
42const int TIMER_POINTER_3BUTTON = 3;
43
44
45//
46// -=- DesktopWindowClass
47
48//
49// Window class used as the basis for all DesktopWindow instances
50//
51
52class DesktopWindowClass {
53public:
54 DesktopWindowClass();
55 ~DesktopWindowClass();
56 ATOM classAtom;
57 HINSTANCE instance;
58};
59
60LRESULT CALLBACK DesktopWindowProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
61 LRESULT result;
62 if (msg == WM_CREATE)
63 SetWindowLong(wnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
64 else if (msg == WM_DESTROY)
65 SetWindowLong(wnd, GWL_USERDATA, 0);
66 DesktopWindow* _this = (DesktopWindow*) GetWindowLong(wnd, GWL_USERDATA);
67 if (!_this) {
68 vlog.info("null _this in %x, message %u", wnd, msg);
69 return rfb::win32::SafeDefWindowProc(wnd, msg, wParam, lParam);
70 }
71
72 try {
73 result = _this->processMessage(msg, wParam, lParam);
george82fd334ad2006-05-29 14:05:20 +000074 } catch (rfb::UnsupportedPixelFormatException &e) {
75 MsgBox(0, e.str(), MB_OK);
76 _this->getCallback()->closeWindow();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000077 } catch (rdr::Exception& e) {
78 vlog.error("untrapped: %s", e.str());
79 }
80
81 return result;
82};
83
84static HCURSOR dotCursor = (HCURSOR)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDC_DOT_CURSOR), IMAGE_CURSOR, 0, 0, LR_SHARED);
85static HCURSOR arrowCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
86
87DesktopWindowClass::DesktopWindowClass() : classAtom(0) {
88 WNDCLASS wndClass;
89 wndClass.style = 0;
90 wndClass.lpfnWndProc = DesktopWindowProc;
91 wndClass.cbClsExtra = 0;
92 wndClass.cbWndExtra = 0;
93 wndClass.hInstance = instance = GetModuleHandle(0);
94 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
95 if (!wndClass.hIcon)
96 printf("unable to load icon:%ld", GetLastError());
97 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
98 wndClass.hbrBackground = NULL;
99 wndClass.lpszMenuName = 0;
100 wndClass.lpszClassName = _T("rfb::win32::DesktopWindowClass");
101 classAtom = RegisterClass(&wndClass);
102 if (!classAtom) {
103 throw rdr::SystemException("unable to register DesktopWindow window class", GetLastError());
104 }
105}
106
107DesktopWindowClass::~DesktopWindowClass() {
108 if (classAtom) {
109 UnregisterClass((const TCHAR*)classAtom, instance);
110 }
111}
112
113DesktopWindowClass baseClass;
114
115//
116// -=- FrameClass
117
118//
119// Window class used for child windows that display pixel data
120//
121
122class FrameClass {
123public:
124 FrameClass();
125 ~FrameClass();
126 ATOM classAtom;
127 HINSTANCE instance;
128};
129
130LRESULT CALLBACK FrameProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
131 LRESULT result;
132 if (msg == WM_CREATE)
133 SetWindowLong(wnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
134 else if (msg == WM_DESTROY)
135 SetWindowLong(wnd, GWL_USERDATA, 0);
136 DesktopWindow* _this = (DesktopWindow*) GetWindowLong(wnd, GWL_USERDATA);
137 if (!_this) {
138 vlog.info("null _this in %x, message %u", wnd, msg);
139 return rfb::win32::SafeDefWindowProc(wnd, msg, wParam, lParam);
140 }
141
142 try {
143 result = _this->processFrameMessage(msg, wParam, lParam);
144 } catch (rdr::Exception& e) {
145 vlog.error("untrapped: %s", e.str());
146 }
147
148 return result;
149}
150
151FrameClass::FrameClass() : classAtom(0) {
152 WNDCLASS wndClass;
153 wndClass.style = 0;
154 wndClass.lpfnWndProc = FrameProc;
155 wndClass.cbClsExtra = 0;
156 wndClass.cbWndExtra = 0;
157 wndClass.hInstance = instance = GetModuleHandle(0);
158 wndClass.hIcon = 0;
159 wndClass.hCursor = NULL;
160 wndClass.hbrBackground = NULL;
161 wndClass.lpszMenuName = 0;
162 wndClass.lpszClassName = _T("rfb::win32::FrameClass");
163 classAtom = RegisterClass(&wndClass);
164 if (!classAtom) {
165 throw rdr::SystemException("unable to register Frame window class", GetLastError());
166 }
167}
168
169FrameClass::~FrameClass() {
170 if (classAtom) {
171 UnregisterClass((const TCHAR*)classAtom, instance);
172 }
173}
174
175FrameClass frameClass;
176
177
178//
179// -=- DesktopWindow instance implementation
180//
181
182DesktopWindow::DesktopWindow(Callback* cb)
183 : buffer(0),
george82ffc14a62006-09-05 06:51:41 +0000184 showToolbar(false), autoScaling(false),
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000185 client_size(0, 0, 16, 16), window_size(0, 0, 32, 32),
186 cursorVisible(false), cursorAvailable(false), cursorInBuffer(false),
187 systemCursorVisible(true), trackingMouseLeave(false),
188 handle(0), frameHandle(0), has_focus(false), palette_changed(false),
189 fullscreenActive(false), fullscreenRestore(false),
190 bumpScroll(false), callback(cb) {
191
192 // Create the window
193 const char* name = "DesktopWindow";
194 handle = CreateWindow((const TCHAR*)baseClass.classAtom, TStr(name),
195 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
196 0, 0, 10, 10, 0, 0, baseClass.instance, this);
197 if (!handle)
198 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
199 vlog.debug("created window \"%s\" (%x)", name, handle);
200
201 // Create the toolbar
202 tb.create(handle);
203 vlog.debug("created toolbar window \"%s\" (%x)", "ViewerToolBar", tb.getHandle());
204
205 // Create the frame window
206 frameHandle = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
207 0, WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
208 CW_USEDEFAULT, CW_USEDEFAULT, handle, 0, frameClass.instance, this);
209 if (!frameHandle) {
210 throw rdr::SystemException("unable to create rfb frame window instance", GetLastError());
211 }
212 vlog.debug("created window \"%s\" (%x)", "Frame Window", frameHandle);
213
214 // Initialise the CPointer pointer handler
215 ptr.setHWND(frameHandle);
216 ptr.setIntervalTimerId(TIMER_POINTER_INTERVAL);
217 ptr.set3ButtonTimerId(TIMER_POINTER_3BUTTON);
218
219 // Initialise the bumpscroll timer
220 bumpScrollTimer.setHWND(handle);
221 bumpScrollTimer.setId(TIMER_BUMPSCROLL);
222
223 // Hook the clipboard
224 clipboard.setNotifier(this);
225
226 // Create the backing buffer
227 buffer = new win32::ScaledDIBSectionBuffer(frameHandle);
228
229 // Show the window
230 centerWindow(handle, 0);
231 ShowWindow(handle, SW_SHOW);
232}
233
234DesktopWindow::~DesktopWindow() {
235 vlog.debug("~DesktopWindow");
236 showSystemCursor();
237 if (handle) {
238 disableLowLevelKeyEvents(handle);
239 DestroyWindow(handle);
240 handle = 0;
241 }
242 delete buffer;
243 vlog.debug("~DesktopWindow done");
244}
245
246
247void DesktopWindow::setFullscreen(bool fs) {
248 if (fs && !fullscreenActive) {
249 fullscreenActive = bumpScroll = true;
250
251 // Un-minimize the window if required
252 if (GetWindowLong(handle, GWL_STYLE) & WS_MINIMIZE)
253 ShowWindow(handle, SW_RESTORE);
254
255 // Save the current window position
256 GetWindowRect(handle, &fullscreenOldRect);
257
258 // Find the size of the display the window is on
259 MonitorInfo mi(handle);
260
261 // Hide the toolbar
262 if (tb.isVisible())
263 tb.hide();
264 SetWindowLong(frameHandle, GWL_EXSTYLE, 0);
265
266 // Set the window full-screen
267 DWORD flags = GetWindowLong(handle, GWL_STYLE);
268 fullscreenOldFlags = flags;
269 flags = flags & ~(WS_CAPTION | WS_THICKFRAME | WS_MAXIMIZE | WS_MINIMIZE);
270 vlog.debug("flags=%x", flags);
271
272 SetWindowLong(handle, GWL_STYLE, flags);
george82ced9db12006-09-11 09:09:14 +0000273 SetWindowPos(handle, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000274 mi.rcMonitor.right-mi.rcMonitor.left,
275 mi.rcMonitor.bottom-mi.rcMonitor.top,
276 SWP_FRAMECHANGED);
277 } else if (!fs && fullscreenActive) {
278 fullscreenActive = bumpScroll = false;
279
280 // Show the toolbar
281 if (showToolbar)
282 tb.show();
283 SetWindowLong(frameHandle, GWL_EXSTYLE, WS_EX_CLIENTEDGE);
284
285 // Set the window non-fullscreen
286 SetWindowLong(handle, GWL_STYLE, fullscreenOldFlags);
287
288 // Set the window position
289 SetWindowPos(handle, HWND_NOTOPMOST,
290 fullscreenOldRect.left, fullscreenOldRect.top,
291 fullscreenOldRect.right - fullscreenOldRect.left,
292 fullscreenOldRect.bottom - fullscreenOldRect.top,
293 SWP_FRAMECHANGED);
294 }
295
296 // Adjust the viewport offset to cope with change in size between FS
297 // and previous window state.
298 setViewportOffset(scrolloffset);
299}
300
301void DesktopWindow::setShowToolbar(bool st)
302{
303 showToolbar = st;
george82be3e9692006-06-10 12:58:41 +0000304 if (fullscreenActive) return;
305
george8222856792006-06-10 11:47:22 +0000306 RECT r;
307 GetWindowRect(handle, &r);
308 bool maximized = GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000309
george8222856792006-06-10 11:47:22 +0000310 if (showToolbar && !tb.isVisible()) {
george8274ea5f32006-09-11 11:40:12 +0000311 refreshToolbarButtons();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000312 tb.show();
george8222856792006-06-10 11:47:22 +0000313 if (!maximized) r.bottom += tb.getHeight();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000314 } else if (!showToolbar && tb.isVisible()) {
315 tb.hide();
george8222856792006-06-10 11:47:22 +0000316 if (!maximized) r.bottom -= tb.getHeight();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000317 }
george8222856792006-06-10 11:47:22 +0000318 // Resize the chiled windows even if the parent window size
319 // has not been changed (the main window is maximized)
320 if (maximized) SendMessage(handle, WM_SIZE, 0, 0);
321 else SetWindowPos(handle, NULL, 0, 0, r.right-r.left, r.bottom-r.top, SWP_NOMOVE | SWP_NOZORDER);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000322}
323
george8274ea5f32006-09-11 11:40:12 +0000324void DesktopWindow::refreshToolbarButtons() {
325 int scale = getDesktopScale();
326 if (scale == 100) tb.enableButton(ID_ACTUAL_SIZE, false);
327 else tb.enableButton(ID_ACTUAL_SIZE, true);
328 if (scale <= 10) {
329 tb.enableButton(ID_ZOOM_IN, true);
330 tb.enableButton(ID_ZOOM_OUT, false);
331 } else if (scale >= 200) {
332 tb.enableButton(ID_ZOOM_IN, false);
333 tb.enableButton(ID_ZOOM_OUT, true);
334 } else {
335 tb.enableButton(ID_ZOOM_IN, true);
336 tb.enableButton(ID_ZOOM_OUT, true);
337 }
338 if (isAutoScaling()) tb.pressButton(ID_AUTO_SIZE, true);
339 else tb.pressButton(ID_AUTO_SIZE, false);
340}
341
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000342void DesktopWindow::setDisableWinKeys(bool dwk) {
343 // Enable low-level event hooking, so we get special keys directly
344 if (dwk)
345 enableLowLevelKeyEvents(handle);
346 else
347 disableLowLevelKeyEvents(handle);
348}
349
350
351void DesktopWindow::setMonitor(const char* monitor) {
352 MonitorInfo mi(monitor);
353 mi.moveTo(handle);
354}
355
356char* DesktopWindow::getMonitor() const {
357 MonitorInfo mi(handle);
358 return strDup(mi.szDevice);
359}
360
361
362bool DesktopWindow::setViewportOffset(const Point& tl) {
363 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
364 max(0, min(tl.y, buffer->height()-client_size.height())));
365 Point delta = np.translate(scrolloffset.negate());
366 if (!np.equals(scrolloffset)) {
367 scrolloffset = np;
368 ScrollWindowEx(frameHandle, -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
369 UpdateWindow(frameHandle);
370 return true;
371 }
372 return false;
373}
374
375
376bool DesktopWindow::processBumpScroll(const Point& pos)
377{
378 if (!bumpScroll) return false;
379 int bumpScrollPixels = 20;
380 bumpScrollDelta = Point();
381
382 if (pos.x == client_size.width()-1)
383 bumpScrollDelta.x = bumpScrollPixels;
384 else if (pos.x == 0)
385 bumpScrollDelta.x = -bumpScrollPixels;
386 if (pos.y == client_size.height()-1)
387 bumpScrollDelta.y = bumpScrollPixels;
388 else if (pos.y == 0)
389 bumpScrollDelta.y = -bumpScrollPixels;
390
391 if (bumpScrollDelta.x || bumpScrollDelta.y) {
392 if (bumpScrollTimer.isActive()) return true;
393 if (setViewportOffset(scrolloffset.translate(bumpScrollDelta))) {
394 bumpScrollTimer.start(25);
395 return true;
396 }
397 }
398
399 bumpScrollTimer.stop();
400 return false;
401}
402
403
404LRESULT
405DesktopWindow::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
406 switch (msg) {
407
408 // -=- Process standard window messages
409
410 case WM_NOTIFY:
411 if (wParam == ID_TOOLBAR)
412 tb.processWM_NOTIFY(wParam, lParam);
413 break;
414
415 case WM_DISPLAYCHANGE:
416 // Display format has changed - notify callback
417 callback->displayChanged();
418 break;
419
420 // -=- Window position
421
422 // Prevent the window from being resized to be too large if in normal mode.
423 // If maximized or fullscreen the allow oversized windows.
424
425 case WM_WINDOWPOSCHANGING:
426 {
427 WINDOWPOS* wpos = (WINDOWPOS*)lParam;
george82ffc14a62006-09-05 06:51:41 +0000428 if ((wpos->flags & SWP_NOSIZE) || isAutoScaling())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000429 break;
430
george82f0775132007-01-27 15:48:25 +0000431 // Calculate the minimum size of main window
432 RECT r;
433 Rect min_size;
434 int tbMinWidth = 0, tbMinHeight = 0;
435 if (isToolbarEnabled()) {
436 tbMinWidth = tb.getTotalWidth();
437 tbMinHeight = tb.getHeight();
438 SetRect(&r, 0, 0, tbMinWidth, tbMinHeight);
439 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
440 min_size = Rect(r.left, r.top, r.right, r.bottom);
441 }
442
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000443 // Work out how big the window should ideally be
444 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
445 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
446 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
447
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000448 SetRect(&r, 0, 0, buffer->width(), buffer->height());
449 AdjustWindowRectEx(&r, style, FALSE, style_ex);
450 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
451 if (current_style & WS_VSCROLL)
452 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
453 if (current_style & WS_HSCROLL)
454 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
455
456 SetRect(&r, reqd_size.tl.x, reqd_size.tl.y, reqd_size.br.x, reqd_size.br.y);
george82ced9db12006-09-11 09:09:14 +0000457 if (isToolbarEnabled())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000458 r.bottom += tb.getHeight();
459 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
460 reqd_size = Rect(r.left, r.top, r.right, r.bottom);
461
462 RECT current;
463 GetWindowRect(handle, &current);
464
george82f0775132007-01-27 15:48:25 +0000465 if (min_size.width() > reqd_size.width()) {
466 reqd_size.tl.x = min_size.tl.x;
467 reqd_size.br.x = min_size.br.x;
468 }
469 if (min_size.height() > reqd_size.height()) {
470 reqd_size.tl.y = min_size.tl.y;
471 reqd_size.br.y = min_size.br.y;
472 }
473
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000474 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
george82f0775132007-01-27 15:48:25 +0000475 // Ensure that the window isn't resized too large or too small
476 if ((wpos->cx < min_size.width()) && isToolbarEnabled()) {
477 wpos->cx = min_size.width();
478 wpos->x = current.left;
479 } else if ((wpos->cx > reqd_size.width())) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000480 wpos->cx = reqd_size.width();
481 wpos->x = current.left;
482 }
george82f0775132007-01-27 15:48:25 +0000483 if ((wpos->cy < min_size.height()) && isToolbarEnabled()) {
484 wpos->cy = min_size.height();
485 wpos->y = current.top;
486 } else if (wpos->cy > reqd_size.height()) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000487 wpos->cy = reqd_size.height();
488 wpos->y = current.top;
489 }
490 }
491 }
492 break;
493
494 // Resize child windows and update window size info we have cached.
495
496 case WM_SIZE:
497 {
498 Point old_offset = desktopToClient(Point(0, 0));
499 RECT r;
500
501 // Resize child windows
502 GetClientRect(handle, &r);
503 if (tb.isVisible()) {
george82858a4642007-01-27 15:32:27 +0000504 MoveWindow(frameHandle, 0, tb.getHeight(), r.right, r.bottom - tb.getHeight(), TRUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000505 } else {
506 MoveWindow(frameHandle, 0, 0, r.right, r.bottom, TRUE);
507 }
508 tb.autoSize();
509
510 // Update the cached sizing information
511 GetWindowRect(frameHandle, &r);
512 window_size = Rect(r.left, r.top, r.right, r.bottom);
513 GetClientRect(frameHandle, &r);
514 client_size = Rect(r.left, r.top, r.right, r.bottom);
515
george82ffc14a62006-09-05 06:51:41 +0000516 // Perform the AutoScaling operation
517 if (isAutoScaling()) {
518 fitBufferToWindow(false);
519 } else {
520 // Determine whether scrollbars are required
521 calculateScrollBars();
522 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000523
524 // Redraw if required
george82ffc14a62006-09-05 06:51:41 +0000525 if ((!old_offset.equals(desktopToClient(Point(0, 0)))) || isAutoScaling())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000526 InvalidateRect(frameHandle, 0, TRUE);
527 }
528 break;
529
530 // -=- Bump-scrolling
531
532 case WM_TIMER:
533 switch (wParam) {
534 case TIMER_BUMPSCROLL:
535 if (!setViewportOffset(scrolloffset.translate(bumpScrollDelta)))
536 bumpScrollTimer.stop();
537 break;
538 case TIMER_POINTER_INTERVAL:
539 case TIMER_POINTER_3BUTTON:
540 ptr.handleTimer(callback, wParam);
541 break;
542 }
543 break;
544
545 // -=- Track whether or not the window has focus
546
547 case WM_SETFOCUS:
548 has_focus = true;
549 break;
550 case WM_KILLFOCUS:
551 has_focus = false;
552 cursorOutsideBuffer();
553 // Restore the keyboard to a consistent state
554 kbd.releaseAllKeys(callback);
555 break;
556
557 // -=- If the menu is about to be shown, make sure it's up to date
558
559 case WM_INITMENU:
560 callback->refreshMenu(true);
561 break;
562
563 // -=- Handle the extra window menu items
564
565 // Pass system menu messages to the callback and only attempt
566 // to process them ourselves if the callback returns false.
567 case WM_SYSCOMMAND:
568 // Call the supplied callback
569 if (callback->sysCommand(wParam, lParam))
570 break;
571
572 // - Not processed by the callback, so process it as a system message
573 switch (wParam & 0xfff0) {
574
575 // When restored, ensure that full-screen mode is re-enabled if required.
576 case SC_RESTORE:
577 {
578 if (GetWindowLong(handle, GWL_STYLE) & WS_MINIMIZE) {
579 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
580 setFullscreen(fullscreenRestore);
581 }
582 else if (fullscreenActive)
583 setFullscreen(false);
584 else
585 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
586
587 return 0;
588 }
589
590 // If we are maximized or minimized then that cancels full-screen mode.
591 case SC_MINIMIZE:
592 case SC_MAXIMIZE:
593 fullscreenRestore = fullscreenActive;
594 setFullscreen(false);
595 break;
596
597 }
598 break;
599
600 // Treat all menu commands as system menu commands
601 case WM_COMMAND:
602 SendMessage(handle, WM_SYSCOMMAND, wParam, lParam);
603 return 0;
604
605 // -=- Handle keyboard input
606
607 case WM_KEYUP:
608 case WM_KEYDOWN:
609 // Hook the MenuKey to pop-up the window menu
610 if (menuKey && (wParam == menuKey)) {
611
612 bool ctrlDown = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0;
613 bool altDown = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0;
614 bool shiftDown = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
615 if (!(ctrlDown || altDown || shiftDown)) {
616
617 // If MenuKey is being released then pop-up the menu
618 if ((msg == WM_KEYDOWN)) {
619 // Make sure it's up to date
620 //
621 // NOTE: Here we call refreshMenu only to grey out Move and Size
622 // menu items. Other things will be refreshed once again
623 // while processing the WM_INITMENU message.
624 //
625 callback->refreshMenu(false);
626
627 // Show it under the pointer
628 POINT pt;
629 GetCursorPos(&pt);
630 cursorInBuffer = false;
631 TrackPopupMenu(GetSystemMenu(handle, FALSE),
632 TPM_CENTERALIGN | TPM_VCENTERALIGN, pt.x, pt.y, 0, handle, 0);
633 }
634
635 // Ignore the MenuKey keypress for both press & release events
636 return 0;
637 }
638 }
639 case WM_SYSKEYDOWN:
640 case WM_SYSKEYUP:
641 kbd.keyEvent(callback, wParam, lParam, (msg == WM_KEYDOWN) || (msg == WM_SYSKEYDOWN));
642 return 0;
643
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000644 // -=- Handle mouse wheel scroll events
645
646#ifdef WM_MOUSEWHEEL
647 case WM_MOUSEWHEEL:
648 processMouseMessage(msg, wParam, lParam);
649 break;
650#endif
651
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000652 // -=- Handle the window closing
653
654 case WM_CLOSE:
655 vlog.debug("WM_CLOSE %x", handle);
656 callback->closeWindow();
657 break;
658
659 }
660
661 return rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
662}
663
664LRESULT
665DesktopWindow::processFrameMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
666 switch (msg) {
667
668 // -=- Paint the remote frame buffer
669
670 case WM_PAINT:
671 {
672 PAINTSTRUCT ps;
673 HDC paintDC = BeginPaint(frameHandle, &ps);
674 if (!paintDC)
675 throw rdr::SystemException("unable to BeginPaint", GetLastError());
676 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
677
678 if (!pr.is_empty()) {
679
680 // Draw using the correct palette
681 PaletteSelector pSel(paintDC, windowPalette.getHandle());
682
683 if (buffer->bitmap) {
684 // Update the bitmap's palette
685 if (palette_changed) {
686 palette_changed = false;
687 buffer->refreshPalette();
688 }
689
690 // Get device context
691 BitmapDC bitmapDC(paintDC, buffer->bitmap);
692
693 // Blit the border if required
694 Rect bufpos = desktopToClient(buffer->getRect());
695 if (!pr.enclosed_by(bufpos)) {
696 vlog.debug("draw border");
697 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
698 RECT r;
699 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
700 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
701 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
702 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
703 }
704
705 // Do the blit
706 Point buf_pos = clientToDesktop(pr.tl);
707
708 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
709 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
710 throw rdr::SystemException("unable to BitBlt to window", GetLastError());
711 }
712 }
713
714 EndPaint(frameHandle, &ps);
715
716 // - Notify the callback that a paint message has finished processing
717 callback->paintCompleted();
718 }
719 return 0;
720
721 // -=- Palette management
722
723 case WM_PALETTECHANGED:
724 vlog.debug("WM_PALETTECHANGED");
725 if ((HWND)wParam == frameHandle) {
726 vlog.debug("ignoring");
727 break;
728 }
729 case WM_QUERYNEWPALETTE:
730 vlog.debug("re-selecting palette");
731 {
732 WindowDC wdc(frameHandle);
733 PaletteSelector pSel(wdc, windowPalette.getHandle());
734 if (pSel.isRedrawRequired()) {
735 InvalidateRect(frameHandle, 0, FALSE);
736 UpdateWindow(frameHandle);
737 }
738 }
739 return TRUE;
740
741 case WM_VSCROLL:
742 case WM_HSCROLL:
743 {
744 Point delta;
745 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
746
747 switch (LOWORD(wParam)) {
748 case SB_PAGEUP: newpos -= 50; break;
749 case SB_PAGEDOWN: newpos += 50; break;
750 case SB_LINEUP: newpos -= 5; break;
751 case SB_LINEDOWN: newpos += 5; break;
752 case SB_THUMBTRACK:
753 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
754 default: vlog.info("received unknown scroll message");
755 };
756
757 if (msg == WM_HSCROLL)
758 setViewportOffset(Point(newpos, scrolloffset.y));
759 else
760 setViewportOffset(Point(scrolloffset.x, newpos));
761
762 SCROLLINFO si;
763 si.cbSize = sizeof(si);
764 si.fMask = SIF_POS;
765 si.nPos = newpos;
766 SetScrollInfo(frameHandle, (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
767 }
768 break;
769
770 // -=- Cursor shape/visibility handling
771
772 case WM_SETCURSOR:
773 if (LOWORD(lParam) != HTCLIENT)
774 break;
775 SetCursor(cursorInBuffer ? dotCursor : arrowCursor);
776 return TRUE;
777
778 case WM_MOUSELEAVE:
779 trackingMouseLeave = false;
780 cursorOutsideBuffer();
781 return 0;
782
783 // -=- Mouse input handling
784
785 case WM_MOUSEMOVE:
786 case WM_LBUTTONUP:
787 case WM_MBUTTONUP:
788 case WM_RBUTTONUP:
789 case WM_LBUTTONDOWN:
790 case WM_MBUTTONDOWN:
791 case WM_RBUTTONDOWN:
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000792 processMouseMessage(msg, wParam, lParam);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000793 break;
794 }
795
796 return rfb::win32::SafeDefWindowProc(frameHandle, msg, wParam, lParam);
797}
798
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000799void
800DesktopWindow::processMouseMessage(UINT msg, WPARAM wParam, LPARAM lParam)
801{
802 if (!has_focus) {
803 cursorOutsideBuffer();
804 return;
805 }
806
807 if (!trackingMouseLeave) {
808 TRACKMOUSEEVENT tme;
809 tme.cbSize = sizeof(TRACKMOUSEEVENT);
810 tme.dwFlags = TME_LEAVE;
811 tme.hwndTrack = frameHandle;
812 _TrackMouseEvent(&tme);
813 trackingMouseLeave = true;
814 }
815 int mask = 0;
816 if (LOWORD(wParam) & MK_LBUTTON) mask |= 1;
817 if (LOWORD(wParam) & MK_MBUTTON) mask |= 2;
818 if (LOWORD(wParam) & MK_RBUTTON) mask |= 4;
819
820#ifdef WM_MOUSEWHEEL
821 if (msg == WM_MOUSEWHEEL) {
822 int delta = (short)HIWORD(wParam);
823 int repeats = (abs(delta)+119) / 120;
824 int wheelMask = (delta > 0) ? 8 : 16;
825 vlog.debug("repeats %d, mask %d\n",repeats,wheelMask);
826 for (int i=0; i<repeats; i++) {
827 ptr.pointerEvent(callback, oldpos, mask | wheelMask);
828 ptr.pointerEvent(callback, oldpos, mask);
829 }
830 } else {
831#endif
832 Point clientPos = Point(LOWORD(lParam), HIWORD(lParam));
833 Point p = clientToDesktop(clientPos);
834
835 // If the mouse is not within the server buffer area, do nothing
836 cursorInBuffer = buffer->getRect().contains(p);
837 if (!cursorInBuffer) {
838 cursorOutsideBuffer();
839 return;
840 }
841
842 // If we're locally rendering the cursor then redraw it
843 if (cursorAvailable) {
844 // - Render the cursor!
845 if (!p.equals(cursorPos)) {
846 hideLocalCursor();
847 cursorPos = p;
848 showLocalCursor();
849 if (cursorVisible)
850 hideSystemCursor();
851 }
852 }
853
854 // If we are doing bump-scrolling then try that first...
855 if (processBumpScroll(clientPos))
856 return;
857
858 // Send a pointer event to the server
859 oldpos = p;
860 if (buffer->isScaling()) {
george822446ed02007-03-10 08:55:35 +0000861 p.x /= buffer->getScaleRatioX();
862 p.y /= buffer->getScaleRatioY();
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000863 }
864 ptr.pointerEvent(callback, p, mask);
865#ifdef WM_MOUSEWHEEL
866 }
867#endif
868}
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000869
870void
871DesktopWindow::hideLocalCursor() {
872 // - Blit the cursor backing store over the cursor
873 // *** ALWAYS call this BEFORE changing buffer PF!!!
874 if (cursorVisible) {
875 cursorVisible = false;
876 buffer->DIBSectionBuffer::imageRect(cursorBackingRect, cursorBacking.data);
877 invalidateDesktopRect(cursorBackingRect, false);
878 }
879}
880
881void
882DesktopWindow::showLocalCursor() {
883 if (cursorAvailable && !cursorVisible && cursorInBuffer) {
george827c721cc2006-09-23 07:09:37 +0000884 if (!buffer->getScaledPixelFormat().equal(cursor.getPF()) ||
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000885 cursor.getRect().is_empty()) {
886 vlog.info("attempting to render invalid local cursor");
887 cursorAvailable = false;
888 showSystemCursor();
889 return;
890 }
891 cursorVisible = true;
892
893 cursorBackingRect = cursor.getRect().translate(cursorPos).translate(cursor.hotspot.negate());
894 cursorBackingRect = cursorBackingRect.intersect(buffer->getRect());
895 buffer->getImage(cursorBacking.data, cursorBackingRect);
896
897 renderLocalCursor();
898
899 invalidateDesktopRect(cursorBackingRect, false);
900 }
901}
902
903void DesktopWindow::cursorOutsideBuffer()
904{
905 cursorInBuffer = false;
906 hideLocalCursor();
907 showSystemCursor();
908}
909
910void
911DesktopWindow::renderLocalCursor()
912{
913 Rect r = cursor.getRect();
914 r = r.translate(cursorPos).translate(cursor.hotspot.negate());
915 buffer->DIBSectionBuffer::maskRect(r, cursor.data, cursor.mask.buf);
916}
917
918void
919DesktopWindow::hideSystemCursor() {
920 if (systemCursorVisible) {
921 vlog.debug("hide system cursor");
922 systemCursorVisible = false;
923 ShowCursor(FALSE);
924 }
925}
926
927void
928DesktopWindow::showSystemCursor() {
929 if (!systemCursorVisible) {
930 vlog.debug("show system cursor");
931 systemCursorVisible = true;
932 ShowCursor(TRUE);
933 }
934}
935
936
937bool
938DesktopWindow::invalidateDesktopRect(const Rect& crect, bool scaling) {
939 Rect rect;
940 if (buffer->isScaling() && scaling) {
941 rect = desktopToClient(buffer->calculateScaleBoundary(crect));
942 } else rect = desktopToClient(crect);
943 if (rect.intersect(client_size).is_empty()) return false;
944 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
945 InvalidateRect(frameHandle, &invalid, FALSE);
946 return true;
947}
948
949
950void
951DesktopWindow::notifyClipboardChanged(const char* text, int len) {
952 callback->clientCutText(text, len);
953}
954
955
956void
957DesktopWindow::setPF(const PixelFormat& pf) {
958 // If the cursor is the wrong format then clear it
george827c721cc2006-09-23 07:09:37 +0000959 if (!pf.equal(buffer->getScaledPixelFormat()))
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000960 setCursor(0, 0, Point(), 0, 0);
961
962 // Update the desktop buffer
963 buffer->setPF(pf);
964
965 // Redraw the window
966 InvalidateRect(frameHandle, 0, FALSE);
967}
968
969void
970DesktopWindow::setSize(int w, int h) {
971 vlog.debug("setSize %dx%d", w, h);
972
973 // If the locally-rendered cursor is visible then remove it
974 hideLocalCursor();
975
976 // Resize the backing buffer
977 buffer->setSize(w, h);
978
george82ffc14a62006-09-05 06:51:41 +0000979 // Calculate the pixel buffer aspect correlation. It's used
980 // for the autoScaling operation.
981 aspect_corr = (double)w / h;
982
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000983 // If the window is not maximised or full-screen then resize it
984 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
985 // Resize the window to the required size
986 RECT r = {0, 0, w, h};
987 AdjustWindowRectEx(&r, GetWindowLong(frameHandle, GWL_STYLE), FALSE,
988 GetWindowLong(frameHandle, GWL_EXSTYLE));
george82ced9db12006-09-11 09:09:14 +0000989 if (isToolbarEnabled())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000990 r.bottom += tb.getHeight();
991 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
992
993 // Resize about the center of the window, and clip to current monitor
994 MonitorInfo mi(handle);
995 resizeWindow(handle, r.right-r.left, r.bottom-r.top);
996 mi.clipTo(handle);
997 } else {
998 // Ensure the screen contents are consistent
999 InvalidateRect(frameHandle, 0, FALSE);
1000 }
1001
1002 // Enable/disable scrollbars as appropriate
1003 calculateScrollBars();
1004}
1005
george8274ea5f32006-09-11 11:40:12 +00001006void DesktopWindow::setAutoScaling(bool as) {
1007 autoScaling = as;
george822446ed02007-03-10 08:55:35 +00001008 if (isToolbarEnabled()) refreshToolbarButtons();
george8274ea5f32006-09-11 11:40:12 +00001009 if (as) fitBufferToWindow();
1010}
1011
george822446ed02007-03-10 08:55:35 +00001012void DesktopWindow::setDesktopScale(int scale_) {
1013 if (buffer->getScale() == scale_ || scale_ <= 0) return;
1014 buffer->setScale(scale_);
george8274ea5f32006-09-11 11:40:12 +00001015 if (isToolbarEnabled()) refreshToolbarButtons();
george822446ed02007-03-10 08:55:35 +00001016 if (!isAutoScaling()) resizeDesktopWindowToBuffer();
george82e0569e42006-09-11 15:56:10 +00001017 char *newTitle = new char[strlen(desktopName)+20];
1018 sprintf(newTitle, "%s @ %i%%", desktopName, getDesktopScale());
1019 SetWindowText(handle, TStr(newTitle));
1020 delete [] newTitle;
george82858a4642007-01-27 15:32:27 +00001021 InvalidateRect(frameHandle, 0, FALSE);
george8204a77712006-05-29 14:18:14 +00001022}
1023
george823c68f5f2006-09-05 06:17:01 +00001024void DesktopWindow::fitBufferToWindow(bool repaint) {
1025 double scale_ratio;
1026 double resized_aspect_corr = double(client_size.width()) / client_size.height();
1027 DWORD style = GetWindowLong(frameHandle, GWL_STYLE);
1028 if (style & (WS_VSCROLL | WS_HSCROLL)) {
1029 style &= ~(WS_VSCROLL | WS_HSCROLL);
1030 SetWindowLong(frameHandle, GWL_STYLE, style);
1031 SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1032 // Update the cached client size
1033 RECT r;
1034 GetClientRect(frameHandle, &r);
1035 client_size = Rect(r.left, r.top, r.right, r.bottom);
1036 }
1037 if (resized_aspect_corr > aspect_corr) {
1038 scale_ratio = double(client_size.height()) / buffer->getSrcHeight();
1039 } else {
1040 scale_ratio = double(client_size.width()) / buffer->getSrcWidth();
1041 }
george822446ed02007-03-10 08:55:35 +00001042 setDesktopScale(int(scale_ratio * 100));
george823c68f5f2006-09-05 06:17:01 +00001043}
1044
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001045void
1046DesktopWindow::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) {
1047 hideLocalCursor();
1048
1049 cursor.hotspot = hotspot;
1050
1051 cursor.setSize(w, h);
george827c721cc2006-09-23 07:09:37 +00001052 cursor.setPF(buffer->getScaledPixelFormat());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001053 cursor.imageRect(cursor.getRect(), data);
1054 memcpy(cursor.mask.buf, mask, cursor.maskLen());
1055 cursor.crop();
1056
1057 cursorBacking.setSize(w, h);
george827c721cc2006-09-23 07:09:37 +00001058 cursorBacking.setPF(buffer->getScaledPixelFormat());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001059
1060 cursorAvailable = true;
1061
1062 showLocalCursor();
1063}
1064
1065PixelFormat
1066DesktopWindow::getNativePF() const {
1067 vlog.debug("getNativePF()");
1068 return WindowDC(handle).getPF();
1069}
1070
1071
1072void
1073DesktopWindow::refreshWindowPalette(int start, int count) {
1074 vlog.debug("refreshWindowPalette(%d, %d)", start, count);
1075
1076 Colour colours[256];
1077 if (count > 256) {
1078 vlog.debug("%d palette entries", count);
1079 throw rdr::Exception("too many palette entries");
1080 }
1081
1082 // Copy the palette from the DIBSectionBuffer
1083 ColourMap* cm = buffer->getColourMap();
1084 if (!cm) return;
1085 for (int i=0; i<count; i++) {
1086 int r, g, b;
1087 cm->lookup(i, &r, &g, &b);
1088 colours[i].r = r;
1089 colours[i].g = g;
1090 colours[i].b = b;
1091 }
1092
1093 // Set the window palette
1094 windowPalette.setEntries(start, count, colours);
1095
1096 // Cause the window to be redrawn
1097 palette_changed = true;
1098 InvalidateRect(handle, 0, FALSE);
1099}
1100
1101
1102void DesktopWindow::calculateScrollBars() {
1103 // Calculate the required size of window
1104 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
1105 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
1106 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
1107 DWORD old_style;
1108 RECT r;
1109 SetRect(&r, 0, 0, buffer->width(), buffer->height());
1110 AdjustWindowRectEx(&r, style, FALSE, style_ex);
1111 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
1112
1113 if (!bumpScroll) {
1114 // We only enable scrollbars if bump-scrolling is not active.
1115 // Effectively, this means if full-screen is not active,
1116 // but I think it's better to make these things explicit.
1117
1118 // Work out whether scroll bars are required
1119 do {
1120 old_style = style;
1121
1122 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
1123 style |= WS_HSCROLL;
1124 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
1125 }
1126 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
1127 style |= WS_VSCROLL;
1128 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
1129 }
1130 } while (style != old_style);
1131 }
1132
1133 // Tell Windows to update the window style & cached settings
1134 if (style != current_style) {
1135 SetWindowLong(frameHandle, GWL_STYLE, style);
1136 SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1137 }
1138
1139 // Update the scroll settings
1140 SCROLLINFO si;
1141 if (style & WS_VSCROLL) {
1142 si.cbSize = sizeof(si);
1143 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1144 si.nMin = 0;
1145 si.nMax = buffer->height();
1146 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
1147 maxscrolloffset.y = max(0, si.nMax-si.nPage);
1148 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
1149 si.nPos = scrolloffset.y;
1150 SetScrollInfo(frameHandle, SB_VERT, &si, TRUE);
1151 }
1152 if (style & WS_HSCROLL) {
1153 si.cbSize = sizeof(si);
1154 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1155 si.nMin = 0;
1156 si.nMax = buffer->width();
1157 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
1158 maxscrolloffset.x = max(0, si.nMax-si.nPage);
1159 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
1160 si.nPos = scrolloffset.x;
1161 SetScrollInfo(frameHandle, SB_HORZ, &si, TRUE);
1162 }
1163
1164 // Update the cached client size
1165 GetClientRect(frameHandle, &r);
1166 client_size = Rect(r.left, r.top, r.right, r.bottom);
1167}
1168
george82858a4642007-01-27 15:32:27 +00001169void DesktopWindow::resizeDesktopWindowToBuffer() {
1170 RECT r;
1171 DWORD style = GetWindowLong(frameHandle, GWL_STYLE) & ~(WS_VSCROLL | WS_HSCROLL);
1172 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
1173
1174 // Calculate the required size of the desktop window
1175 SetRect(&r, 0, 0, buffer->width(), buffer->height());
1176 AdjustWindowRectEx(&r, style, FALSE, style_ex);
1177 if (isToolbarEnabled())
1178 r.bottom += tb.getHeight();
1179 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
1180
1181 // Set the required size, center the main window and clip to the current monitor
1182 SetWindowPos(handle, 0, 0, 0, r.right-r.left, r.bottom-r.top, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
1183 centerWindow(handle, NULL);
1184 MonitorInfo mi(getMonitor());
1185 mi.clipTo(handle);
1186
1187 // Enable/disable scrollbars as appropriate
1188 calculateScrollBars();
1189}
1190
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001191
1192void
1193DesktopWindow::setName(const char* name) {
1194 SetWindowText(handle, TStr(name));
george82e0569e42006-09-11 15:56:10 +00001195 strCopy(desktopName, name, sizeof(desktopName));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001196}
1197
1198
1199void
1200DesktopWindow::serverCutText(const char* str, int len) {
1201 CharArray t(len+1);
1202 memcpy(t.buf, str, len);
1203 t.buf[len] = 0;
1204 clipboard.setClipText(t.buf);
1205}
1206
1207
1208void DesktopWindow::fillRect(const Rect& r, Pixel pix) {
1209 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1210 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1211 buffer->fillRect(r, pix);
1212 invalidateDesktopRect(r);
1213}
1214void DesktopWindow::imageRect(const Rect& r, void* pixels) {
1215 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1216 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1217 buffer->imageRect(r, pixels);
1218 invalidateDesktopRect(r);
1219}
1220void DesktopWindow::copyRect(const Rect& r, int srcX, int srcY) {
1221 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1222 if (cursorBackingRect.overlaps(img_rect) ||
1223 cursorBackingRect.overlaps(Rect(srcX, srcY, srcX+img_rect.width(), srcY+img_rect.height())))
1224 hideLocalCursor();
1225 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1226 invalidateDesktopRect(r);
1227}
1228
1229void DesktopWindow::invertRect(const Rect& r) {
1230 int stride;
1231 rdr::U8* p = buffer->isScaling() ? buffer->getPixelsRW(buffer->calculateScaleBoundary(r), &stride)
1232 : buffer->getPixelsRW(r, &stride);
1233 for (int y = 0; y < r.height(); y++) {
1234 for (int x = 0; x < r.width(); x++) {
1235 switch (buffer->getPF().bpp) {
1236 case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break;
1237 case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break;
1238 case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break;
1239 }
1240 }
1241 }
1242 invalidateDesktopRect(r);
1243}