explicit restore to MAX_MAXIMUS instead of empty flag at handleMaximize
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blob4324b03b9fd794c0267bf9358d59f4723480e967
1 /* KeyboardShortcuts.c- keyboard shortcut bindings
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "config.h" /* for HAVE_XCONVERTCASE */
24 #include "WPrefs.h"
25 #include <ctype.h>
27 #include <X11/keysym.h>
28 #include <X11/XKBlib.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 WMColor *gray;
54 WMFont *font;
56 Bool capturing;
57 char **shortcuts;
58 int actionCount;
59 } _Panel;
61 #define ICON_FILE "keyshortcuts"
64 * List of user definable shortcut keys
65 * First parameter is the internal keyword known by WMaker
66 * Second is the text displayed to the user
68 static const struct {
69 const char *key;
70 const char *title;
71 } keyOptions[] = {
72 { "RootMenuKey", N_("Open applications menu") },
73 { "WindowListKey", N_("Open window list menu") },
74 { "WindowMenuKey", N_("Open window commands menu") },
75 { "HideKey", N_("Hide active application") },
76 { "HideOthersKey", N_("Hide other applications") },
77 { "MiniaturizeKey", N_("Miniaturize active window") },
78 { "MinimizeAllKey", N_("Miniaturize all windows") },
79 { "CloseKey", N_("Close active window") },
80 { "MaximizeKey", N_("Maximize active window") },
81 { "VMaximizeKey", N_("Maximize active window vertically") },
82 { "HMaximizeKey", N_("Maximize active window horizontally") },
83 { "LHMaximizeKey", N_("Maximize active window left half") },
84 { "RHMaximizeKey", N_("Maximize active window right half") },
85 { "THMaximizeKey", N_("Maximize active window top half") },
86 { "BHMaximizeKey", N_("Maximize active window bottom half") },
87 { "LTCMaximizeKey", N_("Maximize active window left top corner") },
88 { "RTCMaximizeKey", N_("Maximize active window right top corner") },
89 { "LBCMaximizeKey", N_("Maximize active window left bottom corner") },
90 { "RBCMaximizeKey", N_("Maximize active window right bottom corner") },
91 { "MaximusKey", N_("Maximus: Tiled maximization ") },
92 { "RaiseKey", N_("Raise active window") },
93 { "LowerKey", N_("Lower active window") },
94 { "RaiseLowerKey", N_("Raise/Lower window under mouse pointer") },
95 { "ShadeKey", N_("Shade active window") },
96 { "MoveResizeKey", N_("Move/Resize active window") },
97 { "SelectKey", N_("Select active window") },
98 { "FocusNextKey", N_("Focus next window") },
99 { "FocusPrevKey", N_("Focus previous window") },
100 { "GroupNextKey", N_("Focus next group window") },
101 { "GroupPrevKey", N_("Focus previous group window") },
103 /* Workspace Related */
104 { "NextWorkspaceKey", N_("Switch to next workspace") },
105 { "PrevWorkspaceKey", N_("Switch to previous workspace") },
106 { "LastWorkspaceKey", N_("Switch to last used workspace") },
107 { "NextWorkspaceLayerKey", N_("Switch to next ten workspaces") },
108 { "PrevWorkspaceLayerKey", N_("Switch to previous ten workspaces") },
109 { "Workspace1Key", N_("Switch to workspace 1") },
110 { "Workspace2Key", N_("Switch to workspace 2") },
111 { "Workspace3Key", N_("Switch to workspace 3") },
112 { "Workspace4Key", N_("Switch to workspace 4") },
113 { "Workspace5Key", N_("Switch to workspace 5") },
114 { "Workspace6Key", N_("Switch to workspace 6") },
115 { "Workspace7Key", N_("Switch to workspace 7") },
116 { "Workspace8Key", N_("Switch to workspace 8") },
117 { "Workspace9Key", N_("Switch to workspace 9") },
118 { "Workspace10Key", N_("Switch to workspace 10") },
119 { "MoveToNextWorkspaceKey", N_("Move window to next workspace") },
120 { "MoveToPrevWorkspaceKey", N_("Move window to previous workspace") },
121 { "MoveToLastWorkspaceKey", N_("Move window to last used workspace") },
122 { "MoveToNextWorkspaceLayerKey", N_("Move window to next ten workspaces") },
123 { "MoveToPrevWorkspaceLayerKey", N_("Move window to previous ten workspaces") },
124 { "MoveToWorkspace1Key", N_("Move window to workspace 1") },
125 { "MoveToWorkspace2Key", N_("Move window to workspace 2") },
126 { "MoveToWorkspace3Key", N_("Move window to workspace 3") },
127 { "MoveToWorkspace4Key", N_("Move window to workspace 4") },
128 { "MoveToWorkspace5Key", N_("Move window to workspace 5") },
129 { "MoveToWorkspace6Key", N_("Move window to workspace 6") },
130 { "MoveToWorkspace7Key", N_("Move window to workspace 7") },
131 { "MoveToWorkspace8Key", N_("Move window to workspace 8") },
132 { "MoveToWorkspace9Key", N_("Move window to workspace 9") },
133 { "MoveToWorkspace10Key", N_("Move window to workspace 10") },
135 /* Window Selection */
136 { "WindowShortcut1Key", N_("Shortcut for window 1") },
137 { "WindowShortcut2Key", N_("Shortcut for window 2") },
138 { "WindowShortcut3Key", N_("Shortcut for window 3") },
139 { "WindowShortcut4Key", N_("Shortcut for window 4") },
140 { "WindowShortcut5Key", N_("Shortcut for window 5") },
141 { "WindowShortcut6Key", N_("Shortcut for window 6") },
142 { "WindowShortcut7Key", N_("Shortcut for window 7") },
143 { "WindowShortcut8Key", N_("Shortcut for window 8") },
144 { "WindowShortcut9Key", N_("Shortcut for window 9") },
145 { "WindowShortcut10Key", N_("Shortcut for window 10") },
147 /* Misc. */
148 { "WindowRelaunchKey", N_("Launch new instance of application") },
149 { "ScreenSwitchKey", N_("Switch to Next Screen/Monitor") },
150 { "DockRaiseLowerKey", N_("Raise/Lower Dock") },
151 { "ClipRaiseLowerKey", N_("Raise/Lower Clip") }
152 #ifdef XKB_MODELOCK
153 ,{ "ToggleKbdModeKey", N_("Toggle keyboard language") }
154 #endif /* XKB_MODELOCK */
157 #ifndef HAVE_XCONVERTCASE
158 /* from Xlib */
160 static void XConvertCase(register KeySym sym, KeySym * lower, KeySym * upper)
162 *lower = sym;
163 *upper = sym;
164 switch (sym >> 8) {
165 case 0: /* Latin 1 */
166 if ((sym >= XK_A) && (sym <= XK_Z))
167 *lower += (XK_a - XK_A);
168 else if ((sym >= XK_a) && (sym <= XK_z))
169 *upper -= (XK_a - XK_A);
170 else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
171 *lower += (XK_agrave - XK_Agrave);
172 else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
173 *upper -= (XK_agrave - XK_Agrave);
174 else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
175 *lower += (XK_oslash - XK_Ooblique);
176 else if ((sym >= XK_oslash) && (sym <= XK_thorn))
177 *upper -= (XK_oslash - XK_Ooblique);
178 break;
179 case 1: /* Latin 2 */
180 /* Assume the KeySym is a legal value (ignore discontinuities) */
181 if (sym == XK_Aogonek)
182 *lower = XK_aogonek;
183 else if (sym >= XK_Lstroke && sym <= XK_Sacute)
184 *lower += (XK_lstroke - XK_Lstroke);
185 else if (sym >= XK_Scaron && sym <= XK_Zacute)
186 *lower += (XK_scaron - XK_Scaron);
187 else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
188 *lower += (XK_zcaron - XK_Zcaron);
189 else if (sym == XK_aogonek)
190 *upper = XK_Aogonek;
191 else if (sym >= XK_lstroke && sym <= XK_sacute)
192 *upper -= (XK_lstroke - XK_Lstroke);
193 else if (sym >= XK_scaron && sym <= XK_zacute)
194 *upper -= (XK_scaron - XK_Scaron);
195 else if (sym >= XK_zcaron && sym <= XK_zabovedot)
196 *upper -= (XK_zcaron - XK_Zcaron);
197 else if (sym >= XK_Racute && sym <= XK_Tcedilla)
198 *lower += (XK_racute - XK_Racute);
199 else if (sym >= XK_racute && sym <= XK_tcedilla)
200 *upper -= (XK_racute - XK_Racute);
201 break;
202 case 2: /* Latin 3 */
203 /* Assume the KeySym is a legal value (ignore discontinuities) */
204 if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
205 *lower += (XK_hstroke - XK_Hstroke);
206 else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
207 *lower += (XK_gbreve - XK_Gbreve);
208 else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
209 *upper -= (XK_hstroke - XK_Hstroke);
210 else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
211 *upper -= (XK_gbreve - XK_Gbreve);
212 else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
213 *lower += (XK_cabovedot - XK_Cabovedot);
214 else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
215 *upper -= (XK_cabovedot - XK_Cabovedot);
216 break;
217 case 3: /* Latin 4 */
218 /* Assume the KeySym is a legal value (ignore discontinuities) */
219 if (sym >= XK_Rcedilla && sym <= XK_Tslash)
220 *lower += (XK_rcedilla - XK_Rcedilla);
221 else if (sym >= XK_rcedilla && sym <= XK_tslash)
222 *upper -= (XK_rcedilla - XK_Rcedilla);
223 else if (sym == XK_ENG)
224 *lower = XK_eng;
225 else if (sym == XK_eng)
226 *upper = XK_ENG;
227 else if (sym >= XK_Amacron && sym <= XK_Umacron)
228 *lower += (XK_amacron - XK_Amacron);
229 else if (sym >= XK_amacron && sym <= XK_umacron)
230 *upper -= (XK_amacron - XK_Amacron);
231 break;
232 case 6: /* Cyrillic */
233 /* Assume the KeySym is a legal value (ignore discontinuities) */
234 if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
235 *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
236 else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
237 *upper += (XK_Serbian_DJE - XK_Serbian_dje);
238 else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
239 *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
240 else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
241 *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
242 break;
243 case 7: /* Greek */
244 /* Assume the KeySym is a legal value (ignore discontinuities) */
245 if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
246 *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
247 else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
248 sym != XK_Greek_iotaaccentdieresis && sym != XK_Greek_upsilonaccentdieresis)
249 *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
250 else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
251 *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
252 else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega && sym != XK_Greek_finalsmallsigma)
253 *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
254 break;
255 case 0x14: /* Armenian */
256 if (sym >= XK_Armenian_AYB && sym <= XK_Armenian_fe) {
257 *lower = sym | 1;
258 *upper = sym & ~1;
260 break;
263 #endif
265 char *capture_shortcut(Display *dpy, Bool *capturing, Bool convert_case)
267 XEvent ev;
268 KeySym ksym, lksym, uksym;
269 char buffer[64];
270 char *key = NULL;
272 while (*capturing) {
273 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
274 WMNextEvent(dpy, &ev);
275 if (ev.type == KeyPress && ev.xkey.keycode != 0) {
276 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
277 if (!IsModifierKey(ksym)) {
278 if (convert_case) {
279 XConvertCase(ksym, &lksym, &uksym);
280 key = XKeysymToString(uksym);
281 } else {
282 key = XKeysymToString(ksym);
285 *capturing = 0;
286 break;
289 WMHandleEvent(&ev);
292 if (!key)
293 return NULL;
295 buffer[0] = 0;
297 if (ev.xkey.state & ControlMask) {
298 strcat(buffer, "Control+");
300 if (ev.xkey.state & ShiftMask) {
301 strcat(buffer, "Shift+");
303 if (ev.xkey.state & Mod1Mask) {
304 strcat(buffer, "Mod1+");
306 if (ev.xkey.state & Mod2Mask) {
307 strcat(buffer, "Mod2+");
309 if (ev.xkey.state & Mod3Mask) {
310 strcat(buffer, "Mod3+");
312 if (ev.xkey.state & Mod4Mask) {
313 strcat(buffer, "Mod4+");
315 if (ev.xkey.state & Mod5Mask) {
316 strcat(buffer, "Mod5+");
318 strcat(buffer, key);
320 return wstrdup(buffer);
323 static void captureClick(WMWidget * w, void *data)
325 _Panel *panel = (_Panel *) data;
326 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
327 char *shortcut;
329 if (!panel->capturing) {
330 panel->capturing = 1;
331 WMSetButtonText(w, _("Cancel"));
332 WMSetLabelText(panel->instructionsL,
333 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
334 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
335 shortcut = capture_shortcut(dpy, &panel->capturing, 1);
336 if (shortcut) {
337 int row = WMGetListSelectedItemRow(panel->actLs);
339 WMSetTextFieldText(panel->shoT, shortcut);
340 if (row >= 0) {
341 if (panel->shortcuts[row])
342 wfree(panel->shortcuts[row]);
343 panel->shortcuts[row] = shortcut;
345 WMRedisplayWidget(panel->actLs);
346 } else {
347 wfree(shortcut);
351 panel->capturing = 0;
352 WMSetButtonText(w, _("Capture"));
353 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
354 XUngrabKeyboard(dpy, CurrentTime);
357 static void clearShortcut(WMWidget * w, void *data)
359 _Panel *panel = (_Panel *) data;
360 int row = WMGetListSelectedItemRow(panel->actLs);
362 WMSetTextFieldText(panel->shoT, NULL);
364 if (row >= 0) {
365 if (panel->shortcuts[row])
366 wfree(panel->shortcuts[row]);
367 panel->shortcuts[row] = NULL;
368 WMRedisplayWidget(panel->actLs);
372 static void typedKeys(void *observerData, WMNotification * notification)
374 _Panel *panel = (_Panel *) observerData;
375 int row = WMGetListSelectedItemRow(panel->actLs);
377 if (row < 0)
378 return;
380 if (panel->shortcuts[row])
381 wfree(panel->shortcuts[row]);
382 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
383 if (strlen(panel->shortcuts[row]) == 0) {
384 wfree(panel->shortcuts[row]);
385 panel->shortcuts[row] = NULL;
387 WMRedisplayWidget(panel->actLs);
390 static void listClick(WMWidget * w, void *data)
392 _Panel *panel = (_Panel *) data;
393 int row = WMGetListSelectedItemRow(w);
395 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
398 static void showData(_Panel * panel)
400 char *str;
401 int i;
403 for (i = 0; i < panel->actionCount; i++) {
405 str = GetStringForKey(keyOptions[i].key);
406 if (panel->shortcuts[i])
407 wfree(panel->shortcuts[i]);
408 if (str)
409 panel->shortcuts[i] = wtrimspace(str);
410 else
411 panel->shortcuts[i] = NULL;
413 if (panel->shortcuts[i] &&
414 (strcasecmp(panel->shortcuts[i], "none") == 0 || strlen(panel->shortcuts[i]) == 0)) {
415 wfree(panel->shortcuts[i]);
416 panel->shortcuts[i] = NULL;
419 WMRedisplayWidget(panel->actLs);
422 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
424 int width, height, x, y;
425 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
426 WMScreen *scr = WMWidgetScreen(lPtr);
427 Display *dpy = WMScreenDisplay(scr);
428 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
430 width = rect->size.width;
431 height = rect->size.height;
432 x = rect->pos.x;
433 y = rect->pos.y;
435 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
437 if (panel->shortcuts[index]) {
438 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
439 WMSize size = WMGetPixmapSize(pix);
441 WMDrawPixmap(pix, d, x + (20 - size.width) / 2, (height - size.height) / 2 + y);
442 WMReleasePixmap(pix);
445 WMDrawString(scr, d, panel->black, panel->font, x + 20, y, text, strlen(text));
448 static void createPanel(Panel * p)
450 _Panel *panel = (_Panel *) p;
451 WMScreen *scr = WMWidgetScreen(panel->parent);
452 WMColor *color;
453 WMFont *boldFont;
454 int i;
456 panel->capturing = 0;
458 panel->white = WMWhiteColor(scr);
459 panel->black = WMBlackColor(scr);
460 panel->gray = WMGrayColor(scr);
461 panel->font = WMSystemFontOfSize(scr, 12);
463 panel->box = WMCreateBox(panel->parent);
464 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
466 boldFont = WMBoldSystemFontOfSize(scr, 12);
468 /* **************** Actions **************** */
469 panel->actL = WMCreateLabel(panel->box);
470 WMResizeWidget(panel->actL, 280, 20);
471 WMMoveWidget(panel->actL, 20, 10);
472 WMSetLabelFont(panel->actL, boldFont);
473 WMSetLabelText(panel->actL, _("Actions"));
474 WMSetLabelRelief(panel->actL, WRSunken);
475 WMSetLabelTextAlignment(panel->actL, WACenter);
476 color = WMDarkGrayColor(scr);
477 WMSetWidgetBackgroundColor(panel->actL, color);
478 WMReleaseColor(color);
479 WMSetLabelTextColor(panel->actL, panel->white);
481 panel->actLs = WMCreateList(panel->box);
482 WMResizeWidget(panel->actLs, 280, 190);
483 WMMoveWidget(panel->actLs, 20, 32);
484 WMSetListUserDrawProc(panel->actLs, paintItem);
485 WMHangData(panel->actLs, panel);
487 for (i = 0; i < sizeof(keyOptions)/sizeof(keyOptions[0]); i++) {
488 WMAddListItem(panel->actLs, _(keyOptions[i].title));
490 WMSetListAction(panel->actLs, listClick, panel);
492 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
493 panel->shortcuts = wmalloc(sizeof(char *) * panel->actionCount);
495 /***************** Shortcut ****************/
497 panel->shoF = WMCreateFrame(panel->box);
498 WMResizeWidget(panel->shoF, 190, 210);
499 WMMoveWidget(panel->shoF, 315, 10);
500 WMSetFrameTitle(panel->shoF, _("Shortcut"));
502 panel->shoT = WMCreateTextField(panel->shoF);
503 WMResizeWidget(panel->shoT, 160, 20);
504 WMMoveWidget(panel->shoT, 15, 65);
505 WMAddNotificationObserver(typedKeys, panel, WMTextDidChangeNotification, panel->shoT);
507 panel->cleB = WMCreateCommandButton(panel->shoF);
508 WMResizeWidget(panel->cleB, 75, 24);
509 WMMoveWidget(panel->cleB, 15, 95);
510 WMSetButtonText(panel->cleB, _("Clear"));
511 WMSetButtonAction(panel->cleB, clearShortcut, panel);
513 panel->defB = WMCreateCommandButton(panel->shoF);
514 WMResizeWidget(panel->defB, 75, 24);
515 WMMoveWidget(panel->defB, 100, 95);
516 WMSetButtonText(panel->defB, _("Capture"));
517 WMSetButtonAction(panel->defB, captureClick, panel);
519 panel->instructionsL = WMCreateLabel(panel->shoF);
520 WMResizeWidget(panel->instructionsL, 160, 55);
521 WMMoveWidget(panel->instructionsL, 15, 140);
522 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
523 WMSetLabelWraps(panel->instructionsL, True);
524 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
526 WMMapSubwidgets(panel->shoF);
528 WMReleaseFont(boldFont);
530 WMRealizeWidget(panel->box);
531 WMMapSubwidgets(panel->box);
533 showData(panel);
536 static void storeData(_Panel * panel)
538 int i;
539 char *str;
541 for (i = 0; i < panel->actionCount; i++) {
542 str = NULL;
543 if (panel->shortcuts[i]) {
544 str = wtrimspace(panel->shortcuts[i]);
545 if (strlen(str) == 0) {
546 wfree(str);
547 str = NULL;
550 if (str) {
551 SetStringForKey(str, keyOptions[i].key);
552 wfree(str);
553 } else {
554 SetStringForKey("None", keyOptions[i].key);
559 Panel *InitKeyboardShortcuts(WMScreen * scr, WMWidget * parent)
561 _Panel *panel;
563 panel = wmalloc(sizeof(_Panel));
565 panel->sectionName = _("Keyboard Shortcut Preferences");
567 panel->description = _("Change the keyboard shortcuts for actions such\n"
568 "as changing workspaces and opening menus.");
570 panel->parent = parent;
572 panel->callbacks.createWidgets = createPanel;
573 panel->callbacks.updateDomain = storeData;
575 AddSection(panel, ICON_FILE);
577 return panel;