//**************************************************************************************** // File: GraphPort.h // // Copyright: 1999-2007, Ableton AG, Berlin. All rights reserved. //**************************************************************************************** #pragma once #if !defined(GraphPort_h) #define GraphPort_h //======================================================================================== //+I{ #include "Base/Exp/BaseTypes.h" #include "CreamOs/Exp/Forgetable.h" #include "CreamOs/Exp/NewWithoutES.h" #include "CreamOs/Exp/Ptr.h" #include "CreamOs/Exp/Type.h" #include "WindView/Exp/Color.h" #include "WindView/Exp/SurfaceInfo.h" #include "WindView/Exp/WindType.h" //+I} //======================================================================================== //+F{ class ABitmap; class AView; //+F} //======================================================================================== //+M{ #undef MDll #ifdef MWindView #pragma MDllExportBegin #define MDll MDllExport #else #define MDll MDllImport #pragma MLibrary(WindView) #endif //+M} //======================================================================================== //! Represents an abstract drawing surface. class MDll AGraphPort : public TForgetable { public: enum { KSubPixelBits = 8, // Number of bits used for sub-pixel precision }; //! Stores the state of a graph port which can later be restored with RestoreState. class TState : public AObject { private: TPoint mOrigin; TRect mConstrainedRect; TRect mClipRect; friend class AGraphPort; }; //! Creates a new graph port for drawing into the given surface. static AGraphPort* SNew(TSurfaceInfo* pSurfaceInfo); //! The size of this graph port. TPoint Size() const; //! Adds a new clipping rectangle. The new clipping rect will be //! the intersection of the given rect and the old clipping rect. //! \param Rect The rectangle (relative to the origin) that should be //! combined with the current clip rectangle. void AddClipRect(TRect Rect); //! Sets the area to be clipped to the given area. This disregards //! the current clip rect. //! \param Rect The rectangle (relative to the origin) that defines the new //! clip area. void SetClipRect(TRect Rect); //! Clears all clipping. void ClearClipRect(); //! Returns current clipping rectangle relative to the origin. TRect ClipRect() const; //! Returns current origin (e.g drawing offset for view) TPoint Origin() const; //! Checks if this GraphPort is empty. That is all drawing is clipped so //! that nothing will ever be drawn. TBool IsEmpty() const; //! Constrains this GraphPort so that all drawing operations are guaranteed //! to be within \a Rect. The new origin for drawing will be the top left //! hand corner of \a Rect. //! \param Rect The rectangle to constrain drawing to - relative to the origin. void Constrain(TRect Rect); //! Saves the current state of this GraphPort in \a State so it can be restored //! by a call to RestoreState. void SaveState(AGraphPort::TState& State) const; //! Restores the state of this GraphPort to a state that was previously saved //! by a call to SaveState. void RestoreState(const AGraphPort::TState& State); //=============================================== Drawing Functions // All drawing functions will convert the source color to the given // destination color format. // The Alpha value of the color will be used so that a value of 0 // means invisible and a value of 255 means solid //! Draw a pixel virtual void DrawPixel(TPoint P, TColor Color) = NULL; //! Sets the pixel at the given location to the given color and alpha value. virtual void SetPixel(TPoint P, TColor Color) = NULL; //! Draws a line with a width of 1 pixel. virtual void DrawLine(TPoint P0, TPoint P1, TColor Color) = NULL; //! Draws a line with a given thickness. virtual void DrawLine(TPoint P0, TPoint P1, TColor Color, TInt Thickness) = NULL; //! Draws a dashed line with a width of 1 pixel. virtual void DrawLine( TPoint P0, TPoint P1, TColor Color1, TColor Color2, const TFillPattern& Pattern) = NULL; //! Draws a dashed line with a given thickness. virtual void DrawLine( TPoint P0, TPoint P1, TColor Color1, TColor Color2, const TFillPattern& Pattern, TInt Thickness) = NULL; virtual void DrawAntiAliasedLine( TFloat X, TFloat Y, TFloat X2, TFloat Y2, TColor Color, TFloat Thickness) = NULL; //! Draw a colored rectangle with a thickness of 1 pixel. virtual void DrawRect(TRect Rect, TColor Color) = NULL; //! Draw a colored rectangle with a given thickness. virtual void DrawRect(TRect Rect, TColor Color, TInt Thickness) = NULL; //! Draw a dashed rectangle with a thickness of 1 pixel. //! The lines are dashed like described by //! DrawLine(TPoint, TPoint, TColor, TColor TFillPattern&). virtual void DrawRect( TRect Rect, TColor Color1, TColor Color2, const TFillPattern& Pattern) = NULL; //! Draw a dashed rectangle with a given thickness. //! The lines are dashed like described by //! DrawLine(TPoint, TPoint, TColor, TColor TFillPattern&). virtual void DrawRect( TRect Rect, TColor Color1, TColor Color2, const TFillPattern& Pattern, TInt Thickness) = NULL; //! Draw a filled rectangle virtual void DrawFilledRect(TRect Rect, TColor Color) = NULL; //! For each pixel in the given rectangle, paint the pixel with Color1 if the //! current pixel had a lightness value (in the HSL color model) greater than //! Threshold, or with Color2 otherwise. Threshold is between 0.0 and 1.0. //! //! Useful for painting lines that have maximal contrast against their background. virtual void DrawFilledRectThreshold( TRect Rect, TColor Color1, TColor Color2, TDouble Threshold) = NULL; //! Draw the 4 corners of a rectangle. virtual void DrawCorners(TRect Rect, TInt Length, TColor Color) = NULL; //! Draw the 4 corners of a rectangle with a given thickness. virtual void DrawCorners(TRect Rect, TInt Length, TColor Color, TInt Thickness) = NULL; //! Draw lowert diagonal half part of a Rect // x Left Point // | \ // | \ // |_____x Right Point virtual void FillLowerDiagonalOfRect(TPoint Left, TPoint Right, TColor Color) = NULL; //! Draws a filled right angle triangle. //! \param Point The point where the right angle of the triangle should be. //! \param X The x position that the triangle should be drawn to. //! \param Y The y position that the triangle should be drawn to. virtual void DrawFilledTriangle(TPoint Point, TInt X, TInt Y, TColor Color) = NULL; //! Clear screen to color (Note: if Alpha is not solid the underground //! will remain visible) virtual void Clear(TColor Color) = NULL; //! Set the alpha value of the pixels in the rectangle to the given value. virtual void SetAlpha(TU8 Alpha, TRect Rect) = NULL; //! Draw a bitmap. //! The funcion can deal with all bitmaps. When you use 8Bit Bitmaps //! without having set a palette you'll get an assertion. //! The UseAlpha flag makes only sense when drawing 32Bit Bitmaps with //! Alpha information per pixel. If the flag is true the alpha information //! in the bitmap will be used if available. In all other cases the bitmap //! is drawn solid. virtual void DrawBitmap( ABitmap *pBitmap, TPoint DstPos, TBool UseAlpha = false) = NULL; virtual void DrawBitmap( ABitmap *pBitmap, TRect Src, TPoint DstPos, TBool UseAlpha = false) = NULL; //! Scales a bitmap. The destination rectangle can have sub pixel positions (if //! \a DstRectInSubPixelFormat is true). virtual void DrawBitmapScaled( TPtr pSrcBitmap, TRect SrcRect, TRect DstRect, TBool UseAlpha, TBool DstRectInSubPixelFormat = false) = NULL; //! Converts a rect to the fixed-point format used by the GraphPort for sub-pixel accuracy. static TRect SRectToSubPixelRect(const TRect& R); //! Converts a rect in the fixed-point format back to integer coordinates. static TRect SRectFromSubPixelRect(const TRect& R); //! Checks whether the given coordinate has sub pixel position - that is, if the lower KSubPixelBits //! are not 0. static TBool SHasSubPixelPosition(TInt Coord); //! Checks whether the given rectangle has sub pixel positions for any of its coordinates. static TBool SHasSubPixelPosition(const TRect& Rect); protected: //! Called whenever the clip rect changes to store the new clip rectangle. //! \param Rect the new clip rectangle (guaranteed to be inside the constrained rectangle) in absolute //! window coordinates. virtual void SetClipRectImpl(const TRect& Rect); AGraphPort(TPoint Size); //! The origin that drawing operations are relative to. TPoint mOrigin; //! The rectangle that all drawing operations are constrained to. The clip rectangle can never be //! set outside of this rectangle. //! Typically this will be a View's visible area in window coordinates. TRect mConstrainedRect; //! The clip rectangle that is inside the current constrained rectangle. TRect mClipRect; }; //---------------------------------------------------------------------------------------- MForceInline TBool AGraphPort::IsEmpty() const { return mClipRect.IsEmpty(); } //---------------------------------------------------------------------------------------- MForceInline TBool AGraphPort::SHasSubPixelPosition(TInt Coord) { return (Coord & ((1 << KSubPixelBits) - 1)) != 0; } //---------------------------------------------------------------------------------------- MForceInline TBool AGraphPort::SHasSubPixelPosition(const TRect& Rect) { return SHasSubPixelPosition(Rect.X0()) || SHasSubPixelPosition(Rect.Y0()) || SHasSubPixelPosition(Rect.X1()) || SHasSubPixelPosition(Rect.Y1()); } //---------------------------------------------------------------------------------------- //+M{ #pragma MDllExportEnd //+M} #endif //**************************************************************************************** // E O F //****************************************************************************************