Back to index

lightning-sunbird  0.9+nobinonly
nsRenderingContextGTK.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 1; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsRenderingContextGTK_h___
00039 #define nsRenderingContextGTK_h___
00040 
00041 #include "nsRenderingContextImpl.h"
00042 #include "nsUnitConversion.h"
00043 #include "nsFont.h"
00044 #include "nsPoint.h"
00045 #include "nsString.h"
00046 #include "nsCRT.h"
00047 #include "nsTransform2D.h"
00048 #include "nsIWidget.h"
00049 #include "nsRect.h"
00050 #include "nsIDeviceContext.h"
00051 #include "nsVoidArray.h"
00052 #include "nsGfxCIID.h"
00053 #include "nsDrawingSurfaceGTK.h"
00054 #include "nsRegionGTK.h"
00055 #include "nsIFontMetricsGTK.h"
00056 
00057 #include <gtk/gtk.h>
00058 
00059 class nsRenderingContextGTK : public nsRenderingContextImpl
00060 {
00061 public:
00062   nsRenderingContextGTK();
00063   virtual ~nsRenderingContextGTK();
00064   static nsresult Shutdown(); // release statics
00065 
00066   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00067 
00068   NS_DECL_ISUPPORTS
00069 
00070   NS_IMETHOD Init(nsIDeviceContext* aContext, nsIWidget *aWindow);
00071   NS_IMETHOD Init(nsIDeviceContext* aContext, nsIDrawingSurface* aSurface);
00072 
00073   NS_IMETHOD Reset(void);
00074 
00075   NS_IMETHOD GetDeviceContext(nsIDeviceContext *&aContext);
00076 
00077   NS_IMETHOD LockDrawingSurface(PRInt32 aX, PRInt32 aY, PRUint32 aWidth, PRUint32 aHeight,
00078                                 void **aBits, PRInt32 *aStride, PRInt32 *aWidthBytes,
00079                                 PRUint32 aFlags);
00080   NS_IMETHOD UnlockDrawingSurface(void);
00081 
00082   NS_IMETHOD SelectOffScreenDrawingSurface(nsIDrawingSurface* aSurface);
00083   NS_IMETHOD GetDrawingSurface(nsIDrawingSurface* *aSurface);
00084   NS_IMETHOD GetHints(PRUint32& aResult);
00085 
00086 #if 0
00087   NS_IMETHOD PushState(PRInt32 aFlags);
00088 #endif
00089   NS_IMETHOD PushState(void);
00090   NS_IMETHOD PopState(void);
00091 
00092   NS_IMETHOD IsVisibleRect(const nsRect& aRect, PRBool &aVisible);
00093 
00094   NS_IMETHOD SetClipRect(const nsRect& aRect, nsClipCombine aCombine);
00095   NS_IMETHOD GetClipRect(nsRect &aRect, PRBool &aClipValid);
00096   NS_IMETHOD SetClipRegion(const nsIRegion& aRegion, nsClipCombine aCombine);
00097   NS_IMETHOD CopyClipRegion(nsIRegion &aRegion);
00098   NS_IMETHOD GetClipRegion(nsIRegion **aRegion);
00099 
00100   NS_IMETHOD SetLineStyle(nsLineStyle aLineStyle);
00101   NS_IMETHOD GetLineStyle(nsLineStyle &aLineStyle);
00102 
00103   NS_IMETHOD SetColor(nscolor aColor);
00104   NS_IMETHOD GetColor(nscolor &aColor) const;
00105 
00106   NS_IMETHOD SetFont(const nsFont& aFont, nsIAtom* aLangGroup);
00107   NS_IMETHOD SetFont(nsIFontMetrics *aFontMetrics);
00108 
00109   NS_IMETHOD GetFontMetrics(nsIFontMetrics *&aFontMetrics);
00110 
00111   NS_IMETHOD Translate(nscoord aX, nscoord aY);
00112   NS_IMETHOD Scale(float aSx, float aSy);
00113   NS_IMETHOD GetCurrentTransform(nsTransform2D *&aTransform);
00114 
00115   NS_IMETHOD CreateDrawingSurface(const nsRect& aBounds, PRUint32 aSurfFlags, nsIDrawingSurface* &aSurface);
00116   NS_IMETHOD DestroyDrawingSurface(nsIDrawingSurface* aDS);
00117 
00118   NS_IMETHOD DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1);
00119   NS_IMETHOD DrawPolyline(const nsPoint aPoints[], PRInt32 aNumPoints);
00120 
00121   NS_IMETHOD DrawRect(const nsRect& aRect);
00122   NS_IMETHOD DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00123 
00124   NS_IMETHOD FillRect(const nsRect& aRect);
00125   NS_IMETHOD FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00126 
00127   NS_IMETHOD InvertRect(const nsRect& aRect);
00128   NS_IMETHOD InvertRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00129 
00130   NS_IMETHOD DrawPolygon(const nsPoint aPoints[], PRInt32 aNumPoints);
00131   NS_IMETHOD FillPolygon(const nsPoint aPoints[], PRInt32 aNumPoints);
00132 
00133   NS_IMETHOD DrawEllipse(const nsRect& aRect);
00134   NS_IMETHOD DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00135   NS_IMETHOD FillEllipse(const nsRect& aRect);
00136   NS_IMETHOD FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00137 
00138   NS_IMETHOD DrawArc(const nsRect& aRect,
00139                      float aStartAngle, float aEndAngle);
00140   NS_IMETHOD DrawArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
00141                      float aStartAngle, float aEndAngle);
00142   NS_IMETHOD FillArc(const nsRect& aRect,
00143                      float aStartAngle, float aEndAngle);
00144   NS_IMETHOD FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
00145                      float aStartAngle, float aEndAngle);
00146 
00147   NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth,
00148                       PRInt32 *aFontID = nsnull)
00149   { return nsRenderingContextImpl::GetWidth(aString, aWidth, aFontID); }
00150   NS_IMETHOD GetWidth(const char* aString, nscoord& aWidth)
00151   { return nsRenderingContextImpl::GetWidth(aString, aWidth); }
00152   NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength,
00153                       nscoord& aWidth)
00154   { return nsRenderingContextImpl::GetWidth(aString, aLength, aWidth); }
00155   NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength,
00156                       nscoord &aWidth, PRInt32 *aFontID = nsnull)
00157   { return nsRenderingContextImpl::GetWidth(aString, aLength, aWidth, aFontID); }
00158   NS_IMETHOD DrawString(const nsString& aString, nscoord aX, nscoord aY,
00159                         PRInt32 aFontID = -1,
00160                         const nscoord* aSpacing = nsnull)
00161   { return nsRenderingContextImpl::DrawString(aString, aX, aY, aFontID, aSpacing); }
00162 
00163   NS_IMETHOD GetWidth(char aC, nscoord &aWidth);
00164   NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth,
00165                       PRInt32 *aFontID);
00166 
00167   NS_IMETHOD GetWidthInternal(const char *aString, PRUint32 aLength, nscoord &aWidth);
00168   NS_IMETHOD GetWidthInternal(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth,
00169                               PRInt32 *aFontID);
00170 
00171   NS_IMETHOD DrawStringInternal(const char *aString, PRUint32 aLength,
00172                                 nscoord aX, nscoord aY,
00173                                 const nscoord* aSpacing);
00174   NS_IMETHOD DrawStringInternal(const PRUnichar *aString, PRUint32 aLength,
00175                                 nscoord aX, nscoord aY,
00176                                 PRInt32 aFontID,
00177                                 const nscoord* aSpacing);
00178 
00179   NS_IMETHOD GetTextDimensionsInternal(const char* aString, PRUint32 aLength,
00180                                        nsTextDimensions& aDimensions);
00181   NS_IMETHOD GetTextDimensionsInternal(const PRUnichar *aString, PRUint32 aLength,
00182                                        nsTextDimensions& aDimensions,PRInt32 *aFontID);
00183   NS_IMETHOD GetTextDimensionsInternal(const char*       aString,
00184                                        PRInt32           aLength,
00185                                        PRInt32           aAvailWidth,
00186                                        PRInt32*          aBreaks,
00187                                        PRInt32           aNumBreaks,
00188                                        nsTextDimensions& aDimensions,
00189                                        PRInt32&          aNumCharsFit,
00190                                        nsTextDimensions& aLastWordDimensions,
00191                                        PRInt32*          aFontID);
00192   NS_IMETHOD GetTextDimensionsInternal(const PRUnichar*  aString,
00193                                        PRInt32           aLength,
00194                                        PRInt32           aAvailWidth,
00195                                        PRInt32*          aBreaks,
00196                                        PRInt32           aNumBreaks,
00197                                        nsTextDimensions& aDimensions,
00198                                        PRInt32&          aNumCharsFit,
00199                                        nsTextDimensions& aLastWordDimensions,
00200                                        PRInt32*          aFontID);
00201 
00202 #ifdef MOZ_MATHML
00203 
00206   NS_IMETHOD GetBoundingMetricsInternal(const char*        aString,
00207                                         PRUint32           aLength,
00208                                         nsBoundingMetrics& aBoundingMetrics);
00209 
00213   NS_IMETHOD GetBoundingMetricsInternal(const PRUnichar*   aString,
00214                                         PRUint32           aLength,
00215                                         nsBoundingMetrics& aBoundingMetrics,
00216                                         PRInt32*           aFontID = nsnull);
00217 
00218 #endif /* MOZ_MATHML */
00219 
00220   virtual PRInt32 GetMaxStringLength();
00221 
00222   NS_IMETHOD CopyOffScreenBits(nsIDrawingSurface* aSrcSurf, PRInt32 aSrcX, PRInt32 aSrcY,
00223                                const nsRect &aDestBounds, PRUint32 aCopyFlags);
00224   NS_IMETHOD RetrieveCurrentNativeGraphicData(void** ngd);
00225 
00226   NS_IMETHOD SetRightToLeftText(PRBool aIsRTL);
00227   NS_IMETHOD GetRightToLeftText(PRBool* aIsRTL);
00228   NS_IMETHOD GetClusterInfo(const PRUnichar *aText, PRUint32 aLength,
00229                             PRUint8 *aClusterStarts);
00230   virtual PRInt32 GetPosition(const PRUnichar *aText, PRUint32 aLength,
00231                               nsPoint aPt);
00232   NS_IMETHOD GetRangeWidth(const PRUnichar *aText,
00233                            PRUint32 aLength,
00234                            PRUint32 aStart,
00235                            PRUint32 aEnd,
00236                            PRUint32 &aWidth);
00237   NS_IMETHOD GetRangeWidth(const char *aText,
00238                            PRUint32 aLength,
00239                            PRUint32 aStart,
00240                            PRUint32 aEnd,
00241                            PRUint32 &aWidth);
00242 
00243   NS_IMETHOD DrawImage(imgIContainer *aImage, const nsRect & aSrcRect, const nsRect & aDestRect);
00244 
00245   NS_IMETHOD GetBackbuffer(const nsRect &aRequestedSize, const nsRect &aMaxSize,
00246                            PRBool aForBlending, nsIDrawingSurface* &aBackbuffer);
00247   NS_IMETHOD ReleaseBackbuffer(void);
00248 
00249   //locals
00250   NS_IMETHOD CommonInit();
00251 
00252   void CreateClipRegion();
00253 
00254   GdkGC *GetGC() {
00255     if (!mGC)
00256       UpdateGC();
00257     return gdk_gc_ref(mGC);
00258   }
00259 
00260   void SetClipRectInPixels(const nsRect& aRect, nsClipCombine aCombine);
00261 
00262   // cause the GC to be updated
00263   void UpdateGC();
00264 
00265   // Get a pointer to the trans matrix
00266   nsTransform2D *GetTranMatrix() {
00267     return mTranMatrix;
00268   }
00269 
00270 private:
00271   nsDrawingSurfaceGTK   *mOffscreenSurface;
00272   nsDrawingSurfaceGTK   *mSurface;
00273   nsIDeviceContext      *mContext;
00274   nsIFontMetricsGTK     *mFontMetrics;
00275   nsCOMPtr<nsIRegion>    mClipRegion;
00276   float                  mP2T;
00277   GdkWChar*              mDrawStringBuf;
00278   PRUint32               mDrawStringSize;
00279 
00280  // graphic state stack (GraphicsState)
00281   nsAutoVoidArray        mStateCache;
00282 
00283   GdkGC                 *mGC;
00284   GdkFunction            mFunction;
00285   GdkLineStyle           mLineStyle;
00286   char                   mDashList[2];
00287   int                    mDashes;
00288   nscolor                mCurrentColor;
00289   nsLineStyle            mCurrentLineStyle;
00290 
00291   // ConditionRect is used to fix coordinate overflow problems for
00292   // rectangles after they are transformed to screen coordinates
00293   void ConditionRect(nscoord &x, nscoord &y, nscoord &w, nscoord &h) {
00294     if ( y < -32766 ) {
00295       y = -32766;
00296     }
00297 
00298     if ( y + h > 32766 ) {
00299       h  = 32766 - y;
00300     }
00301 
00302     if ( x < -32766 ) {
00303       x = -32766;
00304     }
00305 
00306     if ( x + w > 32766 ) {
00307       w  = 32766 - x;
00308     }
00309   }
00310 };
00311 
00312 #endif /* nsRenderingContextGTK_h___ */