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.

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