native theme zooming fix
authorpekka.aho
Sat, 19 Apr 2008 19:06:26 +0300
changeset 16869 fc03afe88f811a449521d46a89cbc7acf13bcaff
parent 16868 d20ffc75a75efb886f6e6f1555b1cc2744bfbb7f
child 16870 634b9d9f09922e66f16644541845f73555035111
push id1298
push userpavlov@mozilla.com
push dateSun, 17 Aug 2008 05:03:09 +0000
treeherderautoland@4a506fa751d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone2.0a1pre
native theme zooming fix
widget/src/qt/nsNativeThemeQt.cpp
widget/src/qt/nsNativeThemeQt.h
--- a/widget/src/qt/nsNativeThemeQt.cpp
+++ b/widget/src/qt/nsNativeThemeQt.cpp
@@ -66,79 +66,82 @@
 #include <malloc.h>
 
 
 #include "gfxASurface.h"
 #include "gfxContext.h"
 #include "gfxQPainterSurface.h"
 #include "nsIRenderingContext.h"
 
-nsNativeThemeQt::nsNativeThemeQt() : mP2A(0)
+nsNativeThemeQt::nsNativeThemeQt()
 {
     combo = new QComboBox((QWidget *)0);
     combo->resize(0, 0);
     ThemeChanged();
 }
 
 nsNativeThemeQt::~nsNativeThemeQt()
 {
     delete combo;
 }
 
 NS_IMPL_ISUPPORTS1(nsNativeThemeQt, nsITheme)
 
