wmii/cmd/wm/layout_column.c

629 lines
15 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "wm.h"
#include "layoutdef.h"
2005-11-18 18:54:58 +03:00
typedef struct Acme Acme;
typedef struct Column Column;
typedef struct Cell Cell;
2005-11-18 18:54:58 +03:00
struct Acme {
2005-12-21 18:18:11 +03:00
Column *sel;
Column *columns;
size_t ncolumns;
Frame *frames;
size_t nframes;
};
struct Cell {
2005-12-21 18:18:11 +03:00
Frame *frame;
Cell *next;
Cell *prev;
Column *col;
2005-11-18 18:54:58 +03:00
};
struct Column {
2005-12-21 18:18:11 +03:00
Cell *sel;
Cell *cells;
size_t ncells;
Bool exclusive;
2005-12-21 18:18:11 +03:00
XRectangle rect;
Column *prev;
Column *next;
2005-11-18 18:54:58 +03:00
};
2005-12-21 18:18:11 +03:00
static void init_col(Area * a, Client * clients);
static Client *deinit_col(Area * a);
static void arrange_col(Area * a);
static Bool attach_col(Area * a, Client * c);
static void detach_col(Area * a, Client * c, Bool unmap);
static void resize_col(Frame * f, XRectangle * new, XPoint * pt);
static void focus_col(Frame * f, Bool raise);
static Frame *frames_col(Area * a);
static Frame *sel_col(Area * a);
static Action *actions_col(Area * a);
2005-12-11 01:43:45 +03:00
2005-12-11 02:03:17 +03:00
static void select_frame(void *obj, char *arg);
static void swap_frame(void *obj, char *arg);
static void new_col(void *obj, char *arg);
static void toggle_excl_col(void *obj, char *arg);
2005-12-11 02:03:17 +03:00
2005-12-11 01:43:45 +03:00
static Action lcol_acttbl[] = {
2005-12-21 18:18:11 +03:00
{"select", select_frame},
{"swap", swap_frame},
{"new", new_col},
{"toggle", toggle_excl_col},
2005-12-21 18:18:11 +03:00
{0, 0}
2005-12-11 01:43:45 +03:00
};
2005-12-21 18:18:11 +03:00
void
init_layout_column()
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Layout *lp, *l = cext_emallocz(sizeof(Layout));
l->name = "column";
l->init = init_col;
l->deinit = deinit_col;
l->arrange = arrange_col;
l->attach = attach_col;
l->detach = detach_col;
l->resize = resize_col;
l->focus = focus_col;
l->frames = frames_col;
l->sel = sel_col;
l->actions = actions_col;
for(lp = layouts; lp && lp->next; lp = lp->next);
if(lp)
lp->next = l;
else
layouts = l;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
arrange_column(Column * col)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Cell *cell;
unsigned int i = 0, h = area_rect.height / col->ncells;
for(cell = col->cells; cell; cell = cell->next) {
Frame *f = cell->frame;
f->rect = col->rect;
f->rect.y = area_rect.y + i * h;
if(!cell->next)
f->rect.height = area_rect.height - f->rect.y + area_rect.y;
else
f->rect.height = h;
resize_frame(f, &f->rect, 0);
i++;
}
}
2005-11-18 18:54:58 +03:00
2005-12-21 18:18:11 +03:00
static void
arrange_col(Area * a)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Column *col;
for(col = acme->columns; col; col = col->next)
arrange_column(col);
XSync(dpy, False);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
init_col(Area * a, Client * clients)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Client *n, *c = clients;
a->aux = cext_emallocz(sizeof(Acme));
while(c) {
n = c->next;
attach_col(a, c);
c = n;
}
}
2005-11-18 18:54:58 +03:00
2005-12-21 18:18:11 +03:00
static void
attach_frame(Area * a, Column * col, Frame * new)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Cell *c, *cell = cext_emallocz(sizeof(Cell));
Frame *f;
cell->frame = new;
cell->col = col;
new->aux = cell;
for(c = col->cells; c && c->next; c = c->next);
if(!c)
col->cells = cell;
else {
c->next = cell;
cell->prev = c;
}
col->sel = cell;
col->ncells++;
for(f = acme->frames; f && f->next; f = f->next);
if(!f)
acme->frames = new;
else {
f->next = new;
new->prev = f;
}
attach_frame_to_area(a, new);
acme->nframes++;
}
2005-12-21 18:18:11 +03:00
static void
detach_frame(Area * a, Frame * old)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Cell *cell = old->aux;
Column *col = cell->col;
if(col->sel == cell) {
if(cell->prev)
col->sel = cell->prev;
else
col->sel = nil;
}
if(cell->prev)
cell->prev->next = cell->next;
else
col->cells = cell->next;
if(cell->next)
cell->next->prev = cell->prev;
if(!col->sel)
col->sel = col->cells;
free(cell);
col->ncells--;
if(old->prev)
old->prev->next = old->next;
else
acme->frames = old->next;
if(old->next)
old->next->prev = old->prev;
old->aux = nil;
detach_frame_from_area(old);
acme->nframes--;
}
2005-12-21 18:18:11 +03:00
static Client *
deinit_col(Area * a)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Frame *f;
Client *cl, *res = nil, *c = nil;
Column *col = acme->columns;
while((f = acme->frames)) {
while((cl = f->clients)) {
detach_client_from_frame(cl, False);
cl->prev = cl->next = 0;
if(!c)
res = c = cl;
else {
c->next = cl;
cl->prev = c;
c = cl;
}
}
detach_frame(a, f);
destroy_frame(f);
}
while((col = acme->columns)) {
acme->columns = col->next;
free(col);
}
free(acme);
a->aux = 0;
return res;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static Bool
attach_col(Area * a, Client * c)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Column *col = acme->sel;
Frame *f = nil;
if(!col) {
col = cext_emallocz(sizeof(Column));
col->exclusive = True; /* first col is exclusive */
2005-12-21 18:18:11 +03:00
col->rect = area_rect;
acme->columns = acme->sel = col;
acme->ncolumns++;
}
else if(col->exclusive && col->ncells && col->next) {
f = col->sel->frame;
acme->sel = col = col->next;
draw_frame(f);
}
2005-12-21 18:18:11 +03:00
f = alloc_frame(&c->rect);
attach_frame(a, col, f);
2005-12-21 18:18:11 +03:00
attach_client_to_frame(f, c);
arrange_column(col);
if(a->page == selpage)
XMapWindow(dpy, f->win);
focus_col(f, True);
if(col->exclusive && col->ncells > 1)
new_col(a, nil);
2005-12-21 18:18:11 +03:00
return True;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
update_column_width(Area * a)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
unsigned int i = 0, width;
Column *col;
if(!acme->ncolumns)
return;
width = area_rect.width / acme->ncolumns;
for(col = acme->columns; col; col = col->next) {
col->rect = area_rect;
col->rect.x = i * width;
col->rect.width = width;
i++;
}
arrange_col(a);
}
2005-12-21 18:18:11 +03:00
static void
detach_col(Area * a, Client * c, Bool unmap)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
Frame *f = c->frame;
Cell *cell = f->aux;
Column *col = cell->col;
detach_client_from_frame(c, unmap);
if(!f->clients) {
detach_frame(a, f);
destroy_frame(f);
} else
return;
if(col->cells)
arrange_column(col);
else {
if(acme->sel == col) {
if(col->prev)
acme->sel = col->prev;
else
acme->sel = nil;
}
if(col->prev)
col->prev->next = col->next;
else
acme->columns = col->next;
if(col->next)
col->next->prev = col->prev;
if(!acme->sel)
acme->sel = acme->columns;
acme->ncolumns--;
free(col);
update_column_width(a);
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
match_frame_horiz(Column * col, XRectangle * r)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Cell *cell;
for(cell = col->cells; cell; cell = cell->next) {
Frame *f = cell->frame;
f->rect.x = r->x;
f->rect.width = r->width;
resize_frame(f, &f->rect, nil);
}
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
drop_resize(Frame * f, XRectangle * new)
2005-12-10 18:38:43 +03:00
{
2005-12-21 18:18:11 +03:00
Column *west = nil, *east = nil, *col = nil;
Cell *north = nil, *south = nil;
Cell *cell = f->aux;
col = cell->col;
west = col->prev;
east = col->next;
north = cell->prev;
south = cell->next;
/* horizontal resize */
if(west && (new->x != f->rect.x)) {
west->rect.width = new->x - west->rect.x;
col->rect.width += f->rect.x - new->x;
col->rect.x = new->x;
match_frame_horiz(west, &west->rect);
match_frame_horiz(col, &col->rect);
}
if(east && (new->x + new->width != f->rect.x + f->rect.width)) {
east->rect.width -= new->x + new->width - east->rect.x;
east->rect.x = new->x + new->width;
col->rect.x = new->x;
col->rect.width = new->width;
match_frame_horiz(col, &col->rect);
match_frame_horiz(east, &east->rect);
}
/* vertical resize */
if(north && (new->y != f->rect.y)) {
north->frame->rect.height = new->y - north->frame->rect.y;
f->rect.height += f->rect.y - new->y;
f->rect.y = new->y;
resize_frame(north->frame, &north->frame->rect, nil);
resize_frame(f, &f->rect, nil);
}
if(south && (new->y + new->height != f->rect.y + f->rect.height)) {
south->frame->rect.height -=
new->y + new->height - south->frame->rect.y;
south->frame->rect.y = new->y + new->height;
f->rect.y = new->y;
f->rect.height = new->height;
resize_frame(f, &f->rect, nil);
resize_frame(south->frame, &south->frame->rect, nil);
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
drop_moving(Frame * f, XRectangle * new, XPoint * pt)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
Area *a = f->area;
Cell *cell = f->aux;
Column *src = cell->col, *tgt = 0;
Acme *acme = a->aux;
if(!pt)
return;
for(tgt = acme->columns;
tgt && !blitz_ispointinrect(pt->x, pt->y, &tgt->rect);
tgt = tgt->next);
if(tgt) {
if(tgt != src) {
if(src->ncells < 2)
return;
detach_frame(a, f);
attach_frame(a, tgt, f);
arrange_column(src);
arrange_column(tgt);
focus_col(f, True);
} else {
Cell *c;
for(c = src->cells;
c && !blitz_ispointinrect(pt->x, pt->y, &c->frame->rect);
c = c->next);
if(c && c != cell) {
Frame *tmp = c->frame;
c->frame = f;
cell->frame = tmp;
arrange_column(src);
focus_col(f, True);
}
}
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
resize_col(Frame * f, XRectangle * new, XPoint * pt)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
if((f->rect.width == new->width)
&& (f->rect.height == new->height))
drop_moving(f, new, pt);
else
drop_resize(f, new);
2005-11-18 18:54:58 +03:00
}
2005-12-09 04:46:59 +03:00
2005-12-21 18:18:11 +03:00
static void
focus_col(Frame * f, Bool raise)
2005-12-09 04:46:59 +03:00
{
2005-12-21 18:18:11 +03:00
Area *a = f->area;
Acme *acme = a->aux;
2006-01-04 18:55:11 +03:00
Frame *old = sel_col(a);
Cell *cell = f->aux;
2006-01-11 19:07:33 +03:00
Client *c = f->sel ? f->sel : f->clients;
2005-12-21 18:18:11 +03:00
2006-01-04 18:55:11 +03:00
acme->sel = cell->col;
cell->col->sel = cell;
2005-12-21 18:18:11 +03:00
a->file[A_SEL_FRAME]->content = f->file[F_PREFIX]->content;
2006-01-11 19:07:33 +03:00
if(raise && c)
XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
c->rect.width / 2, c->rect.height / 2);
focus_client(c);
if(old && old != f)
draw_frame(old);
2005-12-21 18:18:11 +03:00
draw_frame(f);
2005-12-09 04:46:59 +03:00
}
2005-12-21 18:18:11 +03:00
static Frame *
frames_col(Area * a)
{
2005-12-21 18:18:11 +03:00
Acme *acme = a->aux;
return acme->frames;
}
2005-12-21 18:18:11 +03:00
static Frame *
sel_col(Area * a)
{
Acme *acme = a->aux;
Column *col = acme->sel;
2005-12-21 18:18:11 +03:00
if(col && col->sel)
return col->sel->frame;
return nil;
}
2005-12-21 18:18:11 +03:00
static Action *
actions_col(Area * a)
2005-12-11 01:43:45 +03:00
{
2005-12-21 18:18:11 +03:00
return lcol_acttbl;
}
2005-12-11 02:03:17 +03:00
2005-12-21 18:18:11 +03:00
static void
select_frame(void *obj, char *arg)
2005-12-11 02:03:17 +03:00
{
2005-12-21 18:18:11 +03:00
Area *a = obj;
Acme *acme = a->aux;
Column *c, *col = acme->sel;
Cell *cell;
if(!col)
return;
cell = col->sel;
if(!cell || !arg)
return;
if(!strncmp(arg, "prev", 5)) {
if(cell->prev)
cell = cell->prev;
else
for(cell = col->cells; cell && cell->next; cell = cell->next);
} else if(!strncmp(arg, "next", 5)) {
if(cell->next)
cell = cell->next;
else
cell = col->cells;
} else if(!strncmp(arg, "west", 5)) {
if(col->prev)
cell = col->prev->sel;
else
for(c = acme->columns; c && c->next; c = c->next);
} else if(!strncmp(arg, "east", 5)) {
if(col->next)
cell = col->next->sel;
else
cell = acme->columns->sel;
} else {
unsigned int i = 0, idx = blitz_strtonum(arg, 0, col->ncells - 1);
for(cell = col->cells; cell && i != idx; cell = cell->next)
i++;
}
if(cell)
focus_col(cell->frame, True);
2005-12-11 02:03:17 +03:00
}
2005-12-21 18:18:11 +03:00
static void
swap_frame(void *obj, char *arg)
{
2005-12-21 18:18:11 +03:00
Area *a = obj;
Acme *acme = a->aux;
Column *west = nil, *east = nil, *col = acme->sel;
Cell *north = nil, *south = nil, *cell = col->sel;
Frame *f;
XRectangle r;
if(!col || !cell || !arg)
return;
west = col->prev;
east = col->next;
north = cell->prev;
south = cell->next;
if(!strncmp(arg, "north", 6) && north) {
r = north->frame->rect;
north->frame->rect = cell->frame->rect;
cell->frame->rect = r;
f = north->frame;
north->frame = cell->frame;
cell->frame = f;
cell->frame->aux = cell;
north->frame->aux = north;
resize_frame(cell->frame, &cell->frame->rect, nil);
resize_frame(north->frame, &north->frame->rect, nil);
focus_col(cell->frame, True);
} else if(!strncmp(arg, "south", 6) && south) {
r = south->frame->rect;
south->frame->rect = cell->frame->rect;
cell->frame->rect = r;
f = south->frame;
south->frame = cell->frame;
cell->frame = f;
cell->frame->aux = cell;
south->frame->aux = south;
resize_frame(cell->frame, &cell->frame->rect, nil);
resize_frame(south->frame, &south->frame->rect, nil);
focus_col(cell->frame, True);
} else if(!strncmp(arg, "west", 5) && west && (col->ncells > 1)) {
Cell *other = west->sel;
r = other->frame->rect;
other->frame->rect = cell->frame->rect;
cell->frame->rect = r;
f = other->frame;
other->frame = cell->frame;
cell->frame = f;
other->frame->aux = other;
cell->frame->aux = cell;
resize_frame(cell->frame, &cell->frame->rect, nil);
resize_frame(other->frame, &other->frame->rect, nil);
focus_col(other->frame, True);
2005-12-21 18:18:11 +03:00
} else if(!strncmp(arg, "east", 5) && east && (col->ncells > 1)) {
Cell *other = east->sel;
r = other->frame->rect;
other->frame->rect = cell->frame->rect;
cell->frame->rect = r;
f = other->frame;
other->frame = cell->frame;
cell->frame = f;
other->frame->aux = other;
cell->frame->aux = cell;
resize_frame(cell->frame, &cell->frame->rect, nil);
resize_frame(other->frame, &other->frame->rect, nil);
focus_col(cell->frame, True);
}
}
2005-12-21 18:18:11 +03:00
static void
new_col(void *obj, char *arg)
{
2005-12-21 18:18:11 +03:00
Area *a = obj;
Acme *acme = a->aux;
Column *c, *new, *col = acme->sel;
Frame *f;
2005-12-21 18:18:11 +03:00
if(!col || col->ncells < 2)
return;
2005-12-21 18:18:11 +03:00
f = col->sel->frame;
for(c = acme->columns; c && c->next; c = c->next);
2005-12-16 19:18:00 +03:00
2005-12-21 18:18:11 +03:00
new = cext_emallocz(sizeof(Column));
new->rect = area_rect;
new->prev = c;
c->next = new;
acme->ncolumns++;
acme->sel = new;
2005-12-16 19:18:00 +03:00
2005-12-21 18:18:11 +03:00
detach_frame(a, f);
attach_frame(a, new, f);
2005-12-16 19:18:00 +03:00
2005-12-21 18:18:11 +03:00
update_column_width(a);
focus_col(f, True);
}
static void
toggle_excl_col(void *obj, char *arg)
{
Area *a = obj;
Acme *acme = a->aux;
Column *col = acme->sel;
if(!col)
return;
if(!strncmp(arg, "0", 1))
col->exclusive = False;
else if(!strncmp(arg, "1", 1))
col->exclusive = True;
}