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");
217 /** Attach a wibox to a client as its titlebar.
218 * \param c The client.
221 titlebar_client_attach(client_t
*c
)
223 /* check if we can register the object */
224 wibox_t
*t
= luaA_object_ref(globalconf
.L
, -1);
226 titlebar_client_detach(c
);
228 /* check if titlebar is already on a client */
229 titlebar_client_detach(client_getbytitlebar(t
));
231 /* check if client already has a titlebar. */
232 titlebar_client_detach(c
);
234 /* set the object as new client's titlebar */
237 t
->type
= WIBOX_TYPE_TITLEBAR
;
238 t
->screen
= c
->screen
;
244 if(!t
->geometry
.height
)
245 t
->geometry
.height
= 1.5 * globalconf
.font
->height
;
249 if(!t
->geometry
.width
)
250 t
->geometry
.width
= 1.5 * globalconf
.font
->height
;
254 /* Update client geometry to include the titlebar. */
255 c
->geometry
= titlebar_geometry_add(c
->titlebar
, 0, c
->geometry
);
257 /* Client geometry without titlebar, but including borders, since that is always consistent. */
258 titlebar_geometry_compute(c
, titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
), &t
->geometry
);
260 wibox_init(t
, c
->phys_screen
);
262 t
->need_update
= true;
264 /* Call update geometry. This will move the wibox to the right place,
265 * which might be the same as `wingeom', but then it will ban the
266 * titlebar if needed. */
267 titlebar_update_geometry(c
);
269 xcb_map_window(globalconf
.connection
, t
->window
);
271 hook_property(c
, "titlebar");
275 /** Map or unmap a titlebar wibox.
276 * \param t The wibox/titlebar.
277 * \param visible The new state of the titlebar.
280 titlebar_set_visible(wibox_t
*t
, bool visible
)
282 if(visible
!= t
->visible
)
284 if((t
->visible
= visible
))
289 hook_property(t
, "visible");
295 luaA_titlebar_set_position(lua_State
*L
, int udx
)
297 wibox_t
*titlebar
= luaA_checkudata(L
, udx
, &wibox_class
);
299 const char *buf
= luaL_checklstring(L
, -1, &len
);
300 position_t position
= position_fromstr(buf
, len
);
301 if(position
!= titlebar
->position
)
306 switch(titlebar
->position
)
314 tmp
= titlebar
->geometry
.width
;
315 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
316 titlebar
->geometry
.height
= tmp
;
319 wibox_set_orientation(L
, udx
, North
);
322 switch(titlebar
->position
)
330 tmp
= titlebar
->geometry
.width
;
331 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
332 titlebar
->geometry
.height
= tmp
;
335 wibox_set_orientation(L
, udx
, South
);
339 switch(titlebar
->position
)
344 tmp
= titlebar
->geometry
.width
;
345 titlebar
->geometry
.width
= titlebar
->geometry
.height
;
346 titlebar
->geometry
.height
= tmp
;
352 wibox_set_orientation(L
, udx
, East
);
355 titlebar
->position
= position
;
357 if((c
= client_getbytitlebar(titlebar
)))
359 titlebar_update_geometry(c
);
360 /* call geometry hook for client because some like to
361 * set titlebar width in that hook, which make sense */
362 hook_property(c
, "geometry");
368 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80