1 /* KeyboardShortcuts.c- keyboard shortcut bindings
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 1998 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 #include <X11/keysym.h>
30 typedef struct _Panel
{
37 CallbackRec callbacks
;
49 WMLabel
*instructionsL
;
63 #define ICON_FILE "keyshortcuts"
66 /* must be in the same order as the corresponding items in actions list */
67 static char *keyOptions
[] = {
87 "NextWorkspaceLayerKey",
88 "PrevWorkspaceLayerKey",
100 "WindowShortcut2Key",
101 "WindowShortcut3Key",
102 "WindowShortcut4Key",
103 "WindowShortcut5Key",
104 "WindowShortcut6Key",
105 "WindowShortcut7Key",
106 "WindowShortcut8Key",
107 "WindowShortcut9Key",
108 "WindowShortcut10Key",
117 #endif /* XKB_MODELOCK */
123 captureShortcut(Display
*dpy
, _Panel
*panel
)
126 KeySym ksym
, lksym
, uksym
;
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;
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+");
173 return wstrdup(buffer
);
178 captureClick(WMWidget
*w
, void *data
)
180 _Panel
*panel
= (_Panel
*)data
;
181 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->parent
));
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
);
192 int row
= WMGetListSelectedItemRow(panel
->actLs
);
194 WMSetTextFieldText(panel
->shoT
, shortcut
);
196 if (panel
->shortcuts
[row
])
197 wfree(panel
->shortcuts
[row
]);
198 panel
->shortcuts
[row
] = shortcut
;
200 WMRedisplayWidget(panel
->actLs
);
206 panel
->capturing
= 0;
207 WMSetButtonText(w
, _("Capture"));
208 WMSetLabelText(panel
->instructionsL
, _("Click Capture to interactively define the shortcut key."));
209 XUngrabKeyboard(dpy
, CurrentTime
);
215 clearShortcut(WMWidget
*w
, void *data
)
217 _Panel
*panel
= (_Panel
*)data
;
218 int row
= WMGetListSelectedItemRow(panel
->actLs
);
220 WMSetTextFieldText(panel
->shoT
, NULL
);
223 if (panel
->shortcuts
[row
])
224 wfree(panel
->shortcuts
[row
]);
225 panel
->shortcuts
[row
]=NULL
;
226 WMRedisplayWidget(panel
->actLs
);
233 typedKeys(void *observerData
, WMNotification
*notification
)
235 _Panel
*panel
= (_Panel
*)observerData
;
236 int row
= WMGetListSelectedItemRow(panel
->actLs
);
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
);
254 listClick(WMWidget
*w
, void *data
)
256 _Panel
*panel
= (_Panel
*)data
;
257 int row
= WMGetListSelectedItemRow(w
);
259 WMSetTextFieldText(panel
->shoT
, panel
->shortcuts
[row
]);
269 while (isspace(*p
)) p
++;
272 while (isspace(p
[i
]) && i
>0) {
282 showData(_Panel
*panel
)
287 for (i
=0; i
<panel
->actionCount
; i
++) {
289 str
= GetStringForKey(keyOptions
[i
]);
290 if (panel
->shortcuts
[i
])
291 wfree(panel
->shortcuts
[i
]);
293 panel
->shortcuts
[i
] = trimstr(str
);
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
);
309 paintItem(WMList
*lPtr
, int index
, Drawable d
, char *text
, int state
,
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
;
322 if (state
& WLDSSelected
)
323 XFillRectangle(dpy
, d
, WMColorGC(panel
->white
), x
, y
, width
,
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
,
342 createPanel(Panel
*p
)
344 _Panel
*panel
= (_Panel
*)p
;
345 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
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"));
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
);
479 storeData(_Panel
*panel
)
484 for (i
=0; i
<panel
->actionCount
; i
++) {
486 if (panel
->shortcuts
[i
]) {
487 str
= trimstr(panel
->shortcuts
[i
]);
488 if (strlen(str
)==0) {
494 SetStringForKey(str
, keyOptions
[i
]);
497 SetStringForKey("None", keyOptions
[i
]);
505 InitKeyboardShortcuts(WMScreen
*scr
, WMWidget
*parent
)
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
);