From 9d13c75153b88be90b227c98d809b25db179805d Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sat, 1 Oct 2011 09:40:44 +0200 Subject: [PATCH] Fit page to width / bestfit --- shortcuts.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/shortcuts.c b/shortcuts.c index 3edf917..a60d6d5 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -22,10 +22,64 @@ sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument), } bool -sc_adjust_window(girara_session_t* session, girara_argument_t* UNUSED(argument), +sc_adjust_window(girara_session_t* session, girara_argument_t* argument, unsigned int UNUSED(t)) { g_return_val_if_fail(session != NULL, false); + g_return_val_if_fail(session->global.data != NULL, false); + zathura_t* zathura = session->global.data; + g_return_val_if_fail(argument != NULL, false); + + unsigned int* pages_per_row = girara_setting_get(session, "pages-per-row"); + + if (zathura->ui.page_view == NULL || zathura->document == NULL || pages_per_row == NULL) { + goto error_ret; + } + + /* get window size */ + /* TODO: Get correct size of the view widget */ + gint width; + gint height; + gtk_window_get_size(GTK_WINDOW(session->gtk.window), &width, &height); + + /* calculate total width and max-height */ + double total_width = 0; + double max_height = 0; + + for (unsigned int page_id = 0; page_id < *pages_per_row; page_id++) { + if (page_id == zathura->document->number_of_pages) { + break; + } + + zathura_page_t* page = zathura->document->pages[page_id]; + if (page == NULL) { + goto error_free; + } + + if (page->height > max_height) { + max_height = page->height; + } + + total_width += page->width; + } + + if (argument->n == ADJUST_WIDTH) { + zathura->document->scale = width / total_width; + } else if (argument->n == ADJUST_BESTFIT) { + zathura->document->scale = height / max_height; + } else { + goto error_free; + } + + /* re-render all pages */ + render_all(zathura); + +error_free: + + /* cleanup */ + free(pages_per_row); + +error_ret: return false; }