10bbfda8aSnia/*
20bbfda8aSnia * Copyright 1992 Claude Lecommandeur.
3645f5050Syouri */
4645f5050Syouri
5645f5050Syouri
60bbfda8aSnia#include "ctwm.h"
70bbfda8aSnia
8645f5050Syouri#include <stdio.h>
90bbfda8aSnia#include <stdlib.h>
100bbfda8aSnia
11645f5050Syouri#include <X11/Xatom.h>
120bbfda8aSnia
13645f5050Syouri#include "screen.h"
14645f5050Syouri#include "add_window.h"
150bbfda8aSnia#include "list.h"
16645f5050Syouri#include "windowbox.h"
170bbfda8aSnia#include "win_decorations.h"
180bbfda8aSnia#include "win_resize.h"
190bbfda8aSnia#include "win_utils.h"
20df1c27a6Snia#include "xparsegeometry.h"
21645f5050Syouri
220bbfda8aSnianame_list **addWindowBox(char *boxname, char *geometry)
23645f5050Syouri{
240bbfda8aSnia	WindowBox *winbox;
25645f5050Syouri
26645f5050Syouri#if 0
270bbfda8aSnia	printf("addWindowBox : name = %s, geometry = %s\n", boxname, geometry);
28645f5050Syouri#endif
290bbfda8aSnia	winbox = malloc(sizeof(WindowBox));
300bbfda8aSnia	winbox->next     = NULL;
310bbfda8aSnia	winbox->name     = strdup(boxname);
320bbfda8aSnia	winbox->geometry = strdup(geometry);
330bbfda8aSnia	winbox->winlist  = NULL;
340bbfda8aSnia	if(!Scr->FirstWindowBox) {
350bbfda8aSnia		Scr->FirstWindowBox = winbox;
360bbfda8aSnia	}
370bbfda8aSnia	return (&(winbox->winlist));
38645f5050Syouri}
39645f5050Syouri
400bbfda8aSniavoid createWindowBoxes(void)
41645f5050Syouri{
420bbfda8aSnia	WindowBox *winbox;
430bbfda8aSnia	char title [128];
440bbfda8aSnia	XWMHints      wmhints;
450bbfda8aSnia	XSizeHints    sizehints;
460bbfda8aSnia
470bbfda8aSnia	for(winbox = Scr->FirstWindowBox; winbox; winbox = winbox->next) {
480bbfda8aSnia		int mask, x, y, gravity;
490bbfda8aSnia		unsigned int w, h;
500bbfda8aSnia		Window win;
510bbfda8aSnia
52df1c27a6Snia		mask = RLayoutXParseGeometry(Scr->Layout, winbox->geometry,
53df1c27a6Snia		                             &x, &y, &w, &h);
540bbfda8aSnia		if(mask & XNegative) {
550bbfda8aSnia			x += Scr->rootw  - w;
560bbfda8aSnia			gravity = (mask & YNegative) ? SouthEastGravity : NorthEastGravity;
570bbfda8aSnia		}
580bbfda8aSnia		else {
590bbfda8aSnia			gravity = (mask & YNegative) ? SouthWestGravity : NorthWestGravity;
600bbfda8aSnia		}
610bbfda8aSnia		if(mask & YNegative) {
620bbfda8aSnia			y += Scr->rooth - h;
630bbfda8aSnia		}
640bbfda8aSnia
650bbfda8aSnia		win = XCreateSimpleWindow(dpy, Scr->Root, x, y, w, h, 0, Scr->Black,
660bbfda8aSnia		                          Scr->White);
670bbfda8aSnia#if 0
680bbfda8aSnia		printf("createWindowBoxes : name = %s, win = 0x%x, x = %d, y = %d, w = %d, h = %d\n",
690bbfda8aSnia		       winbox->name, win, x, y, w, h);
700bbfda8aSnia#endif
710bbfda8aSnia		sprintf(title, "%s", winbox->name);
720bbfda8aSnia
730bbfda8aSnia		sizehints.flags  = USPosition | USSize | PWinGravity;
740bbfda8aSnia		sizehints.x      = x;
750bbfda8aSnia		sizehints.y      = y;
760bbfda8aSnia		sizehints.width  = w;
770bbfda8aSnia		sizehints.height = h;
780bbfda8aSnia		sizehints.win_gravity = gravity;
790bbfda8aSnia
800bbfda8aSnia		wmhints.initial_state = NormalState;
810bbfda8aSnia		wmhints.input         = True;
820bbfda8aSnia		wmhints.flags         = InputHint | StateHint;
830bbfda8aSnia
840bbfda8aSnia		XmbSetWMProperties(dpy, win, title, title, NULL, 0,
850bbfda8aSnia		                   &sizehints, &wmhints, NULL);
860bbfda8aSnia
870bbfda8aSnia		winbox->window = win;
880bbfda8aSnia		winbox->twmwin = AddWindow(win, AWT_WINDOWBOX, NULL, Scr->currentvs);
890bbfda8aSnia		if(!winbox->twmwin) {
900bbfda8aSnia			fprintf(stderr, "cannot create %s window box, exiting...\n", winbox->name);
910bbfda8aSnia			exit(1);
920bbfda8aSnia		}
930bbfda8aSnia		winbox->twmwin->iswinbox = true;
940bbfda8aSnia		XMapWindow(dpy, win);
95645f5050Syouri	}
96645f5050Syouri}
97645f5050Syouri
980bbfda8aSniaWindowBox *findWindowBox(TwmWindow *twmwin)
99645f5050Syouri{
1000bbfda8aSnia	WindowBox *winbox;
1010bbfda8aSnia	if(twmwin->iswinbox) {
1020bbfda8aSnia		return NULL;
1030bbfda8aSnia	}
1040bbfda8aSnia	if(!Scr->FirstWindowBox) {
1050bbfda8aSnia		return NULL;
1060bbfda8aSnia	}
1070bbfda8aSnia	for(winbox = Scr->FirstWindowBox; winbox; winbox = winbox->next) {
1080bbfda8aSnia		if(LookInList(winbox->winlist, twmwin->name, &twmwin->class)) {
1090bbfda8aSnia			if(visible(winbox->twmwin)) {
1100bbfda8aSnia				twmwin->winbox = winbox;
1110bbfda8aSnia				return winbox;
1120bbfda8aSnia			}
1130bbfda8aSnia		}
114645f5050Syouri	}
1150bbfda8aSnia	return NULL;
116645f5050Syouri}
117645f5050Syouri
1180bbfda8aSniavoid ConstrainedToWinBox(TwmWindow *twmwin, int x, int y, int *nx, int *ny)
119645f5050Syouri{
1200bbfda8aSnia	XWindowAttributes attr;
1210bbfda8aSnia
1220bbfda8aSnia	*nx = x;
1230bbfda8aSnia	*ny = y;
1240bbfda8aSnia	XGetWindowAttributes(dpy, twmwin->winbox->window, &attr);
1250bbfda8aSnia	if(x < 0) {
1260bbfda8aSnia		*nx = 0;
1270bbfda8aSnia	}
1280bbfda8aSnia	if(y < 0) {
1290bbfda8aSnia		*ny = 0;
1300bbfda8aSnia	}
1310bbfda8aSnia	if(x >  attr.width - 1) {
1320bbfda8aSnia		*nx = attr.width - 1;
1330bbfda8aSnia	}
1340bbfda8aSnia	if(y > attr.height - 1) {
1350bbfda8aSnia		*ny = attr.height - 1;
1360bbfda8aSnia	}
137645f5050Syouri}
138645f5050Syouri
1390bbfda8aSniavoid fittocontent(TwmWindow *twmwin)
140645f5050Syouri{
1410bbfda8aSnia	TwmWindow   *t;
1420bbfda8aSnia	int minx, miny, maxx, maxy, x, y, w, h;
1430bbfda8aSnia	minx = Scr->rootw;
1440bbfda8aSnia	miny = Scr->rooth;
1450bbfda8aSnia	maxx = 0;
1460bbfda8aSnia	maxy = 0;
1470bbfda8aSnia	for(t = Scr->FirstWindow; t != NULL; t = t->next) {
1480bbfda8aSnia		if(t->winbox && (t->winbox->twmwin == twmwin)) {
1490bbfda8aSnia			if(t->frame_x < minx) {
1500bbfda8aSnia				minx = t->frame_x;
1510bbfda8aSnia			}
1520bbfda8aSnia			if(t->frame_y < miny) {
1530bbfda8aSnia				miny = t->frame_y;
1540bbfda8aSnia			}
1550bbfda8aSnia			w = t->frame_width  + 2 * t->frame_bw;
1560bbfda8aSnia			h = t->frame_height + 2 * t->frame_bw;
1570bbfda8aSnia			if(t->frame_x + w > maxx) {
1580bbfda8aSnia				maxx = t->frame_x + w;
1590bbfda8aSnia			}
1600bbfda8aSnia			if(t->frame_y + h > maxy) {
1610bbfda8aSnia				maxy = t->frame_y + h;
1620bbfda8aSnia			}
1630bbfda8aSnia		}
164645f5050Syouri	}
1650bbfda8aSnia	x = twmwin->frame_x + minx;
1660bbfda8aSnia	y = twmwin->frame_y + miny;
1670bbfda8aSnia	w = maxx - minx + 2 * twmwin->frame_bw3D;
1680bbfda8aSnia	h = maxy - miny + 2 * twmwin->frame_bw3D;
1690bbfda8aSnia	SetupWindow(twmwin, x, y, w, h, -1);
1700bbfda8aSnia	for(t = Scr->FirstWindow; t != NULL; t = t->next) {
1710bbfda8aSnia		if(t->winbox && (t->winbox->twmwin == twmwin)) {
1720bbfda8aSnia			SetupWindow(t, t->frame_x - minx, t->frame_y - miny,
1730bbfda8aSnia			            t->frame_width, t->frame_height, -1);
1740bbfda8aSnia		}
175645f5050Syouri	}
176645f5050Syouri}
177