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 | // -=- 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 | |
| 28 | using namespace rfb; |
| 29 | using namespace rfb::win32; |
| 30 | |
| 31 | static LogWriter vlog("MsgWindow"); |
| 32 | |
| 33 | // |
| 34 | // -=- MsgWindowClass |
| 35 | // |
| 36 | |
| 37 | class MsgWindowClass { |
| 38 | public: |
| 39 | MsgWindowClass(); |
| 40 | ~MsgWindowClass(); |
| 41 | ATOM classAtom; |
| 42 | HINSTANCE instance; |
| 43 | }; |
| 44 | |
| 45 | LRESULT 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 | |
| 67 | MsgWindowClass::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 | |
| 85 | MsgWindowClass::~MsgWindowClass() { |
| 86 | if (classAtom) { |
| 87 | UnregisterClass((const TCHAR*)classAtom, instance); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | MsgWindowClass baseClass; |
| 92 | |
| 93 | // |
| 94 | // -=- MsgWindow |
| 95 | // |
| 96 | |
| 97 | MsgWindow::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 | |
| 107 | MsgWindow::~MsgWindow() { |
| 108 | if (handle) |
| 109 | DestroyWindow(handle); |
| 110 | vlog.debug("destroyed window \"%s\" (%x)", (const char*)CStr(name.buf), handle); |
| 111 | } |
| 112 | |
| 113 | LRESULT |
| 114 | MsgWindow::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) { |
| 115 | return SafeDefWindowProc(getHandle(), msg, wParam, lParam); |
| 116 | } |