Audio plugin host https://kx.studio/carla
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.

236 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. extern void* getUser32Function (const char*);
  16. namespace IconConverters
  17. {
  18. extern HICON createHICONFromImage (const Image&, BOOL isIcon, int hotspotX, int hotspotY);
  19. }
  20. //==============================================================================
  21. class SystemTrayIconComponent::Pimpl
  22. {
  23. public:
  24. Pimpl (SystemTrayIconComponent& owner_, HICON hicon, HWND hwnd)
  25. : owner (owner_),
  26. originalWndProc ((WNDPROC) GetWindowLongPtr (hwnd, GWLP_WNDPROC)),
  27. taskbarCreatedMessage (RegisterWindowMessage (TEXT ("TaskbarCreated")))
  28. {
  29. SetWindowLongPtr (hwnd, GWLP_WNDPROC, (LONG_PTR) hookedWndProc);
  30. zerostruct (iconData);
  31. iconData.cbSize = sizeof (iconData);
  32. iconData.hWnd = hwnd;
  33. iconData.uID = (UINT) (pointer_sized_int) hwnd;
  34. iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  35. iconData.uCallbackMessage = WM_TRAYNOTIFY;
  36. iconData.hIcon = hicon;
  37. notify (NIM_ADD);
  38. // In order to receive the "TaskbarCreated" message, we need to request that it's not filtered out.
  39. // (Need to load dynamically, as ChangeWindowMessageFilter is only available in Vista and later)
  40. typedef BOOL (WINAPI* ChangeWindowMessageFilterType) (UINT, DWORD);
  41. if (ChangeWindowMessageFilterType changeWindowMessageFilter
  42. = (ChangeWindowMessageFilterType) getUser32Function ("ChangeWindowMessageFilter"))
  43. changeWindowMessageFilter (taskbarCreatedMessage, 1 /* MSGFLT_ADD */);
  44. }
  45. ~Pimpl()
  46. {
  47. SetWindowLongPtr (iconData.hWnd, GWLP_WNDPROC, (LONG_PTR) originalWndProc);
  48. iconData.uFlags = 0;
  49. notify (NIM_DELETE);
  50. DestroyIcon (iconData.hIcon);
  51. }
  52. void updateIcon (HICON hicon)
  53. {
  54. HICON oldIcon = iconData.hIcon;
  55. iconData.hIcon = hicon;
  56. iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  57. notify (NIM_MODIFY);
  58. DestroyIcon (oldIcon);
  59. }
  60. void setToolTip (const String& toolTip)
  61. {
  62. iconData.uFlags = NIF_TIP;
  63. toolTip.copyToUTF16 (iconData.szTip, sizeof (iconData.szTip) - 1);
  64. notify (NIM_MODIFY);
  65. }
  66. void handleTaskBarEvent (const LPARAM lParam)
  67. {
  68. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  69. {
  70. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN
  71. || lParam == WM_LBUTTONDBLCLK || lParam == WM_RBUTTONDBLCLK)
  72. {
  73. if (auto* current = Component::getCurrentlyModalComponent())
  74. current->inputAttemptWhenModal();
  75. }
  76. }
  77. else
  78. {
  79. ModifierKeys eventMods (ComponentPeer::getCurrentModifiersRealtime());
  80. if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK)
  81. eventMods = eventMods.withFlags (ModifierKeys::leftButtonModifier);
  82. else if (lParam == WM_RBUTTONDOWN || lParam == WM_RBUTTONDBLCLK)
  83. eventMods = eventMods.withFlags (ModifierKeys::rightButtonModifier);
  84. else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
  85. eventMods = eventMods.withoutMouseButtons();
  86. const Time eventTime (getMouseEventTime());
  87. const MouseEvent e (Desktop::getInstance().getMainMouseSource(), {}, eventMods,
  88. MouseInputSource::defaultPressure, MouseInputSource::defaultOrientation,
  89. MouseInputSource::defaultRotation, MouseInputSource::defaultTiltX, MouseInputSource::defaultTiltY,
  90. &owner, &owner, eventTime, {}, eventTime, 1, false);
  91. if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN)
  92. {
  93. SetFocus (iconData.hWnd);
  94. SetForegroundWindow (iconData.hWnd);
  95. owner.mouseDown (e);
  96. }
  97. else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
  98. {
  99. owner.mouseUp (e);
  100. }
  101. else if (lParam == WM_LBUTTONDBLCLK || lParam == WM_RBUTTONDBLCLK)
  102. {
  103. owner.mouseDoubleClick (e);
  104. }
  105. else if (lParam == WM_MOUSEMOVE)
  106. {
  107. owner.mouseMove (e);
  108. }
  109. }
  110. }
  111. static Pimpl* getPimpl (HWND hwnd)
  112. {
  113. if (JuceWindowIdentifier::isJUCEWindow (hwnd))
  114. if (ComponentPeer* peer = (ComponentPeer*) GetWindowLongPtr (hwnd, 8))
  115. if (SystemTrayIconComponent* const iconComp = dynamic_cast<SystemTrayIconComponent*> (&(peer->getComponent())))
  116. return iconComp->pimpl.get();
  117. return nullptr;
  118. }
  119. static LRESULT CALLBACK hookedWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  120. {
  121. if (Pimpl* const p = getPimpl (hwnd))
  122. return p->windowProc (hwnd, message, wParam, lParam);
  123. return DefWindowProcW (hwnd, message, wParam, lParam);
  124. }
  125. LRESULT windowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  126. {
  127. if (message == WM_TRAYNOTIFY)
  128. {
  129. handleTaskBarEvent (lParam);
  130. }
  131. else if (message == taskbarCreatedMessage)
  132. {
  133. iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  134. notify (NIM_ADD);
  135. }
  136. return CallWindowProc (originalWndProc, hwnd, message, wParam, lParam);
  137. }
  138. void showBubble (const String& title, const String& content)
  139. {
  140. iconData.uFlags = 0x10 /*NIF_INFO*/;
  141. title.copyToUTF16 (iconData.szInfoTitle, sizeof (iconData.szInfoTitle) - 1);
  142. content.copyToUTF16 (iconData.szInfo, sizeof (iconData.szInfo) - 1);
  143. notify (NIM_MODIFY);
  144. }
  145. SystemTrayIconComponent& owner;
  146. NOTIFYICONDATA iconData;
  147. private:
  148. WNDPROC originalWndProc;
  149. const DWORD taskbarCreatedMessage;
  150. enum { WM_TRAYNOTIFY = WM_USER + 100 };
  151. void notify (DWORD message) noexcept { Shell_NotifyIcon (message, &iconData); }
  152. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  153. };
  154. //==============================================================================
  155. void SystemTrayIconComponent::setIconImage (const Image& colourImage, const Image&)
  156. {
  157. if (colourImage.isValid())
  158. {
  159. HICON hicon = IconConverters::createHICONFromImage (colourImage, TRUE, 0, 0);
  160. if (pimpl == nullptr)
  161. pimpl.reset (new Pimpl (*this, hicon, (HWND) getWindowHandle()));
  162. else
  163. pimpl->updateIcon (hicon);
  164. }
  165. else
  166. {
  167. pimpl.reset();
  168. }
  169. }
  170. void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
  171. {
  172. if (pimpl != nullptr)
  173. pimpl->setToolTip (tooltip);
  174. }
  175. void SystemTrayIconComponent::setHighlighted (bool)
  176. {
  177. // N/A on Windows.
  178. }
  179. void SystemTrayIconComponent::showInfoBubble (const String& title, const String& content)
  180. {
  181. if (pimpl != nullptr)
  182. pimpl->showBubble (title, content);
  183. }
  184. void SystemTrayIconComponent::hideInfoBubble()
  185. {
  186. showInfoBubble (String(), String());
  187. }
  188. void* SystemTrayIconComponent::getNativeHandle() const
  189. {
  190. return pimpl != nullptr ? &(pimpl->iconData) : nullptr;
  191. }
  192. } // namespace juce