move adjustment code from sc_adjust_window to adjust_view

This new function adjust_view is in charge of recomputing the scale
according to adjustment settings and trigger a render_all.

adjust_view contains the old sc_adjust_window code, slightly simplified
thanks to the availability of the document_get_viewport_size.

Then it is used by sc_adjust_window, document_open and the
cb_view_resized callback. Makes slightly more sense this way than
calling the shortcut sc_adjust_window directly.
This commit is contained in:
Abdo Roig-Maranges 2013-10-24 22:11:52 +02:00
parent 54b6f7336d
commit c4245600c9
4 changed files with 81 additions and 108 deletions

View file

@ -459,23 +459,19 @@ error_ret:
}
bool
cb_view_resized(GtkWidget* UNUSED(widget), GtkAllocation* allocation, zathura_t* zathura)
cb_view_resized(GtkWidget* UNUSED(widget), GtkAllocation* UNUSED(allocation), zathura_t* zathura)
{
if (zathura == NULL || zathura->document == NULL) {
return false;
}
static int height = -1;
static int width = -1;
/* adjust the scale according to settings. If nothing needs to be resized,
it does not trigger the resize event.
/* adjust only if the allocation changed */
if (width != allocation->width || height != allocation->height) {
girara_argument_t argument = { zathura_document_get_adjust_mode(zathura->document), NULL };
sc_adjust_window(zathura->ui.session, &argument, NULL, 0);
width = allocation->width;
height = allocation->height;
}
The right viewport size is already in the document object, due to a
previous call to adjustment_changed. We don't want to use the allocation in
here, because we would have to subtract scrollbars, etc. */
adjust_view(zathura);
return false;
}

View file

@ -102,101 +102,8 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
unsigned int pages_per_row = 1;
girara_setting_get(session, "pages-per-row", &pages_per_row);
unsigned int first_page_column = 1;
girara_setting_get(session, "first-page-column", &first_page_column);
int padding = 1;
girara_setting_get(zathura->ui.session, "page-padding", &padding);
if (zathura->ui.page_widget == NULL || zathura->document == NULL) {
goto error_ret;
}
zathura_document_set_adjust_mode(zathura->document, argument->n);
if (argument->n == ZATHURA_ADJUST_NONE) {
/* there is nothing todo */
goto error_ret;
}
/* get window size */
GtkAllocation allocation;
gtk_widget_get_allocation(session->gtk.view, &allocation);
unsigned int width = allocation.width;
unsigned int height = allocation.height;
/* scrollbar spacing */
gint spacing;
gtk_widget_style_get(session->gtk.view, "scrollbar_spacing", &spacing, NULL);
width -= spacing;
/* correct view size */
if (gtk_widget_get_visible(GTK_WIDGET(session->gtk.inputbar)) == true) {
gtk_widget_get_allocation(session->gtk.inputbar, &allocation);
height += allocation.height;
}
double scale = 1.0;
unsigned int cell_height = 0, cell_width = 0;
unsigned int document_height = 0, document_width = 0;
zathura_document_set_scale(zathura->document, scale);
zathura_document_get_cell_size(zathura->document, &cell_height, &cell_width);
zathura_document_get_document_size(zathura->document, &document_height, &document_width);
double page_ratio = (double)cell_height / (double)document_width;
double window_ratio = (double)height / (double)width;
if (argument->n == ZATHURA_ADJUST_WIDTH ||
(argument->n == ZATHURA_ADJUST_BESTFIT && page_ratio < window_ratio)) {
scale = (double)(width - (pages_per_row - 1) * padding) /
(double)(pages_per_row * cell_width);
zathura_document_set_scale(zathura->document, scale);
bool show_vscrollbar = false;
girara_setting_get(session, "show-v-scrollbar", &show_vscrollbar);
if (show_vscrollbar) {
/* If the document is taller than the view, there's a vertical
* scrollbar; we need to substract its width from the view's width. */
zathura_document_get_document_size(zathura->document, &document_height, &document_width);
if (height < document_height) {
GtkWidget* vscrollbar = gtk_scrolled_window_get_vscrollbar(
GTK_SCROLLED_WINDOW(session->gtk.view));
if (vscrollbar != NULL) {
int scroll_width;
#if (GTK_MAJOR_VERSION == 3)
gtk_widget_get_preferred_width(GTK_WIDGET(vscrollbar), NULL, &scroll_width);
#else
GtkRequisition requisition;
gtk_widget_get_requisition(vscrollbar, &requisition);
scroll_width = requisition.width;
#endif
if (0 < scroll_width && (unsigned int)scroll_width < width) {
width -= scroll_width;
scale = (double)(width - (pages_per_row - 1) * padding) /
(double)(pages_per_row * cell_width);
zathura_document_set_scale(zathura->document, scale);
}
}
}
}
}
else if (argument->n == ZATHURA_ADJUST_BESTFIT) {
scale = (double)height / (double)cell_height;
zathura_document_set_scale(zathura->document, scale);
}
else {
goto error_ret;
}
/* re-render all pages */
render_all(zathura);
error_ret:
adjust_view(zathura);
return false;
}

