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.4KB

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