juce_Component.h

Go to the documentation of this file.
00001 /*
00002   ==============================================================================
00003 
00004    This file is part of the JUCE library - "Jules' Utility Class Extensions"
00005    Copyright 2004-7 by Raw Material Software ltd.
00006 
00007   ------------------------------------------------------------------------------
00008 
00009    JUCE can be redistributed and/or modified under the terms of the
00010    GNU General Public License, as published by the Free Software Foundation;
00011    either version 2 of the License, or (at your option) any later version.
00012 
00013    JUCE is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with JUCE; if not, visit www.gnu.org/licenses or write to the
00020    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00021    Boston, MA 02111-1307 USA
00022 
00023   ------------------------------------------------------------------------------
00024 
00025    If you'd like to release a closed-source product which uses JUCE, commercial
00026    licenses are also available: visit www.rawmaterialsoftware.com/juce for
00027    more information.
00028 
00029   ==============================================================================
00030 */
00031 
00032 #ifndef __JUCE_COMPONENT_JUCEHEADER__
00033 #define __JUCE_COMPONENT_JUCEHEADER__
00034 
00035 #include "mouse/juce_MouseCursor.h"
00036 #include "mouse/juce_MouseListener.h"
00037 #include "juce_ComponentListener.h"
00038 #include "keyboard/juce_KeyListener.h"
00039 #include "keyboard/juce_KeyboardFocusTraverser.h"
00040 #include "../graphics/effects/juce_ImageEffectFilter.h"
00041 #include "../graphics/geometry/juce_RectangleList.h"
00042 #include "../graphics/geometry/juce_BorderSize.h"
00043 #include "windows/juce_ComponentPeer.h"
00044 #include "../../events/juce_MessageListener.h"
00045 #include "../../../juce_core/text/juce_StringArray.h"
00046 #include "../../../juce_core/containers/juce_VoidArray.h"
00047 #include "../../../juce_core/containers/juce_PropertySet.h"
00048 class LookAndFeel;
00049 
00050 
00051 //==============================================================================
00056 class JUCE_API  Component  : public MouseListener,
00057                              protected MessageListener
00058 {
00059 public:
00060     //==============================================================================
00073     Component() throw();
00074 
00080     virtual ~Component();
00081 
00082     //==============================================================================
00087     Component (const String& componentName) throw();
00088 
00093     const String& getName() const throw()                   { return componentName_; }
00094 
00102     virtual void setName (const String& newName);
00103 
00104     //==============================================================================
00115     bool isValidComponent() const throw();
00116 
00117     //==============================================================================
00131     virtual void setVisible (bool shouldBeVisible);
00132 
00141     bool isVisible() const throw()                          { return flags.visibleFlag; }
00142 
00147     virtual void visibilityChanged();
00148 
00154     bool isShowing() const throw();
00155 
00176     void fadeOutComponent (const int lengthOfFadeOutInMilliseconds,
00177                            const int deltaXToMove = 0,
00178                            const int deltaYToMove = 0,
00179                            const float scaleFactorAtEnd = 1.0f);
00180 
00181     //==============================================================================
00205     virtual void addToDesktop (int windowStyleFlags,
00206                                void* nativeWindowToAttachTo = 0);
00207 
00215     void removeFromDesktop();
00216 
00221     bool isOnDesktop() const throw();
00222 
00233     ComponentPeer* getPeer() const throw();
00234 
00242     virtual void userTriedToCloseWindow();
00243 
00250     virtual void minimisationStateChanged (bool isNowMinimised);
00251 
00252     //==============================================================================
00263     void toFront (const bool shouldAlsoGainFocus);
00264 
00272     void toBack();
00273 
00278     void toBehind (Component* const other);
00279 
00284     void setAlwaysOnTop (const bool shouldStayOnTop);
00285 
00290     bool isAlwaysOnTop() const throw();
00291 
00292     //==============================================================================
00299     inline int getX() const throw()                         { return bounds_.getX(); }
00300 
00307     inline int getY() const throw()                         { return bounds_.getY(); }
00308 
00310     inline int getWidth() const throw()                     { return bounds_.getWidth(); }
00311 
00313     inline int getHeight() const throw()                    { return bounds_.getHeight(); }
00314 
00319     int getRight() const throw()                            { return bounds_.getRight(); }
00320 
00325     int getBottom() const throw()                           { return bounds_.getBottom(); }
00326 
00331     const Rectangle& getBounds() const throw()              { return bounds_; }
00332 
00341     void getVisibleArea (RectangleList& result,
00342                          const bool includeSiblings) const;
00343 
00344     //==============================================================================
00349     int getScreenX() const throw();
00350 
00355     int getScreenY() const throw();
00356 
00361     void relativePositionToGlobal (int& x, int& y) const throw();
00362 
00367     void globalPositionToRelative (int& x, int& y) const throw();
00368 
00374     void relativePositionToOtherComponent (const Component* const targetComponent,
00375                                            int& x, int& y) const throw();
00376 
00377     //==============================================================================
00387     void setTopLeftPosition (const int x, const int y);
00388 
00396     void setTopRightPosition (const int x, const int y);
00397 
00402     void setSize (const int newWidth, const int newHeight);
00403 
00414     void setBounds (int x, int y, int width, int height);
00415 
00420     void setBounds (const Rectangle& newBounds);
00421 
00429     void setBoundsRelative (const float proportionalX, const float proportionalY,
00430                             const float proportionalWidth, const float proportionalHeight);
00431 
00437     void setBoundsInset (const BorderSize& borders);
00438 
00451     void setBoundsToFit (int x, int y, int width, int height,
00452                          const Justification& justification,
00453                          const bool onlyReduceInSize);
00454 
00460     void setCentrePosition (const int x, const int y);
00461 
00468     void setCentreRelative (const float x, const float y);
00469 
00475     void centreWithSize (const int width, const int height);
00476 
00477     //==============================================================================
00482     int proportionOfWidth (const float proportion) const throw();
00483 
00488     int proportionOfHeight (const float proportion) const throw();
00489 
00495     int getParentWidth() const throw();
00496 
00502     int getParentHeight() const throw();
00503 
00510     const Rectangle getParentMonitorArea() const throw();
00511 
00512     //==============================================================================
00517     int getNumChildComponents() const throw();
00518 
00528     Component* getChildComponent (const int index) const throw();
00529 
00539     int getIndexOfChildComponent (const Component* const child) const throw();
00540 
00551     void addChildComponent (Component* const child,
00552                             int zOrder = -1);
00553 
00559     void addAndMakeVisible (Component* const child,
00560                             int zOrder = -1);
00561 
00571     void removeChildComponent (Component* const childToRemove);
00572 
00582     Component* removeChildComponent (const int childIndexToRemove);
00583 
00588     void removeAllChildren();
00589 
00594     void deleteAllChildren();
00595 
00601     Component* getParentComponent() const throw()                   { return parentComponent_; }
00602 
00611     template <class TargetClass>
00612     TargetClass* findParentComponentOfClass (TargetClass* const dummyParameter = 0) const
00613     {
00614         (void) dummyParameter;
00615         Component* p = parentComponent_;
00616         while (p != 0)
00617         {
00618             TargetClass* target = dynamic_cast <TargetClass*> (p);
00619             if (target != 0)
00620                 return target;
00621 
00622             p = p->parentComponent_;
00623         }
00624 
00625         return 0;
00626     }
00627 
00634     Component* getTopLevelComponent() const throw();
00635 
00641     bool isParentOf (const Component* possibleChild) const throw();
00642 
00643     //==============================================================================
00654     virtual void parentHierarchyChanged();
00655 
00660     virtual void childrenChanged();
00661 
00662     //==============================================================================
00699     virtual bool hitTest (int x, int y);
00700 
00718     void setInterceptsMouseClicks (const bool allowClicksOnThisComponent,
00719                                    const bool allowClicksOnChildComponents) throw();
00720 
00728     void getInterceptsMouseClicks (bool& allowsClicksOnThisComponent,
00729                                    bool& allowsClicksOnChildComponents) const throw();
00730 
00731 
00744     virtual bool contains (int x, int y);
00745 
00757     bool reallyContains (int x, int y,
00758                          const bool returnTrueIfWithinAChild);
00759 
00770     Component* getComponentAt (const int x, const int y);
00771 
00772     //==============================================================================
00789     void repaint() throw();
00790 
00804     void repaint (const int x, const int y,
00805                   const int width, const int height) throw();
00806 
00807     //==============================================================================
00825     void setBufferedToImage (const bool shouldBeBuffered) throw();
00826 
00844     Image* createComponentSnapshot (const Rectangle& areaToGrab,
00845                                     const bool clipImageToComponentBounds = true);
00846 
00860     void paintEntireComponent (Graphics& context);
00861 
00862 
00863     //==============================================================================
00877     void setComponentEffect (ImageEffectFilter* const newEffect);
00878 
00883     ImageEffectFilter* getComponentEffect() const throw()               { return effect_; }
00884 
00885     //==============================================================================
00894     LookAndFeel& getLookAndFeel() const throw();
00895 
00909     void setLookAndFeel (LookAndFeel* const newLookAndFeel);
00910 
00921     virtual void lookAndFeelChanged();
00922 
00930     void sendLookAndFeelChange();
00931 
00932     //==============================================================================
00947     void setOpaque (const bool shouldBeOpaque) throw();
00948 
00954     bool isOpaque() const throw();
00955 
00956     //==============================================================================
00969     void setBroughtToFrontOnMouseClick (const bool shouldBeBroughtToFront) throw();
00970 
00975     bool isBroughtToFrontOnMouseClick() const throw();
00976 
00977     //==============================================================================
00978     // Keyboard focus methods
00979 
00990     void setWantsKeyboardFocus (const bool wantsFocus) throw();
00991 
00999     bool getWantsKeyboardFocus() const throw();
01000 
01001     //==============================================================================
01008     void setMouseClickGrabsKeyboardFocus (const bool shouldGrabFocus);
01009 
01014     bool getMouseClickGrabsKeyboardFocus() const throw();
01015 
01016     //==============================================================================
01036     void grabKeyboardFocus();
01037 
01048     bool hasKeyboardFocus (const bool trueIfChildIsFocused) const throw();
01049 
01054     static Component* JUCE_CALLTYPE getCurrentlyFocusedComponent() throw();
01055 
01056     //==============================================================================
01068     void moveKeyboardFocusToSibling (const bool moveToNext);
01069 
01085     virtual KeyboardFocusTraverser* createFocusTraverser();
01086 
01101     int getExplicitFocusOrder() const throw();
01102 
01111     void setExplicitFocusOrder (const int newFocusOrderIndex) throw();
01112 
01125     void setFocusContainer (const bool isFocusContainer) throw();
01126 
01133     bool isFocusContainer() const throw();
01134 
01135     //==============================================================================
01147     bool isEnabled() const throw();
01148 
01159     void setEnabled (const bool shouldBeEnabled);
01160 
01171     virtual void enablementChanged();
01172 
01173     //==============================================================================
01181     void setMouseCursor (const MouseCursor& cursorType) throw();
01182 
01191     virtual const MouseCursor getMouseCursor();
01192 
01202     void updateMouseCursor() const throw();
01203 
01204     //==============================================================================
01228     virtual void paint (Graphics& g);
01229 
01238     virtual void paintOverChildren (Graphics& g);
01239 
01240 
01241     //==============================================================================
01252     virtual void mouseMove         (const MouseEvent& e);
01253 
01271     virtual void mouseEnter        (const MouseEvent& e);
01272 
01289     virtual void mouseExit         (const MouseEvent& e);
01290 
01303     virtual void mouseDown         (const MouseEvent& e);
01304 
01318     virtual void mouseDrag         (const MouseEvent& e);
01319 
01332     virtual void mouseUp           (const MouseEvent& e);
01333 
01347     virtual void mouseDoubleClick  (const MouseEvent& e);
01348 
01366     virtual void mouseWheelMove    (const MouseEvent& e,
01367                                     float wheelIncrementX,
01368                                     float wheelIncrementY);
01369 
01370     //==============================================================================
01389     static void beginDragAutoRepeat (const int millisecondIntervalBetweenCallbacks);
01390 
01402     void setRepaintsOnMouseActivity (const bool shouldRepaint) throw();
01403 
01421     void addMouseListener (MouseListener* const newListener,
01422                            const bool wantsEventsForAllNestedChildComponents) throw();
01423 
01428     void removeMouseListener (MouseListener* const listenerToRemove) throw();
01429 
01430     //==============================================================================
01441     void addKeyListener (KeyListener* const newListener) throw();
01442 
01447     void removeKeyListener (KeyListener* const listenerToRemove) throw();
01448 
01465     virtual bool keyPressed (const KeyPress& key);
01466 
01487     virtual bool keyStateChanged();
01488 
01502     virtual void modifierKeysChanged (const ModifierKeys& modifiers);
01503 
01504     //==============================================================================
01506     enum FocusChangeType
01507     {
01508         focusChangedByMouseClick,   
01509         focusChangedByTabKey,       
01510         focusChangedDirectly        
01511     };
01512 
01517     virtual void focusGained (FocusChangeType cause);
01518 
01523     virtual void focusLost (FocusChangeType cause);
01524 
01533     virtual void focusOfChildComponentChanged (FocusChangeType cause);
01534 
01535     //==============================================================================
01548     bool isMouseOver() const throw();
01549 
01558     bool isMouseButtonDown() const throw();
01559 
01566     bool isMouseOverOrDragging() const throw();
01567 
01576     static bool JUCE_CALLTYPE isMouseButtonDownAnywhere() throw();
01577 
01582     void getMouseXYRelative (int& x, int& y) const throw();
01583 
01589     static Component* JUCE_CALLTYPE getComponentUnderMouse() throw();
01590 
01609     void enableUnboundedMouseMovement (bool shouldUnboundedMovementBeEnabled,
01610                                        bool keepCursorVisibleUntilOffscreen = false) throw();
01611 
01612     //==============================================================================
01628     virtual void resized();
01629 
01645     virtual void moved();
01646 
01654     virtual void childBoundsChanged (Component* child);
01655 
01663     virtual void parentSizeChanged();
01664 
01672     virtual void broughtToFront();
01673 
01683     void addComponentListener (ComponentListener* const newListener) throw();
01684 
01689     void removeComponentListener (ComponentListener* const listenerToRemove) throw();
01690 
01691     //==============================================================================
01703     void postCommandMessage (const int commandId) throw();
01704 
01712     virtual void handleCommandMessage (int commandId);
01713 
01714     //==============================================================================
01731     int runModalLoop();
01732 
01745     void enterModalState (const bool takeKeyboardFocus = true);
01746 
01754     void exitModalState (const int returnValue);
01755 
01764     bool isCurrentlyModal() const throw();
01765 
01771     static Component* JUCE_CALLTYPE getCurrentlyModalComponent() throw();
01772 
01781     bool isCurrentlyBlockedByAnotherModalComponent() const throw();
01782 
01794     virtual bool canModalEventBeSentToComponent (const Component* targetComponent);
01795 
01807     virtual void inputAttemptWhenModal();
01808 
01809 
01810     //==============================================================================
01819     const String getComponentProperty (const String& keyName,
01820                                        const bool useParentComponentIfNotFound,
01821                                        const String& defaultReturnValue = String::empty) const throw();
01822 
01831     int getComponentPropertyInt (const String& keyName,
01832                                  const bool useParentComponentIfNotFound,
01833                                  const int defaultReturnValue = 0) const throw();
01834 
01843     double getComponentPropertyDouble (const String& keyName,
01844                                        const bool useParentComponentIfNotFound,
01845                                        const double defaultReturnValue = 0.0) const throw();
01846 
01858     bool getComponentPropertyBool (const String& keyName,
01859                                    const bool useParentComponentIfNotFound,
01860                                    const bool defaultReturnValue = false) const throw();
01861 
01870     const Colour getComponentPropertyColour (const String& keyName,
01871                                              const bool useParentComponentIfNotFound,
01872                                              const Colour& defaultReturnValue = Colours::black) const throw();
01873 
01880     void setComponentProperty (const String& keyName, const String& value) throw();
01881 
01888     void setComponentProperty (const String& keyName, const int value) throw();
01889 
01896     void setComponentProperty (const String& keyName, const double value) throw();
01897 
01904     void setComponentProperty (const String& keyName, const bool value) throw();
01905 
01912     void setComponentProperty (const String& keyName, const Colour& newColour) throw();
01913 
01919     void removeComponentProperty (const String& keyName) throw();
01920 
01927     PropertySet* getComponentProperties() const throw()           { return propertySet_; }
01928 
01929     //==============================================================================
01943     const Colour findColour (const int colourId, const bool inheritFromParent = false) const throw();
01944 
01955     void setColour (const int colourId, const Colour& colour);
01956 
01961     void removeColour (const int colourId);
01962 
01966     bool isColourSpecified (const int colourId) const throw();
01967 
01971     void copyAllExplicitColoursTo (Component& target) const throw();
01972 
01977     virtual void colourChanged();
01978 
01979     //==============================================================================
01984     void* getWindowHandle() const throw();
01985 
01992     uint32 getComponentUID() const throw()                { return componentUID; }
01993 
01994     //==============================================================================
01995     juce_UseDebuggingNewOperator
01996 
01997 private:
01998     //==============================================================================
01999     friend class ComponentPeer;
02000     friend class InternalDragRepeater;
02001 
02002     static Component* currentlyFocusedComponent;
02003     static Component* componentUnderMouse;
02004 
02005     //==============================================================================
02006     String componentName_;
02007     Component* parentComponent_;
02008     uint32 componentUID;
02009     Rectangle bounds_;
02010     unsigned short numDeepMouseListeners;
02011     Array <Component*> childComponentList_;
02012     LookAndFeel* lookAndFeel_;
02013     MouseCursor cursor_;
02014     ImageEffectFilter* effect_;
02015     Image* bufferedImage_;
02016     VoidArray* mouseListeners_;
02017     VoidArray* keyListeners_;
02018     VoidArray* componentListeners_;
02019     PropertySet* propertySet_;
02020 
02021     struct ComponentFlags
02022     {
02023         bool hasHeavyweightPeerFlag     : 1;
02024         bool visibleFlag                : 1;
02025         bool opaqueFlag                 : 1;
02026         bool ignoresMouseClicksFlag     : 1;
02027         bool allowChildMouseClicksFlag  : 1;
02028         bool wantsFocusFlag             : 1;
02029         bool isFocusContainerFlag       : 1;
02030         bool dontFocusOnMouseClickFlag  : 1;
02031         bool alwaysOnTopFlag            : 1;
02032         bool bufferToImageFlag          : 1;
02033         bool bringToFrontOnClickFlag    : 1;
02034         bool repaintOnMouseActivityFlag : 1;
02035         bool draggingFlag               : 1;
02036         bool mouseOverFlag              : 1;
02037         bool mouseInsideFlag            : 1;
02038         bool currentlyModalFlag         : 1;
02039         bool isDisabledFlag             : 1;
02040         bool childCompFocusedFlag       : 1;
02041 #ifdef JUCE_DEBUG
02042         bool isInsidePaintCall          : 1;
02043 #endif
02044     };
02045 
02046     union
02047     {
02048         uint32 componentFlags_;
02049         ComponentFlags flags;
02050     };
02051 
02052     //==============================================================================
02053     void internalMouseEnter (int x, int y, const int64 time);
02054     void internalMouseExit  (int x, int y, const int64 time);
02055     void internalMouseDown  (int x, int y);
02056     void internalMouseUp    (const int oldModifiers, int x, int y, const int64 time);
02057     void internalMouseDrag  (int x, int y, const int64 time);
02058     void internalMouseMove  (int x, int y, const int64 time);
02059     void internalMouseWheel (const int intAmountX, const int intAmountY, const int64 time);
02060     void internalBroughtToFront();
02061     void internalFocusGain (const FocusChangeType cause);
02062     void internalFocusLoss (const FocusChangeType cause);
02063     void internalChildFocusChange (FocusChangeType cause);
02064     void internalModalInputAttempt();
02065     void internalModifierKeysChanged();
02066     void internalChildrenChanged();
02067     void internalHierarchyChanged();
02068     void internalUpdateMouseCursor (const bool forcedUpdate) throw();
02069     void sendMovedResizedMessages (const bool wasMoved, const bool wasResized);
02070     void repaintParent() throw();
02071     void sendFakeMouseMove() const;
02072     void takeKeyboardFocus (const FocusChangeType cause);
02073     void grabFocusInternal (const FocusChangeType cause, const bool canTryParent = true);
02074     static void giveAwayFocus();
02075     void sendEnablementChangeMessage();
02076     static void* runModalLoopCallback (void*);
02077     void subtractObscuredRegions (RectangleList& result,
02078                                   const int deltaX, const int deltaY,
02079                                   const Rectangle& clipRect,
02080                                   const Component* const compToAvoid) const throw();
02081     void clipObscuredRegions (Graphics& g, const Rectangle& clipRect,
02082                               const int deltaX, const int deltaY) const throw();
02083 
02084     // how much of the component is not off the edges of its parents
02085     const Rectangle getUnclippedArea() const;
02086     void sendVisibilityChangeMessage();
02087 
02088     //==============================================================================
02089     // This is included here just to cause a compile error if your code is still handling
02090     // drag-and-drop with this method. If so, just update it to use the new FileDragAndDropTarget
02091     // class, which is easy (just make your class inherit from FileDragAndDropTarget, and
02092     // implement its methods instead of this Component method).
02093     virtual void filesDropped (const StringArray&, int, int) {}
02094 
02095     // components aren't allowed to have copy constructors, as this would mess up parent
02096     // hierarchies. You might need to give your subclasses a private dummy constructor like
02097     // this one to avoid compiler warnings.
02098     Component (const Component&);
02099 
02100     const Component& operator= (const Component&);
02101 
02102 protected:
02104     virtual void internalRepaint (int x, int y, int w, int h);
02105 
02106     virtual ComponentPeer* createNewPeer (int styleFlags, void* nativeWindowToAttachTo);
02107 
02114     void handleMessage (const Message&);
02115 };
02116 
02117 
02118 #endif   // __JUCE_COMPONENT_JUCEHEADER__