From 0398692705f1279319f9f8f1753b7cf6db2c0f55 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 29 Dec 2010 11:46:13 +0100 Subject: [PATCH] Detect which page should be rendered --- callbacks.c | 35 ++++++++++++++++++++++++++ callbacks.h | 1 + ft/document.c | 6 +++++ ft/document.h | 2 ++ utils.c | 15 ++++++++++-- utils.h | 2 +- zathura.c | 68 +++++++++++++++++++++++++++++++-------------------- zathura.h | 1 + 8 files changed, 101 insertions(+), 29 deletions(-) diff --git a/callbacks.c b/callbacks.c index 07c0f5e..28f1979 100644 --- a/callbacks.c +++ b/callbacks.c @@ -3,8 +3,10 @@ #include #include #include +#include #include "zathura.h" +#include "ft/document.h" gboolean cb_destroy(GtkWidget* widget, gpointer data) @@ -30,3 +32,36 @@ buffer_changed(girara_session_t* session) girara_statusbar_item_set_text(session, Zathura.UI.statusbar.buffer, ""); } } + +void +cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data) +{ + if(!Zathura.document || !Zathura.document->pages || !Zathura.UI.page_view) { + return; + } + + /* get current adjustment values */ + gdouble lower = gtk_adjustment_get_lower(adjustment); + gdouble upper = gtk_adjustment_get_lower(adjustment); + + /* 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]; + double begin = page->offset; + double end = page->offset + page->height; + + if( begin <= 10 ) { // FIXME + /* render page */ + GtkWidget* image = zathura_page_render(page); + if(!image) { + printf("error: rendering failed\n"); + return; + } + + /* add new page */ + gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE, TRUE, 0); + gtk_box_reorder_child(GTK_BOX(Zathura.UI.page_view), image, page->number); + } + } +} diff --git a/callbacks.h b/callbacks.h index 254d7e8..2e4d575 100644 --- a/callbacks.h +++ b/callbacks.h @@ -8,5 +8,6 @@ gboolean cb_destroy(GtkWidget* widget, gpointer data); void buffer_changed(girara_session_t* session); +void cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data); #endif // CALLBACKS_H diff --git a/ft/document.c b/ft/document.c index 616b32e..fcf9a86 100644 --- a/ft/document.c +++ b/ft/document.c @@ -102,6 +102,7 @@ zathura_document_open(const char* path, const char* password) goto error_free; } + double offset = 0; for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { zathura_page_t* page = zathura_page_get(document, page_id); @@ -109,6 +110,11 @@ zathura_document_open(const char* path, const char* password) goto error_free; } + page->offset = offset; + page->number = page_id; + + offset += page->height; + document->pages[page_id] = page; } diff --git a/ft/document.h b/ft/document.h index ffc86a7..6c581be 100644 --- a/ft/document.h +++ b/ft/document.h @@ -77,6 +77,8 @@ typedef struct zathura_page_s { double height; double width; + double offset; + unsigned int number; zathura_document_t* document; void* data; } zathura_page_t; diff --git a/utils.c b/utils.c index 6a0012a..80cb3a5 100644 --- a/utils.c +++ b/utils.c @@ -121,7 +121,7 @@ execute_command(char* const argv[], char** output) return true; } -GdkPixbuf* +GtkWidget* page_blank(unsigned int width, unsigned int height) { if((width == 0) || (height == 0)) { @@ -146,5 +146,16 @@ page_blank(unsigned int width, unsigned int height) return NULL; } - return pixbuf; + /* convert to image */ + GtkWidget* image = gtk_image_new(); + if(!image) { + free(buffer); + g_object_unref(pixbuf); + return false; + } + + gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); + gtk_widget_show(image); + + return image; } diff --git a/utils.h b/utils.h index f70b962..0331196 100644 --- a/utils.h +++ b/utils.h @@ -9,6 +9,6 @@ bool file_exists(const char* path); const char* file_get_extension(const char* path); bool execute_command(char* const argv[], char** output); -GdkPixbuf* page_blank(unsigned int width, unsigned int height); +GtkWidget* page_blank(unsigned int width, unsigned int height); #endif // UTILS_H diff --git a/zathura.c b/zathura.c index 60638e5..b1b61df 100644 --- a/zathura.c +++ b/zathura.c @@ -26,6 +26,16 @@ init_zathura() Zathura.UI.statusbar.buffer = NULL; Zathura.UI.statusbar.page_number = NULL; Zathura.UI.drawing_area = NULL; + Zathura.UI.page_view = NULL; + + /* page view */ + Zathura.UI.page_view = gtk_vbox_new(FALSE, 0); + if(!Zathura.UI.page_view) { + goto error_free; + } + + gtk_widget_show(Zathura.UI.page_view); + gtk_box_set_spacing(GTK_BOX(Zathura.UI.page_view), 0); /* statusbar */ Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL); @@ -48,6 +58,9 @@ init_zathura() /* signals */ g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL); + GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view)); + g_signal_connect(G_OBJECT(view_vadjustment), "value-changed", G_CALLBACK(cb_view_vadjustment_value_changed), NULL); + /* girara events */ Zathura.UI.session->events.buffer_changed = buffer_changed; @@ -62,6 +75,10 @@ error_free: g_object_unref(Zathura.UI.drawing_area); } + if(Zathura.UI.page_view) { + g_object_unref(Zathura.UI.page_view); + } + girara_session_destroy(Zathura.UI.session); error_out: @@ -73,43 +90,47 @@ bool document_open(const char* path, const char* password) { if(!path) { - return false; + goto error_out; } zathura_document_t* document = zathura_document_open(path, password); if(!document) { - return false; + goto error_out; } Zathura.document = document; - GtkWidget* page_view = gtk_vbox_new(FALSE, 0); - gtk_box_set_spacing(GTK_BOX(page_view), 0); - + /* create blank pages */ for(unsigned int i = 0; i < document->number_of_pages; i++) { - GdkPixbuf* pixbuf = page_blank(document->pages[i]->width, document->pages[i]->height); - if(!pixbuf) { - return false; - } + /* create blank page */ + GtkWidget* image = page_blank(document->pages[i]->width, document->pages[i]->height); - GtkWidget* image = gtk_image_new(); if(!image) { - g_object_unref(pixbuf); - return false; + goto error_free; } - gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); - gtk_widget_show(image); - gtk_box_pack_start(GTK_BOX(page_view), image, TRUE, TRUE, 0); + /* pack to page view */ + gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE, TRUE, 0); } - gtk_widget_show(page_view); + girara_set_view(Zathura.UI.session, Zathura.UI.page_view); - girara_set_view(Zathura.UI.session, page_view); + /* first page */ + if(!page_set(0)) { + goto error_free; + } return true; + +error_free: + + zathura_document_free(document); + +error_out: + + return false; } bool @@ -147,7 +168,7 @@ error_out: bool page_set(unsigned int page_id) { - if(!Zathura.document) { + if(!Zathura.document || !Zathura.document->pages) { goto error_out; } @@ -156,19 +177,14 @@ page_set(unsigned int page_id) } /* render page */ - zathura_page_t* page = zathura_page_get(Zathura.document, page_id); + zathura_page_t* page = Zathura.document->pages[page_id]; if(!page) { goto error_out; } - if(!page_render(page)) { - zathura_page_free(page); - fprintf(stderr, "error: rendering failed\n"); - goto error_out; - } - - zathura_page_free(page); + GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view)); + gtk_adjustment_set_value(view_vadjustment, page->offset); /* update page number */ Zathura.document->current_page_number = page_id; diff --git a/zathura.h b/zathura.h index cc15c33..b5d781a 100644 --- a/zathura.h +++ b/zathura.h @@ -37,6 +37,7 @@ struct } statusbar; GtkWidget *drawing_area; + GtkWidget *page_view; } UI; zathura_document_t* document; /**> The current document */