Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / Focus.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* Focus.c- input and colormap focus stuff
6830b057 dan2004-10-12 21:28:27 +00002 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * WPrefs - Window Maker Preferences Program
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1998-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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.
11 *
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.
16 *
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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
9d2e6ef9 scottc1998-09-29 22:36:29 +000023#include "WPrefs.h"
24
25typedef struct _Panel {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020026 WMBox *box;
9d2e6ef9 scottc1998-09-29 22:36:29 +000027
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020028 char *sectionName;
9d2e6ef9 scottc1998-09-29 22:36:29 +000029
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020030 char *description;
d3f29988 kojima1999-04-25 01:49:46 +000031
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020032 CallbackRec callbacks;
6830b057 dan2004-10-12 21:28:27 +000033
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034 WMWidget *parent;
6830b057 dan2004-10-12 21:28:27 +000035
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020036 WMFrame *kfocF;
37 WMButton *kfocB[2];
6830b057 dan2004-10-12 21:28:27 +000038
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020039 WMFrame *cfocF;
40 WMButton *autB;
41 WMButton *manB;
6830b057 dan2004-10-12 21:28:27 +000042
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020043 WMFrame *raisF;
44 WMButton *raisB[5];
45 WMTextField *raisT;
46 WMLabel *raisL;
6830b057 dan2004-10-12 21:28:27 +000047
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020048 WMFrame *optF;
49 WMButton *ignB;
50 WMButton *newB;
6830b057 dan2004-10-12 21:28:27 +000051
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020052 char raiseDelaySelected;
9d2e6ef9 scottc1998-09-29 22:36:29 +000053} _Panel;
54
9d2e6ef9 scottc1998-09-29 22:36:29 +000055#define ICON_FILE "windowfocus"
56
57#define DELAY_ICON "timer%i"
58#define DELAY_ICON_S "timer%is"
59
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020060static void showData(_Panel * panel)
9d2e6ef9 scottc1998-09-29 22:36:29 +000061{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020062 char *str;
63 int i;
64 char buffer[32];
65
66 str = GetStringForKey("FocusMode");
67 if (!str)
68 str = "manual";
69 if (strcasecmp(str, "manual") == 0 || strcasecmp(str, "clicktofocus") == 0)
70 WMSetButtonSelected(panel->kfocB[0], 1);
71 else if (strcasecmp(str, "auto") == 0 || strcasecmp(str, "semiauto") == 0
72 || strcasecmp(str, "sloppy") == 0)
73 WMSetButtonSelected(panel->kfocB[1], 1);
74 else {
75 wwarning(_("bad option value %s for option FocusMode. Using default Manual"), str);
76 WMSetButtonSelected(panel->kfocB[0], 1);
77 }
78
79 /**/ str = GetStringForKey("ColormapMode");
80 if (!str)
81 str = "auto";
82 if (strcasecmp(str, "manual") == 0 || strcasecmp(str, "clicktofocus") == 0) {
83 WMPerformButtonClick(panel->manB);
84 } else if (strcasecmp(str, "auto") == 0 || strcasecmp(str, "focusfollowsmouse") == 0) {
85 WMPerformButtonClick(panel->autB);
86 } else {
87 wwarning(_("bad option value %s for option ColormapMode. Using default Auto"), str);
88 WMPerformButtonClick(panel->manB);
89 }
90
91 /**/ i = GetIntegerForKey("RaiseDelay");
92 sprintf(buffer, "%i", i);
93 WMSetTextFieldText(panel->raisT, buffer);
94
95 switch (i) {
96 case 0:
97 WMPerformButtonClick(panel->raisB[0]);
98 break;
99 case 10:
100 WMPerformButtonClick(panel->raisB[1]);
101 break;
102 case 100:
103 WMPerformButtonClick(panel->raisB[2]);
104 break;
105 case 350:
106 WMPerformButtonClick(panel->raisB[3]);
107 break;
108 case 800:
109 WMPerformButtonClick(panel->raisB[4]);
110 break;
111 }
112
113 /**/ WMSetButtonSelected(panel->ignB, GetBoolForKey("IgnoreFocusClick"));
114
115 WMSetButtonSelected(panel->newB, GetBoolForKey("AutoFocus"));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000116}
117
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200118static void storeData(_Panel * panel)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000119{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200120 char *str;
121 int i;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000122
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200123 if (WMGetButtonSelected(panel->kfocB[1]))
124 str = "sloppy";
125 else
126 str = "manual";
6830b057 dan2004-10-12 21:28:27 +0000127
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200128 SetStringForKey(str, "FocusMode");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000129
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200130 if (WMGetButtonSelected(panel->manB)) {
131 SetStringForKey("manual", "ColormapMode");
132 } else {
133 SetStringForKey("auto", "ColormapMode");
134 }
6830b057 dan2004-10-12 21:28:27 +0000135
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200136 str = WMGetTextFieldText(panel->raisT);
137 if (sscanf(str, "%i", &i) != 1)
138 i = 0;
139 SetIntegerForKey(i, "RaiseDelay");
6830b057 dan2004-10-12 21:28:27 +0000140
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200141 SetBoolForKey(WMGetButtonSelected(panel->ignB), "IgnoreFocusClick");
142 SetBoolForKey(WMGetButtonSelected(panel->newB), "AutoFocus");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000143}
144
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200145static void pushDelayButton(WMWidget * w, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200147 _Panel *panel = (_Panel *) data;
148
149 panel->raiseDelaySelected = 1;
150 if (w == panel->raisB[0]) {
151 WMSetTextFieldText(panel->raisT, "OFF");
152 } else if (w == panel->raisB[1]) {
153 WMSetTextFieldText(panel->raisT, "10");
154 } else if (w == panel->raisB[2]) {
155 WMSetTextFieldText(panel->raisT, "100");
156 } else if (w == panel->raisB[3]) {
157 WMSetTextFieldText(panel->raisT, "350");
158 } else if (w == panel->raisB[4]) {
159 WMSetTextFieldText(panel->raisT, "800");
160 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000161}
162
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163static void raiseTextChanged(void *observerData, WMNotification * notification)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000164{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200165 _Panel *panel = (_Panel *) observerData;
166 int i;
167
168 if (panel->raiseDelaySelected) {
169 for (i = 0; i < 5; i++) {
170 WMSetButtonSelected(panel->raisB[i], False);
171 }
172 panel->raiseDelaySelected = 0;
173 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000174}
175
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200176static void createPanel(Panel * p)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000177{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200178 _Panel *panel = (_Panel *) p;
179 WMScreen *scr = WMWidgetScreen(panel->parent);
180 int i;
181 char *buf1, *buf2;
182 WMPixmap *icon;
183 WMColor *color;
184 WMFont *font;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000185
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200186 panel->box = WMCreateBox(panel->parent);
187 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
6830b057 dan2004-10-12 21:28:27 +0000188
9d2e6ef9 scottc1998-09-29 22:36:29 +0000189 /***************** Input Focus Mode *****************/
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200190 panel->kfocF = WMCreateFrame(panel->box);
191 WMResizeWidget(panel->kfocF, 240, 130);
192 WMMoveWidget(panel->kfocF, 15, 15);
193 WMSetFrameTitle(panel->kfocF, _("Input Focus Mode"));
194
195 {
196 WMBox *box = WMCreateBox(panel->kfocF);
197 WMSetViewExpandsToParent(WMWidgetView(box), 10, 15, 10, 10);
198 WMSetBoxHorizontal(box, False);
199
200 panel->kfocB[0] = WMCreateRadioButton(box);
201 WMSetButtonText(panel->kfocB[0], _("Manual: Click on the window to set "
202 "keyboard input focus."));
203 WMAddBoxSubview(box, WMWidgetView(panel->kfocB[0]), True, True, 20, 0, 0);
204
205 panel->kfocB[1] = WMCreateRadioButton(box);
206 WMGroupButtons(panel->kfocB[0], panel->kfocB[1]);
207 WMSetButtonText(panel->kfocB[1], _("Auto: Set keyboard input focus to "
208 "the window under the mouse pointer."));
209 WMAddBoxSubview(box, WMWidgetView(panel->kfocB[1]), True, True, 20, 0, 0);
210
211 WMMapSubwidgets(box);
212 WMMapWidget(box);
213 }
6830b057 dan2004-10-12 21:28:27 +0000214
9d2e6ef9 scottc1998-09-29 22:36:29 +0000215 /***************** Colormap Installation Mode ****************/
6830b057 dan2004-10-12 21:28:27 +0000216
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200217 panel->cfocF = WMCreateFrame(panel->box);
218 WMResizeWidget(panel->cfocF, 240, 70);
219 WMMoveWidget(panel->cfocF, 15, 150);
220 WMSetFrameTitle(panel->cfocF, _("Install colormap in the window..."));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000221
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200222 panel->manB = WMCreateRadioButton(panel->cfocF);
223 WMResizeWidget(panel->manB, 225, 20);
224 WMMoveWidget(panel->manB, 10, 18);
225 WMSetButtonText(panel->manB, _("...that has the input focus."));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000226
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200227 panel->autB = WMCreateRadioButton(panel->cfocF);
228 WMResizeWidget(panel->autB, 225, 20);
229 WMMoveWidget(panel->autB, 10, 43);
230 WMSetButtonText(panel->autB, _("...that's under the mouse pointer."));
231 WMGroupButtons(panel->manB, panel->autB);
6830b057 dan2004-10-12 21:28:27 +0000232
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200233 WMMapSubwidgets(panel->cfocF);
6830b057 dan2004-10-12 21:28:27 +0000234
9d2e6ef9 scottc1998-09-29 22:36:29 +0000235 /***************** Automatic window raise delay *****************/
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200236 panel->raisF = WMCreateFrame(panel->box);
237 WMResizeWidget(panel->raisF, 245, 70);
238 WMMoveWidget(panel->raisF, 265, 15);
239 WMSetFrameTitle(panel->raisF, _("Automatic Window Raise Delay"));
240
241 buf1 = wmalloc(strlen(DELAY_ICON) + 1);
242 buf2 = wmalloc(strlen(DELAY_ICON_S) + 1);
243
244 for (i = 0; i < 5; i++) {
245 char *path;
246
247 panel->raisB[i] = WMCreateCustomButton(panel->raisF, WBBStateChangeMask);
248 WMResizeWidget(panel->raisB[i], 25, 25);
249 WMMoveWidget(panel->raisB[i], 10 + (30 * i), 25);
250 WMSetButtonBordered(panel->raisB[i], False);
251 WMSetButtonImagePosition(panel->raisB[i], WIPImageOnly);
252 WMSetButtonAction(panel->raisB[i], pushDelayButton, panel);
253 if (i > 0)
254 WMGroupButtons(panel->raisB[0], panel->raisB[i]);
255 sprintf(buf1, DELAY_ICON, i);
256 sprintf(buf2, DELAY_ICON_S, i);
257 path = LocateImage(buf1);
258 if (path) {
259 icon = WMCreatePixmapFromFile(scr, path);
260 if (icon) {
261 WMSetButtonImage(panel->raisB[i], icon);
262 WMReleasePixmap(icon);
263 } else {
264 wwarning(_("could not load icon file %s"), path);
265 }
266 wfree(path);
267 }
268 path = LocateImage(buf2);
269 if (path) {
270 icon = WMCreatePixmapFromFile(scr, path);
271 if (icon) {
272 WMSetButtonAltImage(panel->raisB[i], icon);
273 WMReleasePixmap(icon);
274 } else {
275 wwarning(_("could not load icon file %s"), path);
276 }
277 wfree(path);
278 }
279 }
280 wfree(buf1);
281 wfree(buf2);
282
283 panel->raisT = WMCreateTextField(panel->raisF);
284 WMResizeWidget(panel->raisT, 36, 20);
285 WMMoveWidget(panel->raisT, 165, 30);
286 WMAddNotificationObserver(raiseTextChanged, panel, WMTextDidChangeNotification, panel->raisT);
287
288 color = WMDarkGrayColor(scr);
289 font = WMSystemFontOfSize(scr, 10);
290
291 panel->raisL = WMCreateLabel(panel->raisF);
292 WMResizeWidget(panel->raisL, 36, 16);
293 WMMoveWidget(panel->raisL, 205, 35);
294 WMSetLabelText(panel->raisL, _("msec"));
295 WMSetLabelTextColor(panel->raisL, color);
296 WMSetLabelFont(panel->raisL, font);
297
298 WMReleaseColor(color);
299 WMReleaseFont(font);
300
301 WMMapSubwidgets(panel->raisF);
6830b057 dan2004-10-12 21:28:27 +0000302
9d2e6ef9 scottc1998-09-29 22:36:29 +0000303 /***************** Options ****************/
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200304 panel->optF = WMCreateFrame(panel->box);
305 WMResizeWidget(panel->optF, 245, 125);
306 WMMoveWidget(panel->optF, 265, 95);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000307
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200308 panel->ignB = WMCreateSwitchButton(panel->optF);
309 WMResizeWidget(panel->ignB, 225, 50);
310 WMMoveWidget(panel->ignB, 10, 10);
311 WMSetButtonText(panel->ignB, _("Do not let applications receive " "the click used to focus windows."));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000312
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200313 panel->newB = WMCreateSwitchButton(panel->optF);
314 WMResizeWidget(panel->newB, 225, 35);
315 WMMoveWidget(panel->newB, 10, 70);
316 WMSetButtonText(panel->newB, _("Automatically focus new windows."));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000317
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318 WMMapSubwidgets(panel->optF);
6830b057 dan2004-10-12 21:28:27 +0000319
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 WMRealizeWidget(panel->box);
321 WMMapSubwidgets(panel->box);
6830b057 dan2004-10-12 21:28:27 +0000322
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200323 showData(panel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000324}
325
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200326Panel *InitFocus(WMScreen * scr, WMWindow * win)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000327{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200328 _Panel *panel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000329
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200330 panel = wmalloc(sizeof(_Panel));
331 memset(panel, 0, sizeof(_Panel));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000332
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200333 panel->sectionName = _("Window Focus Preferences");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000334
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200335 panel->description = _("Keyboard focus switching policy, colormap switching\n"
336 "policy for 8bpp displays and other related options.");
d3f29988 kojima1999-04-25 01:49:46 +0000337
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200338 panel->parent = win;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000339
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200340 panel->callbacks.createWidgets = createPanel;
341 panel->callbacks.updateDomain = storeData;
6830b057 dan2004-10-12 21:28:27 +0000342
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200343 AddSection(panel, ICON_FILE);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000344
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200345 return panel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000346}