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.

867 lines
31KB

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