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.

juce_win32_SystemTrayIcon.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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::invalidPressure, MouseInputSource::invalidOrientation,
  89. MouseInputSource::invalidRotation, MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  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