awsetbg.1: fix -t option
[awesome.git] / titlebar.c
blob45b341435eb9689e6fcfad8f60ba727d421a17d5
1 /*
2 * titlebar.c - titlebar management
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
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 <xcb/xcb.h>
24 #include "titlebar.h"
25 #include "client.h"
26 #include "widget.h"
27 #include "wibox.h"
28 #include "screen.h"
30 extern awesome_t globalconf;
32 /** Get a client by its titlebar.
33 * \param titlebar The titlebar.
34 * \return A client.
36 client_t *
37 client_getbytitlebar(wibox_t *titlebar)
39 client_t *c;
41 for(c = globalconf.clients; c; c = c->next)
42 if(c->titlebar == titlebar)
43 return c;
45 return NULL;
48 /** Get a client by its titlebar window.
49 * \param win The window.
50 * \return A client.
52 client_t *
53 client_getbytitlebarwin(xcb_window_t win)
55 client_t *c;
57 for(c = globalconf.clients; c; c = c->next)
58 if(c->titlebar && c->titlebar->sw.window == win)
59 return c;
61 return NULL;
64 void
65 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
67 int height, width, x_offset = 0, y_offset = 0;
69 switch(c->titlebar->position)
71 default:
72 return;
73 case Top:
74 width = MAX(1, geometry.width);
75 switch(c->titlebar->align)
77 default:
78 break;
79 case AlignRight:
80 x_offset = geometry.width - width;
81 break;
82 case AlignCenter:
83 x_offset = (geometry.width - width) / 2;
84 break;
86 res->x = geometry.x + x_offset;
87 res->y = geometry.y;
88 res->width = width;
89 res->height = c->titlebar->sw.geometry.height;
90 break;
91 case Bottom:
92 width = MAX(1, geometry.width);
93 switch(c->titlebar->align)
95 default:
96 break;
97 case AlignRight:
98 x_offset = geometry.width - width;
99 break;
100 case AlignCenter:
101 x_offset = (geometry.width - width) / 2;
102 break;
104 res->x = geometry.x + x_offset;
105 res->y = geometry.y + geometry.height - c->titlebar->sw.geometry.height;
106 res->width = width;
107 res->height = c->titlebar->sw.geometry.height;
108 break;
109 case Left:
110 height = MAX(1, geometry.height);
111 switch(c->titlebar->align)
113 default:
114 break;
115 case AlignRight:
116 y_offset = geometry.height - height;
117 break;
118 case AlignCenter:
119 y_offset = (geometry.height - height) / 2;
120 break;
122 res->x = geometry.x;
123 res->y = geometry.y + y_offset;
124 res->width = c->titlebar->sw.geometry.width;
125 res->height = height;
126 break;
127 case Right:
128 height = MAX(1, geometry.height);
129 switch(c->titlebar->align)
131 default:
132 break;
133 case AlignRight:
134 y_offset = geometry.height - height;
135 break;
136 case AlignCenter:
137 y_offset = (geometry.height - height) / 2;
138 break;
140 res->x = geometry.x + geometry.width - c->titlebar->sw.geometry.width;
141 res->y = geometry.y + y_offset;
142 res->width = c->titlebar->sw.geometry.width;
143 res->height = height;
144 break;
148 /** Detach a wibox titlebar from its client.
149 * \param c The client.
151 void
152 titlebar_client_detach(client_t *c)
154 /* If client has a titlebar, kick it out. */
155 if(c && c->titlebar)
157 simplewindow_wipe(&c->titlebar->sw);
158 c->titlebar->type = WIBOX_TYPE_NORMAL;
159 c->titlebar->screen = SCREEN_UNDEF;
160 wibox_unref(&c->titlebar);
161 c->titlebar = NULL;
162 client_need_arrange(c);
163 client_stack();
167 /** Attach a wibox to a client as its titlebar.
168 * \param c The client.
169 * \param t The wibox/titlebar.
171 void
172 titlebar_client_attach(client_t *c, wibox_t *t)
174 titlebar_client_detach(c);
176 if(c && t)
178 area_t wingeom;
180 /* check if titlebar is already on a client */
181 titlebar_client_detach(client_getbytitlebar(t));
183 c->titlebar = wibox_ref(&t);
184 t->type = WIBOX_TYPE_TITLEBAR;
185 t->screen = c->screen;
187 switch(t->position)
189 case Floating:
190 t->position = Top;
191 case Top:
192 case Bottom:
193 if(!t->sw.geometry.height)
194 t->sw.geometry.height = 1.5 * globalconf.font->height;
195 break;
196 case Left:
197 case Right:
198 if(!t->sw.geometry.width)
199 t->sw.geometry.width = 1.5 * globalconf.font->height;
200 break;
203 titlebar_geometry_compute(c, c->geometry, &wingeom);
205 simplewindow_init(&t->sw, c->phys_screen,
206 wingeom, 0, t->sw.orientation,
207 &t->sw.ctx.fg, &t->sw.ctx.bg);
208 simplewindow_border_color_set(&t->sw, &t->sw.border.color);
210 t->need_update = true;
212 /* This may seem useless, but it's the cleanest way to avoid seeing titlebars for banned clients. */
213 titlebar_update_geometry(c);
215 if(t->isvisible)
216 xcb_map_window(globalconf.connection, t->sw.window);
218 client_need_arrange(c);
219 client_stack();
223 /** Map or unmap a titlebar wibox.
224 * \param t The wibox/titlebar.
225 * \param visible The new state of the titlebar.
227 void
228 titlebar_set_visible(wibox_t *t, bool visible)
230 if (visible != t->isvisible)
232 /* The price of (un)mapping something small like a titlebar is pretty cheap.
233 * It would complicate matters if this rare case was treated like clients.
234 * Clients are moved out of the viewport when banned.
236 if ((t->isvisible = visible))
237 xcb_map_window(globalconf.connection, t->sw.window);
238 else
239 xcb_unmap_window(globalconf.connection, t->sw.window);
241 globalconf.screens[t->screen].need_arrange = true;
242 client_stack();
246 /** Titlebar newindex.
247 * \param L The Lua VM state.
248 * \param titlebar The wibox titlebar.
249 * \param tok The attribute token.
250 * \return The number of elements pushed on stack.
253 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
255 client_t *c = NULL;
257 switch(tok)
259 position_t position;
260 int i;
261 size_t len;
262 const char *buf;
264 case A_TK_ALIGN:
265 if((buf = luaL_checklstring(L, 3, &len)))
266 titlebar->align = draw_align_fromstr(buf, len);
267 else
268 return 0;
269 break;
270 case A_TK_BORDER_WIDTH:
271 if((i = luaL_checknumber(L, 3)) >= 0)
272 simplewindow_border_width_set(&titlebar->sw, i);
273 else
274 return 0;
275 break;
276 case A_TK_BORDER_COLOR:
277 if((buf = luaL_checklstring(L, 3, &len)))
278 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
279 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
280 return 0;
281 case A_TK_POSITION:
282 buf = luaL_checklstring(L, 3, &len);
283 position = position_fromstr(buf, len);
284 if(position != titlebar->position)
286 switch(position)
288 case Left:
289 switch(titlebar->position)
291 int tmp;
292 case Left:
293 case Right:
294 break;
295 case Top:
296 case Bottom:
297 case Floating:
298 tmp = titlebar->sw.geometry.width;
299 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
300 titlebar->sw.geometry.height = tmp;
301 break;
303 simplewindow_orientation_set(&titlebar->sw, North);
304 break;
305 case Right:
306 switch(titlebar->position)
308 int tmp;
309 case Left:
310 case Right:
311 break;
312 case Top:
313 case Bottom:
314 case Floating:
315 tmp = titlebar->sw.geometry.width;
316 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
317 titlebar->sw.geometry.height = tmp;
318 break;
320 simplewindow_orientation_set(&titlebar->sw, South);
321 break;
322 case Top:
323 case Bottom:
324 case Floating:
325 switch(titlebar->position)
327 int tmp;
328 case Left:
329 case Right:
330 tmp = titlebar->sw.geometry.width;
331 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
332 titlebar->sw.geometry.height = tmp;
333 break;
334 case Top:
335 case Bottom:
336 case Floating:
337 break;
339 simplewindow_orientation_set(&titlebar->sw, East);
340 break;
342 titlebar->position = position;
343 if((c = client_getbytitlebar(titlebar)))
345 titlebar_update_geometry(c);
346 /* call geometry hook for client because some like to
347 * set titlebar width in that hook, which make sense */
348 hooks_property(c, "geometry");
351 break;
352 default:
353 return 0;
356 if((c || (c = client_getbytitlebar(titlebar))))
357 client_need_arrange(c);
359 return 0;
362 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80