awesome/statusbar.c

303 lines
10 KiB
C
Raw Normal View History

/*
2007-10-17 10:53:32 +02:00
* statusbar.c - statusbar functions
*
* Copyright © 2007 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdio.h>
#include <math.h>
#include "layout.h"
#include "statusbar.h"
#include "draw.h"
2007-09-15 15:26:51 +02:00
#include "screen.h"
#include "util.h"
#include "tag.h"
#include "widget.h"
extern AwesomeConf globalconf;
void
statusbar_draw(int screen)
{
int phys_screen = get_phys_screen(screen);
VirtScreen vscreen;
Widget *widget, *last_drawn = NULL;
int left = 0, right = 0;
vscreen = globalconf.screens[screen];
/* don't waste our time */
if(vscreen.statusbar->position == BarOff)
return;
2007-12-30 18:13:33 +01:00
XFreePixmap(globalconf.display, vscreen.statusbar->drawable);
DrawCtx *ctx = draw_get_context(phys_screen,
vscreen.statusbar->width,
vscreen.statusbar->height);
2007-12-22 20:17:24 +01:00
draw_rectangle(ctx,
0,
0,
vscreen.statusbar->width,
vscreen.statusbar->height,
True,
vscreen.colors_normal[ColBG]);
for(widget = vscreen.statusbar->widgets; widget; widget = widget->next)
if (widget->alignment == AlignLeft)
2007-12-16 04:28:29 +01:00
left += widget->draw(widget, ctx, left, (left + right));
/* renders right widget from last to first */
for(widget = vscreen.statusbar->widgets; widget; widget = widget->next)
if (widget->alignment == AlignRight && last_drawn == widget->next)
{
2007-12-16 04:28:29 +01:00
right += widget->draw(widget, ctx, right, (left + right));
last_drawn = widget;
widget = vscreen.statusbar->widgets;
}
for(widget = vscreen.statusbar->widgets; widget; widget = widget->next)
if (widget->alignment == AlignFlex)
2007-12-16 04:28:29 +01:00
left += widget->draw(widget, ctx, left, (left + right));
if(vscreen.statusbar->position == BarRight
|| vscreen.statusbar->position == BarLeft)
{
Drawable d;
if(vscreen.statusbar->position == BarRight)
d = draw_rotate(ctx,
phys_screen,
M_PI_2,
vscreen.statusbar->height,
0);
2007-11-11 19:49:42 +01:00
else
d = draw_rotate(ctx,
phys_screen,
- M_PI_2,
0,
vscreen.statusbar->width);
vscreen.statusbar->drawable = d;
draw_free_context(ctx);
}
else
{
vscreen.statusbar->drawable = ctx->drawable;
/* just delete the struct, don't delete the drawable */
p_delete(&ctx);
}
statusbar_display(screen);
}
void
statusbar_display(int screen)
{
VirtScreen vscreen = globalconf.screens[screen];
int phys_screen = get_phys_screen(screen);
2007-12-30 14:23:01 +01:00
/* don't waste our time */
if(vscreen.statusbar->position == BarOff)
return;
if(vscreen.statusbar->position == BarRight
|| vscreen.statusbar->position == BarLeft)
XCopyArea(globalconf.display, vscreen.statusbar->drawable,
vscreen.statusbar->window,
DefaultGC(globalconf.display, phys_screen), 0, 0,
vscreen.statusbar->height,
vscreen.statusbar->width, 0, 0);
else
XCopyArea(globalconf.display, vscreen.statusbar->drawable,
vscreen.statusbar->window,
DefaultGC(globalconf.display, phys_screen), 0, 0,
vscreen.statusbar->width, vscreen.statusbar->height, 0, 0);
}
2007-09-15 15:26:51 +02:00
2007-09-15 22:25:49 +02:00
void
2007-12-19 05:16:49 +01:00
statusbar_init(int screen)
2007-09-15 22:25:49 +02:00
{
Widget *widget;
2007-09-15 22:25:49 +02:00
XSetWindowAttributes wa;
int phys_screen = get_phys_screen(screen);
Area area = get_screen_area(screen,
NULL,
&globalconf.screens[screen].padding);
2007-12-19 05:16:49 +01:00
Statusbar *statusbar = globalconf.screens[screen].statusbar;
2007-09-15 22:25:49 +02:00
2007-12-30 18:54:17 +01:00
if(statusbar->height <= 0)
{
/* 1.5 as default factor, it fits nice but no one know why */
statusbar->height = globalconf.screens[screen].font->height * 1.5;
2007-12-30 18:54:17 +01:00
for(widget = statusbar->widgets; widget; widget = widget->next)
if(widget->font)
statusbar->height = MAX(statusbar->height, widget->font->height * 1.5);
}
2007-12-30 18:55:46 +01:00
if(statusbar->width <= 0)
{
if(statusbar->position == BarRight || statusbar->position == BarLeft)
statusbar->width = area.height;
else
statusbar->width = area.width;
}
statusbar->screen = screen;
2007-09-15 22:25:49 +02:00
wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
| EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
2007-12-19 05:16:49 +01:00
wa.cursor = globalconf.cursor[CurNormal];
2007-09-15 22:25:49 +02:00
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
wa.event_mask = ButtonPressMask | ExposureMask;
if(statusbar->dposition == BarRight || statusbar->dposition == BarLeft)
2007-12-19 05:16:49 +01:00
statusbar->window = XCreateWindow(globalconf.display,
RootWindow(globalconf.display,
phys_screen),
0, 0,
statusbar->height,
statusbar->width,
2007-12-19 05:16:49 +01:00
0,
DefaultDepth(globalconf.display,
phys_screen),
CopyFromParent,
DefaultVisual(globalconf.display,
phys_screen),
CWOverrideRedirect |
CWBackPixmap |
CWEventMask,
&wa);
else
2007-12-19 05:16:49 +01:00
statusbar->window = XCreateWindow(globalconf.display,
RootWindow(globalconf.display,
phys_screen),
0, 0,
statusbar->width,
statusbar->height,
2007-12-19 05:16:49 +01:00
0,
DefaultDepth(globalconf.display,
phys_screen),
CopyFromParent,
DefaultVisual(globalconf.display,
phys_screen),
CWOverrideRedirect |
CWBackPixmap |
CWEventMask,
&wa);
statusbar->drawable = XCreatePixmap(globalconf.display,
RootWindow(globalconf.display, phys_screen),
statusbar->width, statusbar->height,
DefaultDepth(globalconf.display, phys_screen));
2007-12-19 05:16:49 +01:00
XDefineCursor(globalconf.display,
statusbar->window,
globalconf.cursor[CurNormal]);
2007-12-22 20:55:17 +01:00
widget_calculate_alignments(statusbar->widgets);
statusbar_update_position(screen);
2007-12-19 05:16:49 +01:00
XMapRaised(globalconf.display, statusbar->window);
2007-09-15 22:25:49 +02:00
}
2007-09-15 15:26:51 +02:00
void
statusbar_update_position(int screen)
2007-09-15 15:26:51 +02:00
{
XEvent ev;
Statusbar *statusbar = globalconf.screens[screen].statusbar;
Area area = get_screen_area(statusbar->screen,
NULL,
&globalconf.screens[screen].padding);
2007-09-15 15:26:51 +02:00
XMapRaised(globalconf.display, statusbar->window);
switch (statusbar->position)
2007-09-15 15:26:51 +02:00
{
default:
XMoveWindow(globalconf.display,
statusbar->window,
area.x,
area.y);
2007-09-15 15:26:51 +02:00
break;
case BarRight:
XMoveWindow(globalconf.display,
statusbar->window,
area.x + (area.width - statusbar->height),
area.y);
break;
2007-09-15 15:26:51 +02:00
case BarBot:
XMoveWindow(globalconf.display,
statusbar->window,
area.x,
area.height - statusbar->height);
2007-09-15 15:26:51 +02:00
break;
case BarOff:
XUnmapWindow(globalconf.display, statusbar->window);
2007-09-15 15:26:51 +02:00
break;
}
XSync(globalconf.display, False);
while(XCheckMaskEvent(globalconf.display, EnterWindowMask, &ev));
2007-09-15 15:26:51 +02:00
}
2007-09-15 15:34:28 +02:00
int
2007-12-14 20:10:52 +01:00
statusbar_get_position_from_str(const char * pos)
{
if(!a_strncmp(pos, "off", 3))
return BarOff;
else if(!a_strncmp(pos, "bottom", 6))
return BarBot;
else if(!a_strncmp(pos, "right", 5))
return BarRight;
else if(!a_strncmp(pos, "left", 4))
return BarLeft;
return BarTop;
}
/** Toggle statusbar
* \param screen Screen ID
* \param arg Unused
* \ingroup ui_callback
*/
2007-09-15 15:34:28 +02:00
void
uicb_statusbar_toggle(int screen, char *arg __attribute__ ((unused)))
2007-09-15 15:34:28 +02:00
{
if(globalconf.screens[screen].statusbar->position == BarOff)
globalconf.screens[screen].statusbar->position = (globalconf.screens[screen].statusbar->dposition == BarOff) ? BarTop : globalconf.screens[screen].statusbar->dposition;
2007-09-15 15:34:28 +02:00
else
globalconf.screens[screen].statusbar->position = BarOff;
statusbar_update_position(screen);
arrange(screen);
2007-09-15 15:34:28 +02:00
}
/** Set statusbar position
* \param screen Screen ID
* \param arg off | bottom | right | left | top
* \ingroup ui_callback
*/
void
uicb_statusbar_set_position(int screen, char *arg)
{
globalconf.screens[screen].statusbar->dposition =
globalconf.screens[screen].statusbar->position =
2007-12-14 20:10:52 +01:00
statusbar_get_position_from_str(arg);
statusbar_update_position(screen);
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80