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

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