wmii/cmd/wm/bar.c

164 lines
3.3 KiB
C
Raw Normal View History

/*
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <string.h>
2006-03-09 04:15:43 +03:00
#include <stdlib.h>
#include "wm.h"
2006-06-08 12:54:19 +04:00
Bar *free_bars = nil;
2006-04-12 12:44:07 +04:00
Bar *
create_bar(Bar **b_link, char *name)
{
static unsigned int id = 1;
2006-06-08 12:54:19 +04:00
Bar **i, *b = bar_of_name(name);;
if(b)
return b;
2006-06-08 12:54:19 +04:00
if(free_bars) {
b = free_bars;
free_bars = b->next;
}
else
b = cext_emallocz(sizeof(Bar));
b->id = id++;
cext_strlcpy(b->name, name, sizeof(b->name));
2006-06-22 11:38:42 +04:00
b->widget = blitz_create_input(barpmap, bargc, &def.font);
b->widget->color = def.normcolor;
b->widget->align = CENTER;
2006-06-08 12:54:19 +04:00
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
destroy_bar(Bar **b_link, Bar *b)
{
Bar **p;
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;
2006-06-22 11:38:42 +04:00
if(b->widget->text)
free(b->widget->text);
blitz_destroy_input(b->widget);
2006-06-08 12:54:19 +04:00
free_bars = b;
}
unsigned int
2006-04-12 12:44:07 +04:00
height_of_bar()
{
enum { BAR_PADDING = 4 };
return def.font.ascent + def.font.descent + BAR_PADDING;
}
void
2006-04-12 12:44:07 +04:00
resize_bar()
{
2006-06-08 12:54:19 +04:00
View *v;
Area *a;
Frame *f;
brect = rect;
2006-04-12 12:44:07 +04:00
brect.height = height_of_bar();
brect.y = rect.height - brect.height;
XMoveResizeWindow(dpy, barwin, brect.x, brect.y, brect.width, brect.height);
XSync(dpy, False);
XFreePixmap(dpy, barpmap);
barpmap = XCreatePixmap(dpy, barwin, brect.width, brect.height,
DefaultDepth(dpy, screen));
XSync(dpy, False);
draw_bar();
2006-06-08 12:54:19 +04:00
for(v=view; v; v=v->next) {
for(a = v->area; a; a=a->next) {
a->rect.height = rect.height - brect.height;
arrange_column(a, False);
}
2006-06-08 12:54:19 +04:00
for(f=v->area->frame; f; f=f->anext) {
resize_client(f->client, &f->rect, False);
}
}
}
void
draw_bar()
{
unsigned int i = 0, w = 0, nb, size = 0;
Bar *b = nil, *prev = nil;
blitz_draw_tile(bartile);
if(!lbar && !rbar)
2006-05-29 12:08:29 +04:00
goto MapBar;
for(b=lbar, nb=2 ;nb; --nb && (b = rbar))
for(; b && (w < brect.width); b=b->next, size++) {
2006-06-22 11:38:42 +04:00
b->widget->rect.x = 0;
b->widget->rect.y = 0;
b->widget->rect.width = brect.height;
if(b->widget->text && strlen(b->widget->text))
b->widget->rect.width += blitz_textwidth(b->widget->font, b->widget->text);
b->widget->rect.height = brect.height;
w += b->widget->rect.width;
}
2006-06-08 12:54:19 +04:00
if(b) { /* give all bars same width */
/* finish counting */
for( ;nb; b = rbar, nb--)
for(; b; b=b->next, size++);
2006-06-08 12:54:19 +04:00
w = brect.width / size;
for(b = lbar, nb=2 ;nb; b = rbar, nb--) {
for(; b; b=b->next) {
2006-06-22 11:38:42 +04:00
b->widget->rect.x = i * w;
b->widget->rect.width = w;
}
}
}
else { /* expand rbar properly */
if(rbar)
2006-06-22 11:38:42 +04:00
rbar->widget->rect.width += (brect.width - w);
for(b=lbar, nb=2 ;nb--; b = rbar)
for(; b; prev = b, b=b->next)
2006-06-22 11:38:42 +04:00
if(prev) b->widget->rect.x = prev->widget->rect.x + prev->widget->rect.width;
}
for(b=lbar, nb=2 ;nb; b=rbar, nb--)
for(; b; b=b->next) {
if(b == rbar)
2006-06-22 11:38:42 +04:00
b->widget->align = EAST;
blitz_draw_input(b->widget);
}
2006-05-29 12:08:29 +04:00
MapBar:
XCopyArea(dpy, barpmap, barwin, bargc, 0, 0, brect.width, brect.height, 0, 0);
XSync(dpy, False);
}
2006-04-12 12:44:07 +04:00
Bar *
bar_of_name(const char *name)
2006-03-09 04:15:43 +03:00
{
static char buf[256];
2006-06-08 12:54:19 +04:00
Bar *b;
2006-03-09 04:15:43 +03:00
cext_strlcpy(buf, name, sizeof(buf));
2006-06-17 15:32:49 +04:00
for(b=lbar; b && strncmp(b->name, name, sizeof(b->name)); b=b->next);
2006-06-08 12:54:19 +04:00
return b;
}
Bar *
bar_of_id(unsigned short id)
{
Bar *b;
2006-06-17 15:32:49 +04:00
for(b=lbar; b && b->id != id; b=b->next);
2006-06-08 12:54:19 +04:00
return b;
2006-03-09 04:15:43 +03:00
}