Browse Source

CallOutBox: changed parameters to allow better positioning, and improved layout algorithm.

tags/2021-05-28
jules 12 years ago
parent
commit
b4fe06fb81
4 changed files with 41 additions and 38 deletions
  1. +4
    -3
      extras/Introjucer/Source/Utility/jucer_MiscUtilities.h
  2. +1
    -1
      extras/JuceDemo/Source/demos/WidgetsDemo.cpp
  3. +18
    -23
      modules/juce_gui_basics/windows/juce_CallOutBox.cpp
  4. +18
    -11
      modules/juce_gui_basics/windows/juce_CallOutBox.h

+ 4
- 3
extras/Introjucer/Source/Utility/jucer_MiscUtilities.h View File

@@ -376,9 +376,10 @@ public:
if (undoManager != nullptr) if (undoManager != nullptr)
undoManager->beginNewTransaction(); undoManager->beginNewTransaction();
CallOutBox::launchAsynchronously (*this, new PopupColourSelector (colourValue,
defaultColour,
canResetToDefault), nullptr);
CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue,
defaultColour,
canResetToDefault),
getScreenBounds(), nullptr);
} }
void valueChanged (Value&) void valueChanged (Value&)


+ 1
- 1
extras/JuceDemo/Source/demos/WidgetsDemo.cpp View File

@@ -242,7 +242,7 @@ public:
colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack); colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
colourSelector->setSize (300, 400); colourSelector->setSize (300, 400);
CallOutBox::launchAsynchronously (*this, colourSelector, nullptr);
CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr);
} }
void changeListenerCallback (ChangeBroadcaster* source) void changeListenerCallback (ChangeBroadcaster* source)


+ 18
- 23
modules/juce_gui_basics/windows/juce_CallOutBox.cpp View File

@@ -23,20 +23,15 @@
============================================================================== ==============================================================================
*/ */
CallOutBox::CallOutBox (Component& contentComponent,
Component& componentToPointTo,
Component* const parent)
: borderSpace (20), arrowSize (16.0f), content (contentComponent)
CallOutBox::CallOutBox (Component& c, const Rectangle<int>& area, Component* const parent)
: borderSpace (20), arrowSize (16.0f), content (c)
{ {
addAndMakeVisible (&content); addAndMakeVisible (&content);
if (parent != nullptr) if (parent != nullptr)
{ {
parent->addChildComponent (this); parent->addChildComponent (this);
updatePosition (parent->getLocalArea (&componentToPointTo, componentToPointTo.getLocalBounds()),
parent->getLocalBounds());
updatePosition (area, parent->getLocalBounds());
setVisible (true); setVisible (true);
} }
else else
@@ -44,8 +39,8 @@ CallOutBox::CallOutBox (Component& contentComponent,
if (! JUCEApplication::isStandaloneApp()) if (! JUCEApplication::isStandaloneApp())
setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level
updatePosition (componentToPointTo.getScreenBounds(),
componentToPointTo.getParentMonitorArea());
updatePosition (area, Desktop::getInstance().getDisplays()
.getDisplayContaining (area.getCentre()).userArea);
addToDesktop (ComponentPeer::windowIsTemporary); addToDesktop (ComponentPeer::windowIsTemporary);
} }
@@ -59,8 +54,8 @@ CallOutBox::~CallOutBox()
class CallOutBoxCallback : public ModalComponentManager::Callback class CallOutBoxCallback : public ModalComponentManager::Callback
{ {
public: public:
CallOutBoxCallback (Component& attachTo, Component* content_, Component* parentComponent)
: content (content_), callout (*content_, attachTo, parentComponent)
CallOutBoxCallback (Component* c, const Rectangle<int>& area, Component* parent)
: content (c), callout (*c, area, parent)
{ {
callout.setVisible (true); callout.setVisible (true);
callout.enterModalState (true, this); callout.enterModalState (true, this);
@@ -68,20 +63,19 @@ public:
void modalStateFinished (int) {} void modalStateFinished (int) {}
private:
ScopedPointer<Component> content; ScopedPointer<Component> content;
CallOutBox callout; CallOutBox callout;
JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback); JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback);
}; };
void CallOutBox::launchAsynchronously (Component& componentToPointTo,
Component* contentComponent,
Component* parentComponent)
CallOutBox& CallOutBox::launchAsynchronously (Component* content,
const Rectangle<int>& area,
Component* parent)
{ {
jassert (contentComponent != nullptr); // must be a valid content component!
jassert (content != nullptr); // must be a valid content component!
new CallOutBoxCallback (componentToPointTo, contentComponent, parentComponent);
return (new CallOutBoxCallback (content, area, parent))->callout;
} }
//============================================================================== //==============================================================================
@@ -185,6 +179,7 @@ void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const R
Line<float> (targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent))) }; Line<float> (targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent))) };
const Rectangle<float> centrePointArea (newAreaToFitIn.reduced (hw, hh).toFloat()); const Rectangle<float> centrePointArea (newAreaToFitIn.reduced (hw, hh).toFloat());
const Point<float> targetCentre (targetArea.getCentre().toFloat());
float nearest = 1.0e9f; float nearest = 1.0e9f;
@@ -193,19 +188,19 @@ void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const R
Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()), Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
centrePointArea.getConstrainedPoint (lines[i].getEnd())); centrePointArea.getConstrainedPoint (lines[i].getEnd()));
const Point<float> centre (constrainedLine.findNearestPointTo (centrePointArea.getCentre()));
float distanceFromCentre = centre.getDistanceFrom (centrePointArea.getCentre());
const Point<float> centre (constrainedLine.findNearestPointTo (targetCentre));
float distanceFromCentre = centre.getDistanceFrom (targets[i]);
if (! (centrePointArea.contains (lines[i].getStart()) || centrePointArea.contains (lines[i].getEnd()))) if (! (centrePointArea.contains (lines[i].getStart()) || centrePointArea.contains (lines[i].getEnd())))
distanceFromCentre *= 2.0f;
distanceFromCentre *= 50.0f;
if (distanceFromCentre < nearest) if (distanceFromCentre < nearest)
{ {
nearest = distanceFromCentre; nearest = distanceFromCentre;
targetPoint = targets[i]; targetPoint = targets[i];
newBounds.setPosition ((int) (centre.getX() - hw),
(int) (centre.getY() - hh));
newBounds.setPosition ((int) (centre.x - hw),
(int) (centre.y - hh));
} }
} }


