mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2024-12-29 15:45:59 +01:00
Detect which page should be rendered
This commit is contained in:
parent
3f961f6c18
commit
0398692705
8 changed files with 101 additions and 29 deletions
35
callbacks.c
35
callbacks.c
|
@ -3,8 +3,10 @@
|
|||
#include <callbacks.h>
|
||||
#include <girara.h>
|
||||
#include <stdlib.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
15
utils.c
15
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;
|
||||
}
|
||||
|
|
2
utils.h
2
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
|
||||
|
|
68
zathura.c
68
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;
|
||||
|
|
|
@ -37,6 +37,7 @@ struct
|
|||
} statusbar;
|
||||
|
||||
GtkWidget *drawing_area;
|
||||
GtkWidget *page_view;
|
||||
} UI;
|
||||
|
||||
zathura_document_t* document; /**> The current document */
|
||||
|
|
Loading…
Reference in a new issue