Compare commits

...

3 Commits

Author SHA1 Message Date
ad29013e44
Add zooming to fit width/height if larger
Also, mildly refactor get_surface_dimensions().
2021-12-21 08:13:31 +01:00
33851295d8
Bind M-Home to going to the home directory 2021-12-21 07:20:06 +01:00
46f90f2f35
Improve the "Save as" dialog, clean up 2021-12-21 06:31:52 +01:00
2 changed files with 90 additions and 50 deletions

View File

@ -94,6 +94,7 @@ static struct key_section help_keys[] = {
{"<alt>Left", "Go back in history"},
{"<alt>Right", "Go forward in history"},
{"<alt>Up", "Go to parent directory"},
{"<alt>Home", "Go home"},
{"F5 r <control>r", "Refresh"},
{}
}},
@ -118,6 +119,8 @@ static struct key_section help_keys[] = {
{"1...9", "Set zoom to N:1"},
{"plus <control>plus", "Zoom in"},
{"minus <control>minus", "Zoom out"},
{"w", "Zoom to fit width if larger"},
{"h", "Zoom to fit height if larger"},
{}
}},
{"Orientation", (struct key[]) {
@ -126,6 +129,12 @@ static struct key_section help_keys[] = {
{"greater", "Rotate clockwise"},
{}
}},
{"Configuration", (struct key[]) {
{"x", "Toggle scale to fit if larger"},
{"i", "Toggle smooth scaling"},
{"t", "Toggle transparency highlighting"},
{}
}},
{"Control", (struct key[]) {
{"bracketleft", "Previous page"},
{"bracketright", "Next page"},
@ -134,12 +143,6 @@ static struct key_section help_keys[] = {
{"space", "Toggle playback"},
{}
}},
{"Configuration", (struct key[]) {
{"x", "Toggle scale to fit"},
{"i", "Toggle smooth scaling"},
{"t", "Toggle transparency highlighting"},
{}
}},
{"Tools", (struct key[]) {
{"<control>p", "Print..."},
{"<control>s", "Save page as..."},
@ -772,6 +775,9 @@ on_key_press(G_GNUC_UNUSED GtkWidget *widget, GdkEventKey *event,
g_free(parent);
}
return TRUE;
case GDK_KEY_Home:
load_directory(g_get_home_dir());
return TRUE;
}
break;
case 0:

View File

@ -33,6 +33,7 @@
struct _FivView {
GtkWidget parent_instance;
gchar *path; ///< Path to the current image (if any)
cairo_surface_t *image; ///< The loaded image (sequence)
cairo_surface_t *page; ///< Current page within image, weak
cairo_surface_t *frame; ///< Current frame within page, weak
@ -49,6 +50,10 @@ struct _FivView {
G_DEFINE_TYPE(FivView, fiv_view, GTK_TYPE_WIDGET)
struct size {
double width, height;
};
static FivIoOrientation view_left[9] = {
[FivIoOrientationUnknown] = FivIoOrientationUnknown,
[FivIoOrientation0] = FivIoOrientation270,
@ -105,6 +110,7 @@ fiv_view_finalize(GObject *gobject)
{
FivView *self = FIV_VIEW(gobject);
cairo_surface_destroy(self->image);
g_free(self->path);
G_OBJECT_CLASS(fiv_view_parent_class)->finalize(gobject);
}
@ -172,12 +178,11 @@ fiv_view_set_property(
}
}
static void
get_surface_dimensions(FivView *self, double *width, double *height)
static struct size
get_surface_dimensions(FivView *self)
{
*width = *height = 0;
if (!self->image)
return;
return (struct size) {};
cairo_rectangle_t extents = {};
switch (cairo_surface_get_type(self->page)) {
@ -199,23 +204,18 @@ get_surface_dimensions(FivView *self, double *width, double *height)
case FivIoOrientationMirror90:
case FivIoOrientation270:
case FivIoOrientationMirror270:
*width = extents.height;
*height = extents.width;
return;
return (struct size) {extents.height, extents.width};
default:
*width = extents.width;
*height = extents.height;
return (struct size) {extents.width, extents.height};
}
}
static void
get_display_dimensions(FivView *self, int *width, int *height)
{
double w, h;
get_surface_dimensions(self, &w, &h);
*width = ceil(w * self->scale);
*height = ceil(h * self->scale);
struct size surface_dimensions = get_surface_dimensions(self);
*width = ceil(surface_dimensions.width * self->scale);
*height = ceil(surface_dimensions.height * self->scale);
}
static cairo_matrix_t
@ -263,9 +263,7 @@ fiv_view_get_preferred_height(GtkWidget *widget, gint *minimum, gint *natural)
{
FivView *self = FIV_VIEW(widget);
if (self->scale_to_fit) {
double sw, sh;
get_surface_dimensions(self, &sw, &sh);
*natural = ceil(sh);
*natural = ceil(get_surface_dimensions(self).height);
*minimum = 1;
} else {
int dw, dh;
@ -279,9 +277,7 @@ fiv_view_get_preferred_width(GtkWidget *widget, gint *minimum, gint *natural)
{
FivView *self = FIV_VIEW(widget);
if (self->scale_to_fit) {
double sw, sh;
get_surface_dimensions(self, &sw, &sh);
*natural = ceil(sw);
*natural = ceil(get_surface_dimensions(self).width);
*minimum = 1;
} else {
int dw, dh;
@ -299,14 +295,13 @@ fiv_view_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
if (!self->image || !self->scale_to_fit)
return;
double w, h;
get_surface_dimensions(self, &w, &h);
struct size surface_dimensions = get_surface_dimensions(self);
self->scale = 1;
if (ceil(w * self->scale) > allocation->width)
self->scale = allocation->width / w;
if (ceil(h * self->scale) > allocation->height)
self->scale = allocation->height / h;
if (ceil(surface_dimensions.width * self->scale) > allocation->width)
self->scale = allocation->width / surface_dimensions.width;
if (ceil(surface_dimensions.height * self->scale) > allocation->height)
self->scale = allocation->height / surface_dimensions.height;
g_object_notify_by_pspec(G_OBJECT(widget), view_properties[PROP_SCALE]);
}
@ -374,9 +369,7 @@ fiv_view_draw(GtkWidget *widget, cairo_t *cr)
return TRUE;
int w, h;
double sw, sh;
get_display_dimensions(self, &w, &h);
get_surface_dimensions(self, &sw, &sh);
double x = 0;
double y = 0;
@ -385,7 +378,9 @@ fiv_view_draw(GtkWidget *widget, cairo_t *cr)
if (h < allocation.height)
y = round((allocation.height - h) / 2.);
cairo_matrix_t matrix = get_orientation_matrix(self->orientation, sw, sh);
struct size surface_dimensions = get_surface_dimensions(self);
cairo_matrix_t matrix = get_orientation_matrix(
self->orientation, surface_dimensions.width, surface_dimensions.height);
cairo_translate(cr, x, y);
if (self->checkerboard) {
gtk_style_context_save(style);
@ -476,6 +471,28 @@ set_scale(FivView *self, double scale)
return set_scale_to_fit(self, false);
}
static gboolean
set_scale_to_fit_width(FivView *self)
{
double w = get_surface_dimensions(self).width;
int allocated = gtk_widget_get_allocated_width(
gtk_widget_get_parent(GTK_WIDGET(self)));
if (ceil(w * self->scale) > allocated)
return set_scale(self, allocated / w);
return TRUE;
}
static gboolean
set_scale_to_fit_height(FivView *self)
{
double h = get_surface_dimensions(self).height;
int allocated = gtk_widget_get_allocated_height(
gtk_widget_get_parent(GTK_WIDGET(self)));
if (ceil(h * self->scale) > allocated)
return set_scale(self, allocated / h);
return TRUE;
}
static gboolean
fiv_view_scroll_event(GtkWidget *widget, GdkEventScroll *event)
{
@ -647,12 +664,11 @@ static void
on_draw_page(G_GNUC_UNUSED GtkPrintOperation *operation,
GtkPrintContext *context, G_GNUC_UNUSED int page_nr, FivView *self)
{
double surface_width_px = 0, surface_height_px = 0;
get_surface_dimensions(self, &surface_width_px, &surface_height_px);
// Any DPI will be wrong, unless we import that information from the image.
double scale = 1 / 96.;
double w = surface_width_px * scale, h = surface_height_px * scale;
struct size surface_dimensions = get_surface_dimensions(self);
double w = surface_dimensions.width * scale;
double h = surface_dimensions.height * scale;
// Scale down to fit the print area, taking care to not divide by zero.
double areaw = gtk_print_context_get_width(context);
@ -663,12 +679,12 @@ on_draw_page(G_GNUC_UNUSED GtkPrintOperation *operation,
cairo_scale(cr, scale, scale);
cairo_set_source_surface(cr, self->frame, 0, 0);
cairo_matrix_t matrix = get_orientation_matrix(
self->orientation, surface_width_px, surface_height_px);
self->orientation, surface_dimensions.width, surface_dimensions.height);
cairo_pattern_set_matrix(cairo_get_source(cr), &matrix);
cairo_paint(cr);
}
static gboolean
static void
print(FivView *self)
{
GtkPrintOperation *print = gtk_print_operation_new();
@ -695,7 +711,6 @@ print(FivView *self)
show_error_dialog(window, error);
g_object_unref(print);
return TRUE;
}
static gboolean
@ -718,21 +733,32 @@ save_as(FivView *self, gboolean frame)
GtkFileFilter *webp_filter = gtk_file_filter_new();
gtk_file_filter_add_mime_type(webp_filter, "image/webp");
gtk_file_filter_add_pattern(webp_filter, "*.webp");
gtk_file_filter_set_name(webp_filter, "Lossless WebP");
gtk_file_filter_set_name(webp_filter, "Lossless WebP (*.webp)");
gtk_file_chooser_add_filter(chooser, webp_filter);
// TODO(p): Derive it from the currently displayed filename,
// and set the directory to the same place.
gtk_file_chooser_set_current_name(
chooser, frame ? "frame.webp" : "page.webp");
// Note that GTK+'s save dialog is too stupid to automatically change
// the extension when user changes the filter. Presumably,
// gtk_file_chooser_set_extra_widget() can be used to circumvent this.
gchar *basename = g_filename_display_basename(self->path);
gchar *name =
g_strdup_printf(frame ? "%s.frame.webp" : "%s.webp", basename);
g_free(basename);
gtk_file_chooser_set_current_name(chooser, name);
g_free(name);
#endif // HAVE_LIBWEBP
gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
gchar *dirname = g_path_get_dirname(self->path);
gtk_file_chooser_set_current_folder(chooser, dirname);
g_free(dirname);
// The format is supported by Exiv2 and ExifTool.
// This is mostly a developer tool.
GtkFileFilter *exv_filter = gtk_file_filter_new();
gtk_file_filter_add_mime_type(exv_filter, "image/x-exv");
gtk_file_filter_add_pattern(exv_filter, "*.exv");
gtk_file_filter_set_name(exv_filter, "Exiv2 metadata");
gtk_file_filter_set_name(exv_filter, "Exiv2 metadata (*.exv)");
gtk_file_chooser_add_filter(chooser, exv_filter);
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
@ -816,6 +842,11 @@ fiv_view_key_press_event(GtkWidget *widget, GdkEventKey *event)
case GDK_KEY_minus:
return command(self, FIV_VIEW_COMMAND_ZOOM_OUT);
case GDK_KEY_w:
return set_scale_to_fit_width(self);
case GDK_KEY_h:
return set_scale_to_fit_height(self);
case GDK_KEY_x: // Inspired by gThumb, which has more such modes.
return command(self, FIV_VIEW_COMMAND_TOGGLE_SCALE_TO_FIT);
case GDK_KEY_i:
@ -910,7 +941,7 @@ fiv_view_init(FivView *self)
self->scale = 1.0;
}
// --- Picture loading ---------------------------------------------------------
// --- Public interface --------------------------------------------------------
// TODO(p): Progressive picture loading, or at least async/cancellable.
gboolean
@ -927,6 +958,9 @@ fiv_view_open(FivView *self, const gchar *path, GError **error)
switch_page(self, self->image);
set_scale_to_fit(self, true);
g_free(self->path);
self->path = g_strdup(path);
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_HAS_IMAGE]);
return TRUE;
}