JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_NativeMessageBox.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29enum class ResultCodeMappingMode
30{
31 plainIndex, // The result code is equal to the index of the selected button.
32 // This is used for NativeMessageBox::show, showAsync, and showMessageBox.
33 alertWindow, // The result code is mapped in the same way as AlertWindow, i.e. if there
34 // are N buttons then button X will return ((X + 1) % N).
35};
36
37static std::unique_ptr<detail::ScopedMessageBoxInterface> makeNativeMessageBoxWithMappedResult (const MessageBoxOptions& opts,
38 ResultCodeMappingMode mode)
39{
40 class Adapter final : public detail::ScopedMessageBoxInterface
41 {
42 public:
43 explicit Adapter (const MessageBoxOptions& options)
44 : inner (detail::ScopedMessageBoxInterface::create (options)),
45 numButtons (options.getNumButtons()) {}
46
47 void runAsync (std::function<void (int)> fn) override
48 {
49 inner->runAsync ([fn, n = numButtons] (int result)
50 {
51 fn (map (result, n));
52 });
53 }
54
55 int runSync() override
56 {
57 return map (inner->runSync(), numButtons);
58 }
59
60 void close() override
61 {
62 inner->close();
63 }
64
65 private:
66 static int map (int button, int numButtons) { return (button + 1) % numButtons; }
67
69 int numButtons = 0;
70 };
71
72 return mode == ResultCodeMappingMode::plainIndex ? detail::ScopedMessageBoxInterface::create (opts)
74}
75
76static int showNativeBoxUnmanaged (const MessageBoxOptions& opts,
77 ModalComponentManager::Callback* cb,
78 ResultCodeMappingMode mode)
79{
80 auto implementation = makeNativeMessageBoxWithMappedResult (opts, mode);
81 return detail::ConcreteScopedMessageBoxImpl::showUnmanaged (std::move (implementation), cb);
82}
83
84#if JUCE_MODAL_LOOPS_PERMITTED
85void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType,
86 const String& title, const String& message,
87 Component* associatedComponent)
88{
89 showNativeBoxUnmanaged (MessageBoxOptions().withIconType (iconType)
90 .withTitle (title)
91 .withMessage (message)
92 .withButton (TRANS ("OK"))
93 .withAssociatedComponent (associatedComponent),
94 nullptr,
95 ResultCodeMappingMode::plainIndex);
96}
97
98int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
99{
100 return showNativeBoxUnmanaged (options, nullptr, ResultCodeMappingMode::plainIndex);
101}
102#endif
103
105 const String& title, const String& message,
106 Component* associatedComponent,
108{
109 auto options = MessageBoxOptions::makeOptionsOk (iconType, title, message, {}, associatedComponent);
110 showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
111}
112
114 const String& title, const String& message,
115 Component* associatedComponent,
117{
118 auto options = MessageBoxOptions::makeOptionsOkCancel (iconType, title, message, {}, {}, associatedComponent);
119 return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow) != 0;
120}
121
123 const String& title, const String& message,
124 Component* associatedComponent,
126{
127 auto options = MessageBoxOptions::makeOptionsYesNoCancel (iconType, title, message, {}, {}, {}, associatedComponent);
128 return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
129}
130
132 const String& title, const String& message,
133 Component* associatedComponent,
135{
136 auto options = MessageBoxOptions::makeOptionsYesNo (iconType, title, message, {}, {}, associatedComponent);
137 return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
138}
139
142{
143 showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::plainIndex);
144}
145
147 std::function<void (int)> callback)
148{
149 showAsync (options, ModalCallbackFunction::create (callback));
150}
151
153{
154 auto implementation = makeNativeMessageBoxWithMappedResult (options, ResultCodeMappingMode::alertWindow);
155 return detail::ConcreteScopedMessageBoxImpl::show (std::move (implementation), std::move (callback));
156}
157
158} // namespace juce
The base class for all JUCE user-interface objects.
Class used to create a set of options to pass to the AlertWindow and NativeMessageBox methods for sho...
static MessageBoxOptions makeOptionsYesNo(MessageBoxIconType iconType, const String &title, const String &message, const String &button1Text=String(), const String &button2Text=String(), Component *associatedComponent=nullptr)
Creates options suitable for a message box with two buttons.
static MessageBoxOptions makeOptionsYesNoCancel(MessageBoxIconType iconType, const String &title, const String &message, const String &button1Text=String(), const String &button2Text=String(), const String &button3Text=String(), Component *associatedComponent=nullptr)
Creates options suitable for a message box with three buttons.
static MessageBoxOptions makeOptionsOkCancel(MessageBoxIconType iconType, const String &title, const String &message, const String &button1Text=String(), const String &button2Text=String(), Component *associatedComponent=nullptr)
Creates options suitable for a message box with two buttons.
static MessageBoxOptions makeOptionsOk(MessageBoxIconType iconType, const String &title, const String &message, const String &buttonText=String(), Component *associatedComponent=nullptr)
Creates options suitable for a message box with a single button.
static ModalComponentManager::Callback * create(CallbackFn &&fn)
This is a utility function to create a ModalComponentManager::Callback that will call a callable obje...
Receives callbacks when a modal component is dismissed.
static int JUCE_CALLTYPE showYesNoBox(MessageBoxIconType iconType, const String &title, const String &message, Component *associatedComponent, ModalComponentManager::Callback *callback)
Shows a dialog box with two buttons.
static void JUCE_CALLTYPE showMessageBoxAsync(MessageBoxIconType iconType, const String &title, const String &message, Component *associatedComponent=nullptr, ModalComponentManager::Callback *callback=nullptr)
Shows a dialog box that just has a message and a single 'ok' button to close it.
static bool JUCE_CALLTYPE showOkCancelBox(MessageBoxIconType iconType, const String &title, const String &message, Component *associatedComponent, ModalComponentManager::Callback *callback)
Shows a dialog box with two buttons.
static ScopedMessageBox showScopedAsync(const MessageBoxOptions &options, std::function< void(int)> callback)
Shows a dialog box using the specified options.
static int JUCE_CALLTYPE showYesNoCancelBox(MessageBoxIconType iconType, const String &title, const String &message, Component *associatedComponent, ModalComponentManager::Callback *callback)
Shows a dialog box with three buttons.
static void JUCE_CALLTYPE showAsync(const MessageBoxOptions &options, ModalComponentManager::Callback *callback)
Shows a dialog box using the specified options.
Objects of this type can be used to programmatically close message boxes.
The JUCE String class!
Definition juce_String.h:53
close
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
T make_unique(T... args)
JUCE Namespace.
MessageBoxIconType
The type of icon to show in the dialog box.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88