blob: 95bd52375ea1c3cb3d70cc05238a826205f72ac9 [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// -=- MsgWindow.cxx
20
21#include <rfb_win32/MsgWindow.h>
22#include <rfb_win32/WMShatter.h>
23#include <rfb/LogWriter.h>
24#include <rdr/Exception.h>
25#include <malloc.h>
26#include <tchar.h>
27
28using namespace rfb;
29using namespace rfb::win32;
30
31static LogWriter vlog("MsgWindow");
32
33//
34// -=- MsgWindowClass
35//
36
37class MsgWindowClass {
38public:
39 MsgWindowClass();
40 ~MsgWindowClass();
41 ATOM classAtom;
42 HINSTANCE instance;
43};
44
45LRESULT CALLBACK MsgWindowProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
46 LRESULT result;
47
48 if (msg == WM_CREATE)
49 SetWindowLong(wnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
50 else if (msg == WM_DESTROY)
51 SetWindowLong(wnd, GWL_USERDATA, 0);
52 MsgWindow* _this = (MsgWindow*) GetWindowLong(wnd, GWL_USERDATA);
53 if (!_this) {
54 vlog.info("null _this in %x, message %x", wnd, msg);
55 return SafeDefWindowProc(wnd, msg, wParam, lParam);
56 }
57
58 try {
59 result = _this->processMessage(msg, wParam, lParam);
60 } catch (rdr::Exception& e) {
61 vlog.error("untrapped: %s", e.str());
62 }
63
64 return result;
65};
66
67MsgWindowClass::MsgWindowClass() : classAtom(0) {
68 WNDCLASS wndClass;
69 wndClass.style = 0;
70 wndClass.lpfnWndProc = MsgWindowProc;
71 wndClass.cbClsExtra = 0;
72 wndClass.cbWndExtra = 0;
73 wndClass.hInstance = instance = GetModuleHandle(0);
74 wndClass.hIcon = 0;
75 wndClass.hCursor = 0;
76 wndClass.hbrBackground = 0;
77 wndClass.lpszMenuName = 0;
78 wndClass.lpszClassName = _T("rfb::win32::MsgWindowClass");
79 classAtom = RegisterClass(&wndClass);
80 if (!classAtom) {
81 throw rdr::SystemException("unable to register MsgWindow window class", GetLastError());
82 }
83}
84
85MsgWindowClass::~MsgWindowClass() {
86 if (classAtom) {
87 UnregisterClass((const TCHAR*)classAtom, instance);
88 }
89}
90
91MsgWindowClass baseClass;
92
93//
94// -=- MsgWindow
95//
96
97MsgWindow::MsgWindow(const TCHAR* name_) : name(tstrDup(name_)), handle(0) {
98 vlog.debug("creating window \"%s\"", (const char*)CStr(name.buf));
99 handle = CreateWindow((const TCHAR*)baseClass.classAtom, name.buf, WS_OVERLAPPED,
100 0, 0, 10, 10, 0, 0, baseClass.instance, this);
101 if (!handle) {
102 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
103 }
104 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name.buf), handle);
105}
106
107MsgWindow::~MsgWindow() {
108 if (handle)
109 DestroyWindow(handle);
110 vlog.debug("destroyed window \"%s\" (%x)", (const char*)CStr(name.buf), handle);
111}
112
113LRESULT
114MsgWindow::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
115 return SafeDefWindowProc(getHandle(), msg, wParam, lParam);
116}