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.

237 lines
8.1KB

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