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.

152 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class SystemTrayIconComponent::Pimpl
  21. {
  22. public:
  23. Pimpl (const Image& im, Window windowH) : image (im)
  24. {
  25. XWindowSystemUtilities::ScopedXLock xLock;
  26. auto* display = XWindowSystem::getInstance()->getDisplay();
  27. auto* screen = X11Symbols::getInstance()->xDefaultScreenOfDisplay (display);
  28. auto screenNumber = X11Symbols::getInstance()->xScreenNumberOfScreen (screen);
  29. String screenAtom ("_NET_SYSTEM_TRAY_S");
  30. screenAtom << screenNumber;
  31. Atom selectionAtom = XWindowSystemUtilities::Atoms::getCreating (display, screenAtom.toUTF8());
  32. X11Symbols::getInstance()->xGrabServer (display);
  33. auto managerWin = X11Symbols::getInstance()->xGetSelectionOwner (display, selectionAtom);
  34. if (managerWin != None)
  35. X11Symbols::getInstance()->xSelectInput (display, managerWin, StructureNotifyMask);
  36. X11Symbols::getInstance()->xUngrabServer (display);
  37. X11Symbols::getInstance()->xFlush (display);
  38. if (managerWin != None)
  39. {
  40. XEvent ev = { 0 };
  41. ev.xclient.type = ClientMessage;
  42. ev.xclient.window = managerWin;
  43. ev.xclient.message_type = XWindowSystemUtilities::Atoms::getCreating (display, "_NET_SYSTEM_TRAY_OPCODE");
  44. ev.xclient.format = 32;
  45. ev.xclient.data.l[0] = CurrentTime;
  46. ev.xclient.data.l[1] = 0 /*SYSTEM_TRAY_REQUEST_DOCK*/;
  47. ev.xclient.data.l[2] = (long) windowH;
  48. ev.xclient.data.l[3] = 0;
  49. ev.xclient.data.l[4] = 0;
  50. X11Symbols::getInstance()->xSendEvent (display, managerWin, False, NoEventMask, &ev);
  51. X11Symbols::getInstance()->xSync (display, False);
  52. }
  53. // For older KDE's ...
  54. long atomData = 1;
  55. Atom trayAtom = XWindowSystemUtilities::Atoms::getCreating (display, "KWM_DOCKWINDOW");
  56. X11Symbols::getInstance()->xChangeProperty (display, windowH, trayAtom, trayAtom,
  57. 32, PropModeReplace, (unsigned char*) &atomData, 1);
  58. // For more recent KDE's...
  59. trayAtom = XWindowSystemUtilities::Atoms::getCreating (display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
  60. X11Symbols::getInstance()->xChangeProperty (display, windowH, trayAtom, XA_WINDOW,
  61. 32, PropModeReplace, (unsigned char*) &windowH, 1);
  62. // A minimum size must be specified for GNOME and Xfce, otherwise the icon is displayed with a width of 1
  63. if (auto* hints = X11Symbols::getInstance()->xAllocSizeHints())
  64. {
  65. hints->flags = PMinSize;
  66. hints->min_width = 22;
  67. hints->min_height = 22;
  68. X11Symbols::getInstance()->xSetWMNormalHints (display, windowH, hints);
  69. X11Symbols::getInstance()->xFree (hints);
  70. }
  71. }
  72. Image image;
  73. private:
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  75. };
  76. //==============================================================================
  77. void SystemTrayIconComponent::setIconImage (const Image& colourImage, const Image&)
  78. {
  79. pimpl.reset();
  80. if (colourImage.isValid())
  81. {
  82. if (! isOnDesktop())
  83. addToDesktop (0);
  84. pimpl.reset (new Pimpl (colourImage, (Window) getWindowHandle()));
  85. setVisible (true);
  86. toFront (false);
  87. }
  88. repaint();
  89. }
  90. void SystemTrayIconComponent::paint (Graphics& g)
  91. {
  92. if (pimpl != nullptr)
  93. g.drawImage (pimpl->image, getLocalBounds().toFloat(),
  94. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize);
  95. }
  96. void SystemTrayIconComponent::setIconTooltip (const String& /*tooltip*/)
  97. {
  98. // xxx Not implemented!
  99. }
  100. void SystemTrayIconComponent::setHighlighted (bool)
  101. {
  102. // xxx Not implemented!
  103. }
  104. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  105. {
  106. // xxx Not implemented!
  107. }
  108. void SystemTrayIconComponent::hideInfoBubble()
  109. {
  110. // xxx Not implemented!
  111. }
  112. void* SystemTrayIconComponent::getNativeHandle() const
  113. {
  114. return getWindowHandle();
  115. }
  116. } // namespace juce