From c65c07312d14e433e2cc197fe12d799705b1dfc8 Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Tue, 16 Aug 2011 10:45:44 +0100 Subject: [PATCH] Small fixes for Viewport and Identifier. --- modules/juce_core/memory/juce_ScopedPointer.h | 2 +- modules/juce_core/text/juce_Identifier.cpp | 2 +- .../components/juce_Component.cpp | 7 ++- .../components/juce_Component.h | 15 +++++++ .../juce_gui_basics/layout/juce_Viewport.cpp | 45 ++++++++++++------- .../juce_gui_basics/layout/juce_Viewport.h | 1 + .../juce_gui_basics/menus/juce_PopupMenu.cpp | 2 +- .../mouse/juce_DragAndDropContainer.cpp | 2 +- 8 files changed, 55 insertions(+), 21 deletions(-) diff --git a/modules/juce_core/memory/juce_ScopedPointer.h b/modules/juce_core/memory/juce_ScopedPointer.h index 5d9f1ea59e..359067b0be 100644 --- a/modules/juce_core/memory/juce_ScopedPointer.h +++ b/modules/juce_core/memory/juce_ScopedPointer.h @@ -47,7 +47,7 @@ If you need to get a pointer out of a ScopedPointer without it being deleted, you can use the release() method. - + Something to note is the main difference between this class and the std::auto_ptr class, which is that ScopedPointer provides a cast-to-object operator, wheras std::auto_ptr requires that you always call get() to retrieve the pointer. The advantages of providing diff --git a/modules/juce_core/text/juce_Identifier.cpp b/modules/juce_core/text/juce_Identifier.cpp index a1e66a974b..efa6562565 100644 --- a/modules/juce_core/text/juce_Identifier.cpp +++ b/modules/juce_core/text/juce_Identifier.cpp @@ -71,7 +71,7 @@ Identifier::~Identifier() bool Identifier::isValidIdentifier (const String& possibleIdentifier) noexcept { return possibleIdentifier.isNotEmpty() - && possibleIdentifier.containsOnly ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:"); + && possibleIdentifier.containsOnly ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:#@$%"); } END_JUCE_NAMESPACE diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 148e81c70c..845f747854 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -593,7 +593,7 @@ void Component::addToDesktop (int styleWanted, void* nativeWindowToAttachTo) removeFromDesktop(); - setTopLeftPosition (topLeft.getX(), topLeft.getY()); + setTopLeftPosition (topLeft); } if (parentComponent != nullptr) @@ -1063,6 +1063,11 @@ void Component::setTopLeftPosition (const int x, const int y) setBounds (x, y, getWidth(), getHeight()); } +void Component::setTopLeftPosition (const Point& pos) +{ + setBounds (pos.getX(), pos.getY(), getWidth(), getHeight()); +} + void Component::setTopRightPosition (const int x, const int y) { setTopLeftPosition (x - getWidth(), y); diff --git a/modules/juce_gui_basics/components/juce_Component.h b/modules/juce_gui_basics/components/juce_Component.h index 0bead08d7b..4cb4288bc9 100644 --- a/modules/juce_gui_basics/components/juce_Component.h +++ b/modules/juce_gui_basics/components/juce_Component.h @@ -415,6 +415,21 @@ public: */ void setTopLeftPosition (int x, int y); + /** Moves the component to a new position. + + Changes the component's top-left position (without changing its size). + The position is relative to the top-left of the component's parent. + + If the component actually moves, this method will make a synchronous call to moved(). + + Note that if you've used setTransform() to apply a transform, then the component's + bounds will no longer be a direct reflection of the position at which it appears within + its parent, as the transform will be applied to whatever bounds you set for it. + + @see setBounds, ComponentListener::componentMovedOrResized + */ + void setTopLeftPosition (const Point& newTopLeftPosition); + /** Moves the component to a new position. Changes the position of the component's top-right corner (keeping it the same size). diff --git a/modules/juce_gui_basics/layout/juce_Viewport.cpp b/modules/juce_gui_basics/layout/juce_Viewport.cpp index 23763b63d4..7d66e48db7 100644 --- a/modules/juce_gui_basics/layout/juce_Viewport.cpp +++ b/modules/juce_gui_basics/layout/juce_Viewport.cpp @@ -88,7 +88,7 @@ void Viewport::setViewedComponent (Component* const newViewedComponent, const bo if (contentComp != nullptr) { contentHolder.addAndMakeVisible (contentComp); - setViewPosition (0, 0); + setViewPosition (Point()); contentComp->addComponentListener (this); } @@ -99,22 +99,28 @@ void Viewport::setViewedComponent (Component* const newViewedComponent, const bo int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); } int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); } +Point Viewport::viewportPosToCompPos (const Point& pos) const +{ + jassert (contentComp != nullptr); + return Point (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -pos.getX())), + jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -pos.getY()))); +} + void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset) { - if (contentComp != nullptr) - contentComp->setTopLeftPosition (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -xPixelsOffset)), - jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -yPixelsOffset))); + setViewPosition (Point (xPixelsOffset, yPixelsOffset)); } void Viewport::setViewPosition (const Point& newPosition) { - setViewPosition (newPosition.getX(), newPosition.getY()); + if (contentComp != nullptr) + contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition)); } void Viewport::setViewPositionProportionately (const double x, const double y) { if (contentComp != nullptr) - setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))), + setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))), jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight())))); } @@ -247,20 +253,27 @@ void Viewport::updateVisibleArea() horizontalScrollBar.setVisible (hBarVisible); verticalScrollBar.setVisible (vBarVisible); - setViewPosition (visibleOrigin); + const Point newContentCompPos (viewportPosToCompPos (visibleOrigin)); - const Rectangle visibleArea (visibleOrigin.getX(), visibleOrigin.getY(), - jmin (contentBounds.getWidth() - visibleOrigin.getX(), contentArea.getWidth()), - jmin (contentBounds.getHeight() - visibleOrigin.getY(), contentArea.getHeight())); - - if (lastVisibleArea != visibleArea) + if (contentComp != nullptr && contentComp->getBounds().getPosition() != newContentCompPos) { - lastVisibleArea = visibleArea; - visibleAreaChanged (visibleArea); + contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again) } + else + { + const Rectangle visibleArea (visibleOrigin.getX(), visibleOrigin.getY(), + jmin (contentBounds.getWidth() - visibleOrigin.getX(), contentArea.getWidth()), + jmin (contentBounds.getHeight() - visibleOrigin.getY(), contentArea.getHeight())); - horizontalScrollBar.handleUpdateNowIfNeeded(); - verticalScrollBar.handleUpdateNowIfNeeded(); + if (lastVisibleArea != visibleArea) + { + lastVisibleArea = visibleArea; + visibleAreaChanged (visibleArea); + } + + horizontalScrollBar.handleUpdateNowIfNeeded(); + verticalScrollBar.handleUpdateNowIfNeeded(); + } } //============================================================================== diff --git a/modules/juce_gui_basics/layout/juce_Viewport.h b/modules/juce_gui_basics/layout/juce_Viewport.h index 1c0c9cc622..158495a019 100644 --- a/modules/juce_gui_basics/layout/juce_Viewport.h +++ b/modules/juce_gui_basics/layout/juce_Viewport.h @@ -263,6 +263,7 @@ private: Component contentHolder; ScrollBar verticalScrollBar; ScrollBar horizontalScrollBar; + Point viewportPosToCompPos (const Point&) const; void updateVisibleArea(); void deleteContentComp(); diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index 9f9045957a..cd4bc4ec76 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -273,7 +273,7 @@ public: items.add (new PopupMenu::ItemComponent (*menu.items.getUnchecked(i), standardItemHeight, this)); calculateWindowPos (target, alignToRectangle); - setTopLeftPosition (windowPos.getX(), windowPos.getY()); + setTopLeftPosition (windowPos.getPosition()); updateYPositions(); if (itemIdThatMustBeVisible != 0) diff --git a/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp b/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp index 8dac7bcef7..d20aedd24c 100644 --- a/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp +++ b/modules/juce_gui_basics/mouse/juce_DragAndDropContainer.cpp @@ -193,7 +193,7 @@ public: //if (newX != getX() || newY != getY()) { - setTopLeftPosition (newPos.getX(), newPos.getY()); + setTopLeftPosition (newPos); Point relPos; DragAndDropTarget* const newTarget = findTarget (screenPos, relPos);