Coverity: fix wmspec dereference after null check
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blob107f0e82565d3409d37eb5b1f9b8ed66cc2c0d42
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 { "KeepOnTopKey", N_("Toggle window on top status") },
93 { "KeepAtBottomKey",N_("Toggle window at bottom status") },
94 { "OmnipresentKey", N_("Toggle window omnipresent status") },
95 { "RaiseKey", N_("Raise active window") },
96 { "LowerKey", N_("Lower active window") },
97 { "RaiseLowerKey", N_("Raise/Lower window under mouse pointer") },
98 { "ShadeKey", N_("Shade active window") },
99 { "MoveResizeKey", N_("Move/Resize active window") },
100 { "SelectKey", N_("Select active window") },
101 { "FocusNextKey", N_("Focus next window") },
102 { "FocusPrevKey", N_("Focus previous window") },
103 { "GroupNextKey", N_("Focus next group window") },
104 { "GroupPrevKey", N_("Focus previous group window") },
106 /* Workspace Related */
107 { "WorkspaceMapKey", N_("Open workspace pager") },
108 { "NextWorkspaceKey", N_("Switch to next workspace") },
109 { "PrevWorkspaceKey", N_("Switch to previous workspace") },
110 { "LastWorkspaceKey", N_("Switch to last used workspace") },
111 { "NextWorkspaceLayerKey", N_("Switch to next ten workspaces") },
112 { "PrevWorkspaceLayerKey", N_("Switch to previous ten workspaces") },
113 { "Workspace1Key", N_("Switch to workspace 1") },
114 { "Workspace2Key", N_("Switch to workspace 2") },
115 { "Workspace3Key", N_("Switch to workspace 3") },
116 { "Workspace4Key", N_("Switch to workspace 4") },
117 { "Workspace5Key", N_("Switch to workspace 5") },
118 { "Workspace6Key", N_("Switch to workspace 6") },
119 { "Workspace7Key", N_("Switch to workspace 7") },
120 { "Workspace8Key", N_("Switch to workspace 8") },
121 { "Workspace9Key", N_("Switch to workspace 9") },
122 { "Workspace10Key", N_("Switch to workspace 10") },
123 { "MoveToNextWorkspaceKey", N_("Move window to next workspace") },
124 { "MoveToPrevWorkspaceKey", N_("Move window to previous workspace") },
125 { "MoveToLastWorkspaceKey", N_("Move window to last used workspace") },
126 { "MoveToNextWorkspaceLayerKey", N_("Move window to next ten workspaces") },
127 { "MoveToPrevWorkspaceLayerKey", N_("Move window to previous ten workspaces") },
128 { "MoveToWorkspace1Key", N_("Move window to workspace 1") },
129 { "MoveToWorkspace2Key", N_("Move window to workspace 2") },
130 { "MoveToWorkspace3Key", N_("Move window to workspace 3") },
131 { "MoveToWorkspace4Key", N_("Move window to workspace 4") },
132 { "MoveToWorkspace5Key", N_("Move window to workspace 5") },
133 { "MoveToWorkspace6Key", N_("Move window to workspace 6") },
134 { "MoveToWorkspace7Key", N_("Move window to workspace 7") },
135 { "MoveToWorkspace8Key", N_("Move window to workspace 8") },
136 { "MoveToWorkspace9Key", N_("Move window to workspace 9") },
137 { "MoveToWorkspace10Key", N_("Move window to workspace 10") },
139 /* Window Selection */
140 { "WindowShortcut1Key", N_("Shortcut for window 1") },
141 { "WindowShortcut2Key", N_("Shortcut for window 2") },
142 { "WindowShortcut3Key", N_("Shortcut for window 3") },
143 { "WindowShortcut4Key", N_("Shortcut for window 4") },
144 { "WindowShortcut5Key", N_("Shortcut for window 5") },
145 { "WindowShortcut6Key", N_("Shortcut for window 6") },
146 { "WindowShortcut7Key", N_("Shortcut for window 7") },
147 { "WindowShortcut8Key", N_("Shortcut for window 8") },
148 { "WindowShortcut9Key", N_("Shortcut for window 9") },
149 { "WindowShortcut10Key", N_("Shortcut for window 10") },
151 /* Head Selection */
152 { "MoveTo12to6Head", N_("Move to right/bottom/left/top head") },
153 { "MoveTo6to12Head", N_("Move to left/top/right/bottom head") },
155 /* Misc. */
156 { "WindowRelaunchKey", N_("Launch new instance of application") },
157 { "ScreenSwitchKey", N_("Switch to Next Screen/Monitor") },
158 { "RunKey", N_("Run application") },
159 { "ExitKey", N_("Exit Window Maker") },
160 { "DockRaiseLowerKey", N_("Raise/Lower Dock") },
161 { "ClipRaiseLowerKey", N_("Raise/Lower Clip") }
162 #ifdef XKB_MODELOCK
163 ,{ "ToggleKbdModeKey", N_("Toggle keyboard language") }
164 #endif /* XKB_MODELOCK */
167 #ifndef HAVE_XCONVERTCASE
168 /* from Xlib */
170 static void XConvertCase(register KeySym sym, KeySym * lower, KeySym * upper)
172 *lower = sym;
173 *upper = sym;
174 switch (sym >> 8) {
175 case 0: /* Latin 1 */
176 if ((sym >= XK_A) && (sym <= XK_Z))
177 *lower += (XK_a - XK_A);
178 else if ((sym >= XK_a) && (sym <= XK_z))
179 *upper -= (XK_a - XK_A);
180 else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
181 *lower += (XK_agrave - XK_Agrave);
182 else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
183 *upper -= (XK_agrave - XK_Agrave);
184 else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
185 *lower += (XK_oslash - XK_Ooblique);
186 else if ((sym >= XK_oslash) && (sym <= XK_thorn))
187 *upper -= (XK_oslash - XK_Ooblique);
188 break;
189 case 1: /* Latin 2 */
190 /* Assume the KeySym is a legal value (ignore discontinuities) */
191 if (sym == XK_Aogonek)
192 *lower = XK_aogonek;
193 else if (sym >= XK_Lstroke && sym <= XK_Sacute)
194 *lower += (XK_lstroke - XK_Lstroke);
195 else if (sym >= XK_Scaron && sym <= XK_Zacute)
196 *lower += (XK_scaron - XK_Scaron);
197 else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
198 *lower += (XK_zcaron - XK_Zcaron);
199 else if (sym == XK_aogonek)
200 *upper = XK_Aogonek;
201 else if (sym >= XK_lstroke && sym <= XK_sacute)
202 *upper -= (XK_lstroke - XK_Lstroke);
203 else if (sym >= XK_scaron && sym <= XK_zacute)
204 *upper -= (XK_scaron - XK_Scaron);
205 else if (sym >= XK_zcaron && sym <= XK_zabovedot)
206 *upper -= (XK_zcaron - XK_Zcaron);
207 else if (sym >= XK_Racute && sym <= XK_Tcedilla)
208 *lower += (XK_racute - XK_Racute);
209 else if (sym >= XK_racute && sym <= XK_tcedilla)
210 *upper -= (XK_racute - XK_Racute);
211 break;
212 case 2: /* Latin 3 */
213 /* Assume the KeySym is a legal value (ignore discontinuities) */
214 if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
215 *lower += (XK_hstroke - XK_Hstroke);
216 else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
217 *lower += (XK_gbreve - XK_Gbreve);
218 else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
219 *upper -= (XK_hstroke - XK_Hstroke);
220 else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
221 *upper -= (XK_gbreve - XK_Gbreve);
222 else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
223 *lower += (XK_cabovedot - XK_Cabovedot);
224 else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
225 *upper -= (XK_cabovedot - XK_Cabovedot);
226 break;
227 case 3: /* Latin 4 */
228 /* Assume the KeySym is a legal value (ignore discontinuities) */
229 if (sym >= XK_Rcedilla && sym <= XK_Tslash)
230 *lower += (XK_rcedilla - XK_Rcedilla);
231 else if (sym >= XK_rcedilla && sym <= XK_tslash)
232 *upper -= (XK_rcedilla - XK_Rcedilla);
233 else if (sym == XK_ENG)
234 *lower = XK_eng;
235 else if (sym == XK_eng)
236 *upper = XK_ENG;
237 else if (sym >= XK_Amacron && sym <= XK_Umacron)
238 *lower += (XK_amacron - XK_Amacron);
239 else if (sym >= XK_amacron && sym <= XK_umacron)
240 *upper -= (XK_amacron - XK_Amacron);
241 break;
242 case 6: /* Cyrillic */
243 /* Assume the KeySym is a legal value (ignore discontinuities) */
244 if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
245 *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
246 else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
247 *upper += (XK_Serbian_DJE - XK_Serbian_dje);
248 else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
249 *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
250 else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
251 *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
252 break;
253 case 7: /* Greek */
254 /* Assume the KeySym is a legal value (ignore discontinuities) */
255 if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
256 *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
257 else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
258 sym != XK_Greek_iotaaccentdieresis && sym != XK_Greek_upsilonaccentdieresis)
259 *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
260 else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
261 *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
262 else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega && sym != XK_Greek_finalsmallsigma)
263 *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
264 break;
265 case 0x14: /* Armenian */
266 if (sym >= XK_Armenian_AYB && sym <= XK_Armenian_fe) {
267 *lower = sym | 1;
268 *upper = sym & ~1;
270 break;
273 #endif
275 static int NumLockMask(Display *dpy)
277 int i, mask;
278 XModifierKeymap *map;
279 static int mask_table[8] = {
280 ShiftMask, LockMask, ControlMask, Mod1Mask,
281 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
283 KeyCode numlock_keycode = XKeysymToKeycode(dpy, XK_Num_Lock);
285 if (numlock_keycode == NoSymbol)
286 return 0;
288 map = XGetModifierMapping(dpy);
289 if (!map)
290 return 0;
292 mask = 0;
293 for (i = 0; i < 8 * map->max_keypermod; i++) {
294 if (map->modifiermap[i] == numlock_keycode && mask == 0) {
295 mask = mask_table[i/map->max_keypermod];
296 break;
300 if (map)
301 XFreeModifiermap(map);
303 return mask;
306 char *capture_shortcut(Display *dpy, Bool *capturing, Bool convert_case)
308 XEvent ev;
309 KeySym ksym, lksym, uksym;
310 char buffer[64];
311 char *key = NULL;
312 unsigned int numlock_mask;
314 while (*capturing) {
315 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
316 WMNextEvent(dpy, &ev);
317 if (ev.type == KeyPress && ev.xkey.keycode != 0) {
318 numlock_mask = NumLockMask(dpy);
320 /* conditional mask check to get numeric keypad keys */
321 ksym = W_KeycodeToKeysym(dpy, ev.xkey.keycode, ev.xkey.state & numlock_mask?1:0);
323 if (!IsModifierKey(ksym)) {
324 if (convert_case) {
325 XConvertCase(ksym, &lksym, &uksym);
326 key = XKeysymToString(uksym);
327 } else {
328 key = XKeysymToString(ksym);
331 *capturing = 0;
332 break;
335 WMHandleEvent(&ev);
338 if (!key)
339 return NULL;
341 buffer[0] = 0;
343 if (ev.xkey.state & ControlMask)
344 strcat(buffer, "Control+");
346 if (ev.xkey.state & ShiftMask)
347 strcat(buffer, "Shift+");
349 if ((numlock_mask != Mod1Mask) && (ev.xkey.state & Mod1Mask))
350 strcat(buffer, "Mod1+");
352 if ((numlock_mask != Mod2Mask) && (ev.xkey.state & Mod2Mask))
353 strcat(buffer, "Mod2+");
355 if ((numlock_mask != Mod3Mask) && (ev.xkey.state & Mod3Mask))
356 strcat(buffer, "Mod3+");
358 if ((numlock_mask != Mod4Mask) && (ev.xkey.state & Mod4Mask))
359 strcat(buffer, "Mod4+");
361 if ((numlock_mask != Mod5Mask) && (ev.xkey.state & Mod5Mask))
362 strcat(buffer, "Mod5+");
364 wstrlcat(buffer, key, sizeof(buffer));
366 return wstrdup(buffer);
370 * check if the keystr entered is already set to another action
371 * if found it returns the position in the keyOptions
373 static int isKeySet(_Panel *panel, char *keystr)
375 int i;
376 char *str;
378 for (i = 0; i < panel->actionCount; i++) {
379 str = NULL;
380 if (panel->shortcuts[i]) {
381 str = wtrimspace(panel->shortcuts[i]);
382 if (strlen(str) == 0) {
383 wfree(str);
384 str = NULL;
387 if (str) {
388 if (strcmp(keystr, str) == 0) {
389 wfree(str);
390 return i;
392 wfree(str);
396 return -1;
399 static void captureClick(WMWidget * w, void *data)
401 _Panel *panel = (_Panel *) data;
402 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
403 char *shortcut;
405 if (!panel->capturing) {
406 panel->capturing = 1;
407 WMSetButtonText(w, _("Cancel"));
408 WMSetLabelText(panel->instructionsL,
409 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
410 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
411 shortcut = capture_shortcut(dpy, &panel->capturing, 1);
412 if (shortcut) {
413 int key_idx = -1;
414 int row = WMGetListSelectedItemRow(panel->actLs);
416 key_idx = isKeySet(panel, shortcut);
417 if (key_idx >= 0 && (key_idx != row)) {
418 char *msg;
420 msg = wstrconcat(_("Key shortcut already in use by the "), _(keyOptions[key_idx].title));
421 WMRunAlertPanel(WMWidgetScreen(w), GetWindow(),
422 _("Error"),
423 msg,
424 _("OK"), NULL, NULL);
425 wfree(msg);
426 wfree(shortcut);
427 } else {
428 WMSetTextFieldText(panel->shoT, shortcut);
429 if (row >= 0) {
430 if (panel->shortcuts[row])
431 wfree(panel->shortcuts[row]);
432 panel->shortcuts[row] = shortcut;
434 WMRedisplayWidget(panel->actLs);
435 } else {
436 wfree(shortcut);
441 panel->capturing = 0;
442 WMSetButtonText(w, _("Capture"));
443 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
444 XUngrabKeyboard(dpy, CurrentTime);
447 static void clearShortcut(WMWidget * w, void *data)
449 _Panel *panel = (_Panel *) data;
450 int row = WMGetListSelectedItemRow(panel->actLs);
452 /* Parameter not used, but tell the compiler that it is ok */
453 (void) w;
455 WMSetTextFieldText(panel->shoT, NULL);
457 if (row >= 0) {
458 if (panel->shortcuts[row])
459 wfree(panel->shortcuts[row]);
460 panel->shortcuts[row] = NULL;
461 WMRedisplayWidget(panel->actLs);
465 static void typedKeys(void *observerData, WMNotification * notification)
467 _Panel *panel = (_Panel *) observerData;
468 int row = WMGetListSelectedItemRow(panel->actLs);
470 /* Parameter not used, but tell the compiler that it is ok */
471 (void) notification;
473 if (row < 0)
474 return;
476 if (panel->shortcuts[row])
477 wfree(panel->shortcuts[row]);
478 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
479 if (strlen(panel->shortcuts[row]) == 0) {
480 wfree(panel->shortcuts[row]);
481 panel->shortcuts[row] = NULL;
483 WMRedisplayWidget(panel->actLs);
486 static void listClick(WMWidget * w, void *data)
488 _Panel *panel = (_Panel *) data;
489 int row = WMGetListSelectedItemRow(w);
491 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
494 static void showData(_Panel * panel)
496 char *str;
497 int i;
499 for (i = 0; i < panel->actionCount; i++) {
501 str = GetStringForKey(keyOptions[i].key);
502 if (panel->shortcuts[i])
503 wfree(panel->shortcuts[i]);
504 if (str)
505 panel->shortcuts[i] = wtrimspace(str);
506 else
507 panel->shortcuts[i] = NULL;
509 if (panel->shortcuts[i] &&
510 (strcasecmp(panel->shortcuts[i], "none") == 0 || strlen(panel->shortcuts[i]) == 0)) {
511 wfree(panel->shortcuts[i]);
512 panel->shortcuts[i] = NULL;
515 WMRedisplayWidget(panel->actLs);
518 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
520 int width, height, x, y;
521 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
522 WMScreen *scr = WMWidgetScreen(lPtr);
523 Display *dpy = WMScreenDisplay(scr);
524 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
526 width = rect->size.width;
527 height = rect->size.height;
528 x = rect->pos.x;
529 y = rect->pos.y;
531 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
533 if (panel->shortcuts[index]) {
534 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
535 WMSize size = WMGetPixmapSize(pix);
537 WMDrawPixmap(pix, d, x + (20 - size.width) / 2, (height - size.height) / 2 + y);
538 WMReleasePixmap(pix);
541 WMDrawString(scr, d, panel->black, panel->font, x + 20, y, text, strlen(text));
544 static void createPanel(Panel * p)
546 _Panel *panel = (_Panel *) p;
547 WMScreen *scr = WMWidgetScreen(panel->parent);
548 WMColor *color;
549 WMFont *boldFont;
550 int i;
552 panel->capturing = 0;
554 panel->white = WMWhiteColor(scr);
555 panel->black = WMBlackColor(scr);
556 panel->gray = WMGrayColor(scr);
557 panel->font = WMSystemFontOfSize(scr, 12);
559 panel->box = WMCreateBox(panel->parent);
560 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
562 boldFont = WMBoldSystemFontOfSize(scr, 12);
564 /* **************** Actions **************** */
565 panel->actL = WMCreateLabel(panel->box);
566 WMResizeWidget(panel->actL, 314, 20);
567 WMMoveWidget(panel->actL, 9, 9);
568 WMSetLabelFont(panel->actL, boldFont);
569 WMSetLabelText(panel->actL, _("Actions"));
570 WMSetLabelRelief(panel->actL, WRSunken);
571 WMSetLabelTextAlignment(panel->actL, WACenter);
572 color = WMDarkGrayColor(scr);
573 WMSetWidgetBackgroundColor(panel->actL, color);
574 WMReleaseColor(color);
575 WMSetLabelTextColor(panel->actL, panel->white);
577 panel->actLs = WMCreateList(panel->box);
578 WMResizeWidget(panel->actLs, 314, 191);
579 WMMoveWidget(panel->actLs, 9, 31);
580 WMSetListUserDrawProc(panel->actLs, paintItem);
581 WMHangData(panel->actLs, panel);
583 for (i = 0; i < wlengthof(keyOptions); i++) {
584 WMAddListItem(panel->actLs, _(keyOptions[i].title));
586 WMSetListAction(panel->actLs, listClick, panel);
588 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
589 panel->shortcuts = wmalloc(sizeof(char *) * panel->actionCount);
591 /***************** Shortcut ****************/
593 panel->shoF = WMCreateFrame(panel->box);
594 WMResizeWidget(panel->shoF, 178, 214);
595 WMMoveWidget(panel->shoF, 333, 8);
596 WMSetFrameTitle(panel->shoF, _("Shortcut"));
598 panel->shoT = WMCreateTextField(panel->shoF);
599 WMResizeWidget(panel->shoT, 160, 20);
600 WMMoveWidget(panel->shoT, 9, 65);
601 WMAddNotificationObserver(typedKeys, panel, WMTextDidChangeNotification, panel->shoT);
603 panel->cleB = WMCreateCommandButton(panel->shoF);
604 WMResizeWidget(panel->cleB, 75, 24);
605 WMMoveWidget(panel->cleB, 9, 95);
606 WMSetButtonText(panel->cleB, _("Clear"));
607 WMSetButtonAction(panel->cleB, clearShortcut, panel);
609 panel->defB = WMCreateCommandButton(panel->shoF);
610 WMResizeWidget(panel->defB, 75, 24);
611 WMMoveWidget(panel->defB, 94, 95);
612 WMSetButtonText(panel->defB, _("Capture"));
613 WMSetButtonAction(panel->defB, captureClick, panel);
615 panel->instructionsL = WMCreateLabel(panel->shoF);
616 WMResizeWidget(panel->instructionsL, 160, 55);
617 WMMoveWidget(panel->instructionsL, 9, 140);
618 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
619 WMSetLabelWraps(panel->instructionsL, True);
620 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
622 WMMapSubwidgets(panel->shoF);
624 WMReleaseFont(boldFont);
626 WMRealizeWidget(panel->box);
627 WMMapSubwidgets(panel->box);
629 showData(panel);
632 static void storeData(_Panel * panel)
634 int i;
635 char *str;
637 for (i = 0; i < panel->actionCount; i++) {
638 str = NULL;
639 if (panel->shortcuts[i]) {
640 str = wtrimspace(panel->shortcuts[i]);
641 if (strlen(str) == 0) {
642 wfree(str);
643 str = NULL;
646 if (str) {
647 SetStringForKey(str, keyOptions[i].key);
648 wfree(str);
649 } else {
650 SetStringForKey("None", keyOptions[i].key);
655 Panel *InitKeyboardShortcuts(WMWidget *parent)
657 _Panel *panel;
659 panel = wmalloc(sizeof(_Panel));
661 panel->sectionName = _("Keyboard Shortcut Preferences");
663 panel->description = _("Change the keyboard shortcuts for actions such\n"
664 "as changing workspaces and opening menus.");
666 panel->parent = parent;
668 panel->callbacks.createWidgets = createPanel;
669 panel->callbacks.updateDomain = storeData;
671 AddSection(panel, ICON_FILE);
673 return panel;