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