wmii/cmd/wm/column.c

273 lines
6.9 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
#include <stdlib.h>
#include <string.h>
#include "wm.h"
2006-01-26 20:29:49 +03:00
void
arrange_column(Page *p, Area *col)
{
size_t i;
2006-01-26 17:24:34 +03:00
unsigned int h;
for(i = 0; (i < col->clientsz) && col->client[i]; i++);
2006-01-26 17:24:34 +03:00
h = col->rect.height;
if(i)
h /= i;
2006-01-26 17:24:34 +03:00
for(i = 0; (i < col->clientsz) && col->client[i]; i++) {
Client *c = col->client[i];
c->frame.rect = col->rect;
c->frame.rect.y += i * h;
if((i + 1 < col->clientsz) && col->client[i + 1])
c->frame.rect.height = h;
else
2006-01-26 17:24:34 +03:00
c->frame.rect.height =
col->rect.height - c->frame.rect.y + col->rect.y;
2006-01-26 17:24:34 +03:00
resize_client(c, &c->frame.rect, 0);
}
}
2006-01-26 17:24:34 +03:00
void
2006-01-26 20:29:49 +03:00
arrange_page(Page *p)
{
2006-01-26 17:24:34 +03:00
size_t i;
for(i = 1; i < p->narea; i++)
arrange_column(p, p->area[i]);
}
2006-01-26 17:24:34 +03:00
void
attach_column(Client *c)
2005-11-18 18:54:58 +03:00
{
Page *p = page[sel];
Area *col = p->narea && (p->sel > 0) ? p->area[p->sel] : nil;
2006-01-26 17:24:34 +03:00
if(!col) {
col = cext_emallocz(sizeof(Area));
col->rect = rect;
p->area = (Area **)cext_array_attach((void **)p->area, col,
sizeof(Area *), &p->areasz);
p->sel = p->narea;
p->narea++;
2005-12-21 18:18:11 +03:00
}
c->area = col;
col->client = (Client **)cext_array_attach((void **)col->client, c,
sizeof(Client *), &col->clientsz);
2006-01-26 20:29:49 +03:00
arrange_column(p, col);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-01-26 17:24:34 +03:00
update_column_width(Page *p)
{
2006-01-26 17:24:34 +03:00
size_t i;
unsigned int width;
if(p->narea == 1)
2006-01-26 17:24:34 +03:00
return;
width = rect.width / (p->narea - 1);
for(i = 1; i < p->narea; i++) {
p->area[i]->rect = rect;
p->area[i]->rect.x = i * width;
p->area[i]->rect.width = width;
2005-12-21 18:18:11 +03:00
}
2006-01-26 20:29:49 +03:00
arrange_page(p);
}
2006-01-26 17:24:34 +03:00
void
detach_column(Client *c)
2005-11-18 18:54:58 +03:00
{
2006-01-26 17:24:34 +03:00
Page *p = c->page;
Area *col = c->area;
2006-01-26 17:24:34 +03:00
cext_array_detach((void **)col->client, c, &col->clientsz);
2006-01-26 17:24:34 +03:00
if(!col->client[0]) {
cext_array_detach((void **)p->area, col, &p->areasz);
p->narea--;
2006-01-26 17:24:34 +03:00
free(col);
update_column_width(p);
}
2006-01-26 17:24:34 +03:00
else
2006-01-26 20:29:49 +03:00
arrange_column(p, col);
2006-01-26 17:24:34 +03:00
}
2005-11-18 18:54:58 +03:00
2005-12-21 18:18:11 +03:00
static void
match_horiz(Area *col, XRectangle *r)
2005-11-18 18:54:58 +03:00
{
2006-01-26 17:24:34 +03:00
size_t i;
for(i = 0; (i < col->clientsz) && col->client[i]; i++) {
Client *c = col->client[i];
c->frame.rect.x = r->x;
c->frame.rect.width = r->width;
resize_client(c, &c->frame.rect, nil);
2005-12-21 18:18:11 +03:00
}
2005-12-10 18:38:43 +03:00
}
2005-11-18 18:54:58 +03:00
2005-12-21 18:18:11 +03:00
static void
2006-01-26 17:24:34 +03:00
drop_resize(Client *c, XRectangle *new)
2005-12-10 18:38:43 +03:00
{
2006-01-26 17:24:34 +03:00
Page *p = c->page;
Area *west = nil, *east = nil, *col = c->area;
2006-01-26 17:24:34 +03:00
Client *north = nil, *south = nil;
size_t i;
2005-12-21 18:18:11 +03:00
for(i = 0; (i < p->narea) && (p->area[i] != col); i++);
west = i ? p->area[i - 1] : nil;
east = (i < p->areasz) && p->area[i + 1] ? p->area[i + 1] : nil;
2006-01-26 17:24:34 +03:00
for(i = 0; (i < col->clientsz) && col->client[i] && (col->client[i] != c); i++);
north = i ? col->client[i - 1] : nil;
south = (i < col->clientsz) && col->client[i + 1] ? col->client[i + 1] : nil;
2005-12-21 18:18:11 +03:00
/* horizontal resize */
2006-01-26 17:24:34 +03:00
if(west && (new->x != c->frame.rect.x)) {
2005-12-21 18:18:11 +03:00
west->rect.width = new->x - west->rect.x;
2006-01-26 17:24:34 +03:00
col->rect.width += c->frame.rect.x - new->x;
col->rect.x = new->x;
match_horiz(west, &west->rect);
match_horiz(col, &col->rect);
2005-12-21 18:18:11 +03:00
}
2006-01-26 17:24:34 +03:00
if(east && (new->x + new->width != c->frame.rect.x + c->frame.rect.width)) {
2005-12-21 18:18:11 +03:00
east->rect.width -= new->x + new->width - east->rect.x;
east->rect.x = new->x + new->width;
2006-01-26 17:24:34 +03:00
col->rect.x = new->x;
col->rect.width = new->width;
match_horiz(col, &col->rect);
match_horiz(east, &east->rect);
2005-12-21 18:18:11 +03:00
}
/* vertical resize */
2006-01-26 17:24:34 +03:00
if(north && (new->y != c->frame.rect.y)) {
north->frame.rect.height = new->y - north->frame.rect.y;
c->frame.rect.height += c->frame.rect.y - new->y;
c->frame.rect.y = new->y;
resize_client(north, &north->frame.rect, nil);
resize_client(c, &c->frame.rect, nil);
2005-12-21 18:18:11 +03:00
}
2006-01-26 17:24:34 +03:00
if(south && (new->y + new->height != c->frame.rect.y + c->frame.rect.height)) {
south->frame.rect.height -= new->y + new->height - south->frame.rect.y;
south->frame.rect.y = new->y + new->height;
c->frame.rect.y = new->y;
c->frame.rect.height = new->height;
resize_client(c, &c->frame.rect, nil);
resize_client(south, &south->frame.rect, nil);
2005-12-21 18:18:11 +03:00
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-01-26 17:24:34 +03:00
drop_moving(Client *c, XRectangle *new, XPoint * pt)
2005-11-18 18:54:58 +03:00
{
2006-01-26 17:24:34 +03:00
Page *p = c->page;
Area *tgt = nil, *src = c->area;
2006-01-26 17:24:34 +03:00
size_t i;
2005-12-21 18:18:11 +03:00
if(!pt)
return;
for(i = 0; (i < p->areasz) && p->area[i] &&
!blitz_ispointinrect(pt->x, pt->y, &p->area[i]->rect); i++);
tgt = (i < p->areasz) ? p->area[i] : nil;
2005-12-21 18:18:11 +03:00
if(tgt) {
if(tgt != src) {
2006-01-26 17:24:34 +03:00
if(src->clientsz <= 1 || !src->client[1])
return;
cext_array_detach((void **)src->client, c, &src->clientsz);
tgt->client = (Client **)cext_array_attach((void **)tgt->client, c,
sizeof(Client *), &tgt->clientsz);
2006-01-26 20:29:49 +03:00
arrange_column(p, src);
arrange_column(p, tgt);
2005-12-21 18:18:11 +03:00
} else {
2006-01-26 17:24:34 +03:00
for(i = 0; (i < src->clientsz) && src->client[i] &&
!blitz_ispointinrect(pt->x, pt->y, &src->client[i]->frame.rect); i++);
if((i < src->clientsz) && src->client[i] && (c != src->client[i])) {
size_t j;
for(j = 0; (j < src->clientsz) && src->client[j] && (src->client[j] != c); j++);
src->client[j] = src->client[i];
src->client[i] = c;
2006-01-26 20:29:49 +03:00
arrange_column(p, src);
2005-12-21 18:18:11 +03:00
}
}
2006-01-26 17:24:34 +03:00
focus_client(c);
2005-12-21 18:18:11 +03:00
}
2005-11-18 18:54:58 +03:00
}
2006-01-26 17:24:34 +03:00
void
resize_column(Client *c, XRectangle *r, XPoint *pt)
2005-11-18 18:54:58 +03:00
{
2006-01-26 17:24:34 +03:00
if((c->frame.rect.width == r->width)
&& (c->frame.rect.height == r->height))
drop_moving(c, r, pt);
2005-12-21 18:18:11 +03:00
else
2006-01-26 17:24:34 +03:00
drop_resize(c, r);
2005-11-18 18:54:58 +03:00
}
2005-12-09 04:46:59 +03:00
2006-01-26 20:29:49 +03:00
void
select_column(Client *c, char *arg)
{
2006-01-26 20:29:49 +03:00
Page *p = c->page;
Area *col = c->area;
2006-01-26 20:29:49 +03:00
size_t i;
2006-01-26 20:29:49 +03:00
for(i = 0; (i < col->clientsz) && col->client[i] && (col->client[i] != c); i++);
if(!strncmp(arg, "prev", 5)) {
if(!i)
for(i = 0; (i < col->clientsz) && col->client[i]; i++);
focus_client(col->client[i - 1]);
return;
} else if(!strncmp(arg, "next", 5)) {
if(col->client[i + 1])
focus_client(col->client[i + 1]);
else
focus_client(col->client[0]);
return;
}
2006-01-26 20:29:49 +03:00
for(i = 0; (i < p->areasz) && p->area[i] && (p->area[i] != col); i++);
2006-01-26 20:29:49 +03:00
if(!strncmp(arg, "west", 5)) {
if(!i)
for(i = 0; (i < p->areasz) && p->area[i]; i++);
col = p->area[i - 1];
2006-01-26 20:29:49 +03:00
} else if(!strncmp(arg, "east", 5)) {
if(p->area[i + 1])
col = p->area[i + 1];
2006-01-26 20:29:49 +03:00
else
col = p->area[0];
2006-01-26 20:29:49 +03:00
} else {
const char *errstr;
for(i = 0; (i < p->areasz) && p->area[i]; i++);
2006-01-26 20:29:49 +03:00
i = cext_strtonum(arg, 0, i - 1, &errstr);
if(errstr)
return;
col = p->area[i];
}
2006-01-26 20:29:49 +03:00
focus_client(col->client[col->sel]);
}
2006-01-26 20:29:49 +03:00
void
new_column(Page *p)
{
2006-01-26 20:29:49 +03:00
Client *c = sel_client_of_page(p);
Area *col, *old = c ? c->area : nil;
2005-12-16 19:18:00 +03:00
if(!old || old->nclient < 2)
2006-01-26 20:29:49 +03:00
return;
2005-12-16 19:18:00 +03:00
col = cext_emallocz(sizeof(Area));
col->rect = rect;
p->area = (Area **)cext_array_attach((void **)p->area, col,
sizeof(Area *), &p->areasz);
p->sel = p->narea;
p->narea++;
cext_array_detach((void **)old->client, c, &old->clientsz);
col->client = (Client **)cext_array_attach((void **)col->client, c,
sizeof(Client *), &col->clientsz);
c->area = col;
2006-01-26 20:29:49 +03:00
update_column_width(p);
focus_client(c);
}