You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MsgWindow.cxx 3.5KB

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