LMMS
Loading...
Searching...
No Matches
juce_CallOutBox.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
30 : content (c)
31{
33
34 if (parent != nullptr)
35 {
36 parent->addChildComponent (this);
37 updatePosition (area, parent->getLocalBounds());
38 setVisible (true);
39 }
40 else
41 {
43 updatePosition (area, Desktop::getInstance().getDisplays().getDisplayForRect (area)->userArea);
45
46 startTimer (100);
47 }
48
50}
51
52//==============================================================================
54 private Timer
55{
56public:
57 CallOutBoxCallback (std::unique_ptr<Component> c, const Rectangle<int>& area, Component* parent)
58 : content (std::move (c)),
59 callout (*content, area, parent)
60 {
61 callout.setVisible (true);
62 callout.enterModalState (true, this);
63 startTimer (200);
64 }
65
66 void modalStateFinished (int) override {}
67
68 void timerCallback() override
69 {
71 callout.dismiss();
72 }
73
74 std::unique_ptr<Component> content;
76
78};
79
81{
82 jassert (content != nullptr); // must be a valid content component!
83
84 return (new CallOutBoxCallback (std::move (content), area, parent))->callout;
85}
86
87//==============================================================================
88void CallOutBox::setArrowSize (const float newSize)
89{
90 arrowSize = newSize;
92}
93
95{
96 return jmax (getLookAndFeel().getCallOutBoxBorderSize (*this), (int) arrowSize);
97}
98
100{
101 resized();
102 repaint();
103}
104
106{
107 getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
108}
109
111{
112 auto borderSpace = getBorderSize();
113 content.setTopLeftPosition (borderSpace, borderSpace);
114 refreshPath();
115}
116
118{
119 refreshPath();
120}
121
126
127bool CallOutBox::hitTest (int x, int y)
128{
129 return outline.contains ((float) x, (float) y);
130}
131
133{
136 {
137 // if you click on the area that originally popped-up the callout, you expect it
138 // to get rid of the box, but deleting the box here allows the click to pass through and
139 // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
140
141 // For touchscreens, we make sure not to dismiss the CallOutBox immediately,
142 // as Windows still sends touch events before the CallOutBox had a chance
143 // to really open.
144
145 auto elapsed = Time::getCurrentTime() - creationTime;
146
147 if (elapsed.inMilliseconds() > 200)
148 dismiss();
149 }
150 else
151 {
152 exitModalState (0);
153 setVisible (false);
154 }
155}
156
161
162static constexpr int callOutBoxDismissCommandId = 0x4f83a04b;
163
165{
167
168 if (commandId == callOutBoxDismissCommandId)
169 {
170 exitModalState (0);
171 setVisible (false);
172 }
173}
174
179
181{
182 if (key.isKeyCode (KeyPress::escapeKey))
183 {
185 return true;
186 }
187
188 return false;
189}
190
191void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
192{
193 targetArea = newAreaToPointTo;
194 availableArea = newAreaToFitIn;
195
196 auto borderSpace = getBorderSize();
197 auto newBounds = getLocalArea (&content, Rectangle<int> (content.getWidth() + borderSpace * 2,
198 content.getHeight() + borderSpace * 2));
199
200 auto hw = newBounds.getWidth() / 2;
201 auto hh = newBounds.getHeight() / 2;
202 auto hwReduced = (float) (hw - borderSpace * 2);
203 auto hhReduced = (float) (hh - borderSpace * 2);
204 auto arrowIndent = (float) borderSpace - arrowSize;
205
206 Point<float> targets[4] = { { (float) targetArea.getCentreX(), (float) targetArea.getBottom() },
207 { (float) targetArea.getRight(), (float) targetArea.getCentreY() },
208 { (float) targetArea.getX(), (float) targetArea.getCentreY() },
209 { (float) targetArea.getCentreX(), (float) targetArea.getY() } };
210
211 Line<float> lines[4] = { { targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent) },
212 { targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced) },
213 { targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced) },
214 { targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent)) } };
215
216 auto centrePointArea = newAreaToFitIn.reduced (hw, hh).toFloat();
217 auto targetCentre = targetArea.getCentre().toFloat();
218
219 float nearest = 1.0e9f;
220
221 for (int i = 0; i < 4; ++i)
222 {
223 Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
224 centrePointArea.getConstrainedPoint (lines[i].getEnd()));
225
226 auto centre = constrainedLine.findNearestPointTo (targetCentre);
227 auto distanceFromCentre = centre.getDistanceFrom (targets[i]);
228
229 if (! centrePointArea.intersects (lines[i]))
230 distanceFromCentre += 1000.0f;
231
232 if (distanceFromCentre < nearest)
233 {
234 nearest = distanceFromCentre;
235 targetPoint = targets[i];
236
237 newBounds.setPosition ((int) (centre.x - (float) hw),
238 (int) (centre.y - (float) hh));
239 }
240 }
241
242 setBounds (newBounds);
243}
244
246{
247 repaint();
248 background = {};
249 outline.clear();
250
251 const float gap = 4.5f;
252
253 outline.addBubble (getLocalArea (&content, content.getLocalBounds().toFloat()).expanded (gap, gap),
254 getLocalBounds().toFloat(),
255 targetPoint - getPosition().toFloat(),
256 getLookAndFeel().getCallOutBoxCornerSize (*this), arrowSize * 0.7f);
257}
258
260{
261 toFront (true);
262 stopTimer();
263}
264
265//==============================================================================
266std::unique_ptr<AccessibilityHandler> CallOutBox::createAccessibilityHandler()
267{
268 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::dialogWindow);
269}
270
271} // namespace juce
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_CallOutBox.cpp:55
void timerCallback() override
Definition juce_CallOutBox.cpp:68
CallOutBoxCallback(std::unique_ptr< Component > c, const Rectangle< int > &area, Component *parent)
Definition juce_CallOutBox.cpp:57
void modalStateFinished(int) override
Definition juce_CallOutBox.cpp:66
std::unique_ptr< Component > content
Definition juce_CallOutBox.cpp:74
CallOutBox callout
Definition juce_CallOutBox.cpp:75
Definition juce_CallOutBox.h:61
void inputAttemptWhenModal() override
Definition juce_CallOutBox.cpp:132
bool keyPressed(const KeyPress &) override
Definition juce_CallOutBox.cpp:180
void handleCommandMessage(int) override
Definition juce_CallOutBox.cpp:164
void dismiss()
Definition juce_CallOutBox.cpp:175
bool dismissalMouseClicksAreAlwaysConsumed
Definition juce_CallOutBox.h:176
Time creationTime
Definition juce_CallOutBox.h:178
int getBorderSize() const noexcept
Definition juce_CallOutBox.cpp:94
Path outline
Definition juce_CallOutBox.h:171
void updatePosition(const Rectangle< int > &newAreaToPointTo, const Rectangle< int > &newAreaToFitIn)
Definition juce_CallOutBox.cpp:191
void childBoundsChanged(Component *) override
Definition juce_CallOutBox.cpp:122
Point< float > targetPoint
Definition juce_CallOutBox.h:172
void refreshPath()
Definition juce_CallOutBox.cpp:245
float arrowSize
Definition juce_CallOutBox.h:175
Image background
Definition juce_CallOutBox.h:174
void setDismissalMouseClicksAreAlwaysConsumed(bool shouldAlwaysBeConsumed) noexcept
Definition juce_CallOutBox.cpp:157
Component & content
Definition juce_CallOutBox.h:170
void timerCallback() override
Definition juce_CallOutBox.cpp:259
void setArrowSize(float newSize)
Definition juce_CallOutBox.cpp:88
static CallOutBox & launchAsynchronously(std::unique_ptr< Component > contentComponent, Rectangle< int > areaToPointTo, Component *parentComponent)
Definition juce_CallOutBox.cpp:80
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Definition juce_CallOutBox.cpp:266
CallOutBox(Component &contentComponent, Rectangle< int > areaToPointTo, Component *parentComponent)
Definition juce_CallOutBox.cpp:29
bool hitTest(int x, int y) override
Definition juce_CallOutBox.cpp:127
void paint(Graphics &) override
Definition juce_CallOutBox.cpp:105
void lookAndFeelChanged() override
Definition juce_CallOutBox.cpp:99
Rectangle< int > targetArea
Definition juce_CallOutBox.h:173
Rectangle< int > availableArea
Definition juce_CallOutBox.h:173
void resized() override
Definition juce_CallOutBox.cpp:110
void moved() override
Definition juce_CallOutBox.cpp:117
Definition juce_Component.h:36
Point< int > getPosition() const noexcept
Definition juce_Component.h:286
Rectangle< int > getLocalArea(const Component *sourceComponent, Rectangle< int > areaRelativeToSourceComponent) const
Definition juce_Component.cpp:1138
void toFront(bool shouldAlsoGainKeyboardFocus)
Definition juce_Component.cpp:954
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
void setAlwaysOnTop(bool shouldStayOnTop)
Definition juce_Component.cpp:1074
void postCommandMessage(int commandId)
Definition juce_Component.cpp:2361
Rectangle< int > getBounds() const noexcept
Definition juce_Component.h:304
void repaint()
Definition juce_Component.cpp:1917
Component() noexcept
Definition juce_Component.cpp:517
Point< int > getMouseXYRelative() const
Definition juce_Component.cpp:3210
void exitModalState(int returnValue)
Definition juce_Component.cpp:1795
virtual void addToDesktop(int windowStyleFlags, void *nativeWindowToAttachTo=nullptr)
Definition juce_Component.cpp:658
void setBounds(int x, int y, int width, int height)
Definition juce_Component.cpp:1147
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
virtual void setVisible(bool shouldBeVisible)
Definition juce_Component.cpp:575
virtual void handleCommandMessage(int commandId)
Definition juce_Component.cpp:2370
@ windowIsTemporary
Definition juce_ComponentPeer.h:52
static Desktop &JUCE_CALLTYPE getInstance()
Definition juce_Desktop.cpp:50
Definition juce_GraphicsContext.h:45
Definition juce_KeyPress.h:40
static const int escapeKey
Definition juce_KeyPress.h:190
Definition juce_Line.h:47
Point< ValueType > findNearestPointTo(Point< ValueType > point) const noexcept
Definition juce_Line.h:315
Point< ValueType > getEnd() const noexcept
Definition juce_Line.h:91
Point< ValueType > getStart() const noexcept
Definition juce_Line.h:88
Definition juce_ModalComponentManager.h:56
Definition juce_Point.h:42
constexpr Point translated(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Point.h:92
Definition juce_Rectangle.h:67
Rectangle< float > toFloat() const noexcept
Definition juce_Rectangle.h:873
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Rectangle.h:485
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
void stopTimer() noexcept
Definition juce_Timer.cpp:357
Timer() noexcept
Definition juce_Timer.cpp:316
void startTimer(int intervalInMilliseconds) noexcept
Definition juce_Timer.cpp:332
int y
Definition inflate.c:1588
int g
Definition inflate.c:1573
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
static uintptr_t parent
Definition pugl.h:1644
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition carla_juce.cpp:31
static constexpr int callOutBoxDismissCommandId
Definition juce_CallOutBox.cpp:162
bool juce_areThereAnyAlwaysOnTopWindows()
Definition juce_linux_Windowing.cpp:31
@ dialogWindow
Definition juce_AccessibilityRole.h:62
static bool isForegroundOrEmbeddedProcess(Component *viewComponent)
Definition juce_gui_basics.cpp:120
Definition juce_Uuid.h:141
return c
Definition crypt.c:175
ZCONST char * key
Definition crypt.c:587
b
Definition crypt.c:628
uch hh[RAND_HEAD_LEN]
Definition crypt.c:595
#define const
Definition zconf.h:137