PopupMenuQt.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is part of the popup menu implementation for <select> elements in WebCore.
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Nokia Corporation and/or its subsidiary(-ies)
  5. * Copyright (C) 2006 Apple Computer, Inc.
  6. * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
  7. * Coypright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public License
  20. * along with this library; see the file COPYING.LIB. If not, write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. *
  24. */
  25. #include "config.h"
  26. #include "PopupMenuQt.h"
  27. #include "ChromeClientQt.h"
  28. #include "FrameView.h"
  29. #include "PopupMenuClient.h"
  30. #include "qwebkitplatformplugin.h"
  31. class SelectData : public QWebSelectData {
  32. public:
  33. SelectData(WebCore::PopupMenuClient*& data) : d(data) { }
  34. virtual ItemType itemType(int) const;
  35. virtual QString itemText(int idx) const { return QString(d ? d->itemText(idx) : ""); }
  36. virtual QString itemToolTip(int idx) const { return QString(d ? d->itemToolTip(idx) : ""); }
  37. virtual bool itemIsEnabled(int idx) const { return d ? d->itemIsEnabled(idx) : false; }
  38. virtual int itemCount() const { return d ? d->listSize() : 0; }
  39. virtual bool itemIsSelected(int idx) const { return d ? d->itemIsSelected(idx) : false; }
  40. virtual bool multiple() const;
  41. virtual QColor backgroundColor() const { return d ? QColor(d->menuStyle().backgroundColor()) : QColor(); }
  42. virtual QColor foregroundColor() const { return d ? QColor(d->menuStyle().foregroundColor()) : QColor(); }
  43. virtual QColor itemBackgroundColor(int idx) const { return d ? QColor(d->itemStyle(idx).backgroundColor()) : QColor(); }
  44. virtual QColor itemForegroundColor(int idx) const { return d ? QColor(d->itemStyle(idx).foregroundColor()) : QColor(); }
  45. private:
  46. WebCore::PopupMenuClient*& d;
  47. };
  48. bool SelectData::multiple() const
  49. {
  50. if (!d)
  51. return false;
  52. return d->multiple();
  53. }
  54. SelectData::ItemType SelectData::itemType(int idx) const
  55. {
  56. if (!d)
  57. return SelectData::Option;
  58. if (d->itemIsSeparator(idx))
  59. return SelectData::Separator;
  60. if (d->itemIsLabel(idx))
  61. return SelectData::Group;
  62. return SelectData::Option;
  63. }
  64. namespace WebCore {
  65. PopupMenuQt::PopupMenuQt(PopupMenuClient* client, const ChromeClientQt* chromeClient)
  66. : m_popupClient(client)
  67. , m_chromeClient(chromeClient)
  68. {
  69. }
  70. PopupMenuQt::~PopupMenuQt()
  71. {
  72. }
  73. void PopupMenuQt::disconnectClient()
  74. {
  75. m_popupClient = 0;
  76. }
  77. void PopupMenuQt::show(const IntRect& rect, FrameView* view, int index)
  78. {
  79. if (!m_popupClient)
  80. return;
  81. if (!m_popup) {
  82. m_popup = m_chromeClient->createSelectPopup();
  83. connect(m_popup.get(), SIGNAL(didHide()), this, SLOT(didHide()));
  84. connect(m_popup.get(), SIGNAL(selectItem(int, bool, bool)), this, SLOT(selectItem(int, bool, bool)));
  85. }
  86. QRect geometry(rect);
  87. geometry.moveTopLeft(view->contentsToWindow(rect.location()));
  88. m_popup->setGeometry(geometry);
  89. m_popup->setFont(m_popupClient->menuStyle().font().syntheticFont());
  90. m_selectData = adoptPtr(new SelectData(m_popupClient));
  91. m_popup->show(*m_selectData.get());
  92. }
  93. void PopupMenuQt::didHide()
  94. {
  95. if (m_popupClient)
  96. m_popupClient->popupDidHide();
  97. }
  98. void PopupMenuQt::hide()
  99. {
  100. if (m_popup)
  101. m_popup->hide();
  102. }
  103. void PopupMenuQt::updateFromElement()
  104. {
  105. if (m_popupClient)
  106. m_popupClient->setTextFromItem(m_popupClient->selectedIndex());
  107. }
  108. void PopupMenuQt::selectItem(int index, bool ctrl, bool shift)
  109. {
  110. if (!m_popupClient)
  111. return;
  112. m_popupClient->listBoxSelectItem(index, ctrl, shift);
  113. return;
  114. m_popupClient->valueChanged(index);
  115. }
  116. }
  117. #include "moc_PopupMenuQt.cpp"
  118. // vim: ts=4 sw=4 et