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 extern awesome_t globalconf
;
32 /** Get a client by its titlebar.
33 * \param titlebar The titlebar.
37 client_getbytitlebar(wibox_t
*titlebar
)
41 for(c
= globalconf
.clients
; c
; c
= c
->next
)
42 if(c
->titlebar
== titlebar
)
48 /** Get a client by its titlebar window.
49 * \param win The window.
53 client_getbytitlebarwin(xcb_window_t win
)
57 for(c
= globalconf
.clients
; c
; c
= c
->next
)
58 if(c
->titlebar
&& c
->titlebar
->sw
.window
== win
)
64 /** Move a titlebar out of the viewport.
65 * \param titlebar The titlebar.
68 titlebar_ban(wibox_t
*titlebar
)
70 /* Do it manually because client geometry remains unchanged. */
71 if(titlebar
&& !titlebar
->isbanned
)
74 simple_window_t
*sw
= &titlebar
->sw
;
78 uint32_t request
[] = { - sw
->geometry
.width
, - sw
->geometry
.height
};
79 /* Move the titlebar to the same place as the window. */
80 xcb_configure_window(globalconf
.connection
, sw
->window
,
81 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
85 /* Remove titlebar geometry from client. */
86 if((c
= client_getbytitlebar(titlebar
)))
87 c
->geometry
= titlebar_geometry_remove(titlebar
, 0, c
->geometry
);
89 titlebar
->isbanned
= true;
93 /** Move a titlebar on top of its client.
94 * \param titlebar The titlebar.
97 titlebar_unban(wibox_t
*titlebar
)
99 /* Do this manually because the system doesn't know we moved the toolbar.
100 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
101 * end up offscreen anyway. */
102 if(titlebar
&& titlebar
->isbanned
)
105 simple_window_t
*sw
= &titlebar
->sw
;
109 /* All resizing is done, so only move now. */
110 uint32_t request
[] = { sw
->geometry
.x
, sw
->geometry
.y
};
112 xcb_configure_window(globalconf
.connection
, sw
->window
,
113 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
117 titlebar
->isbanned
= false;
119 /* Add titlebar geometry from client. */
120 if((c
= client_getbytitlebar(titlebar
)))
121 c
->geometry
= titlebar_geometry_add(titlebar
, 0, c
->geometry
);
125 /** Get titlebar area.
126 * \param c The client
127 * \param geometry The client geometry including borders, excluding titlebars.
128 * \param res Pointer to area of titlebar, must be allocated already.
131 titlebar_geometry_compute(client_t
*c
, area_t geometry
, area_t
*res
)
133 int height
, width
, x_offset
= 0, y_offset
= 0;
135 switch(c
->titlebar
->position
)
140 width
= MAX(1, geometry
.width
);
141 switch(c
->titlebar
->align
)
146 x_offset
= geometry
.width
- width
;
149 x_offset
= (geometry
.width
- width
) / 2;
152 res
->x
= geometry
.x
+ x_offset
;
153 res
->y
= geometry
.y
- c
->titlebar
->sw
.geometry
.height
;
155 res
->height
= c
->titlebar
->sw
.geometry
.height
;
158 width
= MAX(1, geometry
.width
);
159 switch(c
->titlebar
->align
)
164 x_offset
= geometry
.width
- width
;
167 x_offset
= (geometry
.width
- width
) / 2;
170 res
->x
= geometry
.x
+ x_offset
;
171 res
->y
= geometry
.y
+ geometry
.height
;
173 res
->height
= c
->titlebar
->sw
.geometry
.height
;
176 height
= MAX(1, geometry
.height
);
177 switch(c
->titlebar
->align
)
182 y_offset
= geometry
.height
- height
;
185 y_offset
= (geometry
.height
- height
) / 2;
188 res
->x
= geometry
.x
- c
->titlebar
->sw
.geometry
.width
;
189 res
->y
= geometry
.y
+ y_offset
;
190 res
->width
= c
->titlebar
->sw
.geometry
.width
;
191 res
->height
= height
;
194 height
= MAX(1, geometry
.height
);
195 switch(c
->titlebar
->align
)
200 y_offset
= geometry
.height
- height
;
203 y_offset
= (geometry
.height
- height
) / 2;
206 res
->x
= geometry
.x
+ geometry
.width
;
207 res
->y
= geometry
.y
+ y_offset
;
208 res
->width
= c
->titlebar
->sw
.geometry
.width
;
209 res
->height
= height
;
214 /** Detach a wibox titlebar from its client.
215 * \param c The client.
218 titlebar_client_detach(client_t
*c
)
220 /* If client has a titlebar, kick it out. */
223 /* Update client geometry to exclude the titlebar. */
224 c
->geometry
= titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
);
225 simplewindow_wipe(&c
->titlebar
->sw
);
226 c
->titlebar
->type
= WIBOX_TYPE_NORMAL
;
227 c
->titlebar
->screen
= SCREEN_UNDEF
;
228 wibox_unref(&c
->titlebar
);
230 client_need_arrange(c
);
235 /** Attach a wibox to a client as its titlebar.
236 * \param c The client.
237 * \param t The wibox/titlebar.
240 titlebar_client_attach(client_t
*c
, wibox_t
*t
)
242 titlebar_client_detach(c
);
248 /* check if titlebar is already on a client */
249 titlebar_client_detach(client_getbytitlebar(t
));
251 /* check if client already has a titlebar. */
252 titlebar_client_detach(c
);
254 c
->titlebar
= wibox_ref(&t
);
255 t
->type
= WIBOX_TYPE_TITLEBAR
;
256 t
->screen
= c
->screen
;
264 if(!t
->sw
.geometry
.height
)
265 t
->sw
.geometry
.height
= 1.5 * globalconf
.font
->height
;
269 if(!t
->sw
.geometry
.width
)
270 t
->sw
.geometry
.width
= 1.5 * globalconf
.font
->height
;
274 /* Update client geometry to include the titlebar. */
275 c
->geometry
= titlebar_geometry_add(c
->titlebar
, 0, c
->geometry
);
277 /* Client geometry without titlebar, but including borders, since that is always consistent. */
278 titlebar_geometry_compute(c
, titlebar_geometry_remove(c
->titlebar
, 0, c
->geometry
), &wingeom
);
280 simplewindow_init(&t
->sw
, c
->phys_screen
,
281 wingeom
, 0, t
->sw
.orientation
,
282 &t
->sw
.ctx
.fg
, &t
->sw
.ctx
.bg
);
283 simplewindow_border_color_set(&t
->sw
, &t
->sw
.border
.color
);
285 t
->need_update
= true;
287 /* Call update geometry. This will move the wibox to the right place,
288 * which might be the same as `wingeom', but then it will ban the
289 * titlebar if needed. */
290 titlebar_update_geometry(c
);
292 xcb_map_window(globalconf
.connection
, t
->sw
.window
);
294 client_need_arrange(c
);
299 /** Map or unmap a titlebar wibox.
300 * \param t The wibox/titlebar.
301 * \param visible The new state of the titlebar.
304 titlebar_set_visible(wibox_t
*t
, bool visible
)
306 if(visible
!= t
->isvisible
)
308 if((t
->isvisible
= visible
))
313 globalconf
.screens
[t
->screen
].need_arrange
= true;
318 /** Titlebar newindex.
319 * \param L The Lua VM state.
320 * \param titlebar The wibox titlebar.
321 * \param tok The attribute token.
322 * \return The number of elements pushed on stack.
325 luaA_titlebar_newindex(lua_State
*L
, wibox_t
*titlebar
, awesome_token_t tok
)
337 if((buf
= luaL_checklstring(L
, 3, &len
)))
338 titlebar
->align
= draw_align_fromstr(buf
, len
);
342 case A_TK_BORDER_WIDTH
:
343 if((i
= luaL_checknumber(L
, 3)) >= 0)
344 simplewindow_border_width_set(&titlebar
->sw
, i
);
348 case A_TK_BORDER_COLOR
:
349 if((buf
= luaL_checklstring(L
, 3, &len
)))
350 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar
->sw
.border
.color
, buf
, len
)))
351 simplewindow_border_color_set(&titlebar
->sw
, &titlebar
->sw
.border
.color
);
354 buf
= luaL_checklstring(L
, 3, &len
);
355 position
= position_fromstr(buf
, len
);
356 if(position
!= titlebar
->position
)
361 switch(titlebar
->position
)
370 tmp
= titlebar
->sw
.geometry
.width
;
371 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
372 titlebar
->sw
.geometry
.height
= tmp
;
375 simplewindow_orientation_set(&titlebar
->sw
, North
);
378 switch(titlebar
->position
)
387 tmp
= titlebar
->sw
.geometry
.width
;
388 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
389 titlebar
->sw
.geometry
.height
= tmp
;
392 simplewindow_orientation_set(&titlebar
->sw
, South
);
397 switch(titlebar
->position
)
402 tmp
= titlebar
->sw
.geometry
.width
;
403 titlebar
->sw
.geometry
.width
= titlebar
->sw
.geometry
.height
;
404 titlebar
->sw
.geometry
.height
= tmp
;
411 simplewindow_orientation_set(&titlebar
->sw
, East
);
414 titlebar
->position
= position
;
415 if((c
= client_getbytitlebar(titlebar
)))
417 titlebar_update_geometry(c
);
418 /* call geometry hook for client because some like to
419 * set titlebar width in that hook, which make sense */
420 hooks_property(c
, "geometry");
428 if((c
|| (c
= client_getbytitlebar(titlebar
))))
429 client_need_arrange(c
);
434 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80