GtkAdjustmentWatcher.cpp [plain text]
#include "config.h"
#include "GtkAdjustmentWatcher.h"
#include "Frame.h"
#include "FrameView.h"
#include "Page.h"
#include "Scrollbar.h"
#include "webkitwebviewprivate.h"
#include <gtk/gtk.h>
using namespace WebCore;
namespace WebKit {
GtkAdjustmentWatcher::GtkAdjustmentWatcher(WebKitWebView* webView)
: m_webView(webView)
, m_handlingGtkAdjustmentChange(false)
, m_updateAdjustmentCallbackId(0)
{
}
GtkAdjustmentWatcher::~GtkAdjustmentWatcher()
{
if (m_updateAdjustmentCallbackId)
g_source_remove(m_updateAdjustmentCallbackId);
}
static void updateAdjustmentFromScrollbar(GtkAdjustment* adjustment, Scrollbar* scrollbar)
{
if (!adjustment)
return;
if (!scrollbar) {
gtk_adjustment_configure(adjustment, 0, 0, 0, 0, 0, 0); return;
}
gtk_adjustment_configure(adjustment, scrollbar->value(), 0, scrollbar->totalSize(),
scrollbar->lineStep(), scrollbar->pageStep(), scrollbar->visibleSize());
}
void GtkAdjustmentWatcher::updateAdjustmentsFromScrollbars()
{
if (m_handlingGtkAdjustmentChange)
return;
if (!core(m_webView) || !core(m_webView)->mainFrame())
return;
FrameView* frameView = core(m_webView)->mainFrame()->view();
updateAdjustmentFromScrollbar(m_horizontalAdjustment.get(), frameView->horizontalScrollbar());
updateAdjustmentFromScrollbar(m_verticalAdjustment.get(), frameView->verticalScrollbar());
m_updateAdjustmentCallbackId = 0;
}
static gboolean updateAdjustmentCallback(GtkAdjustmentWatcher* watcher)
{
watcher->updateAdjustmentsFromScrollbars();
return FALSE;
}
void GtkAdjustmentWatcher::updateAdjustmentsFromScrollbarsLater() const
{
if (m_updateAdjustmentCallbackId)
return;
m_updateAdjustmentCallbackId = g_timeout_add_full(G_PRIORITY_HIGH_IDLE + 10, 0,
reinterpret_cast<GSourceFunc>(updateAdjustmentCallback),
const_cast<void*>(static_cast<const void*>(this)), 0);
}
static void adjustmentValueChangedCallback(GtkAdjustment* adjustment, GtkAdjustmentWatcher* watcher)
{
watcher->adjustmentValueChanged(adjustment);
}
static void setAdjustment(GtkAdjustmentWatcher* watcher, GRefPtr<GtkAdjustment>& adjustmentMember, GtkAdjustment* newAdjustment)
{
if (adjustmentMember) {
g_signal_handlers_disconnect_by_func(adjustmentMember.get(),
reinterpret_cast<void*>(adjustmentValueChangedCallback), watcher);
}
adjustmentMember = newAdjustment;
if (newAdjustment)
g_signal_connect(newAdjustment, "value-changed", G_CALLBACK(adjustmentValueChangedCallback), watcher);
}
void GtkAdjustmentWatcher::setHorizontalAdjustment(GtkAdjustment* newAdjustment)
{
setAdjustment(this, m_horizontalAdjustment, newAdjustment);
}
void GtkAdjustmentWatcher::setVerticalAdjustment(GtkAdjustment* newAdjustment)
{
setAdjustment(this, m_verticalAdjustment, newAdjustment);
}
void GtkAdjustmentWatcher::adjustmentValueChanged(GtkAdjustment* adjustment)
{
FrameView* frameView = core(m_webView)->mainFrame()->view();
Scrollbar* scrollbar = (adjustment == m_horizontalAdjustment.get()) ?
frameView->horizontalScrollbar() : frameView->verticalScrollbar();
if (!scrollbar)
return;
int newValue = static_cast<int>(gtk_adjustment_get_value(adjustment));
if (newValue != scrollbar->value()) {
m_handlingGtkAdjustmentChange = true;
frameView->scrollToOffsetWithoutAnimation(scrollbar->orientation(), newValue);
m_handlingGtkAdjustmentChange = false;
}
}
}