The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

113 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_WINDOWS
  18. namespace juce
  19. {
  20. // This function is in juce_win32_Windowing.cpp
  21. extern bool offerKeyMessageToJUCEWindow (MSG&);
  22. }
  23. namespace
  24. {
  25. static HHOOK mouseWheelHook = 0, keyboardHook = 0;
  26. static int numHookUsers = 0;
  27. static bool keyboardHookReentrant = false;
  28. struct WindowsHooks
  29. {
  30. WindowsHooks()
  31. {
  32. if (numHookUsers++ == 0)
  33. {
  34. mouseWheelHook = SetWindowsHookEx (WH_MOUSE, mouseWheelHookCallback,
  35. (HINSTANCE) Process::getCurrentModuleInstanceHandle(),
  36. GetCurrentThreadId());
  37. keyboardHook = SetWindowsHookEx (WH_GETMESSAGE, keyboardHookCallback,
  38. (HINSTANCE) Process::getCurrentModuleInstanceHandle(),
  39. GetCurrentThreadId());
  40. }
  41. }
  42. ~WindowsHooks()
  43. {
  44. if (--numHookUsers == 0)
  45. {
  46. if (mouseWheelHook != 0)
  47. {
  48. UnhookWindowsHookEx (mouseWheelHook);
  49. mouseWheelHook = 0;
  50. }
  51. if (keyboardHook != 0)
  52. {
  53. UnhookWindowsHookEx (keyboardHook);
  54. keyboardHook = 0;
  55. }
  56. }
  57. }
  58. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  59. {
  60. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  61. {
  62. // using a local copy of this struct to support old mingw libraries
  63. struct MOUSEHOOKSTRUCTEX_ : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  64. const MOUSEHOOKSTRUCTEX_& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  65. if (Component* const comp = Desktop::getInstance().findComponentAt (Point<int> (hs.pt.x, hs.pt.y)))
  66. if (comp->getWindowHandle() != 0)
  67. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  68. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  69. }
  70. return CallNextHookEx (mouseWheelHook, nCode, wParam, lParam);
  71. }
  72. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  73. {
  74. if (keyboardHookReentrant)
  75. return 1;
  76. ScopedValueSetter<bool> setter (keyboardHookReentrant, true, false);
  77. MSG& msg = *(MSG*) lParam;
  78. if (nCode == HC_ACTION && offerKeyMessageToJUCEWindow (msg))
  79. {
  80. zerostruct (msg);
  81. msg.message = WM_USER;
  82. return 1;
  83. }
  84. return CallNextHookEx (keyboardHook, nCode, wParam, lParam);
  85. }
  86. };
  87. }
  88. #endif