1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-12-23 15:32:25 +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
OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \
gui.o \
file.o git-file.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 \
-Wmissing-prototypes -Wmissing-declarations \
`pkg-config --cflags cairo` \
`pkg-config --cflags libgit2`
`pkg-config --cflags libgit2` \
`pkg-config --cflags gtk+-3.0`
LDLIBS = -lm \
`pkg-config --libs cairo` \
`pkg-config --libs libgit2`
`pkg-config --libs cairo` \
`pkg-config --libs libgit2` \
`pkg-config --libs gtk+-3.0`
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 -------------------------------------------------------- */

View File

@ -16,16 +16,25 @@
#include <stdint.h>
#include <cairo/cairo.h>
#include "gfx.h"
struct cro_ctx;
extern const struct gfx_ops cro_png_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_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);
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 */

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