Implement native look for comboboxes
authorzack <zack@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Feb 2007 12:27:56 +0000 (12:27 +0000)
committerzack <zack@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Feb 2007 12:27:56 +0000 (12:27 +0000)
and fix/implement popup menus. Also add the m_timestamp
and m_eventType setting to PlatformMouseEvent
r=Lars

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@19338 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/PopupMenu.h
WebCore/platform/qt/PlatformMouseEventQt.cpp
WebCore/platform/qt/PopupMenuQt.cpp
WebCore/platform/qt/RenderThemeQt.cpp

index 89ea046..2809078 100644 (file)
@@ -1,3 +1,25 @@
+2007-02-01  Zack Rusin  <zrusin@trolltech.com>
+
+        Reviewed by Lars
+
+        Implement native look for comboboxes
+        and fix/implement popup menus.
+
+        * platform/PopupMenu.h:
+        * platform/qt/PopupMenuQt.cpp:
+        (WebCore::PopupMenu::PopupMenu):
+        (WebCore::PopupMenu::~PopupMenu):
+        (WebCore::PopupMenu::clear):
+        (WebCore::PopupMenu::populate):
+        (WebCore::PopupMenu::show):
+        (WebCore::PopupMenu::hide):
+        (WebCore::PopupMenu::updateFromElement):
+        * platform/qt/RenderThemeQt.cpp:
+        (WebCore::RenderThemeQt::adjustMenuListStyle):
+        (WebCore::RenderThemeQt::paintButton):
+        (WebCore::RenderThemeQt::paintTextField):
+        (WebCore::RenderThemeQt::paintMenuList):
+
 2007-02-01  Darin Adler  <darin@apple.com>
 
         Reviewed by Maciej.
index 8641217..7fa7089 100644 (file)
@@ -38,6 +38,10 @@ class NSPopUpButtonCell;
 typedef struct HWND__* HWND;
 typedef struct HDC__* HDC;
 typedef struct HBITMAP__* HBITMAP;
