glcompset.c 7.1 KB

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