2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "PageOverlayList.h"
32 #include "PageOverlay.h"
33 #include "WebPageOverlay.h"
34 #include "WebViewImpl.h"
38 PassOwnPtr<PageOverlayList> PageOverlayList::create(WebViewImpl* viewImpl)
40 return adoptPtr(new PageOverlayList(viewImpl));
43 PageOverlayList::PageOverlayList(WebViewImpl* viewImpl)
44 : m_viewImpl(viewImpl)
48 PageOverlayList::~PageOverlayList()
52 bool PageOverlayList::add(WebPageOverlay* overlay, int zOrder)
55 size_t index = find(overlay);
56 if (index == WTF::notFound) {
57 OwnPtr<PageOverlay> pageOverlay = PageOverlay::create(m_viewImpl, overlay);
58 m_pageOverlays.append(pageOverlay.release());
59 index = m_pageOverlays.size() - 1;
63 PageOverlay* pageOverlay = m_pageOverlays[index].get();
64 pageOverlay->setZOrder(zOrder);
66 // Adjust page overlay list order based on their z-order numbers. We first
67 // check if we need to move the overlay up and do so if needed. Otherwise,
68 // check if we need to move it down.
69 bool zOrderChanged = false;
70 for (size_t i = index; i + 1 < m_pageOverlays.size(); ++i) {
71 if (m_pageOverlays[i]->zOrder() >= m_pageOverlays[i + 1]->zOrder()) {
72 m_pageOverlays[i].swap(m_pageOverlays[i + 1]);
78 for (size_t i = index; i >= 1; --i) {
79 if (m_pageOverlays[i]->zOrder() < m_pageOverlays[i - 1]->zOrder()) {
80 m_pageOverlays[i].swap(m_pageOverlays[i - 1]);
86 // If we did move the overlay, that means z-order is changed and we need to
87 // update overlay layers' z-order. Otherwise, just update current overlay.
89 for (size_t i = 0; i < m_pageOverlays.size(); ++i)
90 m_pageOverlays[i]->clear();
93 pageOverlay->update();
98 bool PageOverlayList::remove(WebPageOverlay* overlay)
100 size_t index = find(overlay);
101 if (index == WTF::notFound)
104 m_pageOverlays[index]->clear();
105 m_pageOverlays.remove(index);
109 void PageOverlayList::update()
111 for (size_t i = 0; i < m_pageOverlays.size(); ++i)
112 m_pageOverlays[i]->update();
115 void PageOverlayList::paintWebFrame(WebCore::GraphicsContext& gc)
117 for (size_t i = 0; i < m_pageOverlays.size(); ++i)
118 m_pageOverlays[i]->paintWebFrame(gc);
121 size_t PageOverlayList::find(WebPageOverlay* overlay)
123 for (size_t i = 0; i < m_pageOverlays.size(); ++i) {
124 if (m_pageOverlays[i]->overlay() == overlay)
127 return WTF::notFound;
130 size_t PageOverlayList::findGraphicsLayer(WebCore::GraphicsLayer* layer)
132 for (size_t i = 0; i < m_pageOverlays.size(); ++i) {
133 if (m_pageOverlays[i]->graphicsLayer() == layer)
136 return WTF::notFound;
139 } // namespace WebKit