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.

108 lines
3.7KB

  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. struct WindowsHooks
  28. {
  29. WindowsHooks()
  30. {
  31. if (numHookUsers++ == 0)
  32. {
  33. mouseWheelHook = SetWindowsHookEx (WH_MOUSE, mouseWheelHookCallback,
  34. (HINSTANCE) Process::getCurrentModuleInstanceHandle(),
  35. GetCurrentThreadId());
  36. keyboardHook = SetWindowsHookEx (WH_GETMESSAGE, keyboardHookCallback,
  37. (HINSTANCE) Process::getCurrentModuleInstanceHandle(),
  38. GetCurrentThreadId());
  39. }
  40. }
  41. ~WindowsHooks()
  42. {
  43. if (--numHookUsers == 0)
  44. {
  45. if (mouseWheelHook != 0)
  46. {
  47. UnhookWindowsHookEx (mouseWheelHook);
  48. mouseWheelHook = 0;
  49. }
  50. if (keyboardHook != 0)
  51. {
  52. UnhookWindowsHookEx (keyboardHook);
  53. keyboardHook = 0;
  54. }
  55. }
  56. }
  57. static LRESULT CALLBACK mouseWheelHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  58. {
  59. if (nCode >= 0 && wParam == WM_MOUSEWHEEL)
  60. {
  61. // using a local copy of this struct to support old mingw libraries
  62. struct MOUSEHOOKSTRUCTEX_ : public MOUSEHOOKSTRUCT { DWORD mouseData; };
  63. const MOUSEHOOKSTRUCTEX_& hs = *(MOUSEHOOKSTRUCTEX_*) lParam;
  64. if (Component* const comp = Desktop::getInstance().findComponentAt (Point<int> (hs.pt.x, hs.pt.y)))
  65. if (comp->getWindowHandle() != 0)
  66. return PostMessage ((HWND) comp->getWindowHandle(), WM_MOUSEWHEEL,
  67. hs.mouseData & 0xffff0000, (hs.pt.x & 0xffff) | (hs.pt.y << 16));
  68. }
  69. return CallNextHookEx (mouseWheelHook, nCode, wParam, lParam);
  70. }
  71. static LRESULT CALLBACK keyboardHookCallback (int nCode, WPARAM wParam, LPARAM lParam)
  72. {
  73. MSG& msg = *(MSG*) lParam;
  74. if (nCode == HC_ACTION && wParam == PM_REMOVE
  75. && offerKeyMessageToJUCEWindow (msg))
  76. {
  77. zerostruct (msg);
  78. msg.message = WM_USER;
  79. return 1;
  80. }
  81. return CallNextHookEx (keyboardHook, nCode, wParam, lParam);
  82. }
  83. };
  84. }
  85. #endif