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

eeshow/gui-over.h, gui-over.c: split overlays from gui.c

This commit is contained in:
Werner Almesberger 2016-08-03 20:55:51 -03:00
parent 2e909029bc
commit 69497551dd
4 changed files with 173 additions and 126 deletions

View File

@ -12,7 +12,7 @@
NAME = eeshow
OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \
gui.o \
gui.o gui-over.o \
file.o git-file.o \
style.o fig.o record.o cro.o diff.o gfx.o dwg.o text.o misc.o

141
eeshow/gui-over.c Normal file
View File

@ -0,0 +1,141 @@
/*
* gui-over.c - GUI: overlays
*
* 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
* https://www.cairographics.org/samples/rounded_rectangle/
*/
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include <cairo/cairo.h>
#include "util.h"
#include "gui-over.h"
#define OVER_FONT_SIZE 16
#define OVER_BORDER 8
#define OVER_RADIUS 6
#define OVER_SEP 8
#define OVER_X0 10
#define OVER_Y0 10
struct overlay {
const char *s;
struct overlay *next;
};
static void rrect(cairo_t *cr, int x, int y, int w, int h, int r)
{
const double deg = M_PI / 180.0;
cairo_new_path(cr);
cairo_arc(cr, x + w - r, y + r, r, -90 * deg, 0);
cairo_arc(cr, x + w - r, y + h - r, r, 0, 90 * deg);
cairo_arc(cr, x + r, y + h - r, r, 90 * deg, 180 * deg);
cairo_arc(cr, x + r, y + r, r, 180 * deg, 270 * deg);
cairo_close_path(cr);
}
static void overlay_draw(const struct overlay *over, cairo_t *cr,
int *x, int *y)
{
cairo_text_extents_t ext;
cairo_set_font_size(cr, OVER_FONT_SIZE);
cairo_text_extents(cr, over->s, &ext);
rrect(cr, *x, *y,
ext.width + 2 * OVER_BORDER, ext.height + 2 * OVER_BORDER,
OVER_RADIUS);
cairo_set_source_rgba(cr, 0.8, 0.9, 1, 0.8);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0.5, 0.5, 1, 0.7);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, *x + OVER_BORDER, *y + OVER_BORDER + ext.height);
cairo_show_text(cr, over->s);
*y += ext.height + OVER_SEP;
}
void overlay_draw_all(const struct overlay *overlays, cairo_t *cr)
{
const struct overlay *over;
int x = OVER_X0;
int y = OVER_Y0;
for (over = overlays; over; over = over->next)
overlay_draw(over, cr, &x, &y);
}
struct overlay *overlay_add(struct overlay **overlays, const char *s)
{
struct overlay *over;
struct overlay **anchor;
over = alloc_type(struct overlay);
over->s = stralloc(s);
for (anchor = overlays; *anchor; anchor = &(*anchor)->next);
over->next = NULL;
*anchor = over;
return over;
}
static void overlay_free(struct overlay *over)
{
free((void *) over->s);
free(over);
}
void overlay_remove(struct overlay **overlays, struct overlay *over)
{
struct overlay **anchor;
for (anchor = overlays; *anchor; anchor = &(*anchor)->next)
if (*anchor == over) {
*anchor = over->next;
overlay_free(over);
return;
}
abort();
}
void overlay_remove_all(struct overlay **overlays)
{
struct overlay *next;
while (*overlays) {
next = (*overlays)->next;
overlay_free(*overlays);
*overlays = next;
}
}

27
eeshow/gui-over.h Normal file
View File

@ -0,0 +1,27 @@
/*
* gui-over.h - GUI: overlays
*
* 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_OVER_H
#define GUI_OVER_H
#include <cairo/cairo.h>
struct overlay;
void overlay_draw_all(const struct overlay *overlays, cairo_t *cr);
struct overlay *overlay_add(struct overlay **overlays, const char *s);
void overlay_remove(struct overlay **overlays, struct overlay *over);
void overlay_remove_all(struct overlay **overlays);
#endif /* !GUI_OVER_H */

View File