+#elif PLATFORM(QT)
+#include <QHash>
+class QMenu;
+class QListWidgetItem;
 #endif
 
 namespace WebCore {
@@ -102,6 +106,11 @@ private:
     void populate();
 
     RetainPtr<NSPopUpButtonCell> m_popup;
+#elif PLATFORM(QT)
+    void clear();
+    void populate(const IntRect&);
+    QMenu* m_popup;
+    QHash<QListWidgetItem*, int> m_actions;
 #elif PLATFORM(WIN)
     void calculatePositionAndSize(const IntRect&, FrameView*);
     void invalidateItem(int index);
index d0185d3..df7494f 100644 (file)
@@ -28,6 +28,8 @@
 #include "config.h"
 #include "PlatformMouseEvent.h"
 
+#include "SystemTime.h"
+
 #include <QMouseEvent>
 
 namespace WebCore {
@@ -38,6 +40,21 @@ PlatformMouseEvent::PlatformMouseEvent(QMouseEvent* event, int clickCount)
 {
     m_position = IntPoint(event->pos());
     m_globalPosition = IntPoint(event->globalPos());
+    
+    m_timestamp = WebCore::currentTime();
+    switch(event->type()) {
+    case QEvent::MouseMove:
+        m_eventType = MouseEventMoved;
+        break;
+    case QEvent::MouseButtonPress:
+        m_eventType = MouseEventPressed;
+        break;
+    case QEvent::MouseButtonRelease:
+        m_eventType = MouseEventReleased;
+        break;
+    default:
+        m_eventType = MouseEventMoved;
+    }
 
     if (event->button() == Qt::LeftButton || (event->buttons() & Qt::LeftButton))
         m_button = LeftButton;
index 06ac3e4..a6bd430 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * This file is part of the popup menu implementation for <select> elements in WebCore.
  *
+ * Copyright (C) 2007 Trolltech
  * Copyright (C) 2006 Apple Computer, Inc.
  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com 
  * Coypright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
 #include "config.h"
 #include "PopupMenu.h"
 
+#include "FrameQt.h"
+#include "FrameView.h"
+#include "PopupMenuClient.h"
+
+#include <QAction>
+#include <QDebug>
+#include <QMenu>
+#include <QPoint>
+#include <QListWidget>
+#include <QListWidgetItem>
+#include <QWidgetAction>
 #include <qglobal.h>
 
 #define notImplemented() qDebug("FIXME: UNIMPLEMENTED: %s:%d (%s)", __FILE__, __LINE__, __FUNCTION__)
 
 namespace WebCore {
 
-PopupMenu::PopupMenu(PopupMenuClient* menuList)
+PopupMenu::PopupMenu(PopupMenuClient* client)
+    : m_popupClient(client)
 {
+    m_popup = new QMenu();
 }
 
 PopupMenu::~PopupMenu()
 {
+    delete m_popup;
+}
+
+void PopupMenu::clear()
+{
+    m_popup->clear();
+    m_actions.clear();
+}
+
+void PopupMenu::populate(const IntRect& r)
+{
+    clear();
+    Q_ASSERT(client());
+    int size = client()->listSize();
+
+    QListWidget* listWidget = new QListWidget(m_popup);
+
+    for (int i = 0; i < size; i++) {
+        QListWidgetItem* item = 0;
+        if (client()->itemIsSeparator(i)) {
+            //FIXME: better seperator item
+            item = new QListWidgetItem("---");
+            item->setFlags(0);
+        }
+        else {
+            RenderStyle* style = client()->itemStyle(i);
+            item = new QListWidgetItem(client()->itemText(i));
+            m_actions.insert(item, i);
+            if (style->font() != Font())
+                item->setFont(style->font());
+
+            Qt::ItemFlags flags = Qt::ItemIsSelectable;
+            if (client()->itemIsEnabled(i))
+                flags |= Qt::ItemIsEnabled;
+            item->setFlags(flags);
+        }
+        if (item)
+            listWidget->addItem(item);
+    }
+    QWidgetAction* action = new QWidgetAction(m_popup);
+    action->setDefaultWidget(listWidget);
+    m_popup->addAction(action);
+    m_popup->setDefaultAction(action);
+    m_popup->resize(r.width(), r.width());
+    QObject::connect(listWidget, SIGNAL(itemActivated(QListWidgetItem*)),
+                     m_popup, SLOT(hide()));
 }
 
-void PopupMenu::show(const IntRect&, FrameView*, int)
+void PopupMenu::show(const IntRect& r, FrameView* v, int index)
 {
-    notImplemented();
+    populate(r);
+    //m_popup->setParent(v->qwidget());
+
+    RefPtr<PopupMenu> protector(this);
+    QAction* action = m_popup->exec(v->qwidget()->mapToGlobal(QPoint(r.x(), r.y())));
+    if (!action)
+        action = m_popup->defaultAction();
+
+    if (client()) {
+        QWidgetAction* wa = qobject_cast<QWidgetAction*>(action);
+
+        client()->hidePopup();
+
+        if (!wa)
+            return;
+        QListWidget* lw = qobject_cast<QListWidget*>(wa->defaultWidget());
+        if (!lw)
+            return;
+        QListWidgetItem* item = lw->currentItem();
+        if (!item)
+            return;
+        int newIndex = m_actions[item];
+        if (index != newIndex && newIndex >= 0)
+            client()->valueChanged(newIndex);
+    }
 }
 
 void PopupMenu::hide()
 {
-    notImplemented();
+    m_popup->hide();
 }
 
 void PopupMenu::updateFromElement()
 {
-    notImplemented();
 }
 
 }
index 297dc5e..5225c3a 100644 (file)
@@ -54,7 +54,7 @@ public:
     {
         return paintButton(o, i, r);
     }
+
     virtual void setCheckboxSize(RenderStyle*) const;
 
     virtual bool paintRadio(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
@@ -82,6 +82,9 @@ public:
     virtual Color platformInactiveSelectionBackgroundColor() const;
     virtual Color platformActiveSelectionForegroundColor() const;
     virtual Color platformInactiveSelectionForegroundColor() const;
+
+    void adjustMenuListStyle(CSSStyleSelector*, RenderStyle*, Element*) const;
+    bool paintMenuList(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r);
 private:
     void addIntrinsicMargins(RenderStyle*) const;
     void close();
@@ -229,22 +232,27 @@ void RenderThemeQt::adjustButtonStyle(CSSStyleSelector* selector, RenderStyle* s
     addIntrinsicMargins(style);
 }
 
+void RenderThemeQt::adjustMenuListStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
+{
+    addIntrinsicMargins(style);
+}
+
 bool RenderThemeQt::paintButton(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
 {
     QStyle* style = 0;
     QPainter* painter = 0;
     QWidget* widget = 0;
-    
+
     if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
         return true;
-    
+
     QStyleOptionButton option;
     option.initFrom(widget);
     option.rect = r;
 
     // Get the correct theme data for a button
     EAppearance appearance = applyTheme(option, o);
-  
+
     if(appearance == PushButtonAppearance || appearance == ButtonAppearance)
         style->drawControl(QStyle::CE_PushButton, &option, painter);
     else if(appearance == RadioAppearance)
@@ -265,10 +273,10 @@ bool RenderThemeQt::paintTextField(RenderObject* o, const RenderObject::PaintInf
     QStyle* style = 0;
     QPainter* painter = 0;
     QWidget* widget = 0;
-    
+
     if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
         return true;
-    
+
     QStyleOptionFrameV2 panel;
     panel.initFrom(widget);
     panel.rect = r;
@@ -307,6 +315,22 @@ Color RenderThemeQt::platformInactiveSelectionForegroundColor() const
     return pal.brush(QPalette::Inactive, QPalette::HighlightedText).color();
 }
 
+bool RenderThemeQt::paintMenuList(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
+{
+    QStyle* style = 0;
+    QPainter* painter = 0;
+    QWidget* widget = 0;
+
+    if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
+        return true;
+
+    QStyleOptionComboBox opt;
+    opt.initFrom(widget);
+    opt.rect = r;
+    style->drawComplexControl(QStyle::CC_ComboBox, &opt, painter, widget);
+    return false;
+}
+
 }
 
 // vim: ts=4 sw=4 et