From dbdea7c6d4f7ed1d18bf37201f05902b68017a51 Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Fri, 29 Oct 2010 18:55:48 +0100 Subject: [PATCH] Removed the Component::getComponentUID() method, and slimmed down the component class implementation slightly. --- .../src/model/jucer_ComponentLayout.cpp | 2 +- juce_amalgamated.cpp | 547 +++++++----------- juce_amalgamated.h | 18 +- src/gui/components/juce_Component.cpp | 500 +++++----------- src/gui/components/juce_Component.h | 18 +- src/gui/components/mouse/juce_MouseCursor.cpp | 21 +- src/native/linux/juce_linux_Audio.cpp | 4 +- src/native/mac/juce_mac_MouseCursor.mm | 7 +- src/native/windows/juce_win32_Windowing.cpp | 7 +- 9 files changed, 394 insertions(+), 730 deletions(-) diff --git a/extras/the jucer/src/model/jucer_ComponentLayout.cpp b/extras/the jucer/src/model/jucer_ComponentLayout.cpp index 687364dc4f..8c763fa6e7 100644 --- a/extras/the jucer/src/model/jucer_ComponentLayout.cpp +++ b/extras/the jucer/src/model/jucer_ComponentLayout.cpp @@ -557,7 +557,7 @@ PopupMenu ComponentLayout::getRelativeTargetMenu (Component* comp, int whichDime { Component* const c = components.getUnchecked(i); - if (c->getComponentUID() != comp->getComponentUID()) + if (c != comp) { m.addItem (menuIdBase + i + 1, T("Relative to ") + getComponentMemberVariableName (c) diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index 8c9cb9dafd..36c333a749 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -39862,18 +39862,158 @@ enum ComponentMessageNumbers exitModalStateMessage = 0x7fff0002 }; -static uint32 nextComponentUID = 0; Component* Component::currentlyFocusedComponent = 0; +class Component::MouseListenerList +{ +public: + MouseListenerList() + : numDeepMouseListeners (0) + { + } + + ~MouseListenerList() + { + } + + void addListener (MouseListener* const newListener, const bool wantsEventsForAllNestedChildComponents) + { + if (! listeners.contains (newListener)) + { + if (wantsEventsForAllNestedChildComponents) + { + listeners.insert (0, newListener); + ++numDeepMouseListeners; + } + else + { + listeners.add (newListener); + } + } + } + + void removeListener (MouseListener* const listenerToRemove) + { + const int index = listeners.indexOf (listenerToRemove); + + if (index >= 0) + { + if (index < numDeepMouseListeners) + --numDeepMouseListeners; + + listeners.remove (index); + } + } + + static void sendMouseEvent (Component* comp, BailOutChecker& checker, + void (MouseListener::*eventMethod) (const MouseEvent&), const MouseEvent& e) + { + if (checker.shouldBailOut()) + return; + + { + MouseListenerList* const list = comp->mouseListeners_; + + if (list != 0) + { + for (int i = list->listeners.size(); --i >= 0;) + { + (list->listeners.getUnchecked(i)->*eventMethod) (e); + + if (checker.shouldBailOut()) + return; + + i = jmin (i, list->listeners.size()); + } + } + } + + Component* p = comp->parentComponent_; + + while (p != 0) + { + MouseListenerList* const list = p->mouseListeners_; + + if (list != 0 && list->numDeepMouseListeners > 0) + { + BailOutChecker checker2 (comp, p); + + for (int i = list->numDeepMouseListeners; --i >= 0;) + { + (list->listeners.getUnchecked(i)->*eventMethod) (e); + + if (checker2.shouldBailOut()) + return; + + i = jmin (i, list->numDeepMouseListeners); + } + } + + p = p->parentComponent_; + } + } + + static void sendWheelEvent (Component* comp, BailOutChecker& checker, const MouseEvent& e, + const float wheelIncrementX, const float wheelIncrementY) + { + if (checker.shouldBailOut()) + return; + + { + MouseListenerList* const list = comp->mouseListeners_; + + if (list != 0) + { + for (int i = list->listeners.size(); --i >= 0;) + { + list->listeners.getUnchecked(i)->mouseWheelMove (e, wheelIncrementX, wheelIncrementY); + + if (checker.shouldBailOut()) + return; + + i = jmin (i, list->listeners.size()); + } + } + } + + Component* p = comp->parentComponent_; + + while (p != 0) + { + MouseListenerList* const list = p->mouseListeners_; + + if (list != 0 && list->numDeepMouseListeners > 0) + { + BailOutChecker checker2 (comp, p); + + for (int i = list->numDeepMouseListeners; --i >= 0;) + { + list->listeners.getUnchecked(i)->mouseWheelMove (e, wheelIncrementX, wheelIncrementY); + + if (checker2.shouldBailOut()) + return; + + i = jmin (i, list->numDeepMouseListeners); + } + } + + p = p->parentComponent_; + } + } + +private: + Array listeners; + int numDeepMouseListeners; + + MouseListenerList (const MouseListenerList&); + MouseListenerList& operator= (const MouseListenerList&); +}; + Component::Component() : parentComponent_ (0), - componentUID (++nextComponentUID), - numDeepMouseListeners (0), lookAndFeel_ (0), effect_ (0), bufferedImage_ (0), - mouseListeners_ (0), - keyListeners_ (0), componentFlags_ (0), componentTransparency (0) { @@ -39882,13 +40022,9 @@ Component::Component() Component::Component (const String& name) : componentName_ (name), parentComponent_ (0), - componentUID (++nextComponentUID), - numDeepMouseListeners (0), lookAndFeel_ (0), effect_ (0), bufferedImage_ (0), - mouseListeners_ (0), - keyListeners_ (0), componentFlags_ (0), componentTransparency (0) { @@ -39915,9 +40051,6 @@ Component::~Component() for (int i = childComponentList_.size(); --i >= 0;) childComponentList_.getUnchecked(i)->parentComponent_ = 0; - - delete mouseListeners_; - delete keyListeners_; } void Component::setName (const String& name) @@ -41756,20 +41889,9 @@ void Component::addMouseListener (MouseListener* const newListener, jassert ((newListener != this) || wantsEventsForAllNestedChildComponents); if (mouseListeners_ == 0) - mouseListeners_ = new Array(); + mouseListeners_ = new MouseListenerList(); - if (! mouseListeners_->contains (newListener)) - { - if (wantsEventsForAllNestedChildComponents) - { - mouseListeners_->insert (0, newListener); - ++numDeepMouseListeners; - } - else - { - mouseListeners_->add (newListener); - } - } + mouseListeners_->addListener (newListener, wantsEventsForAllNestedChildComponents); } void Component::removeMouseListener (MouseListener* const listenerToRemove) @@ -41779,17 +41901,7 @@ void Component::removeMouseListener (MouseListener* const listenerToRemove) checkMessageManagerIsLocked if (mouseListeners_ != 0) - { - const int index = mouseListeners_->indexOf (listenerToRemove); - - if (index >= 0) - { - if (index < numDeepMouseListeners) - --numDeepMouseListeners; - - mouseListeners_->remove (index); - } - } + mouseListeners_->removeListener (listenerToRemove); } void Component::internalMouseEnter (MouseInputSource& source, const Point& relativePos, const Time& time) @@ -41813,9 +41925,7 @@ void Component::internalMouseEnter (MouseInputSource& source, const Point& repaint(); const MouseEvent me (source, relativePos, source.getCurrentModifiers(), - this, this, time, relativePos, - time, 0, false); - + this, this, time, relativePos, time, 0, false); mouseEnter (me); if (checker.shouldBailOut()) @@ -41824,43 +41934,7 @@ void Component::internalMouseEnter (MouseInputSource& source, const Point& Desktop::getInstance().resetTimer(); Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseEnter, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - mouseListeners_->getUnchecked(i)->mouseEnter (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked(i)->mouseEnter (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseEnter, me); } } @@ -41886,8 +41960,7 @@ void Component::internalMouseExit (MouseInputSource& source, const Point& r repaint(); const MouseEvent me (source, relativePos, source.getCurrentModifiers(), - this, this, time, relativePos, - time, 0, false); + this, this, time, relativePos, time, 0, false); mouseExit (me); if (checker.shouldBailOut()) @@ -41896,43 +41969,7 @@ void Component::internalMouseExit (MouseInputSource& source, const Point& r Desktop::getInstance().resetTimer(); Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseExit, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseExit (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseExit (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseExit, me); } } @@ -42060,43 +42097,7 @@ void Component::internalMouseDown (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDown, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDown (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDown (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDown, me); } void Component::internalMouseUp (MouseInputSource& source, const Point& relativePos, const Time& time, const ModifierKeys& oldModifiers) @@ -42127,95 +42128,21 @@ void Component::internalMouseUp (MouseInputSource& source, const Point& rel desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseUp, me); + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseUp, me); + if (checker.shouldBailOut()) return; - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseUp (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - { - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseUp (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } - } - // check for double-click if (me.getNumberOfClicks() >= 2) { - const int numListeners = (mouseListeners_ != 0) ? mouseListeners_->size() : 0; - mouseDoubleClick (me); if (checker.shouldBailOut()) return; desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDoubleClick, me); - - if (checker.shouldBailOut()) - return; - - for (int i = numListeners; --i >= 0;) - { - if (checker.shouldBailOut()) - return; - - MouseListener* const ml = (MouseListener*)((*mouseListeners_)[i]); - if (ml != 0) - ml->mouseDoubleClick (me); - } - - if (checker.shouldBailOut()) - return; - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDoubleClick (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDoubleClick, me); } } } @@ -42245,43 +42172,7 @@ void Component::internalMouseDrag (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDrag (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDrag (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDrag, me); } } @@ -42311,43 +42202,7 @@ void Component::internalMouseMove (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseMove, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseMove (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseMove (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseMove, me); } } @@ -42377,43 +42232,7 @@ void Component::internalMouseWheel (MouseInputSource& source, const Point& desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheelIncrementX, wheelIncrementY); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseWheelMove (me, wheelIncrementX, wheelIncrementY); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseWheelMove (me, wheelIncrementX, wheelIncrementY); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendWheelEvent (this, checker, me, wheelIncrementX, wheelIncrementY); } } @@ -71403,15 +71222,13 @@ private: }; MouseCursor::MouseCursor() - : cursorHandle (SharedCursorHandle::createStandard (NormalCursor)) + : cursorHandle (0) { - jassert (cursorHandle != 0); } MouseCursor::MouseCursor (const StandardCursorType type) - : cursorHandle (SharedCursorHandle::createStandard (type)) + : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : 0) { - jassert (cursorHandle != 0); } MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY) @@ -71420,19 +71237,24 @@ MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotS } MouseCursor::MouseCursor (const MouseCursor& other) - : cursorHandle (other.cursorHandle->retain()) + : cursorHandle (other.cursorHandle == 0 ? 0 : other.cursorHandle->retain()) { } MouseCursor::~MouseCursor() { - cursorHandle->release(); + if (cursorHandle != 0) + cursorHandle->release(); } MouseCursor& MouseCursor::operator= (const MouseCursor& other) { - other.cursorHandle->retain(); - cursorHandle->release(); + if (other.cursorHandle != 0) + other.cursorHandle->retain(); + + if (cursorHandle != 0) + cursorHandle->release(); + cursorHandle = other.cursorHandle; return *this; } @@ -71449,7 +71271,7 @@ bool MouseCursor::operator!= (const MouseCursor& other) const throw() void* MouseCursor::getHandle() const throw() { - return cursorHandle->getHandle(); + return cursorHandle != 0 ? cursorHandle->getHandle() : 0; } void MouseCursor::showWaitCursor() @@ -244300,7 +244122,12 @@ void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorT void MouseCursor::showInWindow (ComponentPeer*) const { - SetCursor ((HCURSOR) getHandle()); + HCURSOR c = (HCURSOR) getHandle(); + + if (c == 0) + c = LoadCursor (0, IDC_ARROW); + + SetCursor (c); } void MouseCursor::showInAllWindows() const @@ -258483,6 +258310,8 @@ public: if (! XInitImage (xImage)) jassertfalse; } + + zeromem (imageData, h * lineStride); } ~XBitmapImage() @@ -261493,7 +261322,8 @@ public: bitDepth (16), numChannelsRunning (0), latency (0), - isInput (forInput) + isInput (forInput), + isInterleaved (true) { failed (snd_pcm_open (&handle, deviceID.toUTF8(), forInput ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, @@ -261654,6 +261484,7 @@ public: if (isInterleaved) { scratch.ensureSize (sizeof (float) * numSamples * numChannelsRunning, false); + scratch.fillWith (0); // (not clearing this data causes warnings in valgrind) snd_pcm_sframes_t num = snd_pcm_readi (handle, scratch.getData(), numSamples); @@ -269587,7 +269418,12 @@ void MouseCursor::showInAllWindows() const void MouseCursor::showInWindow (ComponentPeer*) const { - [((NSCursor*) getHandle()) set]; + NSCursor* c = (NSCursor*) getHandle(); + + if (c == 0) + c = [NSCursor arrowCursor]; + + [c set]; } #else @@ -274171,7 +274007,12 @@ void MouseCursor::showInAllWindows() const void MouseCursor::showInWindow (ComponentPeer*) const { - [((NSCursor*) getHandle()) set]; + NSCursor* c = (NSCursor*) getHandle(); + + if (c == 0) + c = [NSCursor arrowCursor]; + + [c set]; } #else diff --git a/juce_amalgamated.h b/juce_amalgamated.h index d2b65ce693..9a16d40a56 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -27802,14 +27802,6 @@ public: */ void* getWindowHandle() const; - /** When created, each component is given a number to uniquely identify it. - - The number is incremented each time a new component is created, so it's a more - unique way of identifying a component than using its memory location (which - may be reused after the component is deleted, of course). - */ - uint32 getComponentUID() const throw() { return componentUID; } - /** Holds a pointer to some type of Component, which automatically becomes null if the component is deleted. @@ -27918,16 +27910,18 @@ private: String componentName_; Component* parentComponent_; - uint32 componentUID; Rectangle bounds_; - int numDeepMouseListeners; Array childComponentList_; LookAndFeel* lookAndFeel_; MouseCursor cursor_; ImageEffectFilter* effect_; Image bufferedImage_; - Array * mouseListeners_; - Array * keyListeners_; + + class MouseListenerList; + friend class MouseListenerList; + friend class ScopedPointer ; + ScopedPointer mouseListeners_; + ScopedPointer > keyListeners_; ListenerList componentListeners; NamedValueSet properties; diff --git a/src/gui/components/juce_Component.cpp b/src/gui/components/juce_Component.cpp index aa5ebc3435..2adb7ca8c1 100644 --- a/src/gui/components/juce_Component.cpp +++ b/src/gui/components/juce_Component.cpp @@ -52,20 +52,161 @@ enum ComponentMessageNumbers exitModalStateMessage = 0x7fff0002 }; -static uint32 nextComponentUID = 0; Component* Component::currentlyFocusedComponent = 0; +//============================================================================== +class Component::MouseListenerList +{ +public: + MouseListenerList() + : numDeepMouseListeners (0) + { + } + + ~MouseListenerList() + { + } + + void addListener (MouseListener* const newListener, const bool wantsEventsForAllNestedChildComponents) + { + if (! listeners.contains (newListener)) + { + if (wantsEventsForAllNestedChildComponents) + { + listeners.insert (0, newListener); + ++numDeepMouseListeners; + } + else + { + listeners.add (newListener); + } + } + } + + void removeListener (MouseListener* const listenerToRemove) + { + const int index = listeners.indexOf (listenerToRemove); + + if (index >= 0) + { + if (index < numDeepMouseListeners) + --numDeepMouseListeners; + + listeners.remove (index); + } + } + + static void sendMouseEvent (Component* comp, BailOutChecker& checker, + void (MouseListener::*eventMethod) (const MouseEvent&), const MouseEvent& e) + { + if (checker.shouldBailOut()) + return; + + { + MouseListenerList* const list = comp->mouseListeners_; + + if (list != 0) + { + for (int i = list->listeners.size(); --i >= 0;) + { + (list->listeners.getUnchecked(i)->*eventMethod) (e); + + if (checker.shouldBailOut()) + return; + + i = jmin (i, list->listeners.size()); + } + } + } + + Component* p = comp->parentComponent_; + + while (p != 0) + { + MouseListenerList* const list = p->mouseListeners_; + + if (list != 0 && list->numDeepMouseListeners > 0) + { + BailOutChecker checker2 (comp, p); + + for (int i = list->numDeepMouseListeners; --i >= 0;) + { + (list->listeners.getUnchecked(i)->*eventMethod) (e); + + if (checker2.shouldBailOut()) + return; + + i = jmin (i, list->numDeepMouseListeners); + } + } + + p = p->parentComponent_; + } + } + + static void sendWheelEvent (Component* comp, BailOutChecker& checker, const MouseEvent& e, + const float wheelIncrementX, const float wheelIncrementY) + { + if (checker.shouldBailOut()) + return; + + { + MouseListenerList* const list = comp->mouseListeners_; + + if (list != 0) + { + for (int i = list->listeners.size(); --i >= 0;) + { + list->listeners.getUnchecked(i)->mouseWheelMove (e, wheelIncrementX, wheelIncrementY); + + if (checker.shouldBailOut()) + return; + + i = jmin (i, list->listeners.size()); + } + } + } + + Component* p = comp->parentComponent_; + + while (p != 0) + { + MouseListenerList* const list = p->mouseListeners_; + + if (list != 0 && list->numDeepMouseListeners > 0) + { + BailOutChecker checker2 (comp, p); + + for (int i = list->numDeepMouseListeners; --i >= 0;) + { + list->listeners.getUnchecked(i)->mouseWheelMove (e, wheelIncrementX, wheelIncrementY); + + if (checker2.shouldBailOut()) + return; + + i = jmin (i, list->numDeepMouseListeners); + } + } + + p = p->parentComponent_; + } + } + +private: + Array listeners; + int numDeepMouseListeners; + + MouseListenerList (const MouseListenerList&); + MouseListenerList& operator= (const MouseListenerList&); +}; + //============================================================================== Component::Component() : parentComponent_ (0), - componentUID (++nextComponentUID), - numDeepMouseListeners (0), lookAndFeel_ (0), effect_ (0), bufferedImage_ (0), - mouseListeners_ (0), - keyListeners_ (0), componentFlags_ (0), componentTransparency (0) { @@ -74,13 +215,9 @@ Component::Component() Component::Component (const String& name) : componentName_ (name), parentComponent_ (0), - componentUID (++nextComponentUID), - numDeepMouseListeners (0), lookAndFeel_ (0), effect_ (0), bufferedImage_ (0), - mouseListeners_ (0), - keyListeners_ (0), componentFlags_ (0), componentTransparency (0) { @@ -107,9 +244,6 @@ Component::~Component() for (int i = childComponentList_.size(); --i >= 0;) childComponentList_.getUnchecked(i)->parentComponent_ = 0; - - delete mouseListeners_; - delete keyListeners_; } //============================================================================== @@ -1980,20 +2114,9 @@ void Component::addMouseListener (MouseListener* const newListener, jassert ((newListener != this) || wantsEventsForAllNestedChildComponents); if (mouseListeners_ == 0) - mouseListeners_ = new Array(); + mouseListeners_ = new MouseListenerList(); - if (! mouseListeners_->contains (newListener)) - { - if (wantsEventsForAllNestedChildComponents) - { - mouseListeners_->insert (0, newListener); - ++numDeepMouseListeners; - } - else - { - mouseListeners_->add (newListener); - } - } + mouseListeners_->addListener (newListener, wantsEventsForAllNestedChildComponents); } void Component::removeMouseListener (MouseListener* const listenerToRemove) @@ -2003,17 +2126,7 @@ void Component::removeMouseListener (MouseListener* const listenerToRemove) checkMessageManagerIsLocked if (mouseListeners_ != 0) - { - const int index = mouseListeners_->indexOf (listenerToRemove); - - if (index >= 0) - { - if (index < numDeepMouseListeners) - --numDeepMouseListeners; - - mouseListeners_->remove (index); - } - } + mouseListeners_->removeListener (listenerToRemove); } //============================================================================== @@ -2038,9 +2151,7 @@ void Component::internalMouseEnter (MouseInputSource& source, const Point& repaint(); const MouseEvent me (source, relativePos, source.getCurrentModifiers(), - this, this, time, relativePos, - time, 0, false); - + this, this, time, relativePos, time, 0, false); mouseEnter (me); if (checker.shouldBailOut()) @@ -2049,43 +2160,7 @@ void Component::internalMouseEnter (MouseInputSource& source, const Point& Desktop::getInstance().resetTimer(); Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseEnter, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - mouseListeners_->getUnchecked(i)->mouseEnter (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked(i)->mouseEnter (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseEnter, me); } } @@ -2111,8 +2186,7 @@ void Component::internalMouseExit (MouseInputSource& source, const Point& r repaint(); const MouseEvent me (source, relativePos, source.getCurrentModifiers(), - this, this, time, relativePos, - time, 0, false); + this, this, time, relativePos, time, 0, false); mouseExit (me); if (checker.shouldBailOut()) @@ -2121,43 +2195,7 @@ void Component::internalMouseExit (MouseInputSource& source, const Point& r Desktop::getInstance().resetTimer(); Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseExit, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseExit (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseExit (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseExit, me); } } @@ -2288,43 +2326,7 @@ void Component::internalMouseDown (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDown, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDown (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDown (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDown, me); } //============================================================================== @@ -2356,95 +2358,21 @@ void Component::internalMouseUp (MouseInputSource& source, const Point& rel desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseUp, me); + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseUp, me); + if (checker.shouldBailOut()) return; - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseUp (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - { - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseUp (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } - } - // check for double-click if (me.getNumberOfClicks() >= 2) { - const int numListeners = (mouseListeners_ != 0) ? mouseListeners_->size() : 0; - mouseDoubleClick (me); if (checker.shouldBailOut()) return; desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDoubleClick, me); - - if (checker.shouldBailOut()) - return; - - for (int i = numListeners; --i >= 0;) - { - if (checker.shouldBailOut()) - return; - - MouseListener* const ml = (MouseListener*)((*mouseListeners_)[i]); - if (ml != 0) - ml->mouseDoubleClick (me); - } - - if (checker.shouldBailOut()) - return; - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDoubleClick (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDoubleClick, me); } } } @@ -2474,43 +2402,7 @@ void Component::internalMouseDrag (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDrag (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseDrag (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseDrag, me); } } @@ -2540,43 +2432,7 @@ void Component::internalMouseMove (MouseInputSource& source, const Point& r desktop.resetTimer(); desktop.mouseListeners.callChecked (checker, &MouseListener::mouseMove, me); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseMove (me); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseMove (me); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendMouseEvent (this, checker, &MouseListener::mouseMove, me); } } @@ -2606,43 +2462,7 @@ void Component::internalMouseWheel (MouseInputSource& source, const Point& desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheelIncrementX, wheelIncrementY); - if (checker.shouldBailOut()) - return; - - if (mouseListeners_ != 0) - { - for (int i = mouseListeners_->size(); --i >= 0;) - { - ((MouseListener*) mouseListeners_->getUnchecked (i))->mouseWheelMove (me, wheelIncrementX, wheelIncrementY); - - if (checker.shouldBailOut()) - return; - - i = jmin (i, mouseListeners_->size()); - } - } - - Component* p = parentComponent_; - - while (p != 0) - { - if (p->numDeepMouseListeners > 0) - { - BailOutChecker checker2 (this, p); - - for (int i = p->numDeepMouseListeners; --i >= 0;) - { - p->mouseListeners_->getUnchecked (i)->mouseWheelMove (me, wheelIncrementX, wheelIncrementY); - - if (checker2.shouldBailOut()) - return; - - i = jmin (i, p->numDeepMouseListeners); - } - } - - p = p->parentComponent_; - } + MouseListenerList::sendWheelEvent (this, checker, me, wheelIncrementX, wheelIncrementY); } } diff --git a/src/gui/components/juce_Component.h b/src/gui/components/juce_Component.h index 618e14c7de..501b1a8c81 100644 --- a/src/gui/components/juce_Component.h +++ b/src/gui/components/juce_Component.h @@ -1898,14 +1898,6 @@ public: */ void* getWindowHandle() const; - /** When created, each component is given a number to uniquely identify it. - - The number is incremented each time a new component is created, so it's a more - unique way of identifying a component than using its memory location (which - may be reused after the component is deleted, of course). - */ - uint32 getComponentUID() const throw() { return componentUID; } - //============================================================================== /** Holds a pointer to some type of Component, which automatically becomes null if the component is deleted. @@ -2019,16 +2011,18 @@ private: //============================================================================== String componentName_; Component* parentComponent_; - uint32 componentUID; Rectangle bounds_; - int numDeepMouseListeners; Array childComponentList_; LookAndFeel* lookAndFeel_; MouseCursor cursor_; ImageEffectFilter* effect_; Image bufferedImage_; - Array * mouseListeners_; - Array * keyListeners_; + + class MouseListenerList; + friend class MouseListenerList; + friend class ScopedPointer ; + ScopedPointer mouseListeners_; + ScopedPointer > keyListeners_; ListenerList componentListeners; NamedValueSet properties; diff --git a/src/gui/components/mouse/juce_MouseCursor.cpp b/src/gui/components/mouse/juce_MouseCursor.cpp index 7fa009b550..c1496dfa25 100644 --- a/src/gui/components/mouse/juce_MouseCursor.cpp +++ b/src/gui/components/mouse/juce_MouseCursor.cpp @@ -125,15 +125,13 @@ private: //============================================================================== MouseCursor::MouseCursor() - : cursorHandle (SharedCursorHandle::createStandard (NormalCursor)) + : cursorHandle (0) { - jassert (cursorHandle != 0); } MouseCursor::MouseCursor (const StandardCursorType type) - : cursorHandle (SharedCursorHandle::createStandard (type)) + : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : 0) { - jassert (cursorHandle != 0); } MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY) @@ -142,19 +140,24 @@ MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotS } MouseCursor::MouseCursor (const MouseCursor& other) - : cursorHandle (other.cursorHandle->retain()) + : cursorHandle (other.cursorHandle == 0 ? 0 : other.cursorHandle->retain()) { } MouseCursor::~MouseCursor() { - cursorHandle->release(); + if (cursorHandle != 0) + cursorHandle->release(); } MouseCursor& MouseCursor::operator= (const MouseCursor& other) { - other.cursorHandle->retain(); - cursorHandle->release(); + if (other.cursorHandle != 0) + other.cursorHandle->retain(); + + if (cursorHandle != 0) + cursorHandle->release(); + cursorHandle = other.cursorHandle; return *this; } @@ -171,7 +174,7 @@ bool MouseCursor::operator!= (const MouseCursor& other) const throw() void* MouseCursor::getHandle() const throw() { - return cursorHandle->getHandle(); + return cursorHandle != 0 ? cursorHandle->getHandle() : 0; } void MouseCursor::showWaitCursor() diff --git a/src/native/linux/juce_linux_Audio.cpp b/src/native/linux/juce_linux_Audio.cpp index 871925bff2..739cc39bed 100644 --- a/src/native/linux/juce_linux_Audio.cpp +++ b/src/native/linux/juce_linux_Audio.cpp @@ -123,7 +123,8 @@ public: bitDepth (16), numChannelsRunning (0), latency (0), - isInput (forInput) + isInput (forInput), + isInterleaved (true) { failed (snd_pcm_open (&handle, deviceID.toUTF8(), forInput ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, @@ -285,6 +286,7 @@ public: if (isInterleaved) { scratch.ensureSize (sizeof (float) * numSamples * numChannelsRunning, false); + scratch.fillWith (0); // (not clearing this data causes warnings in valgrind) snd_pcm_sframes_t num = snd_pcm_readi (handle, scratch.getData(), numSamples); diff --git a/src/native/mac/juce_mac_MouseCursor.mm b/src/native/mac/juce_mac_MouseCursor.mm index 5541e87193..8a0355582a 100644 --- a/src/native/mac/juce_mac_MouseCursor.mm +++ b/src/native/mac/juce_mac_MouseCursor.mm @@ -118,7 +118,12 @@ void MouseCursor::showInAllWindows() const void MouseCursor::showInWindow (ComponentPeer*) const { - [((NSCursor*) getHandle()) set]; + NSCursor* c = (NSCursor*) getHandle(); + + if (c == 0) + c = [NSCursor arrowCursor]; + + [c set]; } #else diff --git a/src/native/windows/juce_win32_Windowing.cpp b/src/native/windows/juce_win32_Windowing.cpp index a0a4e51e66..52608c7532 100644 --- a/src/native/windows/juce_win32_Windowing.cpp +++ b/src/native/windows/juce_win32_Windowing.cpp @@ -2681,7 +2681,12 @@ void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorT //============================================================================== void MouseCursor::showInWindow (ComponentPeer*) const { - SetCursor ((HCURSOR) getHandle()); + HCURSOR c = (HCURSOR) getHandle(); + + if (c == 0) + c = LoadCursor (0, IDC_ARROW); + + SetCursor (c); } void MouseCursor::showInAllWindows() const