Back to index

lightning-sunbird  0.9+nobinonly
nsDrawingSurfaceQt.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *   Lars Knoll <knoll@kde.org>
00024  *   Zack Rusin <zack@kde.org>
00025  *   John C. Griggs <johng@corel.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsDrawingSurfaceQt.h"
00042 #include "nsRenderingContextQt.h"
00043 #include <qpaintdevicemetrics.h>
00044 
00045 #include "qtlog.h"
00046 
00047 NS_IMPL_ISUPPORTS1(nsDrawingSurfaceQt, nsIDrawingSurface)
00048 
00049 nsDrawingSurfaceQt::nsDrawingSurfaceQt()
00050 {
00051     mPaintDevice = nsnull;
00052     mGC          = nsnull;
00053     mDepth       = -1;
00054     mWidth       = 0;
00055     mHeight      = 0;
00056     mFlags       = 0;
00057     mLockWidth   = 0;
00058     mLockHeight  = 0;
00059     mLockFlags   = 0;
00060     mLocked      = PR_FALSE;
00061 
00062     // I have no idea how to compute these values.
00063     // FIXME
00064     mPixFormat.mRedMask = 0;
00065     mPixFormat.mGreenMask = 0;
00066     mPixFormat.mBlueMask = 0;
00067     mPixFormat.mAlphaMask = 0;
00068 
00069     mPixFormat.mRedShift = 0;
00070     mPixFormat.mGreenShift = 0;
00071     mPixFormat.mBlueShift = 0;
00072     mPixFormat.mAlphaShift = 0;
00073 }
00074 
00075 nsDrawingSurfaceQt::~nsDrawingSurfaceQt()
00076 {
00077     if (mGC && mGC->isActive()) {
00078         mGC->end();
00079     }
00080 
00081     delete mGC;
00082     mGC = nsnull;
00083 
00084     if (mPaintDevice) {
00085         if (mIsOffscreen && !mPaintDevice->paintingActive() && mPaintDevice != &mPixmap)
00086             delete mPaintDevice;
00087         mPaintDevice = nsnull;
00088     }
00089 }
00090 
00091 NS_IMETHODIMP nsDrawingSurfaceQt::Lock(PRInt32 aX,PRInt32 aY,
00092                                        PRUint32 aWidth,PRUint32 aHeight,
00093                                        void **aBits,PRInt32 *aStride,
00094                                        PRInt32 *aWidthBytes,PRUint32 aFlags)
00095 {
00096     if (mLocked) {
00097         NS_ASSERTION(0, "nested lock attempt");
00098         return NS_ERROR_FAILURE;
00099     }
00100     if (mPixmap.isNull()) {
00101         NS_ASSERTION(0, "NULL pixmap in lock attempt");
00102         return NS_ERROR_FAILURE;
00103     }
00104     mLocked     = PR_TRUE;
00105     mLockX      = aX;
00106     mLockY      = aY;
00107     mLockWidth  = aWidth;
00108     mLockHeight = aHeight;
00109     mLockFlags  = aFlags;
00110 
00111     if (mImage.isNull())
00112         mImage = mPixmap.convertToImage();
00113 
00114     *aBits = mImage.bits();
00115     *aStride = mImage.bytesPerLine();
00116     *aWidthBytes = mImage.bytesPerLine();
00117 
00118     return NS_OK;
00119 }
00120 
00121 NS_IMETHODIMP nsDrawingSurfaceQt::Unlock(void)
00122 {
00123     if (!mLocked) {
00124         NS_ASSERTION(0,"attempting to unlock an DrawingSurface that isn't locked");
00125         return NS_ERROR_FAILURE;
00126     }
00127     if (mPixmap.isNull()) {
00128         NS_ASSERTION(0, "NULL pixmap in unlock attempt");
00129         return NS_ERROR_FAILURE;
00130     }
00131     if (!(mLockFlags & NS_LOCK_SURFACE_READ_ONLY)) {
00132         qDebug("nsDrawingSurfaceQt::Unlock: w/h=%d/%d", mPixmap.width(), mPixmap.height());
00133         mGC->drawPixmap(0, 0, mPixmap, mLockY, mLockY, mLockWidth, mLockHeight);
00134     }
00135     mLocked = PR_FALSE;
00136 
00137     return NS_OK;
00138 }
00139 
00140 NS_IMETHODIMP nsDrawingSurfaceQt::GetDimensions(PRUint32 *aWidth,
00141                                                 PRUint32 *aHeight)
00142 {
00143     *aWidth = mWidth;
00144     *aHeight = mHeight;
00145 
00146     return NS_OK;
00147 }
00148 
00149 NS_IMETHODIMP nsDrawingSurfaceQt::IsOffscreen(PRBool *aOffScreen)
00150 {
00151     *aOffScreen = mIsOffscreen;
00152     return NS_OK;
00153 }
00154 
00155 NS_IMETHODIMP nsDrawingSurfaceQt::IsPixelAddressable(PRBool *aAddressable)
00156 {
00157     *aAddressable = PR_FALSE;
00158     return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP nsDrawingSurfaceQt::GetPixelFormat(nsPixelFormat *aFormat)
00162 {
00163     *aFormat = mPixFormat;
00164 
00165     return NS_OK;
00166 }
00167 
00168 NS_IMETHODIMP nsDrawingSurfaceQt::Init(QPaintDevice *aPaintDevice,
00169                                        QPainter *aGC)
00170 {
00171     PR_LOG(gQtLogModule, QT_BASIC, ("[%p] nsDrawingSurface::Init\n", this));
00172     NS_ASSERTION(aPaintDevice, "need paint dev.");
00173     QPaintDeviceMetrics qMetrics(aPaintDevice);
00174     mGC = aGC;
00175 
00176     mPaintDevice = aPaintDevice;
00177 
00178     mWidth = qMetrics.width();
00179     mHeight = qMetrics.height();
00180     mDepth = qMetrics.depth();
00181 
00182     mIsOffscreen = PR_FALSE;
00183 
00184     mImage.reset();
00185 
00186     return CommonInit();
00187 }
00188 
00189 NS_IMETHODIMP nsDrawingSurfaceQt::Init(QPainter *aGC,
00190                                        PRUint32 aWidth,
00191                                        PRUint32 aHeight,
00192                                        PRUint32 aFlags)
00193 {
00194     PR_LOG(gQtLogModule, QT_BASIC, ("[%p] nsDrawingSurface::Init\n", this));
00195     if (nsnull == aGC || aWidth <= 0 || aHeight <= 0) {
00196         return NS_ERROR_FAILURE;
00197     }
00198     mGC     = aGC;
00199     mWidth  = aWidth;
00200     mHeight = aHeight;
00201     mFlags  = aFlags;
00202 
00203     mPixmap = QPixmap(mWidth, mHeight, mDepth);
00204     mPaintDevice = &mPixmap;
00205     NS_ASSERTION(mPaintDevice, "this better not fail");
00206 
00207     mIsOffscreen = PR_TRUE;
00208 
00209     mImage.reset();
00210 
00211     return CommonInit();
00212 }
00213 
00214 NS_IMETHODIMP nsDrawingSurfaceQt::CommonInit()
00215 {
00216     PR_LOG(gQtLogModule, QT_BASIC, ("[%p] nsDrawingSurface::CommonInit\n", this));
00217     if (nsnull  == mGC || nsnull == mPaintDevice) {
00218         return NS_ERROR_FAILURE;
00219     }
00220     if (mGC->isActive()) {
00221         mGC->end();
00222     }
00223     mGC->begin(mPaintDevice);
00224     return NS_OK;
00225 }
00226 
00227 QPainter* nsDrawingSurfaceQt::GetGC()
00228 {
00229     return mGC;
00230 }
00231 
00232 QPaintDevice* nsDrawingSurfaceQt::GetPaintDevice()
00233 {
00234     PR_LOG(gQtLogModule, QT_BASIC, ("[%p] nsDrawingSurfaceQt::GetPaintDevice\n", this));
00235     NS_ASSERTION(mPaintDevice, "No paint device! Something will probably crash soon.");
00236     return mPaintDevice;
00237 }