glcompbutton.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/glcompbutton.h>
  11. #include <glcomp/glcomplabel.h>
  12. #include <glcomp/glcompimage.h>
  13. #include <glcomp/glcompfont.h>
  14. #include <glcomp/glutils.h>
  15. #include <glcomp/glcompset.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <GL/glut.h>
  20. #include <util/alloc.h>
  21. glCompButton *glCompButtonNew(void *par, float x, float y, float w, float h,
  22. char *caption) {
  23. glCompButton *p = gv_alloc(sizeof(glCompButton));
  24. glCompInitCommon(&p->base, par, x, y);
  25. /*customize button color */
  26. p->base.common.color.R = GLCOMPSET_BUTTON_COLOR_R;
  27. p->base.common.color.G = GLCOMPSET_BUTTON_COLOR_G;
  28. p->base.common.color.B = GLCOMPSET_BUTTON_COLOR_B;
  29. p->base.common.color.A = GLCOMPSET_BUTTON_COLOR_ALPHA;
  30. p->base.common.borderWidth = GLCOMPSET_BUTTON_BEVEL;
  31. p->base.common.width = w;
  32. p->base.common.height = h;
  33. p->status = false; // false not pressed, true pressed
  34. p->groupid = 0;
  35. p->base.common.callbacks.click = NULL;
  36. /*set event functions */
  37. p->base.common.functions.draw = (glcompdrawfunc_t)glCompButtonDraw;
  38. p->base.common.functions.click = glCompButtonClick;
  39. p->base.common.functions.doubleclick = glCompButtonDoubleClick;
  40. p->base.common.functions.mousedown = glCompButtonMouseDown;
  41. p->base.common.functions.mousein = glCompButtonMouseIn;
  42. p->base.common.functions.mouseout = glCompButtonMouseOut;
  43. p->base.common.functions.mouseover = glCompButtonMouseOver;
  44. p->base.common.functions.mouseup = glCompButtonMouseUp;
  45. /*caption */
  46. glDeleteFont(&p->base.common.font);
  47. p->base.common.font = glNewFontFromParent(&p->base, NULL);
  48. p->label = glCompLabelNew(p, caption);
  49. p->label->base.common.font.justify.VJustify = glFontVJustifyCenter;
  50. p->label->base.common.font.justify.HJustify = glFontHJustifyCenter;
  51. p->label->base.common.align = glAlignParent;
  52. /*image */
  53. p->image = NULL;
  54. return p;
  55. }
  56. int glCompButtonAddPngGlyph(glCompButton *b, const char *fileName) {
  57. int rv;
  58. /*delete if there is an existing image */
  59. if (b->image)
  60. glCompImageDelete(b->image);
  61. /*image on left for now */
  62. b->image = glCompImageNew(b, 0, 0);
  63. rv = glCompImageLoadPng(b->image, fileName);
  64. if (rv) {
  65. b->image->base.common.anchor.leftAnchor = 1;
  66. b->image->base.common.anchor.left = 0;
  67. b->image->base.common.anchor.topAnchor = 1;
  68. b->image->base.common.anchor.top = 0;
  69. b->image->base.common.anchor.bottomAnchor = 1;
  70. b->image->base.common.anchor.bottom = 0;
  71. b->label->base.common.anchor.leftAnchor = 1;
  72. b->label->base.common.anchor.left = b->image->base.common.width;
  73. b->label->base.common.anchor.rightAnchor = 1;
  74. b->label->base.common.anchor.right = 0;
  75. b->label->base.common.anchor.topAnchor = 1;
  76. b->label->base.common.anchor.top = 0;
  77. b->label->base.common.anchor.bottomAnchor = 1;
  78. b->label->base.common.anchor.bottom = 0;
  79. b->label->base.common.align = glAlignNone;
  80. }
  81. return rv;
  82. }
  83. void glCompButtonHide(glCompButton * p)
  84. {
  85. p->base.common.visible = 0;
  86. if (p->label)
  87. p->label->base.common.visible = 0;
  88. if (p->image)
  89. p->image->base.common.visible = 0;
  90. }
  91. void glCompButtonShow(glCompButton * p)
  92. {
  93. p->base.common.visible = 1;
  94. if (p->label)
  95. p->label->base.common.visible = 1;
  96. if (p->image)
  97. p->image->base.common.visible = 1;
  98. }
  99. void glCompButtonDraw(glCompButton * p)
  100. {
  101. glCompCommon ref = p->base.common;
  102. glCompCalcWidget(p->base.common.parent, &p->base.common, &ref);
  103. if (!p->base.common.visible)
  104. return;
  105. /*draw panel */
  106. glCompDrawRectPrism(&(ref.pos), ref.width, ref.height,
  107. p->base.common.borderWidth, 0.01f, &ref.color,
  108. !p->status);
  109. if (p->label)
  110. p->label->base.common.functions.draw(p->label);
  111. if (p->image)
  112. p->image->base.common.functions.draw(p->image);
  113. if (p->base.common.callbacks.draw)
  114. p->base.common.callbacks.draw(p); // user defined drawing routines are called here
  115. }
  116. void glCompButtonClick(glCompObj *o, float x, float y, glMouseButtonType t) {
  117. glCompButton *p = (glCompButton *) o;
  118. ((glCompButton *) o)->status=((glCompButton *) o)->refStatus ;
  119. if (p->groupid == -1) {
  120. p->status = !p->status;
  121. } else {
  122. p->status = false;
  123. }
  124. if (p->base.common.callbacks.click)
  125. p->base.common.callbacks.click(&p->base, x, y, t);
  126. }
  127. void glCompButtonDoubleClick(glCompObj *obj, float x, float y,
  128. glMouseButtonType t)
  129. {
  130. if (obj->common.callbacks.doubleclick)
  131. obj->common.callbacks.doubleclick(obj, x, y, t);
  132. }
  133. void glCompButtonMouseDown(glCompObj *obj, float x, float y,
  134. glMouseButtonType t)
  135. {
  136. /*Put your internal code here */
  137. ((glCompButton *) obj)->refStatus = ((glCompButton *) obj)->status;
  138. ((glCompButton *) obj)->status = true;
  139. if (obj->common.callbacks.mousedown)
  140. obj->common.callbacks.mousedown(obj, x, y, t);
  141. }
  142. void glCompButtonMouseIn(glCompObj *obj, float x, float y) {
  143. if (obj->common.callbacks.mousein)
  144. obj->common.callbacks.mousein(obj, x, y);
  145. }
  146. void glCompButtonMouseOut(glCompObj *obj, float x, float y) {
  147. if (obj->common.callbacks.mouseout)
  148. obj->common.callbacks.mouseout(obj, x, y);
  149. }
  150. void glCompButtonMouseOver(glCompObj *obj, float x, float y) {
  151. if (obj->common.callbacks.mouseover)
  152. obj->common.callbacks.mouseover(obj, x, y);
  153. }
  154. void glCompButtonMouseUp(glCompObj *obj, float x, float y, glMouseButtonType t)
  155. {
  156. if (obj->common.callbacks.mouseup)
  157. obj->common.callbacks.mouseup(obj, x, y, t);
  158. }