Graphviz 14.1.3~dev.20260126.0926
Loading...
Searching...
No Matches
glcompset.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <glcomp/glcompset.h>
14#include <glcomp/glcomppanel.h>
15#include <glcomp/glcomplabel.h>
16#include <glcomp/glcompbutton.h>
17#include <glcomp/glcompmouse.h>
18
19#include <glcomp/glutils.h>
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdlib.h>
23#include <util/alloc.h>
24
25static float startX, startY;
26
27static int glCompPointInObject(glCompObj * p, float x, float y)
28{
29 return x > p->common.refPos.x
30 && x < p->common.refPos.x + p->common.width
31 && y > p->common.refPos.y
32 && y < p->common.refPos.y + p->common.height;
33}
34
36 glCompObj *rv = NULL;
37 for (size_t ind = 0; ind < s.objcnt; ind++) {
38 if (s.obj[ind]->common.visible && glCompPointInObject(s.obj[ind], m.x, m.y)) {
39 if (!rv || s.obj[ind]->common.layer >= rv->common.layer) {
40 if (s.obj[ind]->common.functions.click)
41 rv = s.obj[ind];
42 }
43 }
44 }
45
46 return rv;
47}
48
49static void glCompMouseMove(glCompObj *obj, float x, float y) {
50 glCompSet *o = (glCompSet *)((char *)obj - offsetof(glCompSet, base));
51 o->mouse.x = x;
52 o->mouse.y = o->base.common.height - y;
53 o->mouse.dragY = o->mouse.y - startY;
54 o->mouse.dragX = o->mouse.x - startX;
56 o->base.common.callbacks.mouseover(obj, x, y);
57 }
58}
59
60static void glCompSetMouseClick(void *obj, float x, float y,
62{
63 glCompObj *o = obj;
64 if (o->common.callbacks.click) {
65 o->common.callbacks.click(obj, x, y, t);
66 }
67}
68
69static void glCompSetMouseDown(void *obj, float x, float y,
71{
72 glCompSet *o = obj;
73 o->mouse.t = t;
74 if (t == glMouseLeftButton) {
75 o->mouse.x = x;
76 o->mouse.y = o->base.common.height - y;
79 if (o->mouse.clickedObj)
82 }
83 o->mouse.down = true;
84 startX = x;
85 startY = o->base.common.height - y;
86}
87
88static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t) {
89 float tempX = x;
90 glCompSet *ob = obj;
91 float tempY = ob->base.common.height - y;
92
93 ob->mouse.down = false;
94 if (t == glMouseLeftButton) {
95 glCompObj *o = NULL;
96 glCompObj *o_clicked = ob->mouse.clickedObj;
97 ob->mouse.x = tempX;
98 ob->mouse.y = tempY;
99 if (o_clicked)
100 o = glCompGetObjByMouse(*ob, ob->mouse);
101 if (!o)
102 return;
103 if (o == o_clicked)
104 o->common.functions.click(o, x, y, t);
105 }
107 ob->base.common.callbacks.mouseup(obj, x, y, t);
108 /*check if mouse is clicked or dragged */
109 if (startX == (int)tempX && startY == tempY)
110 glCompSetMouseClick(obj, x, y, t);
111}
112
113void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x,
114 float y) {
115 glCompCommon *c;
117 c = &childObj->common;
118 c->align = glAlignNone;
119 c->anchor.bottom = 0;
120 c->anchor.left = 0;
121 c->anchor.top = 0;
122 c->anchor.right = 0;
123 c->anchor.leftAnchor = 0;
124 c->anchor.rightAnchor = 0;
125 c->anchor.topAnchor = 0;
126 c->anchor.bottomAnchor = 0;
127 c->data = 0;
128 c->enabled = 1;
131 c->visible = 1;
132 c->pos.x = x;
133 c->pos.y = y;
135
136 /*NULL function pointers */
137 childObj->common.callbacks.click = NULL;
138 childObj->common.callbacks.mouseover = NULL;
139 childObj->common.callbacks.mouseup = NULL;
140
141 childObj->common.functions.click = NULL;
142 childObj->common.functions.draw = NULL;
143 childObj->common.functions.mousedown = NULL;
144 childObj->common.functions.mouseover = NULL;
145 childObj->common.functions.mouseup = NULL;
146
147
148
149 if (parentObj) {
150 c->parent = &parentObj->common;
151 parent = &parentObj->common;
152 c->color = parent->color;
153 c->layer = parent->layer + 1;
154 c->pos.z = parent->pos.z;
155 glCompSetAddObj(parent->compset, childObj);
156 } else {
157 c->parent = NULL;
162 c->layer = 0;
163 c->pos.z = 0;
164 }
165 c->font = glNewFontFromParent(childObj, NULL);
166}
167
169{
170 glDeleteFont(&c->font);
171}
172
174{
175 glCompSet *s = gv_alloc(sizeof(glCompSet));
176 glCompInitCommon(&s->base, NULL, 0.0f, 0.0f);
177 s->base.common.width = (float)w;
178 s->base.common.height = (float)h;
179 s->objcnt = 0;
180 s->obj = NULL;
181 s->textureCount = 0;
182 s->textures = NULL;
183 glDeleteFont(&s->base.common.font);
184 s->base.common.font = glNewFontFromParent(&s->base, NULL);
185 s->base.common.compset = s;
186 s->base.common.functions.mouseover = glCompMouseMove;
187 s->base.common.functions.mousedown = (glcompmousedownfunc_t)glCompSetMouseDown;
188 s->base.common.functions.mouseup = (glcompmouseupfunc_t)glCompSetMouseUp;
189 glCompMouseInit(&s->mouse);
190 return s;
191}
192
194{
195 s->obj = gv_recalloc(s->obj, s->objcnt, s->objcnt + 1, sizeof(glCompObj*));
196 s->objcnt++;
197 s->obj[s->objcnt - 1] = obj;
198 obj->common.compset = s;
199}
200
201void glCompDrawBegin(void) //pushes a gl stack
202{
203 int vPort[4];
204
205 glGetIntegerv(GL_VIEWPORT, vPort);
206
207 glMatrixMode(GL_PROJECTION);
208 glPushMatrix();
209 glLoadIdentity();
210
211
212 glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
213 glMatrixMode(GL_MODELVIEW);
214 glEnable(GL_BLEND);
215
216 glPushMatrix();
217 glLoadIdentity();
218 glDisable(GL_DEPTH_TEST);
219}
220
221void glCompDrawEnd(void) //pops the gl stack
222{
223 glMatrixMode(GL_PROJECTION);
224 glPopMatrix();
225 glMatrixMode(GL_MODELVIEW);
226 glPopMatrix();
227 glEnable(GL_DEPTH_TEST);
228}
229
232 for (size_t ind = 0; ind < s->objcnt; ind++) {
233 s->obj[ind]->common.functions.draw(s->obj[ind]);
234 }
236}
237
238void glcompsetUpdateBorder(glCompSet * s, int w, int h)
239{
240 if (w > 0 && h > 0) {
241 s->base.common.width = (float)w;
242 s->base.common.height = (float)h;
243 }
244}
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define parent(i)
Definition closest.c:75
#define GLCOMPSET_BORDERWIDTH
Definition glcompdefs.h:53
#define GLCOMP_DEFAULT_HEIGHT
Definition glcompdefs.h:58
void(* glcompmouseupfunc_t)(glCompObj *obj, float x, float y, glMouseButtonType t)
Definition glcompdefs.h:82
#define GLCOMP_DEFAULT_WIDTH
Definition glcompdefs.h:57
void(* glcompmousedownfunc_t)(glCompObj *obj, float x, float y, glMouseButtonType t)
Definition glcompdefs.h:80
#define GLCOMPSET_PANEL_COLOR_B
Definition glcompdefs.h:35
#define GLCOMPSET_PANEL_COLOR_G
Definition glcompdefs.h:34
#define GLCOMPSET_PANEL_COLOR_R
Definition glcompdefs.h:33
#define GLCOMPSET_PANEL_COLOR_ALPHA
Definition glcompdefs.h:36
glMouseButtonType
Definition glcompdefs.h:67
@ glMouseLeftButton
Definition glcompdefs.h:67
@ glAlignNone
Definition glcompdefs.h:60
glCompFont glNewFontFromParent(glCompObj *o, const char *text)
Definition glcompfont.c:72
void glDeleteFont(glCompFont *f)
Definition glcompfont.c:40
void glCompMouseInit(glCompMouse *m)
Definition glcompmouse.c:19
static int glCompPointInObject(glCompObj *p, float x, float y)
Definition glcompset.c:27
void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x, float y)
Definition glcompset.c:113
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:168
void glCompDrawEnd(void)
Definition glcompset.c:221
static glCompObj * glCompGetObjByMouse(glCompSet s, const glCompMouse m)
Definition glcompset.c:35
static float startX
Definition glcompset.c:25
static void glCompMouseMove(glCompObj *obj, float x, float y)
Definition glcompset.c:49
void glCompSetDraw(glCompSet *s)
Definition glcompset.c:230
void glCompSetAddObj(glCompSet *s, glCompObj *obj)
Definition glcompset.c:193
void glCompDrawBegin(void)
Definition glcompset.c:201
glCompSet * glCompSetNew(int w, int h)
Definition glcompset.c:173
static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:88
static void glCompSetMouseDown(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:69
static float startY
Definition glcompset.c:25
static void glCompSetMouseClick(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:60
void glcompsetUpdateBorder(glCompSet *s, int w, int h)
Definition glcompset.c:238
node NULL
Definition grammar.y:181
glcompmouseupfunc_t mouseup
Definition glcompdefs.h:154
glcompclickfunc_t click
Definition glcompdefs.h:151
glcompmousedownfunc_t mousedown
Definition glcompdefs.h:153
glcompdrawfunc_t draw
Definition glcompdefs.h:150
glcompmouseoverfunc_t mouseover
Definition glcompdefs.h:152
glCompColor color
Definition glcompdefs.h:168
glCompCallBacks callbacks
Definition glcompdefs.h:178
glCompCallBacks functions
Definition glcompdefs.h:179
glCompPoint pos
Definition glcompdefs.h:164
glCompAnchor anchor
Definition glcompdefs.h:176
glCompAlignment align
Definition glcompdefs.h:175
void * parent
Definition glcompdefs.h:172
glCompFont font
font to use
Definition glcompdefs.h:174
glCompPoint refPos
Definition glcompdefs.h:165
glCompSet * compset
Definition glcompdefs.h:171
float borderWidth
Definition glcompdefs.h:167
glMouseButtonType t
Definition glcompdefs.h:223
glCompObj * clickedObj
Definition glcompdefs.h:230
float y
current mouse pos
Definition glcompdefs.h:224
object prototype
Definition glcompdefs.h:184
glCompCommon common
Definition glcompdefs.h:185
glCompObj base
Definition glcompdefs.h:240
glCompMouse mouse
Definition glcompdefs.h:245
Definition grammar.c:90