mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-10-04 06:29:47 +03:00
1771c635b0
"record" as in "render with record_*"
350 lines
7.1 KiB
C
350 lines
7.1 KiB
C
/*
|
|
* gui.c - GUI for eeshow
|
|
*
|
|
* Written 2016 by Werner Almesberger
|
|
* Copyright 2016 by Werner Almesberger
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
/*
|
|
* Resources:
|
|
*
|
|
* http://zetcode.com/gfx/cairo/cairobackends/
|
|
* https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "util.h"
|
|
#include "cro.h"
|
|
#include "gfx.h"
|
|
#include "sch.h"
|
|
#include "gui.h"
|
|
|
|
|
|
struct gui_sheet {
|
|
const const struct sheet *sch;
|
|
struct cro_ctx *gfx_ctx;
|
|
|
|
int w, h;
|
|
int xmin, ymin;
|
|
|
|
struct gui_sheet *next;
|
|
};
|
|
|
|
|
|
struct gui_ctx {
|
|
GtkWidget *da;
|
|
|
|
int curr_x; /* last on-screen mouse position */
|
|
int curr_y;
|
|
|
|
unsigned zoom; /* scale by 1.0 / (1 << zoom) */
|
|
int x, y; /* center, in eeschema coordinates */
|
|
|
|
bool panning;
|
|
int pan_x, pan_y;
|
|
|
|
const struct gui_sheet *curr_sheet;
|
|
/* current sheet */
|
|
|
|
struct gui_sheet *sheets;
|
|
} gui_ctx;
|
|
|
|
|
|
/* ----- Rendering --------------------------------------------------------- */
|
|
|
|
|
|
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
|
|
gpointer user_data)
|
|
{
|
|
const struct gui_ctx *ctx = user_data;
|
|
const struct gui_sheet *sheet = ctx->curr_sheet;
|
|
GtkAllocation alloc;
|
|
|
|
float f = 1.0 / (1 << ctx->zoom);
|
|
int x, y;
|
|
|
|
gtk_widget_get_allocation(ctx->da, &alloc);
|
|
x = -(sheet->xmin + ctx->x) * f + alloc.width / 2;
|
|
y = -(sheet->ymin + ctx->y) * f + alloc.height / 2;
|
|
cro_canvas_draw(sheet->gfx_ctx, cr, x, y, f);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
static void render(struct gui_ctx *ctx, struct gui_sheet *sheet)
|
|
{
|
|
char *argv[] = { "gui", NULL };
|
|
|
|
gfx_init(&cro_canvas_ops, 1, argv);
|
|
sch_render(sheet->sch);
|
|
cro_canvas_end(gfx_ctx,
|
|
&sheet->w, &sheet->h, &sheet->xmin, &sheet->ymin);
|
|
sheet->gfx_ctx = gfx_ctx;
|
|
|
|
ctx->x = sheet->w >> 1;
|
|
ctx->y = sheet->h >> 1;
|
|
// gfx_end();
|
|
}
|
|
|
|
|
|
/* ----- Tools ------------------------------------------------------------- */
|
|
|
|
|
|
static void canvas_coord(const struct gui_ctx *ctx,
|
|
int ex, int ey, int *x, int *y)
|
|
{
|
|
int sx, sy;
|
|
GtkAllocation alloc;
|
|
|
|
gtk_widget_get_allocation(ctx->da, &alloc);
|
|
sx = ex - alloc.width / 2;
|
|
sy = ey - alloc.height / 2;
|
|
*x = (sx << ctx->zoom) + ctx->x;
|
|
*y = (sy << ctx->zoom) + ctx->y;
|
|
}
|
|
|
|
|
|
/* ----- Panning ----------------------------------------------------------- */
|
|
|
|
|
|
static void pan_begin(struct gui_ctx *ctx, int x, int y)
|
|
{
|
|
if (ctx->panning)
|
|
return;
|
|
ctx->panning = 1;
|
|
ctx->pan_x = x;
|
|
ctx->pan_y = y;
|
|
}
|
|
|
|
|
|
static void pan_update(struct gui_ctx *ctx, int x, int y)
|
|
{
|
|
if (!ctx->panning)
|
|
return;
|
|
|
|
ctx->x -= (x - ctx->pan_x) << ctx->zoom;
|
|
ctx->y -= (y - ctx->pan_y) << ctx->zoom;
|
|
ctx->pan_x = x;
|
|
ctx->pan_y = y;
|
|
|
|
gtk_widget_queue_draw(ctx->da);
|
|
}
|
|
|
|
|
|
static void pan_end(struct gui_ctx *ctx, int x, int y)
|
|
{
|
|
pan_update(ctx, x, y);
|
|
ctx->panning = 0;
|
|
}
|
|
|
|
|
|
/* ----- Zoom -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void zoom_in(struct gui_ctx *ctx, int x, int y)
|
|
{
|
|
if (ctx->zoom == 0)
|
|
return;
|
|
ctx->zoom--;
|
|
ctx->x = (ctx->x + x) / 2;
|
|
ctx->y = (ctx->y + y) / 2;
|
|
gtk_widget_queue_draw(ctx->da);
|
|
}
|
|
|
|
|
|
static void zoom_out(struct gui_ctx *ctx, int x, int y)
|
|
{
|
|
if (ctx->curr_sheet->w >> ctx->zoom <= 16)
|
|
return;
|
|
ctx->zoom++;
|
|
ctx->x = 2 * ctx->x - x;
|
|
ctx->y = 2 * ctx->y - y;
|
|
gtk_widget_queue_draw(ctx->da);
|
|
}
|
|
|
|
|
|
/* ----- Event handlers ---------------------------------------------------- */
|
|
|
|
|
|
static gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event,
|
|
gpointer data)
|
|
{
|
|
struct gui_ctx *ctx = data;
|
|
int x, y;
|
|
|
|
ctx->curr_x = event->x;
|
|
ctx->curr_y = event->y;
|
|
|
|
canvas_coord(ctx, event->x, event->y, &x, &y);
|
|
// fprintf(stderr, "motion\n");
|
|
pan_update(ctx, event->x, event->y);
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
|
|
gpointer data)
|
|
{
|
|
struct gui_ctx *ctx = data;
|
|
int x, y;
|
|
|
|
canvas_coord(ctx, event->x, event->y, &x, &y);
|
|
fprintf(stderr, "button press\n");
|
|
switch (event->button) {
|
|
case 1:
|
|
break;
|
|
case 2:
|
|
pan_begin(ctx, event->x, event->y);
|
|
break;
|
|
case 3:
|
|
break;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
|
|
gpointer data)
|
|
{
|
|
struct gui_ctx *ctx = data;
|
|
int x, y;
|
|
|
|
canvas_coord(ctx, event->x, event->y, &x, &y);
|
|
fprintf(stderr, "button release\n");
|
|
switch (event->button) {
|
|
case 1:
|
|
break;
|
|
case 2:
|
|
pan_end(ctx, event->x, event->y);
|
|
break;
|
|
case 3:
|
|
break;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
|
|
gpointer data)
|
|
{
|
|
struct gui_ctx *ctx = data;
|
|
int x, y;
|
|
|
|
canvas_coord(ctx, ctx->curr_x, ctx->curr_y, &x, &y);
|
|
switch (event->keyval) {
|
|
case '+':
|
|
case '=':
|
|
zoom_in(ctx, x, y);
|
|
break;
|
|
case '-':
|
|
zoom_out(ctx, x, y);
|
|
break;
|
|
case GDK_KEY_q:
|
|
gtk_main_quit();
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event,
|
|
gpointer data)
|
|
{
|
|
struct gui_ctx *ctx = data;
|
|
int x, y;
|
|
|
|
canvas_coord(ctx, event->x, event->y, &x, &y);
|
|
switch (event->direction) {
|
|
case GDK_SCROLL_UP:
|
|
zoom_in(ctx, x, y);
|
|
break;
|
|
case GDK_SCROLL_DOWN:
|
|
zoom_out(ctx, x, y);
|
|
break;
|
|
default:
|
|
/* ignore */;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
/* ----- Initialization ---------------------------------------------------- */
|
|
|
|
|
|
static void get_sheets( struct gui_ctx *ctx, const struct sheet *sheets)
|
|
{
|
|
const struct sheet *sheet;
|
|
struct gui_sheet **next = &ctx->sheets;
|
|
struct gui_sheet *gui_sheet;
|
|
|
|
for (sheet = sheets; sheet; sheet = sheet->next) {
|
|
gui_sheet = alloc_type(struct gui_sheet);
|
|
gui_sheet->sch = sheet;
|
|
render(ctx, gui_sheet);
|
|
*next = gui_sheet;
|
|
next = &gui_sheet->next;
|
|
}
|
|
*next = NULL;
|
|
ctx->curr_sheet = ctx->sheets;
|
|
}
|
|
|
|
|
|
int gui(const struct sheet *sheets)
|
|
{
|
|
GtkWidget *window;
|
|
struct gui_ctx ctx = {
|
|
.zoom = 4, /* scale by 1 / 16 */
|
|
.panning = 0,
|
|
};
|
|
|
|
get_sheets(&ctx, sheets);
|
|
|
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
ctx.da = gtk_drawing_area_new();
|
|
gtk_container_add(GTK_CONTAINER(window), ctx.da);
|
|
|
|
g_signal_connect(G_OBJECT(ctx.da), "draw",
|
|
G_CALLBACK(on_draw_event), &ctx);
|
|
g_signal_connect(G_OBJECT(ctx.da), "motion_notify_event",
|
|
G_CALLBACK(motion_notify_event), &ctx);
|
|
g_signal_connect(G_OBJECT(ctx.da), "button_press_event",
|
|
G_CALLBACK(button_press_event), &ctx);
|
|
g_signal_connect(G_OBJECT(ctx.da), "button_release_event",
|
|
G_CALLBACK(button_release_event), &ctx);
|
|
g_signal_connect(G_OBJECT(ctx.da), "scroll_event",
|
|
G_CALLBACK(scroll_event), &ctx);
|
|
g_signal_connect(G_OBJECT(ctx.da), "key_press_event",
|
|
G_CALLBACK(key_press_event), &ctx);
|
|
g_signal_connect(window, "destroy",
|
|
G_CALLBACK(gtk_main_quit), NULL);
|
|
|
|
gtk_widget_set_can_focus(ctx.da, TRUE);
|
|
|
|
gtk_widget_set_events(ctx.da,
|
|
GDK_EXPOSE | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
|
|
GDK_KEY_PRESS_MASK |
|
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
|
|
GDK_SCROLL_MASK |
|
|
GDK_POINTER_MOTION_MASK);
|
|
|
|
// gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
|
// gtk_window_set_default_size(GTK_WINDOW(window), 400, 90);
|
|
gtk_window_set_title(GTK_WINDOW(window), "eeshow");
|
|
|
|
gtk_widget_show_all(window);
|
|
|
|
gtk_main();
|
|
|
|
return 0;
|
|
}
|