Commit 03986927 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Detect which page should be rendered

parent 3f961f6c
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
#include <callbacks.h> #include <callbacks.h>
#include <girara.h> #include <girara.h>
#include <stdlib.h> #include <stdlib.h>
#include <gtk/gtk.h>
#include "zathura.h" #include "zathura.h"
#include "ft/document.h"
gboolean gboolean
cb_destroy(GtkWidget* widget, gpointer data) cb_destroy(GtkWidget* widget, gpointer data)
...@@ -30,3 +32,36 @@ buffer_changed(girara_session_t* session) ...@@ -30,3 +32,36 @@ buffer_changed(girara_session_t* session)
girara_statusbar_item_set_text(session, Zathura.UI.statusbar.buffer, ""); 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);
}
}
}
...@@ -8,5 +8,6 @@ ...@@ -8,5 +8,6 @@
gboolean cb_destroy(GtkWidget* widget, gpointer data); gboolean cb_destroy(GtkWidget* widget, gpointer data);
void buffer_changed(girara_session_t* session); void buffer_changed(girara_session_t* session);
void cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data);
#endif // CALLBACKS_H #endif // CALLBACKS_H
...@@ -102,6 +102,7 @@ zathura_document_open(const char* path, const char* password) ...@@ -102,6 +102,7 @@ zathura_document_open(const char* path, const char* password)
goto error_free; goto error_free;
} }
double offset = 0;
for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) for(unsigned int page_id = 0; page_id < document->number_of_pages; page_id++)
{ {
zathura_page_t* page = zathura_page_get(document, 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) ...@@ -109,6 +110,11 @@ zathura_document_open(const char* path, const char* password)
goto error_free; goto error_free;
} }
page->offset = offset;
page->number = page_id;
offset += page->height;
document->pages[page_id] = page; document->pages[page_id] = page;
} }
......
...@@ -77,6 +77,8 @@ typedef struct zathura_page_s ...@@ -77,6 +77,8 @@ typedef struct zathura_page_s
{ {
double height; double height;
double width; double width;
double offset;
unsigned int number;
zathura_document_t* document; zathura_document_t* document;
void* data; void* data;
} zathura_page_t; } zathura_page_t;
......
...@@ -121,7 +121,7 @@ execute_command(char* const argv[], char** output) ...@@ -121,7 +121,7 @@ execute_command(char* const argv[], char** output)
return true; return true;
} }
GdkPixbuf* GtkWidget*
page_blank(unsigned int width, unsigned int height) page_blank(unsigned int width, unsigned int height)
{ {
if((width == 0) || (height == 0)) { if((width == 0) || (height == 0)) {
...@@ -146,5 +146,16 @@ page_blank(unsigned int width, unsigned int height) ...@@ -146,5 +146,16 @@ page_blank(unsigned int width, unsigned int height)
return NULL; 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;
} }
...@@ -9,6 +9,6 @@ ...@@ -9,6 +9,6 @@
bool file_exists(const char* path); bool file_exists(const char* path);
const char* file_get_extension(const char* path); const char* file_get_extension(const char* path);
bool execute_command(char* const argv[], char** output); 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 #endif // UTILS_H
...@@ -26,6 +26,16 @@ init_zathura() ...@@ -26,6 +26,16 @@ init_zathura()
Zathura.UI.statusbar.buffer = NULL; Zathura.UI.statusbar.buffer = NULL;
Zathura.UI.statusbar.page_number = NULL; Zathura.UI.statusbar.page_number = NULL;
Zathura.UI.drawing_area = 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 */ /* statusbar */
Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL); Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL);
...@@ -48,6 +58,9 @@ init_zathura() ...@@ -48,6 +58,9 @@ init_zathura()
/* signals */ /* signals */
g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL); 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 */ /* girara events */
Zathura.UI.session->events.buffer_changed = buffer_changed; Zathura.UI.session->events.buffer_changed = buffer_changed;
...@@ -62,6 +75,10 @@ error_free: ...@@ -62,6 +75,10 @@ error_free:
g_object_unref(Zathura.UI.drawing_area); 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); girara_session_destroy(Zathura.UI.session);
error_out: error_out:
...@@ -73,43 +90,47 @@ bool ...@@ -73,43 +90,47 @@ bool
document_open(const char* path, const char* password) document_open(const char* path, const char* password)
{ {
if(!path) { if(!path) {
return false; goto error_out;
} }
zathura_document_t* document = zathura_document_open(path, password); zathura_document_t* document = zathura_document_open(path, password);
if(!document) { if(!document) {
return false; goto error_out;
} }
Zathura.document = document; Zathura.document = document;
GtkWidget* page_view = gtk_vbox_new(FALSE, 0); /* create blank pages */
gtk_box_set_spacing(GTK_BOX(page_view), 0);
for(unsigned int i = 0; i < document->number_of_pages; i++) for(unsigned int i = 0; i < document->number_of_pages; i++)
{ {
GdkPixbuf* pixbuf = page_blank(document->pages[i]->width, document->pages[i]->height); /* create blank page */
if(!pixbuf) { GtkWidget* image = page_blank(document->pages[i]->width, document->pages[i]->height);
return false;
}
GtkWidget* image = gtk_image_new();
if(!image) { if(!image) {
g_object_unref(pixbuf); goto error_free;
return false;
} }
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); /* pack to page view */
gtk_widget_show(image); gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(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; return true;
error_free:
zathura_document_free(document);
error_out:
return false;
} }
bool bool
...@@ -147,7 +168,7 @@ error_out: ...@@ -147,7 +168,7 @@ error_out:
bool bool
page_set(unsigned int page_id) page_set(unsigned int page_id)
{ {
if(!Zathura.document) { if(!Zathura.document || !Zathura.document->pages) {
goto error_out; goto error_out;
} }
...@@ -156,19 +177,14 @@ page_set(unsigned int page_id) ...@@ -156,19 +177,14 @@ page_set(unsigned int page_id)
} }
/* render page */ /* render page */
zathura_page_t* page = zathura_page_get(Zathura.document, page_id); zathura_page_t* page = Zathura.document->pages[page_id];
if(!page) { if(!page) {
goto error_out; goto error_out;
} }
if(!page_render(page)) { GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
zathura_page_free(page); gtk_adjustment_set_value(view_vadjustment, page->offset);
fprintf(stderr, "error: rendering failed\n");
goto error_out;
}
zathura_page_free(page);
/* update page number */ /* update page number */
Zathura.document->current_page_number = page_id; Zathura.document->current_page_number = page_id;
......
...@@ -37,6 +37,7 @@ struct ...@@ -37,6 +37,7 @@ struct
} statusbar; } statusbar;
GtkWidget *drawing_area; GtkWidget *drawing_area;
GtkWidget *page_view;
} UI; } UI;
zathura_document_t* document; /**> The current document */ zathura_document_t* document; /**> The current document */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment