Fix CursorMove command to correctly honour EdgeScroll settings.
[fvwm.git] / modules / FvwmScript / Widgets / HScrollBar.c
blob46da74116a3be1c8ad5761b0a2f2a22f6ad09fa0
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 HScrollBar
27 void DrawThumbH(struct XObj *xobj, XEvent *evp)
29 int x,y,w,h;
30 XSegment segm;
31 char str[20];
33 x = 2 +
34 (xobj->width - 36)*(xobj->value - xobj->value2) /
35 (xobj->value3 - xobj->value2);
36 y = xobj->height/2 - 11;
37 w = 32;
38 h = 22;
39 DrawReliefRect(x, y, w, h, xobj, hili, shad);
40 segm.x1 = x + 15;
41 segm.y1 = y + 3;
42 segm.x2 = x + 15;
43 segm.y2 = y +h - 3;
44 XSetForeground(dpy, xobj->gc, xobj->TabColor[shad]);
45 XDrawSegments(dpy, xobj->win, xobj->gc, &segm, 1);
46 segm.x1 = x + 16;
47 segm.y1 = y + 3;
48 segm.x2 = x+16;
49 segm.y2 = y + h-3;
50 XSetForeground(dpy, xobj->gc, xobj->TabColor[hili]);
51 XDrawSegments(dpy, xobj->win, xobj->gc, &segm, 1);
52 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
54 sprintf(str, "%d", xobj->value);
55 x = x + 15 - (FlocaleTextWidth(xobj->Ffont, str, strlen(str)) / 2);
56 y = y - xobj->Ffont->descent - 4;
57 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
58 !xobj->flags[1], NULL, evp);
61 void HideThumbH(struct XObj *xobj)
63 int x,y;
65 x = 2 +
66 (xobj->width - 36) * (xobj->value - xobj->value2) /
67 (xobj->value3-xobj->value2);
68 y = xobj->height/2 - 11;
69 XClearArea(dpy, xobj->win, x, y, 32, 22, False);
70 /* clear "text" */
71 XClearArea(dpy, xobj->win, 0, 0, xobj->width, xobj->height/2-15, False);
74 void InitHScrollBar(struct XObj *xobj)
76 unsigned long mask;
77 XSetWindowAttributes Attr;
78 int i;
79 char str[20];
81 /* Enregistrement des couleurs et de la police */
82 if (xobj->colorset >= 0) {
83 xobj->TabColor[fore] = Colorset[xobj->colorset].fg;
84 xobj->TabColor[back] = Colorset[xobj->colorset].bg;
85 xobj->TabColor[hili] = Colorset[xobj->colorset].hilite;
86 xobj->TabColor[shad] = Colorset[xobj->colorset].shadow;
87 } else {
88 xobj->TabColor[fore] = GetColor(xobj->forecolor);
89 xobj->TabColor[back] = GetColor(xobj->backcolor);
90 xobj->TabColor[hili] = GetColor(xobj->hilicolor);
91 xobj->TabColor[shad] = GetColor(xobj->shadcolor);
94 mask = 0;
95 Attr.background_pixel = xobj->TabColor[back];
96 mask |= CWBackPixel;
97 Attr.cursor = XCreateFontCursor(dpy,XC_hand2);
98 mask |= CWCursor; /* Curseur pour la fenetre */
100 xobj->win = XCreateWindow(dpy, *xobj->ParentWin,
101 xobj->x, xobj->y, xobj->width, xobj->height, 0,
102 CopyFromParent, InputOutput, CopyFromParent,
103 mask, &Attr);
104 xobj->gc = fvwmlib_XCreateGC(dpy, xobj->win, 0, NULL);
105 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
106 XSetBackground(dpy, xobj->gc, xobj->TabColor[back]);
108 if ((xobj->Ffont = FlocaleLoadFont(dpy, xobj->font, ScriptName)) == NULL)
110 fprintf(stderr, "%s: Couldn't load font. Exiting!\n", ScriptName);
111 exit(1);
113 if (xobj->Ffont->font != NULL)
114 XSetFont(dpy, xobj->gc, xobj->Ffont->font->fid);
116 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
118 if ((xobj->value3 - xobj->value2) <= 0)
119 xobj->value3 = xobj->value2 + 10;
120 if (!((xobj->value >= xobj->value2) && (xobj->value <= xobj->value3)))
121 xobj->value = xobj->value2;
122 xobj->height = xobj->Ffont->height * 2 + 30;
123 sprintf(str, "%d", xobj->value2);
124 i = FlocaleTextWidth(xobj->Ffont, str, strlen(str));
125 sprintf(str, "%d", xobj->value3);
126 i = FlocaleTextWidth(xobj->Ffont, str, strlen(str)) + i + 20;
127 if (xobj->width<i)
128 xobj->width = i;
129 XResizeWindow(dpy, xobj->win, xobj->width, xobj->height);
130 if (xobj->colorset >= 0)
131 SetWindowBackground(dpy, xobj->win, xobj->width, xobj->height,
132 &Colorset[xobj->colorset], Pdepth,
133 xobj->gc, True);
134 XSelectInput(dpy, xobj->win, ExposureMask);
137 void DestroyHScrollBar(struct XObj *xobj)
139 FlocaleUnloadFont(dpy,xobj->Ffont);
140 XFreeGC(dpy,xobj->gc);
141 XDestroyWindow(dpy,xobj->win);
144 void DrawHScrollBar(struct XObj *xobj, XEvent *evp)
146 int x,y,w,h;
147 char str[20];
149 /* Calcul de la taille de l'ascenseur */
150 x = 0;
151 y = xobj->height/2-13;
152 w = xobj->width;
153 h = 26;
154 DrawThumbH(xobj, evp);
155 DrawReliefRect(x, y, w, h, xobj, shad, hili);
156 /* Ecriture des valeurs */
157 sprintf(str, "%d", xobj->value2);
158 x = 4;
159 y = y + xobj->Ffont->ascent + h;
160 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
161 !xobj->flags[1], NULL, evp);
162 sprintf(str, "%d", xobj->value3);
163 x = w - FlocaleTextWidth(xobj->Ffont, str, strlen(str)) - 4;
164 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
165 !xobj->flags[1], NULL, evp);
168 void EvtMouseHScrollBar(struct XObj *xobj, XButtonEvent *EvtButton)
170 static XEvent event;
171 int x,y,w,h;
172 int oldx = 0;
173 int oldvalue = -1;
174 int newvalue;
175 int x1,y1,x2,y2;
176 Window Win1,Win2;
177 unsigned int modif;
178 fd_set in_fdset;
180 x = 2 + ((xobj->width - 36) * xobj->value) / (xobj->value3 - xobj->value2);
181 y = xobj->height/2 - 11;
182 w = 34;
183 h = 22;
187 /* On suit les mouvements de la souris */
188 FQueryPointer(dpy, *xobj->ParentWin, &Win1, &Win2, &x1, &y1, &x2, &y2, &modif);
189 x2 = x2 - xobj->x;
190 if (x2 < 15)
191 x2 = 15;
192 if (x2 > xobj->width-21)
193 x2 = xobj->width - 21;
194 if (oldx!=x2)
196 oldx = x2;
197 /* calcule de xobj->value */
198 newvalue = (x2-15)*xobj->width / (xobj->width - 36) *
199 (xobj->value3 - xobj->value2) / (xobj->width) + xobj->value2;
200 if (newvalue!=oldvalue)
202 HideThumbH(xobj);
203 xobj->value = newvalue;
204 DrawThumbH(xobj, NULL);
205 oldvalue = newvalue;
206 SendMsg(xobj,SingleClic);
207 XSync(dpy, 0);
208 usleep(10000);
211 FD_ZERO(&in_fdset);
212 FD_SET(x_fd, &in_fdset);
213 select(32, SELECT_FD_SET_CAST &in_fdset, NULL, NULL, NULL);
215 while (!FCheckTypedEvent(dpy, ButtonRelease, &event) && EvtButton != NULL);
218 void EvtKeyHScrollBar(struct XObj *xobj, XKeyEvent *EvtKey)
220 KeySym ks;
221 unsigned char buf[10];
223 XLookupString(EvtKey, (char *)buf, sizeof(buf), &ks, NULL);
224 if (ks == XK_Left && xobj->value > 0) {
225 HideThumbH(xobj);
226 xobj->value--;
227 DrawThumbH(xobj, NULL);
228 SendMsg(xobj,SingleClic);
230 else if (ks == XK_Right &&
231 xobj->value <
232 xobj->width*(xobj->value3-xobj->value2)/(xobj->width) + xobj->value2) {
233 HideThumbH(xobj);
234 xobj->value++;
235 DrawThumbH(xobj, NULL);
236 SendMsg(xobj,SingleClic);
238 else if (ks == XK_Return) {
239 EvtMouseHScrollBar(xobj, NULL);
243 void ProcessMsgHScrollBar(struct XObj *xobj,unsigned long type,unsigned long *body)