Debian Codegraph @

lib/glcomp/glcompset.c

raw
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
25 static float startX, startY;
26
27 static 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
35 static glCompObj *glCompGetObjByMouse(glCompSet s, const glCompMouse m) {
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
49 static 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;
55 if (o->base.common.callbacks.mouseover) {
56 o->base.common.callbacks.mouseover(obj, x, y);
57 }
58 }
59
60 static void glCompSetMouseClick(void *obj, float x, float y,
61 glMouseButtonType t)
62 {
63 glCompObj *o = obj;
64 if (o->common.callbacks.click) {
65 o->common.callbacks.click(obj, x, y, t);
66 }
67 }
68
69 static void glCompSetMouseDown(void *obj, float x, float y,
70 glMouseButtonType t)
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;
77 o->mouse.clickedObj = glCompGetObjByMouse(*o->base.common.compset,
78 o->base.common.compset->mouse);
79 if (o->mouse.clickedObj)
80 if (o->mouse.clickedObj->common.functions.mousedown)
81 o->mouse.clickedObj->common.functions.mousedown(o->mouse.clickedObj, x, y, t);
82 }
83 o->mouse.down = true;
84 startX = x;
85 startY = o->base.common.height - y;
86 }
87
88 static 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 }
106 if (ob->base.common.callbacks.mouseup)
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
113 void glCompInitCommon(glCompObj *childObj, glCompObj *parentObj, float x,
114 float y) {
115 glCompCommon *c;
116 glCompCommon *parent;
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;
129 c->height = GLCOMP_DEFAULT_HEIGHT;;
130 c->width = GLCOMP_DEFAULT_WIDTH;
131 c->visible = 1;
132 c->pos.x = x;
133 c->pos.y = y;
134 c->borderWidth = GLCOMPSET_BORDERWIDTH;
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;
158 c->color.R = GLCOMPSET_PANEL_COLOR_R;
159 c->color.G = GLCOMPSET_PANEL_COLOR_G;
160 c->color.B = GLCOMPSET_PANEL_COLOR_B;
161 c->color.A = GLCOMPSET_PANEL_COLOR_ALPHA;
162 c->layer = 0;
163 c->pos.z = 0;
164 }
165 c->font = glNewFontFromParent(childObj, NULL);
166 }
167
168 void glCompEmptyCommon(glCompCommon * c)
169 {
170 glDeleteFont(&c->font);
171 }
172
173 glCompSet *glCompSetNew(int w, int h)
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
193 void glCompSetAddObj(glCompSet * s, glCompObj * obj)
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
201 void 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
221 void 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
230 void glCompSetDraw(glCompSet *s) {
231 glCompDrawBegin();
232 for (size_t ind = 0; ind < s->objcnt; ind++) {
233 s->obj[ind]->common.functions.draw(s->obj[ind]);
234 }
235 glCompDrawEnd();
236 }
237
238 void 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 }