mirror of
git://projects.qi-hardware.com/fped.git
synced 2024-11-05 06:03:44 +02:00
5add8b5229
- made xpm name manipulation compatible with older versions of ImageMagick (reported by Alvaro Lopes) - corrected all lines beginning with five or more spaces - Makefile - README: fixed loop example (reported by Joerg Reisenweber) - moved draw_arc and draw_circle from gui_inst.c to gui_util.c - added dragging and hovering - added creation of lines, rectangles, and arcs via GUI - removed arc tool (use circle instead) - fixed reference counting in vectors git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5384 99fdad57-331a-0410-800a-d7fa5415bdb3
237 lines
5.2 KiB
C
237 lines
5.2 KiB
C
/*
|
|
* gui_util.c - GUI helper functions
|
|
*
|
|
* Written 2009 by Werner Almesberger
|
|
* Copyright 2009 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.
|
|
*/
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "util.h"
|
|
#include "gui_style.h"
|
|
#include "gui.h"
|
|
#include "gui_util.h"
|
|
|
|
|
|
/* ----- look up a color --------------------------------------------------- */
|
|
|
|
|
|
GdkColor get_color(const char *spec)
|
|
{
|
|
GdkColormap *cmap;
|
|
GdkColor color;
|
|
|
|
cmap = gdk_drawable_get_colormap(root->window);
|
|
if (!gdk_color_parse(spec, &color))
|
|
abort();
|
|
if (!gdk_colormap_alloc_color(cmap, &color, FALSE, TRUE))
|
|
abort();
|
|
return color;
|
|
}
|
|
|
|
|
|
/* ----- lines with a width ------------------------------------------------ */
|
|
|
|
|
|
void set_width(GdkGC *gc, int width)
|
|
{
|
|
gdk_gc_set_line_attributes(gc, width < 1 ? 1 : width,
|
|
GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
|
|
}
|
|
|
|
|
|
/* ----- backing store ----------------------------------------------------- */
|
|
|
|
|
|
struct pix_buf *save_pix_buf(GdkDrawable *da, int xa, int ya, int xb, int yb,
|
|
int border)
|
|
{
|
|
struct pix_buf *buf;
|
|
int tmp;
|
|
int w, h;
|
|
|
|
if (xa > xb) {
|
|
tmp = xa;
|
|
xa = xb;
|
|
xb = tmp;
|
|
}
|
|
if (ya > yb) {
|
|
tmp = ya;
|
|
ya = yb;
|
|
yb = tmp;
|
|
}
|
|
buf = alloc_type(struct pix_buf);
|
|
buf->da = da;
|
|
buf->x = xa-border;
|
|
buf->y = ya-border;
|
|
w = xb-xa+1+2*border;
|
|
h = yb-ya+1+2*border;
|
|
if (buf->x < 0) {
|
|
w += buf->x;
|
|
buf->x = 0;
|
|
}
|
|
if (buf->y < 0) {
|
|
w += buf->y;
|
|
buf->y = 0;
|
|
}
|
|
buf->buf = gdk_pixbuf_get_from_drawable(NULL, da, NULL,
|
|
buf->x, buf->y, 0, 0, w, h);
|
|
return buf;
|
|
}
|
|
|
|
|
|
void restore_pix_buf(struct pix_buf *buf)
|
|
{
|
|
gdk_draw_pixbuf(buf->da, NULL, buf->buf, 0, 0, buf->x, buf->y, -1, -1,
|
|
GDK_RGB_DITHER_NORMAL, 0, 0);
|
|
g_object_unref(G_OBJECT(buf->buf));
|
|
free(buf);
|
|
}
|
|
|
|
|
|
/* ----- arcs and circles -------------------------------------------------- */
|
|
|
|
|
|
void draw_arc(GdkDrawable *da, GdkGC *gc, int fill,
|
|
int x, int y, int r, double a1, double a2)
|
|
{
|
|
if (a1 == a2)
|
|
a2 = a1+360;
|
|
gdk_draw_arc(da, gc, fill, x-r, y-r, 2*r, 2*r, a1*64, (a2-a1)*64);
|
|
}
|
|
|
|
|
|
void draw_circle(GdkDrawable *da, GdkGC *gc, int fill,
|
|
int x, int y, int r)
|
|
{
|
|
draw_arc(da, gc, fill, x, y, r, 0, 360);
|
|
}
|
|
|
|
|
|
/* ----- labels in a box --------------------------------------------------- */
|
|
|
|
|
|
GtkWidget *label_in_box_new(const char *s)
|
|
{
|
|
GtkWidget *evbox, *label;
|
|
|
|
evbox = gtk_event_box_new();
|
|
label = gtk_label_new(s);
|
|
gtk_misc_set_padding(GTK_MISC(label), 1, 1);
|
|
gtk_container_add(GTK_CONTAINER(evbox), label);
|
|
return label;
|
|
}
|
|
|
|
|
|
GtkWidget *box_of_label(GtkWidget *label)
|
|
{
|
|
return gtk_widget_get_ancestor(label, GTK_TYPE_EVENT_BOX);
|
|
}
|
|
|
|
|
|
void label_in_box_bg(GtkWidget *label, const char *color)
|
|
{
|
|
GtkWidget *box;
|
|
GdkColor col = get_color(color);
|
|
|
|
box = box_of_label(label);
|
|
gtk_widget_modify_bg(box, GTK_STATE_NORMAL, &col);
|
|
}
|
|
|
|
|
|
/* ----- render a text string ---------------------------------------------- */
|
|
|
|
|
|
void render_text(GdkDrawable *da, GdkGC *gc, int x, int y, double angle,
|
|
const char *s, const char *font, double xalign, double yalign,
|
|
int xmax, int ymax)
|
|
{
|
|
GdkScreen *screen;
|
|
PangoRenderer *renderer;
|
|
PangoContext *context;
|
|
PangoLayout *layout;
|
|
PangoFontDescription *desc;
|
|
int width, height;
|
|
PangoMatrix m = PANGO_MATRIX_INIT;
|
|
double f_min, f;
|
|
|
|
/* set up the renderer */
|
|
|
|
screen = gdk_drawable_get_screen(da);
|
|
renderer = gdk_pango_renderer_get_default(screen);
|
|
gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER(renderer), da);
|
|
gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER(renderer), gc);
|
|
|
|
/* start preparing the layout */
|
|
|
|
context = gdk_pango_context_get_for_screen(screen);
|
|
layout = pango_layout_new(context);
|
|
pango_layout_set_text(layout, s, -1);
|
|
|
|
/* apply the font */
|
|
|
|
desc = pango_font_description_from_string(font);
|
|
pango_layout_set_font_description(layout, desc);
|
|
pango_font_description_free(desc);
|
|
|
|
/* align and position the text */
|
|
|
|
pango_layout_get_size(layout, &width, &height);
|
|
f_min = 1.0;
|
|
if (xmax) {
|
|
f = xmax/((double) width/PANGO_SCALE);
|
|
if (f < f_min)
|
|
f_min = f;
|
|
}
|
|
if (ymax) {
|
|
f = ymax/((double) height/PANGO_SCALE);
|
|
if (f < f_min)
|
|
f_min = f;
|
|
}
|
|
if (f_min < MIN_FONT_SCALE)
|
|
f_min = MIN_FONT_SCALE;
|
|
pango_matrix_translate(&m, x, y);
|
|
pango_matrix_rotate(&m, angle);
|
|
pango_matrix_translate(&m,
|
|
-xalign*f_min*width/PANGO_SCALE,
|
|
(yalign-1)*f_min*height/PANGO_SCALE);
|
|
pango_matrix_scale(&m, f_min, f_min);
|
|
|
|
pango_context_set_matrix(context, &m);
|
|
pango_layout_context_changed(layout);
|
|
pango_renderer_draw_layout(renderer, layout, 0, 0);
|
|
|
|
/* clean up renderer */
|
|
|
|
gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER(renderer), NULL);
|
|
gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER(renderer), NULL);
|
|
|
|
/* free objects */
|
|
|
|
g_object_unref(layout);
|
|
g_object_unref(context);
|
|
}
|
|
|
|
|
|
/* ----- kill the content of a container ----------------------------------- */
|
|
|
|
|
|
static void destroy_callback(GtkWidget *widget, gpointer data)
|
|
{
|
|
gtk_widget_destroy(widget);
|
|
}
|
|
|
|
|
|
void destroy_all_children(GtkContainer *container)
|
|
{
|
|
gtk_container_foreach(container, destroy_callback, NULL);
|
|
}
|