Root/eeshow/gui/glabel.c

1/*
2 * gui/glabel.c - Global label pop-up
3 *
4 * Written 2016 by Werner Almesberger
5 * Copyright 2016 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <stddef.h>
14#include <stdbool.h>
15#include <string.h>
16
17#include <gtk/gtk.h>
18
19#include "misc/util.h"
20#include "kicad/dwg.h"
21#include "gui/style.h"
22#include "gui/input.h"
23#include "gui/aoi.h"
24#include "gui/over.h"
25#include "gui/common.h"
26
27
28/* small offset to hide rounding errors */
29#define CHEAT 1
30
31
32struct glabel_aoi_ctx {
33    const struct gui_sheet *sheet;
34    const struct sch_obj *obj;
35    struct dwg_bbox bbox;
36    struct overlay *over;
37};
38
39
40#define GLABEL_W 100
41
42
43/* ----- Tools ------------------------------------------------------------- */
44
45
46static void eeschema_coord(const struct gui_ctx *ctx,
47    int x, int y, int *rx, int *ry)
48{
49    GtkAllocation alloc;
50
51    gtk_widget_get_allocation(ctx->da, &alloc);
52    *rx = ((x - ctx->x) * ctx->scale) + alloc.width / 2;
53    *ry = ((y - ctx->y) * ctx->scale) + alloc.height / 2;
54}
55
56
57/* ----- AoIs -------------------------------------------------------------- */
58
59
60static void glabel_dest_click(void *user)
61{
62    struct gui_sheet *sheet = user;
63
64    go_to_sheet(sheet->ctx, sheet);
65}
66
67
68void dehover_glabel(struct gui_ctx *ctx)
69{
70    overlay_remove_all(&ctx->pop_overlays);
71    overlay_remove_all(&ctx->pop_underlays);
72    ctx->pop_origin = NULL;
73    redraw(ctx);
74}
75
76
77static void add_dest_header(struct gui_ctx *ctx, const char *label)
78{
79    struct overlay_style style = {
80        .font = BOLD_FONT,
81        .wmin = GLABEL_W,
82        .wmax = GLABEL_W,
83        .radius = 0,
84        .pad = 0,
85        .skip = 6,
86        .fg = { 0.5, 0.0, 0.0, 1.0 },
87        .bg = { 0.0, 0.0, 0.0, 0.0 },
88        .frame = { 1.0, 1.0, 1.0, 1.0 }, /* debugging */
89        .width = 0,
90    };
91    struct overlay *over;
92
93    over = overlay_add(&ctx->pop_overlays, NULL, NULL, NULL, NULL);
94    overlay_text(over, "%s", label);
95    overlay_style(over, &style);
96}
97
98
99static void add_dest_overlay(struct gui_ctx *ctx, const char *label,
100    struct gui_sheet *sheet, unsigned n)
101{
102    struct overlay_style style = {
103        .font = BOLD_FONT,
104        .wmin = GLABEL_W,
105        .wmax = GLABEL_W,
106        .radius = 0,
107        .pad = 0,
108        .skip = 4,
109        .fg = { 0.0, 0.0, 0.0, 1.0 },
110        .bg = { 0.0, 0.0, 0.0, 0.0 },
111        .frame = { 1.0, 1.0, 1.0, 1.0 }, /* debugging */
112        .width = 0,
113    };
114    const struct sch_obj *obj;
115    struct overlay *over;
116
117    if (sheet == ctx->curr_sheet)
118        style.fg = RGBA(0.5, 0.5, 0.5, 1.0);
119
120    for (obj = sheet->sch->objs; obj; obj = obj->next) {
121        if (obj->type != sch_obj_glabel)
122            continue;
123        if (strcmp(obj->u.text.s, label))
124            continue;
125        over = overlay_add(&ctx->pop_overlays,
126            &ctx->aois, NULL, glabel_dest_click, sheet);
127        overlay_text(over, "%d %s", n,
128            sheet->sch->title ? sheet->sch->title : "(unnamed)");
129        overlay_style(over, &style);
130        break;
131    }
132}
133
134
135static bool pop_hover(void *user, bool on, int dx, int dy)
136{
137    struct gui_ctx *ctx = user;
138
139    if (!on)
140        dehover_glabel(ctx);
141    return 1;
142}
143
144
145static void add_dest_frame(struct gui_ctx *ctx)
146{
147    int w, h;
148
149    overlay_size_all(ctx->pop_overlays,
150        gtk_widget_get_pango_context(ctx->da), 0, 1, &w, &h);
151
152    struct overlay_style style = {
153        .font = BOLD_FONT,
154        .wmin = w,
155        .hmin = h,
156        .radius = 0,
157        .pad = GLABEL_STACK_PADDING,
158        .skip = 0,
159        .fg = { 0.0, 0.0, 0.0, 1.0 },
160        .bg = { 0.9, 0.9, 0.3, 0.8 },
161        .frame = { 0.0, 0.0, 0.0, 1.0 }, /* debugging */
162        .width = 1,
163    };
164    struct overlay *over;
165
166    over = overlay_add(&ctx->pop_underlays, &ctx->aois,
167        pop_hover, NULL, ctx);
168    overlay_text_raw(over, "");
169    overlay_style(over, &style);
170
171    /*
172     * This makes it all work. When we receive a click while hovering, it
173     * goes to the hovering overlay if that overlay accepts clicks.
174     * However, if the overlay accepting the click is different, we first
175     * de-hover.
176     *
177     * Now, in the case of the frame overlay, dehovering would destroy the
178     * destination overlays right before trying to deliver the click.
179     *
180     * We solve this by declaring the frame overlay to be "related" to the
181     * destination overlays. This suppresses dehovering.
182     */
183    overlay_set_related_all(ctx->pop_overlays, over);
184}
185
186
187static bool hover_glabel(void *user, bool on, int dx, int dy)
188{
189    struct glabel_aoi_ctx *aoi_ctx = user;
190    struct gui_ctx *ctx = aoi_ctx->sheet->ctx;
191    const struct gui_sheet *curr_sheet = ctx->curr_sheet;
192    const struct dwg_bbox *bbox = &aoi_ctx->bbox;
193
194    if (!on) {
195        dehover_glabel(ctx);
196        return 1;
197    }
198    if (ctx->pop_underlays) {
199        if (ctx->pop_origin == aoi_ctx)
200            return 0;
201        dehover_glabel(ctx);
202    }
203
204    GtkAllocation alloc;
205    int sx, sy, ex, ey, mx, my;
206    unsigned n = 0;
207    struct gui_sheet *sheet;
208
209    ctx->glabel = aoi_ctx->obj->u.text.s;
210    ctx->pop_origin = aoi_ctx;
211
212    aoi_dehover();
213    overlay_remove_all(&ctx->pop_overlays);
214    overlay_remove_all(&ctx->pop_underlays);
215
216    add_dest_header(ctx, aoi_ctx->obj->u.text.s);
217    for (sheet = ctx->new_hist->sheets; sheet; sheet = sheet->next)
218        add_dest_overlay(ctx, aoi_ctx->obj->u.text.s, sheet, ++n);
219    add_dest_frame(ctx);
220
221    eeschema_coord(ctx,
222        bbox->x - curr_sheet->xmin, bbox->y - curr_sheet->ymin,
223        &sx, &sy);
224    eeschema_coord(ctx, bbox->x + bbox->w - curr_sheet->xmin,
225        bbox->y + bbox->h - curr_sheet->ymin, &ex, &ey);
226
227    gtk_widget_get_allocation(ctx->da, &alloc);
228    mx = (sx + ex) / 2;
229    my = (sy + ey) / 2;
230    if (mx < alloc.width / 2) {
231        ctx->pop_x = sx - CHEAT;
232        ctx->pop_dx = 1;
233    } else {
234        ctx->pop_x = ex + CHEAT;
235        ctx->pop_dx = -1;
236    }
237    if (my < alloc.height / 2) {
238        ctx->pop_y = sy - CHEAT;
239        ctx->pop_dy = 1;
240    } else {
241        ctx->pop_y = ey + CHEAT;
242        ctx->pop_dy = -1;
243    }
244
245    /*
246     * @@@ The idea is to get input to trigger hovering over the pop-up.
247     * However, this doesn't work because the overlay has not been drawn
248     * yet and therefore has not created its AoI. We therefore only get a
249     * chance to begin hovering at the next motion update, which may
250     * already be outside the pop-up.
251     *
252     * Probably the only way to fix this is by making overlay_add do the
253     * layout calculation and create the AoI immediately.
254     *
255     * Another problem occurs as deep zoom levels, when the label is larger
256     * than the pop-up. Then we can trigger pop-up creation from a location
257     * that will be outside the pop-up.
258     *
259     * We could fix this by aligning the pop-up with the mouse position
260     * instead the box, either in general, or in this specific case. Not
261     * sure if it's worth the trouble, though.
262     *
263     * Another way to avoid the problem would be to size the pop-up such
264     * that it always includes the mouse position. But that could lead to
265     * rather weird-looking results at deep high zoom levels.
266     *
267     * Yet another option would be to move the mouse pointer onto the
268     * pop-up. The problem with this is that forced mouse pointer movement
269     * is not appreciated by all users.
270     *
271     * Both issues result in a "hanging" pop-up because AoI (and input)
272     * don't even know we're hovering. The pop-up can be cleared by
273     * - hovering into it,
274     * - hovering over some other glabel,
275     * - clicking, or
276     * - pressing Escape.
277     */
278    input_update();
279    redraw(ctx);
280    return 0;
281}
282
283
284void add_glabel_aoi(struct gui_sheet *sheet, const struct sch_obj *obj)
285{
286    const struct dwg_bbox *bbox = &obj->u.text.bbox;
287    struct glabel_aoi_ctx *aoi_ctx = alloc_type(struct glabel_aoi_ctx);
288
289    struct aoi cfg = {
290        .x = bbox->x,
291        .y = bbox->y,
292        .w = bbox->w,
293        .h = bbox->h,
294        .hover = hover_glabel,
295        .user = aoi_ctx,
296    };
297
298    aoi_ctx->sheet = sheet;
299    aoi_ctx->obj = obj;
300    aoi_ctx->bbox = *bbox;
301
302    aoi_add(&sheet->aois, &cfg);
303}
304

Archive Download this file

Branches:
master



interactive