From 92a0cfab4b57f1637d7b3f34e74f5571aee6db56 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 3 Aug 2016 13:05:30 -0300 Subject: [PATCH] eeshow/gui.c: zoom to extents on [*] and when showing a new sheet --- eeshow/gui.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/eeshow/gui.c b/eeshow/gui.c index 151209c..54eb88f 100644 --- a/eeshow/gui.c +++ b/eeshow/gui.c @@ -176,8 +176,8 @@ static void render(struct gui_ctx *ctx, struct gui_sheet *sheet) static void canvas_coord(const struct gui_ctx *ctx, int ex, int ey, int *x, int *y) { - int sx, sy; GtkAllocation alloc; + int sx, sy; gtk_widget_get_allocation(ctx->da, &alloc); sx = ex - alloc.width / 2; @@ -247,6 +247,24 @@ static void zoom_out(struct gui_ctx *ctx, int x, int y) } +static void zoom_to_extents(struct gui_ctx *ctx) +{ + const struct gui_sheet *sheet = ctx->curr_sheet; + GtkAllocation alloc; + + ctx->x = sheet->w / 2; + ctx->y = sheet->h / 2; + + gtk_widget_get_allocation(ctx->da, &alloc); + ctx->zoom = 0; + while (sheet->w >> ctx->zoom > alloc.width || + sheet->h >> ctx->zoom > alloc.height) + ctx->zoom++; + + gtk_widget_queue_draw(ctx->da); +} + + /* ----- Event handlers ---------------------------------------------------- */ @@ -328,12 +346,15 @@ static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event, case '-': zoom_out(ctx, x, y); break; + case '*': + zoom_to_extents(ctx); + break; case GDK_KEY_BackSpace: case GDK_KEY_Delete: if (sheet->prev) { ctx->curr_sheet = sheet->prev; sheet->prev = NULL; - gtk_widget_queue_draw(ctx->da); + zoom_to_extents(ctx); } break; case GDK_KEY_q: @@ -377,7 +398,7 @@ static void select_subsheet(struct gui_ctx *ctx, void *user) if (sheet->sch == obj->u.sheet.sheet) { sheet->prev = ctx->curr_sheet; ctx->curr_sheet = sheet; - gtk_widget_queue_draw(ctx->da); + zoom_to_extents(ctx); return; } abort(); @@ -479,6 +500,7 @@ int gui(const struct sheet *sheets) gtk_window_set_title(GTK_WINDOW(window), "eeshow"); gtk_widget_show_all(window); + zoom_to_extents(&ctx); gtk_main();