~ctwm/ctwm/windowstack2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * Various operations done on windows.
 */

#include "ctwm.h"

#include <stdio.h>

#include "animate.h"
#include "colormaps.h"
#include "drawing.h"
#include "events.h"
#include "iconmgr.h"
#include "image.h"
#include "otp.h"
#include "screen.h"
#include "win_decorations.h"
#include "win_iconify.h"
#include "win_ops.h"
#include "win_utils.h"


/*
 * Update the visuals of a window (e.g., its own decorations and its
 * representation in the icon manager) for having/losing focus.
 *
 * Formerly in util.c
 */
void
SetFocusVisualAttributes(TwmWindow *tmp_win, bool focus)
{
	if(! tmp_win) {
		return;
	}

	if(focus == tmp_win->hasfocusvisible) {
		return;
	}
	if(tmp_win->highlight) {
		if(Scr->use3Dborders) {
			PaintBorders(tmp_win, focus);
		}
		else {
			if(focus) {
				XSetWindowBorder(dpy, tmp_win->frame, tmp_win->borderC.back);
				if(tmp_win->title_w) {
					XSetWindowBorder(dpy, tmp_win->title_w, tmp_win->borderC.back);
				}
			}
			else {
				/*
				 * XXX It seems possible this could be replaced by a
				 * single global 'gray' pixmap; I don't think it actually
				 * varies per window, and I don't see any obvious reason
				 * it can't be reused, so we may be able to save an
				 * allocation for each window by doing so...
				 */
				XSetWindowBorderPixmap(dpy, tmp_win->frame, tmp_win->gray);
				if(tmp_win->title_w) {
					XSetWindowBorderPixmap(dpy, tmp_win->title_w, tmp_win->gray);
				}
			}
		}
	}

	if(focus) {
		bool hil = false;

		if(tmp_win->lolite_wl) {
			XUnmapWindow(dpy, tmp_win->lolite_wl);
		}
		if(tmp_win->lolite_wr) {
			XUnmapWindow(dpy, tmp_win->lolite_wr);
		}
		if(tmp_win->hilite_wl) {
			XMapWindow(dpy, tmp_win->hilite_wl);
			hil = true;
		}
		if(tmp_win->hilite_wr) {
			XMapWindow(dpy, tmp_win->hilite_wr);
			hil = true;
		}
		if(hil && tmp_win->HiliteImage && tmp_win->HiliteImage->next) {
			MaybeAnimate = true;
		}
		if(tmp_win->iconmanagerlist) {
			ActiveIconManager(tmp_win->iconmanagerlist);
		}
	}
	else {
		if(tmp_win->hilite_wl) {
			XUnmapWindow(dpy, tmp_win->hilite_wl);
		}
		if(tmp_win->hilite_wr) {
			XUnmapWindow(dpy, tmp_win->hilite_wr);
		}
		if(tmp_win->lolite_wl) {
			XMapWindow(dpy, tmp_win->lolite_wl);
		}
		if(tmp_win->lolite_wr) {
			XMapWindow(dpy, tmp_win->lolite_wr);
		}
		if(tmp_win->iconmanagerlist) {
			NotActiveIconManager(tmp_win->iconmanagerlist);
		}
	}
	if(Scr->use3Dtitles && Scr->SunkFocusWindowTitle && tmp_win->title_height) {
		ButtonState bs;

		bs = focus ? on : off;
		Draw3DBorder(tmp_win->title_w, Scr->TBInfo.titlex, 0,
		             tmp_win->title_width - Scr->TBInfo.titlex -
		             Scr->TBInfo.rightoff - Scr->TitlePadding,
		             Scr->TitleHeight, Scr->TitleShadowDepth,
		             tmp_win->title, bs, false, false);
	}
	tmp_win->hasfocusvisible = focus;
}


/*
 * Shift the focus to a given window, and do whatever subsidiary ops that
 * entails.
 *
 * Formerly in util.c
 */
