Debian Codegraph @

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