Graphviz 13.1.2~dev.20250726.0945
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 <glcomp/glcompset.h>
12#include <glcomp/glcomppanel.h>
13#include <glcomp/glcomplabel.h>
14#include <glcomp/glcompbutton.h>
15#include <glcomp/glcompmouse.h>
16
17#include <glcomp/glutils.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <stdlib.h>
21#include <util/alloc.h>
22
23static float startX, startY;
24
25static int glCompPointInObject(glCompObj * p, float x, float y)
26{
27 return x > p->common.refPos.x
28 && x < p->common.refPos.x + p->common.width
29 && y > p->common.refPos.y
30 && y < p->common.refPos.y + p->common.height;
31}
32
34 glCompObj *rv = NULL;
35 for (size_t ind = 0; ind < s.objcnt; ind++) {
36 if (s.obj[ind]->common.visible && glCompPointInObject(s.obj[ind], m.x, m.y)) {
37 if (!rv || s.obj[ind]->common.layer >= rv->common.layer) {
38 if (s.obj[ind]->common.functions.click)
39 rv = s.obj[ind];
40 }
41 }
42 }
43
44 return rv;
45}
46
47static void glCompMouseMove(glCompObj *obj, float x, float y) {
48 glCompSet *o = (glCompSet *)((char *)obj - offsetof(glCompSet, base));
49 o->mouse.x = x;
50 o->mouse.y = o->base.common.height - y;
51 o->mouse.dragY = o->mouse.y - startY;
52 o->mouse.dragX = o->mouse.x - startX;
54 o->base.common.callbacks.mouseover(obj, x, y);
55 }
56}
57
58static void glCompSetMouseClick(void *obj, float x, float y,
60{
61 glCompObj *o = obj;
62 if (o->common.callbacks.click) {
63 o->common.callbacks.click(obj, x, y, t);
64 }
65}
66
67static void glCompSetMouseDown(void *obj, float x, float y,
69{
70 glCompSet *o = obj;
71 o->mouse.t = t;
72 if (t == glMouseLeftButton) {
73 o->mouse.x = x;
74 o->mouse.y = o->base.common.height - y;
77 if (o->mouse.clickedObj)
80 }
81 o->mouse.down = true;
82 startX = x;
83 startY = o->base.common.height - y;
84}
85
86static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t) {
87 float tempX = x;
88 glCompSet *ob = obj;
89 float tempY = ob->base.common.height - y;
90
91 ob->mouse.down = false;
92 if (t == glMouseLeftButton) {
93 glCompObj *o = NULL;
94 glCompObj *o_clicked = ob->mouse.clickedObj;
95 ob->mouse.x = tempX;
96 ob->mouse.y = tempY;
97 if (o_clicked)
98 o = glCompGetObjByMouse(*ob, ob->mouse);
99 if (!o)
100 return;
101 if (o == o_clicked)
102 o->common.functions.click(o, x, y, t);
103 }
105 ob->base.common.callbacks.mouseup(obj, x, y, t);
106 /*check if mouse is clicked or dragged */
107 if (startX == (int)tempX && startY == tempY)
108 glCompSetMouseClick(obj, x, y, t);
109}
110
111void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x,
112 float y) {
113 glCompCommon *c;
115 c = &childObj->common;
116 c->align = glAlignNone;
117 c->anchor.bottom = 0;
118 c->anchor.left = 0;
119 c->anchor.top = 0;
120 c->anchor.right = 0;
121 c->anchor.leftAnchor = 0;
122 c->anchor.rightAnchor = 0;
123 c->anchor.topAnchor = 0;
124 c->anchor.bottomAnchor = 0;
125 c->data = 0;
126 c->enabled = 1;
129 c->visible = 1;
130 c->pos.x = x;
131 c->pos.y = y;
133
134 /*NULL function pointers */
135 childObj->common.callbacks.click = NULL;
136 childObj->common.callbacks.mouseover = NULL;
137 childObj->common.callbacks.mouseup = NULL;
138
139 childObj->common.functions.click = NULL;
140 childObj->common.functions.draw = NULL;
141 childObj->common.functions.mousedown = NULL;
142 childObj->common.functions.mouseover = NULL;
143 childObj->common.functions.mouseup = NULL;
144
145
146
147 if (parentObj) {
148 c->parent = &parentObj->common;
149 parent = &parentObj->common;
150 c->color = parent->color;
151 c->layer = parent->layer + 1;
152 c->pos.z = parent->pos.z;
153 glCompSetAddObj(parent->compset, childObj);
154 } else {
155 c->parent = NULL;
160 c->layer = 0;
161 c->pos.z = 0;
162 }
163 c->font = glNewFontFromParent(childObj, NULL);
164}
165
167{
168 glDeleteFont(&c->font);
169}
170
172{
173 glCompSet *s = gv_alloc(sizeof(glCompSet));
174 glCompInitCommon(&s->base, NULL, 0.0f, 0.0f);
175 s->base.common.width = (float)w;
176 s->base.common.height = (float)h;
177 s->objcnt = 0;
178 s->obj = NULL;
179 s->textureCount = 0;
180 s->textures = NULL;
181 glDeleteFont(&s->base.common.font);
182 s->base.common.font = glNewFontFromParent(&s->base, NULL);
183 s->base.common.compset = s;
184 s->base.common.functions.mouseover = glCompMouseMove;
185 s->base.common.functions.mousedown = (glcompmousedownfunc_t)glCompSetMouseDown;
186 s->base.common.functions.mouseup = (glcompmouseupfunc_t)glCompSetMouseUp;
187 glCompMouseInit(&s->mouse);
188 return s;
189}
190
192{
193 s->obj = gv_recalloc(s->obj, s->objcnt, s->objcnt + 1, sizeof(glCompObj*));
194 s->objcnt++;
195 s->obj[s->objcnt - 1] = obj;
196 obj->common.compset = s;
197}
198
199void glCompDrawBegin(void) //pushes a gl stack
200{
201 int vPort[4];
202
203 glGetIntegerv(GL_VIEWPORT, vPort);
204
205 glMatrixMode(GL_PROJECTION);
206 glPushMatrix();
207 glLoadIdentity();
208
209
210 glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
211 glMatrixMode(GL_MODELVIEW);
212 glEnable(GL_BLEND);
213
214 glPushMatrix();
215 glLoadIdentity();
216 glDisable(GL_DEPTH_TEST);
217}
218
219void glCompDrawEnd(void) //pops the gl stack
220{
221 glMatrixMode(GL_PROJECTION);
222 glPopMatrix();
223 glMatrixMode(GL_MODELVIEW);
224 glPopMatrix();
225 glEnable(GL_DEPTH_TEST);
226}
227
230 for (size_t ind = 0; ind < s->objcnt; ind++) {
231 s->obj[ind]->common.functions.draw(s->obj[ind]);
232 }
234}
235
236void glcompsetUpdateBorder(glCompSet * s, int w, int h)
237{
238 if (w > 0 && h > 0) {
239 s->base.common.width = (float)w;
240 s->base.common.height = (float)h;
241 }
242}
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:80
#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:70
void glDeleteFont(glCompFont *f)
Definition glcompfont.c:38
void glCompMouseInit(glCompMouse *m)
Definition glcompmouse.c:17
static int glCompPointInObject(glCompObj *p, float x, float y)
Definition glcompset.c:25
void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x, float y)
Definition glcompset.c:111
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:166
void glCompDrawEnd(void)
Definition glcompset.c:219
static glCompObj * glCompGetObjByMouse(glCompSet s, const glCompMouse m)
Definition glcompset.c:33
static float startX
Definition glcompset.c:23
static void glCompMouseMove(glCompObj *obj, float x, float y)
Definition glcompset.c:47
void glCompSetDraw(glCompSet *s)
Definition glcompset.c:228
void glCompSetAddObj(glCompSet *s, glCompObj *obj)
Definition glcompset.c:191
void glCompDrawBegin(void)
Definition glcompset.c:199
glCompSet * glCompSetNew(int w, int h)
Definition glcompset.c:171
static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:86
static void glCompSetMouseDown(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:67
static float startY
Definition glcompset.c:23
static void glCompSetMouseClick(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:58
void glcompsetUpdateBorder(glCompSet *s, int w, int h)
Definition glcompset.c:236
node NULL
Definition grammar.y:180
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:89