calculate visible pages and update current page number

This commit is contained in:
Sebastian Ramacher 2012-02-07 14:56:58 +01:00
parent 01bdd58104
commit 57291989c5
6 changed files with 85 additions and 42 deletions

View File

@ -40,41 +40,61 @@ buffer_changed(girara_session_t* session)
}
void
cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpointer data)
{
zathura_t* zathura = data;
if (!zathura || !zathura->document || !zathura->document->pages || !zathura->ui.page_view) {
return;
}
/* get current adjustment values */
gdouble lower = gtk_adjustment_get_value(adjustment);
gdouble upper = lower + gtk_adjustment_get_page_size(adjustment);
GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
GdkRectangle view_rect;
/* get current adjustment values */
view_rect.y = gtk_adjustment_get_value(view_vadjustment);
view_rect.height = gtk_adjustment_get_page_size(view_vadjustment);
view_rect.x = gtk_adjustment_get_value(view_hadjustment);
view_rect.width = gtk_adjustment_get_page_size(view_hadjustment);
int page_padding = 1;
girara_setting_get(zathura->ui.session, "page-padding", &page_padding);
GdkRectangle center;
center.x = view_rect.x + (view_rect.width + 1) / 2;
center.y = view_rect.y + (view_rect.height + 1) / 2;
center.height = center.width = 2*page_padding + 1;
bool updated = false;
/* find page that fits */
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++)
{
zathura_page_t* page = zathura->document->pages[page_id];
page_offset_t* offset = page_calculate_offset(page);
if (offset == NULL) {
continue;
}
page_offset_t offset;
page_calculate_offset(page, &offset);
double begin = offset->y;
double end = offset->y + page->height * zathura->document->scale;
GdkRectangle page_rect;
page_rect.x = offset.x;
page_rect.y = offset.y;
page_rect.width = page->width * zathura->document->scale;
page_rect.height = page->height * zathura->document->scale;
if ( ( (begin >= lower) && (end <= upper) ) /* [> page is in viewport <]*/
|| ( (begin <= lower) && (end >= lower) && (end <= upper) ) /* [> end of the page is in viewport <] */
|| ( (begin >= lower) && (end >= upper) && (begin <= upper) ) /* [> begin of the page is in viewport <] */
) {
if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) {
page->visible = true;
girara_info("page %d visible", page_id);
if (updated == false && gdk_rectangle_intersect(&center, &page_rect, NULL) == TRUE) {
zathura->document->current_page_number = page_id;
updated = true;
}
} else {
page->visible = false;
girara_info("page %d not visible", page_id);
}
free(offset);
}
statusbar_page_number_update(zathura);
}
void

View File

@ -29,6 +29,16 @@ static gboolean zathura_page_view_expose(GtkWidget* widget, GdkEventExpose* even
static void zathura_page_view_finalize(GObject* object);
static void zathura_page_view_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
static void zathura_page_view_size_allocate(GtkWidget* widget, GdkRectangle* allocation);
static void zathura_page_view_grab_focus(GtkWidget* widget);
static gboolean zathura_page_view_focus_in(GtkWidget *widget, GtkDirectionType GIRARA_UNUSED(event))
{
ZathuraPageView* view = ZATHURA_PAGE_VIEW(widget);
zathura_page_view_private_t* priv = ZATHURA_PAGE_VIEW_GET_PRIVATE(view);
girara_info("lala: %d", priv->page->number);
return false;
}
enum properties_e
{
@ -46,6 +56,8 @@ zathura_page_view_class_init(ZathuraPageViewClass* class)
GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(class);
widget_class->expose_event = zathura_page_view_expose;
widget_class->size_allocate = zathura_page_view_size_allocate;
widget_class->grab_focus = zathura_page_view_grab_focus;
widget_class->focus = zathura_page_view_focus_in;
GObjectClass* object_class = G_OBJECT_CLASS(class);
object_class->finalize = zathura_page_view_finalize;
@ -67,7 +79,9 @@ zathura_page_view_init(ZathuraPageView* widget)
/* we want mouse events */
gtk_widget_add_events(GTK_WIDGET(widget),
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK);
/* widgets can focus */
gtk_widget_set_can_focus(GTK_WIDGET(widget), TRUE);
}
GtkWidget*
@ -286,3 +300,12 @@ zathura_page_view_clear_rectangles(ZathuraPageView* widget)
GIRARA_LIST_FOREACH_END(priv->rectangles, pv_rect_t*, iter, rect);
girara_list_clear(priv->rectangles);
}
static void
zathura_page_view_grab_focus(GtkWidget* widget)
{
ZathuraPageView* view = ZATHURA_PAGE_VIEW(widget);
zathura_page_view_private_t* priv = ZATHURA_PAGE_VIEW_GET_PRIVATE(view);
girara_info("lala: %d", priv->page->number);
}

