#include "config.h"
#include "ScrollView.h"
#include "ChromeClient.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HostWindow.h"
#include "IntRect.h"
#include "Page.h"
#include "PlatformMouseEvent.h"
#include "PlatformWheelEvent.h"
#include "ScrollbarGtk.h"
#include "ScrollbarTheme.h"
#include <gtk/gtk.h>
using namespace std;
namespace WebCore {
void ScrollView::platformInit()
{
m_horizontalAdjustment = 0;
m_verticalAdjustment = 0;
}
void ScrollView::platformDestroy()
{
m_horizontalAdjustment = 0;
m_verticalAdjustment = 0;
}
PassRefPtr<Scrollbar> ScrollView::createScrollbar(ScrollbarOrientation orientation)
{
if (orientation == HorizontalScrollbar && m_horizontalAdjustment)
return ScrollbarGtk::createScrollbar(this, orientation, m_horizontalAdjustment);
else if (orientation == VerticalScrollbar && m_verticalAdjustment)
return ScrollbarGtk::createScrollbar(this, orientation, m_verticalAdjustment);
else
return Scrollbar::createNativeScrollbar(this, orientation, RegularScrollbar);
}
void ScrollView::setGtkAdjustments(GtkAdjustment* hadj, GtkAdjustment* vadj, bool resetValues)
{
ASSERT(!hadj == !vadj);
m_horizontalAdjustment = hadj;
m_verticalAdjustment = vadj;
if (m_horizontalAdjustment) {
ScrollbarGtk* hScrollbar = reinterpret_cast<ScrollbarGtk*>(horizontalScrollbar());
if (hScrollbar)
hScrollbar->attachAdjustment(m_horizontalAdjustment);
ScrollbarGtk* vScrollbar = reinterpret_cast<ScrollbarGtk*>(verticalScrollbar());
if (vScrollbar)
vScrollbar->attachAdjustment(m_verticalAdjustment);
int horizontalPageStep = max(max<int>(frameRect().width() * Scrollbar::minFractionToStepWhenPaging(), frameRect().width() - Scrollbar::maxOverlapBetweenPages()), 1);
gtk_adjustment_configure(m_horizontalAdjustment,
resetValues ? 0 : scrollOffset().width(), 0,
resetValues ? 0 : contentsSize().width(),
resetValues ? 0 : Scrollbar::pixelsPerLineStep(),
resetValues ? 0 : horizontalPageStep,
resetValues ? 0 : frameRect().width());
int verticalPageStep = max(max<int>(frameRect().height() * Scrollbar::minFractionToStepWhenPaging(), frameRect().height() - Scrollbar::maxOverlapBetweenPages()), 1);
gtk_adjustment_configure(m_verticalAdjustment,
resetValues ? 0 : scrollOffset().height(), 0,
resetValues ? 0 : contentsSize().height(),
resetValues ? 0 : Scrollbar::pixelsPerLineStep(),
resetValues ? 0 : verticalPageStep,
resetValues ? 0 : frameRect().height());
} else {
ScrollbarGtk* hScrollbar = reinterpret_cast<ScrollbarGtk*>(horizontalScrollbar());
if (hScrollbar)
hScrollbar->detachAdjustment();
ScrollbarGtk* vScrollbar = reinterpret_cast<ScrollbarGtk*>(verticalScrollbar());
if (vScrollbar)
vScrollbar->detachAdjustment();
}
setHasVerticalScrollbar(false);
setHasHorizontalScrollbar(false);
}
void ScrollView::platformAddChild(Widget* child)
{
if (!GTK_IS_SOCKET(child->platformWidget()))
gtk_container_add(GTK_CONTAINER(hostWindow()->platformPageClient()), child->platformWidget());
}
void ScrollView::platformRemoveChild(Widget* child)
{
GtkWidget* parent;
if (hostWindow())
parent = GTK_WIDGET(hostWindow()->platformPageClient());
else
parent = GTK_WIDGET(child->platformWidget()->parent);
if (GTK_IS_CONTAINER(parent) && parent == child->platformWidget()->parent)
gtk_container_remove(GTK_CONTAINER(parent), child->platformWidget());
}
IntRect ScrollView::visibleContentRect(bool includeScrollbars) const
{
if (!m_horizontalAdjustment)
return IntRect(IntPoint(m_scrollOffset.width(), m_scrollOffset.height()),
IntSize(max(0, width() - (verticalScrollbar() && !includeScrollbars ? verticalScrollbar()->width() : 0)),
max(0, height() - (horizontalScrollbar() && !includeScrollbars ? horizontalScrollbar()->height() : 0))));
GtkWidget* measuredWidget = hostWindow()->platformPageClient();
GtkWidget* parent = gtk_widget_get_parent(measuredWidget);
if (parent && includeScrollbars)
measuredWidget = parent;
return IntRect(IntPoint(m_scrollOffset.width(), m_scrollOffset.height()),
IntSize(measuredWidget->allocation.width,
measuredWidget->allocation.height));
}
void ScrollView::setScrollbarModes(ScrollbarMode horizontalMode, ScrollbarMode verticalMode, bool, bool)
{
if (horizontalMode == m_horizontalScrollbarMode && verticalMode == m_verticalScrollbarMode)
return;
m_horizontalScrollbarMode = horizontalMode;
m_verticalScrollbarMode = verticalMode;
if (!m_horizontalAdjustment) {
updateScrollbars(scrollOffset());
return;
}
if (!isFrameView())
return;
if (hostWindow())
hostWindow()->scrollbarsModeDidChange();
}
}