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