2 * titlebar.c - titlebar management
4 * Copyright © 2008-2009 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.
31 /** Get a client by its titlebar.
32 * \param titlebar The titlebar.
36 client_getbytitlebar(wibox_t
*titlebar
)
38 foreach(c
, globalconf
.clients
)
39 if((*c
)->titlebar
== titlebar
)
45 /** Get a client by its titlebar window.
46 * \param win The window.
50 client_getbytitlebarwin(xcb_window_t win
)
52 foreach(c
, globalconf
.clients
)
53 if((*c
)->titlebar
&& (*c
)->titlebar
->window
== win
)
59 /** Move a titlebar out of the viewport.
60 * \param titlebar The titlebar.
63 titlebar_ban(wibox_t
*titlebar
)
65 /* Do it manually because client geometry remains unchanged. */
66 if(titlebar
&& !titlebar
->isbanned
)
71 xcb_unmap_window(globalconf
.connection
, titlebar
->window
);
73 /* Remove titlebar geometry from client. */
74 if((c
= client_getbytitlebar(titlebar
)))
75 c
->geometry
= titlebar_geometry_remove(titlebar
, 0, c
->geometry
);
77 titlebar
->isbanned
= true;
81 /** Move a titlebar on top of its client.
82 * \param titlebar The titlebar.
85 titlebar_unban(wibox_t
*titlebar
)
87 /* Do this manually because the system doesn't know we moved the toolbar.
88 * Note that !visible titlebars are unmapped and for fullscreen it'll
89 * end up offscreen anyway. */
90 if(titlebar
&& titlebar
->isbanned
)
95 xcb_map_window(globalconf
.connection
, titlebar
->window
);
97 titlebar
->isbanned
= false;
99 /* Add titlebar geometry from client. */
100 if((c
= client_getbytitlebar(titlebar
)))
101 c
->geometry
= titlebar_geometry_add(titlebar
, 0, c
->geometry
);
105 /** Get titlebar area.
106 * \param c The client
107 * \param geometry The client geometry including borders, excluding titlebars.
108 * \param res Pointer to area of titlebar, must be allocated already.
111 titlebar_geometry_compute(client_t
*c
, area_t geometry
, area_t
*res
)
113 int height
, width
, x_offset
= 0, y_offset
= 0;
115 switch(c
->titlebar
->position
)
120 width
= MAX(1, geometry
.width
);
121 switch(c
->titlebar
->align
)
126 x_offset
= geometry
.width
- width
;
129 x_offset
= (geometry
.width
- width
) / 2;
132 res
->x
= geometry
.x
+ x_offset
;
133 res
->y
= geometry
.y
- c
->titlebar
->geometry
.height
;
135 res
->height
= c
->titlebar
->geometry
.height
;
138 width
= MAX(1, geometry
.width
);
139 switch(c
->titlebar
->align
)
144 x_offset
= geometry
.width
- width
;
147 x_offset
= (geometry
.width
- width
) / 2;
150 res
->x
= geometry
.x
+ x_offset
;
151 res
->y
= geometry
.y
+ geometry
.height
;
153 res
->height
= c
->titlebar
->geometry
.height
;
156 height
= MAX(1, geometry
.height
);
157 switch(c
->titlebar
->align
)
162 y_offset
= geometry
.height
- height
;
165 y_offset
= (geometry
.height
- height
) / 2;
168 res
->x
= geometry
.x
- c
->titlebar
->geometry
.width
;
169 res
->y
= geometry
.y
+ y_offset
;
170 res
->width
= c
->titlebar
->geometry
.width
;
171 res
->height
= height
;
174 height
= MAX(1, geometry
.height
);
175 switch(c
->titlebar
->align
)
180 y_offset
= geometry
.height
- height
;
183 y_offset
= (geometry
.height
- height
) / 2;
186 res
->x
= geometry
.x
+ geometry
.width
;
187 res
->y
= geometry
.y
+ y_offset
;
188 res
->width
= c
->titlebar
->geometry
.width
;
189 res
->height
= height
;
194 /** Detach a wibox titlebar from its client.
195 * \param c The client.
198 titlebar_client_detach(client_t
*c
)
200 /* If client has a titlebar, kick it out. */
203 /* Update client geometry to exclude the titlebar. */
204 c
->geometry
= titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
);
205 wibox_wipe(c
->titlebar
);
206 c
->titlebar
->type
= WIBOX_TYPE_NORMAL
;
207 c
->titlebar
->screen
= NULL
;
209 luaA_object_unref(globalconf
.L
, c
->titlebar
);
212 hook_property(c
, "titlebar");
213 luaA_object_push(globalconf
.L
, c
);
214 luaA_object_emit_signal(globalconf
.L
, -1, "property::titlebar", 0);
215 lua_pop(globalconf
.L
, 1);
220 /** Attach a wibox to a client as its titlebar.
221 * \param c The client.
224 titlebar_client_attach(client_t
*c
)
226 /* check if we can register the object */
227 wibox_t
*t
= luaA_object_ref_class(globalconf
.L
, -1, &wibox_class
);
229 titlebar_client_detach(c
);
231 /* check if titlebar is already on a client */
232 titlebar_client_detach(client_getbytitlebar(t
));
234 /* check if client already has a titlebar. */
235 titlebar_client_detach(c
);
237 /* set the object as new client's titlebar */
240 t
->type
= WIBOX_TYPE_TITLEBAR
;
241 t
->screen
= c
->screen
;
247 if(!t
->geometry
.height
)
248 t
->geometry
.height
= 1.5 * globalconf
.font
->height
;
252 if(!t
->geometry
.width
)
253 t
->geometry
.width
= 1.5 * globalconf
.font
->height
;
257 /* Update client geometry to include the titlebar. */
258 c
->geometry
= titlebar_geometry_add(c
->titlebar
, 0, c
->geometry
);
260 /* Client geometry without titlebar, but including borders, since that is always consistent. */
261 titlebar_geometry_compute(c
, titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
), &t
->geometry
);
263 wibox_init(t
, c
->phys_screen
);
265 t
->need_update
= true;
267 /* Call update geometry. This will move the wibox to the right place,
268 * which might be the same as `wingeom', but then it will ban the
269 * titlebar if needed. */
270 titlebar_update_geometry(c
);
272 xcb_map_window(globalconf
.connection
, t
->window
);
274 hook_property(c
, "titlebar");
276 luaA_object_push(globalconf
.L
, c
);
277 luaA_object_emit_signal(globalconf
.L
, -1, "property::titlebar", 0);
278 lua_pop(globalconf
.L
, 1);
282 /** Map or unmap a titlebar wibox.
283 * \param t The wibox/titlebar.
284 * \param visible The new state of the titlebar.
287 titlebar_set_visible(wibox_t
*t
, bool visible
)
289 if(visible
!= t
->visible
)
291 if((t
->visible
= visible
))
296 hook_property(t
, "visible");
302 luaA_titlebar_set_position(lua_State
*L
, int udx
)
304 wibox_t
*titlebar
= luaA_checkudata(L
, udx
, &wibox_class
);
306 const char *buf
= luaL_checklstring(L
, -1, &len
);
307 position_t position
= position_fromstr(buf
, len
);
308 if(position
!= titlebar
->position
)
313 switch(titlebar
->position
)
321 tmp
= titlebar
->geometry
.width
;
322 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
323 titlebar
->geometry
.height
= tmp
;
326 wibox_set_orientation(L
, udx
, North
);
329 switch(titlebar
->position
)
337 tmp
= titlebar
->geometry
.width
;
338 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
339 titlebar
->geometry
.height
= tmp
;
342 wibox_set_orientation(L
, udx
, South
);
346 switch(titlebar
->position
)
351 tmp
= titlebar
->geometry
.width
;
352 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
353 titlebar
->geometry
.height
= tmp
;
359 wibox_set_orientation(L
, udx
, East
);
362 titlebar
->position
= position
;
364 if((c
= client_getbytitlebar(titlebar
)))
366 titlebar_update_geometry(c
);
367 /* call geometry hook for client because some like to
368 * set titlebar width in that hook, which make sense */
369 hook_property(c
, "geometry");
375 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80