awesome/statusbar.c

309 lines
8.9 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 "statusbar.h"
2007-09-15 15:26:51 +02:00
#include "screen.h"
#include "tag.h"
#include "widget.h"
#include "window.h"
extern AwesomeConf globalconf;
static void
statusbar_update_position(Statusbar *statusbar)
{
Area area;
XMapRaised(globalconf.display, statusbar->sw->window);
/* Top and Bottom Statusbar have prio */
if(statusbar->position == Top || statusbar->position == Bottom)
2008-01-29 19:06:44 +01:00
area = screen_get_area(statusbar->screen,
NULL,
&globalconf.screens[statusbar->screen].padding);
else
2008-01-29 19:06:44 +01:00
area = screen_get_area(statusbar->screen,
globalconf.screens[statusbar->screen].statusbar,
&globalconf.screens[statusbar->screen].padding);
switch(statusbar->position)
{
case Top:
simplewindow_move(statusbar->sw, area.x, area.y);
break;
case Bottom:
2008-01-23 17:18:49 +01:00
simplewindow_move(statusbar->sw, area.x, (area.y + area.height) - statusbar->sw->geometry.height);
break;
case Left:
2008-01-24 23:25:12 +01:00
simplewindow_move(statusbar->sw, area.x - statusbar->sw->geometry.width,
(area.y + area.height) - statusbar->sw->geometry.height);
break;
case Right:
simplewindow_move(statusbar->sw, area.x + area.width, area.y);
break;
case Off:
XUnmapWindow(globalconf.display, statusbar->sw->window);
break;
}
}
2008-01-12 21:50:14 +01:00
static void
2007-12-30 21:00:34 +01:00
statusbar_draw(Statusbar *statusbar)
{
2007-12-30 21:00:34 +01:00
int phys_screen = get_phys_screen(statusbar->screen);
Widget *widget;
int left = 0, right = 0;
Area rectangle = { 0, 0, 0, 0, NULL };
Drawable d;
2008-01-07 18:12:38 +01:00
/* don't waste our time */
switch(statusbar->position)
{
case Off:
return;
break;
case Right:
case Left:
/* we need a new pixmap this way [ ] to render */
XFreePixmap(globalconf.display, statusbar->sw->drawable);
d = XCreatePixmap(globalconf.display,
RootWindow(globalconf.display, phys_screen),
statusbar->width, statusbar->height,
DefaultDepth(globalconf.display, phys_screen));
statusbar->sw->drawable = d;
break;
default:
break;
}
DrawCtx *ctx = draw_context_new(globalconf.display,
2008-01-24 18:43:24 +01:00
phys_screen,
2007-12-30 21:00:34 +01:00
statusbar->width,
statusbar->height,
statusbar->sw->drawable);
2007-12-30 21:00:34 +01:00
2008-01-12 23:38:31 +01:00
rectangle.width = statusbar->width;
rectangle.height = statusbar->height;
draw_rectangle(ctx, rectangle, True,
2007-12-30 21:00:34 +01:00
globalconf.screens[statusbar->screen].colors_normal[ColBG]);
for(widget = statusbar->widgets; widget; widget = widget->next)
if (widget->alignment == AlignLeft)
2008-01-07 18:12:38 +01:00
{
widget->cache.needs_update = False;
2007-12-16 04:28:29 +01:00
left += widget->draw(widget, ctx, left, (left + right));
2008-01-07 18:12:38 +01:00
}
/* renders right widget from last to first */
for(widget = *widget_list_last(&statusbar->widgets);
widget;
widget = widget_list_prev(&statusbar->widgets, widget))
if (widget->alignment == AlignRight)
{
2008-01-07 18:12:38 +01:00
widget->cache.needs_update = False;
2007-12-16 04:28:29 +01:00
right += widget->draw(widget, ctx, right, (left + right));
}
2007-12-30 21:00:34 +01:00
for(widget = statusbar->widgets; widget; widget = widget->next)
if (widget->alignment == AlignFlex)
2008-01-07 18:12:38 +01:00
{
widget->cache.needs_update = False;
2007-12-16 04:28:29 +01:00
left += widget->draw(widget, ctx, left, (left + right));
2008-01-07 18:12:38 +01:00
}
2008-01-23 17:44:07 +01:00
switch(statusbar->position)
{
2008-01-23 17:44:07 +01:00
case Right:
d = draw_rotate(ctx, phys_screen, M_PI_2,
statusbar->height, 0);
XFreePixmap(globalconf.display, statusbar->sw->drawable);
statusbar->sw->drawable = d;
2008-01-23 17:44:07 +01:00
break;
case Left:
d = draw_rotate(ctx, phys_screen, - M_PI_2,
0, statusbar->width);
XFreePixmap(globalconf.display, statusbar->sw->drawable);
statusbar->sw->drawable = d;
2008-01-23 17:44:07 +01:00
break;
default:
break;
}
2008-01-31 18:18:15 +01:00
draw_context_delete(ctx);
2007-12-30 21:00:34 +01:00
statusbar_display(statusbar);
}
void
2007-12-30 21:00:34 +01:00
statusbar_display(Statusbar *statusbar)
{
2007-12-30 14:23:01 +01:00
/* don't waste our time */
2008-01-24 20:22:39 +01:00
if(statusbar->position != Off)
simplewindow_refresh_drawable(statusbar->sw, get_phys_screen(statusbar->screen));
}
2007-09-15 15:26:51 +02:00
2007-09-15 22:25:49 +02:00
void
2008-01-16 17:50:37 +01:00
statusbar_preinit(Statusbar *statusbar)
2007-09-15 22:25:49 +02:00
{
Widget *widget;
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[statusbar->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);
}
}
void
statusbar_init(Statusbar *statusbar)
{
Statusbar *sb;
int phys_screen = get_phys_screen(statusbar->screen);
2008-01-29 19:06:44 +01:00
Area area = screen_get_area(statusbar->screen,
globalconf.screens[statusbar->screen].statusbar,
&globalconf.screens[statusbar->screen].padding);
/* Top and Bottom Statusbar have prio */
for(sb = globalconf.screens[statusbar->screen].statusbar; sb; sb = sb->next)
switch(sb->position)
{
case Left:
case Right:
area.width += sb->height;
break;
default:
break;
}
2007-12-30 18:55:46 +01:00
if(statusbar->width <= 0)
{
2008-01-04 19:12:07 +01:00
if(statusbar->position == Right || statusbar->position == Left)
2007-12-30 18:55:46 +01:00
statusbar->width = area.height;
else
statusbar->width = area.width;
}
2008-01-24 10:37:16 +01:00
switch(statusbar->dposition)
{
case Right:
case Left:
statusbar->sw =
2008-01-26 17:58:01 +01:00
simplewindow_new(globalconf.display, phys_screen, 0, 0,
statusbar->height, statusbar->width, 0);
2008-01-24 10:37:16 +01:00
break;
default:
statusbar->sw =
2008-01-26 17:58:01 +01:00
simplewindow_new(globalconf.display, phys_screen, 0, 0,
statusbar->width, statusbar->height, 0);
2008-01-24 10:37:16 +01:00
break;
}
2007-12-22 20:55:17 +01:00
widget_calculate_alignments(statusbar->widgets);
2007-12-30 21:00:34 +01:00
statusbar_update_position(statusbar);
2007-12-31 10:04:03 +01:00
statusbar_draw(statusbar);
2007-09-15 22:25:49 +02:00
}
2007-09-15 15:26:51 +02:00
2008-01-07 18:12:38 +01:00
void
statusbar_refresh()
{
int screen;
Statusbar *statusbar;
Widget *widget;
2008-01-24 13:48:49 +01:00
for(screen = 0; screen < globalconf.nscreen; screen++)
2008-01-07 18:12:38 +01:00
for(statusbar = globalconf.screens[screen].statusbar;
statusbar;
statusbar = statusbar->next)
for(widget = statusbar->widgets; widget; widget = widget->next)
if(widget->cache.needs_update)
{
statusbar_draw(statusbar);
break;
}
}
2008-01-04 19:12:07 +01:00
Position
2007-12-30 21:00:34 +01:00
statusbar_get_position_from_str(const char *pos)
{
if(!a_strncmp(pos, "off", 3))
2008-01-04 19:12:07 +01:00
return Off;
else if(!a_strncmp(pos, "bottom", 6))
2008-01-04 19:12:07 +01:00
return Bottom;
else if(!a_strncmp(pos, "right", 5))
2008-01-04 19:12:07 +01:00
return Right;
else if(!a_strncmp(pos, "left", 4))
2008-01-04 19:12:07 +01:00
return Left;
return Top;
}
2007-12-30 21:00:34 +01:00
static Statusbar *
get_statusbar_byname(int screen, const char *name)
{
Statusbar *sb;
for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
if(!a_strcmp(sb->name, name))
return sb;
return NULL;
}
static void
statusbar_toggle(Statusbar *statusbar)
2007-09-15 15:34:28 +02:00
{
2008-01-04 19:12:07 +01:00
if(statusbar->position == Off)
statusbar->position = (statusbar->dposition == Off) ? Top : statusbar->dposition;
2007-09-15 15:34:28 +02:00
else
2008-01-04 19:12:07 +01:00
statusbar->position = Off;
2007-12-30 21:00:34 +01:00
statusbar_update_position(statusbar);
2008-01-18 09:34:52 +01:00
globalconf.screens[statusbar->screen].need_arrange = True;
2007-09-15 15:34:28 +02:00
}
2007-12-30 21:00:34 +01:00
/** Toggle statusbar
* \param screen Screen ID
2008-01-07 18:12:38 +01:00
* \param arg statusbar name
* \ingroup ui_callback
*/
void
2007-12-30 21:00:34 +01:00
uicb_statusbar_toggle(int screen, char *arg)
{
2007-12-30 21:00:34 +01:00
Statusbar *sb = get_statusbar_byname(screen, arg);
if(sb)
statusbar_toggle(sb);
else
for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
statusbar_toggle(sb);
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80