blob: 4c50c2fdaf6db8802c2a6369e9df55c05b97091f [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRCc75dc442010-05-20 07:44:49 +00002 * Copyright (C) 2010 D. R. Commander. All Rights Reserved.
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20// -=- MsgWindow.cxx
21
22#include <rfb_win32/MsgWindow.h>
23#include <rfb_win32/WMShatter.h>
24#include <rfb/LogWriter.h>
25#include <rdr/Exception.h>
26#include <malloc.h>
27#include <tchar.h>
28
29using namespace rfb;
30using namespace rfb::win32;
31
32static LogWriter vlog("MsgWindow");
33
34//
35// -=- MsgWindowClass
36//
37
38class MsgWindowClass {
39public:
40 MsgWindowClass();
41 ~MsgWindowClass();
42 ATOM classAtom;
43 HINSTANCE instance;
44};
45
46LRESULT CALLBACK MsgWindowProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
Peter Åstrand7c0e2292008-12-10 10:49:58 +000047 LRESULT result = 0;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000048
49 if (msg == WM_CREATE)
DRCc75dc442010-05-20 07:44:49 +000050 SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR)((CREATESTRUCT*)lParam)->lpCreateParams);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000051 else if (msg == WM_DESTROY)
DRCc75dc442010-05-20 07:44:49 +000052 SetWindowLongPtr(wnd, GWLP_USERDATA, 0);
53 MsgWindow* _this = (MsgWindow*) GetWindowLongPtr(wnd, GWLP_USERDATA);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000054 if (!_this) {
55 vlog.info("null _this in %x, message %x", wnd, msg);
56 return SafeDefWindowProc(wnd, msg, wParam, lParam);
57 }
58
59 try {
60 result = _this->processMessage(msg, wParam, lParam);
61 } catch (rdr::Exception& e) {
62 vlog.error("untrapped: %s", e.str());
63 }
64
65 return result;
66};
67
68MsgWindowClass::MsgWindowClass() : classAtom(0) {
69 WNDCLASS wndClass;
70 wndClass.style = 0;
71 wndClass.lpfnWndProc = MsgWindowProc;
72 wndClass.cbClsExtra = 0;
73 wndClass.cbWndExtra = 0;
74 wndClass.hInstance = instance = GetModuleHandle(0);
75 wndClass.hIcon = 0;
76 wndClass.hCursor = 0;
77 wndClass.hbrBackground = 0;
78 wndClass.lpszMenuName = 0;
79 wndClass.lpszClassName = _T("rfb::win32::MsgWindowClass");
80 classAtom = RegisterClass(&wndClass);
81 if (!classAtom) {
82 throw rdr::SystemException("unable to register MsgWindow window class", GetLastError());
83 }
84}
85
86MsgWindowClass::~MsgWindowClass() {
87 if (classAtom) {
88 UnregisterClass((const TCHAR*)classAtom, instance);
89 }
90}
91
Peter Åstrand2a448992008-12-09 10:42:56 +000092static MsgWindowClass baseClass;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000093
94//
95// -=- MsgWindow
96//
97
98MsgWindow::MsgWindow(const TCHAR* name_) : name(tstrDup(name_)), handle(0) {
99 vlog.debug("creating window \"%s\"", (const char*)CStr(name.buf));
100 handle = CreateWindow((const TCHAR*)baseClass.classAtom, name.buf, WS_OVERLAPPED,
101 0, 0, 10, 10, 0, 0, baseClass.instance, this);
102 if (!handle) {
103 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
104 }
105 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name.buf), handle);
106}
107
108MsgWindow::~MsgWindow() {
109 if (handle)
110 DestroyWindow(handle);
111 vlog.debug("destroyed window \"%s\" (%x)", (const char*)CStr(name.buf), handle);
112}
113
114LRESULT
115MsgWindow::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
116 return SafeDefWindowProc(getHandle(), msg, wParam, lParam);
117}