SelectionOverlay.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2012, 2013 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #if USE(ACCELERATED_COMPOSITING)
  20. #include "SelectionOverlay.h"
  21. #include "Frame.h"
  22. #include "GraphicsContext.h"
  23. #include "LayerMessage.h"
  24. #include "LayerWebKitThread.h"
  25. #include "Path.h"
  26. #include "RenderTheme.h"
  27. #include "RenderView.h"
  28. #include "WebPage_p.h"
  29. #include <BlackBerryPlatformMessageClient.h>
  30. using namespace WebCore;
  31. namespace BlackBerry {
  32. namespace WebKit {
  33. SelectionOverlay::SelectionOverlay(WebPagePrivate* page)
  34. : m_page(page)
  35. {
  36. }
  37. SelectionOverlay::~SelectionOverlay()
  38. {
  39. }
  40. void SelectionOverlay::draw(const Selection& selection)
  41. {
  42. ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
  43. m_selection = selection;
  44. while (m_layers.size() < static_cast<size_t>(m_selection.size()))
  45. m_layers.append(GraphicsLayer::create(this));
  46. m_layers.resize(m_selection.size());
  47. size_t i = 0;
  48. for (Selection::iterator it = m_selection.begin(); it != m_selection.end(); ++it, ++i) {
  49. GraphicsLayer* parent = it->key;
  50. GraphicsLayer* overlay = m_layers[i].get();
  51. parent->platformLayer()->addOverlay(overlay->platformLayer());
  52. overlay->setPosition(FloatPoint::zero());
  53. if (parent == m_page->m_overlayLayer)
  54. overlay->setSize(m_page->contentsSize());
  55. else
  56. overlay->setSize(parent->size());
  57. overlay->setAnchorPoint(FloatPoint3D(0, 0, 0));
  58. overlay->setDrawsContent(true);
  59. overlay->setNeedsDisplay();
  60. }
  61. }
  62. void SelectionOverlay::hide()
  63. {
  64. ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
  65. for (size_t i = 0; i < m_layers.size(); ++i)
  66. m_layers[i]->platformLayer()->removeFromSuperlayer();
  67. m_selection.clear();
  68. }
  69. void SelectionOverlay::notifyFlushRequired(const GraphicsLayer* layer)
  70. {
  71. m_page->notifyFlushRequired(layer);
  72. }
  73. void SelectionOverlay::paintContents(const GraphicsLayer* layer, GraphicsContext& c, GraphicsLayerPaintingPhase, const IntRect& inClip)
  74. {
  75. if (!layer->platformLayer()->superlayer())
  76. return;
  77. Selection::iterator it = m_selection.find(layer->platformLayer()->superlayer()->owner());
  78. if (it == m_selection.end())
  79. return;
  80. const Vector<WebCore::FloatQuad>& quads = it->value;
  81. GraphicsLayer* parent = it->key;
  82. IntRect clip(inClip);
  83. clip.move(parent->offsetFromRenderer().width(), parent->offsetFromRenderer().height());
  84. c.save();
  85. c.translate(-parent->offsetFromRenderer().width(), -parent->offsetFromRenderer().height());
  86. Color color = RenderTheme::defaultTheme()->activeSelectionBackgroundColor();
  87. c.setFillColor(color, ColorSpaceDeviceRGB);
  88. for (unsigned i = 0; i < quads.size(); ++i) {
  89. FloatRect rectToPaint = quads[i].boundingBox();
  90. // Selection on non-composited sub-frames need to be adjusted.
  91. if (!m_page->focusedOrMainFrame()->contentRenderer()->isComposited()) {
  92. WebCore::IntPoint framePosition = m_page->frameOffset(m_page->focusedOrMainFrame());
  93. rectToPaint.move(framePosition.x(), framePosition.y());
  94. }
  95. rectToPaint.intersect(clip);
  96. if (rectToPaint.isEmpty())
  97. continue;
  98. c.fillRect(rectToPaint);
  99. }
  100. c.restore();
  101. }
  102. } // namespace WebKit
  103. } // namespace BlackBerry
  104. #endif // USE(ACCELERATED_COMPOSITING)