Back to index

lightning-sunbird  0.9+nobinonly
nsGraphicState.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 #include "nsGraphicState.h"
00039 #include "nsDrawingSurfaceMac.h"
00040 #include "nsRegionMac.h"
00041 #include "nsIFontMetrics.h"
00042 #include "nsCarbonHelpers.h"
00043 #include "nsRegionPool.h"
00044 
00045 nsGraphicStatePool sGraphicStatePool;
00046 
00047 
00048 //------------------------------------------------------------------------
00049 
00050 nsGraphicStatePool::nsGraphicStatePool()
00051 {
00052        mFreeList = nsnull;
00053 }
00054 
00055 //------------------------------------------------------------------------
00056 
00057 nsGraphicStatePool::~nsGraphicStatePool()
00058 {
00059        nsGraphicState* gs = mFreeList;
00060        while (gs != nsnull) {
00061               nsGraphicState* next = gs->mNext;
00062               delete gs;
00063               gs = next;
00064        }
00065 }
00066 
00067 //------------------------------------------------------------------------
00068 
00069 nsGraphicState* nsGraphicStatePool::GetNewGS()
00070 {
00071        nsGraphicState* gs = mFreeList;
00072        if (gs != nsnull) {
00073               mFreeList = gs->mNext;
00074               return gs;
00075        }
00076        return new nsGraphicState;
00077 }
00078 
00079 //------------------------------------------------------------------------
00080 
00081 void nsGraphicStatePool::ReleaseGS(nsGraphicState* aGS)
00082 {
00083        // clear the graphics state? this will cause its transformation matrix and regions
00084        // to be released. I'm dubious about that. in fact, shouldn't the matrix always be
00085        // a member object of the graphics state? why have a separate allocation at all?
00086        aGS->Clear();
00087        aGS->mNext = mFreeList;
00088        mFreeList = aGS;
00089 }
00090 
00091 
00092 #pragma mark -
00093 //------------------------------------------------------------------------
00094 
00095 nsGraphicState::nsGraphicState()
00096 {
00097        // everything is initialized to 0 through the 'new' operator
00098 }
00099 
00100 //------------------------------------------------------------------------
00101 
00102 nsGraphicState::~nsGraphicState()
00103 {
00104        Clear();
00105 }
00106 
00107 //------------------------------------------------------------------------
00108 
00109 void nsGraphicState::Clear()
00110 {
00111        mTMatrix.SetToIdentity();
00112 
00113        if (mMainRegion) {
00114               sNativeRegionPool.ReleaseRegion(mMainRegion); //::DisposeRgn(mMainRegion);
00115               mMainRegion = nsnull;
00116        }
00117 
00118        if (mClipRegion) {
00119               sNativeRegionPool.ReleaseRegion(mClipRegion); //::DisposeRgn(mClipRegion);
00120               mClipRegion = nsnull;
00121        }
00122 
00123   NS_IF_RELEASE(mFontMetrics);
00124 
00125   mOffx                                          = 0;
00126   mOffy                                          = 0;
00127   mColor                                  = NS_RGB(255,255,255);
00128        mFont                                     = 0;
00129   mFontMetrics              = nsnull;
00130   mCurrFontHandle    = 0;
00131   mLineStyle = nsLineStyle_kSolid;
00132 }
00133 
00134 //------------------------------------------------------------------------
00135 
00136 void nsGraphicState::Init(nsIDrawingSurface* aSurface)
00137 {
00138        // retrieve the grafPort
00139        nsDrawingSurfaceMac* surface = static_cast<nsDrawingSurfaceMac*>(aSurface);
00140        CGrafPtr port;
00141        surface->GetGrafPtr(&port);
00142 
00143        // init from grafPort
00144        Init(port);
00145 }
00146 
00147 //------------------------------------------------------------------------
00148 
00149 void nsGraphicState::Init(CGrafPtr aPort)
00150 {
00151        // delete old values
00152        Clear();
00153 
00154        // init from grafPort (usually an offscreen port)
00155        RgnHandle     rgn = sNativeRegionPool.GetNewRegion(); //::NewRgn();
00156        if ( rgn ) {
00157               Rect bounds;
00158               ::RectRgn(rgn, ::GetPortBounds(aPort, &bounds));
00159        }
00160 
00161   Rect    portBounds;
00162   ::GetPortBounds(aPort, &portBounds);
00163   mOffx = -portBounds.left;
00164   mOffy = -portBounds.top;
00165   
00166   mMainRegion                             = rgn;
00167   mClipRegion                             = DuplicateRgn(rgn);
00168 }
00169 
00170 //------------------------------------------------------------------------
00171 
00172 void nsGraphicState::Init(nsIWidget* aWindow)
00173 {
00174        // delete old values
00175        Clear();
00176 
00177        // init from widget
00178   mOffx                                          = (PRInt32)aWindow->GetNativeData(NS_NATIVE_OFFSETX);
00179   mOffy                                          = (PRInt32)aWindow->GetNativeData(NS_NATIVE_OFFSETY);
00180 
00181        RgnHandle widgetRgn = (RgnHandle)aWindow->GetNativeData(NS_NATIVE_REGION);
00182        mMainRegion                               = DuplicateRgn(widgetRgn);
00183   mClipRegion                             = DuplicateRgn(widgetRgn);
00184 }
00185 
00186 //------------------------------------------------------------------------
00187 
00188 void nsGraphicState::Duplicate(nsGraphicState* aGS)
00189 {
00190        // clear old values? no need, just copying them any way.
00191        // Clear();
00192 
00193        // copy new ones
00194        mTMatrix.SetMatrix(&aGS->mTMatrix);
00195 
00196        mOffx                                     = aGS->mOffx;
00197        mOffy                                     = aGS->mOffy;
00198 
00199        mMainRegion                 = DuplicateRgn(aGS->mMainRegion, mMainRegion);
00200        mClipRegion                 = DuplicateRgn(aGS->mClipRegion, mClipRegion);
00201 
00202        mColor                             = aGS->mColor;
00203        mFont                                     = aGS->mFont;
00204        
00205        NS_IF_RELEASE(mFontMetrics);
00206        mFontMetrics         = aGS->mFontMetrics;
00207        NS_IF_ADDREF(mFontMetrics);
00208 
00209        mCurrFontHandle      = aGS->mCurrFontHandle;
00210 
00211        mLineStyle = aGS->mLineStyle;
00212        
00213        mChanges                           = aGS->mChanges;
00214 }
00215 
00216 //------------------------------------------------------------------------
00217 
00218 RgnHandle nsGraphicState::DuplicateRgn(RgnHandle aRgn, RgnHandle aDestRgn)
00219 {
00220        RgnHandle dupRgn = aDestRgn;
00221        if (aRgn)     {
00222               if (nsnull == dupRgn)
00223                      dupRgn = sNativeRegionPool.GetNewRegion(); //::NewRgn();
00224               if (dupRgn)
00225                      ::CopyRgn(aRgn, dupRgn);
00226        }
00227        return dupRgn;
00228 }