new italian po files.
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blobbdd5a927c40e2cf46d060887d1fbf4fd5446e96d
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 "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, lksym, uksym;
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 XConvertCase(ksym, &lksym, &uksym);
136 key=XKeysymToString(uksym);
137 panel->capturing = 0;
138 break;
141 WMHandleEvent(&ev);
144 if (!key)
145 return NULL;
147 buffer[0] = 0;
149 if (ev.xkey.state & ControlMask) {
150 strcat(buffer, "Control+");
152 if (ev.xkey.state & ShiftMask) {
153 strcat(buffer, "Shift+");
155 if (ev.xkey.state & Mod1Mask) {
156 strcat(buffer, "Mod1+");
158 if (ev.xkey.state & Mod2Mask) {
159 strcat(buffer, "Mod2+");
161 if (ev.xkey.state & Mod3Mask) {
162 strcat(buffer, "Mod3+");
164 if (ev.xkey.state & Mod4Mask) {
165 strcat(buffer, "Mod4+");
167 if (ev.xkey.state & Mod5Mask) {
168 strcat(buffer, "Mod5+");
170 strcat(buffer, key);
172 return wstrdup(buffer);
176 static void
177 captureClick(WMWidget *w, void *data)
179 _Panel *panel = (_Panel*)data;
180 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
181 char *shortcut;
183 if (!panel->capturing) {
184 panel->capturing = 1;
185 WMSetButtonText(w, _("Cancel"));
186 WMSetLabelText(panel->instructionsL, _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
187 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync,
188 GrabModeAsync, CurrentTime);
189 shortcut = captureShortcut(dpy, panel);
190 if (shortcut) {
191 int row = WMGetListSelectedItemRow(panel->actLs);
193 WMSetTextFieldText(panel->shoT, shortcut);
194 if (row>=0) {
195 if (panel->shortcuts[row])
196 wfree(panel->shortcuts[row]);
197 panel->shortcuts[row] = shortcut;
199 WMRedisplayWidget(panel->actLs);
200 } else {
201 wfree(shortcut);
205 panel->capturing = 0;
206 WMSetButtonText(w, _("Capture"));
207 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
208 XUngrabKeyboard(dpy, CurrentTime);
213 static void
214 clearShortcut(WMWidget *w, void *data)
216 _Panel *panel = (_Panel*)data;
217 int row = WMGetListSelectedItemRow(panel->actLs);
219 WMSetTextFieldText(panel->shoT, NULL);
221 if (row>=0) {
222 if (panel->shortcuts[row])
223 wfree(panel->shortcuts[row]);
224 panel->shortcuts[row]=NULL;
225 WMRedisplayWidget(panel->actLs);
231 static void
232 typedKeys(void *observerData, WMNotification *notification)
234 _Panel *panel = (_Panel*)observerData;
235 int row = WMGetListSelectedItemRow(panel->actLs);
237 if (row<0)
238 return;
240 if (panel->shortcuts[row])
241 wfree(panel->shortcuts[row]);
242 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
243 if (strlen(panel->shortcuts[row])==0) {
244 wfree(panel->shortcuts[row]);
245 panel->shortcuts[row] = NULL;
247 WMRedisplayWidget(panel->actLs);
252 static void
253 listClick(WMWidget *w, void *data)
255 _Panel *panel = (_Panel*)data;
256 int row = WMGetListSelectedItemRow(w);
258 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
262 static char*
263 trimstr(char *str)
265 char *p = str;
266 int i;
268 while (isspace(*p)) p++;
269 p = wstrdup(p);
270 i = strlen(p);
271 while (isspace(p[i]) && i>0) {
272 p[i]=0;
273 i--;
276 return p;
280 static void
281 showData(_Panel *panel)
283 char *str;
284 int i;
286 for (i=0; i<panel->actionCount; i++) {
288 str = GetStringForKey(keyOptions[i]);
289 if (panel->shortcuts[i])
290 wfree(panel->shortcuts[i]);
291 if (str)
292 panel->shortcuts[i] = trimstr(str);
293 else
294 panel->shortcuts[i] = NULL;
296 if (panel->shortcuts[i] &&
297 (strcasecmp(panel->shortcuts[i], "none")==0
298 || strlen(panel->shortcuts[i])==0)) {
299 wfree(panel->shortcuts[i]);
300 panel->shortcuts[i] = NULL;
303 WMRedisplayWidget(panel->actLs);
307 static void
308 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
309 WMRect *rect)
311 int width, height, x, y;
312 _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
313 WMScreen *scr = WMWidgetScreen(lPtr);
314 Display *dpy = WMScreenDisplay(scr);
316 width = rect->size.width;
317 height = rect->size.height;
318 x = rect->pos.x;
319 y = rect->pos.y;
321 if (state & WLDSSelected)
322 XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width,
323 height);
324 else
325 XClearArea(dpy, d, x, y, width, height, False);
327 if (panel->shortcuts[index]) {
328 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
329 WMSize size = WMGetPixmapSize(pix);
331 WMDrawPixmap(pix, d, x+(20-size.width)/2, (height-size.height)/2+y);
332 WMReleasePixmap(pix);
335 WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+20, y,
336 text, strlen(text));
340 static void
341 createPanel(Panel *p)
343 _Panel *panel = (_Panel*)p;
344 WMScreen *scr = WMWidgetScreen(panel->parent);
345 WMColor *color;
346 WMFont *boldFont;
348 panel->capturing = 0;
352 panel->box = WMCreateBox(panel->parent);
353 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
355 boldFont = WMBoldSystemFontOfSize(scr, 12);
357 /* **************** Actions **************** */
358 panel->actL = WMCreateLabel(panel->box);
359 WMResizeWidget(panel->actL, 280, 20);
360 WMMoveWidget(panel->actL, 20, 10);
361 WMSetLabelFont(panel->actL, boldFont);
362 WMSetLabelText(panel->actL, _("Actions"));
363 WMSetLabelRelief(panel->actL, WRSunken);
364 WMSetLabelTextAlignment(panel->actL, WACenter);
365 color = WMDarkGrayColor(scr);
366 WMSetWidgetBackgroundColor(panel->actL, color);
367 WMReleaseColor(color);
368 color = WMWhiteColor(scr);
369 WMSetLabelTextColor(panel->actL, color);
370 WMReleaseColor(color);
372 panel->actLs = WMCreateList(panel->box);
373 WMResizeWidget(panel->actLs, 280, 190);
374 WMMoveWidget(panel->actLs, 20, 32);
375 WMSetListUserDrawProc(panel->actLs, paintItem);
376 WMHangData(panel->actLs, panel);
378 WMAddListItem(panel->actLs, _("Open applications menu"));
379 WMAddListItem(panel->actLs, _("Open window list menu"));
380 WMAddListItem(panel->actLs, _("Open window commands menu"));
381 WMAddListItem(panel->actLs, _("Hide active application"));
382 WMAddListItem(panel->actLs, _("Miniaturize active window"));
383 WMAddListItem(panel->actLs, _("Close active window"));
384 WMAddListItem(panel->actLs, _("Maximize active window"));
385 WMAddListItem(panel->actLs, _("Maximize active window vertically"));
386 WMAddListItem(panel->actLs, _("Raise active window"));
387 WMAddListItem(panel->actLs, _("Lower active window"));
388 WMAddListItem(panel->actLs, _("Raise/Lower window under mouse pointer"));
389 WMAddListItem(panel->actLs, _("Shade active window"));
390 WMAddListItem(panel->actLs, _("Move/Resize active window"));
391 WMAddListItem(panel->actLs, _("Select active window"));
392 WMAddListItem(panel->actLs, _("Focus next window"));
393 WMAddListItem(panel->actLs, _("Focus previous window"));
394 WMAddListItem(panel->actLs, _("Switch to next workspace"));
395 WMAddListItem(panel->actLs, _("Switch to previous workspace"));
396 WMAddListItem(panel->actLs, _("Switch to next ten workspaces"));
397 WMAddListItem(panel->actLs, _("Switch to previous ten workspaces"));
398 WMAddListItem(panel->actLs, _("Switch to workspace 1"));
399 WMAddListItem(panel->actLs, _("Switch to workspace 2"));
400 WMAddListItem(panel->actLs, _("Switch to workspace 3"));
401 WMAddListItem(panel->actLs, _("Switch to workspace 4"));
402 WMAddListItem(panel->actLs, _("Switch to workspace 5"));
403 WMAddListItem(panel->actLs, _("Switch to workspace 6"));
404 WMAddListItem(panel->actLs, _("Switch to workspace 7"));
405 WMAddListItem(panel->actLs, _("Switch to workspace 8"));
406 WMAddListItem(panel->actLs, _("Switch to workspace 9"));
407 WMAddListItem(panel->actLs, _("Switch to workspace 10"));
408 WMAddListItem(panel->actLs, _("Shortcut for window 1"));
409 WMAddListItem(panel->actLs, _("Shortcut for window 2"));
410 WMAddListItem(panel->actLs, _("Shortcut for window 3"));
411 WMAddListItem(panel->actLs, _("Shortcut for window 4"));
412 WMAddListItem(panel->actLs, _("Shortcut for window 5"));
413 WMAddListItem(panel->actLs, _("Shortcut for window 6"));
414 WMAddListItem(panel->actLs, _("Shortcut for window 7"));
415 WMAddListItem(panel->actLs, _("Shortcut for window 8"));
416 WMAddListItem(panel->actLs, _("Shortcut for window 9"));
417 WMAddListItem(panel->actLs, _("Shortcut for window 10"));
418 WMAddListItem(panel->actLs, _("Switch to Next Screen/Monitor"));
419 WMAddListItem(panel->actLs, _("Raise Clip"));
420 WMAddListItem(panel->actLs, _("Lower Clip"));
421 WMAddListItem(panel->actLs, _("Raise/Lower Clip"));
422 #ifdef XKB_MODELOCK
423 WMAddListItem(panel->actLs, _("Toggle keyboard language"));
424 #endif /* XKB_MODELOCK */
426 WMSetListAction(panel->actLs, listClick, panel);
428 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
429 panel->shortcuts = wmalloc(sizeof(char*)*panel->actionCount);
430 memset(panel->shortcuts, 0, sizeof(char*)*panel->actionCount);
432 /***************** Shortcut ****************/
434 panel->shoF = WMCreateFrame(panel->box);
435 WMResizeWidget(panel->shoF, 190, 210);
436 WMMoveWidget(panel->shoF, 315, 10);
437 WMSetFrameTitle(panel->shoF, _("Shortcut"));
439 panel->shoT = WMCreateTextField(panel->shoF);
440 WMResizeWidget(panel->shoT, 160, 20);
441 WMMoveWidget(panel->shoT, 15, 65);
442 WMAddNotificationObserver(typedKeys, panel,
443 WMTextDidChangeNotification, panel->shoT);
445 panel->cleB = WMCreateCommandButton(panel->shoF);
446 WMResizeWidget(panel->cleB, 75, 24);
447 WMMoveWidget(panel->cleB, 15, 95);
448 WMSetButtonText(panel->cleB, _("Clear"));
449 WMSetButtonAction(panel->cleB, clearShortcut, panel);
451 panel->defB = WMCreateCommandButton(panel->shoF);
452 WMResizeWidget(panel->defB, 75, 24);
453 WMMoveWidget(panel->defB, 100, 95);
454 WMSetButtonText(panel->defB, _("Capture"));
455 WMSetButtonAction(panel->defB, captureClick, panel);
457 panel->instructionsL = WMCreateLabel(panel->shoF);
458 WMResizeWidget(panel->instructionsL, 160, 55);
459 WMMoveWidget(panel->instructionsL, 15, 140);
460 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
461 WMSetLabelWraps(panel->instructionsL, True);
462 WMSetLabelText(panel->instructionsL, _("Click Capture to interactively define the shortcut key."));
464 WMMapSubwidgets(panel->shoF);
466 WMReleaseFont(boldFont);
468 WMRealizeWidget(panel->box);
469 WMMapSubwidgets(panel->box);
472 showData(panel);
476 static void
477 storeData(_Panel *panel)
479 int i;
480 char *str;
482 for (i=0; i<panel->actionCount; i++) {
483 str = NULL;
484 if (panel->shortcuts[i]) {
485 str = trimstr(panel->shortcuts[i]);
486 if (strlen(str)==0) {
487 wfree(str);
488 str = NULL;
491 if (str) {
492 SetStringForKey(str, keyOptions[i]);
493 wfree(str);
494 } else {
495 SetStringForKey("None", keyOptions[i]);
502 Panel*
503 InitKeyboardShortcuts(WMScreen *scr, WMWidget *parent)
505 _Panel *panel;
507 panel = wmalloc(sizeof(_Panel));
508 memset(panel, 0, sizeof(_Panel));
510 panel->sectionName = _("Keyboard Shortcut Preferences");
512 panel->description = _("Change the keyboard shortcuts for actions such\n"
513 "as changing workspaces and opening menus.");
515 panel->parent = parent;
517 panel->callbacks.createWidgets = createPanel;
518 panel->callbacks.updateDomain = storeData;
520 panel->white = WMWhiteColor(scr);
521 panel->black = WMBlackColor(scr);
522 panel->font = WMSystemFontOfSize(scr, 12);
524 AddSection(panel, ICON_FILE);
526 return panel;