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.

116 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::detail
  19. {
  20. struct AlertWindowHelpers
  21. {
  22. AlertWindowHelpers() = delete;
  23. static std::unique_ptr<ScopedMessageBoxInterface> create (const MessageBoxOptions& opts)
  24. {
  25. class AlertWindowImpl : public detail::ScopedMessageBoxInterface
  26. {
  27. public:
  28. explicit AlertWindowImpl (const MessageBoxOptions& opts) : options (opts) {}
  29. void runAsync (std::function<void (int)> recipient) override
  30. {
  31. if (auto* comp = setUpAlert())
  32. comp->enterModalState (true, ModalCallbackFunction::create (std::move (recipient)), true);
  33. else
  34. NullCheckedInvocation::invoke (recipient, 0);
  35. }
  36. int runSync() override
  37. {
  38. #if JUCE_MODAL_LOOPS_PERMITTED
  39. if (auto comp = rawToUniquePtr (setUpAlert()))
  40. return comp->runModalLoop();
  41. #endif
  42. jassertfalse;
  43. return 0;
  44. }
  45. void close() override
  46. {
  47. if (alert != nullptr)
  48. if (alert->isCurrentlyModal())
  49. alert->exitModalState();
  50. alert = nullptr;
  51. }
  52. private:
  53. Component* setUpAlert()
  54. {
  55. auto* component = options.getAssociatedComponent();
  56. auto& lf = component != nullptr ? component->getLookAndFeel()
  57. : LookAndFeel::getDefaultLookAndFeel();
  58. alert = lf.createAlertWindow (options.getTitle(),
  59. options.getMessage(),
  60. options.getButtonText (0),
  61. options.getButtonText (1),
  62. options.getButtonText (2),
  63. options.getIconType(),
  64. options.getNumButtons(),
  65. component);
  66. if (alert == nullptr)
  67. {
  68. // You have to return an alert box!
  69. jassertfalse;
  70. return nullptr;
  71. }
  72. if (auto* parent = options.getParentComponent())
  73. {
  74. parent->addAndMakeVisible (alert);
  75. if (options.getAssociatedComponent() == nullptr)
  76. alert->setCentrePosition (parent->getLocalBounds().getCentre());
  77. }
  78. alert->setAlwaysOnTop (WindowUtils::areThereAnyAlwaysOnTopWindows());
  79. return alert;
  80. }
  81. const MessageBoxOptions options;
  82. Component::SafePointer<AlertWindow> alert;
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AlertWindowImpl)
  84. };
  85. return std::make_unique<AlertWindowImpl> (opts);
  86. }
  87. };
  88. } // namespace juce::detail