View File

@ -4,6 +4,7 @@
#include <girara/settings.h>
#include <girara/datastructures.h>
#include <girara/shortcuts.h>
#include <girara/utils.h>
#include <gtk/gtk.h>
#include "callbacks.h"
@ -18,7 +19,20 @@ bool
sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument),
girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
girara_info("in sc_abort");
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
zathura_page_t* page = zathura->document->pages[page_id];
if (page == NULL) {
continue;
}
zathura_page_view_clear_rectangles(ZATHURA_PAGE_VIEW(page->drawing_area));
}
girara_mode_set(session, session->modes.normal);
@ -314,6 +328,8 @@ sc_search(girara_session_t* session, girara_argument_t* argument,
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
girara_info("in sc_search");
return false;
}

22
utils.c
View File

@ -178,28 +178,14 @@ document_index_build(GtkTreeModel* model, GtkTreeIter* parent,
GIRARA_LIST_FOREACH_END(list, gchar*, iter, name);
}
page_offset_t*
page_calculate_offset(zathura_page_t* page)
void
page_calculate_offset(zathura_page_t* page, page_offset_t* offset)
{
if (page == NULL || page->document == NULL || page->document->zathura == NULL) {
return NULL;
}
page_offset_t* offset = malloc(sizeof(page_offset_t));
if (offset == NULL) {
return NULL;
}
g_return_if_fail(page != NULL && page->document != NULL && page->document->zathura != NULL && offset != NULL);
zathura_document_t* document = page->document;
zathura_t* zathura = document->zathura;
if (gtk_widget_translate_coordinates(page->drawing_area, zathura->ui.page_view, 0, 0, &(offset->x), &(offset->y)) == false) {
free(offset);
return NULL;
}
return offset;
g_return_if_fail(gtk_widget_translate_coordinates(page->drawing_area, zathura->ui.page_view, 0, 0, &(offset->x), &(offset->y)) == true);
}
zathura_rectangle_t

View File

@ -69,7 +69,7 @@ void document_index_build(GtkTreeModel* model, GtkTreeIter* parent, girara_tree_
* @param page The Page
* @return The calculated offset or NULL if an error occured
*/
page_offset_t* page_calculate_offset(zathura_page_t* page);
void page_calculate_offset(zathura_page_t* page, page_offset_t* offset);
/**
* Calculates the new coordinates based on the rotation and scale level of the

View File

@ -568,15 +568,13 @@ page_set(zathura_t* zathura, unsigned int page_id)
goto error_out;
}
page_offset_t* offset = page_calculate_offset(page);
if (offset == NULL) {
goto error_out;
}
page_offset_t offset;
page_calculate_offset(page, &offset);
GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
gtk_adjustment_set_value(view_hadjustment, offset->x);
gtk_adjustment_set_value(view_vadjustment, offset->y);
gtk_adjustment_set_value(view_hadjustment, offset.x);
gtk_adjustment_set_value(view_vadjustment, offset.y);
/* update page number */
zathura->document->current_page_number = page_id;