+ 18
- 11
modules/juce_gui_basics/windows/juce_CallOutBox.h View File

@@ -64,12 +64,14 @@ public:
update itself when the component's size is changed later). update itself when the component's size is changed later).
Obviously this component must not be deleted until the Obviously this component must not be deleted until the
call-out box has been deleted. call-out box has been deleted.
@param componentToPointTo the component that the call-out's arrow should point towards
@param areaToPointTo the area that the call-out's arrow should point towards. If
a parentComponent is supplied, then this is relative to that
parent; otherwise, it's a global screen coord.
@param parentComponent if non-zero, this is the component to add the call-out to. If @param parentComponent if non-zero, this is the component to add the call-out to. If
this is zero, the call-out will be added to the desktop.
this is a nullptr, the call-out will be added to the desktop.
*/ */
CallOutBox (Component& contentComponent, CallOutBox (Component& contentComponent,
Component& componentToPointTo,
const Rectangle<int>& areaToPointTo,
Component* parentComponent); Component* parentComponent);
/** Destructor. */ /** Destructor. */
@@ -98,15 +100,20 @@ public:
the box will continue to run modally until the user clicks on some other component, at the box will continue to run modally until the user clicks on some other component, at
which point it will be dismissed automatically. which point it will be dismissed automatically.
The content component that is passed-in will be owned by the callout, which will
delete it when it is dismissed.
The parentComponent parameter can be a nullptr if you want the window to appear on
the desktop.
@param contentComponent the component to display inside the call-out. This should
already have a size set (although the call-out will also
update itself when the component's size is changed later).
This copmonent will be owned by the callout box and deleted
later when the box is dismissed.
@param areaToPointTo the area that the call-out's arrow should point towards. If
a parentComponent is supplied, then this is relative to that
parent; otherwise, it's a global screen coord.
@param parentComponent if non-zero, this is the component to add the call-out to. If
this is a nullptr, the call-out will be added to the desktop.
*/ */
static void launchAsynchronously (Component& componentToPointTo,
Component* contentComponent,
Component* parentComponent);
static CallOutBox& launchAsynchronously (Component* contentComponent,
const Rectangle<int>& areaToPointTo,
Component* parentComponent);
//============================================================================== //==============================================================================
/** @internal */ /** @internal */


Loading…
Cancel
Save