fix get_screen_bycoord() and add move_mouse_pointer_to_screen() for focus{next,prev...
[awesome.git] / statusbar.c
blobd5be960df0bf5154f870bdcf9b03780bda1f6e4e
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 <stdio.h>
24 #include "layout.h"
25 #include "statusbar.h"
26 #include "draw.h"
27 #include "screen.h"
28 #include "util.h"
29 #include "layouts/tile.h"
31 extern Client *clients, *sel, *stack; /* global client list and stack */
33 /** Check if at least a client is tagged with tag number t and is on screen
34 * screen
35 * \param t tag number
36 * \param screen screen number
37 * \return True or False
39 static Bool
40 isoccupied(unsigned int t, int screen)
42 Client *c;
44 for(c = clients; c; c = c->next)
45 if(c->tags[t] && c->screen == screen)
46 return True;
47 return False;
50 void
51 drawstatusbar(Display *disp, DC *drawcontext, awesome_config * awesomeconf)
53 int x, i;
55 drawcontext->x = drawcontext->y = 0;
56 for(i = 0; i < awesomeconf->ntags; i++)
58 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->tags[i].name, drawcontext->font.height);
59 if(awesomeconf->tags[i].selected)
61 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->sel);
62 if(isoccupied(i, awesomeconf->screen))
63 drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->sel[ColFG]);
65 else
67 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->tags[i].name, drawcontext->norm);
68 if(isoccupied(i, awesomeconf->screen))
69 drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel && sel->tags[i], drawcontext->norm[ColFG]);
71 drawcontext->x += drawcontext->w;
73 drawcontext->w = awesomeconf->statusbar.width;
74 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->current_layout->symbol, drawcontext->norm);
75 x = drawcontext->x + drawcontext->w;
76 drawcontext->w = textw(drawcontext->font.set, drawcontext->font.xfont, awesomeconf->statustext, drawcontext->font.height);
77 drawcontext->x = DisplayWidth(disp, awesomeconf->screen) - drawcontext->w;
78 if(drawcontext->x < x)
80 drawcontext->x = x;
81 drawcontext->w = DisplayWidth(disp, awesomeconf->screen) - x;
83 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, awesomeconf->statustext, drawcontext->norm);
84 if((drawcontext->w = drawcontext->x - x) > awesomeconf->statusbar.height)
86 drawcontext->x = x;
87 if(sel)
89 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->name, drawcontext->sel);
90 if(sel->isfloating)
91 drawsquare(disp, *drawcontext, awesomeconf->statusbar.drawable, sel->ismax, drawcontext->sel[ColFG]);
93 else if(IS_ARRANGE(layout_tile) || IS_ARRANGE(layout_tileleft))
95 char buf[256];
96 snprintf(buf, sizeof(buf), "nmaster: %d ncols: %d mwfact: %.2lf", awesomeconf->nmaster, awesomeconf->ncols, awesomeconf->mwfact);
97 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, buf, drawcontext->norm);
99 else
100 drawtext(disp, *drawcontext, awesomeconf->statusbar.drawable, NULL, drawcontext->norm);
102 XCopyArea(disp, awesomeconf->statusbar.drawable, awesomeconf->statusbar.window, drawcontext->gc, 0, 0, DisplayWidth(disp, awesomeconf->screen), awesomeconf->statusbar.height, 0, 0);
103 XSync(disp, False);
106 void
107 initstatusbar(Display *disp, int screen, DC *drawcontext, Statusbar *statusbar)
109 XSetWindowAttributes wa;
110 int real_screen;
112 statusbar->screen = screen;
114 if(XineramaIsActive(disp))
115 real_screen = DefaultScreen(disp);
116 else
117 real_screen = screen;
119 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
120 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
121 wa.cursor = drawcontext->cursor[CurNormal];
122 wa.override_redirect = 1;
123 wa.background_pixmap = ParentRelative;
124 wa.event_mask = ButtonPressMask | ExposureMask;
125 statusbar->window = XCreateWindow(disp, RootWindow(disp, screen), 0, 0, DisplayWidth(disp, real_screen),
126 statusbar->height, 0, DefaultDepth(disp, real_screen), CopyFromParent,
127 DefaultVisual(disp, real_screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
128 XDefineCursor(disp, statusbar->window, drawcontext->cursor[CurNormal]);
129 updatebarpos(disp, *statusbar);
130 XMapRaised(disp, statusbar->window);
131 statusbar->drawable = XCreatePixmap(disp,
132 RootWindow(disp, real_screen),
133 DisplayWidth(disp, real_screen),
134 statusbar->height,
135 DefaultDepth(disp, real_screen));
138 void
139 updatebarpos(Display *disp, Statusbar statusbar)
141 XEvent ev;
142 ScreenInfo *si;
144 switch (statusbar.position)
146 default:
147 XMoveWindow(disp, statusbar.window, 0, 0);
148 break;
149 case BarBot:
150 si = get_display_info(disp, statusbar.screen, statusbar);
151 XMoveWindow(disp, statusbar.window, 0, si->height);
152 XFree(si);
153 break;
154 case BarOff:
155 XMoveWindow(disp, statusbar.window, 0, 0 - statusbar.height);
156 break;
158 XSync(disp, False);
159 while(XCheckMaskEvent(disp, EnterWindowMask, &ev));
162 void
163 uicb_togglebar(Display *disp,
164 DC *drawcontext,
165 awesome_config *awesomeconf,
166 const char *arg __attribute__ ((unused)))
168 if(awesomeconf->statusbar.position == BarOff)
169 awesomeconf->statusbar.position = (awesomeconf->statusbar.position == BarOff) ? BarTop : awesomeconf->statusbar_default_position;
170 else
171 awesomeconf->statusbar.position = BarOff;
172 updatebarpos(disp, awesomeconf->statusbar);
173 arrange(disp, drawcontext, awesomeconf);