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
[] = {
86 "NextWorkspaceLayerKey",
87 "PrevWorkspaceLayerKey",
100 "WindowShortcut3Key",
101 "WindowShortcut4Key",
102 "WindowShortcut5Key",
103 "WindowShortcut6Key",
104 "WindowShortcut7Key",
105 "WindowShortcut8Key",
106 "WindowShortcut9Key",
107 "WindowShortcut10Key",
116 #endif /* XKB_MODELOCK */
122 captureShortcut(Display
*dpy
, _Panel
*panel
)
125 KeySym ksym
, lksym
, uksym
;
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;
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+");
172 return wstrdup(buffer
);
177 captureClick(WMWidget
*w
, void *data
)
179 _Panel
*panel
= (_Panel
*)data
;
180 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->parent
));
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
);
191 int row
= WMGetListSelectedItemRow(panel
->actLs
);
193 WMSetTextFieldText(panel
->shoT
, shortcut
);
195 if (panel
->shortcuts
[row
])
196 wfree(panel
->shortcuts
[row
]);
197 panel
->shortcuts
[row
] = shortcut
;
199 WMRedisplayWidget(panel
->actLs
);
205 panel
->capturing
= 0;
206 WMSetButtonText(w
, _("Capture"));
207 WMSetLabelText(panel
->instructionsL
, _("Click Capture to interactively define the shortcut key."));
208 XUngrabKeyboard(dpy
, CurrentTime
);
214 clearShortcut(WMWidget
*w
, void *data
)
216 _Panel
*panel
= (_Panel
*)data
;
217 int row
= WMGetListSelectedItemRow(panel
->actLs
);
219 WMSetTextFieldText(panel
->shoT
, NULL
);
222 if (panel
->shortcuts
[row
])
223 wfree(panel
->shortcuts
[row
]);
224 panel
->shortcuts
[row
]=NULL
;
225 WMRedisplayWidget(panel
->actLs
);
232 typedKeys(void *observerData
, WMNotification
*notification
)
234 _Panel
*panel
= (_Panel
*)observerData
;
235 int row
= WMGetListSelectedItemRow(panel
->actLs
);
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
);
253 listClick(WMWidget
*w
, void *data
)
255 _Panel
*panel
= (_Panel
*)data
;
256 int row
= WMGetListSelectedItemRow(w
);
258 WMSetTextFieldText(panel
->shoT
, panel
->shortcuts
[row
]);
268 while (isspace(*p
)) p
++;
271 while (isspace(p
[i
]) && i
>0) {
281 showData(_Panel
*panel
)
286 for (i
=0; i
<panel
->actionCount
; i
++) {
288 str
= GetStringForKey(keyOptions
[i
]);
289 if (panel
->shortcuts
[i
])
290 wfree(panel
->shortcuts
[i
]);
292 panel
->shortcuts
[i
] = trimstr(str
);
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
);
308 paintItem(WMList
*lPtr
, int index
, Drawable d
, char *text
, int state
,
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
;
321 if (state
& WLDSSelected
)
322 XFillRectangle(dpy
, d
, WMColorGC(panel
->white
), x
, y
, width
,
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
,
341 createPanel(Panel
*p
)
343 _Panel
*panel
= (_Panel
*)p
;
344 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
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"));
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
);
477 storeData(_Panel
*panel
)
482 for (i
=0; i
<panel
->actionCount
; i
++) {
484 if (panel
->shortcuts
[i
]) {
485 str
= trimstr(panel
->shortcuts
[i
]);
486 if (strlen(str
)==0) {
492 SetStringForKey(str
, keyOptions
[i
]);
495 SetStringForKey("None", keyOptions
[i
]);
503 InitKeyboardShortcuts(WMScreen
*scr
, WMWidget
*parent
)
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
);