-static QRect qRectInPixels(const nsRect &aRect,
-    const nsTransform2D *aTrans, const PRInt32 p2a)
+static inline QRect qRectInPixels(const nsRect &aRect,
+                                  const PRInt32 p2a)
 {
-    int x = aRect.x;
-    int y = aRect.y;
-    int w = aRect.width;
-    int h = aRect.height;
-    aTrans->TransformCoord(&x,&y,&w,&h);
-    return QRect(
-        NSAppUnitsToIntPixels(x, p2a),
-        NSAppUnitsToIntPixels(y, p2a),
-        NSAppUnitsToIntPixels(w, p2a),
-        NSAppUnitsToIntPixels(h, p2a));
+    return QRect(NSAppUnitsToIntPixels(aRect.x, p2a),
+                 NSAppUnitsToIntPixels(aRect.y, p2a),
+                 NSAppUnitsToIntPixels(aRect.width, p2a),
+                 NSAppUnitsToIntPixels(aRect.height, p2a));
 }
 
 NS_IMETHODIMP
 nsNativeThemeQt::DrawWidgetBackground(nsIRenderingContext* aContext,
                                       nsIFrame* aFrame,
                                       PRUint8 aWidgetType,
                                       const nsRect& aRect,
                                       const nsRect& aClipRect)
 {
 //    qDebug("%s : %d", __PRETTY_FUNCTION__, IsDisabled(aFrame));
 
     gfxContext* context = aContext->ThebesContext();
     nsRefPtr<gfxASurface> surface = context->CurrentSurface();
-    gfxASurface* raw = surface;
-    gfxQPainterSurface* qSurface = (gfxQPainterSurface*)raw;
+
+    if (surface->GetType() != gfxASurface::SurfaceTypeQPainter)
+        return NS_ERROR_NOT_IMPLEMENTED;
+
+    gfxQPainterSurface* qSurface = (gfxQPainterSurface*) (surface.get());
     QPainter* qPainter = qSurface->GetQPainter();
 
 //     qDebug("aWidgetType = %d", aWidgetType);
     if (!qPainter)
         return NS_OK;
 
-    EnsuremP2A(aContext);
-
     QStyle* style = qApp->style();
 //    const QPalette::ColorGroup cg = qApp->palette().currentColorGroup();
 
-    nsTransform2D* curTrans;
-    aContext->GetCurrentTransform(curTrans);
+    qPainter->save();
+
+    gfxPoint offs = surface->GetDeviceOffset();
+    qPainter->translate(offs.x, offs.y);
 
-    QRect r = qRectInPixels(aRect, curTrans, mP2A);
-    QRect cr = qRectInPixels(aClipRect, curTrans, mP2A);
+    gfxMatrix ctm = context->CurrentMatrix();
+    if (!ctm.HasNonTranslation()) {
+        ctm.x0 = NSToCoordRound(ctm.x0);
+        ctm.y0 = NSToCoordRound(ctm.y0);
+    }
 
-//    context->UpdateGC();
-    qPainter->save();
-    qPainter->translate(r.x(), r.y());
-    r.translate(-r.x(), -r.y());
+    QMatrix qctm(ctm.xx, ctm.xy, ctm.yx, ctm.yy, ctm.x0, ctm.y0);
+    qPainter->setWorldMatrix(qctm, true);
+
+    PRInt32 p2a = GetAppUnitsPerDevPixel(aContext);
+
+    QRect r = qRectInPixels(aRect, p2a);
+    QRect cr = qRectInPixels(aClipRect, p2a);
 
 //     qDebug("rect=%d %d %d %d\nr=%d %d %d %d",
 //         aRect.x, aRect.y, aRect.width, aRect.height,
 //         r.x(), r.y(), r.width(), r.height());
 
     QStyle::PrimitiveElement pe = QStyle::PE_CustomBase;
 
     QStyle::ControlElement ce = QStyle::CE_CustomBase;
@@ -358,29 +361,26 @@ nsNativeThemeQt::GetMinimumWidgetSize(ns
 {
 //    qDebug("%s", __PRETTY_FUNCTION__);
 
     (*aResult).width = (*aResult).height = 0;
     *aIsOverridable = PR_TRUE;
 
     QStyle *s = qApp->style();
 
+    PRInt32 p2a = GetAppUnitsPerDevPixel(aContext);
+
     switch (aWidgetType) {
     case NS_THEME_RADIO_SMALL:
     case NS_THEME_RADIO:
     case NS_THEME_CHECKBOX_SMALL:
     case NS_THEME_CHECKBOX: {
         nsRect frameRect = aFrame->GetRect();
 
-        EnsuremP2A(aContext);
-
-        nsTransform2D* curTrans;
-        aContext->GetCurrentTransform(curTrans);
-
-        QRect qRect = qRectInPixels(frameRect, curTrans, mP2A);
+        QRect qRect = qRectInPixels(frameRect, p2a);
 
         QStyleOptionButton option;
 
         ButtonStyle(aFrame, qRect, &option);
 
         QRect rect = s->subElementRect(
             (aWidgetType == NS_THEME_CHECKBOX || aWidgetType == NS_THEME_CHECKBOX_SMALL ) ?
                 QStyle::SE_CheckBoxIndicator :
@@ -390,22 +390,17 @@ nsNativeThemeQt::GetMinimumWidgetSize(ns
 
         (*aResult).width = rect.width();
         (*aResult).height = rect.height();
         break;
     }
     case NS_THEME_BUTTON: {
         nsRect frameRect = aFrame->GetRect();
 
-        EnsuremP2A(aContext);
-
-        nsTransform2D* curTrans;
-        aContext->GetCurrentTransform(curTrans);
-
-        QRect qRect = qRectInPixels(frameRect, curTrans, mP2A);
+        QRect qRect = qRectInPixels(frameRect, p2a);
 
         QStyleOptionButton option;
 
         ButtonStyle(aFrame, qRect, &option);
 
         QRect rect = s->subElementRect(
             QStyle::SE_PushButtonFocusRect,
             &option,
@@ -557,25 +552,17 @@ nsNativeThemeQt::ThemeDrawsFocusForWidge
 
 PRBool
 nsNativeThemeQt::ThemeNeedsComboboxDropmarker()
 {
 //    qDebug("%s", __PRETTY_FUNCTION__);
     return PR_FALSE;
 }
 
-void
-nsNativeThemeQt::EnsuremP2A(nsIRenderingContext* aContext)
-{
-    if (!mP2A) {
-        nsCOMPtr<nsIDeviceContext> dctx = nsnull;
-        aContext->GetDeviceContext(*getter_AddRefs(dctx));
-        mP2A = dctx->AppUnitsPerDevPixel();
-    }
-}
+
 
 void
 nsNativeThemeQt::ButtonStyle(nsIFrame* aFrame,
                              QRect aRect,
                              QStyleOptionButton* aOption,
                              QStyle::State optFlags /*= QStyle::State_None*/)
 {
     QStyle::State flags = IsDisabled(aFrame) ?
--- a/widget/src/qt/nsNativeThemeQt.h
+++ b/widget/src/qt/nsNativeThemeQt.h
@@ -40,16 +40,17 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include <QStyle>
 
 #include "nsITheme.h"
 #include "nsCOMPtr.h"
 #include "nsIAtom.h"
 #include "nsNativeTheme.h"
+#include "nsIDeviceContext.h"
 
 class QComboBox;
 class QStyleOptionButton;
 class QStyleOptionFrameV2;
 class QRect;
 class nsIFrame;
 
 class nsNativeThemeQt : private nsNativeTheme,
@@ -96,17 +97,21 @@ public:
 
   PRBool ThemeNeedsComboboxDropmarker();
 
   nsNativeThemeQt();
   virtual ~nsNativeThemeQt();
 
 private:
 
-  void EnsuremP2A(nsIRenderingContext* aContext);
+  inline PRInt32 GetAppUnitsPerDevPixel(nsIRenderingContext* aContext){
+    nsCOMPtr<nsIDeviceContext> dctx = nsnull;
+    aContext->GetDeviceContext(*getter_AddRefs(dctx));
+    return dctx->AppUnitsPerDevPixel();
+  }
 
   void ButtonStyle(nsIFrame* aFrame,
                    QRect aRect,
                    QStyleOptionButton* aOption,
                    QStyle::State optFlags = QStyle::State_None);
 
   void FrameStyle(nsIFrame* aFrame,
                   QRect aRect,
@@ -119,11 +124,10 @@ private:
                   QStyle::State optFlags = QStyle::State_None);
 
 private:
 
   QComboBox *combo;
 
   PRInt32 frameWidth;
 
-  PRInt32 mP2A;
 };