Graphviz 12.0.1~dev.20240716.0800
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 <cgraph/alloc.h>
12#include <glcomp/glcompset.h>
13#include <glcomp/glcomppanel.h>
14#include <glcomp/glcomplabel.h>
15#include <glcomp/glcompbutton.h>
16#include <glcomp/glcompmouse.h>
17
18#include <glcomp/glutils.h>
19#include <stdlib.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->pos.x, m->pos.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 *) obj)->mouse.pos.x = x;
50 ((glCompSet *) obj)->mouse.pos.y =
51 ((glCompObj *) obj)->common.height - y;
52 ((glCompSet *) obj)->mouse.pos.z = 0;
53 ((glCompSet *) obj)->mouse.dragY =
54 ((glCompSet *) obj)->mouse.pos.y - startY;
55 ((glCompSet *) obj)->mouse.dragX =
56 ((glCompSet *) obj)->mouse.pos.x - startX;
57 if (((glCompSet *) obj)->common.callbacks.mouseover)
58 ((glCompSet *) obj)->common.callbacks.mouseover(obj, x, y);
59}
60
61static void glCompSetMouseClick(void *obj, float x, float y,
63{
64 if (((glCompSet *) obj)->common.callbacks.click)
65 ((glCompSet *) obj)->common.callbacks.click(obj, x, y, t);
66}
67
68static void glCompSetMouseDown(void *obj, float x, float y,
70{
71 ((glCompSet *) obj)->mouse.t = t;
72 if (t == glMouseLeftButton) {
73 ((glCompSet *) obj)->mouse.pos.x = x;
74 ((glCompSet *) obj)->mouse.pos.y =
75 ((glCompObj *) obj)->common.height - y;
76 ((glCompSet *) obj)->mouse.pos.z = 0;
77 ((glCompSet *) obj)->mouse.clickedObj =
78 glCompGetObjByMouse(((glCompObj *) obj)->common.compset,
79 &((glCompSet *) (((glCompObj *) obj)->
80 common.compset))->mouse);
81 if (((glCompSet *) obj)->mouse.clickedObj)
82 if (((glCompSet *) obj)->mouse.clickedObj->common.functions.
83 mousedown)
84 ((glCompSet *) obj)->mouse.clickedObj->common.functions.
85 mousedown(((glCompSet *) obj)->mouse.clickedObj, x, y,
86 t);
87 }
88 ((glCompSet *) obj)->mouse.down = 1;
89 startX = x;
90 startY = ((glCompObj *) obj)->common.height - y;
91 if (((glCompSet *) obj)->common.callbacks.mousedown)
92 ((glCompSet *) obj)->common.callbacks.mousedown(obj, x, y, t);
93}
94
95static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t) {
96 float tempX = x;
97 float tempY = ((glCompObj *) obj)->common.height - y;
98
99 ((glCompSet *) obj)->mouse.down = 0;
100 if (t == glMouseLeftButton) {
101 glCompObj *o = NULL;
102 glCompObj *o_clicked = ((glCompSet *) obj)->mouse.clickedObj;
103 ((glCompSet *) obj)->mouse.pos.x = tempX;
104 ((glCompSet *) obj)->mouse.pos.y = tempY;
105 ((glCompSet *) obj)->mouse.pos.z = 0;
106 if (o_clicked)
107 o = glCompGetObjByMouse(obj, &((glCompSet *) obj)->mouse);
108 if (!o)
109 return;
110 if (o == o_clicked)
111 o->common.functions.click(o, x, y, t);
112 }
113 if (((glCompSet *) obj)->common.callbacks.mouseup)
114 ((glCompSet *) obj)->common.callbacks.mouseup(obj, x, y, t);
115 /*check if mouse is clicked or dragged */
116 if (startX == (int)tempX && startY == tempY)
117 glCompSetMouseClick(obj, x, y, t);
118}
119
120void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x,
121 float y) {
122 glCompCommon *c;
124 c = &childObj->common;
125 c->align = glAlignNone;
126 c->anchor.bottom = 0;
127 c->anchor.left = 0;
128 c->anchor.top = 0;
129 c->anchor.right = 0;
130 c->anchor.leftAnchor = 0;
131 c->anchor.rightAnchor = 0;
132 c->anchor.topAnchor = 0;
133 c->anchor.bottomAnchor = 0;
134 c->data = 0;
135 c->enabled = 1;
138 c->visible = 1;
139 c->pos.x = x;
140 c->pos.y = y;
142
143 /*NULL function pointers */
144 childObj->common.callbacks.click = NULL;
145 childObj->common.callbacks.doubleclick = NULL;
146 childObj->common.callbacks.draw = NULL;
147 childObj->common.callbacks.mousedown = NULL;
148 childObj->common.callbacks.mousein = NULL;
149 childObj->common.callbacks.mouseout = NULL;
150 childObj->common.callbacks.mouseover = NULL;
151 childObj->common.callbacks.mouseup = NULL;
152
153 childObj->common.functions.click = NULL;
154 childObj->common.functions.doubleclick = NULL;
155 childObj->common.functions.draw = NULL;
156 childObj->common.functions.mousedown = NULL;
157 childObj->common.functions.mousein = NULL;
158 childObj->common.functions.mouseout = NULL;
159 childObj->common.functions.mouseover = NULL;
160 childObj->common.functions.mouseup = NULL;
161
162
163
164 if (parentObj) {
165 c->parent = &parentObj->common;
166 parent = &parentObj->common;
167 c->color = parent->color;
168 c->layer = parent->layer + 1;
169 c->pos.z = parent->pos.z;
170 glCompSetAddObj(parent->compset, childObj);
171 } else {
172 c->parent = NULL;
177 c->layer = 0;
178 c->pos.z = 0;
179 }
180 c->font = glNewFontFromParent(childObj, NULL);
181}
182
184{
185 glDeleteFont(c->font);
186}
187
189{
190 glCompSet *s = gv_alloc(sizeof(glCompSet));
191 glCompInitCommon((glCompObj *) s, NULL, 0.0f, 0.0f);
192 s->common.width = (float)w;
193 s->common.height = (float)h;
194 s->objcnt = 0;
195 s->obj = NULL;
196 s->textureCount = 0;
197 s->textures = NULL;
198 s->common.font = glNewFontFromParent((glCompObj *) s, NULL);
199 s->common.compset = s;
200 s->common.functions.mouseover = (glcompmouseoverfunc_t)glCompMouseMove;
201 s->common.functions.mousedown = (glcompmousedownfunc_t)glCompSetMouseDown;
202 s->common.functions.mouseup = (glcompmouseupfunc_t)glCompSetMouseUp;
203 glCompMouseInit(&s->mouse);
204 return s;
205}
206
208{
209 s->obj = gv_recalloc(s->obj, s->objcnt, s->objcnt + 1, sizeof(glCompObj*));
210 s->objcnt++;
211 s->obj[s->objcnt - 1] = obj;
212 obj->common.compset = s;
213}
214
215void glCompDrawBegin(void) //pushes a gl stack
216{
217 int vPort[4];
218
219 glGetIntegerv(GL_VIEWPORT, vPort);
220
221 glMatrixMode(GL_PROJECTION);
222 glPushMatrix();
223 glLoadIdentity();
224
225
226 glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
227 glMatrixMode(GL_MODELVIEW);
228 glEnable(GL_BLEND);
229
230 glPushMatrix();
231 glLoadIdentity();
232 glDisable(GL_DEPTH_TEST);
233}
234
235void glCompDrawEnd(void) //pops the gl stack
236{
237 glMatrixMode(GL_PROJECTION);
238 glPopMatrix();
239 glMatrixMode(GL_MODELVIEW);
240 glPopMatrix();
241 glEnable(GL_DEPTH_TEST);
242}
243
246 for (size_t ind = 0; ind < s->objcnt; ind++) {
247 s->obj[ind]->common.functions.draw(s->obj[ind]);
248 }
250}
251
252void glcompsetUpdateBorder(glCompSet * s, int w, int h)
253{
254 if (w > 0 && h > 0) {
255 s->common.width = (float)w;
256 s->common.height = (float)h;
257 }
258}
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:78
#define GLCOMPSET_BORDERWIDTH
Definition glcompdefs.h:73
#define GLCOMP_DEFAULT_HEIGHT
Definition glcompdefs.h:78
void(* glcompmouseoverfunc_t)(glCompObj *obj, float x, float y)
Definition glcompdefs.h:106
void(* glcompmouseupfunc_t)(glCompObj *obj, float x, float y, glMouseButtonType t)
Definition glcompdefs.h:114
#define GLCOMP_DEFAULT_WIDTH
Definition glcompdefs.h:77
void(* glcompmousedownfunc_t)(glCompObj *obj, float x, float y, glMouseButtonType t)
Definition glcompdefs.h:112
#define GLCOMPSET_PANEL_COLOR_B
Definition glcompdefs.h:34
#define GLCOMPSET_PANEL_COLOR_G
Definition glcompdefs.h:33
#define GLCOMPSET_PANEL_COLOR_R
Definition glcompdefs.h:32
#define GLCOMPSET_PANEL_COLOR_ALPHA
Definition glcompdefs.h:35
glMouseButtonType
Definition glcompdefs.h:89
@ glMouseLeftButton
Definition glcompdefs.h:89
@ glAlignNone
Definition glcompdefs.h:80
glCompFont * glNewFontFromParent(glCompObj *o, char *text)
Definition glcompfont.c:75
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:120
void glCompEmptyCommon(glCompCommon *c)
Definition glcompset.c:183
static glCompObj * glCompGetObjByMouse(glCompSet *s, glCompMouse *m)
Definition glcompset.c:31
void glCompDrawEnd(void)
Definition glcompset.c:235
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:244
void glCompSetAddObj(glCompSet *s, glCompObj *obj)
Definition glcompset.c:207
void glCompDrawBegin(void)
Definition glcompset.c:215
glCompSet * glCompSetNew(int w, int h)
Definition glcompset.c:188
static void glCompSetMouseUp(void *obj, float x, float y, glMouseButtonType t)
Definition glcompset.c:95
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:61
void glcompsetUpdateBorder(glCompSet *s, int w, int h)
Definition glcompset.c:252
node NULL
Definition grammar.y:149
glCompCommon common
Definition glcompdefs.h:274
glcompdoubleclickfunc_t doubleclick
Definition glcompdefs.h:192
glcompmouseoutfunc_t mouseout
Definition glcompdefs.h:195
glcompmouseupfunc_t mouseup
Definition glcompdefs.h:197
glcompclickfunc_t click
Definition glcompdefs.h:191
glcompmouseinfunc_t mousein
Definition glcompdefs.h:194
glcompmousedownfunc_t mousedown
Definition glcompdefs.h:196
glcompdrawfunc_t draw
Definition glcompdefs.h:190
glcompmouseoverfunc_t mouseover
Definition glcompdefs.h:193
glCompColor color
Definition glcompdefs.h:212
glCompCallBacks callbacks
Definition glcompdefs.h:222
glCompCallBacks functions
Definition glcompdefs.h:223
glCompPoint pos
Definition glcompdefs.h:208
glCompFont * font
Definition glcompdefs.h:218
glCompAnchor anchor
Definition glcompdefs.h:220
glCompAlignment align
Definition glcompdefs.h:219
void * parent
Definition glcompdefs.h:216
glCompPoint refPos
Definition glcompdefs.h:209
void * compset
Definition glcompdefs.h:215
float borderWidth
Definition glcompdefs.h:211
glCompPoint pos
Definition glcompdefs.h:279
Definition grammar.c:93