zathura/zathura.c

174 lines
3.4 KiB
C
Raw Normal View History

/* See LICENSE file for license and copyright information */
2010-11-12 13:48:18 +01:00
#include "callbacks.h"
#include "config.h"
2010-11-18 02:35:33 +01:00
#include "ft/document.h"
2010-11-12 13:48:18 +01:00
#include "shortcuts.h"
2010-11-10 19:18:01 +01:00
#include "zathura.h"
2009-12-26 17:57:46 +01:00
2010-11-10 19:18:01 +01:00
/* function implementation */
bool
init_zathura()
2010-06-03 18:05:34 +02:00
{
/* UI */
2010-11-17 22:51:15 +01:00
if(!(Zathura.UI.session = girara_session_create())) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-11-17 22:51:15 +01:00
}
2010-06-03 18:05:34 +02:00
2010-11-17 22:51:15 +01:00
if(!girara_session_init(Zathura.UI.session)) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-11-17 22:51:15 +01:00
}
2010-06-03 18:05:34 +02:00
2010-12-26 01:52:17 +01:00
Zathura.UI.statusbar.file = NULL;
Zathura.UI.statusbar.buffer = NULL;
Zathura.UI.statusbar.page_number = NULL;
Zathura.UI.drawing_area = NULL;
/* statusbar */
2010-11-18 21:22:43 +01:00
Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL);
if(!Zathura.UI.statusbar.file) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-13 12:40:48 +01:00
}
2010-11-18 21:22:43 +01:00
Zathura.UI.statusbar.buffer = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
if(!Zathura.UI.statusbar.buffer) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-18 21:22:43 +01:00
}
Zathura.UI.statusbar.page_number = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
if(!Zathura.UI.statusbar.page_number) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-18 21:22:43 +01:00
}
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, "[No Name]");
2010-11-12 13:48:18 +01:00
/* signals */
g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL);
2010-11-13 12:40:48 +01:00
/* girara events */
Zathura.UI.session->events.buffer_changed = buffer_changed;
2010-11-12 13:48:18 +01:00
/* configuration */
config_load_default();
2010-11-10 19:18:01 +01:00
return true;
2010-12-26 01:52:17 +01:00
error_free:
if(Zathura.UI.drawing_area) {
g_object_unref(Zathura.UI.drawing_area);
}
girara_session_destroy(Zathura.UI.session);
error_out:
return false;
2010-06-03 18:05:34 +02:00
}
2010-12-12 22:04:42 +01:00
bool
document_open(const char* path, const char* password)
{
if(!path) {
return false;
}
zathura_document_t* document = zathura_document_open(path, password);
if(!document) {
return false;
}
Zathura.document = document;
if(!page_set(0)) {
2010-12-23 19:41:07 +01:00
zathura_document_free(document);
2010-12-12 22:04:42 +01:00
return false;
}
return true;
}
bool
document_close()
{
if(!Zathura.document) {
return false;
}
zathura_document_free(Zathura.document);
return true;
}
bool
page_set(unsigned int page_id)
{
if(!Zathura.document) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
if(page_id >= Zathura.document->number_of_pages) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
/* render page */
zathura_page_t* page = zathura_page_get(Zathura.document, page_id);
if(!page) {
goto error_out;
}
GtkWidget* image = zathura_page_render(page);
if(!image) {
zathura_page_free(page);
fprintf(stderr, "error: rendering failed\n");
goto error_out;
}
zathura_page_free(page);
/* draw new rendered page */
if(!girara_set_view(Zathura.UI.session, image)) {
goto error_out;
}
/* update page number */
2010-12-12 22:04:42 +01:00
Zathura.document->current_page_number = page_id;
char* page_number = g_strdup_printf("[%d/%d]", page_id + 1, Zathura.document->number_of_pages);
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.page_number, page_number);
g_free(page_number);
return true;
2010-12-26 01:52:17 +01:00
error_out:
return false;
2010-12-12 22:04:42 +01:00
}
2009-08-11 23:18:50 +02:00
/* main function */
int main(int argc, char* argv[])
2009-08-11 23:18:50 +02:00
{
g_thread_init(NULL);
gdk_threads_init();
2009-08-11 23:18:50 +02:00
gtk_init(&argc, &argv);
2010-11-18 14:51:13 +01:00
if(!init_zathura()) {
printf("error: coult not initialize zathura\n");
return -1;
}
2010-11-18 02:35:33 +01:00
if(argc > 1) {
2010-12-12 22:04:42 +01:00
if(!document_open(argv[1], NULL)) {
printf("error: could not open document\n");
2010-11-18 02:35:33 +01:00
return -1;
}
}
2010-01-01 13:54:21 +01:00
2010-11-18 14:51:13 +01:00
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
2009-08-11 23:18:50 +02:00
return 0;
}