Update for 0.51.0
[wmaker-crm.git] / WPrefs.app / MouseSettings.c
blobca1e1d692a8701466c625abbe20fe3ded4ba8617
1 /* MouseSettings.c- mouse options (some are equivalent to xset)
2 *
3 * WPrefs - Window Maker Preferences Program
4 *
5 * Copyright (c) 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "WPrefs.h"
26 #include <X11/Xutil.h>
28 #include <math.h>
30 /* double-click tester */
31 #include "double.h"
35 #define XSET "xset"
39 typedef struct _Panel {
40 WMFrame *frame;
42 char *sectionName;
44 CallbackRec callbacks;
46 WMWindow *win;
48 WMFrame *speedF;
49 WMLabel *speedL;
50 WMButton *speedB[5];
51 WMLabel *acceL;
52 WMTextField *acceT;
53 WMLabel *threL;
54 WMTextField *threT;
56 WMFrame *ddelaF;
57 WMButton *ddelaB[5];
58 DoubleTest *tester;
60 WMFrame *menuF;
61 WMLabel *listL;
62 WMLabel *appL;
63 WMLabel *selL;
64 WMLabel *mblL;
65 WMLabel *mbmL;
66 WMLabel *mbrL;
67 WMButton *lmb[3];
68 WMButton *amb[3];
69 WMButton *smb[3];
72 WMButton *disaB;
74 WMFrame *grabF;
75 WMPopUpButton *grabP;
77 /**/
78 WMButton *lastClickedSpeed;
79 int maxThreshold;
80 float acceleration;
81 } _Panel;
84 #define ICON_FILE "mousesettings"
86 #define SPEED_ICON_FILE "mousespeed"
87 #define SPEED_IMAGE "speed%i"
88 #define SPEED_IMAGE_S "speed%is"
90 #define DELAY_ICON "timer%i"
91 #define DELAY_ICON_S "timer%is"
93 #define MOUSEB_L "minimouseleft"
94 #define MOUSEB_M "minimousemiddle"
95 #define MOUSEB_R "minimouseright"
97 /* need access to the double click variables */
98 #include "WINGsP.h"
102 static char *modifierNames[] = {
103 "Shift",
104 "Lock",
105 "Control",
106 "Mod1",
107 "Mod2",
108 "Mod3",
109 "Mod4",
110 "Mod5"
114 #define DELAY(i) ((i)*75+170)
117 int ModifierFromKey(Display *dpy, char *key);
120 static void
121 setMouseAccel(WMScreen *scr, float accel, int threshold)
123 int n, d;
125 d = 10;
126 n = accel*d;
128 XChangePointerControl(WMScreenDisplay(scr), True, True, n, d, threshold);
132 static void
133 speedClick(WMWidget *w, void *data)
135 _Panel *panel = (_Panel*)data;
136 int i;
137 char buffer[64];
138 int threshold;
139 char *tmp;
141 if (w == NULL) {
142 float accel;
144 tmp = WMGetTextFieldText(panel->acceT);
145 if (sscanf(tmp, "%f", &accel)!=1 || accel < 0) {
146 WMRunAlertPanel(WMWidgetScreen(w), GetWindow(panel), _("Error"),
147 _("Invalid mouse acceleration value. Must be a positive real value."),
148 _("OK"), NULL, NULL);
149 free(tmp);
150 return;
152 panel->acceleration = accel;
153 free(tmp);
154 } else {
155 for (i=0; i<5; i++) {
156 if (panel->speedB[i]==w)
157 break;
160 panel->acceleration = 0.5+(i*0.5);
162 sprintf(buffer, "%.2f", 0.5+(i*0.5));
163 WMSetTextFieldText(panel->acceT, buffer);
166 tmp = WMGetTextFieldText(panel->threT);
167 if (sscanf(tmp, "%i", &threshold)!=1 || threshold < 0
168 || threshold > panel->maxThreshold) {
169 WMRunAlertPanel(WMWidgetScreen(panel->win), GetWindow(panel), _("Error"),
170 _("Invalid mouse acceleration threshold value. Must be the number of pixels to travel before accelerating."),
171 _("OK"), NULL, NULL);
172 } else {
173 setMouseAccel(WMWidgetScreen(panel->win), panel->acceleration,
174 threshold);
176 free(tmp);
180 static void
181 returnPressed(void *observerData, WMNotification *notification)
183 _Panel *panel = (_Panel*)observerData;
185 speedClick(NULL, panel);
189 static void
190 doubleClick(WMWidget *w, void *data)
192 _Panel *panel = (_Panel*)data;
193 int i;
194 extern _WINGsConfiguration WINGsConfiguration;
196 for (i=0; i<5; i++) {
197 if (panel->ddelaB[i]==w)
198 break;
200 WINGsConfiguration.doubleClickDelay = DELAY(i);
206 getbutton(char *str)
208 if (!str)
209 return -2;
211 if (strcasecmp(str, "left")==0)
212 return 0;
213 else if (strcasecmp(str, "middle")==0)
214 return 1;
215 else if (strcasecmp(str, "right")==0)
216 return 2;
217 else if (strcasecmp(str, "button1")==0)
218 return 0;
219 else if (strcasecmp(str, "button2")==0)
220 return 1;
221 else if (strcasecmp(str, "button3")==0)
222 return 2;
223 else if (strcasecmp(str, "button4")==0
224 || strcasecmp(str, "button5")==0) {
225 wwarning(_("mouse button %s not supported by WPrefs."), str);
226 return -2;
227 } else {
228 return -1;
233 static void
234 getMouseParameters(Display *dpy, float *accel, int *thre)
236 int n, d;
238 XGetPointerControl(dpy, &n, &d, thre);
240 *accel = (float)n/(float)d;
245 static void
246 showData(_Panel *panel)
248 char *str;
249 int i;
250 int a=-1, b=-1, c=-1;
251 float accel;
252 char buffer[32];
253 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->win));
255 str = GetStringForKey("SelectWindowsMouseButton");
256 i = getbutton(str);
257 if (i==-1) {
258 a = 0;
259 wwarning(_("bad value %s for option %s"),str, "SelectWindowsMouseButton");
260 WMPerformButtonClick(panel->smb[0]);
261 } else if (i>=0) {
262 a = i;
263 WMPerformButtonClick(panel->smb[i]);
266 str = GetStringForKey("WindowListMouseButton");
267 i = getbutton(str);
268 if (i==-1) {
269 b = 0;
270 wwarning(_("bad value %s for option %s"), str, "WindowListMouseButton");
271 WMPerformButtonClick(panel->lmb[1]);
272 } else if (i>=0) {
273 b = i;
274 WMPerformButtonClick(panel->lmb[i]);
277 str = GetStringForKey("ApplicationMenuMouseButton");
278 i = getbutton(str);
279 if (i==-1) {
280 c = 0;
281 wwarning(_("bad value %s for option %s"), str, "ApplicationMenuMouseButton");
282 WMPerformButtonClick(panel->amb[2]);
283 } else if (i>=0) {
284 c = i;
285 WMPerformButtonClick(panel->amb[i]);
289 WMSetButtonSelected(panel->disaB, GetBoolForKey("DisableWSMouseActions"));
291 /**/
292 getMouseParameters(dpy, &accel, &a);
293 panel->maxThreshold = WidthOfScreen(DefaultScreenOfDisplay(dpy));
294 if (a > panel->maxThreshold) {
295 panel->maxThreshold = a;
297 sprintf(buffer, "%i", a);
298 WMSetTextFieldText(panel->threT, buffer);
299 /* find best match */
300 a = 0;
301 for (i=0; i<5; i++) {
302 if (fabs((0.5+((float)i*0.5))-accel) < fabs((0.5+((float)a*0.5))-accel))
303 a = i;
305 WMPerformButtonClick(panel->speedB[a]);
306 panel->lastClickedSpeed = panel->speedB[a];
307 panel->acceleration = accel;
309 speedClick(panel->lastClickedSpeed, panel);
310 /**/
311 b = GetIntegerForKey("DoubleClickTime");
312 /* find best match */
313 a = 0;
314 for (i=0; i<5; i++) {
315 if (abs(b - DELAY(i)) < abs(b - DELAY(a)))
316 a = i;
318 WMPerformButtonClick(panel->ddelaB[a]);
320 /**/
321 str = GetStringForKey("ModifierKey");
323 a = ModifierFromKey(dpy, str);
325 if (a != -1) {
326 str = modifierNames[a];
328 a = 0;
329 for (i=0; i<WMGetPopUpButtonNumberOfItems(panel->grabP); i++) {
330 if (strstr(WMGetPopUpButtonItem(panel->grabP, i), str)) {
331 WMSetPopUpButtonSelectedItem(panel->grabP, i);
332 a = 1;
333 break;
338 if (a < 1) {
339 sscanf(WMGetPopUpButtonItem(panel->grabP, 0), "%s", buffer);
340 WMSetPopUpButtonSelectedItem(panel->grabP, 0);
341 wwarning(_("modifier key %s for option ModifierKey was not recognized. Using %s as default"),
342 str, buffer);
348 static void
349 fillModifierPopUp(WMPopUpButton *pop)
351 XModifierKeymap *mapping;
352 Display *dpy = WMScreenDisplay(WMWidgetScreen(pop));
353 int i, j;
354 char *str;
355 char buffer[64];
358 mapping = XGetModifierMapping(dpy);
360 if (!mapping || mapping->max_keypermod==0) {
361 WMAddPopUpButtonItem(pop, "Mod1");
362 WMAddPopUpButtonItem(pop, "Mod2");
363 WMAddPopUpButtonItem(pop, "Mod3");
364 WMAddPopUpButtonItem(pop, "Mod4");
365 WMAddPopUpButtonItem(pop, "Mod5");
366 wwarning(_("could not retrieve keyboard modifier mapping"));
367 return;
371 for (j=0; j<8; j++) {
372 int idx;
373 char *array[8];
374 int a;
375 KeySym ksym;
376 int k;
377 char *ptr;
378 char *tmp;
380 a = 0;
381 memset(array, 0, sizeof(char*)*8);
382 for (i=0; i < mapping->max_keypermod; i++) {
383 idx = i+j*mapping->max_keypermod;
384 if (mapping->modifiermap[idx]!=0) {
385 int l;
386 for (l=0; l<4; l++) {
387 ksym = XKeycodeToKeysym(dpy, mapping->modifiermap[idx], l);
388 if (ksym!=NoSymbol)
389 break;
391 if (ksym!=NoSymbol)
392 str = XKeysymToString(ksym);
393 else
394 str = NULL;
395 if (str && !strstr(str, "_Lock") && !strstr(str, "Shift")
396 && !strstr(str, "Control")) {
397 array[a++] = wstrdup(str);
402 for (k=0; k<a; k++) {
403 if (array[k]==NULL)
404 continue;
405 tmp = wstrdup(array[k]);
406 ptr = strstr(tmp, "_L");
407 if (ptr)
408 *ptr = 0;
409 ptr = strstr(tmp, "_R");
410 if (ptr)
411 *ptr = 0;
412 sprintf(buffer, "%s (%s)", modifierNames[j], tmp);
413 WMAddPopUpButtonItem(pop, buffer);
414 for (i=k+1; i<a; i++) {
415 if (strstr(array[i], tmp)) {
416 free(array[i]);
417 array[i]=NULL;
418 break;
421 free(tmp);
424 while (--a>0) {
425 if (array[a])
426 free(array[a]);
430 if (mapping)
431 XFreeModifiermap(mapping);
436 static void
437 mouseButtonClickA(WMWidget *w, void *data)
439 _Panel *panel = (_Panel*)data;
440 int i;
442 for (i=0; i<3; i++) {
443 if (panel->amb[i]==w)
444 break;
446 if (i==3)
447 return;
448 if (WMGetButtonSelected(panel->lmb[i]))
449 WMSetButtonSelected(panel->lmb[i], False);
450 if (WMGetButtonSelected(panel->smb[i]))
451 WMSetButtonSelected(panel->smb[i], False);
454 static void
455 mouseButtonClickL(WMWidget *w, void *data)
457 _Panel *panel = (_Panel*)data;
458 int i;
460 for (i=0; i<3; i++) {
461 if (panel->lmb[i]==w)
462 break;
464 if (i==3)
465 return;
466 if (WMGetButtonSelected(panel->smb[i]))
467 WMSetButtonSelected(panel->smb[i], False);
468 if (WMGetButtonSelected(panel->amb[i]))
469 WMSetButtonSelected(panel->amb[i], False);
472 static void
473 mouseButtonClickS(WMWidget *w, void *data)
475 _Panel *panel = (_Panel*)data;
476 int i;
478 for (i=0; i<3; i++) {
479 if (panel->smb[i]==w)
480 break;
482 if (i==3)
483 return;
484 if (WMGetButtonSelected(panel->lmb[i]))
485 WMSetButtonSelected(panel->lmb[i], False);
486 if (WMGetButtonSelected(panel->amb[i]))
487 WMSetButtonSelected(panel->amb[i], False);
490 static void
491 createPanel(Panel *p)
493 _Panel *panel = (_Panel*)p;
494 WMScreen *scr = WMWidgetScreen(panel->win);
495 WMPixmap *icon;
496 char *buf1, *buf2;
497 int i;
498 RColor color;
499 char *path;
501 color.red = 0xaa;
502 color.green = 0xae;
503 color.blue = 0xaa;
505 panel->frame = WMCreateFrame(panel->win);
506 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
507 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
509 /**************** Mouse Speed ****************/
510 panel->speedF = WMCreateFrame(panel->frame);
511 WMResizeWidget(panel->speedF, 245, 100);
512 WMMoveWidget(panel->speedF, 15, 15);
513 WMSetFrameTitle(panel->speedF, _("Mouse Speed"));
515 panel->speedL = WMCreateLabel(panel->speedF);
516 WMResizeWidget(panel->speedL, 40, 46);
517 WMMoveWidget(panel->speedL, 15, 14);
518 WMSetLabelImagePosition(panel->speedL, WIPImageOnly);
519 path = LocateImage(SPEED_ICON_FILE);
520 if (path) {
521 icon = WMCreateBlendedPixmapFromFile(scr, path, &color);
522 if (icon) {
523 WMSetLabelImage(panel->speedL, icon);
524 WMReleasePixmap(icon);
525 } else {
526 wwarning(_("could not load icon %s"), path);
528 free(path);
531 buf1 = wmalloc(strlen(SPEED_IMAGE)+1);
532 buf2 = wmalloc(strlen(SPEED_IMAGE_S)+1);
534 for (i = 0; i < 5; i++) {
535 panel->speedB[i] = WMCreateCustomButton(panel->speedF,
536 WBBStateChangeMask);
537 WMResizeWidget(panel->speedB[i], 26, 26);
538 WMMoveWidget(panel->speedB[i], 60+(35*i), 25);
539 WMSetButtonBordered(panel->speedB[i], False);
540 WMSetButtonImagePosition(panel->speedB[i], WIPImageOnly);
541 WMSetButtonAction(panel->speedB[i], speedClick, panel);
542 if (i > 0) {
543 WMGroupButtons(panel->speedB[0], panel->speedB[i]);
545 sprintf(buf1, SPEED_IMAGE, i);
546 sprintf(buf2, SPEED_IMAGE_S, i);
547 path = LocateImage(buf1);
548 if (path) {
549 icon = WMCreatePixmapFromFile(scr, path);
550 if (icon) {
551 WMSetButtonImage(panel->speedB[i], icon);
552 WMReleasePixmap(icon);
553 } else {
554 wwarning(_("could not load icon file %s"), path);
556 free(path);
558 path = LocateImage(buf2);
559 if (path) {
560 icon = WMCreatePixmapFromFile(scr, path);
561 if (icon) {
562 WMSetButtonAltImage(panel->speedB[i], icon);
563 WMReleasePixmap(icon);
564 } else {
565 wwarning(_("could not load icon file %s"), path);
569 free(buf1);
570 free(buf2);
572 panel->acceL = WMCreateLabel(panel->speedF);
573 WMResizeWidget(panel->acceL, 80, 16);
574 WMMoveWidget(panel->acceL, 10, 67);
575 WMSetLabelText(panel->acceL, _("Acceler.:"));
577 panel->acceT = WMCreateTextField(panel->speedF);
578 WMResizeWidget(panel->acceT, 35, 20);
579 WMMoveWidget(panel->acceT, 85, 65);
580 WMAddNotificationObserver(returnPressed, panel,
581 WMTextDidEndEditingNotification, panel->acceT);
584 panel->threL = WMCreateLabel(panel->speedF);
585 WMResizeWidget(panel->threL, 80, 16);
586 WMMoveWidget(panel->threL, 120, 67);
587 WMSetLabelText(panel->threL, _("Threshold:"));
589 panel->threT = WMCreateTextField(panel->speedF);
590 WMResizeWidget(panel->threT, 30, 20);
591 WMMoveWidget(panel->threT, 200, 65);
592 WMAddNotificationObserver(returnPressed, panel,
593 WMTextDidEndEditingNotification, panel->threT);
595 WMMapSubwidgets(panel->speedF);
597 /***************** Doubleclick Delay ****************/
599 panel->ddelaF = WMCreateFrame(panel->frame);
600 WMResizeWidget(panel->ddelaF, 245, 95);
601 WMMoveWidget(panel->ddelaF, 15, 125);
602 WMSetFrameTitle(panel->ddelaF, _("Double-Click Delay"));
604 buf1 = wmalloc(strlen(DELAY_ICON)+1);
605 buf2 = wmalloc(strlen(DELAY_ICON_S)+1);
607 for (i = 0; i < 5; i++) {
608 panel->ddelaB[i] = WMCreateCustomButton(panel->ddelaF,
609 WBBStateChangeMask);
610 WMResizeWidget(panel->ddelaB[i], 25, 25);
611 WMMoveWidget(panel->ddelaB[i], 30+(40*i), 20);
612 WMSetButtonBordered(panel->ddelaB[i], False);
613 WMSetButtonImagePosition(panel->ddelaB[i], WIPImageOnly);
614 WMSetButtonAction(panel->ddelaB[i], doubleClick, panel);
615 if (i>0) {
616 WMGroupButtons(panel->ddelaB[0], panel->ddelaB[i]);
618 sprintf(buf1, DELAY_ICON, i+1);
619 sprintf(buf2, DELAY_ICON_S, i+1);
620 path = LocateImage(buf1);
621 if (path) {
622 icon = WMCreatePixmapFromFile(scr, path);
623 if (icon) {
624 WMSetButtonImage(panel->ddelaB[i], icon);
625 WMReleasePixmap(icon);
626 } else {
627 wwarning(_("could not load icon file %s"), path);
629 free(path);
631 path = LocateImage(buf2);
632 if (path) {
633 icon = WMCreatePixmapFromFile(scr, path);
634 if (icon) {
635 WMSetButtonAltImage(panel->ddelaB[i], icon);
636 WMReleasePixmap(icon);
637 } else {
638 wwarning(_("could not load icon file %s"), path);
640 free(path);
643 free(buf1);
644 free(buf2);
646 panel->tester = CreateDoubleTest(panel->ddelaF, _("Test"));
647 WMResizeWidget(panel->tester, 84, 29);
648 WMMoveWidget(panel->tester, 85, 55);
650 WMMapSubwidgets(panel->ddelaF);
652 /* ************** Workspace Action Buttons **************** */
653 panel->menuF = WMCreateFrame(panel->frame);
654 WMResizeWidget(panel->menuF, 240, 145);
655 WMMoveWidget(panel->menuF, 270, 15);
656 WMSetFrameTitle(panel->menuF, _("Workspace Mouse Actions"));
658 panel->disaB = WMCreateSwitchButton(panel->menuF);
659 WMResizeWidget(panel->disaB, 205, 18);
660 WMMoveWidget(panel->disaB, 10, 20);
661 WMSetButtonText(panel->disaB, _("Disable mouse actions"));
663 panel->mblL = WMCreateLabel(panel->menuF);
664 WMResizeWidget(panel->mblL, 16, 22);
665 WMMoveWidget(panel->mblL, 135, 40);
666 WMSetLabelImagePosition(panel->mblL, WIPImageOnly);
667 path = LocateImage(MOUSEB_L);
668 if (path) {
669 icon = WMCreatePixmapFromFile(scr, path);
670 if (icon) {
671 WMSetLabelImage(panel->mblL, icon);
672 WMReleasePixmap(icon);
673 } else {
674 wwarning(_("could not load icon file %s"), path);
676 free(path);
678 panel->mbmL = WMCreateLabel(panel->menuF);
679 WMResizeWidget(panel->mbmL, 16, 22);
680 WMMoveWidget(panel->mbmL, 170, 40);
681 WMSetLabelImagePosition(panel->mbmL, WIPImageOnly);
682 path = LocateImage(MOUSEB_M);
683 if (path) {
684 icon = WMCreatePixmapFromFile(scr, path);
685 if (icon) {
686 WMSetLabelImage(panel->mbmL, icon);
687 WMReleasePixmap(icon);
688 } else {
689 wwarning(_("could not load icon file %s"), path);
691 free(path);
694 panel->mbrL = WMCreateLabel(panel->menuF);
695 WMResizeWidget(panel->mbrL, 16, 22);
696 WMMoveWidget(panel->mbrL, 205, 40);
697 WMSetLabelImagePosition(panel->mbrL, WIPImageOnly);
698 path = LocateImage(MOUSEB_R);
699 if (path) {
700 icon = WMCreatePixmapFromFile(scr, path);
701 if (icon) {
702 WMSetLabelImage(panel->mbrL, icon);
703 WMReleasePixmap(icon);
704 } else {
705 wwarning(_("could not load icon file %s"), path);
707 free(path);
710 panel->appL = WMCreateLabel(panel->menuF);
711 WMResizeWidget(panel->appL, 125, 16);
712 WMMoveWidget(panel->appL, 5, 65);
713 WMSetLabelTextAlignment(panel->appL, WARight);
714 WMSetLabelText(panel->appL, _("Applications menu"));
716 panel->listL = WMCreateLabel(panel->menuF);
717 WMResizeWidget(panel->listL, 125, 16);
718 WMMoveWidget(panel->listL, 5, 90);
719 WMSetLabelTextAlignment(panel->listL, WARight);
720 WMSetLabelText(panel->listL, _("Window list menu"));
722 panel->selL = WMCreateLabel(panel->menuF);
723 WMResizeWidget(panel->selL, 125, 16);
724 WMMoveWidget(panel->selL, 5, 115);
725 WMSetLabelTextAlignment(panel->selL, WARight);
726 WMSetLabelText(panel->selL, _("Select windows"));
729 for (i=0; i<3; i++) {
730 panel->amb[i] = WMCreateRadioButton(panel->menuF);
731 WMResizeWidget(panel->amb[i], 24, 24);
732 WMMoveWidget(panel->amb[i], 135+35*i, 65);
733 WMSetButtonText(panel->amb[i], NULL);
734 WMSetButtonAction(panel->amb[i], mouseButtonClickA, panel);
736 panel->lmb[i] = WMCreateRadioButton(panel->menuF);
737 WMResizeWidget(panel->lmb[i], 24, 24);
738 WMMoveWidget(panel->lmb[i], 135+35*i, 90);
739 WMSetButtonText(panel->lmb[i], NULL);
740 WMSetButtonAction(panel->lmb[i], mouseButtonClickL, panel);
742 panel->smb[i] = WMCreateRadioButton(panel->menuF);
743 WMResizeWidget(panel->smb[i], 24, 24);
744 WMMoveWidget(panel->smb[i], 135+35*i, 115);
745 WMSetButtonText(panel->smb[i], NULL);
746 WMSetButtonAction(panel->smb[i], mouseButtonClickS, panel);
748 if (i>0) {
749 WMGroupButtons(panel->lmb[0], panel->lmb[i]);
750 WMGroupButtons(panel->amb[0], panel->amb[i]);
751 WMGroupButtons(panel->smb[0], panel->smb[i]);
755 WMMapSubwidgets(panel->menuF);
757 /* ************** Grab Modifier **************** */
758 panel->grabF = WMCreateFrame(panel->frame);
759 WMResizeWidget(panel->grabF, 240, 55);
760 WMMoveWidget(panel->grabF, 270, 165);
761 WMSetFrameTitle(panel->grabF, _("Mouse Grab Modifier"));
763 panel->grabP = WMCreatePopUpButton(panel->grabF);
764 WMResizeWidget(panel->grabP, 160, 20);
765 WMMoveWidget(panel->grabP, 40, 25);
767 fillModifierPopUp(panel->grabP);
769 WMMapSubwidgets(panel->grabF);
771 WMRealizeWidget(panel->frame);
772 WMMapSubwidgets(panel->frame);
775 showData(panel);
779 static void
780 storeCommandInScript(char *cmd, char *line)
782 char *path;
783 char *p;
784 FILE *f;
785 char buffer[128];
787 p = wusergnusteppath();
788 path = wmalloc(strlen(p)+64);
789 sprintf(path, "%s/Library/WindowMaker/autostart", p);
791 f = fopen(path, "r");
792 if (!f) {
793 f = fopen(path, "w");
794 if (!f) {
795 wsyserror(_("could not create %s"), path);
796 goto end;
798 fprintf(f, "#!/bin/sh\n");
799 fputs(line, f);
800 fputs("\n", f);
801 } else {
802 int len = strlen(cmd);
803 int ok = 0;
804 char *tmppath;
805 FILE *fo;
807 tmppath = wmalloc(strlen(p)+64);
808 sprintf(tmppath, "%s/Library/WindowMaker/autostart.tmp", p);
809 fo = fopen(tmppath, "w");
810 if (!fo) {
811 wsyserror(_("could not create temporary file %s"), tmppath);
812 goto end;
815 while (!feof(f)) {
816 if (!fgets(buffer, 127, f)) {
817 break;
819 if (strncmp(buffer, cmd, len)==0) {
820 if (!ok) {
821 fputs(line, fo);
822 fputs("\n", fo);
823 ok = 1;
825 } else {
826 fputs(buffer, fo);
829 if (!ok) {
830 fputs(line, fo);
831 fputs("\n", fo);
833 fclose(fo);
835 if (rename(tmppath, path)!=0) {
836 wsyserror(_("could not rename file %s to %s\n"), tmppath, path);
838 free(tmppath);
840 sprintf(buffer, "chmod u+x %s", path);
841 system(buffer);
843 end:
844 free(p);
845 free(path);
846 if (f)
847 fclose(f);
851 static void
852 storeData(_Panel *panel)
854 char buffer[64];
855 int i;
856 char *tmp, *p;
857 static char *button[3] = {"left", "middle", "right"};
859 tmp = WMGetTextFieldText(panel->threT);
860 if (strlen(tmp)==0) {
861 free(tmp);
862 tmp = wstrdup("0");
865 sprintf(buffer, XSET" m %i/%i %s\n", (int)(panel->acceleration*10),10, tmp);
866 storeCommandInScript(XSET" m", buffer);
868 free(tmp);
870 for (i=0; i<5; i++) {
871 if (WMGetButtonSelected(panel->ddelaB[i]))
872 break;
874 SetIntegerForKey(DELAY(i), "DoubleClickTime");
876 SetBoolForKey(WMGetButtonSelected(panel->disaB), "DisableWSMouseActions");
878 for (i=0; i<3; i++) {
879 if (WMGetButtonSelected(panel->amb[i]))
880 break;
882 if (i<3)
883 SetStringForKey(button[i], "ApplicationMenuMouseButton");
885 for (i=0; i<3; i++) {
886 if (WMGetButtonSelected(panel->lmb[i]))
887 break;
889 if (i<3)
890 SetStringForKey(button[i], "WindowListMouseButton");
892 for (i=0; i<3; i++) {
893 if (WMGetButtonSelected(panel->smb[i]))
894 break;
896 if (i<3)
897 SetStringForKey(button[i], "SelectWindowsMouseButton");
899 tmp = WMGetPopUpButtonItem(panel->grabP,
900 WMGetPopUpButtonSelectedItem(panel->grabP));
901 tmp = wstrdup(tmp);
902 p = strchr(tmp, ' ');
903 *p = 0;
905 SetStringForKey(tmp, "ModifierKey");
907 free(tmp);
911 Panel*
912 InitMouseSettings(WMScreen *scr, WMWindow *win)
914 _Panel *panel;
916 panel = wmalloc(sizeof(_Panel));
917 memset(panel, 0, sizeof(_Panel));
919 panel->sectionName = _("Mouse Preferences");
921 panel->win = win;
923 panel->callbacks.createWidgets = createPanel;
924 panel->callbacks.updateDomain = storeData;
926 AddSection(panel, ICON_FILE);
928 return panel;