Added reset of pointer after memory free to avoid double-free crash
[wmaker-crm.git] / WPrefs.app / WindowHandling.c
blob335690c541881b94aedab98aa4ceede2dbc8d233
1 /* WindowHandling.c- options for handling windows
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 "WPrefs.h"
24 typedef struct _Panel {
25 WMBox *box;
27 char *sectionName;
29 char *description;
31 CallbackRec callbacks;
33 WMWidget *parent;
35 WMFrame *placF;
36 WMPopUpButton *placP;
37 WMLabel *porigL;
38 WMLabel *porigvL;
39 WMFrame *porigF;
40 WMLabel *porigW;
42 WMSlider *vsli;
43 WMSlider *hsli;
45 WMFrame *resF;
46 WMSlider *resS;
47 WMLabel *resL;
48 WMButton *resaB;
49 WMButton *resrB;
51 WMFrame *maxiF;
52 WMButton *miconB;
53 WMButton *mdockB;
55 WMLabel *resizeL;
56 WMLabel *resizeTextL;
57 WMSlider *resizeS;
59 WMFrame *opaqF;
60 WMButton *opaqB;
62 WMButton *opaqresizeB;
64 WMFrame *tranF;
65 WMButton *tranB;
66 } _Panel;
68 #define ICON_FILE "whandling"
70 #define OPAQUE_MOVE_PIXMAP "opaque"
72 #define NON_OPAQUE_MOVE_PIXMAP "nonopaque"
74 #define OPAQUE_RESIZE_PIXMAP "opaqueresize"
76 #define NON_OPAQUE_RESIZE_PIXMAP "noopaqueresize"
78 #define THUMB_SIZE 16
80 static char *placements[] = {
81 "auto",
82 "random",
83 "manual",
84 "cascade",
85 "smart",
86 "center"
89 static void sliderCallback(WMWidget * w, void *data)
91 _Panel *panel = (_Panel *) data;
92 int x, y, rx, ry;
93 char buffer[64];
94 int swidth = WMGetSliderMaxValue(panel->hsli);
95 int sheight = WMGetSliderMaxValue(panel->vsli);
97 x = WMGetSliderValue(panel->hsli);
98 y = WMGetSliderValue(panel->vsli);
100 rx = x * (WMWidgetWidth(panel->porigF) - 3) / swidth + 2;
101 ry = y * (WMWidgetHeight(panel->porigF) - 3) / sheight + 2;
102 WMMoveWidget(panel->porigW, rx, ry);
104 sprintf(buffer, "(%i,%i)", x, y);
105 WMSetLabelText(panel->porigvL, buffer);
108 static void resistanceCallback(WMWidget * w, void *data)
110 _Panel *panel = (_Panel *) data;
111 char buffer[64];
112 int i;
114 i = WMGetSliderValue(panel->resS);
116 if (i == 0)
117 WMSetLabelText(panel->resL, "OFF");
118 else {
119 sprintf(buffer, "%i", i);
120 WMSetLabelText(panel->resL, buffer);
124 static void resizeCallback(WMWidget * w, void *data)
126 _Panel *panel = (_Panel *) data;
127 char buffer[64];
128 int i;
130 i = WMGetSliderValue(panel->resizeS);
132 if (i == 0)
133 WMSetLabelText(panel->resizeL, "OFF");
134 else {
135 sprintf(buffer, "%i", i);
136 WMSetLabelText(panel->resizeL, buffer);
140 static int getPlacement(char *str)
142 if (!str)
143 return 0;
145 if (strcasecmp(str, "auto") == 0)
146 return 0;
147 else if (strcasecmp(str, "random") == 0)
148 return 1;
149 else if (strcasecmp(str, "manual") == 0)
150 return 2;
151 else if (strcasecmp(str, "cascade") == 0)
152 return 3;
153 else if (strcasecmp(str, "smart") == 0)
154 return 4;
155 else if (strcasecmp(str, "center") == 0)
156 return 5;
157 else
158 wwarning(_("bad option value %s in WindowPlacement. Using default value"), str);
159 return 0;
162 static void showData(_Panel * panel)
164 char *str;
165 WMPropList *arr;
166 int x, y;
168 str = GetStringForKey("WindowPlacement");
170 WMSetPopUpButtonSelectedItem(panel->placP, getPlacement(str));
172 arr = GetObjectForKey("WindowPlaceOrigin");
174 x = 0;
175 y = 0;
176 if (arr && (!WMIsPLArray(arr) || WMGetPropListItemCount(arr) != 2)) {
177 wwarning(_("invalid data in option WindowPlaceOrigin. Using default (0,0)"));
178 } else {
179 if (arr) {
180 x = atoi(WMGetFromPLString(WMGetFromPLArray(arr, 0)));
181 y = atoi(WMGetFromPLString(WMGetFromPLArray(arr, 1)));
185 WMSetSliderValue(panel->hsli, x);
186 WMSetSliderValue(panel->vsli, y);
188 sliderCallback(NULL, panel);
190 x = GetIntegerForKey("EdgeResistance");
191 WMSetSliderValue(panel->resS, x);
192 resistanceCallback(NULL, panel);
194 x = GetIntegerForKey("ResizeIncrement");
195 WMSetSliderValue(panel->resizeS, x);
196 resizeCallback(NULL, panel);
198 WMSetButtonSelected(panel->tranB, GetBoolForKey("OpenTransientOnOwnerWorkspace"));
200 WMSetButtonSelected(panel->opaqB, GetBoolForKey("OpaqueMove"));
202 WMSetButtonSelected(panel->opaqresizeB, GetBoolForKey("OpaqueResize"));
204 WMSetButtonSelected(panel->miconB, GetBoolForKey("NoWindowOverIcons"));
206 WMSetButtonSelected(panel->mdockB, GetBoolForKey("NoWindowOverDock"));
208 if (GetBoolForKey("Attraction"))
209 WMPerformButtonClick(panel->resrB);
210 else
211 WMPerformButtonClick(panel->resaB);
214 static void storeData(_Panel * panel)
216 WMPropList *arr;
217 char x[16], y[16];
219 SetBoolForKey(WMGetButtonSelected(panel->miconB), "NoWindowOverIcons");
220 SetBoolForKey(WMGetButtonSelected(panel->mdockB), "NoWindowOverDock");
221 SetBoolForKey(WMGetButtonSelected(panel->opaqB), "OpaqueMove");
222 SetBoolForKey(WMGetButtonSelected(panel->opaqresizeB), "OpaqueResize");
223 SetBoolForKey(WMGetButtonSelected(panel->tranB), "OpenTransientOnOwnerWorkspace");
224 SetStringForKey(placements[WMGetPopUpButtonSelectedItem(panel->placP)], "WindowPlacement");
225 sprintf(x, "%i", WMGetSliderValue(panel->hsli));
226 sprintf(y, "%i", WMGetSliderValue(panel->vsli));
227 arr = WMCreatePLArray(WMCreatePLString(x), WMCreatePLString(y), NULL);
228 SetObjectForKey(arr, "WindowPlaceOrigin");
229 SetIntegerForKey(WMGetSliderValue(panel->resS), "EdgeResistance");
230 SetIntegerForKey(WMGetSliderValue(panel->resizeS), "ResizeIncrement");
231 SetBoolForKey(WMGetButtonSelected(panel->resrB), "Attraction");
232 WMReleasePropList(arr);
235 static void createPanel(Panel * p)
237 _Panel *panel = (Panel *) p;
238 WMScreen *scr = WMWidgetScreen(panel->parent);
239 WMColor *color;
240 WMPixmap *pixmap;
241 int width, height;
242 int swidth, sheight;
243 char *path;
244 WMBox *hbox;
246 panel->box = WMCreateBox(panel->parent);
247 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
248 WMSetBoxHorizontal(panel->box, False);
249 WMSetBoxBorderWidth(panel->box, 8);
251 hbox = WMCreateBox(panel->box);
252 WMSetBoxHorizontal(hbox, True);
253 WMAddBoxSubview(panel->box, WMWidgetView(hbox), False, True, 110, 0, 10);
255 /************** Window Placement ***************/
256 panel->placF = WMCreateFrame(hbox);
257 WMMapWidget(panel->placF);
258 WMAddBoxSubview(hbox, WMWidgetView(panel->placF), True, True, 100, 0, 10);
260 WMSetFrameTitle(panel->placF, _("Window Placement"));
261 WMSetBalloonTextForView(_("How to place windows when they are first put\n"
262 "on screen."), WMWidgetView(panel->placF));
264 panel->placP = WMCreatePopUpButton(panel->placF);
265 WMResizeWidget(panel->placP, 105, 20);
266 WMMoveWidget(panel->placP, 15, 20);
267 WMAddPopUpButtonItem(panel->placP, _("Automatic"));
268 WMAddPopUpButtonItem(panel->placP, _("Random"));
269 WMAddPopUpButtonItem(panel->placP, _("Manual"));
270 WMAddPopUpButtonItem(panel->placP, _("Cascade"));
271 WMAddPopUpButtonItem(panel->placP, _("Smart"));
272 WMAddPopUpButtonItem(panel->placP, _("Center"));
274 panel->porigL = WMCreateLabel(panel->placF);
275 WMResizeWidget(panel->porigL, 120, 32);
276 WMMoveWidget(panel->porigL, 5, 45);
277 WMSetLabelTextAlignment(panel->porigL, WACenter);
278 WMSetLabelText(panel->porigL, _("Placement Origin"));
280 panel->porigvL = WMCreateLabel(panel->placF);
281 WMResizeWidget(panel->porigvL, 80, 20);
282 WMMoveWidget(panel->porigvL, 30, 75);
283 WMSetLabelTextAlignment(panel->porigvL, WACenter);
285 color = WMCreateRGBColor(scr, 0x5100, 0x5100, 0x7100, True);
286 panel->porigF = WMCreateFrame(panel->placF);
287 WMSetWidgetBackgroundColor(panel->porigF, color);
288 WMReleaseColor(color);
289 WMSetFrameRelief(panel->porigF, WRSunken);
291 swidth = WidthOfScreen(DefaultScreenOfDisplay(WMScreenDisplay(scr)));
292 sheight = HeightOfScreen(DefaultScreenOfDisplay(WMScreenDisplay(scr)));
294 if (sheight > swidth) {
295 height = 70;
296 width = 70 * swidth / sheight;
297 if (width > 240)
298 width = 240;
299 height = 240 * sheight / swidth;
300 } else {
301 width = 240;
302 height = 240 * sheight / swidth;
303 if (height > 70)
304 height = 70;
305 width = 70 * swidth / sheight;
307 WMResizeWidget(panel->porigF, width, height);
308 WMMoveWidget(panel->porigF, 130 + (240 - width) / 2, 20 + (70 - height) / 2);
310 panel->porigW = WMCreateLabel(panel->porigF);
311 WMResizeWidget(panel->porigW, THUMB_SIZE, THUMB_SIZE);
312 WMMoveWidget(panel->porigW, 2, 2);
313 WMSetLabelRelief(panel->porigW, WRRaised);
315 panel->hsli = WMCreateSlider(panel->placF);
316 WMResizeWidget(panel->hsli, width, 12);
317 WMMoveWidget(panel->hsli, 130 + (240 - width) / 2, 20 + (70 - height) / 2 + height + 2);
318 WMSetSliderAction(panel->hsli, sliderCallback, panel);
319 WMSetSliderMinValue(panel->hsli, 0);
320 WMSetSliderMaxValue(panel->hsli, swidth);
322 panel->vsli = WMCreateSlider(panel->placF);
323 WMResizeWidget(panel->vsli, 12, height);
324 WMMoveWidget(panel->vsli, 130 + (240 - width) / 2 + width + 2, 20 + (70 - height) / 2);
325 WMSetSliderAction(panel->vsli, sliderCallback, panel);
326 WMSetSliderMinValue(panel->vsli, 0);
327 WMSetSliderMaxValue(panel->vsli, sheight);
329 WMMapSubwidgets(panel->porigF);
331 WMMapSubwidgets(panel->placF);
333 /************** Opaque Move, Resize ***************/
334 panel->opaqF = WMCreateFrame(hbox);
335 WMMapWidget(panel->opaqF);
336 WMAddBoxSubview(hbox, WMWidgetView(panel->opaqF), False, True, 122, 0, 0);
338 WMSetFrameTitle(panel->opaqF, _("Opaque Move/Resize"));
339 WMSetBalloonTextForView(_("Whether the window contents or only a frame should\n"
340 "be displayed during a move or resize.\n"),
341 WMWidgetView(panel->opaqF));
343 panel->opaqB = WMCreateButton(panel->opaqF, WBTToggle);
344 WMResizeWidget(panel->opaqB, 48,48);
345 WMMoveWidget(panel->opaqB, 7, 35);
346 WMSetButtonImagePosition(panel->opaqB, WIPImageOnly);
348 path = LocateImage(NON_OPAQUE_MOVE_PIXMAP);
349 if (path) {
350 pixmap = WMCreatePixmapFromFile(scr, path);
351 if (pixmap) {
352 WMSetButtonImage(panel->opaqB, pixmap);
353 WMReleasePixmap(pixmap);
354 } else {
355 wwarning(_("could not load icon %s"), path);
357 wfree(path);
360 path = LocateImage(OPAQUE_MOVE_PIXMAP);
361 if (path) {
362 pixmap = WMCreatePixmapFromFile(scr, path);
363 if (pixmap) {
364 WMSetButtonAltImage(panel->opaqB, pixmap);
365 WMReleasePixmap(pixmap);
366 } else {
367 wwarning(_("could not load icon %s"), path);
369 wfree(path);
374 panel->opaqresizeB = WMCreateButton(panel->opaqF, WBTToggle);
375 WMResizeWidget(panel->opaqresizeB, 48,48);
376 WMMoveWidget(panel->opaqresizeB, 65, 35);
377 WMSetButtonImagePosition(panel->opaqresizeB, WIPImageOnly);
379 path = LocateImage(NON_OPAQUE_RESIZE_PIXMAP);
380 if (path) {
381 pixmap = WMCreatePixmapFromFile(scr, path);
382 if (pixmap) {
383 WMSetButtonImage(panel->opaqresizeB, pixmap);
384 WMReleasePixmap(pixmap);
385 } else {
386 wwarning(_("could not load icon %s"), path);
388 wfree(path);
391 path = LocateImage(OPAQUE_RESIZE_PIXMAP);
392 if (path) {
393 pixmap = WMCreatePixmapFromFile(scr, path);
394 if (pixmap) {
395 WMSetButtonAltImage(panel->opaqresizeB, pixmap);
396 WMReleasePixmap(pixmap);
397 } else {
398 wwarning(_("could not load icon %s"), path);
400 wfree(path);
403 WMMapSubwidgets(panel->opaqF);
406 /**************** Account for Icon/Dock ***************/
407 panel->maxiF = WMCreateFrame(panel->box);
408 WMResizeWidget(panel->maxiF, 205, 95);
409 WMMoveWidget(panel->maxiF, 305, 125);
410 WMSetFrameTitle(panel->maxiF, _("When maximizing..."));
412 panel->miconB = WMCreateSwitchButton(panel->maxiF);
413 WMResizeWidget(panel->miconB, 190, 30);
414 WMMoveWidget(panel->miconB, 10, 12);
415 WMSetButtonText(panel->miconB, _("...do not cover icons"));
417 panel->mdockB = WMCreateSwitchButton(panel->maxiF);
418 WMResizeWidget(panel->mdockB, 190, 30);
419 WMMoveWidget(panel->mdockB, 10, 35);
421 WMSetButtonText(panel->mdockB, _("...do not cover dock"));
423 panel->resizeS = WMCreateSlider(panel->maxiF);
424 WMResizeWidget(panel->resizeS, 50, 15);
425 WMMoveWidget(panel->resizeS, 10, 70);
426 WMSetSliderMinValue(panel->resizeS, 0);
427 WMSetSliderMaxValue(panel->resizeS, 100);
428 WMSetSliderAction(panel->resizeS, resizeCallback, panel);
430 panel->resizeL = WMCreateLabel(panel->maxiF);
431 WMResizeWidget(panel->resizeL, 30, 15);
432 WMMoveWidget(panel->resizeL, 60, 70);
434 panel->resizeTextL = WMCreateLabel(panel->maxiF);
435 WMSetLabelText(panel->resizeTextL, "Mod+Wheel\nresize increment");
436 WMResizeWidget(panel->resizeTextL, 110, 30);
437 WMMoveWidget(panel->resizeTextL, 90, 62);
439 WMMapSubwidgets(panel->maxiF);
441 /**************** Edge Resistance ****************/
443 panel->resF = WMCreateFrame(panel->box);
444 WMResizeWidget(panel->resF, 285, 45);
445 WMMoveWidget(panel->resF, 8, 125);
446 WMSetFrameTitle(panel->resF, _("Edge Resistance"));
448 WMSetBalloonTextForView(_("Edge resistance will make windows `resist'\n"
449 "being moved further for the defined threshold\n"
450 "when moved against other windows or the edges\n"
451 "of the screen."), WMWidgetView(panel->resF));
453 panel->resS = WMCreateSlider(panel->resF);
454 WMResizeWidget(panel->resS, 80, 15);
455 WMMoveWidget(panel->resS, 10, 20);
456 WMSetSliderMinValue(panel->resS, 0);
457 WMSetSliderMaxValue(panel->resS, 80);
458 WMSetSliderAction(panel->resS, resistanceCallback, panel);
460 panel->resL = WMCreateLabel(panel->resF);
461 WMResizeWidget(panel->resL, 30, 15);
462 WMMoveWidget(panel->resL, 95, 20);
464 panel->resaB = WMCreateRadioButton(panel->resF);
465 WMMoveWidget(panel->resaB, 130, 15);
466 WMResizeWidget(panel->resaB, 70, 25);
467 WMSetButtonText(panel->resaB, _("Resist"));
469 panel->resrB = WMCreateRadioButton(panel->resF);
470 WMMoveWidget(panel->resrB, 200, 15);
471 WMResizeWidget(panel->resrB, 70, 25);
472 WMSetButtonText(panel->resrB, _("Attract"));
473 WMGroupButtons(panel->resrB, panel->resaB);
475 WMMapSubwidgets(panel->resF);
477 /**************** Transients on Parent Workspace ****************/
479 panel->tranF = WMCreateFrame(panel->box);
480 WMResizeWidget(panel->tranF, 285, 40);
481 WMMoveWidget(panel->tranF, 8, 180);
483 panel->tranB = WMCreateSwitchButton(panel->tranF);
484 WMMoveWidget(panel->tranB, 10, 5);
485 WMResizeWidget(panel->tranB, 250, 30);
486 WMSetButtonText(panel->tranB, _("Open dialogs in the same workspace\nas their owners"));
488 WMMapSubwidgets(panel->tranF);
490 WMRealizeWidget(panel->box);
491 WMMapSubwidgets(panel->box);
493 /* show the config data */
494 showData(panel);
497 static void undo(_Panel * panel)
499 showData(panel);
502 Panel *InitWindowHandling(WMScreen * scr, WMWidget * parent)
504 _Panel *panel;
506 panel = wmalloc(sizeof(_Panel));
508 panel->sectionName = _("Window Handling Preferences");
510 panel->description = _("Window handling options. Initial placement style\n"
511 "edge resistance, opaque move etc.");
513 panel->parent = parent;
515 panel->callbacks.createWidgets = createPanel;
516 panel->callbacks.updateDomain = storeData;
517 panel->callbacks.undoChanges = undo;
519 AddSection(panel, ICON_FILE);
521 return panel;