added ScreenSwitchKey
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blobf8ee69156dcadaaea27b6dcdf6e00dd3bf6d4d72
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 WMFrame *frame;
33 char *sectionName;
35 char *description;
37 CallbackRec callbacks;
39 WMWindow *win;
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 "RaiseKey",
77 "LowerKey",
78 "RaiseLowerKey",
79 "ShadeKey",
80 "MoveResizeKey",
81 "SelectKey",
82 "FocusNextKey",
83 "FocusPrevKey",
84 "NextWorkspaceKey",
85 "PrevWorkspaceKey",
86 "NextWorkspaceLayerKey",
87 "PrevWorkspaceLayerKey",
88 "Workspace1Key",
89 "Workspace2Key",
90 "Workspace3Key",
91 "Workspace4Key",
92 "Workspace5Key",
93 "Workspace6Key",
94 "Workspace7Key",
95 "Workspace8Key",
96 "Workspace9Key",
97 "Workspace10Key",
98 "WindowShortcut1Key",
99 "WindowShortcut2Key",
100 "WindowShortcut3Key",
101 "WindowShortcut4Key",
102 "WindowShortcut5Key",
103 "WindowShortcut6Key",
104 "WindowShortcut7Key",
105 "WindowShortcut8Key",
106 "WindowShortcut9Key",
107 "WindowShortcut10Key",
108 "ScreenSwitchKey",
109 "ClipRaiseKey",
110 "ClipLowerKey",
111 #ifndef XKB_MODELOCK
112 "ClipRaiseLowerKey"
113 #else
114 "ClipRaiseLowerKey",
115 "ToggleKbdModeKey"
116 #endif /* XKB_MODELOCK */
121 static char*
122 captureShortcut(Display *dpy, _Panel *panel)
124 XEvent ev;
125 KeySym ksym;
126 char buffer[64];
127 char *key = NULL;
129 while (panel->capturing) {
130 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
131 WMNextEvent(dpy, &ev);
132 if (ev.type==KeyPress && ev.xkey.keycode!=0) {
133 ksym = XKeycodeToKeysym(dpy, ev.xkey.keycode, 0);
134 if (!IsModifierKey(ksym)) {
135 key=XKeysymToString(ksym);
136 panel->capturing = 0;
137 break;
140 WMHandleEvent(&ev);
143 if (!key)
144 return NULL;
146 buffer[0] = 0;
148 if (ev.xkey.state & ControlMask) {
149 strcat(buffer, "Control+");
151 if (ev.xkey.state & ShiftMask) {
152 strcat(buffer, "Shift+");
154 if (ev.xkey.state & Mod1Mask) {
155 strcat(buffer, "Mod1+");
157 if (ev.xkey.state & Mod2Mask) {
158 strcat(buffer, "Mod2+");
160 if (ev.xkey.state & Mod3Mask) {
161 strcat(buffer, "Mod3+");
163 if (ev.xkey.state & Mod4Mask) {
164 strcat(buffer, "Mod4+");
166 if (ev.xkey.state & Mod5Mask) {
167 strcat(buffer, "Mod5+");
169 strcat(buffer, key);
171 return wstrdup(buffer);
175 static void
176 captureClick(WMWidget *w, void *data)
178 _Panel *panel = (_Panel*)data;
179 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->win));
180 char *shortcut;
182 if (!panel->capturing) {
183 panel->capturing = 1;
184 WMSetButtonText(w, _("Cancel"));
185 WMSetLabelText(panel->instructionsL, _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
186 XGrabKeyboard(dpy, WMWidgetXID(panel->win), True, GrabModeAsync,
187 GrabModeAsync, CurrentTime);
188 shortcut = captureShortcut(dpy, panel);
189 if (shortcut) {
190 int row = WMGetListSelectedItemRow(panel->actLs);
192 WMSetTextFieldText(panel->shoT, shortcut);
193 if (row>=0) {
194 if (panel->shortcuts[row])
195 wfree(panel->shortcuts[row]);
196 panel->shortcuts[row] = shortcut;
198 WMRedisplayWidget(panel->actLs);
199 } else {
200 wfree(shortcut);
204 panel->capturing = 0;
205 WMSetButtonText(w, _("Capture"));
206 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
207 XUngrabKeyboard(dpy, CurrentTime);
212 static void
213 clearShortcut(WMWidget *w, void *data)
215 _Panel *panel = (_Panel*)data;
216 int row = WMGetListSelectedItemRow(panel->actLs);
218 WMSetTextFieldText(panel->shoT, NULL);
220 if (row>=0) {
221 if (panel->shortcuts[row])
222 wfree(panel->shortcuts[row]);
223 panel->shortcuts[row]=NULL;
224 WMRedisplayWidget(panel->actLs);
230 static void
231 typedKeys(void *observerData, WMNotification *notification)
233 _Panel *panel = (_Panel*)observerData;
234 int row = WMGetListSelectedItemRow(panel->actLs);
236 if (row<0)
237 return;
239 if (panel->shortcuts[row])
240 wfree(panel->shortcuts[row]);
241 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
242 if (strlen(panel->shortcuts[row])==0) {
243 wfree(panel->shortcuts[row]);
244 panel->shortcuts[row] = NULL;
246 WMRedisplayWidget(panel->actLs);
251 static void
252 listClick(WMWidget *w, void *data)
254 _Panel *panel = (_Panel*)data;
255 int row = WMGetListSelectedItemRow(w);
257 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
261 static char*
262 trimstr(char *str)
264 char *p = str;
265 int i;
267 while (isspace(*p)) p++;
268 p = wstrdup(p);
269 i = strlen(p);
270 while (isspace(p[i]) && i>0) {
271 p[i]=0;
272 i--;
275 return p;
279 static void
280 showData(_Panel *panel)
282 char *str;
283 int i;
285 for (i=0; i<panel->actionCount; i++) {
287 str = GetStringForKey(keyOptions[i]);
288 if (panel->shortcuts[i])
289 wfree(panel->shortcuts[i]);
290 if (str)
291 panel->shortcuts[i] = trimstr(str);
292 else
293 panel->shortcuts[i] = NULL;
295 if (panel->shortcuts[i] &&
296 (strcasecmp(panel->shortcuts[i], "none")==0
297 || strlen(panel->shortcuts[i])==0)) {
298 wfree(panel->shortcuts[i]);
299 panel->shortcuts[i] = NULL;
302 WMRedisplayWidget(panel->actLs);
306 static void
307 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
308 WMRect *rect)
310 int width, height, x, y;
311 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
312 WMScreen *scr = WMWidgetScreen(lPtr);
313 Display *dpy = WMScreenDisplay(scr);
315 width = rect->size.width;
316 height = rect->size.height;
317 x = rect->pos.x;
318 y = rect->pos.y;
320 if (state & WLDSSelected)
321 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
322 height);
323 else
324 XClearArea(dpy, d, x, y, width, height, False);
326 if (panel->shortcuts[index]) {
327 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
328 WMSize size = WMGetPixmapSize(pix);
330 WMDrawPixmap(pix, d, x+(20-size.width)/2, (height-size.height)/2+y);
331 WMReleasePixmap(pix);
334 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+20, y,
335 text, strlen(text));
339 static void
340 createPanel(Panel *p)
342 _Panel *panel = (_Panel*)p;
343 WMScreen *scr = WMWidgetScreen(panel->win);
344 WMColor *color;
345 WMFont *boldFont;
347 panel->capturing = 0;
349 panel->frame = WMCreateFrame(panel->win);
350 WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
351 WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
353 boldFont = WMBoldSystemFontOfSize(scr, 12);
355 /* **************** Actions **************** */
356 panel->actL = WMCreateLabel(panel->frame);
357 WMResizeWidget(panel->actL, 280, 20);
358 WMMoveWidget(panel->actL, 20, 10);
359 WMSetLabelFont(panel->actL, boldFont);
360 WMSetLabelText(panel->actL, _("Actions"));
361 WMSetLabelRelief(panel->actL, WRSunken);
362 WMSetLabelTextAlignment(panel->actL, WACenter);
363 color = WMDarkGrayColor(scr);
364 WMSetWidgetBackgroundColor(panel->actL, color);
365 WMReleaseColor(color);
366 color = WMWhiteColor(scr);
367 WMSetLabelTextColor(panel->actL, color);
368 WMReleaseColor(color);
370 panel->actLs = WMCreateList(panel->frame);
371 WMResizeWidget(panel->actLs, 280, 190);
372 WMMoveWidget(panel->actLs, 20, 32);
373 WMSetListUserDrawProc(panel->actLs, paintItem);
374 WMHangData(panel->actLs, panel);
376 WMAddListItem(panel->actLs, _("Open applications menu"));
377 WMAddListItem(panel->actLs, _("Open window list menu"));
378 WMAddListItem(panel->actLs, _("Open window commands menu"));
379 WMAddListItem(panel->actLs, _("Hide active application"));
380 WMAddListItem(panel->actLs, _("Miniaturize active window"));
381 WMAddListItem(panel->actLs, _("Close active window"));
382 WMAddListItem(panel->actLs, _("Maximize active window"));
383 WMAddListItem(panel->actLs, _("Maximize active window vertically"));
384 WMAddListItem(panel->actLs, _("Raise active window"));
385 WMAddListItem(panel->actLs, _("Lower active window"));
386 WMAddListItem(panel->actLs, _("Raise/Lower window under mouse pointer"));
387 WMAddListItem(panel->actLs, _("Shade active window"));
388 WMAddListItem(panel->actLs, _("Move/Resize active window"));
389 WMAddListItem(panel->actLs, _("Select active window"));
390 WMAddListItem(panel->actLs, _("Focus next window"));
391 WMAddListItem(panel->actLs, _("Focus previous window"));
392 WMAddListItem(panel->actLs, _("Switch to next workspace"));
393 WMAddListItem(panel->actLs, _("Switch to previous workspace"));
394 WMAddListItem(panel->actLs, _("Switch to next ten workspaces"));
395 WMAddListItem(panel->actLs, _("Switch to previous ten workspaces"));
396 WMAddListItem(panel->actLs, _("Switch to workspace 1"));
397 WMAddListItem(panel->actLs, _("Switch to workspace 2"));
398 WMAddListItem(panel->actLs, _("Switch to workspace 3"));
399 WMAddListItem(panel->actLs, _("Switch to workspace 4"));
400 WMAddListItem(panel->actLs, _("Switch to workspace 5"));
401 WMAddListItem(panel->actLs, _("Switch to workspace 6"));
402 WMAddListItem(panel->actLs, _("Switch to workspace 7"));
403 WMAddListItem(panel->actLs, _("Switch to workspace 8"));
404 WMAddListItem(panel->actLs, _("Switch to workspace 9"));
405 WMAddListItem(panel->actLs, _("Switch to workspace 10"));
406 WMAddListItem(panel->actLs, _("Shortcut for window 1"));
407 WMAddListItem(panel->actLs, _("Shortcut for window 2"));
408 WMAddListItem(panel->actLs, _("Shortcut for window 3"));
409 WMAddListItem(panel->actLs, _("Shortcut for window 4"));
410 WMAddListItem(panel->actLs, _("Shortcut for window 5"));
411 WMAddListItem(panel->actLs, _("Shortcut for window 6"));
412 WMAddListItem(panel->actLs, _("Shortcut for window 7"));
413 WMAddListItem(panel->actLs, _("Shortcut for window 8"));
414 WMAddListItem(panel->actLs, _("Shortcut for window 9"));
415 WMAddListItem(panel->actLs, _("Shortcut for window 10"));
416 WMAddListItem(panel->actLs, _("Switch to Next Screen/Monitor"));
417 WMAddListItem(panel->actLs, _("Raise Clip"));
418 WMAddListItem(panel->actLs, _("Lower Clip"));
419 WMAddListItem(panel->actLs, _("Raise/Lower Clip"));
420 #ifdef XKB_MODELOCK
421 WMAddListItem(panel->actLs, _("Toggle keyboard language"));
422 #endif /* XKB_MODELOCK */
424 WMSetListAction(panel->actLs, listClick, panel);
426 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
427 panel->shortcuts = wmalloc(sizeof(char*)*panel->actionCount);
428 memset(panel->shortcuts, 0, sizeof(char*)*panel->actionCount);
430 /***************** Shortcut ****************/
432 panel->shoF = WMCreateFrame(panel->frame);
433 WMResizeWidget(panel->shoF, 190, 210);
434 WMMoveWidget(panel->shoF, 315, 10);
435 WMSetFrameTitle(panel->shoF, _("Shortcut"));
437 panel->shoT = WMCreateTextField(panel->shoF);
438 WMResizeWidget(panel->shoT, 160, 20);
439 WMMoveWidget(panel->shoT, 15, 65);
440 WMAddNotificationObserver(typedKeys, panel,
441 WMTextDidChangeNotification, panel->shoT);
443 panel->cleB = WMCreateCommandButton(panel->shoF);
444 WMResizeWidget(panel->cleB, 75, 24);
445 WMMoveWidget(panel->cleB, 15, 95);
446 WMSetButtonText(panel->cleB, _("Clear"));
447 WMSetButtonAction(panel->cleB, clearShortcut, panel);
449 panel->defB = WMCreateCommandButton(panel->shoF);
450 WMResizeWidget(panel->defB, 75, 24);
451 WMMoveWidget(panel->defB, 100, 95);
452 WMSetButtonText(panel->defB, _("Capture"));
453 WMSetButtonAction(panel->defB, captureClick, panel);
455 panel->instructionsL = WMCreateLabel(panel->shoF);
456 WMResizeWidget(panel->instructionsL, 160, 55);
457 WMMoveWidget(panel->instructionsL, 15, 140);
458 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
459 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
461 WMMapSubwidgets(panel->shoF);
463 WMReleaseFont(boldFont);
465 WMRealizeWidget(panel->frame);
466 WMMapSubwidgets(panel->frame);
469 showData(panel);
473 static void
474 storeData(_Panel *panel)
476 int i;
477 char *str;
479 for (i=0; i<panel->actionCount; i++) {
480 str = NULL;
481 if (panel->shortcuts[i]) {
482 str = trimstr(panel->shortcuts[i]);
483 if (strlen(str)==0) {
484 wfree(str);
485 str = NULL;
488 if (str) {
489 SetStringForKey(str, keyOptions[i]);
490 wfree(str);
491 } else {
492 SetStringForKey("None", keyOptions[i]);
499 Panel*
500 InitKeyboardShortcuts(WMScreen *scr, WMWindow *win)
502 _Panel *panel;
504 panel = wmalloc(sizeof(_Panel));
505 memset(panel, 0, sizeof(_Panel));
507 panel->sectionName = _("Keyboard Shortcut Preferences");
509 panel->description = _("Change the keyboard shortcuts for actions such\n"
510 "as changing workspaces and opening menus.");
512 panel->win = win;
514 panel->callbacks.createWidgets = createPanel;
515 panel->callbacks.updateDomain = storeData;
517 panel->white = WMWhiteColor(scr);
518 panel->black = WMBlackColor(scr);
519 panel->font = WMSystemFontOfSize(scr, 12);
521 AddSection(panel, ICON_FILE);
523 return panel;