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.

150 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. class SystemTrayIconComponent::Pimpl
  22. {
  23. public:
  24. Pimpl (const Image& im, Window windowH) : image (im)
  25. {
  26. ScopedXDisplay xDisplay;
  27. auto display = xDisplay.display;
  28. ScopedXLock xlock (display);
  29. Screen* const screen = XDefaultScreenOfDisplay (display);
  30. const int screenNumber = XScreenNumberOfScreen (screen);
  31. String screenAtom ("_NET_SYSTEM_TRAY_S");
  32. screenAtom << screenNumber;
  33. Atom selectionAtom = Atoms::getCreating (display, screenAtom.toUTF8());
  34. XGrabServer (display);
  35. Window managerWin = XGetSelectionOwner (display, selectionAtom);
  36. if (managerWin != None)
  37. XSelectInput (display, managerWin, StructureNotifyMask);
  38. XUngrabServer (display);
  39. XFlush (display);
  40. if (managerWin != None)
  41. {
  42. XEvent ev = { 0 };
  43. ev.xclient.type = ClientMessage;
  44. ev.xclient.window = managerWin;
  45. ev.xclient.message_type = Atoms::getCreating (display, "_NET_SYSTEM_TRAY_OPCODE");
  46. ev.xclient.format = 32;
  47. ev.xclient.data.l[0] = CurrentTime;
  48. ev.xclient.data.l[1] = 0 /*SYSTEM_TRAY_REQUEST_DOCK*/;
  49. ev.xclient.data.l[2] = (long) windowH;
  50. ev.xclient.data.l[3] = 0;
  51. ev.xclient.data.l[4] = 0;
  52. XSendEvent (display, managerWin, False, NoEventMask, &ev);
  53. XSync (display, False);
  54. }
  55. // For older KDE's ...
  56. long atomData = 1;
  57. Atom trayAtom = Atoms::getCreating (display, "KWM_DOCKWINDOW");
  58. XChangeProperty (display, windowH, trayAtom, trayAtom, 32, PropModeReplace, (unsigned char*) &atomData, 1);
  59. // For more recent KDE's...
  60. trayAtom = Atoms::getCreating (display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
  61. XChangeProperty (display, windowH, trayAtom, XA_WINDOW, 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. XSizeHints* hints = XAllocSizeHints();
  64. hints->flags = PMinSize;
  65. hints->min_width = 22;
  66. hints->min_height = 22;
  67. XSetWMNormalHints (display, windowH, hints);
  68. XFree (hints);
  69. }
  70. Image image;
  71. private:
  72. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  73. };
  74. //==============================================================================
  75. void SystemTrayIconComponent::setIconImage (const Image& colourImage, const Image&)
  76. {
  77. pimpl.reset();
  78. if (colourImage.isValid())
  79. {
  80. if (! isOnDesktop())
  81. addToDesktop (0);
  82. pimpl.reset (new Pimpl (colourImage, (Window) getWindowHandle()));
  83. setVisible (true);
  84. toFront (false);
  85. }
  86. repaint();
  87. }
  88. void SystemTrayIconComponent::paint (Graphics& g)
  89. {
  90. if (pimpl != nullptr)
  91. g.drawImage (pimpl->image, getLocalBounds().toFloat(),
  92. RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize);
  93. }
  94. void SystemTrayIconComponent::setIconTooltip (const String& /*tooltip*/)
  95. {
  96. // xxx Not implemented!
  97. }
  98. void SystemTrayIconComponent::setHighlighted (bool)
  99. {
  100. // xxx Not implemented!
  101. }
  102. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  103. {
  104. // xxx Not implemented!
  105. }
  106. void SystemTrayIconComponent::hideInfoBubble()
  107. {
  108. // xxx Not implemented!
  109. }
  110. void* SystemTrayIconComponent::getNativeHandle() const
  111. {
  112. return getWindowHandle();
  113. }
  114. } // namespace juce