Fix CursorMove command to correctly honour EdgeScroll settings.
[fvwm.git] / modules / FvwmScript / Widgets / VScrollBar.c
blob89d717cca80ffffcfb01b8851123b6f594f095e7
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 VScrollBar
27 void DrawThumbV(struct XObj *xobj, XEvent *evp)
29 int x,y,w,h;
30 XSegment segm;
31 char str[20];
33 x = xobj->width/2 - 10;
34 y = 2 +
35 (xobj->height - 36)*(xobj->value - xobj->value3) /
36 (xobj->value2 - xobj->value3);
37 w = 20;
38 h = 32;
39 DrawReliefRect(x, y, w, h, xobj, hili, shad);
40 segm.x1 = x + 3;
41 segm.y1 = y + 15;
42 segm.x2 = x + w - 3;
43 segm.y2 = y + 15;
44 XSetForeground(dpy, xobj->gc, xobj->TabColor[shad]);
45 XDrawSegments(dpy, xobj->win, xobj->gc, &segm, 1);
46 segm.x1 = x + 3;
47 segm.y1 = y + 16;
48 segm.x2 = x + w - 3;
49 segm.y2 = y + 16;
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-FlocaleTextWidth(xobj->Ffont, str, strlen(str))-6;
56 y = y + 13 + xobj->Ffont->ascent/2;
57 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
58 !xobj->flags[1], NULL, evp);
61 void HideThumbV(struct XObj *xobj)
63 int x,y;
64 char str[20];
66 x = xobj->width/2 - 10;
67 y = 2 +
68 (xobj->height - 36) * (xobj->value - xobj->value3) /
69 (xobj->value2 - xobj->value3);
70 XClearArea(dpy, xobj->win, x, y, 20, 32, False);
71 sprintf(str, "%d", xobj->value);
72 XClearArea(
73 dpy, xobj->win, 0, 0,xobj->width/2 - 14,xobj->height, False);
76 void InitVScrollBar(struct XObj *xobj)
78 unsigned long mask;
79 XSetWindowAttributes Attr;
80 int i,j;
81 char str[20];
83 /* Enregistrement des couleurs et de la police */
84 if (xobj->colorset >= 0) {
85 xobj->TabColor[fore] = Colorset[xobj->colorset].fg;
86 xobj->TabColor[back] = Colorset[xobj->colorset].bg;
87 xobj->TabColor[hili] = Colorset[xobj->colorset].hilite;
88 xobj->TabColor[shad] = Colorset[xobj->colorset].shadow;
89 } else {
90 xobj->TabColor[fore] = GetColor(xobj->forecolor);
91 xobj->TabColor[back] = GetColor(xobj->backcolor);
92 xobj->TabColor[hili] = GetColor(xobj->hilicolor);
93 xobj->TabColor[shad] = GetColor(xobj->shadcolor);
96 mask = 0;
97 Attr.background_pixel = xobj->TabColor[back];
98 mask |= CWBackPixel;
99 Attr.cursor = XCreateFontCursor(dpy,XC_hand2);
100 mask |= CWCursor; /* Curseur pour la fenetre */
102 xobj->win = XCreateWindow(
103 dpy, *xobj->ParentWin, xobj->x, xobj->y, xobj->width,
104 xobj->height, 0, CopyFromParent, InputOutput, CopyFromParent,
105 mask, &Attr);
106 xobj->gc = fvwmlib_XCreateGC(dpy, xobj->win, 0, NULL);
107 XSetForeground(dpy, xobj->gc, xobj->TabColor[fore]);
110 if ((xobj->Ffont = FlocaleLoadFont(dpy, xobj->font, ScriptName)) ==
111 NULL)
113 fprintf(
114 stderr, "%s: Couldn't load font. Exiting!\n",
115 ScriptName);
116 exit(1);
118 if (xobj->Ffont->font != NULL)
119 XSetFont(dpy, xobj->gc, xobj->Ffont->font->fid);
121 XSetLineAttributes(dpy, xobj->gc, 1, LineSolid, CapRound, JoinMiter);
123 if ((xobj->value3 - xobj->value2) <= 0)
124 xobj->value3 = xobj->value2 + 10;
125 if (!((xobj->value >= xobj->value2) && (xobj->value <= xobj->value3)))
126 xobj->value = xobj->value2;
128 i = (xobj->Ffont->height)*2+30;
129 if (xobj->height < i)
130 xobj->height = i;
131 sprintf(str, "%d", xobj->value2);
132 i = FlocaleTextWidth(xobj->Ffont, str, strlen(str));
133 sprintf(str, "%d", xobj->value3);
134 j = FlocaleTextWidth(xobj->Ffont, str, strlen(str));
135 if (i<j)
136 i = j*2+30;
137 else
138 i = i*2+30;
139 xobj->width = i;
140 XResizeWindow(dpy, xobj->win, xobj->width, xobj->height);
141 if (xobj->colorset >= 0)
142 SetWindowBackground(dpy, xobj->win, xobj->width, xobj->height,
143 &Colorset[xobj->colorset], Pdepth,
144 xobj->gc, True);
145 XSelectInput(dpy, xobj->win, ExposureMask);
148 void DestroyVScrollBar(struct XObj *xobj)
150 FlocaleUnloadFont(dpy,xobj->Ffont);
151 XFreeGC(dpy,xobj->gc);
152 XDestroyWindow(dpy,xobj->win);
155 void DrawVScrollBar(struct XObj *xobj, XEvent *evp)
157 int x,y,w,h;
158 char str[20];
160 /* Calcul de la taille de l'ascenseur */
161 x = xobj->width/2 - 12;
162 y = 0;
163 w = 24;
164 h = xobj->height;
165 DrawThumbV(xobj, evp);
166 DrawReliefRect(x, y, w, h, xobj, shad, hili);
167 /* Ecriture des valeurs */
168 x = x + 26;
169 y = xobj->Ffont->ascent + 2;
170 sprintf(str, "%d", xobj->value3);
171 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
172 !xobj->flags[1], NULL, evp);
173 sprintf(str, "%d", xobj->value2);
174 y = h - xobj->Ffont->descent - 2;
175 MyDrawString(dpy, xobj, xobj->win, x, y, str, fore, hili, back,
176 !xobj->flags[1], NULL, evp);
179 void EvtMouseVScrollBar(struct XObj *xobj, XButtonEvent *EvtButton)
181 static XEvent event;
182 int oldy = 0;
183 int oldvalue = -1;
184 int newvalue;
185 int x1,y1,x2,y2;
186 Window Win1,Win2;
187 unsigned int modif;
188 fd_set in_fdset;
192 /* On suit les mouvements de la souris */
193 FQueryPointer(dpy, *xobj->ParentWin, &Win1, &Win2,
194 &x1, &y1, &x2, &y2, &modif);
195 y2 = y2 - xobj->y;
196 if (y2 < 15)
197 y2 = 15;
198 if (y2 > xobj->height - 21)
199 y2 = xobj->height - 21;
200 if (oldy != y2)
202 oldy = y2;
203 /* calcule de xobj->value */
204 newvalue = (y2-15)*xobj->height/(xobj->height - 36) *
205 (xobj->value2 - xobj->value3) /
206 (xobj->height) + xobj->value3;
207 if (newvalue!=oldvalue)
209 HideThumbV(xobj);
210 xobj->value = newvalue;
211 DrawThumbV(xobj, NULL);
212 oldvalue = newvalue;
213 SendMsg(xobj,SingleClic);
214 XSync(dpy,0);
215 usleep(10000);
218 FD_ZERO(&in_fdset);
219 FD_SET(x_fd, &in_fdset);
220 select(32, SELECT_FD_SET_CAST &in_fdset, NULL, NULL, NULL);
222 while (!FCheckTypedEvent(dpy, ButtonRelease, &event) &&
223 EvtButton != NULL);
226 void EvtKeyVScrollBar(struct XObj *xobj, XKeyEvent *EvtKey)
228 KeySym ks;
229 unsigned char buf[10];
231 XLookupString(EvtKey, (char *)buf, sizeof(buf), &ks, NULL);
232 if (ks == XK_Down && xobj->value > 0) {
233 HideThumbV(xobj);
234 xobj->value--;
235 DrawThumbV(xobj, NULL);
236 SendMsg(xobj,SingleClic);
238 else if (ks == XK_Up &&
239 xobj->value <
240 xobj->width*(xobj->value3-xobj->value2) /
241 (xobj->width)+xobj->value2) {
242 HideThumbV(xobj);
243 xobj->value++;
244 DrawThumbV(xobj, NULL);
245 SendMsg(xobj,SingleClic);
247 else if (ks == XK_Return) {
248 EvtMouseVScrollBar(xobj, NULL);
252 void ProcessMsgVScrollBar(
253 struct XObj *xobj,unsigned long type,unsigned long *body)