Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* 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> |
george82 | fd334ad | 2006-05-29 14:05:20 +0000 | [diff] [blame^] | 28 | #include <rfb_win32/MsgBox.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 29 | #include <vncviewer/DesktopWindow.h> |
| 30 | #include <vncviewer/resource.h> |
| 31 | |
| 32 | using namespace rfb; |
| 33 | using namespace rfb::win32; |
| 34 | |
| 35 | |
| 36 | // - Statics & consts |
| 37 | |
| 38 | static LogWriter vlog("DesktopWindow"); |
| 39 | |
| 40 | const int TIMER_BUMPSCROLL = 1; |
| 41 | const int TIMER_POINTER_INTERVAL = 2; |
| 42 | const 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 | |
| 52 | class DesktopWindowClass { |
| 53 | public: |
| 54 | DesktopWindowClass(); |
| 55 | ~DesktopWindowClass(); |
| 56 | ATOM classAtom; |
| 57 | HINSTANCE instance; |
| 58 | }; |
| 59 | |
| 60 | LRESULT 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); |
george82 | fd334ad | 2006-05-29 14:05:20 +0000 | [diff] [blame^] | 74 | } catch (rfb::UnsupportedPixelFormatException &e) { |
| 75 | MsgBox(0, e.str(), MB_OK); |
| 76 | _this->getCallback()->closeWindow(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 77 | } catch (rdr::Exception& e) { |
| 78 | vlog.error("untrapped: %s", e.str()); |
| 79 | } |
| 80 | |
| 81 | return result; |
| 82 | }; |
| 83 | |
| 84 | static HCURSOR dotCursor = (HCURSOR)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDC_DOT_CURSOR), IMAGE_CURSOR, 0, 0, LR_SHARED); |
| 85 | static HCURSOR arrowCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED); |
| 86 | |
| 87 | DesktopWindowClass::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 | |
| 107 | DesktopWindowClass::~DesktopWindowClass() { |
| 108 | if (classAtom) { |
| 109 | UnregisterClass((const TCHAR*)classAtom, instance); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | DesktopWindowClass baseClass; |
| 114 | |
| 115 | // |
| 116 | // -=- FrameClass |
| 117 | |
| 118 | // |
| 119 | // Window class used for child windows that display pixel data |
| 120 | // |
| 121 | |
| 122 | class FrameClass { |
| 123 | public: |
| 124 | FrameClass(); |
| 125 | ~FrameClass(); |
| 126 | ATOM classAtom; |
| 127 | HINSTANCE instance; |
| 128 | }; |
| 129 | |
| 130 | LRESULT 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 | |
| 151 | FrameClass::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 | |
| 169 | FrameClass::~FrameClass() { |
| 170 | if (classAtom) { |
| 171 | UnregisterClass((const TCHAR*)classAtom, instance); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | FrameClass frameClass; |
| 176 | |
| 177 | |
| 178 | // |
| 179 | // -=- DesktopWindow instance implementation |
| 180 | // |
| 181 | |
| 182 | DesktopWindow::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 | |
| 234 | DesktopWindow::~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 | |
| 247 | void 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 | |
| 301 | void 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 | |
| 312 | void 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 | |
| 321 | void DesktopWindow::setMonitor(const char* monitor) { |
| 322 | MonitorInfo mi(monitor); |
| 323 | mi.moveTo(handle); |
| 324 | } |
| 325 | |
| 326 | char* DesktopWindow::getMonitor() const { |
| 327 | MonitorInfo mi(handle); |
| 328 | return strDup(mi.szDevice); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | bool 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 | |
| 346 | bool 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 | |
| 374 | LRESULT |
| 375 | DesktopWindow::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, ¤t); |
| 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 | |
| 596 | LRESULT |
| 597 | DesktopWindow::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 | |
| 800 | void |
| 801 | DesktopWindow::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 | |
| 811 | void |
| 812 | DesktopWindow::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 | |
| 833 | void DesktopWindow::cursorOutsideBuffer() |
| 834 | { |
| 835 | cursorInBuffer = false; |
| 836 | hideLocalCursor(); |
| 837 | showSystemCursor(); |
| 838 | } |
| 839 | |
| 840 | void |
| 841 | DesktopWindow::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 | |
| 848 | void |
| 849 | DesktopWindow::hideSystemCursor() { |
| 850 | if (systemCursorVisible) { |
| 851 | vlog.debug("hide system cursor"); |
| 852 | systemCursorVisible = false; |
| 853 | ShowCursor(FALSE); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | void |
| 858 | DesktopWindow::showSystemCursor() { |
| 859 | if (!systemCursorVisible) { |
| 860 | vlog.debug("show system cursor"); |
| 861 | systemCursorVisible = true; |
| 862 | ShowCursor(TRUE); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | |
| 867 | bool |
| 868 | DesktopWindow::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 | |
| 880 | void |
| 881 | DesktopWindow::notifyClipboardChanged(const char* text, int len) { |
| 882 | callback->clientCutText(text, len); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | void |
| 887 | DesktopWindow::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 | |
| 899 | void |
| 900 | DesktopWindow::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 | |
| 932 | void |
| 933 | DesktopWindow::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) { |
| 934 | hideLocalCursor(); |
| 935 | |
| 936 | cursor.hotspot = hotspot; |
| 937 | |
| 938 | cursor.setSize(w, h); |
| 939 | cursor.setPF(buffer->getPF()); |
| 940 | cursor.imageRect(cursor.getRect(), data); |
| 941 | memcpy(cursor.mask.buf, mask, cursor.maskLen()); |
| 942 | cursor.crop(); |
| 943 | |
| 944 | cursorBacking.setSize(w, h); |
| 945 | cursorBacking.setPF(buffer->getPF()); |
| 946 | |
| 947 | cursorAvailable = true; |
| 948 | |
| 949 | showLocalCursor(); |
| 950 | } |
| 951 | |
| 952 | PixelFormat |
| 953 | DesktopWindow::getNativePF() const { |
| 954 | vlog.debug("getNativePF()"); |
| 955 | return WindowDC(handle).getPF(); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | void |
| 960 | DesktopWindow::refreshWindowPalette(int start, int count) { |
| 961 | vlog.debug("refreshWindowPalette(%d, %d)", start, count); |
| 962 | |
| 963 | Colour colours[256]; |
| 964 | if (count > 256) { |
| 965 | vlog.debug("%d palette entries", count); |
| 966 | throw rdr::Exception("too many palette entries"); |
| 967 | } |
| 968 | |
| 969 | // Copy the palette from the DIBSectionBuffer |
| 970 | ColourMap* cm = buffer->getColourMap(); |
| 971 | if (!cm) return; |
| 972 | for (int i=0; i<count; i++) { |
| 973 | int r, g, b; |
| 974 | cm->lookup(i, &r, &g, &b); |
| 975 | colours[i].r = r; |
| 976 | colours[i].g = g; |
| 977 | colours[i].b = b; |
| 978 | } |
| 979 | |
| 980 | // Set the window palette |
| 981 | windowPalette.setEntries(start, count, colours); |
| 982 | |
| 983 | // Cause the window to be redrawn |
| 984 | palette_changed = true; |
| 985 | InvalidateRect(handle, 0, FALSE); |
| 986 | } |
| 987 | |
| 988 | |
| 989 | void DesktopWindow::calculateScrollBars() { |
| 990 | // Calculate the required size of window |
| 991 | DWORD current_style = GetWindowLong(frameHandle, GWL_STYLE); |
| 992 | DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL); |
| 993 | DWORD style_ex = GetWindowLong(frameHandle, GWL_EXSTYLE); |
| 994 | DWORD old_style; |
| 995 | RECT r; |
| 996 | SetRect(&r, 0, 0, buffer->width(), buffer->height()); |
| 997 | AdjustWindowRectEx(&r, style, FALSE, style_ex); |
| 998 | Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom); |
| 999 | |
| 1000 | if (!bumpScroll) { |
| 1001 | // We only enable scrollbars if bump-scrolling is not active. |
| 1002 | // Effectively, this means if full-screen is not active, |
| 1003 | // but I think it's better to make these things explicit. |
| 1004 | |
| 1005 | // Work out whether scroll bars are required |
| 1006 | do { |
| 1007 | old_style = style; |
| 1008 | |
| 1009 | if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) { |
| 1010 | style |= WS_HSCROLL; |
| 1011 | reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL); |
| 1012 | } |
| 1013 | if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) { |
| 1014 | style |= WS_VSCROLL; |
| 1015 | reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL); |
| 1016 | } |
| 1017 | } while (style != old_style); |
| 1018 | } |
| 1019 | |
| 1020 | // Tell Windows to update the window style & cached settings |
| 1021 | if (style != current_style) { |
| 1022 | SetWindowLong(frameHandle, GWL_STYLE, style); |
| 1023 | SetWindowPos(frameHandle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); |
| 1024 | } |
| 1025 | |
| 1026 | // Update the scroll settings |
| 1027 | SCROLLINFO si; |
| 1028 | if (style & WS_VSCROLL) { |
| 1029 | si.cbSize = sizeof(si); |
| 1030 | si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; |
| 1031 | si.nMin = 0; |
| 1032 | si.nMax = buffer->height(); |
| 1033 | si.nPage = buffer->height() - (reqd_size.height() - window_size.height()); |
| 1034 | maxscrolloffset.y = max(0, si.nMax-si.nPage); |
| 1035 | scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y); |
| 1036 | si.nPos = scrolloffset.y; |
| 1037 | SetScrollInfo(frameHandle, SB_VERT, &si, TRUE); |
| 1038 | } |
| 1039 | if (style & WS_HSCROLL) { |
| 1040 | si.cbSize = sizeof(si); |
| 1041 | si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; |
| 1042 | si.nMin = 0; |
| 1043 | si.nMax = buffer->width(); |
| 1044 | si.nPage = buffer->width() - (reqd_size.width() - window_size.width()); |
| 1045 | maxscrolloffset.x = max(0, si.nMax-si.nPage); |
| 1046 | scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x); |
| 1047 | si.nPos = scrolloffset.x; |
| 1048 | SetScrollInfo(frameHandle, SB_HORZ, &si, TRUE); |
| 1049 | } |
| 1050 | |
| 1051 | // Update the cached client size |
| 1052 | GetClientRect(frameHandle, &r); |
| 1053 | client_size = Rect(r.left, r.top, r.right, r.bottom); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | void |
| 1058 | DesktopWindow::setName(const char* name) { |
| 1059 | SetWindowText(handle, TStr(name)); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | void |
| 1064 | DesktopWindow::serverCutText(const char* str, int len) { |
| 1065 | CharArray t(len+1); |
| 1066 | memcpy(t.buf, str, len); |
| 1067 | t.buf[len] = 0; |
| 1068 | clipboard.setClipText(t.buf); |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | void DesktopWindow::fillRect(const Rect& r, Pixel pix) { |
| 1073 | Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r; |
| 1074 | if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor(); |
| 1075 | buffer->fillRect(r, pix); |
| 1076 | invalidateDesktopRect(r); |
| 1077 | } |
| 1078 | void DesktopWindow::imageRect(const Rect& r, void* pixels) { |
| 1079 | Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r; |
| 1080 | if (cursorBackingRect.overlaps(img_rect)) hideLocalCursor(); |
| 1081 | buffer->imageRect(r, pixels); |
| 1082 | invalidateDesktopRect(r); |
| 1083 | } |
| 1084 | void DesktopWindow::copyRect(const Rect& r, int srcX, int srcY) { |
| 1085 | Rect img_rect = buffer->isScaling() ? buffer->calculateScaleBoundary(r) : r; |
| 1086 | if (cursorBackingRect.overlaps(img_rect) || |
| 1087 | cursorBackingRect.overlaps(Rect(srcX, srcY, srcX+img_rect.width(), srcY+img_rect.height()))) |
| 1088 | hideLocalCursor(); |
| 1089 | buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY)); |
| 1090 | invalidateDesktopRect(r); |
| 1091 | } |
| 1092 | |
| 1093 | void DesktopWindow::invertRect(const Rect& r) { |
| 1094 | int stride; |
| 1095 | rdr::U8* p = buffer->isScaling() ? buffer->getPixelsRW(buffer->calculateScaleBoundary(r), &stride) |
| 1096 | : buffer->getPixelsRW(r, &stride); |
| 1097 | for (int y = 0; y < r.height(); y++) { |
| 1098 | for (int x = 0; x < r.width(); x++) { |
| 1099 | switch (buffer->getPF().bpp) { |
| 1100 | case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break; |
| 1101 | case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break; |
| 1102 | case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | invalidateDesktopRect(r); |
| 1107 | } |