/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "config.h"
#include "ScrollbarThemeClutter.h"

#include <glib.h>
#include "NotImplemented.h"
#include "RenderThemeClutter.h"
#include "Scrollbar.h"

#include "webkit/webkitirendertheme.h"

namespace WebCore {

ScrollbarTheme* ScrollbarTheme::nativeTheme()
{
    static ScrollbarThemeClutter theme;
    return &theme;
}

ScrollbarThemeClutter::ScrollbarThemeClutter()
{
    updateThemeProperties();
}

void ScrollbarThemeClutter::updateThemeProperties()
{
    WebKitIRenderThemeScrollbarMetrics metrics;
    WebKitIRenderTheme *theme = webkit_irender_theme_get_default();

    webkit_irender_theme_get_scrollbar_metrics(theme, &metrics);
    m_thumbFatness = metrics.slider_width;
    m_troughBorderWidth = metrics.trough_border;
    m_stepperSize = metrics.stepper_size;
    m_stepperSpacing = metrics.stepper_spacing;
    m_minThumbLength = metrics.min_slider_size;
    m_troughUnderSteppers = metrics.trough_under_steppers;
    m_hasForwardButtonStartPart = metrics.has_secondary_forward_stepper;
    m_hasBackButtonEndPart = metrics.has_secondary_backward_stepper;
    m_hasButtons = metrics.has_buttons;
}

int ScrollbarThemeClutter::scrollbarThickness(ScrollbarControlSize)
{
    return m_thumbFatness + (m_troughBorderWidth * 2);;
}

double ScrollbarThemeClutter::initialAutoscrollTimerDelay()
{
    notImplemented();
    return 0.5;
}

double ScrollbarThemeClutter::autoscrollTimerDelay()
{
    notImplemented();
    return 0.5;
}

void ScrollbarThemeClutter::registerScrollbar(ScrollbarThemeClient*)
{
    notImplemented();
}

void ScrollbarThemeClutter::unregisterScrollbar(ScrollbarThemeClient*)
{
    notImplemented();
}

bool ScrollbarThemeClutter::hasThumb(ScrollbarThemeClient* scrollbar)
{
    return thumbLength(scrollbar) > 0;
}

IntRect ScrollbarThemeClutter::trackRect(ScrollbarThemeClient* scrollbar, bool painting)
{
    IntRect trackRect(scrollbar->frameRect());

    // Buttons.
    if (scrollbar->orientation() == VerticalScrollbar) {
        trackRect.move(0, buttonSize(scrollbar).height());
        trackRect.setHeight(trackRect.height() - (buttonSize(scrollbar).height() * 2));
    } else {
        trackRect.move(buttonSize(scrollbar).width(), 0);
        trackRect.setWidth(trackRect.width() - (buttonSize(scrollbar).width() * 2));
    }

    return trackRect;
}

int ScrollbarThemeClutter::minimumThumbLength(ScrollbarThemeClient* scrollbar)
{
    return m_minThumbLength;
}

void ScrollbarThemeClutter::paintScrollbarBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar)
{
    WebKitIRenderThemeWidgetState state;
    state.focused = FALSE;
    state.isDefault = FALSE;
    state.canDefault = FALSE;
    state.disabled = FALSE;
    state.active = TRUE;
    state.inHover = FALSE;

    IntRect fullScrollbarRect = IntRect(scrollbar->x(), scrollbar->y(), scrollbar->width(), scrollbar->height());
    static_cast<RenderThemeClutter*>(RenderTheme::defaultTheme().get())->paintWidget(widgetScrolledWindow, context, fullScrollbarRect, &state, 0);
}

void ScrollbarThemeClutter::paintTrackPiece(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart scrollbarPart)
{
    WebKitIRenderThemeWidgetState state;
    state.focused = FALSE;
    state.isDefault = FALSE;
    state.canDefault = FALSE;
    state.disabled = FALSE;
    state.active = FALSE;
    state.inHover = FALSE;

    WebKitIRenderThemeWidgetType type = scrollbar->orientation() == VerticalScrollbar ? widgetScrollbarTrackVertical : widgetScrollbarTrackHorizontal;
    static_cast<RenderThemeClutter*>(RenderTheme::defaultTheme().get())->paintWidget(type, context, rect, &state, 0);
}

