- s/sprintf/snprintf
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blobecbbb634eb0d0bd3b65ca25c6614478f58cc57cf
1 /* KeyboardShortcuts.c- keyboard shortcut bindings
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"
25 #include <ctype.h>
27 #include <X11/keysym.h>
30 typedef struct _Panel {
31 WMBox *box;
33 char *sectionName;
35 char *description;
37 CallbackRec callbacks;
39 WMWidget *parent;
41 WMLabel *actL;
42 WMList *actLs;
44 WMFrame *shoF;
45 WMTextField *shoT;
46 WMButton *cleB;
47 WMButton *defB;
49 WMLabel *instructionsL;
51 WMColor *white;
52 WMColor *black;
53 WMFont *font;
55 /**/
56 char capturing;
57 char **shortcuts;
58 int actionCount;
59 } _Panel;
63 #define ICON_FILE "keyshortcuts"
66 /* must be in the same order as the corresponding items in actions list */
67 static char *keyOptions[] = {
68 "RootMenuKey",
69 "WindowListKey",
70 "WindowMenuKey",
71 "HideKey",
72 "MiniaturizeKey",
73 "CloseKey",
74 "MaximizeKey",
75 "VMaximizeKey",
76 "HMaximizeKey",
77 "RaiseKey",
78 "LowerKey",
79 "RaiseLowerKey",
80 "ShadeKey",
81 "MoveResizeKey",
82 "SelectKey",
83 "FocusNextKey",
84 "FocusPrevKey",
85 "NextWorkspaceKey",
86 "PrevWorkspaceKey",
87 "NextWorkspaceLayerKey",
88 "PrevWorkspaceLayerKey",
89 "Workspace1Key",
90 "Workspace2Key",
91 "Workspace3Key",
92 "Workspace4Key",
93 "Workspace5Key",
94 "Workspace6Key",
95 "Workspace7Key",
96 "Workspace8Key",
97 "Workspace9Key",
98 "Workspace10Key",
99 "WindowShortcut1Key",
100 "WindowShortcut2Key",
101 "WindowShortcut3Key",
102 "WindowShortcut4Key",
103 "WindowShortcut5Key",
104 "WindowShortcut6Key",
105 "WindowShortcut7Key",
106 "WindowShortcut8Key",
107 "WindowShortcut9Key",
108 "WindowShortcut10Key",
109 "ScreenSwitchKey",
110 "ClipRaiseKey",
111 "ClipLowerKey",
112 #ifndef XKB_MODELOCK
113 "ClipRaiseLowerKey"
114 #else
115 "ClipRaiseLowerKey",
116 "ToggleKbdModeKey"
117 #endif /* XKB_MODELOCK */
122 static char*
123 captureShortcut(Display *dpy, _Panel *panel)
125 XEvent ev;
126 KeySym ksym, lksym, uksym;
127 char buffer[64];
128 char *key = NULL;
130 while (panel->capturing) {
131 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
132 WMNextEvent(dpy, &ev);
133 if (ev.type==KeyPress && ev.xkey.keycode!=0) {
134 ksym = XKeycodeToKeysym(dpy, ev.xkey.keycode, 0);
135 if (!IsModifierKey(ksym)) {
136 XConvertCase(ksym, &lksym, &uksym);
137 key=XKeysymToString(uksym);
138 panel->capturing = 0;
139 break;
142 WMHandleEvent(&ev);
145 if (!key)
146 return NULL;
148 buffer[0] = 0;
150 if (ev.xkey.state & ControlMask) {
151 strcat(buffer, "Control+");
153 if (ev.xkey.state & ShiftMask) {
154 strcat(buffer, "Shift+");
156 if (ev.xkey.state & Mod1Mask) {
157 strcat(buffer, "Mod1+");
159 if (ev.xkey.state & Mod2Mask) {
160 strcat(buffer, "Mod2+");
162 if (ev.xkey.state & Mod3Mask) {
163 strcat(buffer, "Mod3+");
165 if (ev.xkey.state & Mod4Mask) {
166 strcat(buffer, "Mod4+");
168 if (ev.xkey.state & Mod5Mask) {
169 strcat(buffer, "Mod5+");
171 strcat(buffer, key);
173 return wstrdup(buffer);
177 static void
178 captureClick(WMWidget *w, void *data)
180 _Panel *panel = (_Panel*)data;
181 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
182 char *shortcut;
184 if (!panel->capturing) {
185 panel->capturing = 1;
186 WMSetButtonText(w, _("Cancel"));
187 WMSetLabelText(panel->instructionsL, _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
188 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync,
189 GrabModeAsync, CurrentTime);
190 shortcut = captureShortcut(dpy, panel);
191 if (shortcut) {
192 int row = WMGetListSelectedItemRow(panel->actLs);
194 WMSetTextFieldText(panel->shoT, shortcut);
195 if (row>=0) {
196 if (panel->shortcuts[row])
197 wfree(panel->shortcuts[row]);
198 panel->shortcuts[row] = shortcut;
200 WMRedisplayWidget(panel->actLs);
201 } else {
202 wfree(shortcut);
206 panel->capturing = 0;
207 WMSetButtonText(w, _("Capture"));
208 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
209 XUngrabKeyboard(dpy, CurrentTime);
214 static void
215 clearShortcut(WMWidget *w, void *data)
217 _Panel *panel = (_Panel*)data;
218 int row = WMGetListSelectedItemRow(panel->actLs);
220 WMSetTextFieldText(panel->shoT, NULL);
222 if (row>=0) {
223 if (panel->shortcuts[row])
224 wfree(panel->shortcuts[row]);
225 panel->shortcuts[row]=NULL;
226 WMRedisplayWidget(panel->actLs);
232 static void
233 typedKeys(void *observerData, WMNotification *notification)
235 _Panel *panel = (_Panel*)observerData;
236 int row = WMGetListSelectedItemRow(panel->actLs);
238 if (row<0)
239 return;
241 if (panel->shortcuts[row])
242 wfree(panel->shortcuts[row]);
243 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
244 if (strlen(panel->shortcuts[row])==0) {
245 wfree(panel->shortcuts[row]);
246 panel->shortcuts[row] = NULL;
248 WMRedisplayWidget(panel->actLs);
253 static void
254 listClick(WMWidget *w, void *data)
256 _Panel *panel = (_Panel*)data;
257 int row = WMGetListSelectedItemRow(w);
259 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
263 static char*
264 trimstr(char *str)
266 char *p = str;
267 int i;
269 while (isspace(*p)) p++;
270 p = wstrdup(p);
271 i = strlen(p);
272 while (isspace(p[i]) && i>0) {
273 p[i]=0;
274 i--;
277 return p;
281 static void
282 showData(_Panel *panel)
284 char *str;
285 int i;
287 for (i=0; i<panel->actionCount; i++) {
289 str = GetStringForKey(keyOptions[i]);
290 if (panel->shortcuts[i])
291 wfree(panel->shortcuts[i]);
292 if (str)
293 panel->shortcuts[i] = trimstr(str);
294 else
295 panel->shortcuts[i] = NULL;
297 if (panel->shortcuts[i] &&
298 (strcasecmp(panel->shortcuts[i], "none")==0
299 || strlen(panel->shortcuts[i])==0)) {
300 wfree(panel->shortcuts[i]);
301 panel->shortcuts[i] = NULL;
304 WMRedisplayWidget(panel->actLs);
308 static void
309 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
310 WMRect *rect)
312 int width, height, x, y;
313 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
314 WMScreen *scr = WMWidgetScreen(lPtr);
315 Display *dpy = WMScreenDisplay(scr);
317 width = rect->size.width;
318 height = rect->size.height;
319 x = rect->pos.x;
320 y = rect->pos.y;
322 if (state & WLDSSelected)
323 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
324 height);
325 else
326 XClearArea(dpy, d, x, y, width, height, False);
328 if (panel->shortcuts[index]) {
329 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
330 WMSize size = WMGetPixmapSize(pix);
332 WMDrawPixmap(pix, d, x+(20-size.width)/2, (height-size.height)/2+y);
333 WMReleasePixmap(pix);
336 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+20, y,
337 text, strlen(text));
341 static void
342 createPanel(Panel *p)
344 _Panel *panel = (_Panel*)p;
345 WMScreen *scr = WMWidgetScreen(panel->parent);
346 WMColor *color;
347 WMFont *boldFont;
349 panel->capturing = 0;
353 panel->box = WMCreateBox(panel->parent);
354 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
356 boldFont = WMBoldSystemFontOfSize(scr, 12);
358 /* **************** Actions **************** */
359 panel->actL = WMCreateLabel(panel->box);
360 WMResizeWidget(panel->actL, 280, 20);
361 WMMoveWidget(panel->actL, 20, 10);
362 WMSetLabelFont(panel->actL, boldFont);
363 WMSetLabelText(panel->actL, _("Actions"));
364 WMSetLabelRelief(panel->actL, WRSunken);
365 WMSetLabelTextAlignment(panel->actL, WACenter);
366 color = WMDarkGrayColor(scr);
367 WMSetWidgetBackgroundColor(panel->actL, color);
368 WMReleaseColor(color);
369 color = WMWhiteColor(scr);
370 WMSetLabelTextColor(panel->actL, color);
371 WMReleaseColor(color);
373 panel->actLs = WMCreateList(panel->box);
374 WMResizeWidget(panel->actLs, 280, 190);
375 WMMoveWidget(panel->actLs, 20, 32);
376 WMSetListUserDrawProc(panel->actLs, paintItem);
377 WMHangData(panel->actLs, panel);
379 WMAddListItem(panel->actLs, _("Open applications menu"));
380 WMAddListItem(panel->actLs, _("Open window list menu"));
381 WMAddListItem(panel->actLs, _("Open window commands menu"));
382 WMAddListItem(panel->actLs, _("Hide active application"));
383 WMAddListItem(panel->actLs, _("Miniaturize active window"));
384 WMAddListItem(panel->actLs, _("Close active window"));
385 WMAddListItem(panel->actLs, _("Maximize active window"));
386 WMAddListItem(panel->actLs, _("Maximize active window vertically"));
387 WMAddListItem(panel->actLs, _("Maximize active window horizontally"));
388 WMAddListItem(panel->actLs, _("Raise active window"));
389 WMAddListItem(panel->actLs, _("Lower active window"));
390 WMAddListItem(panel->actLs, _("Raise/Lower window under mouse pointer"));
391 WMAddListItem(panel->actLs, _("Shade active window"));
392 WMAddListItem(panel->actLs, _("Move/Resize active window"));
393 WMAddListItem(panel->actLs, _("Select active window"));
394 WMAddListItem(panel->actLs, _("Focus next window"));
395 WMAddListItem(panel->actLs, _("Focus previous window"));
396 WMAddListItem(panel->actLs, _("Switch to next workspace"));
397 WMAddListItem(panel->actLs, _("Switch to previous workspace"));
398 WMAddListItem(panel->actLs, _("Switch to next ten workspaces"));
399 WMAddListItem(panel->actLs, _("Switch to previous ten workspaces"));
400 WMAddListItem(panel->actLs, _("Switch to workspace 1"));
401 WMAddListItem(panel->actLs, _("Switch to workspace 2"));
402 WMAddListItem(panel->actLs, _("Switch to workspace 3"));
403 WMAddListItem(panel->actLs, _("Switch to workspace 4"));
404 WMAddListItem(panel->actLs, _("Switch to workspace 5"));
405 WMAddListItem(panel->actLs, _("Switch to workspace 6"));
406 WMAddListItem(panel->actLs, _("Switch to workspace 7"));
407 WMAddListItem(panel->actLs, _("Switch to workspace 8"));
408 WMAddListItem(panel->actLs, _("Switch to workspace 9"));
409 WMAddListItem(panel->actLs, _("Switch to workspace 10"));
410 WMAddListItem(panel->actLs, _("Shortcut for window 1"));
411 WMAddListItem(panel->actLs, _("Shortcut for window 2"));
412 WMAddListItem(panel->actLs, _("Shortcut for window 3"));
413 WMAddListItem(panel->actLs, _("Shortcut for window 4"));
414 WMAddListItem(panel->actLs, _("Shortcut for window 5"));
415 WMAddListItem(panel->actLs, _("Shortcut for window 6"));
416 WMAddListItem(panel->actLs, _("Shortcut for window 7"));
417 WMAddListItem(panel->actLs, _("Shortcut for window 8"));
418 WMAddListItem(panel->actLs, _("Shortcut for window 9"));
419 WMAddListItem(panel->actLs, _("Shortcut for window 10"));
420 WMAddListItem(panel->actLs, _("Switch to Next Screen/Monitor"));
421 WMAddListItem(panel->actLs, _("Raise Clip"));
422 WMAddListItem(panel->actLs, _("Lower Clip"));
423 WMAddListItem(panel->actLs, _("Raise/Lower Clip"));
424 #ifdef XKB_MODELOCK
425 WMAddListItem(panel->actLs, _("Toggle keyboard language"));
426 #endif /* XKB_MODELOCK */
428 WMSetListAction(panel->actLs, listClick, panel);
430 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
431 panel->shortcuts = wmalloc(sizeof(char*)*panel->actionCount);
432 memset(panel->shortcuts, 0, sizeof(char*)*panel->actionCount);
434 /***************** Shortcut ****************/
436 panel->shoF = WMCreateFrame(panel->box);
437 WMResizeWidget(panel->shoF, 190, 210);
438 WMMoveWidget(panel->shoF, 315, 10);
439 WMSetFrameTitle(panel->shoF, _("Shortcut"));
441 panel->shoT = WMCreateTextField(panel->shoF);
442 WMResizeWidget(panel->shoT, 160, 20);
443 WMMoveWidget(panel->shoT, 15, 65);
444 WMAddNotificationObserver(typedKeys, panel,
445 WMTextDidChangeNotification, panel->shoT);
447 panel->cleB = WMCreateCommandButton(panel->shoF);
448 WMResizeWidget(panel->cleB, 75, 24);
449 WMMoveWidget(panel->cleB, 15, 95);
450 WMSetButtonText(panel->cleB, _("Clear"));
451 WMSetButtonAction(panel->cleB, clearShortcut, panel);
453 panel->defB = WMCreateCommandButton(panel->shoF);
454 WMResizeWidget(panel->defB, 75, 24);
455 WMMoveWidget(panel->defB, 100, 95);
456 WMSetButtonText(panel->defB, _("Capture"));
457 WMSetButtonAction(panel->defB, captureClick, panel);
459 panel->instructionsL = WMCreateLabel(panel->shoF);
460 WMResizeWidget(panel->instructionsL, 160, 55);
461 WMMoveWidget(panel->instructionsL, 15, 140);
462 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
463 WMSetLabelWraps(panel->instructionsL, True);
464 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
466 WMMapSubwidgets(panel->shoF);
468 WMReleaseFont(boldFont);
470 WMRealizeWidget(panel->box);
471 WMMapSubwidgets(panel->box);
474 showData(panel);
478 static void
479 storeData(_Panel *panel)
481 int i;
482 char *str;
484 for (i=0; i<panel->actionCount; i++) {
485 str = NULL;
486 if (panel->shortcuts[i]) {
487 str = trimstr(panel->shortcuts[i]);
488 if (strlen(str)==0) {
489 wfree(str);
490 str = NULL;
493 if (str) {
494 SetStringForKey(str, keyOptions[i]);
495 wfree(str);
496 } else {
497 SetStringForKey("None", keyOptions[i]);
504 Panel*
505 InitKeyboardShortcuts(WMScreen *scr, WMWidget *parent)
507 _Panel *panel;
509 panel = wmalloc(sizeof(_Panel));
510 memset(panel, 0, sizeof(_Panel));
512 panel->sectionName = _("Keyboard Shortcut Preferences");
514 panel->description = _("Change the keyboard shortcuts for actions such\n"
515 "as changing workspaces and opening menus.");
517 panel->parent = parent;
519 panel->callbacks.createWidgets = createPanel;
520 panel->callbacks.updateDomain = storeData;
522 panel->white = WMWhiteColor(scr);
523 panel->black = WMBlackColor(scr);
524 panel->font = WMSystemFontOfSize(scr, 12);
526 AddSection(panel, ICON_FILE);
528 return panel;