windowbox.c revision df1c27a6
1/*
2 * Copyright 1992 Claude Lecommandeur.
3 */
4
5
6#include "ctwm.h"
7
8#include <stdio.h>
9#include <stdlib.h>
10
11#include <X11/Xatom.h>
12
13#include "screen.h"
14#include "add_window.h"
15#include "list.h"
16#include "windowbox.h"
17#include "win_decorations.h"
18#include "win_resize.h"
19#include "win_utils.h"
20#include "xparsegeometry.h"
21
22name_list **addWindowBox(char *boxname, char *geometry)
23{
24	WindowBox *winbox;
25
26#if 0
27	printf("addWindowBox : name = %s, geometry = %s\n", boxname, geometry);
28#endif
29	winbox = malloc(sizeof(WindowBox));
30	winbox->next     = NULL;
31	winbox->name     = strdup(boxname);
32	winbox->geometry = strdup(geometry);
33	winbox->winlist  = NULL;
34	if(!Scr->FirstWindowBox) {
35		Scr->FirstWindowBox = winbox;
36	}
37	return (&(winbox->winlist));
38}
39
40void createWindowBoxes(void)
41{
42	WindowBox *winbox;
43	char title [128];
44	XWMHints      wmhints;
45	XSizeHints    sizehints;
46
47	for(winbox = Scr->FirstWindowBox; winbox; winbox = winbox->next) {
48		int mask, x, y, gravity;
49		unsigned int w, h;
50		Window win;
51
52		mask = RLayoutXParseGeometry(Scr->Layout, winbox->geometry,
53		                             &x, &y, &w, &h);
54		if(mask & XNegative) {
55			x += Scr->rootw  - w;
56			gravity = (mask & YNegative) ? SouthEastGravity : NorthEastGravity;
57		}
58		else {
59			gravity = (mask & YNegative) ? SouthWestGravity : NorthWestGravity;
60		}
61		if(mask & YNegative) {
62			y += Scr->rooth - h;
63		}
64
65		win = XCreateSimpleWindow(dpy, Scr->Root, x, y, w, h, 0, Scr->Black,
66		                          Scr->White);
67#if 0
68		printf("createWindowBoxes : name = %s, win = 0x%x, x = %d, y = %d, w = %d, h = %d\n",
69		       winbox->name, win, x, y, w, h);
70#endif
71		sprintf(title, "%s", winbox->name);
72
73		sizehints.flags  = USPosition | USSize | PWinGravity;
74		sizehints.x      = x;
75		sizehints.y      = y;
76		sizehints.width  = w;
77		sizehints.height = h;
78		sizehints.win_gravity = gravity;
79
80		wmhints.initial_state = NormalState;
81		wmhints.input         = True;
82		wmhints.flags         = InputHint | StateHint;
83
84		XmbSetWMProperties(dpy, win, title, title, NULL, 0,
85		                   &sizehints, &wmhints, NULL);
86
87		winbox->window = win;
88		winbox->twmwin = AddWindow(win, AWT_WINDOWBOX, NULL, Scr->currentvs);
89		if(!winbox->twmwin) {
90			fprintf(stderr, "cannot create %s window box, exiting...\n", winbox->name);
91			exit(1);
92		}
93		winbox->twmwin->iswinbox = true;
94		XMapWindow(dpy, win);
95	}
96}
97
98WindowBox *findWindowBox(TwmWindow *twmwin)
99{
100	WindowBox *winbox;
101	if(twmwin->iswinbox) {
102		return NULL;
103	}
104	if(!Scr->FirstWindowBox) {
105		return NULL;
106	}
107	for(winbox = Scr->FirstWindowBox; winbox; winbox = winbox->next) {
108		if(LookInList(winbox->winlist, twmwin->name, &twmwin->class)) {
109			if(visible(winbox->twmwin)) {
110				twmwin->winbox = winbox;
111				return winbox;
112			}
113		}
114	}
115	return NULL;
116}
117
118void ConstrainedToWinBox(TwmWindow *twmwin, int x, int y, int *nx, int *ny)
119{
120	XWindowAttributes attr;
121
122	*nx = x;
123	*ny = y;
124	XGetWindowAttributes(dpy, twmwin->winbox->window, &attr);
125	if(x < 0) {
126		*nx = 0;
127	}
128	if(y < 0) {
129		*ny = 0;
130	}
131	if(x >  attr.width - 1) {
132		*nx = attr.width - 1;
133	}
134	if(y > attr.height - 1) {
135		*ny = attr.height - 1;
136	}
137}
138
139void fittocontent(TwmWindow *twmwin)
140{
141	TwmWindow   *t;
142	int minx, miny, maxx, maxy, x, y, w, h;
143	minx = Scr->rootw;
144	miny = Scr->rooth;
145	maxx = 0;
146	maxy = 0;
147	for(t = Scr->FirstWindow; t != NULL; t = t->next) {
148		if(t->winbox && (t->winbox->twmwin == twmwin)) {
149			if(t->frame_x < minx) {
150				minx = t->frame_x;
151			}
152			if(t->frame_y < miny) {
153				miny = t->frame_y;
154			}
155			w = t->frame_width  + 2 * t->frame_bw;
156			h = t->frame_height + 2 * t->frame_bw;
157			if(t->frame_x + w > maxx) {
158				maxx = t->frame_x + w;
159			}
160			if(t->frame_y + h > maxy) {
161				maxy = t->frame_y + h;
162			}
163		}
164	}
165	x = twmwin->frame_x + minx;
166	y = twmwin->frame_y + miny;
167	w = maxx - minx + 2 * twmwin->frame_bw3D;
168	h = maxy - miny + 2 * twmwin->frame_bw3D;
169	SetupWindow(twmwin, x, y, w, h, -1);
170	for(t = Scr->FirstWindow; t != NULL; t = t->next) {
171		if(t->winbox && (t->winbox->twmwin == twmwin)) {
172			SetupWindow(t, t->frame_x - minx, t->frame_y - miny,
173			            t->frame_width, t->frame_height, -1);
174		}
175	}
176}
177