wmii/bar.c

132 lines
3.1 KiB
C
Raw Normal View History

2006-10-12 18:10:57 +04:00
/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
2006-10-12 18:10:57 +04:00
#include "wm.h"
#include <math.h>
#include <string.h>
2006-03-09 04:15:43 +03:00
#include <stdlib.h>
2006-10-12 18:10:57 +04:00
Bar *free_bars = NULL;
2006-04-12 12:44:07 +04:00
Bar *
2006-10-12 18:10:57 +04:00
create_bar(Bar **b_link, char *name) {
static unsigned int id = 1;
Bar **i, *b = bar_of_name(*b_link, name);;
2006-10-12 18:10:57 +04:00
if(b)
return b;
2006-06-08 12:54:19 +04:00
if(free_bars) {
b = free_bars;
free_bars = b->next;
2006-07-06 01:40:55 +04:00
memset(b, 0, sizeof(*b));
2006-06-08 12:54:19 +04:00
}
else
2006-10-12 18:10:57 +04:00
b = ixp_emallocz(sizeof(Bar));
b->id = id++;
2006-10-12 18:10:57 +04:00
strncpy(b->name, name, sizeof(b->name));
b->brush = screen->bbrush;
b->brush.color = def.normcolor;
for(i=b_link; *i; i=&(*i)->next)
if(strcmp((*i)->name, name) >= 0)
2006-06-17 15:32:49 +04:00
break;
2006-06-08 12:54:19 +04:00
b->next = *i;
*i = b;
return b;
}
void
2006-10-12 18:10:57 +04:00
destroy_bar(Bar **b_link, Bar *b) {
Bar **p;
2006-10-12 18:10:57 +04:00
for(p=b_link; *p && *p != b; p=&(*p)->next);
*p = b->next;
2006-06-08 12:54:19 +04:00
b->next = free_bars;
free_bars = b;
}
void
2006-10-12 18:10:57 +04:00
resize_bar(WMScreen *s) {
2006-06-08 12:54:19 +04:00
View *v;
s->brect = s->rect;
2006-10-12 18:12:22 +04:00
s->brect.height = labelh(&def.font);
s->brect.y = s->rect.height - s->brect.height;
2006-07-03 20:41:14 +04:00
XMoveResizeWindow(blz.dpy, s->barwin, s->brect.x, s->brect.y, s->brect.width, s->brect.height);
XSync(blz.dpy, False);
draw_bar(s);
for(v=view; v; v=v->next)
arrange_view(v);
}
void
2006-10-12 18:10:57 +04:00
draw_bar(WMScreen *s) {
unsigned int width, tw, nb, size;
float shrink;
Bar *b, *tb, *largest, **pb;
2006-10-12 18:12:22 +04:00
draw_tile(&s->bbrush);
if(!s->lbar && !s->rbar)
2006-05-29 12:08:29 +04:00
goto MapBar;
2006-10-12 18:10:57 +04:00
largest = b = tb = NULL;
tw = width = nb = size = 0;
for(b=s->lbar, nb=2 ;nb; --nb && (b = s->rbar))
for(; b; b=b->next) {
b->brush.rect.x = b->brush.rect.y = 0;
b->brush.rect.width = def.font.height;
2006-06-22 13:46:39 +04:00
if(b->text && strlen(b->text))
2006-10-12 18:12:22 +04:00
b->brush.rect.width += textwidth(b->brush.font, b->text);
b->brush.rect.height = s->brect.height;
width += b->brush.rect.width;
}
/* Not enough room. Shrink bars until they all fit */
if(width > s->brect.width) {
for(b=s->lbar, nb=2 ;nb; --nb && (b = s->rbar))
for(; b; b=b->next) {
for(pb=&largest; *pb; pb=&(*pb)->smaller)
if((*pb)->brush.rect.width < b->brush.rect.width) break;
b->smaller = *pb;
*pb = b;
}
for(tb=largest; tb; tb=tb->smaller) {
width -= tb->brush.rect.width;
tw += tb->brush.rect.width;
shrink = (s->brect.width - width) / (float)tw;
if(tb->smaller)
if(tb->brush.rect.width * shrink >= tb->smaller->brush.rect.width)
break;
}
2006-06-30 07:09:47 +04:00
if(tb)
for(b=largest; b != tb->smaller; b=b->smaller)
b->brush.rect.width = floor(b->brush.rect.width * shrink);
width += tw * shrink;
2006-10-12 18:10:57 +04:00
tb=NULL;
}
for(b=s->lbar, nb=2 ;nb; b=s->rbar, nb--)
for(; b; tb = b, b=b->next) {
if(b == s->rbar) {
b->brush.align = EAST;
s->rbar->brush.rect.width += (s->brect.width - width);
}else
b->brush.align = CENTER;
if(tb)
b->brush.rect.x = tb->brush.rect.x + tb->brush.rect.width;
2006-10-12 18:12:22 +04:00
draw_label(&b->brush, b->text);
}
2006-05-29 12:08:29 +04:00
MapBar:
2006-07-03 20:41:14 +04:00
XCopyArea(blz.dpy, s->bbrush.drawable, s->barwin, s->bbrush.gc, 0, 0,
s->brect.width, s->brect.height, 0, 0);
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
}
2006-04-12 12:44:07 +04:00
Bar *
2006-10-12 18:10:57 +04:00
bar_of_name(Bar *b_link, const char *name) {
static char buf[256];
2006-06-08 12:54:19 +04:00
Bar *b;
2006-03-09 04:15:43 +03:00
2006-10-12 18:10:57 +04:00
strncpy(buf, name, sizeof(buf));
for(b=b_link; b; b=b->next)
if(!strncmp(b->name, name, sizeof(b->name))) break;
2006-06-08 12:54:19 +04:00
return b;
2006-03-09 04:15:43 +03:00
}