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.
30 /** Get a client by its titlebar.
31 * \param titlebar The titlebar.
35 client_getbytitlebar(wibox_t
*titlebar
)
37 foreach(c
, globalconf
.clients
)
38 if((*c
)->titlebar
== titlebar
)
44 /** Get a client by its titlebar window.
45 * \param win The window.
49 client_getbytitlebarwin(xcb_window_t win
)
51 foreach(c
, globalconf
.clients
)
52 if((*c
)->titlebar
&& (*c
)->titlebar
->window
== win
)
58 /** Move a titlebar out of the viewport.
59 * \param titlebar The titlebar.
62 titlebar_ban(wibox_t
*titlebar
)
64 /* Do it manually because client geometry remains unchanged. */
65 if(titlebar
&& !titlebar
->isbanned
)
70 xcb_unmap_window(globalconf
.connection
, titlebar
->window
);
72 /* Remove titlebar geometry from client. */
73 if((c
= client_getbytitlebar(titlebar
)))
74 c
->geometry
= titlebar_geometry_remove(titlebar
, 0, c
->geometry
);
76 titlebar
->isbanned
= true;
80 /** Move a titlebar on top of its client.
81 * \param titlebar The titlebar.
84 titlebar_unban(wibox_t
*titlebar
)
86 /* Do this manually because the system doesn't know we moved the toolbar.
87 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
88 * end up offscreen anyway. */
89 if(titlebar
&& titlebar
->isbanned
)
94 xcb_map_window(globalconf
.connection
, titlebar
->window
);
96 titlebar
->isbanned
= false;
98 /* Add titlebar geometry from client. */
99 if((c
= client_getbytitlebar(titlebar
)))
100 c
->geometry
= titlebar_geometry_add(titlebar
, 0, c
->geometry
);
104 /** Get titlebar area.
105 * \param c The client
106 * \param geometry The client geometry including borders, excluding titlebars.
107 * \param res Pointer to area of titlebar, must be allocated already.
110 titlebar_geometry_compute(client_t
*c
, area_t geometry
, area_t
*res
)
112 int height
, width
, x_offset
= 0, y_offset
= 0;
114 switch(c
->titlebar
->position
)
119 width
= MAX(1, geometry
.width
);
120 switch(c
->titlebar
->align
)
125 x_offset
= geometry
.width
- width
;
128 x_offset
= (geometry
.width
- width
) / 2;
131 res
->x
= geometry
.x
+ x_offset
;
132 res
->y
= geometry
.y
- c
->titlebar
->geometry
.height
;
134 res
->height
= c
->titlebar
->geometry
.height
;
137 width
= MAX(1, geometry
.width
);
138 switch(c
->titlebar
->align
)
143 x_offset
= geometry
.width
- width
;
146 x_offset
= (geometry
.width
- width
) / 2;
149 res
->x
= geometry
.x
+ x_offset
;
150 res
->y
= geometry
.y
+ geometry
.height
;
152 res
->height
= c
->titlebar
->geometry
.height
;
155 height
= MAX(1, geometry
.height
);
156 switch(c
->titlebar
->align
)
161 y_offset
= geometry
.height
- height
;
164 y_offset
= (geometry
.height
- height
) / 2;
167 res
->x
= geometry
.x
- c
->titlebar
->geometry
.width
;
168 res
->y
= geometry
.y
+ y_offset
;
169 res
->width
= c
->titlebar
->geometry
.width
;
170 res
->height
= height
;
173 height
= MAX(1, geometry
.height
);
174 switch(c
->titlebar
->align
)
179 y_offset
= geometry
.height
- height
;
182 y_offset
= (geometry
.height
- height
) / 2;
185 res
->x
= geometry
.x
+ geometry
.width
;
186 res
->y
= geometry
.y
+ y_offset
;
187 res
->width
= c
->titlebar
->geometry
.width
;
188 res
->height
= height
;
193 /** Detach a wibox titlebar from its client.
194 * \param c The client.
197 titlebar_client_detach(client_t
*c
)
199 /* If client has a titlebar, kick it out. */
202 /* Update client geometry to exclude the titlebar. */
203 c
->geometry
= titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
);
204 wibox_wipe(c
->titlebar
);
205 c
->titlebar
->type
= WIBOX_TYPE_NORMAL
;
206 c
->titlebar
->screen
= NULL
;
208 wibox_unref(globalconf
.L
, c
->titlebar
);
211 hook_property(client
, c
, "titlebar");
216 /** Attach a wibox to a client as its titlebar.
217 * \param c The client.
220 titlebar_client_attach(client_t
*c
)
222 /* check if we can register the object */
223 wibox_t
*t
= wibox_ref(globalconf
.L
, -1);
225 titlebar_client_detach(c
);
227 /* check if titlebar is already on a client */
228 titlebar_client_detach(client_getbytitlebar(t
));
230 /* check if client already has a titlebar. */
231 titlebar_client_detach(c
);
233 /* set the object as new client's titlebar */
236 t
->type
= WIBOX_TYPE_TITLEBAR
;
237 t
->screen
= c
->screen
;
243 if(!t
->geometry
.height
)
244 t
->geometry
.height
= 1.5 * globalconf
.font
->height
;
248 if(!t
->geometry
.width
)
249 t
->geometry
.width
= 1.5 * globalconf
.font
->height
;
253 /* Update client geometry to include the titlebar. */
254 c
->geometry
= titlebar_geometry_add(c
->titlebar
, 0, c
->geometry
);
256 /* Client geometry without titlebar, but including borders, since that is always consistent. */
257 titlebar_geometry_compute(c
, titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
), &t
->geometry
);
259 wibox_init(t
, c
->phys_screen
);
261 t
->need_update
= true;
263 /* Call update geometry. This will move the wibox to the right place,
264 * which might be the same as `wingeom', but then it will ban the
265 * titlebar if needed. */
266 titlebar_update_geometry(c
);
268 xcb_map_window(globalconf
.connection
, t
->window
);
270 hook_property(client
, c
, "titlebar");
274 /** Map or unmap a titlebar wibox.
275 * \param t The wibox/titlebar.
276 * \param visible The new state of the titlebar.
279 titlebar_set_visible(wibox_t
*t
, bool visible
)
281 if(visible
!= t
->isvisible
)
283 if((t
->isvisible
= visible
))
288 hook_property(wibox
, t
, "visible");
293 /** Titlebar newindex.
294 * \param L The Lua VM state.
295 * \param titlebar The wibox titlebar.
296 * \param tok The attribute token.
297 * \return The number of elements pushed on stack.
300 luaA_titlebar_newindex(lua_State
*L
, wibox_t
*titlebar
, awesome_token_t tok
)
312 if((buf
= luaL_checklstring(L
, 3, &len
)))
313 titlebar
->align
= draw_align_fromstr(buf
, len
);
317 case A_TK_BORDER_WIDTH
:
318 if((i
= luaL_checknumber(L
, 3)) >= 0)
319 wibox_border_width_set(titlebar
, i
);
323 case A_TK_BORDER_COLOR
:
324 if((buf
= luaL_checklstring(L
, 3, &len
)))
325 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar
->border
.color
, buf
, len
)))
326 wibox_border_color_set(titlebar
, &titlebar
->border
.color
);
329 buf
= luaL_checklstring(L
, 3, &len
);
330 position
= position_fromstr(buf
, len
);
331 if(position
!= titlebar
->position
)
336 switch(titlebar
->position
)
344 tmp
= titlebar
->geometry
.width
;
345 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
346 titlebar
->geometry
.height
= tmp
;
349 wibox_orientation_set(titlebar
, North
);
352 switch(titlebar
->position
)
360 tmp
= titlebar
->geometry
.width
;
361 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
362 titlebar
->geometry
.height
= tmp
;
365 wibox_orientation_set(titlebar
, South
);
369 switch(titlebar
->position
)
374 tmp
= titlebar
->geometry
.width
;
375 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
376 titlebar
->geometry
.height
= tmp
;
382 wibox_orientation_set(titlebar
, East
);
385 titlebar
->position
= position
;
386 if((c
= client_getbytitlebar(titlebar
)))
388 titlebar_update_geometry(c
);
389 /* call geometry hook for client because some like to
390 * set titlebar width in that hook, which make sense */
391 hook_property(client
, c
, "geometry");
402 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80