@ -30,6 +30,7 @@
#include "cro.h"
#include "gfx.h"
#include "sch.h"
#include "gui-over.h"
#include "gui.h"
@ -59,8 +60,6 @@ struct gui_sheet {
struct gui_sheet *next;
};
struct overlay;
struct gui_ctx {
GtkWidget *da;
@ -149,126 +148,6 @@ static void aoi_click(struct gui_ctx *ctx, int x, int y)
}
/* ----- Overlays ---------------------------------------------------------- */
struct overlay {
const char *s;
struct overlay *next;
};
#define OVER_FONT_SIZE 16
#define OVER_BORDER 8
#define OVER_RADIUS 6
#define OVER_SEP 8
#define OVER_X0 10
#define OVER_Y0 10
static void rrect(cairo_t *cr, int x, int y, int w, int h, int r)
{
const double deg = M_PI / 180.0;
// https://www.cairographics.org/samples/rounded_rectangle/
cairo_new_path(cr);
cairo_arc(cr, x + w - r, y + r, r, -90 * deg, 0);
cairo_arc(cr, x + w - r, y + h - r, r, 0, 90 * deg);
cairo_arc(cr, x + r, y + h - r, r, 90 * deg, 180 * deg);
cairo_arc(cr, x + r, y + r, r, 180 * deg, 270 * deg);
cairo_close_path(cr);
}
static void overlay_draw(const struct overlay *over, cairo_t *cr,
int *x, int *y)
{
cairo_text_extents_t ext;
cairo_set_font_size(cr, OVER_FONT_SIZE);
cairo_text_extents(cr, over->s, &ext);
rrect(cr, *x, *y,
ext.width + 2 * OVER_BORDER, ext.height + 2 * OVER_BORDER,
OVER_RADIUS);
cairo_set_source_rgba(cr, 0.8, 0.9, 1, 0.8);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0.5, 0.5, 1, 0.7);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, *x + OVER_BORDER, *y + OVER_BORDER + ext.height);
cairo_show_text(cr, over->s);
*y += ext.height + OVER_SEP;
}
static void overlay_draw_all(const struct gui_ctx *ctx, cairo_t *cr)
{
const struct overlay *over;
int x = OVER_X0;
int y = OVER_Y0;
for (over = ctx->overlays; over; over = over->next)
overlay_draw(over, cr, &x, &y);
}
static struct overlay *overlay_add(struct gui_ctx *ctx, const char *s)
{
struct overlay *over;
struct overlay **anchor;
over = alloc_type(struct overlay);
over->s = stralloc(s);
for (anchor = &ctx->overlays; *anchor; anchor = &(*anchor)->next);
over->next = NULL;
*anchor = over;
return over;
}
static void overlay_free(struct overlay *over)
{
free((void *) over->s);
free(over);
}
static void overlay_remove(struct gui_ctx *ctx, struct overlay *over)
{
struct overlay **anchor;
for (anchor = &ctx->overlays; *anchor; anchor = &(*anchor)->next)
if (*anchor == over) {
*anchor = over->next;
overlay_free(over);
redraw(ctx);
return;
}
abort();
}
static void overlay_remove_all(struct gui_ctx *ctx)
{
struct overlay *next;
while (ctx->overlays) {
next = ctx->overlays->next;
overlay_free(ctx->overlays);
ctx->overlays = next;
}
redraw(ctx);
}
/* ----- Rendering --------------------------------------------------------- */
@ -287,7 +166,7 @@ static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
y = -(sheet->ymin + ctx->y) * f + alloc.height / 2;
cro_canvas_draw(sheet->gfx_ctx, cr, x, y, f);
overlay_draw_all(ctx, cr);
overlay_draw_all(ctx->overlays, cr);
return FALSE;
}
@ -473,7 +352,7 @@ static void set_sheet(struct gui_ctx *ctx, struct gui_sheet *sheet)
if (sheet->prev == sheet)
sheet->prev = NULL;
ctx->curr_sheet = sheet;
overlay_remove_all(ctx);
overlay_remove_all(&ctx->overlays);
zoom_to_extents(ctx);
}
@ -556,7 +435,7 @@ static void select_subsheet(struct gui_ctx *ctx, void *user)
if (sheet->sch == obj->u.sheet.sheet) {
sheet->prev = ctx->curr_sheet;
set_sheet(ctx, sheet);
overlay_add(ctx, obj->u.sheet.name);
overlay_add(&ctx->overlays, obj->u.sheet.name);
return;
}
abort();