View file

@ -811,9 +811,9 @@ document_open(zathura_t* zathura, const char* path, const char* password,
g_free(file_uri);
/* adjust window */
girara_argument_t argument = { zathura_document_get_adjust_mode(document), NULL };
sc_adjust_window(zathura->ui.session, &argument, NULL, 0);
/* adjust_view and set position*/
adjust_view(zathura);
/* set position */
if (file_info.position_x != 0 || file_info.position_y != 0) {
@ -1271,6 +1271,69 @@ zathura_jumplist_hide_inputbar(zathura_t* zathura)
}
}
bool
adjust_view(zathura_t* zathura) {
g_return_val_if_fail(zathura != NULL, false);
if (zathura->ui.page_widget == NULL || zathura->document == NULL) {
goto error_ret;
}
zathura_adjust_mode_t adjust_mode = zathura_document_get_adjust_mode(zathura->document);
if (adjust_mode == ZATHURA_ADJUST_NONE) {
/* there is nothing todo */
goto error_ret;
}
unsigned int cell_height = 0, cell_width = 0;
unsigned int document_height = 0, document_width = 0;
unsigned int view_height = 0, view_width = 0;
zathura_document_get_cell_size(zathura->document, &cell_height, &cell_width);
zathura_document_get_document_size(zathura->document, &document_height, &document_width);
zathura_document_get_viewport_size(zathura->document, &view_height, &view_width);
double scale = zathura_document_get_scale(zathura->document);
if (view_height == 0 || view_width == 0 || cell_height == 0 || cell_width == 0) {
goto error_ret;
}
double page_ratio = (double)cell_height / (double)document_width;
double view_ratio = (double)view_height / (double)view_width;
double newscale = scale;
if (adjust_mode == ZATHURA_ADJUST_WIDTH ||
(adjust_mode == ZATHURA_ADJUST_BESTFIT && page_ratio < view_ratio)) {
newscale = scale * (double)view_width / (double)document_width;
} else if (adjust_mode == ZATHURA_ADJUST_BESTFIT) {
newscale = scale * (double)view_height / (double)cell_height;
} else {
goto error_ret;
}
/* save new scale and recompute cell size */
zathura_document_set_scale(zathura->document, newscale);
unsigned int new_cell_height = 0, new_cell_width = 0;
zathura_document_get_cell_size(zathura->document, &new_cell_height, &new_cell_width);
/* if the change in scale changes page cell dimensions by at least one pixel, render */
if (abs(new_cell_width - cell_width) > 1 ||
abs(new_cell_height - cell_height) > 1) {
render_all(zathura);
refresh_view(zathura);
/* otherwise set the old scale and leave */
} else {
zathura_document_set_scale(zathura->document, scale);
}
error_ret:
return false;
}
bool
zathura_jumplist_has_previous(zathura_t* zathura)
{

View file

@ -334,6 +334,13 @@ bool position_set(zathura_t* zathura, double position_x, double position_y);
*/
void refresh_view(zathura_t* zathura);
/**
* Recompute the scale according to settings
*
* @param zathura Zathura session
*/
bool adjust_view(zathura_t* zathura);
/**
* Builds the box structure to show the rendered pages
*