1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-12-23 19:02:59 +02:00

eeshow/: add rendering on Gtk canvas

This commit is contained in:
Werner Almesberger 2016-08-03 07:38:57 -03:00
parent 3c13d609f7
commit 76c1f916df
6 changed files with 161 additions and 6 deletions

View File

@ -12,16 +12,19 @@
NAME = eeshow NAME = eeshow
OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \ OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \
gui.o \
file.o git-file.o \ file.o git-file.o \
style.o fig.o record.o cro.o diff.o gfx.o dwg.o text.o misc.o style.o fig.o record.o cro.o diff.o gfx.o dwg.o text.o misc.o
CFLAGS = -g -Wall -Wextra -Wno-unused-parameter -Wshadow \ CFLAGS = -g -Wall -Wextra -Wno-unused-parameter -Wshadow \
-Wmissing-prototypes -Wmissing-declarations \ -Wmissing-prototypes -Wmissing-declarations \
`pkg-config --cflags cairo` \ `pkg-config --cflags cairo` \
`pkg-config --cflags libgit2` `pkg-config --cflags libgit2` \
`pkg-config --cflags gtk+-3.0`
LDLIBS = -lm \ LDLIBS = -lm \
`pkg-config --libs cairo` \ `pkg-config --libs cairo` \
`pkg-config --libs libgit2` `pkg-config --libs libgit2` \
`pkg-config --libs gtk+-3.0`
include ../common/Makefile.c-common include ../common/Makefile.c-common

View File

@ -456,6 +456,31 @@ void cro_img_write(void *ctx, const char *name)
} }
void cro_canvas_end(void *ctx)
{
struct cro_ctx *cc = ctx;
int w, h;
end_common(cc, &w, &h);
}
void cro_canvas_draw(struct cro_ctx *cc, cairo_t *cr)
{
set_color(cr, COLOR_WHITE);
cairo_paint(cr);
cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_line_width(cr, 2);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cc->cr = cr;
record_replay(&cc->record);
}
/* ----- Operations -------------------------------------------------------- */ /* ----- Operations -------------------------------------------------------- */

View File

@ -16,16 +16,25 @@
#include <stdint.h> #include <stdint.h>
#include <cairo/cairo.h>
#include "gfx.h" #include "gfx.h"
struct cro_ctx;
extern const struct gfx_ops cro_png_ops; extern const struct gfx_ops cro_png_ops;
extern const struct gfx_ops cro_pdf_ops; extern const struct gfx_ops cro_pdf_ops;
#define cro_img_ops cro_png_ops /* just don't call cro_img_ops.end */ #define cro_img_ops cro_png_ops /* just don't call cro_img_ops.end */
#define cro_canvas_ops cro_png_ops /* just don't call cro_canvas_ops.end */
uint32_t *cro_img_end(void *ctx, int *w, int *h, int *stride); uint32_t *cro_img_end(void *ctx, int *w, int *h, int *stride);
void cro_img_write(void *ctx, const char *name); void cro_img_write(void *ctx, const char *name);
void cro_canvas_end(void *ctx);
void cro_canvas_draw(struct cro_ctx *cc, cairo_t *cr);
#endif /* !CRO_H */ #endif /* !CRO_H */

86
eeshow/gui.c Normal file
View File

@ -0,0 +1,86 @@
/*
* 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 <gtk/gtk.h>
#include "cro.h"
#include "gfx.h"
#include "sch.h"
#include "gui.h"
struct gui_ctx {
const struct sch_ctx *sch;
struct cro_ctx *gfx_ctx;
} gui_ctx;
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
const struct gui_ctx *ctx = user_data;
cro_canvas_draw(ctx->gfx_ctx, cr);
return FALSE;
}
static void render(struct gui_ctx *ctx)
{
char *argv[] = { "gui", NULL };
gfx_init(&cro_canvas_ops, 1, argv);
sch_render(ctx->sch->sheets);
cro_canvas_end(gfx_ctx);
ctx->gfx_ctx = gfx_ctx;
// gfx_end();
}
int gui(struct sch_ctx *sch)
{
GtkWidget *window;
GtkWidget *da;
struct gui_ctx ctx = {
.sch = sch,
};
render(&ctx);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
da = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), da);
g_signal_connect(G_OBJECT(da), "draw",
G_CALLBACK(on_draw_event), &ctx);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
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), "GTK window");
gtk_widget_show_all(window);
gtk_main();
return 0;
}

21
eeshow/gui.h Normal file
View File

@ -0,0 +1,21 @@
/*
* gui.h - 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.
*/
#ifndef GUI_H
#define GUI_H
#include "sch.h"
int gui(struct sch_ctx *sch);
#endif /* !GUI_H */

View File

@ -17,6 +17,8 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <gtk/gtk.h>
#include "util.h" #include "util.h"
#include "fig.h" #include "fig.h"
#include "cro.h" #include "cro.h"
@ -25,6 +27,7 @@
#include "file.h" #include "file.h"
#include "lib.h" #include "lib.h"
#include "sch.h" #include "sch.h"
#include "gui.h"
#include "main.h" #include "main.h"
@ -43,7 +46,7 @@ void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: %s [-r] [-v ...] [[rev:]file.lib ...] [rev:]file.sch\n" "usage: %s [-r] [-v ...] [[rev:]file.lib ...] [rev:]file.sch\n"
" %*s-- driver_spec\n" " %*s[-- driver_spec]\n"
" %s [-v ...] -C [rev:]file\n" " %s [-v ...] -C [rev:]file\n"
"\n" "\n"
" rev git revision\n" " rev git revision\n"
@ -77,7 +80,7 @@ void usage(const char *name)
} }
int main(int argc, char *const *argv) int main(int argc, char **argv)
{ {
struct lib lib; struct lib lib;
struct sch_ctx sch_ctx; struct sch_ctx sch_ctx;
@ -86,13 +89,18 @@ int main(int argc, char *const *argv)
const char *cat = NULL; const char *cat = NULL;
char c; char c;
int arg, dashdash; int arg, dashdash;
bool have_dashdash = 0;
int gfx_argc; int gfx_argc;
char **gfx_argv; char **gfx_argv;
const struct gfx_ops **ops = ops_list; const struct gfx_ops **ops = ops_list;
gtk_init(&argc, &argv);
for (dashdash = 1; dashdash != argc; dashdash++) for (dashdash = 1; dashdash != argc; dashdash++)
if (!strcmp(argv[dashdash], "--")) if (!strcmp(argv[dashdash], "--")) {
have_dashdash = 1;
break; break;
}
while ((c = getopt(dashdash, argv, "rvC:")) != EOF) while ((c = getopt(dashdash, argv, "rvC:")) != EOF)
switch (c) { switch (c) {
@ -158,6 +166,9 @@ found:
sch_parse(&sch_ctx, &sch_file, &lib); sch_parse(&sch_ctx, &sch_file, &lib);
file_close(&sch_file); file_close(&sch_file);
if (!have_dashdash)
return gui(&sch_ctx);
gfx_init(*ops, gfx_argc, gfx_argv); gfx_init(*ops, gfx_argc, gfx_argv);
if (recurse) { if (recurse) {
const struct sheet *sheet; const struct sheet *sheet;