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.

937 lines
34KB

  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. //==============================================================================
  21. static int numAlwaysOnTopPeers = 0;
  22. bool juce_areThereAnyAlwaysOnTopWindows() { return numAlwaysOnTopPeers > 0; }
  23. //==============================================================================
  24. class LinuxComponentPeer : public ComponentPeer,
  25. private XWindowSystemUtilities::XSettings::Listener
  26. {
  27. public:
  28. LinuxComponentPeer (Component& comp, int windowStyleFlags, ::Window parentToAddTo)
  29. : ComponentPeer (comp, windowStyleFlags),
  30. isAlwaysOnTop (comp.isAlwaysOnTop())
  31. {
  32. // it's dangerous to create a window on a thread other than the message thread.
  33. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  34. const auto* instance = XWindowSystem::getInstance();
  35. if (! instance->isX11Available())
  36. return;
  37. if (isAlwaysOnTop)
  38. ++numAlwaysOnTopPeers;
  39. repainter = std::make_unique<LinuxRepaintManager> (*this);
  40. windowH = instance->createWindow (parentToAddTo, this);
  41. parentWindow = parentToAddTo;
  42. setTitle (component.getName());
  43. if (auto* xSettings = instance->getXSettings())
  44. xSettings->addListener (this);
  45. getNativeRealtimeModifiers = []() -> ModifierKeys { return XWindowSystem::getInstance()->getNativeRealtimeModifiers(); };
  46. }
  47. ~LinuxComponentPeer() override
  48. {
  49. // it's dangerous to delete a window on a thread other than the message thread.
  50. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  51. auto* instance = XWindowSystem::getInstance();
  52. repainter = nullptr;
  53. instance->destroyWindow (windowH);
  54. if (auto* xSettings = instance->getXSettings())
  55. xSettings->removeListener (this);
  56. if (isAlwaysOnTop)
  57. --numAlwaysOnTopPeers;
  58. }
  59. ::Window getWindowHandle() const noexcept
  60. {
  61. return windowH;
  62. }
  63. //==============================================================================
  64. void* getNativeHandle() const override
  65. {
  66. return reinterpret_cast<void*> (getWindowHandle());
  67. }
  68. //==============================================================================
  69. void forceSetBounds (const Rectangle<int>& correctedNewBounds, bool isNowFullScreen)
  70. {
  71. bounds = correctedNewBounds;
  72. updateScaleFactorFromNewBounds (bounds, false);
  73. auto physicalBounds = parentWindow == 0 ? Desktop::getInstance().getDisplays().logicalToPhysical (bounds)
  74. : bounds * currentScaleFactor;
  75. WeakReference<Component> deletionChecker (&component);
  76. XWindowSystem::getInstance()->setBounds (windowH, physicalBounds, isNowFullScreen);
  77. fullScreen = isNowFullScreen;
  78. if (deletionChecker != nullptr)
  79. {
  80. updateBorderSize();
  81. handleMovedOrResized();
  82. }
  83. }
  84. void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override
  85. {
  86. const auto correctedNewBounds = newBounds.withSize (jmax (1, newBounds.getWidth()),
  87. jmax (1, newBounds.getHeight()));
  88. if (bounds != correctedNewBounds || fullScreen != isNowFullScreen)
  89. forceSetBounds (correctedNewBounds, isNowFullScreen);
  90. }
  91. Point<int> getScreenPosition (bool physical) const
  92. {
  93. auto physicalParentPosition = XWindowSystem::getInstance()->getPhysicalParentScreenPosition();
  94. auto parentPosition = parentWindow == 0 ? Desktop::getInstance().getDisplays().physicalToLogical (physicalParentPosition)
  95. : physicalParentPosition / currentScaleFactor;
  96. auto screenBounds = parentWindow == 0 ? bounds
  97. : bounds.translated (parentPosition.x, parentPosition.y);
  98. if (physical)
  99. return parentWindow == 0 ? Desktop::getInstance().getDisplays().logicalToPhysical (screenBounds.getTopLeft())
  100. : screenBounds.getTopLeft() * currentScaleFactor;
  101. return screenBounds.getTopLeft();
  102. }
  103. Rectangle<int> getBounds() const override
  104. {
  105. return bounds;
  106. }
  107. OptionalBorderSize getFrameSizeIfPresent() const override
  108. {
  109. return windowBorder;
  110. }
  111. BorderSize<int> getFrameSize() const override
  112. {
  113. const auto optionalBorderSize = getFrameSizeIfPresent();
  114. return optionalBorderSize ? (*optionalBorderSize) : BorderSize<int>();
  115. }
  116. Point<float> localToGlobal (Point<float> relativePosition) override
  117. {
  118. return relativePosition + getScreenPosition (false).toFloat();
  119. }
  120. Point<float> globalToLocal (Point<float> screenPosition) override
  121. {
  122. return screenPosition - getScreenPosition (false).toFloat();
  123. }
  124. using ComponentPeer::localToGlobal;
  125. using ComponentPeer::globalToLocal;
  126. //==============================================================================
  127. StringArray getAvailableRenderingEngines() override
  128. {
  129. return { "Software Renderer" };
  130. }
  131. void setVisible (bool shouldBeVisible) override
  132. {
  133. XWindowSystem::getInstance()->setVisible (windowH, shouldBeVisible);
  134. }
  135. void setTitle (const String& title) override
  136. {
  137. XWindowSystem::getInstance()->setTitle (windowH, title);
  138. }
  139. void setMinimised (bool shouldBeMinimised) override
  140. {
  141. if (shouldBeMinimised)
  142. XWindowSystem::getInstance()->setMinimised (windowH, shouldBeMinimised);
  143. else
  144. setVisible (true);
  145. }
  146. bool isMinimised() const override
  147. {
  148. return XWindowSystem::getInstance()->isMinimised (windowH);
  149. }
  150. void setFullScreen (bool shouldBeFullScreen) override
  151. {
  152. auto r = lastNonFullscreenBounds; // (get a copy of this before de-minimising)
  153. setMinimised (false);
  154. if (fullScreen != shouldBeFullScreen)
  155. {
  156. const auto usingNativeTitleBar = ((styleFlags & windowHasTitleBar) != 0);
  157. if (usingNativeTitleBar)
  158. XWindowSystem::getInstance()->setMaximised (windowH, shouldBeFullScreen);
  159. if (shouldBeFullScreen)
  160. r = usingNativeTitleBar ? XWindowSystem::getInstance()->getWindowBounds (windowH, parentWindow)
  161. : Desktop::getInstance().getDisplays().getDisplayForRect (bounds)->userArea;
  162. if (! r.isEmpty())
  163. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, r), shouldBeFullScreen);
  164. component.repaint();
  165. }
  166. }
  167. bool isFullScreen() const override
  168. {
  169. return fullScreen;
  170. }
  171. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  172. {
  173. if (! bounds.withZeroOrigin().contains (localPos))
  174. return false;
  175. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  176. {
  177. auto* c = Desktop::getInstance().getComponent (i);
  178. if (c == &component)
  179. break;
  180. if (! c->isVisible())
  181. continue;
  182. if (auto* peer = c->getPeer())
  183. if (peer->contains (localPos + bounds.getPosition() - peer->getBounds().getPosition(), true))
  184. return false;
  185. }
  186. if (trueIfInAChildWindow)
  187. return true;
  188. return XWindowSystem::getInstance()->contains (windowH, localPos * currentScaleFactor);
  189. }
  190. void toFront (bool makeActive) override
  191. {
  192. if (makeActive)
  193. {
  194. setVisible (true);
  195. grabFocus();
  196. }
  197. XWindowSystem::getInstance()->toFront (windowH, makeActive);
  198. handleBroughtToFront();
  199. }
  200. void toBehind (ComponentPeer* other) override
  201. {
  202. if (auto* otherPeer = dynamic_cast<LinuxComponentPeer*> (other))
  203. {
  204. if (otherPeer->styleFlags & windowIsTemporary)
  205. return;
  206. setMinimised (false);
  207. XWindowSystem::getInstance()->toBehind (windowH, otherPeer->windowH);
  208. }
  209. else
  210. {
  211. jassertfalse; // wrong type of window?
  212. }
  213. }
  214. bool isFocused() const override
  215. {
  216. return XWindowSystem::getInstance()->isFocused (windowH);
  217. }
  218. void grabFocus() override
  219. {
  220. if (XWindowSystem::getInstance()->grabFocus (windowH))
  221. isActiveApplication = true;
  222. }
  223. //==============================================================================
  224. void repaint (const Rectangle<int>& area) override
  225. {
  226. if (repainter != nullptr)
  227. repainter->repaint (area.getIntersection (bounds.withZeroOrigin()));
  228. }
  229. void performAnyPendingRepaintsNow() override
  230. {
  231. if (repainter != nullptr)
  232. repainter->performAnyPendingRepaintsNow();
  233. }
  234. void setIcon (const Image& newIcon) override
  235. {
  236. XWindowSystem::getInstance()->setIcon (windowH, newIcon);
  237. }
  238. double getPlatformScaleFactor() const noexcept override
  239. {
  240. return currentScaleFactor;
  241. }
  242. void setAlpha (float) override {}
  243. bool setAlwaysOnTop (bool) override { return false; }
  244. void textInputRequired (Point<int>, TextInputTarget&) override {}
  245. //==============================================================================
  246. void addOpenGLRepaintListener (Component* dummy)
  247. {
  248. if (dummy != nullptr)
  249. glRepaintListeners.addIfNotAlreadyThere (dummy);
  250. }
  251. void removeOpenGLRepaintListener (Component* dummy)
  252. {
  253. if (dummy != nullptr)
  254. glRepaintListeners.removeAllInstancesOf (dummy);
  255. }
  256. void repaintOpenGLContexts()
  257. {
  258. for (auto* c : glRepaintListeners)
  259. c->handleCommandMessage (0);
  260. }
  261. //==============================================================================
  262. ::Window getParentWindow() { return parentWindow; }
  263. void setParentWindow (::Window newParent) { parentWindow = newParent; }
  264. //==============================================================================
  265. bool isConstrainedNativeWindow() const
  266. {
  267. return constrainer != nullptr
  268. && (styleFlags & (windowHasTitleBar | windowIsResizable)) == (windowHasTitleBar | windowIsResizable)
  269. && ! isKioskMode();
  270. }
  271. void updateWindowBounds()
  272. {
  273. if (windowH == 0)
  274. {
  275. jassertfalse;
  276. return;
  277. }
  278. if (isConstrainedNativeWindow())
  279. XWindowSystem::getInstance()->updateConstraints (windowH);
  280. auto physicalBounds = XWindowSystem::getInstance()->getWindowBounds (windowH, parentWindow);
  281. updateScaleFactorFromNewBounds (physicalBounds, true);
  282. bounds = parentWindow == 0 ? Desktop::getInstance().getDisplays().physicalToLogical (physicalBounds)
  283. : physicalBounds / currentScaleFactor;
  284. }
  285. void updateBorderSize()
  286. {
  287. if ((styleFlags & windowHasTitleBar) == 0)
  288. {
  289. windowBorder = ComponentPeer::OptionalBorderSize { BorderSize<int>() };
  290. }
  291. else if (! windowBorder
  292. || ((*windowBorder).getTopAndBottom() == 0 && (*windowBorder).getLeftAndRight() == 0))
  293. {
  294. windowBorder = XWindowSystem::getInstance()->getBorderSize (windowH);
  295. }
  296. }
  297. //==============================================================================
  298. static bool isActiveApplication;
  299. bool focused = false;
  300. private:
  301. //==============================================================================
  302. class LinuxRepaintManager : public Timer
  303. {
  304. public:
  305. LinuxRepaintManager (LinuxComponentPeer& p)
  306. : peer (p),
  307. isSemiTransparentWindow ((peer.getStyleFlags() & ComponentPeer::windowIsSemiTransparent) != 0)
  308. {
  309. }
  310. void timerCallback() override
  311. {
  312. XWindowSystem::getInstance()->processPendingPaintsForWindow (peer.windowH);
  313. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  314. return;
  315. if (! regionsNeedingRepaint.isEmpty())
  316. {
  317. stopTimer();
  318. performAnyPendingRepaintsNow();
  319. }
  320. else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
  321. {
  322. stopTimer();
  323. image = Image();
  324. }
  325. }
  326. void repaint (Rectangle<int> area)
  327. {
  328. if (! isTimerRunning())
  329. startTimer (repaintTimerPeriod);
  330. regionsNeedingRepaint.add (area * peer.currentScaleFactor);
  331. }
  332. void performAnyPendingRepaintsNow()
  333. {
  334. if (XWindowSystem::getInstance()->getNumPaintsPendingForWindow (peer.windowH) > 0)
  335. {
  336. startTimer (repaintTimerPeriod);
  337. return;
  338. }
  339. auto originalRepaintRegion = regionsNeedingRepaint;
  340. regionsNeedingRepaint.clear();
  341. auto totalArea = originalRepaintRegion.getBounds();
  342. if (! totalArea.isEmpty())
  343. {
  344. const auto wasImageNull = image.isNull();
  345. if (wasImageNull || image.getWidth() < totalArea.getWidth()
  346. || image.getHeight() < totalArea.getHeight())
  347. {
  348. image = XWindowSystem::getInstance()->createImage (isSemiTransparentWindow,
  349. totalArea.getWidth(), totalArea.getHeight(),
  350. useARGBImagesForRendering);
  351. if (wasImageNull)
  352. {
  353. // After calling createImage() XWindowSystem::getWindowBounds() will return
  354. // changed coordinates that look like the result of some position
  355. // defaulting mechanism. If we handle a configureNotifyEvent after
  356. // createImage() and before we would issue new, valid coordinates, we will
  357. // apply these default, unwanted coordinates to our window. To avoid that
  358. // we immediately send another positioning message to guarantee that the
  359. // next configureNotifyEvent will read valid values.
  360. //
  361. // This issue only occurs right after peer creation, when the image is
  362. // null. Updating when only the width or height is changed would lead to
  363. // incorrect behaviour.
  364. peer.forceSetBounds (ScalingHelpers::scaledScreenPosToUnscaled (peer.component,
  365. peer.component.getBoundsInParent()),
  366. peer.isFullScreen());
  367. }
  368. }
  369. startTimer (repaintTimerPeriod);
  370. RectangleList<int> adjustedList (originalRepaintRegion);
  371. adjustedList.offsetAll (-totalArea.getX(), -totalArea.getY());
  372. if (XWindowSystem::getInstance()->canUseARGBImages())
  373. for (auto& i : originalRepaintRegion)
  374. image.clear (i - totalArea.getPosition());
  375. {
  376. auto context = peer.getComponent().getLookAndFeel()
  377. .createGraphicsContext (image, -totalArea.getPosition(), adjustedList);
  378. context->addTransform (AffineTransform::scale ((float) peer.currentScaleFactor));
  379. peer.handlePaint (*context);
  380. }
  381. for (auto& i : originalRepaintRegion)
  382. XWindowSystem::getInstance()->blitToWindow (peer.windowH, image, i, totalArea);
  383. }
  384. lastTimeImageUsed = Time::getApproximateMillisecondCounter();
  385. startTimer (repaintTimerPeriod);
  386. }
  387. private:
  388. enum { repaintTimerPeriod = 1000 / 100 };
  389. LinuxComponentPeer& peer;
  390. const bool isSemiTransparentWindow;
  391. Image image;
  392. uint32 lastTimeImageUsed = 0;
  393. RectangleList<int> regionsNeedingRepaint;
  394. bool useARGBImagesForRendering = XWindowSystem::getInstance()->canUseARGBImages();
  395. JUCE_DECLARE_NON_COPYABLE (LinuxRepaintManager)
  396. };
  397. //==============================================================================
  398. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  399. {
  400. static StringArray possibleSettings { XWindowSystem::getWindowScalingFactorSettingName(),
  401. "Gdk/UnscaledDPI",
  402. "Xft/DPI" };
  403. if (possibleSettings.contains (settingThatHasChanged.name))
  404. forceDisplayUpdate();
  405. }
  406. void updateScaleFactorFromNewBounds (const Rectangle<int>& newBounds, bool isPhysical)
  407. {
  408. Point<int> translation = (parentWindow != 0 ? getScreenPosition (isPhysical) : Point<int>());
  409. const auto& desktop = Desktop::getInstance();
  410. if (auto* display = desktop.getDisplays().getDisplayForRect (newBounds.translated (translation.x, translation.y),
  411. isPhysical))
  412. {
  413. auto newScaleFactor = display->scale / desktop.getGlobalScaleFactor();
  414. if (! approximatelyEqual (newScaleFactor, currentScaleFactor))
  415. {
  416. currentScaleFactor = newScaleFactor;
  417. scaleFactorListeners.call ([&] (ScaleFactorListener& l) { l.nativeScaleFactorChanged (currentScaleFactor); });
  418. }
  419. }
  420. }
  421. //==============================================================================
  422. std::unique_ptr<LinuxRepaintManager> repainter;
  423. ::Window windowH = {}, parentWindow = {};
  424. Rectangle<int> bounds;
  425. ComponentPeer::OptionalBorderSize windowBorder;
  426. bool fullScreen = false, isAlwaysOnTop = false;
  427. double currentScaleFactor = 1.0;
  428. Array<Component*> glRepaintListeners;
  429. //==============================================================================
  430. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LinuxComponentPeer)
  431. };
  432. bool LinuxComponentPeer::isActiveApplication = false;
  433. //==============================================================================
  434. ComponentPeer* Component::createNewPeer (int styleFlags, void* nativeWindowToAttachTo)
  435. {
  436. return new LinuxComponentPeer (*this, styleFlags, (::Window) nativeWindowToAttachTo);
  437. }
  438. //==============================================================================
  439. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return LinuxComponentPeer::isActiveApplication; }
  440. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  441. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  442. //==============================================================================
  443. void Desktop::setKioskComponent (Component* comp, bool enableOrDisable, bool)
  444. {
  445. if (enableOrDisable)
  446. comp->setBounds (getDisplays().getDisplayForRect (comp->getScreenBounds())->totalArea);
  447. }
  448. void Displays::findDisplays (float masterScale)
  449. {
  450. if (XWindowSystem::getInstance()->getDisplay() != nullptr)
  451. {
  452. displays = XWindowSystem::getInstance()->findDisplays (masterScale);
  453. if (! displays.isEmpty())
  454. updateToLogical();
  455. }
  456. }
  457. bool Desktop::canUseSemiTransparentWindows() noexcept
  458. {
  459. return XWindowSystem::getInstance()->canUseSemiTransparentWindows();
  460. }
  461. class Desktop::NativeDarkModeChangeDetectorImpl : private XWindowSystemUtilities::XSettings::Listener
  462. {
  463. public:
  464. NativeDarkModeChangeDetectorImpl()
  465. {
  466. const auto* windowSystem = XWindowSystem::getInstance();
  467. if (auto* xSettings = windowSystem->getXSettings())
  468. xSettings->addListener (this);
  469. darkModeEnabled = windowSystem->isDarkModeActive();
  470. }
  471. ~NativeDarkModeChangeDetectorImpl() override
  472. {
  473. if (auto* windowSystem = XWindowSystem::getInstanceWithoutCreating())
  474. if (auto* xSettings = windowSystem->getXSettings())
  475. xSettings->removeListener (this);
  476. }
  477. bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
  478. private:
  479. void settingChanged (const XWindowSystemUtilities::XSetting& settingThatHasChanged) override
  480. {
  481. if (settingThatHasChanged.name == XWindowSystem::getThemeNameSettingName())
  482. {
  483. const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, XWindowSystem::getInstance()->isDarkModeActive());
  484. if (darkModeEnabled != wasDarkModeEnabled)
  485. Desktop::getInstance().darkModeChanged();
  486. }
  487. }
  488. bool darkModeEnabled = false;
  489. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
  490. };
  491. std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
  492. {
  493. return std::make_unique<NativeDarkModeChangeDetectorImpl>();
  494. }
  495. bool Desktop::isDarkModeActive() const
  496. {
  497. return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
  498. }
  499. static bool screenSaverAllowed = true;
  500. void Desktop::setScreenSaverEnabled (bool isEnabled)
  501. {
  502. if (screenSaverAllowed != isEnabled)
  503. {
  504. screenSaverAllowed = isEnabled;
  505. XWindowSystem::getInstance()->setScreenSaverEnabled (screenSaverAllowed);
  506. }
  507. }
  508. bool Desktop::isScreenSaverEnabled()
  509. {
  510. return screenSaverAllowed;
  511. }
  512. double Desktop::getDefaultMasterScale() { return 1.0; }
  513. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const { return upright; }
  514. void Desktop::allowedOrientationsChanged() {}
  515. //==============================================================================
  516. bool MouseInputSource::SourceList::addSource()
  517. {
  518. if (sources.isEmpty())
  519. {
  520. addSource (0, MouseInputSource::InputSourceType::mouse);
  521. return true;
  522. }
  523. return false;
  524. }
  525. bool MouseInputSource::SourceList::canUseTouch()
  526. {
  527. return false;
  528. }
  529. Point<float> MouseInputSource::getCurrentRawMousePosition()
  530. {
  531. return Desktop::getInstance().getDisplays().physicalToLogical (XWindowSystem::getInstance()->getCurrentMousePosition());
  532. }
  533. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  534. {
  535. XWindowSystem::getInstance()->setMousePosition (Desktop::getInstance().getDisplays().logicalToPhysical (newPosition));
  536. }
  537. //==============================================================================
  538. class MouseCursor::PlatformSpecificHandle
  539. {
  540. public:
  541. explicit PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  542. : cursorHandle (makeHandle (type)) {}
  543. explicit PlatformSpecificHandle (const CustomMouseCursorInfo& info)
  544. : cursorHandle (makeHandle (info)) {}
  545. ~PlatformSpecificHandle()
  546. {
  547. if (cursorHandle != Cursor{})
  548. XWindowSystem::getInstance()->deleteMouseCursor (cursorHandle);
  549. }
  550. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer* peer)
  551. {
  552. const auto cursor = handle != nullptr ? handle->cursorHandle : Cursor{};
  553. if (peer != nullptr)
  554. XWindowSystem::getInstance()->showCursor ((::Window) peer->getNativeHandle(), cursor);
  555. }
  556. private:
  557. static Cursor makeHandle (const CustomMouseCursorInfo& info)
  558. {
  559. const auto image = info.image.getImage();
  560. return XWindowSystem::getInstance()->createCustomMouseCursorInfo (image.rescaled ((int) (image.getWidth() / info.image.getScale()),
  561. (int) (image.getHeight() / info.image.getScale())), info.hotspot);
  562. }
  563. static Cursor makeHandle (MouseCursor::StandardCursorType type)
  564. {
  565. return XWindowSystem::getInstance()->createStandardMouseCursor (type);
  566. }
  567. Cursor cursorHandle;
  568. //==============================================================================
  569. JUCE_DECLARE_NON_COPYABLE (PlatformSpecificHandle)
  570. JUCE_DECLARE_NON_MOVEABLE (PlatformSpecificHandle)
  571. };
  572. //==============================================================================
  573. static LinuxComponentPeer* getPeerForDragEvent (Component* sourceComp)
  574. {
  575. if (sourceComp == nullptr)
  576. if (auto* draggingSource = Desktop::getInstance().getDraggingMouseSource (0))
  577. sourceComp = draggingSource->getComponentUnderMouse();
  578. if (sourceComp != nullptr)
  579. if (auto* lp = dynamic_cast<LinuxComponentPeer*> (sourceComp->getPeer()))
  580. return lp;
  581. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  582. return nullptr;
  583. }
  584. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  585. Component* sourceComp, std::function<void()> callback)
  586. {
  587. if (files.isEmpty())
  588. return false;
  589. if (auto* peer = getPeerForDragEvent (sourceComp))
  590. return XWindowSystem::getInstance()->externalDragFileInit (peer, files, canMoveFiles, std::move (callback));
  591. // This method must be called in response to a component's mouseDown or mouseDrag event!
  592. jassertfalse;
  593. return false;
  594. }
  595. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component* sourceComp,
  596. std::function<void()> callback)
  597. {
  598. if (text.isEmpty())
  599. return false;
  600. if (auto* peer = getPeerForDragEvent (sourceComp))
  601. return XWindowSystem::getInstance()->externalDragTextInit (peer, text, std::move (callback));
  602. // This method must be called in response to a component's mouseDown or mouseDrag event!
  603. jassertfalse;
  604. return false;
  605. }
  606. //==============================================================================
  607. void SystemClipboard::copyTextToClipboard (const String& clipText)
  608. {
  609. XWindowSystem::getInstance()->copyTextToClipboard (clipText);
  610. }
  611. String SystemClipboard::getTextFromClipboard()
  612. {
  613. return XWindowSystem::getInstance()->getTextFromClipboard();
  614. }
  615. //==============================================================================
  616. bool KeyPress::isKeyCurrentlyDown (int keyCode)
  617. {
  618. return XWindowSystem::getInstance()->isKeyCurrentlyDown (keyCode);
  619. }
  620. void LookAndFeel::playAlertSound()
  621. {
  622. std::cout << "\a" << std::flush;
  623. }
  624. //==============================================================================
  625. static int showDialog (const MessageBoxOptions& options,
  626. ModalComponentManager::Callback* callback,
  627. Async async)
  628. {
  629. const auto dummyCallback = [] (int) {};
  630. switch (options.getNumButtons())
  631. {
  632. case 2:
  633. {
  634. if (async == Async::yes && callback == nullptr)
  635. callback = ModalCallbackFunction::create (dummyCallback);
  636. return AlertWindow::showOkCancelBox (options.getIconType(),
  637. options.getTitle(),
  638. options.getMessage(),
  639. options.getButtonText (0),
  640. options.getButtonText (1),
  641. options.getAssociatedComponent(),
  642. callback) ? 1 : 0;
  643. }
  644. case 3:
  645. {
  646. if (async == Async::yes && callback == nullptr)
  647. callback = ModalCallbackFunction::create (dummyCallback);
  648. return AlertWindow::showYesNoCancelBox (options.getIconType(),
  649. options.getTitle(),
  650. options.getMessage(),
  651. options.getButtonText (0),
  652. options.getButtonText (1),
  653. options.getButtonText (2),
  654. options.getAssociatedComponent(),
  655. callback);
  656. }
  657. case 1:
  658. default:
  659. break;
  660. }
  661. #if JUCE_MODAL_LOOPS_PERMITTED
  662. if (async == Async::no)
  663. {
  664. AlertWindow::showMessageBox (options.getIconType(),
  665. options.getTitle(),
  666. options.getMessage(),
  667. options.getButtonText (0),
  668. options.getAssociatedComponent());
  669. }
  670. else
  671. #endif
  672. {
  673. AlertWindow::showMessageBoxAsync (options.getIconType(),
  674. options.getTitle(),
  675. options.getMessage(),
  676. options.getButtonText (0),
  677. options.getAssociatedComponent(),
  678. callback);
  679. }
  680. return 0;
  681. }
  682. #if JUCE_MODAL_LOOPS_PERMITTED
  683. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType,
  684. const String& title, const String& message,
  685. Component* /*associatedComponent*/)
  686. {
  687. AlertWindow::showMessageBox (iconType, title, message);
  688. }
  689. int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
  690. {
  691. return showDialog (options, nullptr, Async::no);
  692. }
  693. #endif
  694. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType iconType,
  695. const String& title, const String& message,
  696. Component* associatedComponent,
  697. ModalComponentManager::Callback* callback)
  698. {
  699. AlertWindow::showMessageBoxAsync (iconType, title, message, {}, associatedComponent, callback);
  700. }
  701. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconType,
  702. const String& title, const String& message,
  703. Component* associatedComponent,
  704. ModalComponentManager::Callback* callback)
  705. {
  706. return AlertWindow::showOkCancelBox (iconType, title, message, {}, {}, associatedComponent, callback);
  707. }
  708. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconType,
  709. const String& title, const String& message,
  710. Component* associatedComponent,
  711. ModalComponentManager::Callback* callback)
  712. {
  713. return AlertWindow::showYesNoCancelBox (iconType, title, message, {}, {}, {},
  714. associatedComponent, callback);
  715. }
  716. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
  717. const String& title, const String& message,
  718. Component* associatedComponent,
  719. ModalComponentManager::Callback* callback)
  720. {
  721. return AlertWindow::showOkCancelBox (iconType, title, message, TRANS("Yes"), TRANS("No"),
  722. associatedComponent, callback);
  723. }
  724. void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
  725. ModalComponentManager::Callback* callback)
  726. {
  727. showDialog (options, callback, Async::yes);
  728. }
  729. void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
  730. std::function<void (int)> callback)
  731. {
  732. showAsync (options, ModalCallbackFunction::create (callback));
  733. }
  734. //==============================================================================
  735. Image juce_createIconForFile (const File&)
  736. {
  737. return {};
  738. }
  739. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy)
  740. {
  741. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  742. linuxPeer->addOpenGLRepaintListener (dummy);
  743. }
  744. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy)
  745. {
  746. if (auto* linuxPeer = dynamic_cast<LinuxComponentPeer*> (peer))
  747. linuxPeer->removeOpenGLRepaintListener (dummy);
  748. }
  749. } // namespace juce