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.

144 lines
4.5KB

  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. //==============================================================================
  18. class SystemTrayIconComponent::Pimpl
  19. {
  20. public:
  21. Pimpl (const Image& im, Window windowH) : image (im)
  22. {
  23. ScopedXDisplay xDisplay;
  24. ::Display* display = xDisplay.get();
  25. ScopedXLock xlock (display);
  26. Screen* const screen = XDefaultScreenOfDisplay (display);
  27. const int screenNumber = XScreenNumberOfScreen (screen);
  28. String screenAtom ("_NET_SYSTEM_TRAY_S");
  29. screenAtom << screenNumber;
  30. Atom selectionAtom = Atoms::getCreating (display, screenAtom.toUTF8());
  31. XGrabServer (display);
  32. Window managerWin = XGetSelectionOwner (display, selectionAtom);
  33. if (managerWin != None)
  34. XSelectInput (display, managerWin, StructureNotifyMask);
  35. XUngrabServer (display);
  36. XFlush (display);
  37. if (managerWin != None)
  38. {
  39. XEvent ev = { 0 };
  40. ev.xclient.type = ClientMessage;
  41. ev.xclient.window = managerWin;
  42. ev.xclient.message_type = Atoms::getCreating (display, "_NET_SYSTEM_TRAY_OPCODE");
  43. ev.xclient.format = 32;
  44. ev.xclient.data.l[0] = CurrentTime;
  45. ev.xclient.data.l[1] = 0 /*SYSTEM_TRAY_REQUEST_DOCK*/;
  46. ev.xclient.data.l[2] = (long) windowH;
  47. ev.xclient.data.l[3] = 0;
  48. ev.xclient.data.l[4] = 0;
  49. XSendEvent (display, managerWin, False, NoEventMask, &ev);
  50. XSync (display, False);
  51. }
  52. // For older KDE's ...
  53. long atomData = 1;
  54. Atom trayAtom = Atoms::getCreating (display, "KWM_DOCKWINDOW");
  55. XChangeProperty (display, windowH, trayAtom, trayAtom, 32, PropModeReplace, (unsigned char*) &atomData, 1);
  56. // For more recent KDE's...
  57. trayAtom = Atoms::getCreating (display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
  58. XChangeProperty (display, windowH, trayAtom, XA_WINDOW, 32, PropModeReplace, (unsigned char*) &windowH, 1);
  59. // A minimum size must be specified for GNOME and Xfce, otherwise the icon is displayed with a width of 1
  60. XSizeHints* hints = XAllocSizeHints();
  61. hints->flags = PMinSize;
  62. hints->min_width = 22;
  63. hints->min_height = 22;
  64. XSetWMNormalHints (display, windowH, hints);
  65. XFree (hints);
  66. }
  67. Image image;
  68. private:
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  70. };
  71. //==============================================================================
  72. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  73. {
  74. pimpl = nullptr;
  75. if (newImage.isValid())
  76. {
  77. if (! isOnDesktop())
  78. addToDesktop (0);
  79. pimpl = new Pimpl (newImage, (Window) getWindowHandle());
  80. setVisible (true);
  81. toFront (false);
  82. }
  83. repaint();
  84. }
  85. void SystemTrayIconComponent::paint (Graphics& g)
  86. {
  87. if (pimpl != nullptr)
  88. g.drawImage (pimpl->image, getLocalBounds().toFloat(),
  89. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize);
  90. }
  91. void SystemTrayIconComponent::setIconTooltip (const String& /*tooltip*/)
  92. {
  93. // xxx Not implemented!
  94. }
  95. void SystemTrayIconComponent::setHighlighted (bool)
  96. {
  97. // xxx Not implemented!
  98. }
  99. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  100. {
  101. // xxx Not implemented!
  102. }
  103. void SystemTrayIconComponent::hideInfoBubble()
  104. {
  105. // xxx Not implemented!
  106. }
  107. void* SystemTrayIconComponent::getNativeHandle() const
  108. {
  109. return getWindowHandle();
  110. }