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.

143 lines
4.8KB

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