blob: 826572337ed5699682930686b3b2529c21e0ad76 [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
431 // Work out how big the window should ideally be
432 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
433 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
434 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
435
436 RECT r;
437 SetRect(&r, 0, 0, buffer->width(), buffer->height());
438 AdjustWindowRectEx(&r, style, FALSE, style_ex);
439 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
440 if (current_style & WS_VSCROLL)
441 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
442 if (current_style & WS_HSCROLL)
443 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
444
445 SetRect(&r, reqd_size.tl.x, reqd_size.tl.y, reqd_size.br.x, reqd_size.br.y);
george82ced9db12006-09-11 09:09:14 +0000446 if (isToolbarEnabled())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000447 r.bottom += tb.getHeight();
448 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
449 reqd_size = Rect(r.left, r.top, r.right, r.bottom);
450
451 RECT current;
452 GetWindowRect(handle, &current);
453
454 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
455 // Ensure that the window isn't resized too large
456 if (wpos->cx > reqd_size.width()) {
457 wpos->cx = reqd_size.width();
458 wpos->x = current.left;
459 }
460 if (wpos->cy > reqd_size.height()) {
461 wpos->cy = reqd_size.height();
462 wpos->y = current.top;
463 }
464 }
465 }
466 break;
467
468 // Resize child windows and update window size info we have cached.
469
470 case WM_SIZE:
471 {
472 Point old_offset = desktopToClient(Point(0, 0));
473 RECT r;
474
475 // Resize child windows
476 GetClientRect(handle, &r);
477 if (tb.isVisible()) {
george82858a4642007-01-27 15:32:27 +0000478 MoveWindow(frameHandle, 0, tb.getHeight(), r.right, r.bottom - tb.getHeight(), TRUE);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000479 } else {
480 MoveWindow(frameHandle, 0, 0, r.right, r.bottom, TRUE);
481 }
482 tb.autoSize();
483
484 // Update the cached sizing information
485 GetWindowRect(frameHandle, &r);
486 window_size = Rect(r.left, r.top, r.right, r.bottom);
487 GetClientRect(frameHandle, &r);
488 client_size = Rect(r.left, r.top, r.right, r.bottom);
489
george82ffc14a62006-09-05 06:51:41 +0000490 // Perform the AutoScaling operation
491 if (isAutoScaling()) {
492 fitBufferToWindow(false);
493 } else {
494 // Determine whether scrollbars are required
495 calculateScrollBars();
496 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000497
498 // Redraw if required
george82ffc14a62006-09-05 06:51:41 +0000499 if ((!old_offset.equals(desktopToClient(Point(0, 0)))) || isAutoScaling())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000500 InvalidateRect(frameHandle, 0, TRUE);
501 }
502 break;
503
504 // -=- Bump-scrolling
505
506 case WM_TIMER:
507 switch (wParam) {
508 case TIMER_BUMPSCROLL:
509 if (!setViewportOffset(scrolloffset.translate(bumpScrollDelta)))
510 bumpScrollTimer.stop();
511 break;
512 case TIMER_POINTER_INTERVAL:
513 case TIMER_POINTER_3BUTTON:
514 ptr.handleTimer(callback, wParam);
515 break;
516 }
517 break;
518
519 // -=- Track whether or not the window has focus
520
521 case WM_SETFOCUS:
522 has_focus = true;
523 break;
524 case WM_KILLFOCUS:
525 has_focus = false;
526 cursorOutsideBuffer();
527 // Restore the keyboard to a consistent state
528 kbd.releaseAllKeys(callback);
529 break;
530
531 // -=- If the menu is about to be shown, make sure it's up to date
532
533 case WM_INITMENU:
534 callback->refreshMenu(true);
535 break;
536
537 // -=- Handle the extra window menu items
538
539 // Pass system menu messages to the callback and only attempt
540 // to process them ourselves if the callback returns false.
541 case WM_SYSCOMMAND:
542 // Call the supplied callback
543 if (callback->sysCommand(wParam, lParam))
544 break;
545
546 // - Not processed by the callback, so process it as a system message
547 switch (wParam & 0xfff0) {
548
549 // When restored, ensure that full-screen mode is re-enabled if required.
550 case SC_RESTORE:
551 {
552 if (GetWindowLong(handle, GWL_STYLE) & WS_MINIMIZE) {
553 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
554 setFullscreen(fullscreenRestore);
555 }
556 else if (fullscreenActive)
557 setFullscreen(false);
558 else
559 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
560
561 return 0;
562 }
563
564 // If we are maximized or minimized then that cancels full-screen mode.
565 case SC_MINIMIZE:
566 case SC_MAXIMIZE:
567 fullscreenRestore = fullscreenActive;
568 setFullscreen(false);
569 break;
570
571 }
572 break;
573
574 // Treat all menu commands as system menu commands
575 case WM_COMMAND:
576 SendMessage(handle, WM_SYSCOMMAND, wParam, lParam);
577 return 0;
578
579 // -=- Handle keyboard input
580
581 case WM_KEYUP:
582 case WM_KEYDOWN:
583 // Hook the MenuKey to pop-up the window menu
584 if (menuKey && (wParam == menuKey)) {
585
586 bool ctrlDown = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0;
587 bool altDown = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0;
588 bool shiftDown = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
589 if (!(ctrlDown || altDown || shiftDown)) {
590
591 // If MenuKey is being released then pop-up the menu
592 if ((msg == WM_KEYDOWN)) {
593 // Make sure it's up to date
594 //
595 // NOTE: Here we call refreshMenu only to grey out Move and Size
596 // menu items. Other things will be refreshed once again
597 // while processing the WM_INITMENU message.
598 //
599 callback->refreshMenu(false);
600
601 // Show it under the pointer
602 POINT pt;
603 GetCursorPos(&pt);
604 cursorInBuffer = false;
605 TrackPopupMenu(GetSystemMenu(handle, FALSE),
606 TPM_CENTERALIGN | TPM_VCENTERALIGN, pt.x, pt.y, 0, handle, 0);
607 }
608
609 // Ignore the MenuKey keypress for both press & release events
610 return 0;
611 }
612 }
613 case WM_SYSKEYDOWN:
614 case WM_SYSKEYUP:
615 kbd.keyEvent(callback, wParam, lParam, (msg == WM_KEYDOWN) || (msg == WM_SYSKEYDOWN));
616 return 0;
617
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000618 // -=- Handle mouse wheel scroll events
619
620#ifdef WM_MOUSEWHEEL
621 case WM_MOUSEWHEEL:
622 processMouseMessage(msg, wParam, lParam);
623 break;
624#endif
625
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000626 // -=- Handle the window closing
627
628 case WM_CLOSE:
629 vlog.debug("WM_CLOSE %x", handle);
630 callback->closeWindow();
631 break;
632
633 }
634
635 return rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
636}
637
638LRESULT
639DesktopWindow::processFrameMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
640 switch (msg) {
641
642 // -=- Paint the remote frame buffer
643
644 case WM_PAINT:
645 {
646 PAINTSTRUCT ps;
647 HDC paintDC = BeginPaint(frameHandle, &ps);
648 if (!paintDC)
649 throw rdr::SystemException("unable to BeginPaint", GetLastError());
650 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
651
652 if (!pr.is_empty()) {
653
654 // Draw using the correct palette
655 PaletteSelector pSel(paintDC, windowPalette.getHandle());
656
657 if (buffer->bitmap) {
658 // Update the bitmap's palette
659 if (palette_changed) {
660 palette_changed = false;
661 buffer->refreshPalette();
662 }
663
664 // Get device context
665 BitmapDC bitmapDC(paintDC, buffer->bitmap);
666
667 // Blit the border if required
668 Rect bufpos = desktopToClient(buffer->getRect());
669 if (!pr.enclosed_by(bufpos)) {
670 vlog.debug("draw border");
671 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
672 RECT r;
673 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
674 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
675 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
676 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
677 }
678
679 // Do the blit
680 Point buf_pos = clientToDesktop(pr.tl);
681
682 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
683 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
684 throw rdr::SystemException("unable to BitBlt to window", GetLastError());
685 }
686 }
687
688 EndPaint(frameHandle, &ps);
689
690 // - Notify the callback that a paint message has finished processing
691 callback->paintCompleted();
692 }
693 return 0;
694
695 // -=- Palette management
696
697 case WM_PALETTECHANGED:
698 vlog.debug("WM_PALETTECHANGED");
699 if ((HWND)wParam == frameHandle) {
700 vlog.debug("ignoring");
701 break;
702 }
703 case WM_QUERYNEWPALETTE:
704 vlog.debug("re-selecting palette");
705 {
706 WindowDC wdc(frameHandle);
707 PaletteSelector pSel(wdc, windowPalette.getHandle());
708 if (pSel.isRedrawRequired()) {
709 InvalidateRect(frameHandle, 0, FALSE);
710 UpdateWindow(frameHandle);
711 }
712 }
713 return TRUE;
714
715 case WM_VSCROLL:
716 case WM_HSCROLL:
717 {
718 Point delta;
719 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
720
721 switch (LOWORD(wParam)) {
722 case SB_PAGEUP: newpos -= 50; break;
723 case SB_PAGEDOWN: newpos += 50; break;
724 case SB_LINEUP: newpos -= 5; break;
725 case SB_LINEDOWN: newpos += 5; break;
726 case SB_THUMBTRACK:
727 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
728 default: vlog.info("received unknown scroll message");
729 };
730
731 if (msg == WM_HSCROLL)
732 setViewportOffset(Point(newpos, scrolloffset.y));
733 else
734 setViewportOffset(Point(scrolloffset.x, newpos));
735
736 SCROLLINFO si;
737 si.cbSize = sizeof(si);
738 si.fMask = SIF_POS;
739 si.nPos = newpos;
740 SetScrollInfo(frameHandle, (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
741 }
742 break;
743
744 // -=- Cursor shape/visibility handling
745
746 case WM_SETCURSOR:
747 if (LOWORD(lParam) != HTCLIENT)
748 break;
749 SetCursor(cursorInBuffer ? dotCursor : arrowCursor);
750 return TRUE;
751
752 case WM_MOUSELEAVE:
753 trackingMouseLeave = false;
754 cursorOutsideBuffer();
755 return 0;
756
757 // -=- Mouse input handling
758
759 case WM_MOUSEMOVE:
760 case WM_LBUTTONUP:
761 case WM_MBUTTONUP:
762 case WM_RBUTTONUP:
763 case WM_LBUTTONDOWN:
764 case WM_MBUTTONDOWN:
765 case WM_RBUTTONDOWN:
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000766 processMouseMessage(msg, wParam, lParam);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000767 break;
768 }
769
770 return rfb::win32::SafeDefWindowProc(frameHandle, msg, wParam, lParam);
771}
772
Constantin Kaplinskyd5f59272006-09-14 05:14:43 +0000773void
774DesktopWindow::processMouseMessage(UINT msg, WPARAM wParam, LPARAM lParam)
775{
776 if (!has_focus) {
777 cursorOutsideBuffer();
778 return;
779 }
780
781 if (!trackingMouseLeave) {
782 TRACKMOUSEEVENT tme;
783 tme.cbSize = sizeof(TRACKMOUSEEVENT);
784 tme.dwFlags = TME_LEAVE;
785 tme.hwndTrack = frameHandle;
786 _TrackMouseEvent(&tme);
787 trackingMouseLeave = true;
788 }
789 int mask = 0;
790 if (LOWORD(wParam) & MK_LBUTTON) mask |= 1;
791 if (LOWORD(wParam) & MK_MBUTTON) mask |= 2;
792 if (LOWORD(wParam) & MK_RBUTTON) mask |= 4;
793
794#ifdef WM_MOUSEWHEEL
795 if (msg == WM_MOUSEWHEEL) {
796 int delta = (short)HIWORD(wParam);
797 int repeats = (abs(delta)+119) / 120;
798 int wheelMask = (delta > 0) ? 8 : 16;
799 vlog.debug("repeats %d, mask %d\n",repeats,wheelMask);
800 for (int i=0; i<repeats; i++) {
801 ptr.pointerEvent(callback, oldpos, mask | wheelMask);
802 ptr.pointerEvent(callback, oldpos, mask);
803 }
804 } else {
805#endif
806 Point clientPos = Point(LOWORD(lParam), HIWORD(lParam));
807 Point p = clientToDesktop(clientPos);
808
809 // If the mouse is not within the server buffer area, do nothing
810 cursorInBuffer = buffer->getRect().contains(p);
811 if (!cursorInBuffer) {
812 cursorOutsideBuffer();
813 return;
814 }
815
816 // If we're locally rendering the cursor then redraw it
817 if (cursorAvailable) {
818 // - Render the cursor!
819 if (!p.equals(cursorPos)) {
820 hideLocalCursor();
821 cursorPos = p;
822 showLocalCursor();
823 if (cursorVisible)
824 hideSystemCursor();
825 }
826 }
827
828 // If we are doing bump-scrolling then try that first...
829 if (processBumpScroll(clientPos))
830 return;
831
832 // Send a pointer event to the server
833 oldpos = p;
834 if (buffer->isScaling()) {
835 p.x /= buffer->getScaleRatio();
836 p.y /= buffer->getScaleRatio();
837 }
838 ptr.pointerEvent(callback, p, mask);
839#ifdef WM_MOUSEWHEEL
840 }
841#endif
842}
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000843
844void
845DesktopWindow::hideLocalCursor() {
846 // - Blit the cursor backing store over the cursor
847 // *** ALWAYS call this BEFORE changing buffer PF!!!
848 if (cursorVisible) {
849 cursorVisible = false;
850 buffer->DIBSectionBuffer::imageRect(cursorBackingRect, cursorBacking.data);
851 invalidateDesktopRect(cursorBackingRect, false);
852 }
853}
854
855void
856DesktopWindow::showLocalCursor() {
857 if (cursorAvailable && !cursorVisible && cursorInBuffer) {
george827c721cc2006-09-23 07:09:37 +0000858 if (!buffer->getScaledPixelFormat().equal(cursor.getPF()) ||
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000859 cursor.getRect().is_empty()) {
860 vlog.info("attempting to render invalid local cursor");
861 cursorAvailable = false;
862 showSystemCursor();
863 return;
864 }
865 cursorVisible = true;
866
867 cursorBackingRect = cursor.getRect().translate(cursorPos).translate(cursor.hotspot.negate());
868 cursorBackingRect = cursorBackingRect.intersect(buffer->getRect());
869 buffer->getImage(cursorBacking.data, cursorBackingRect);
870
871 renderLocalCursor();
872
873 invalidateDesktopRect(cursorBackingRect, false);
874 }
875}
876
877void DesktopWindow::cursorOutsideBuffer()
878{
879 cursorInBuffer = false;
880 hideLocalCursor();
881 showSystemCursor();
882}
883
884void
885DesktopWindow::renderLocalCursor()
886{
887 Rect r = cursor.getRect();
888 r = r.translate(cursorPos).translate(cursor.hotspot.negate());
889 buffer->DIBSectionBuffer::maskRect(r, cursor.data, cursor.mask.buf);
890}
891
892void
893DesktopWindow::hideSystemCursor() {
894 if (systemCursorVisible) {
895 vlog.debug("hide system cursor");
896 systemCursorVisible = false;
897 ShowCursor(FALSE);
898 }
899}
900
901void
902DesktopWindow::showSystemCursor() {
903 if (!systemCursorVisible) {
904 vlog.debug("show system cursor");
905 systemCursorVisible = true;
906 ShowCursor(TRUE);
907 }
908}
909
910
911bool
912DesktopWindow::invalidateDesktopRect(const Rect& crect, bool scaling) {
913 Rect rect;
914 if (buffer->isScaling() && scaling) {
915 rect = desktopToClient(buffer->calculateScaleBoundary(crect));
916 } else rect = desktopToClient(crect);
917 if (rect.intersect(client_size).is_empty()) return false;
918 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
919 InvalidateRect(frameHandle, &invalid, FALSE);
920 return true;
921}
922
923
924void
925DesktopWindow::notifyClipboardChanged(const char* text, int len) {
926 callback->clientCutText(text, len);
927}
928
929
930void
931DesktopWindow::setPF(const PixelFormat& pf) {
932 // If the cursor is the wrong format then clear it
george827c721cc2006-09-23 07:09:37 +0000933 if (!pf.equal(buffer->getScaledPixelFormat()))
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000934 setCursor(0, 0, Point(), 0, 0);
935
936 // Update the desktop buffer
937 buffer->setPF(pf);
938
939 // Redraw the window
940 InvalidateRect(frameHandle, 0, FALSE);
941}
942
943void
944DesktopWindow::setSize(int w, int h) {
945 vlog.debug("setSize %dx%d", w, h);
946
947 // If the locally-rendered cursor is visible then remove it
948 hideLocalCursor();
949
950 // Resize the backing buffer
951 buffer->setSize(w, h);
952
george82ffc14a62006-09-05 06:51:41 +0000953 // Calculate the pixel buffer aspect correlation. It's used
954 // for the autoScaling operation.
955 aspect_corr = (double)w / h;
956
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000957 // If the window is not maximised or full-screen then resize it
958 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
959 // Resize the window to the required size
960 RECT r = {0, 0, w, h};
961 AdjustWindowRectEx(&r, GetWindowLong(frameHandle, GWL_STYLE), FALSE,
962 GetWindowLong(frameHandle, GWL_EXSTYLE));
george82ced9db12006-09-11 09:09:14 +0000963 if (isToolbarEnabled())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000964 r.bottom += tb.getHeight();
965 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
966
967 // Resize about the center of the window, and clip to current monitor
968 MonitorInfo mi(handle);
969 resizeWindow(handle, r.right-r.left, r.bottom-r.top);
970 mi.clipTo(handle);
971 } else {
972 // Ensure the screen contents are consistent
973 InvalidateRect(frameHandle, 0, FALSE);
974 }
975
976 // Enable/disable scrollbars as appropriate
977 calculateScrollBars();
978}
979
george8274ea5f32006-09-11 11:40:12 +0000980void DesktopWindow::setAutoScaling(bool as) {
981 autoScaling = as;
982 if (as) fitBufferToWindow();
983}
984
george822baaaec2006-09-11 15:09:09 +0000985void DesktopWindow::setDesktopScaleRatio(double scale_ratio) {
986 buffer->setScaleRatio(scale_ratio);
george82858a4642007-01-27 15:32:27 +0000987 if (!isAutoScaling()) resizeDesktopWindowToBuffer();
george8274ea5f32006-09-11 11:40:12 +0000988 if (isToolbarEnabled()) refreshToolbarButtons();
george82e0569e42006-09-11 15:56:10 +0000989 char *newTitle = new char[strlen(desktopName)+20];
990 sprintf(newTitle, "%s @ %i%%", desktopName, getDesktopScale());
991 SetWindowText(handle, TStr(newTitle));
992 delete [] newTitle;
george82858a4642007-01-27 15:32:27 +0000993 InvalidateRect(frameHandle, 0, FALSE);
george8204a77712006-05-29 14:18:14 +0000994}
995
george823c68f5f2006-09-05 06:17:01 +0000996void DesktopWindow::fitBufferToWindow(bool repaint) {
997 double scale_ratio;
998 double resized_aspect_corr = double(client_size.width()) / client_size.height();
999 DWORD style = GetWindowLong(frameHandle, GWL_STYLE);
1000 if (style & (WS_VSCROLL | WS_HSCROLL)) {
1001 style &= ~(WS_VSCROLL | WS_HSCROLL);
1002 SetWindowLong(frameHandle, GWL_STYLE, style);
1003 SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1004 // Update the cached client size
1005 RECT r;
1006 GetClientRect(frameHandle, &r);
1007 client_size = Rect(r.left, r.top, r.right, r.bottom);
1008 }
1009 if (resized_aspect_corr > aspect_corr) {
1010 scale_ratio = double(client_size.height()) / buffer->getSrcHeight();
1011 } else {
1012 scale_ratio = double(client_size.width()) / buffer->getSrcWidth();
1013 }
george822baaaec2006-09-11 15:09:09 +00001014 setDesktopScaleRatio(scale_ratio);
george823c68f5f2006-09-05 06:17:01 +00001015}
1016
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001017void
1018DesktopWindow::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) {
1019 hideLocalCursor();
1020
1021 cursor.hotspot = hotspot;
1022
1023 cursor.setSize(w, h);
george827c721cc2006-09-23 07:09:37 +00001024 cursor.setPF(buffer->getScaledPixelFormat());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001025 cursor.imageRect(cursor.getRect(), data);
1026 memcpy(cursor.mask.buf, mask, cursor.maskLen());
1027 cursor.crop();
1028
1029 cursorBacking.setSize(w, h);
george827c721cc2006-09-23 07:09:37 +00001030 cursorBacking.setPF(buffer->getScaledPixelFormat());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001031
1032 cursorAvailable = true;
1033
1034 showLocalCursor();
1035}
1036
1037PixelFormat
1038DesktopWindow::getNativePF() const {
1039 vlog.debug("getNativePF()");
1040 return WindowDC(handle).getPF();
1041}
1042
1043
1044void
1045DesktopWindow::refreshWindowPalette(int start, int count) {
1046 vlog.debug("refreshWindowPalette(%d, %d)", start, count);
1047
1048 Colour colours[256];
1049 if (count > 256) {
1050 vlog.debug("%d palette entries", count);
1051 throw rdr::Exception("too many palette entries");
1052 }
1053
1054 // Copy the palette from the DIBSectionBuffer
1055 ColourMap* cm = buffer->getColourMap();
1056 if (!cm) return;
1057 for (int i=0; i<count; i++) {
1058 int r, g, b;
1059 cm->lookup(i, &r, &g, &b);
1060 colours[i].r = r;
1061 colours[i].g = g;
1062 colours[i].b = b;
1063 }
1064
1065 // Set the window palette
1066 windowPalette.setEntries(start, count, colours);
1067
1068 // Cause the window to be redrawn
1069 palette_changed = true;
1070 InvalidateRect(handle, 0, FALSE);
1071}
1072
1073
1074void DesktopWindow::calculateScrollBars() {
1075 // Calculate the required size of window
1076 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
1077 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
1078 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
1079 DWORD old_style;
1080 RECT r;
1081 SetRect(&r, 0, 0, buffer->width(), buffer->height());
1082 AdjustWindowRectEx(&r, style, FALSE, style_ex);
1083 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
1084
1085 if (!bumpScroll) {
1086 // We only enable scrollbars if bump-scrolling is not active.
1087 // Effectively, this means if full-screen is not active,
1088 // but I think it's better to make these things explicit.
1089
1090 // Work out whether scroll bars are required
1091 do {
1092 old_style = style;
1093
1094 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
1095 style |= WS_HSCROLL;
1096 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
1097 }
1098 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
1099 style |= WS_VSCROLL;
1100 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
1101 }
1102 } while (style != old_style);
1103 }
1104
1105 // Tell Windows to update the window style & cached settings
1106 if (style != current_style) {
1107 SetWindowLong(frameHandle, GWL_STYLE, style);
1108 SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1109 }
1110
1111 // Update the scroll settings
1112 SCROLLINFO si;
1113 if (style & WS_VSCROLL) {
1114 si.cbSize = sizeof(si);
1115 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1116 si.nMin = 0;
1117 si.nMax = buffer->height();
1118 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
1119 maxscrolloffset.y = max(0, si.nMax-si.nPage);
1120 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
1121 si.nPos = scrolloffset.y;
1122 SetScrollInfo(frameHandle, SB_VERT, &si, TRUE);
1123 }
1124 if (style & WS_HSCROLL) {
1125 si.cbSize = sizeof(si);
1126 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1127 si.nMin = 0;
1128 si.nMax = buffer->width();
1129 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
1130 maxscrolloffset.x = max(0, si.nMax-si.nPage);
1131 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
1132 si.nPos = scrolloffset.x;
1133 SetScrollInfo(frameHandle, SB_HORZ, &si, TRUE);
1134 }
1135
1136 // Update the cached client size
1137 GetClientRect(frameHandle, &r);
1138 client_size = Rect(r.left, r.top, r.right, r.bottom);
1139}
1140
george82858a4642007-01-27 15:32:27 +00001141void DesktopWindow::resizeDesktopWindowToBuffer() {
1142 RECT r;
1143 DWORD style = GetWindowLong(frameHandle, GWL_STYLE) & ~(WS_VSCROLL | WS_HSCROLL);
1144 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
1145
1146 // Calculate the required size of the desktop window
1147 SetRect(&r, 0, 0, buffer->width(), buffer->height());
1148 AdjustWindowRectEx(&r, style, FALSE, style_ex);
1149 if (isToolbarEnabled())
1150 r.bottom += tb.getHeight();
1151 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
1152
1153 // Set the required size, center the main window and clip to the current monitor
1154 SetWindowPos(handle, 0, 0, 0, r.right-r.left, r.bottom-r.top, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
1155 centerWindow(handle, NULL);
1156 MonitorInfo mi(getMonitor());
1157 mi.clipTo(handle);
1158
1159 // Enable/disable scrollbars as appropriate
1160 calculateScrollBars();
1161}
1162
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001163
1164void
1165DesktopWindow::setName(const char* name) {
1166 SetWindowText(handle, TStr(name));
george82e0569e42006-09-11 15:56:10 +00001167 strCopy(desktopName, name, sizeof(desktopName));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001168}
1169
1170
1171void
1172DesktopWindow::serverCutText(const char* str, int len) {
1173 CharArray t(len+1);
1174 memcpy(t.buf, str, len);
1175 t.buf[len] = 0;
1176 clipboard.setClipText(t.buf);
1177}
1178
1179
1180void DesktopWindow::fillRect(const Rect& r, Pixel pix) {
1181 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1182 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1183 buffer->fillRect(r, pix);
1184 invalidateDesktopRect(r);
1185}
1186void DesktopWindow::imageRect(const Rect& r, void* pixels) {
1187 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1188 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1189 buffer->imageRect(r, pixels);
1190 invalidateDesktopRect(r);
1191}
1192void DesktopWindow::copyRect(const Rect& r, int srcX, int srcY) {
1193 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1194 if (cursorBackingRect.overlaps(img_rect) ||
1195 cursorBackingRect.overlaps(Rect(srcX, srcY, srcX+img_rect.width(), srcY+img_rect.height())))
1196 hideLocalCursor();
1197 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1198 invalidateDesktopRect(r);
1199}
1200
1201void DesktopWindow::invertRect(const Rect& r) {
1202 int stride;
1203 rdr::U8* p = buffer->isScaling() ? buffer->getPixelsRW(buffer->calculateScaleBoundary(r), &stride)
1204 : buffer->getPixelsRW(r, &stride);
1205 for (int y = 0; y < r.height(); y++) {
1206 for (int x = 0; x < r.width(); x++) {
1207 switch (buffer->getPF().bpp) {
1208 case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break;
1209 case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break;
1210 case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break;
1211 }
1212 }
1213 }
1214 invalidateDesktopRect(r);
1215}