void
SetFocus(TwmWindow *tmp_win, Time tim)
{
	Window w = (tmp_win ? tmp_win->w : PointerRoot);
	bool f_iconmgr = false;

	if(Scr->Focus && (Scr->Focus->isiconmgr)) {
		f_iconmgr = true;
	}
	if(Scr->SloppyFocus && (w == PointerRoot) && (!f_iconmgr)) {
		return;
	}

	XSetInputFocus(dpy, w, RevertToPointerRoot, tim);
#ifdef EWMH
	EwmhSet_NET_ACTIVE_WINDOW(w);
#endif
	if(Scr->Focus == tmp_win) {
		return;
	}

	if(Scr->Focus) {
		if(Scr->Focus->AutoSqueeze && !Scr->Focus->squeezed) {
			AutoSqueeze(Scr->Focus);
		}
		SetFocusVisualAttributes(Scr->Focus, false);
#ifdef EWMH
		// Priority may change when focus does
		if(OtpIsFocusDependent(Scr->Focus)) {
			OtpUnfocusWindow(Scr->Focus);
			// That Scr->Focus = NULL's internally for us, but we don't
			// care, since we're about to reset it if we need to.
		}
#endif
	}

	if(tmp_win) {
		if(tmp_win->AutoSqueeze && tmp_win->squeezed) {
			AutoSqueeze(tmp_win);
		}
		SetFocusVisualAttributes(tmp_win, true);
#ifdef EWMH
		// Priority may change when focus does
		if(OtpIsFocusDependent(tmp_win)) {
			OtpFocusWindow(tmp_win);
			// Pre-sets Scr->Focus
		}
#endif
	}

	// in the EWMH cases, this was already done.
	Scr->Focus = tmp_win;

	return;
}


/*
 * Move the focus straight to the root, with associated cleanup.
 *
 * Formerly in menus.c
 */
void FocusOnRoot(void)
{
	SetFocus(NULL, EventTime);
	InstallColormaps(0, &Scr->RootColormaps);
	if(! Scr->ClickToFocus) {
		Scr->FocusRoot = true;
	}
}


/*
 * Handle doing squeezing bits for AutoSqueeze{} windows.
 *
 * Formerly in menus.c
 */
void
AutoSqueeze(TwmWindow *tmp_win)
{
	if(tmp_win->isiconmgr) {
		return;
	}
	if(Scr->RaiseWhenAutoUnSqueeze && tmp_win->squeezed) {
		OtpRaise(tmp_win, WinWin);
	}
	Squeeze(tmp_win);
}


/*
 * Toggle a window's squeezed state.
 *
 * Formerly in menus.c
 */
void
Squeeze(TwmWindow *tmp_win)
{
	long fx, fy, savex, savey;
	int  neww, newh;
	bool south;
	int  grav = ((tmp_win->hints.flags & PWinGravity)
	             ? tmp_win->hints.win_gravity : NorthWestGravity);
	long eventMask;
	if(tmp_win->squeezed) {
		tmp_win->squeezed = false;
#ifdef EWMH
		EwmhSet_NET_WM_STATE(tmp_win, EWMH_STATE_SHADED);
#endif /* EWMH */
		if(!tmp_win->isicon) {
			XMapWindow(dpy, tmp_win->w);
		}
		SetupWindow(tmp_win, tmp_win->actual_frame_x, tmp_win->actual_frame_y,
		            tmp_win->actual_frame_width, tmp_win->actual_frame_height, -1);
		ReMapTransients(tmp_win);
		return;
	}

	newh = tmp_win->title_height + 2 * tmp_win->frame_bw3D;
	if(newh < 3) {
		XBell(dpy, 0);
		return;
	}
	switch(grav) {
		case SouthWestGravity :
		case SouthGravity :
		case SouthEastGravity :
			south = true;
			break;
		default :
			south = false;
			break;
	}
	if(tmp_win->title_height && !tmp_win->AlwaysSqueezeToGravity) {
		south = false;
	}

	tmp_win->squeezed = true;
	tmp_win->actual_frame_width  = tmp_win->frame_width;
	tmp_win->actual_frame_height = tmp_win->frame_height;
	savex = fx = tmp_win->frame_x;
	savey = fy = tmp_win->frame_y;
	neww  = tmp_win->actual_frame_width;
	if(south) {
		fy += tmp_win->frame_height - newh;
	}
	if(tmp_win->squeeze_info) {
		fx  += tmp_win->title_x + tmp_win->frame_bw - tmp_win->frame_bw3D;
		neww = tmp_win->title_width + 2 * tmp_win->frame_bw3D;
	}

	eventMask = mask_out_event(tmp_win->w, StructureNotifyMask);
#ifdef EWMH
	EwmhSet_NET_WM_STATE(tmp_win, EWMH_STATE_SHADED);
#endif /* EWMH */
	XUnmapWindow(dpy, tmp_win->w);
	restore_mask(tmp_win->w, eventMask);

	if(fx + neww >= Scr->rootw - Scr->BorderRight) {
		fx = Scr->rootw - Scr->BorderRight - neww;
	}
	if(fy + newh >= Scr->rooth - Scr->BorderBottom) {
		fy = Scr->rooth - Scr->BorderBottom - newh;
	}
	SetupWindow(tmp_win, fx, fy, neww, newh, -1);
	tmp_win->actual_frame_x = savex;
	tmp_win->actual_frame_y = savey;

	/* Now make the group members disappear */
	UnmapTransients(tmp_win, false, eventMask);
}


