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.

135 lines
4.4KB

  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. std::unique_ptr<ScopedMessageBoxInterface> ScopedMessageBoxInterface::create (const MessageBoxOptions& options)
  21. {
  22. class OSXMessageBox : public ScopedMessageBoxInterface
  23. {
  24. public:
  25. explicit OSXMessageBox (const MessageBoxOptions& opts)
  26. : options (opts) {}
  27. void runAsync (std::function<void (int)> recipient) override
  28. {
  29. makeAlert();
  30. const auto onDone = [recipient] (NSModalResponse result)
  31. {
  32. recipient (convertResult (result));
  33. };
  34. if (auto* comp = options.getAssociatedComponent())
  35. {
  36. if (auto* peer = comp->getPeer())
  37. {
  38. if (auto* view = static_cast<NSView*> (peer->getNativeHandle()))
  39. {
  40. if (auto* window = [view window])
  41. {
  42. if (@available (macOS 10.9, *))
  43. {
  44. [alertWindow.get() beginSheetModalForWindow: window completionHandler: ^(NSModalResponse result)
  45. {
  46. onDone (result);
  47. }];
  48. return;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. const auto result = [alertWindow.get() runModal];
  55. onDone (result);
  56. }
  57. int runSync() override
  58. {
  59. makeAlert();
  60. return convertResult ([alertWindow.get() runModal]);
  61. }
  62. void close() override
  63. {
  64. if (auto* alert = alertWindow.get())
  65. [[alert window] close];
  66. }
  67. private:
  68. static int convertResult (NSModalResponse response)
  69. {
  70. switch (response)
  71. {
  72. case NSAlertFirstButtonReturn: return 0;
  73. case NSAlertSecondButtonReturn: return 1;
  74. case NSAlertThirdButtonReturn: return 2;
  75. default: break;
  76. }
  77. jassertfalse;
  78. return 0;
  79. }
  80. static void addButton (NSAlert* alert, const String& button)
  81. {
  82. if (! button.isEmpty())
  83. [alert addButtonWithTitle: juceStringToNS (button)];
  84. }
  85. void makeAlert()
  86. {
  87. NSAlert* alert = [[NSAlert alloc] init];
  88. [alert setMessageText: juceStringToNS (options.getTitle())];
  89. [alert setInformativeText: juceStringToNS (options.getMessage())];
  90. [alert setAlertStyle: options.getIconType() == MessageBoxIconType::WarningIcon ? NSAlertStyleCritical
  91. : NSAlertStyleInformational];
  92. const auto button1Text = options.getButtonText (0);
  93. addButton (alert, button1Text.isEmpty() ? "OK" : button1Text);
  94. addButton (alert, options.getButtonText (1));
  95. addButton (alert, options.getButtonText (2));
  96. alertWindow.reset (alert);
  97. }
  98. NSUniquePtr<NSAlert> alertWindow;
  99. MessageBoxOptions options;
  100. std::unique_ptr<ModalComponentManager::Callback> callback;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSXMessageBox)
  102. };
  103. return std::make_unique<OSXMessageBox> (options);
  104. }
  105. } // namespace juce::detail