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
->sw
.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
)
68 simple_window_t
*sw
= &titlebar
->sw
;
72 uint32_t request
[] = { - sw
->geometry
.width
, - sw
->geometry
.height
};
73 /* Move the titlebar to the same place as the window. */
74 xcb_configure_window(globalconf
.connection
, sw
->window
,
75 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
79 /* Remove titlebar geometry from client. */
80 if((c
= client_getbytitlebar(titlebar
)))
81 c
->geometry
= titlebar_geometry_remove(titlebar
, 0, c
->geometry
);
83 titlebar
->isbanned
= true;
87 /** Move a titlebar on top of its client.
88 * \param titlebar The titlebar.
91 titlebar_unban(wibox_t
*titlebar
)
93 /* Do this manually because the system doesn't know we moved the toolbar.
94 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
95 * end up offscreen anyway. */
96 if(titlebar
&& titlebar
->isbanned
)
99 simple_window_t
*sw
= &titlebar
->sw
;
103 /* All resizing is done, so only move now. */
104 uint32_t request
[] = { sw
->geometry
.x
, sw
->geometry
.y
};
106 xcb_configure_window(globalconf
.connection
, sw
->window
,
107 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
111 titlebar
->isbanned
= false;
113 /* Add titlebar geometry from client. */
114 if((c
= client_getbytitlebar(titlebar
)))
115 c
->geometry
= titlebar_geometry_add(titlebar
, 0, c
->geometry
);
119 /** Get titlebar area.
120 * \param c The client
121 * \param geometry The client geometry including borders, excluding titlebars.
122 * \param res Pointer to area of titlebar, must be allocated already.
125 titlebar_geometry_compute(client_t
*c
, area_t geometry
, area_t
*res
)
127 int height
, width
, x_offset
= 0, y_offset
= 0;
129 switch(c
->titlebar
->position
)
134 width
= MAX(1, geometry
.width
);
135 switch(c
->titlebar
->align
)
140 x_offset
= geometry
.width
- width
;
143 x_offset
= (geometry
.width
- width
) / 2;
146 res
->x
= geometry
.x
+ x_offset
;
147 res
->y
= geometry
.y
- c
->titlebar
->sw
.geometry
.height
;
149 res
->height
= c
->titlebar
->sw
.geometry
.height
;
152 width
= MAX(1, geometry
.width
);
153 switch(c
->titlebar
->align
)
158 x_offset
= geometry
.width
- width
;
161 x_offset
= (geometry
.width
- width
) / 2;
164 res
->x
= geometry
.x
+ x_offset
;
165 res
->y
= geometry
.y
+ geometry
.height
;
167 res
->height
= c
->titlebar
->sw
.geometry
.height
;
170 height
= MAX(1, geometry
.height
);
171 switch(c
->titlebar
->align
)
176 y_offset
= geometry
.height
- height
;
179 y_offset
= (geometry
.height
- height
) / 2;
182 res
->x
= geometry
.x
- c
->titlebar
->sw
.geometry
.width
;
183 res
->y
= geometry
.y
+ y_offset
;
184 res
->width
= c
->titlebar
->sw
.geometry
.width
;
185 res
->height
= height
;
188 height
= MAX(1, geometry
.height
);
189 switch(c
->titlebar
->align
)
194 y_offset
= geometry
.height
- height
;
197 y_offset
= (geometry
.height
- height
) / 2;
200 res
->x
= geometry
.x
+ geometry
.width
;
201 res
->y
= geometry
.y
+ y_offset
;
202 res
->width
= c
->titlebar
->sw
.geometry
.width
;
203 res
->height
= height
;
208 /** Detach a wibox titlebar from its client.
209 * \param c The client.
212 titlebar_client_detach(client_t
*c
)
214 /* If client has a titlebar, kick it out. */
217 /* Update client geometry to exclude the titlebar. */
218 c
->geometry
= titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
);
219 simplewindow_wipe(&c
->titlebar
->sw
);
220 c
->titlebar
->type
= WIBOX_TYPE_NORMAL
;
221 c
->titlebar
->screen
= SCREEN_UNDEF
;
223 wibox_unref(globalconf
.L
, c
->titlebar
);
226 client_need_arrange(c
);
231 /** Attach a wibox to a client as its titlebar.
232 * \param c The client.
233 * \param ud The index of the wibox on the stack.
236 titlebar_client_attach(client_t
*c
)
238 /* check if we can register the object */
239 wibox_t
*t
= wibox_ref(globalconf
.L
);
241 titlebar_client_detach(c
);
243 /* check if titlebar is already on a client */
244 titlebar_client_detach(client_getbytitlebar(t
));
246 /* check if client already has a titlebar. */
247 titlebar_client_detach(c
);
249 /* set the object as new client's titlebar */
252 t
->type
= WIBOX_TYPE_TITLEBAR
;
253 t
->screen
= c
->screen
;
261 if(!t
->sw
.geometry
.height
)
262 t
->sw
.geometry
.height
= 1.5 * globalconf
.font
->height
;
266 if(!t
->sw
.geometry
.width
)
267 t
->sw
.geometry
.width
= 1.5 * globalconf
.font
->height
;
271 /* Update client geometry to include the titlebar. */
272 c
->geometry
= titlebar_geometry_add(c
->titlebar
, 0, c
->geometry
);
274 /* Client geometry without titlebar, but including borders, since that is always consistent. */
276 titlebar_geometry_compute(c
, titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
), &wingeom
);
278 simplewindow_init(&t
->sw
, c
->phys_screen
,
279 wingeom
, 0, t
->sw
.orientation
,
280 &t
->sw
.ctx
.fg
, &t
->sw
.ctx
.bg
);
281 simplewindow_border_color_set(&t
->sw
, &t
->sw
.border
.color
);
283 t
->need_update
= true;
285 /* Call update geometry. This will move the wibox to the right place,
286 * which might be the same as `wingeom', but then it will ban the
287 * titlebar if needed. */
288 titlebar_update_geometry(c
);
290 xcb_map_window(globalconf
.connection
, t
->sw
.window
);
292 client_need_arrange(c
);
296 /** Map or unmap a titlebar wibox.
297 * \param t The wibox/titlebar.
298 * \param visible The new state of the titlebar.
301 titlebar_set_visible(wibox_t
*t
, bool visible
)
303 if(visible
!= t
->isvisible
)
305 if((t
->isvisible
= visible
))
310 globalconf
.screens
[t
->screen
].need_arrange
= true;
315 /** Titlebar newindex.
316 * \param L The Lua VM state.
317 * \param titlebar The wibox titlebar.
318 * \param tok The attribute token.
319 * \return The number of elements pushed on stack.
322 luaA_titlebar_newindex(lua_State
*L
, wibox_t
*titlebar
, awesome_token_t tok
)
334 if((buf
= luaL_checklstring(L
, 3, &len
)))
335 titlebar
->align
= draw_align_fromstr(buf
, len
);
339 case A_TK_BORDER_WIDTH
:
340 if((i
= luaL_checknumber(L
, 3)) >= 0)
341 simplewindow_border_width_set(&titlebar
->sw
, i
);
345 case A_TK_BORDER_COLOR
:
346 if((buf
= luaL_checklstring(L
, 3, &len
)))
347 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar
->sw
.border
.color
, buf
, len
)))
348 simplewindow_border_color_set(&titlebar
->sw
, &titlebar
->sw
.border
.color
);
351 buf
= luaL_checklstring(L
, 3, &len
);
352 position
= position_fromstr(buf
, len
);
353 if(position
!= titlebar
->position
)
358 switch(titlebar
->position
)
367 tmp
= titlebar
->sw
.geometry
.width
;
368 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
369 titlebar
->sw
.geometry
.height
= tmp
;
372 simplewindow_orientation_set(&titlebar
->sw
, North
);
375 switch(titlebar
->position
)
384 tmp
= titlebar
->sw
.geometry
.width
;
385 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
386 titlebar
->sw
.geometry
.height
= tmp
;
389 simplewindow_orientation_set(&titlebar
->sw
, South
);
394 switch(titlebar
->position
)
399 tmp
= titlebar
->sw
.geometry
.width
;
400 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
401 titlebar
->sw
.geometry
.height
= tmp
;
408 simplewindow_orientation_set(&titlebar
->sw
, East
);
411 titlebar
->position
= position
;
412 if((c
= client_getbytitlebar(titlebar
)))
414 titlebar_update_geometry(c
);
415 /* call geometry hook for client because some like to
416 * set titlebar width in that hook, which make sense */
417 hooks_property(c
, "geometry");
425 if((c
|| (c
= client_getbytitlebar(titlebar
))))
426 client_need_arrange(c
);
431 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80