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.
23 #include <xcb/xcb_aux.h>
31 extern awesome_t globalconf
;
33 DO_LUA_NEW(extern, titlebar_t
, titlebar
, "titlebar", titlebar_ref
)
34 DO_LUA_GC(titlebar_t
, titlebar
, "titlebar", titlebar_unref
)
35 DO_LUA_EQ(titlebar_t
, titlebar
, "titlebar")
37 /** Get a client by its titlebar.
38 * \param titlebar The titlebar.
42 client_getbytitlebar(titlebar_t
*titlebar
)
46 for(c
= globalconf
.clients
; c
; c
= c
->next
)
47 if(c
->titlebar
== titlebar
)
53 /** Get a client by its titlebar window.
54 * \param win The window.
58 client_getbytitlebarwin(xcb_window_t win
)
62 for(c
= globalconf
.clients
; c
; c
= c
->next
)
63 if(c
->titlebar
&& c
->titlebar
->sw
&& c
->titlebar
->sw
->window
== win
)
69 /** Draw the titlebar content.
70 * \param c The client.
73 titlebar_draw(client_t
*c
)
75 xcb_drawable_t dw
= 0;
79 if(!c
|| !c
->titlebar
|| !c
->titlebar
->sw
|| !c
->titlebar
->position
)
82 s
= xutil_screen_get(globalconf
.connection
,
83 c
->titlebar
->sw
->phys_screen
);
85 switch(c
->titlebar
->position
)
91 dw
= xcb_generate_id(globalconf
.connection
);
92 xcb_create_pixmap(globalconf
.connection
, s
->root_depth
,
95 c
->titlebar
->sw
->geometry
.height
,
96 c
->titlebar
->sw
->geometry
.width
);
97 ctx
= draw_context_new(globalconf
.connection
, c
->titlebar
->sw
->phys_screen
,
98 c
->titlebar
->sw
->geometry
.height
,
99 c
->titlebar
->sw
->geometry
.width
,
101 &c
->titlebar
->colors
.fg
,
102 &c
->titlebar
->colors
.bg
);
105 ctx
= draw_context_new(globalconf
.connection
, c
->titlebar
->sw
->phys_screen
,
106 c
->titlebar
->sw
->geometry
.width
,
107 c
->titlebar
->sw
->geometry
.height
,
108 c
->titlebar
->sw
->pixmap
,
109 &c
->titlebar
->colors
.fg
,
110 &c
->titlebar
->colors
.bg
);
114 widget_render(c
->titlebar
->widgets
, ctx
, c
->titlebar
->sw
->gc
, c
->titlebar
->sw
->pixmap
,
115 c
->screen
, c
->titlebar
->position
,
116 c
->titlebar
->sw
->geometry
.x
, c
->titlebar
->sw
->geometry
.y
,
117 c
->titlebar
, AWESOME_TYPE_TITLEBAR
);
119 switch(c
->titlebar
->position
)
122 draw_rotate(ctx
, ctx
->pixmap
, c
->titlebar
->sw
->pixmap
,
123 ctx
->width
, ctx
->height
,
124 ctx
->height
, ctx
->width
,
125 - M_PI_2
, 0, c
->titlebar
->sw
->geometry
.height
);
126 xcb_free_pixmap(globalconf
.connection
, dw
);
129 draw_rotate(ctx
, ctx
->pixmap
, c
->titlebar
->sw
->pixmap
,
130 ctx
->width
, ctx
->height
,
131 ctx
->height
, ctx
->width
,
132 M_PI_2
, c
->titlebar
->sw
->geometry
.width
, 0);
133 xcb_free_pixmap(globalconf
.connection
, dw
);
138 simplewindow_refresh_pixmap(c
->titlebar
->sw
);
140 draw_context_delete(&ctx
);
142 c
->titlebar
->need_update
= false;
145 /** Titlebar refresh function.
148 titlebar_refresh(void)
152 for(c
= globalconf
.clients
; c
; c
= c
->next
)
153 if(c
->titlebar
&& c
->titlebar
->need_update
)
158 titlebar_geometry_compute(client_t
*c
, area_t geometry
, area_t
*res
)
160 int width
, x_offset
= 0, y_offset
= 0;
162 switch(c
->titlebar
->position
)
167 if(c
->titlebar
->width
)
168 width
= MAX(1, MIN(c
->titlebar
->width
, geometry
.width
- 2 * c
->titlebar
->border
.width
));
170 width
= MAX(1, geometry
.width
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
);
171 switch(c
->titlebar
->align
)
176 x_offset
= 2 * c
->border
+ geometry
.width
- width
- 2 * c
->titlebar
->border
.width
;
179 x_offset
= (geometry
.width
- width
) / 2;
182 res
->x
= geometry
.x
+ x_offset
;
183 res
->y
= geometry
.y
- c
->titlebar
->height
- 2 * c
->titlebar
->border
.width
+ c
->border
;
185 res
->height
= c
->titlebar
->height
;
188 if(c
->titlebar
->width
)
189 width
= MAX(1, MIN(c
->titlebar
->width
, geometry
.width
- 2 * c
->titlebar
->border
.width
));
191 width
= MAX(1, geometry
.width
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
);
192 switch(c
->titlebar
->align
)
197 x_offset
= 2 * c
->border
+ geometry
.width
- width
- 2 * c
->titlebar
->border
.width
;
200 x_offset
= (geometry
.width
- width
) / 2;
203 res
->x
= geometry
.x
+ x_offset
;
204 res
->y
= geometry
.y
+ geometry
.height
+ c
->border
;
206 res
->height
= c
->titlebar
->height
;
209 if(c
->titlebar
->width
)
210 width
= MAX(1, MIN(c
->titlebar
->width
, geometry
.height
- 2 * c
->titlebar
->border
.width
));
212 width
= MAX(1, geometry
.height
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
);
213 switch(c
->titlebar
->align
)
218 y_offset
= 2 * c
->border
+ geometry
.height
- width
- 2 * c
->titlebar
->border
.width
;
221 y_offset
= (geometry
.height
- width
) / 2;
224 res
->x
= geometry
.x
- c
->titlebar
->height
+ c
->border
;
225 res
->y
= geometry
.y
+ y_offset
;
226 res
->width
= c
->titlebar
->height
;
230 if(c
->titlebar
->width
)
231 width
= MAX(1, MIN(c
->titlebar
->width
, geometry
.height
- 2 * c
->titlebar
->border
.width
));
233 width
= MAX(1, geometry
.height
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
);
234 switch(c
->titlebar
->align
)
239 y_offset
= 2 * c
->border
+ geometry
.height
- width
- 2 * c
->titlebar
->border
.width
;
242 y_offset
= (geometry
.height
- width
) / 2;
245 res
->x
= geometry
.x
+ geometry
.width
+ c
->border
;
246 res
->y
= geometry
.y
+ y_offset
;
247 res
->width
= c
->titlebar
->height
;
253 /** Set client titlebar position.
254 * \param c The client.
257 titlebar_init(client_t
*c
)
259 int width
= 0, height
= 0;
262 switch(c
->titlebar
->position
)
265 c
->titlebar
->position
= Off
;
269 if(c
->titlebar
->width
)
270 width
= MIN(c
->titlebar
->width
, c
->geometry
.width
- 2 * c
->titlebar
->border
.width
);
272 width
= c
->geometry
.width
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
;
273 height
= c
->titlebar
->height
;
277 if(c
->titlebar
->width
)
278 height
= MIN(c
->titlebar
->width
, c
->geometry
.height
- 2 * c
->titlebar
->border
.width
);
280 height
= c
->geometry
.height
+ 2 * c
->border
- 2 * c
->titlebar
->border
.width
;
281 width
= c
->titlebar
->height
;
285 titlebar_geometry_compute(c
, c
->geometry
, &geom
);
287 c
->titlebar
->sw
= simplewindow_new(globalconf
.connection
, c
->phys_screen
, geom
.x
, geom
.y
,
288 geom
.width
, geom
.height
, c
->titlebar
->border
.width
);
290 if(c
->titlebar
->border
.width
)
291 xcb_change_window_attributes(globalconf
.connection
, c
->titlebar
->sw
->window
,
292 XCB_CW_BORDER_PIXEL
, &c
->titlebar
->border
.color
.pixel
);
294 if(client_isvisible(c
, c
->screen
))
295 globalconf
.screens
[c
->screen
].need_arrange
= true;
297 c
->titlebar
->need_update
= true;
300 /** Create a new titlebar.
301 * \param L The Lua VM state.
302 * \return The number of value pushed.
305 * \lparam A table with values: align, position, fg, bg, border_width,
306 * border_color, width and height.
307 * \lreturn A brand new titlebar.
310 luaA_titlebar_new(lua_State
*L
)
316 luaA_checktable(L
, 2);
318 tb
= p_new(titlebar_t
, 1);
320 buf
= luaA_getopt_lstring(L
, 2, "align", "left", &len
);
321 tb
->align
= draw_align_fromstr(buf
, len
);
323 tb
->width
= luaA_getopt_number(L
, 2, "width", 0);
324 tb
->height
= luaA_getopt_number(L
, 2, "height", 0);
326 /* 1.5 as default factor, it fits nice but no one knows why */
327 tb
->height
= 1.5 * globalconf
.font
->height
;
329 buf
= luaA_getopt_lstring(L
, 2, "position", "top", &len
);
330 tb
->position
= position_fromstr(buf
, len
);
332 tb
->colors
.fg
= globalconf
.colors
.fg
;
333 if((buf
= luaA_getopt_lstring(L
, 2, "fg", NULL
, &len
)))
334 xcolor_init(&tb
->colors
.fg
, globalconf
.connection
,
335 globalconf
.default_screen
, buf
, len
);
337 tb
->colors
.bg
= globalconf
.colors
.bg
;
338 if((buf
= luaA_getopt_lstring(L
, 2, "bg", NULL
, &len
)))
339 xcolor_init(&tb
->colors
.bg
, globalconf
.connection
,
340 globalconf
.default_screen
, buf
, len
);
342 tb
->border
.color
= globalconf
.colors
.fg
;
343 if((buf
= luaA_getopt_lstring(L
, 2, "border_color", NULL
, &len
)))
344 xcolor_init(&tb
->border
.color
, globalconf
.connection
,
345 globalconf
.default_screen
, buf
, len
);
347 tb
->border
.width
= luaA_getopt_number(L
, 2, "border_width", 0);
349 return luaA_titlebar_userdata_new(globalconf
.L
, tb
);
352 /** Add a widget to a titlebar.
353 * \param L The Lua VM state.
354 * \return The number of value pushed.
357 * \lvalue A titlebar.
361 luaA_titlebar_widget_add(lua_State
*L
)
363 titlebar_t
**tb
= luaA_checkudata(L
, 1, "titlebar");
364 widget_t
**widget
= luaA_checkudata(L
, 2, "widget");
365 widget_node_t
*witer
, *w
;
367 if((*widget
)->type
== systray_new
)
368 luaL_error(L
, "cannot add systray widget to titlebar");
370 /* check that there is not already a widget with that name in the titlebar */
371 for(witer
= (*tb
)->widgets
; witer
; witer
= witer
->next
)
372 if(witer
->widget
!= *widget
373 && !a_strcmp(witer
->widget
->name
, (*widget
)->name
))
374 luaL_error(L
, "a widget with name `%s' already on titlebar", witer
->widget
->name
);
376 w
= p_new(widget_node_t
, 1);
379 widget_node_list_append(&(*tb
)->widgets
, w
);
382 (*tb
)->need_update
= true;
387 /** Remove a widget from a titlebar.
388 * \param L The Lua VM State.
391 * \lvalue A statusbar.
395 luaA_titlebar_widget_remove(lua_State
*L
)
397 titlebar_t
**tb
= luaA_checkudata(L
, 1, "titlebar");
398 widget_t
**widget
= luaA_checkudata(L
, 2, "widget");
399 widget_node_t
*w
, *wnext
;
401 for(w
= (*tb
)->widgets
; w
; w
= wnext
)
404 if(w
->widget
== *widget
)
406 if((*widget
)->detach
)
407 (*widget
)->detach(*widget
, *tb
);
408 widget_unref(widget
);
409 widget_node_list_detach(&(*tb
)->widgets
, w
);
411 (*tb
)->need_update
= true;
419 /** Get all widgets from a titlebar.
420 * \param L The Lua VM state.
421 * \return The number of value pushed.
425 * \lreturn A table with all widgets from the titlebar.
428 luaA_titlebar_widget_get(lua_State
*L
)
430 titlebar_t
**tb
= luaA_checkudata(L
, 1, "titlebar");
431 widget_node_t
*witer
;
435 for(witer
= (*tb
)->widgets
; witer
; witer
= witer
->next
)
437 luaA_widget_userdata_new(L
, witer
->widget
);
438 /* ref again for the list */
439 widget_ref(&witer
->widget
);
440 lua_setfield(L
, -2, witer
->widget
->name
);
446 /** Get the client which the titlebar is attached to. That is a the same as
447 * checking if every clients's titlebar is equal to titlebar.
448 * \param L The Lua VM state.
451 * \lvalue A titlebar.
452 * \lreturn A client if the titlebar is attached, nil otherwise.
455 luaA_titlebar_client_get(lua_State
*L
)
457 titlebar_t
**titlebar
= luaA_checkudata(L
, 1, "titlebar");
460 if((c
= client_getbytitlebar(*titlebar
)))
461 return luaA_client_userdata_new(L
, c
);
466 /** Titlebar newindex.
467 * \param L The Lua VM state.
468 * \return The number of elements pushed on stack.
471 luaA_titlebar_newindex(lua_State
*L
)
474 titlebar_t
**titlebar
= luaA_checkudata(L
, 1, "titlebar");
475 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
479 switch(a_tokenize(attr
, len
))
482 if((buf
= luaL_checklstring(L
, 3, &len
)))
483 (*titlebar
)->align
= draw_align_fromstr(buf
, len
);
487 case A_TK_BORDER_WIDTH
:
488 if((i
= luaL_checknumber(L
, 3)) >= 0)
489 (*titlebar
)->border
.width
= i
;
493 case A_TK_BORDER_COLOR
:
494 if((buf
= luaL_checklstring(L
, 3, &len
)))
495 if(xcolor_init(&(*titlebar
)->border
.color
, globalconf
.connection
,
496 globalconf
.default_screen
, buf
, len
))
498 xcb_change_window_attributes(globalconf
.connection
, (*titlebar
)->sw
->window
,
499 XCB_CW_BORDER_PIXEL
, &(*titlebar
)->border
.color
.pixel
);
502 if((buf
= luaL_checklstring(L
, 3, &len
)))
503 if(xcolor_init(&(*titlebar
)->colors
.fg
, globalconf
.connection
,
504 globalconf
.default_screen
, buf
, len
))
505 (*titlebar
)->need_update
= true;
508 if((buf
= luaL_checklstring(L
, 3, &len
)))
509 if(xcolor_init(&(*titlebar
)->colors
.bg
, globalconf
.connection
,
510 globalconf
.default_screen
, buf
, len
))
511 (*titlebar
)->need_update
= true;
517 if((c
= client_getbytitlebar(*titlebar
))
518 && client_isvisible(c
, c
->screen
))
519 globalconf
.screens
[c
->screen
].need_arrange
= true;
525 * \param L The Lua VM state.
526 * \return The number of elements pushed on stack.
528 * \lfield align Alignment relative to the client.
529 * \lfield border_width Border width.
530 * \lfield border_color Border color.
531 * \lfield fg Foreground color.
532 * \lfield bg Background color.
535 luaA_titlebar_index(lua_State
*L
)
538 titlebar_t
**titlebar
= luaA_checkudata(L
, 1, "titlebar");
539 const char *attr
= luaL_checklstring(L
, 2, &len
);
541 if(luaA_usemetatable(L
, 1, 2))
544 switch(a_tokenize(attr
, len
))
547 lua_pushstring(L
, draw_align_tostr((*titlebar
)->align
));
549 case A_TK_BORDER_WIDTH
:
550 lua_pushnumber(L
, (*titlebar
)->border
.width
);
552 case A_TK_BORDER_COLOR
:
553 luaA_pushcolor(L
, &(*titlebar
)->border
.color
);
556 luaA_pushcolor(L
, &(*titlebar
)->colors
.fg
);
559 luaA_pushcolor(L
, &(*titlebar
)->colors
.bg
);
568 /** Convert a titlebar to a printable string.
569 * \param L The Lua VM state.
570 * \return The number of value pushed.
573 * \lvalue A titlebar.
577 luaA_titlebar_tostring(lua_State
*L
)
579 titlebar_t
**p
= luaA_checkudata(L
, 1, "titlebar");
580 lua_pushfstring(L
, "[titlebar udata(%p)]", *p
);
584 const struct luaL_reg awesome_titlebar_methods
[] =
586 { "__call", luaA_titlebar_new
},
589 const struct luaL_reg awesome_titlebar_meta
[] =
591 { "widget_add", luaA_titlebar_widget_add
},
592 { "widget_remove", luaA_titlebar_widget_remove
},
593 { "widget_get", luaA_titlebar_widget_get
},
594 { "client_get", luaA_titlebar_client_get
},
595 { "__index", luaA_titlebar_index
},
596 { "__newindex", luaA_titlebar_newindex
},
597 { "__eq", luaA_titlebar_eq
},
598 { "__gc", luaA_titlebar_gc
},
599 { "__tostring", luaA_titlebar_tostring
},
603 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80