fix the ultimate bug when restarting awesome, client misdisplayed
[awesome.git] / statusbar.c
blobbafe666468b486b54cd41ed0b7be865a5077b813
1 /*
2 * draw.c - draw functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "layout.h"
23 #include "statusbar.h"
24 #include "draw.h"
25 #include "screen.h"
26 #include "util.h"
28 extern Client *clients, *sel, *stack; /* global client list and stack */
30 /** Check if at least a client is tagged with tag number t and is on screen
31 * screen
32 * \param t tag number
33 * \param screen screen number
34 * \return True or False
36 static Bool
37 isoccupied(unsigned int t, int screen)
39 Client *c;
41 for(c = clients; c; c = c->next)
42 if(c->tags[t] && c->screen == screen)
43 return True;
44 return False;
47 void
48 drawstatusbar(Display *disp, int screen, DC *drawcontext, awesome_config * awesomeconf)
50 int x, i;
51 drawcontext->x = drawcontext->y = 0;
52 for(i = 0; i < awesomeconf->ntags; i++)
54 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->tags[i], drawcontext->font.height);
55 if(awesomeconf->selected_tags[i])
57 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i], drawcontext->sel);
58 drawsquare(disp, *drawcontext, sel && sel->tags[i], isoccupied(i, screen), drawcontext->sel, &awesomeconf->statusbar);
60 else
62 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i], drawcontext->norm);
63 drawsquare(disp, *drawcontext, sel && sel->tags[i], isoccupied(i, screen), drawcontext->norm, &awesomeconf->statusbar);
65 drawcontext->x += drawcontext->w;
67 drawcontext->w = awesomeconf->statusbar.width;
68 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->current_layout->symbol, drawcontext->norm);
69 x = drawcontext->x + drawcontext->w;
70 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->statustext, drawcontext->font.height);
71 drawcontext->x = DisplayWidth(disp, screen) - drawcontext->w;
72 if(drawcontext->x < x)
74 drawcontext->x = x;
75 drawcontext->w = DisplayWidth(disp, screen) - x;
77 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->statustext, drawcontext->norm);
78 if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height)
80 drawcontext->x = x;
81 if(sel)
83 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->name, drawcontext->sel);
84 drawsquare(disp, *drawcontext, sel->ismax, sel->isfloating, drawcontext->sel, &awesomeconf->statusbar);
86 else
87 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, NULL, drawcontext->norm);
89 XCopyArea(disp, awesomeconf->statusbar.drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, DisplayWidth(disp, screen), awesomeconf->statusbar.height, 0, 0);
90 XSync(disp, False);
93 void
94 initstatusbar(Display *disp, int screen, DC *drawcontext, Statusbar *statusbar)
96 XSetWindowAttributes wa;
98 statusbar->screen = screen;
100 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
101 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
102 wa.cursor = drawcontext->cursor[CurNormal];
103 wa.override_redirect = 1;
104 wa.background_pixmap = ParentRelative;
105 wa.event_mask = ButtonPressMask | ExposureMask;
106 statusbar->window = XCreateWindow(disp, RootWindow(disp, screen), 0, 0, DisplayWidth(disp, screen),
107 statusbar->height, 0, DefaultDepth(disp, screen), CopyFromParent,
108 DefaultVisual(disp, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
109 XDefineCursor(disp, statusbar->window, drawcontext->cursor[CurNormal]);
110 updatebarpos(disp, *statusbar);
111 XMapRaised(disp, statusbar->window);
112 statusbar->drawable = XCreatePixmap(disp,
113 RootWindow(disp, screen),
114 DisplayWidth(disp, screen),
115 statusbar->height,
116 DefaultDepth(disp, screen));
119 void
120 updatebarpos(Display *disp, Statusbar statusbar)
122 XEvent ev;
123 ScreenInfo *si;
125 switch (statusbar.position)
127 default:
128 XMoveWindow(disp, statusbar.window, 0, 0);
129 break;
130 case BarBot:
131 si = get_display_info(disp, statusbar.screen, statusbar);
132 XMoveWindow(disp, statusbar.window, 0, si->height);
133 XFree(si);
134 break;
135 case BarOff:
136 XMoveWindow(disp, statusbar.window, 0, 0 - statusbar.height);
137 break;
139 XSync(disp, False);
140 while(XCheckMaskEvent(disp, EnterWindowMask, &ev));
143 void
144 uicb_togglebar(Display *disp,
145 DC *drawcontext,
146 awesome_config *awesomeconf,
147 const char *arg __attribute__ ((unused)))
149 if(awesomeconf->statusbar.position == BarOff)
150 awesomeconf->statusbar.position = (awesomeconf->statusbar.position == BarOff) ? BarTop : awesomeconf->statusbar_default_position;
151 else
152 awesomeconf->statusbar.position = BarOff;
153 updatebarpos(disp, awesomeconf->statusbar);
154 arrange(disp, drawcontext, awesomeconf);