Restructure how we look for Read files slightly.
[fvwm.git] / modules / FvwmScript / Widgets / CheckBox.c
blob4c0978402bff5482021bd7269870e757c37093ba
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "config.h"
19 #include "libs/fvwmlib.h"
20 #include "libs/ColorUtils.h"
21 #include "libs/Graphics.h"
22 #include "Tools.h"
25 * Fonction pour CheckBox / functions for CheckBox
27 void InitCheckBox(struct XObj *xobj)
29 unsigned long mask;
30 XSetWindowAttributes Attr;
32 /* Enregistrement des couleurs et de la police / fonts and colors */
33 if (xobj->colorset >= 0) {
34 xobj->TabColor[fore] = Colorset[xobj->colorset].fg;
35 xobj->TabColor[back] = Colorset[xobj->colorset].bg;
36 xobj->TabColor[hili] = Colorset[xobj->colorset].hilite;
37 xobj->TabColor[shad] = Colorset[xobj->colorset].shadow;
38 } else {
39 xobj->TabColor[fore] = GetColor(xobj->forecolor);
40 xobj->TabColor[back] = GetColor(xobj->backcolor);
41 xobj->TabColor[hili] = GetColor(xobj->hilicolor);
42 xobj->TabColor[shad] = GetColor(xobj->shadcolor);
45 mask = 0;
46 Attr.background_pixel = xobj->TabColor[back];
47 mask |= CWBackPixel;
48 Attr.cursor = XCreateFontCursor(dpy,XC_hand2);
49 mask |= CWCursor; /* Curseur pour la fenetre / Cursor for the window */
51 xobj->win = XCreateWindow(dpy, *xobj->ParentWin,
52 xobj->x, xobj->y, xobj->width, xobj->height,0,
53 CopyFromParent, InputOutput, CopyFromParent,
54 mask, &Attr);
55 xobj->gc = fvwmlib_XCreateGC(dpy,xobj->win,0,NULL);
56 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
58 if ((xobj->Ffont = FlocaleLoadFont(dpy, xobj->font, ScriptName)) == NULL)
60 fprintf(stderr, "%s: Couldn't load font. Exiting!\n", ScriptName);
61 exit(1);
63 if (xobj->Ffont->font != NULL)
64 XSetFont(dpy, xobj->gc, xobj->Ffont->font->fid);
66 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
68 /* Redimensionnement du widget / resize the widget */
69 xobj->height = xobj->Ffont->height + 5;
70 xobj->width =
71 FlocaleTextWidth(xobj->Ffont, xobj->title, strlen(xobj->title)) + 30;
72 XResizeWindow(dpy, xobj->win, xobj->width, xobj->height);
73 if (xobj->colorset >= 0)
74 SetWindowBackground(dpy, xobj->win, xobj->width, xobj->height,
75 &Colorset[xobj->colorset], Pdepth,
76 xobj->gc, True);
77 XSelectInput(dpy, xobj->win, ExposureMask);
80 void DestroyCheckBox(struct XObj *xobj)
82 FlocaleUnloadFont(dpy, xobj->Ffont);
83 XFreeGC(dpy,xobj->gc);
84 XDestroyWindow(dpy,xobj->win);
87 void DrawCheckBox(struct XObj *xobj, XEvent *evp)
89 XSegment segm[2];
91 /* Dessin du rectangle arrondi / drawing of the round rectangle */
92 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height, xobj->height,
93 xobj, hili, shad);
95 /* Calcul de la position de la chaine de charactere */
96 /* Compute the position of string */
97 MyDrawString(dpy, xobj, xobj->win, 23, xobj->Ffont->ascent, xobj->title,
98 fore, hili, back, !xobj->flags[1], NULL, evp);
99 /* Dessin de la croix / draw the cross */
100 if (xobj->value)
102 XSetLineAttributes(dpy, xobj->gc, 2, LineSolid, CapProjecting, JoinMiter);
103 segm[0].x1 = 5;
104 segm[0].y1 = 5;
105 segm[0].x2 = xobj->height - 6;
106 segm[0].y2 = xobj->height - 6;
107 segm[1].x1 = 5;
108 segm[1].y1 = xobj->height - 6;
109 segm[1].x2 = xobj->height - 6;
110 segm[1].y2 = 5;
111 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
112 XDrawSegments(dpy, xobj->win, xobj->gc, segm, 2);
113 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
117 void EvtMouseCheckBox(struct XObj *xobj, XButtonEvent *EvtButton)
119 static XEvent event;
120 int End = 1;
121 unsigned int modif;
122 int x1,x2,y1,y2;
123 Window Win1,Win2;
124 Window WinBut = 0;
125 int In = 0;
126 XSegment segm[2];
128 while (End)
130 FNextEvent(dpy, &event);
131 switch (event.type)
133 case EnterNotify:
134 FQueryPointer(dpy, *xobj->ParentWin,
135 &Win1, &Win2, &x1, &y1, &x2, &y2, &modif);
136 if (WinBut == 0)
138 WinBut = Win2;
139 /* Mouse on button */
140 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height,
141 xobj->height, xobj, shad, hili);
142 In = 1;
144 else
146 if (Win2 == WinBut)
148 /* Mouse on button */
149 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height, xobj->height,
150 xobj, shad, hili);
151 In = 1;
153 else if (In)
155 In = 0;
156 /* Mouse not on button */
157 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height, xobj->height,
158 xobj, hili, shad);
161 break;
162 case LeaveNotify:
163 FQueryPointer(dpy, *xobj->ParentWin,
164 &Win1, &Win2, &x1, &y1, &x2, &y2, &modif);
165 if (Win2 == WinBut)
167 In = 1;
168 /* Mouse on button */
169 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height, xobj->height,
170 xobj, shad, hili);
172 else if (In)
174 /* Mouse not on button */
175 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height, xobj->height,
176 xobj, hili, shad);
177 In = 0;
179 break;
180 case ButtonRelease:
181 End = 0;
182 /* Mouse not on button */
183 if (In)
185 /* Envoie d'un message vide de type SingleClic pour un clique souris */
186 xobj->value = !xobj->value;
187 DrawReliefRect(0, xobj->Ffont->ascent -11, xobj->height, xobj->height,
188 xobj, hili, shad);
189 SendMsg(xobj,SingleClic);
191 if (xobj->value)
193 XSetLineAttributes(dpy, xobj->gc, 2,
194 LineSolid, CapProjecting, JoinMiter);
195 segm[0].x1 = 5;
196 segm[0].y1 = 5;
197 segm[0].x2 = xobj->height - 6;
198 segm[0].y2 = xobj->height - 6;
199 segm[1].x1 = 5;
200 segm[1].y1 = xobj->height - 6;
201 segm[1].x2 = xobj->height - 6;
202 segm[1].y2 = 5;
203 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
204 XDrawSegments(dpy, xobj->win, xobj->gc, segm, 2);
205 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
207 else
209 XClearArea(dpy, xobj->win, 4, 4, xobj->height-8, xobj->height-8, False);
211 break;
216 void EvtKeyCheckBox(struct XObj *xobj, XKeyEvent *EvtKey)
218 KeySym ks;
219 unsigned char buf[10];
220 XSegment segm[2];
222 XLookupString(EvtKey, (char *)buf, sizeof(buf), &ks, NULL);
223 if (ks == XK_Return) {
224 xobj->value = !xobj->value;
225 DrawReliefRect(0, xobj->Ffont->ascent - 11, xobj->height,
226 xobj->height, xobj, hili, shad);
227 SendMsg(xobj,SingleClic);
228 if (xobj->value)
230 XSetLineAttributes(dpy, xobj->gc, 2,
231 LineSolid, CapProjecting, JoinMiter);
232 segm[0].x1 = 5;
233 segm[0].y1 = 5;
234 segm[0].x2 = xobj->height - 6;
235 segm[0].y2 = xobj->height - 6;
236 segm[1].x1 = 5;
237 segm[1].y1 = xobj->height - 6;
238 segm[1].x2 = xobj->height - 6;
239 segm[1].y2 = 5;
240 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
241 XDrawSegments(dpy, xobj->win, xobj->gc, segm, 2);
242 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
244 else
246 XClearArea(dpy, xobj->win, 4, 4, xobj->height-8, xobj->height-8, False);
252 void ProcessMsgCheckBox(struct XObj *xobj, unsigned long type, unsigned long *body)