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