void ScrollbarThemeClutter::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
{
    // The track background is only painted by this method when in one piece - when there is no
    // thumb, otherwise we'll paint over the buttons.
    if (hasThumb(scrollbar))
         return;

    WebKitIRenderThemeWidgetState state;
    state.focused = FALSE;
    state.isDefault = FALSE;
    state.canDefault = FALSE;
    state.disabled = FALSE;
    state.active = FALSE;
    state.inHover = FALSE;

    WebKitIRenderThemeWidgetType type = scrollbar->orientation() == VerticalScrollbar ? widgetScrollbarTrackVertical : widgetScrollbarTrackHorizontal;
    static_cast<RenderThemeClutter*>(RenderTheme::defaultTheme().get())->paintWidget(type, context, rect, &state, 0);
}

IntSize ScrollbarThemeClutter::buttonSize(ScrollbarThemeClient* scrollbar)
{
    if (scrollbar->orientation() == VerticalScrollbar)
        return IntSize(m_thumbFatness, m_stepperSize);

    // HorizontalScrollbar
    return IntSize(m_stepperSize, m_thumbFatness);
}

IntRect ScrollbarThemeClutter::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
{
    if (part == BackButtonEndPart)
        return IntRect();

    IntSize size = buttonSize(scrollbar);
    return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height());
}

IntRect ScrollbarThemeClutter::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
{
    if (part == ForwardButtonStartPart)
        return IntRect();

    IntSize size = buttonSize(scrollbar);
    int x, y;
    if (scrollbar->orientation() == HorizontalScrollbar) {
        x = scrollbar->x() + scrollbar->width() - size.width();
        y = scrollbar->y();
    } else {
        x = scrollbar->x();
        y = scrollbar->y() + scrollbar->height() - size.height();
    }
    return IntRect(x, y, size.width(), size.height());
}

bool ScrollbarThemeClutter::hasButtons(ScrollbarThemeClient* scrollbar)
{
    return m_hasButtons;
}

void ScrollbarThemeClutter::paintButton(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect, ScrollbarPart part)
{
    int flags = 0;
    if (scrollbar->orientation() == VerticalScrollbar)
        flags |= stepperVertical;

    if (part == ForwardButtonEndPart)
        flags |= (stepperDown | stepperBottom);
    if (part == ForwardButtonStartPart)
        flags |= stepperDown;

    WebKitIRenderThemeWidgetState state;
    state.focused = TRUE;
    state.isDefault = TRUE;
    state.canDefault = TRUE;

    if ((BackButtonStartPart == part && scrollbar->currentPos())
        || (BackButtonEndPart == part && scrollbar->currentPos())
        || (ForwardButtonEndPart == part && scrollbar->currentPos() != scrollbar->maximum())
        || (ForwardButtonStartPart == part && scrollbar->currentPos() != scrollbar->maximum())) {
        state.disabled = FALSE;
        state.active = part == scrollbar->pressedPart();
        state.inHover = part == scrollbar->hoveredPart();
    } else {
        state.disabled = TRUE;
        state.active = FALSE;
        state.inHover = FALSE;
    }

    static_cast<RenderThemeClutter*>(RenderTheme::defaultTheme().get())->paintWidget(widgetScrollbarButton, context, rect, &state, flags);
}

void ScrollbarThemeClutter::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
{
    WebKitIRenderThemeWidgetState state;
    state.focused = FALSE;
    state.isDefault = FALSE;
    state.canDefault = FALSE;
    state.disabled = FALSE;
    state.active = scrollbar->pressedPart() == ThumbPart;
    state.inHover = scrollbar->hoveredPart() == ThumbPart;
    state.maxpos = scrollbar->maximum();
    state.curpos = scrollbar->currentPos();

    WebKitIRenderThemeWidgetType type = scrollbar->orientation() == VerticalScrollbar ? widgetScrollbarThumbVertical : widgetScrollbarThumbHorizontal;
    static_cast<RenderThemeClutter*>(RenderTheme::defaultTheme().get())->paintWidget(type, context, rect, &state, 0);
}

bool ScrollbarThemeClutter::shouldCenterOnThumb(ScrollbarThemeClient*, const PlatformMouseEvent&)
{
    notImplemented();
    return false;
}

}
