blob: 55000252c307e2c4eef7c21ead34bdf0ec2593b9 [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),
184 showToolbar(false),
185 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);
273 SetWindowPos(handle, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
274 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;
304
305 if (showToolbar && !tb.isVisible() && !fullscreenActive) {
306 tb.show();
307 } else if (!showToolbar && tb.isVisible()) {
308 tb.hide();
309 }
310}
311
312void DesktopWindow::setDisableWinKeys(bool dwk) {
313 // Enable low-level event hooking, so we get special keys directly
314 if (dwk)
315 enableLowLevelKeyEvents(handle);
316 else
317 disableLowLevelKeyEvents(handle);
318}
319
320
321void DesktopWindow::setMonitor(const char* monitor) {
322 MonitorInfo mi(monitor);
323 mi.moveTo(handle);
324}
325
326char* DesktopWindow::getMonitor() const {
327 MonitorInfo mi(handle);
328 return strDup(mi.szDevice);
329}
330
331
332bool DesktopWindow::setViewportOffset(const Point& tl) {
333 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
334 max(0, min(tl.y, buffer->height()-client_size.height())));
335 Point delta = np.translate(scrolloffset.negate());
336 if (!np.equals(scrolloffset)) {
337 scrolloffset = np;
338 ScrollWindowEx(frameHandle, -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
339 UpdateWindow(frameHandle);
340 return true;
341 }
342 return false;
343}
344
345
346bool DesktopWindow::processBumpScroll(const Point& pos)
347{
348 if (!bumpScroll) return false;
349 int bumpScrollPixels = 20;
350 bumpScrollDelta = Point();
351
352 if (pos.x == client_size.width()-1)
353 bumpScrollDelta.x = bumpScrollPixels;
354 else if (pos.x == 0)
355 bumpScrollDelta.x = -bumpScrollPixels;
356 if (pos.y == client_size.height()-1)
357 bumpScrollDelta.y = bumpScrollPixels;
358 else if (pos.y == 0)
359 bumpScrollDelta.y = -bumpScrollPixels;
360
361 if (bumpScrollDelta.x || bumpScrollDelta.y) {
362 if (bumpScrollTimer.isActive()) return true;
363 if (setViewportOffset(scrolloffset.translate(bumpScrollDelta))) {
364 bumpScrollTimer.start(25);
365 return true;
366 }
367 }
368
369 bumpScrollTimer.stop();
370 return false;
371}
372
373
374LRESULT
375DesktopWindow::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
376 switch (msg) {
377
378 // -=- Process standard window messages
379
380 case WM_NOTIFY:
381 if (wParam == ID_TOOLBAR)
382 tb.processWM_NOTIFY(wParam, lParam);
383 break;
384
385 case WM_DISPLAYCHANGE:
386 // Display format has changed - notify callback
387 callback->displayChanged();
388 break;
389
390 // -=- Window position
391
392 // Prevent the window from being resized to be too large if in normal mode.
393 // If maximized or fullscreen the allow oversized windows.
394
395 case WM_WINDOWPOSCHANGING:
396 {
397 WINDOWPOS* wpos = (WINDOWPOS*)lParam;
398 if (wpos->flags & SWP_NOSIZE)
399 break;
400
401 // Work out how big the window should ideally be
402 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
403 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
404 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
405
406 RECT r;
407 SetRect(&r, 0, 0, buffer->width(), buffer->height());
408 AdjustWindowRectEx(&r, style, FALSE, style_ex);
409 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
410 if (current_style & WS_VSCROLL)
411 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
412 if (current_style & WS_HSCROLL)
413 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
414
415 SetRect(&r, reqd_size.tl.x, reqd_size.tl.y, reqd_size.br.x, reqd_size.br.y);
416 if (tb.isVisible())
417 r.bottom += tb.getHeight();
418 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
419 reqd_size = Rect(r.left, r.top, r.right, r.bottom);
420
421 RECT current;
422 GetWindowRect(handle, &current);
423
424 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
425 // Ensure that the window isn't resized too large
426 if (wpos->cx > reqd_size.width()) {
427 wpos->cx = reqd_size.width();
428 wpos->x = current.left;
429 }
430 if (wpos->cy > reqd_size.height()) {
431 wpos->cy = reqd_size.height();
432 wpos->y = current.top;
433 }
434 }
435 }
436 break;
437
438 // Resize child windows and update window size info we have cached.
439
440 case WM_SIZE:
441 {
442 Point old_offset = desktopToClient(Point(0, 0));
443 RECT r;
444
445 // Resize child windows
446 GetClientRect(handle, &r);
447 if (tb.isVisible()) {
448 MoveWindow(frameHandle, 0, tb.getHeight(),
449 r.right, r.bottom - tb.getHeight(), TRUE);
450 } else {
451 MoveWindow(frameHandle, 0, 0, r.right, r.bottom, TRUE);
452 }
453 tb.autoSize();
454
455 // Update the cached sizing information
456 GetWindowRect(frameHandle, &r);
457 window_size = Rect(r.left, r.top, r.right, r.bottom);
458 GetClientRect(frameHandle, &r);
459 client_size = Rect(r.left, r.top, r.right, r.bottom);
460
461 // Determine whether scrollbars are required
462 calculateScrollBars();
463
464 // Redraw if required
465 if ((!old_offset.equals(desktopToClient(Point(0, 0)))))
466 InvalidateRect(frameHandle, 0, TRUE);
467 }
468 break;
469
470 // -=- Bump-scrolling
471
472 case WM_TIMER:
473 switch (wParam) {
474 case TIMER_BUMPSCROLL:
475 if (!setViewportOffset(scrolloffset.translate(bumpScrollDelta)))
476 bumpScrollTimer.stop();
477 break;
478 case TIMER_POINTER_INTERVAL:
479 case TIMER_POINTER_3BUTTON:
480 ptr.handleTimer(callback, wParam);
481 break;
482 }
483 break;
484
485 // -=- Track whether or not the window has focus
486
487 case WM_SETFOCUS:
488 has_focus = true;
489 break;
490 case WM_KILLFOCUS:
491 has_focus = false;
492 cursorOutsideBuffer();
493 // Restore the keyboard to a consistent state
494 kbd.releaseAllKeys(callback);
495 break;
496
497 // -=- If the menu is about to be shown, make sure it's up to date
498
499 case WM_INITMENU:
500 callback->refreshMenu(true);
501 break;
502
503 // -=- Handle the extra window menu items
504
505 // Pass system menu messages to the callback and only attempt
506 // to process them ourselves if the callback returns false.
507 case WM_SYSCOMMAND:
508 // Call the supplied callback
509 if (callback->sysCommand(wParam, lParam))
510 break;
511
512 // - Not processed by the callback, so process it as a system message
513 switch (wParam & 0xfff0) {
514
515 // When restored, ensure that full-screen mode is re-enabled if required.
516 case SC_RESTORE:
517 {
518 if (GetWindowLong(handle, GWL_STYLE) & WS_MINIMIZE) {
519 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
520 setFullscreen(fullscreenRestore);
521 }
522 else if (fullscreenActive)
523 setFullscreen(false);
524 else
525 rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
526
527 return 0;
528 }
529
530 // If we are maximized or minimized then that cancels full-screen mode.
531 case SC_MINIMIZE:
532 case SC_MAXIMIZE:
533 fullscreenRestore = fullscreenActive;
534 setFullscreen(false);
535 break;
536
537 }
538 break;
539
540 // Treat all menu commands as system menu commands
541 case WM_COMMAND:
542 SendMessage(handle, WM_SYSCOMMAND, wParam, lParam);
543 return 0;
544
545 // -=- Handle keyboard input
546
547 case WM_KEYUP:
548 case WM_KEYDOWN:
549 // Hook the MenuKey to pop-up the window menu
550 if (menuKey && (wParam == menuKey)) {
551
552 bool ctrlDown = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0;
553 bool altDown = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0;
554 bool shiftDown = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
555 if (!(ctrlDown || altDown || shiftDown)) {
556
557 // If MenuKey is being released then pop-up the menu
558 if ((msg == WM_KEYDOWN)) {
559 // Make sure it's up to date
560 //
561 // NOTE: Here we call refreshMenu only to grey out Move and Size
562 // menu items. Other things will be refreshed once again
563 // while processing the WM_INITMENU message.
564 //
565 callback->refreshMenu(false);
566
567 // Show it under the pointer
568 POINT pt;
569 GetCursorPos(&pt);
570 cursorInBuffer = false;
571 TrackPopupMenu(GetSystemMenu(handle, FALSE),
572 TPM_CENTERALIGN | TPM_VCENTERALIGN, pt.x, pt.y, 0, handle, 0);
573 }
574
575 // Ignore the MenuKey keypress for both press & release events
576 return 0;
577 }
578 }
579 case WM_SYSKEYDOWN:
580 case WM_SYSKEYUP:
581 kbd.keyEvent(callback, wParam, lParam, (msg == WM_KEYDOWN) || (msg == WM_SYSKEYDOWN));
582 return 0;
583
584 // -=- Handle the window closing
585
586 case WM_CLOSE:
587 vlog.debug("WM_CLOSE %x", handle);
588 callback->closeWindow();
589 break;
590
591 }
592
593 return rfb::win32::SafeDefWindowProc(handle, msg, wParam, lParam);
594}
595
596LRESULT
597DesktopWindow::processFrameMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
598 switch (msg) {
599
600 // -=- Paint the remote frame buffer
601
602 case WM_PAINT:
603 {
604 PAINTSTRUCT ps;
605 HDC paintDC = BeginPaint(frameHandle, &ps);
606 if (!paintDC)
607 throw rdr::SystemException("unable to BeginPaint", GetLastError());
608 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
609
610 if (!pr.is_empty()) {
611
612 // Draw using the correct palette
613 PaletteSelector pSel(paintDC, windowPalette.getHandle());
614
615 if (buffer->bitmap) {
616 // Update the bitmap's palette
617 if (palette_changed) {
618 palette_changed = false;
619 buffer->refreshPalette();
620 }
621
622 // Get device context
623 BitmapDC bitmapDC(paintDC, buffer->bitmap);
624
625 // Blit the border if required
626 Rect bufpos = desktopToClient(buffer->getRect());
627 if (!pr.enclosed_by(bufpos)) {
628 vlog.debug("draw border");
629 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
630 RECT r;
631 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
632 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
633 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
634 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
635 }
636
637 // Do the blit
638 Point buf_pos = clientToDesktop(pr.tl);
639
640 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
641 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
642 throw rdr::SystemException("unable to BitBlt to window", GetLastError());
643 }
644 }
645
646 EndPaint(frameHandle, &ps);
647
648 // - Notify the callback that a paint message has finished processing
649 callback->paintCompleted();
650 }
651 return 0;
652
653 // -=- Palette management
654
655 case WM_PALETTECHANGED:
656 vlog.debug("WM_PALETTECHANGED");
657 if ((HWND)wParam == frameHandle) {
658 vlog.debug("ignoring");
659 break;
660 }
661 case WM_QUERYNEWPALETTE:
662 vlog.debug("re-selecting palette");
663 {
664 WindowDC wdc(frameHandle);
665 PaletteSelector pSel(wdc, windowPalette.getHandle());
666 if (pSel.isRedrawRequired()) {
667 InvalidateRect(frameHandle, 0, FALSE);
668 UpdateWindow(frameHandle);
669 }
670 }
671 return TRUE;
672
673 case WM_VSCROLL:
674 case WM_HSCROLL:
675 {
676 Point delta;
677 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
678
679 switch (LOWORD(wParam)) {
680 case SB_PAGEUP: newpos -= 50; break;
681 case SB_PAGEDOWN: newpos += 50; break;
682 case SB_LINEUP: newpos -= 5; break;
683 case SB_LINEDOWN: newpos += 5; break;
684 case SB_THUMBTRACK:
685 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
686 default: vlog.info("received unknown scroll message");
687 };
688
689 if (msg == WM_HSCROLL)
690 setViewportOffset(Point(newpos, scrolloffset.y));
691 else
692 setViewportOffset(Point(scrolloffset.x, newpos));
693
694 SCROLLINFO si;
695 si.cbSize = sizeof(si);
696 si.fMask = SIF_POS;
697 si.nPos = newpos;
698 SetScrollInfo(frameHandle, (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
699 }
700 break;
701
702 // -=- Cursor shape/visibility handling
703
704 case WM_SETCURSOR:
705 if (LOWORD(lParam) != HTCLIENT)
706 break;
707 SetCursor(cursorInBuffer ? dotCursor : arrowCursor);
708 return TRUE;
709
710 case WM_MOUSELEAVE:
711 trackingMouseLeave = false;
712 cursorOutsideBuffer();
713 return 0;
714
715 // -=- Mouse input handling
716
717 case WM_MOUSEMOVE:
718 case WM_LBUTTONUP:
719 case WM_MBUTTONUP:
720 case WM_RBUTTONUP:
721 case WM_LBUTTONDOWN:
722 case WM_MBUTTONDOWN:
723 case WM_RBUTTONDOWN:
724#ifdef WM_MOUSEWHEEL
725 case WM_MOUSEWHEEL:
726#endif
727 if (has_focus)
728 {
729 if (!trackingMouseLeave) {
730 TRACKMOUSEEVENT tme;
731 tme.cbSize = sizeof(TRACKMOUSEEVENT);
732 tme.dwFlags = TME_LEAVE;
733 tme.hwndTrack = frameHandle;
734 _TrackMouseEvent(&tme);
735 trackingMouseLeave = true;
736 }
737 int mask = 0;
738 if (LOWORD(wParam) & MK_LBUTTON) mask |= 1;
739 if (LOWORD(wParam) & MK_MBUTTON) mask |= 2;
740 if (LOWORD(wParam) & MK_RBUTTON) mask |= 4;
741
742#ifdef WM_MOUSEWHEEL
743 if (msg == WM_MOUSEWHEEL) {
744 int delta = (short)HIWORD(wParam);
745 int repeats = (abs(delta)+119) / 120;
746 int wheelMask = (delta > 0) ? 8 : 16;
747 vlog.debug("repeats %d, mask %d\n",repeats,wheelMask);
748 for (int i=0; i<repeats; i++) {
749 ptr.pointerEvent(callback, oldpos, mask | wheelMask);
750 ptr.pointerEvent(callback, oldpos, mask);
751 }
752 } else {
753#endif
754 Point clientPos = Point(LOWORD(lParam), HIWORD(lParam));
755 Point p = clientToDesktop(clientPos);
756
757 // If the mouse is not within the server buffer area, do nothing
758 cursorInBuffer = buffer->getRect().contains(p);
759 if (!cursorInBuffer) {
760 cursorOutsideBuffer();
761 break;
762 }
763
764 // If we're locally rendering the cursor then redraw it
765 if (cursorAvailable) {
766 // - Render the cursor!
767 if (!p.equals(cursorPos)) {
768 hideLocalCursor();
769 cursorPos = p;
770 showLocalCursor();
771 if (cursorVisible)
772 hideSystemCursor();
773 }
774 }
775
776 // If we are doing bump-scrolling then try that first...
777 if (processBumpScroll(clientPos))
778 break;
779
780 // Send a pointer event to the server
781 oldpos = p;
782 if (buffer->isScaling()) {
783 p.x /= double(buffer->getScale()) / 100.0;
784 p.y /= double(buffer->getScale()) / 100.0;
785 }
786 ptr.pointerEvent(callback, p, mask);
787#ifdef WM_MOUSEWHEEL
788 }
789#endif
790 } else {
791 cursorOutsideBuffer();
792 }
793 break;
794 }
795
796 return rfb::win32::SafeDefWindowProc(frameHandle, msg, wParam, lParam);
797}
798
799
800void
801DesktopWindow::hideLocalCursor() {
802 // - Blit the cursor backing store over the cursor
803 // *** ALWAYS call this BEFORE changing buffer PF!!!
804 if (cursorVisible) {
805 cursorVisible = false;
806 buffer->DIBSectionBuffer::imageRect(cursorBackingRect, cursorBacking.data);
807 invalidateDesktopRect(cursorBackingRect, false);
808 }
809}
810
811void
812DesktopWindow::showLocalCursor() {
813 if (cursorAvailable && !cursorVisible && cursorInBuffer) {
814 if (!buffer->getPF().equal(cursor.getPF()) ||
815 cursor.getRect().is_empty()) {
816 vlog.info("attempting to render invalid local cursor");
817 cursorAvailable = false;
818 showSystemCursor();
819 return;
820 }
821 cursorVisible = true;
822
823 cursorBackingRect = cursor.getRect().translate(cursorPos).translate(cursor.hotspot.negate());
824 cursorBackingRect = cursorBackingRect.intersect(buffer->getRect());
825 buffer->getImage(cursorBacking.data, cursorBackingRect);
826
827 renderLocalCursor();
828
829 invalidateDesktopRect(cursorBackingRect, false);
830 }
831}
832
833void DesktopWindow::cursorOutsideBuffer()
834{
835 cursorInBuffer = false;
836 hideLocalCursor();
837 showSystemCursor();
838}
839
840void
841DesktopWindow::renderLocalCursor()
842{
843 Rect r = cursor.getRect();
844 r = r.translate(cursorPos).translate(cursor.hotspot.negate());
845 buffer->DIBSectionBuffer::maskRect(r, cursor.data, cursor.mask.buf);
846}
847
848void
849DesktopWindow::hideSystemCursor() {
850 if (systemCursorVisible) {
851 vlog.debug("hide system cursor");
852 systemCursorVisible = false;
853 ShowCursor(FALSE);
854 }
855}
856
857void
858DesktopWindow::showSystemCursor() {
859 if (!systemCursorVisible) {
860 vlog.debug("show system cursor");
861 systemCursorVisible = true;
862 ShowCursor(TRUE);
863 }
864}
865
866
867bool
868DesktopWindow::invalidateDesktopRect(const Rect& crect, bool scaling) {
869 Rect rect;
870 if (buffer->isScaling() && scaling) {
871 rect = desktopToClient(buffer->calculateScaleBoundary(crect));
872 } else rect = desktopToClient(crect);
873 if (rect.intersect(client_size).is_empty()) return false;
874 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
875 InvalidateRect(frameHandle, &invalid, FALSE);
876 return true;
877}
878
879
880void
881DesktopWindow::notifyClipboardChanged(const char* text, int len) {
882 callback->clientCutText(text, len);
883}
884
885
886void
887DesktopWindow::setPF(const PixelFormat& pf) {
888 // If the cursor is the wrong format then clear it
889 if (!pf.equal(buffer->getPF()))
890 setCursor(0, 0, Point(), 0, 0);
891
892 // Update the desktop buffer
893 buffer->setPF(pf);
894
895 // Redraw the window
896 InvalidateRect(frameHandle, 0, FALSE);
897}
898
899void
900DesktopWindow::setSize(int w, int h) {
901 vlog.debug("setSize %dx%d", w, h);
902
903 // If the locally-rendered cursor is visible then remove it
904 hideLocalCursor();
905
906 // Resize the backing buffer
907 buffer->setSize(w, h);
908
909 // If the window is not maximised or full-screen then resize it
910 if (!(GetWindowLong(handle, GWL_STYLE) & WS_MAXIMIZE) && !fullscreenActive) {
911 // Resize the window to the required size
912 RECT r = {0, 0, w, h};
913 AdjustWindowRectEx(&r, GetWindowLong(frameHandle, GWL_STYLE), FALSE,
914 GetWindowLong(frameHandle, GWL_EXSTYLE));
915 if (tb.isVisible())
916 r.bottom += tb.getHeight();
917 AdjustWindowRect(&r, GetWindowLong(handle, GWL_STYLE), FALSE);
918
919 // Resize about the center of the window, and clip to current monitor
920 MonitorInfo mi(handle);
921 resizeWindow(handle, r.right-r.left, r.bottom-r.top);
922 mi.clipTo(handle);
923 } else {
924 // Ensure the screen contents are consistent
925 InvalidateRect(frameHandle, 0, FALSE);
926 }
927
928 // Enable/disable scrollbars as appropriate
929 calculateScrollBars();
930}
931
george8204a77712006-05-29 14:18:14 +0000932void DesktopWindow::setDesktopScale(int scale) {
933 buffer->setScale(scale);
934 InvalidateRect(frameHandle, 0, FALSE);
935 calculateScrollBars();
936}
937
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000938void
939DesktopWindow::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) {
940 hideLocalCursor();
941
942 cursor.hotspot = hotspot;
943
944 cursor.setSize(w, h);
945 cursor.setPF(buffer->getPF());
946 cursor.imageRect(cursor.getRect(), data);
947 memcpy(cursor.mask.buf, mask, cursor.maskLen());
948 cursor.crop();
949
950 cursorBacking.setSize(w, h);
951 cursorBacking.setPF(buffer->getPF());
952
953 cursorAvailable = true;
954
955 showLocalCursor();
956}
957
958PixelFormat
959DesktopWindow::getNativePF() const {
960 vlog.debug("getNativePF()");
961 return WindowDC(handle).getPF();
962}
963
964
965void
966DesktopWindow::refreshWindowPalette(int start, int count) {
967 vlog.debug("refreshWindowPalette(%d, %d)", start, count);
968
969 Colour colours[256];
970 if (count > 256) {
971 vlog.debug("%d palette entries", count);
972 throw rdr::Exception("too many palette entries");
973 }
974
975 // Copy the palette from the DIBSectionBuffer
976 ColourMap* cm = buffer->getColourMap();
977 if (!cm) return;
978 for (int i=0; i<count; i++) {
979 int r, g, b;
980 cm->lookup(i, &r, &g, &b);
981 colours[i].r = r;
982 colours[i].g = g;
983 colours[i].b = b;
984 }
985
986 // Set the window palette
987 windowPalette.setEntries(start, count, colours);
988
989 // Cause the window to be redrawn
990 palette_changed = true;
991 InvalidateRect(handle, 0, FALSE);
992}
993
994
995void DesktopWindow::calculateScrollBars() {
996 // Calculate the required size of window
997 DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE);
998 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
999 DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE);
1000 DWORD old_style;
1001 RECT r;
1002 SetRect(&r, 0, 0, buffer->width(), buffer->height());
1003 AdjustWindowRectEx(&r, style, FALSE, style_ex);
1004 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
1005
1006 if (!bumpScroll) {
1007 // We only enable scrollbars if bump-scrolling is not active.
1008 // Effectively, this means if full-screen is not active,
1009 // but I think it's better to make these things explicit.
1010
1011 // Work out whether scroll bars are required
1012 do {
1013 old_style = style;
1014
1015 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
1016 style |= WS_HSCROLL;
1017 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
1018 }
1019 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
1020 style |= WS_VSCROLL;
1021 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
1022 }
1023 } while (style != old_style);
1024 }
1025
1026 // Tell Windows to update the window style & cached settings
1027 if (style != current_style) {
1028 SetWindowLong(frameHandle, GWL_STYLE, style);
1029 SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1030 }
1031
1032 // Update the scroll settings
1033 SCROLLINFO si;
1034 if (style & WS_VSCROLL) {
1035 si.cbSize = sizeof(si);
1036 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1037 si.nMin = 0;
1038 si.nMax = buffer->height();
1039 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
1040 maxscrolloffset.y = max(0, si.nMax-si.nPage);
1041 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
1042 si.nPos = scrolloffset.y;
1043 SetScrollInfo(frameHandle, SB_VERT, &si, TRUE);
1044 }
1045 if (style & WS_HSCROLL) {
1046 si.cbSize = sizeof(si);
1047 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1048 si.nMin = 0;
1049 si.nMax = buffer->width();
1050 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
1051 maxscrolloffset.x = max(0, si.nMax-si.nPage);
1052 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
1053 si.nPos = scrolloffset.x;
1054 SetScrollInfo(frameHandle, SB_HORZ, &si, TRUE);
1055 }
1056
1057 // Update the cached client size
1058 GetClientRect(frameHandle, &r);
1059 client_size = Rect(r.left, r.top, r.right, r.bottom);
1060}
1061
1062
1063void
1064DesktopWindow::setName(const char* name) {
1065 SetWindowText(handle, TStr(name));
1066}
1067
1068
1069void
1070DesktopWindow::serverCutText(const char* str, int len) {
1071 CharArray t(len+1);
1072 memcpy(t.buf, str, len);
1073 t.buf[len] = 0;
1074 clipboard.setClipText(t.buf);
1075}
1076
1077
1078void DesktopWindow::fillRect(const Rect& r, Pixel pix) {
1079 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1080 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1081 buffer->fillRect(r, pix);
1082 invalidateDesktopRect(r);
1083}
1084void DesktopWindow::imageRect(const Rect& r, void* pixels) {
1085 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1086 if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor();
1087 buffer->imageRect(r, pixels);
1088 invalidateDesktopRect(r);
1089}
1090void DesktopWindow::copyRect(const Rect& r, int srcX, int srcY) {
1091 Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r;
1092 if (cursorBackingRect.overlaps(img_rect) ||
1093 cursorBackingRect.overlaps(Rect(srcX, srcY, srcX+img_rect.width(), srcY+img_rect.height())))
1094 hideLocalCursor();
1095 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1096 invalidateDesktopRect(r);
1097}
1098
1099void DesktopWindow::invertRect(const Rect& r) {
1100 int stride;
1101 rdr::U8* p = buffer->isScaling() ? buffer->getPixelsRW(buffer->calculateScaleBoundary(r), &stride)
1102 : buffer->getPixelsRW(r, &stride);
1103 for (int y = 0; y < r.height(); y++) {
1104 for (int x = 0; x < r.width(); x++) {
1105 switch (buffer->getPF().bpp) {
1106 case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break;
1107 case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break;
1108 case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break;
1109 }
1110 }
1111 }
1112 invalidateDesktopRect(r);
1113}