/***********************************************************************
 *
 *  Procedure:
 *      MoveOutline - move a window outline
 *
 *  Inputs:
 *      root        - the window we are outlining
 *      x           - upper left x coordinate
 *      y           - upper left y coordinate
 *      width       - the width of the rectangle
 *      height      - the height of the rectangle
 *      bw          - the border width of the frame
 *      th          - title height
 *
 ***********************************************************************
 */
void
MoveOutline(Window root, int x, int y, int width, int height, int bw, int th)
{
	static int  lastx = 0;
	static int  lasty = 0;
	static int  lastWidth = 0;
	static int  lastHeight = 0;
	static int  lastBW = 0;
	static int  lastTH = 0;
	int         xl, xr, yt, yb, xinnerl, xinnerr, yinnert, yinnerb;
	int         xthird, ythird;
	XSegment    outline[18];
	XSegment   *r;

	if(x == lastx && y == lasty && width == lastWidth && height == lastHeight
	                && lastBW == bw && th == lastTH) {
		return;
	}

	r = outline;

#define DRAWIT() \
    if (lastWidth || lastHeight)                        \
    {                                                   \
        xl = lastx;                                     \
        xr = lastx + lastWidth - 1;                     \
        yt = lasty;                                     \
        yb = lasty + lastHeight - 1;                    \
        xinnerl = xl + lastBW;                          \
        xinnerr = xr - lastBW;                          \
        yinnert = yt + lastTH + lastBW;                 \
        yinnerb = yb - lastBW;                          \
        xthird = (xinnerr - xinnerl) / 3;               \
        ythird = (yinnerb - yinnert) / 3;               \
                                                        \
        r->x1 = xl;                                     \
        r->y1 = yt;                                     \
        r->x2 = xr;                                     \
        r->y2 = yt;                                     \
        r++;                                            \
                                                        \
        r->x1 = xl;                                     \
        r->y1 = yb;                                     \
        r->x2 = xr;                                     \
        r->y2 = yb;                                     \
        r++;                                            \
                                                        \
        r->x1 = xl;                                     \
        r->y1 = yt;                                     \
        r->x2 = xl;                                     \
        r->y2 = yb;                                     \
        r++;                                            \
                                                        \
        r->x1 = xr;                                     \
        r->y1 = yt;                                     \
        r->x2 = xr;                                     \
        r->y2 = yb;                                     \
        r++;                                            \
                                                        \
        r->x1 = xinnerl + xthird;                       \
        r->y1 = yinnert;                                \
        r->x2 = r->x1;                                  \
        r->y2 = yinnerb;                                \
        r++;                                            \
                                                        \
        r->x1 = xinnerl + (2 * xthird);                 \
        r->y1 = yinnert;                                \
        r->x2 = r->x1;                                  \
        r->y2 = yinnerb;                                \
        r++;                                            \
                                                        \
        r->x1 = xinnerl;                                \
        r->y1 = yinnert + ythird;                       \
        r->x2 = xinnerr;                                \
        r->y2 = r->y1;                                  \
        r++;                                            \
                                                        \
        r->x1 = xinnerl;                                \
        r->y1 = yinnert + (2 * ythird);                 \
        r->x2 = xinnerr;                                \
        r->y2 = r->y1;                                  \
        r++;                                            \
                                                        \
        if (lastTH != 0) {                              \
            r->x1 = xl;                                 \
            r->y1 = yt + lastTH;                        \
            r->x2 = xr;                                 \
            r->y2 = r->y1;                              \
            r++;                                        \
        }                                               \
    }

	/* undraw the old one, if any */
	DRAWIT();

	lastx = x;
	lasty = y;
	lastWidth = width;
	lastHeight = height;
	lastBW = bw;
	lastTH = th;

	/* draw the new one, if any */
	DRAWIT();

#undef DRAWIT


	if(r != outline) {
		XDrawSegments(dpy, root, Scr->DrawGC, outline, r - outline);
	}
}