awesomerc.lua: use nano by default if no editor found in env
[awesome.git] / titlebar.c
blob130f9c13e3b5df0876d679f07de0117f7fc8be54
1 /*
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.
22 #include <xcb/xcb.h>
24 #include "titlebar.h"
25 #include "client.h"
26 #include "widget.h"
27 #include "wibox.h"
28 #include "screen.h"
30 extern awesome_t globalconf;
32 /** Get a client by its titlebar.
33 * \param titlebar The titlebar.
34 * \return A client.
36 client_t *
37 client_getbytitlebar(wibox_t *titlebar)
39 client_t *c;
41 for(c = globalconf.clients; c; c = c->next)
42 if(c->titlebar == titlebar)
43 return c;
45 return NULL;
48 /** Get a client by its titlebar window.
49 * \param win The window.
50 * \return A client.
52 client_t *
53 client_getbytitlebarwin(xcb_window_t win)
55 client_t *c;
57 for(c = globalconf.clients; c; c = c->next)
58 if(c->titlebar && c->titlebar->sw.window == win)
59 return c;
61 return NULL;
64 void
65 titlebar_geometry_compute(client_t *c, area_t geometry, area_t *res)
67 int width, x_offset = 0, y_offset = 0;
69 switch(c->titlebar->position)
71 default:
72 return;
73 case Top:
74 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->sw.border.width);
75 switch(c->titlebar->align)
77 default:
78 break;
79 case AlignRight:
80 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->sw.border.width;
81 break;
82 case AlignCenter:
83 x_offset = (geometry.width - width) / 2;
84 break;
86 res->x = geometry.x + x_offset;
87 res->y = geometry.y - c->titlebar->sw.geometry.height - 2 * c->titlebar->sw.border.width + c->border;
88 res->width = width;
89 res->height = c->titlebar->sw.geometry.height;
90 break;
91 case Bottom:
92 width = MAX(1, geometry.width + 2 * c->border - 2 * c->titlebar->sw.border.width);
93 switch(c->titlebar->align)
95 default:
96 break;
97 case AlignRight:
98 x_offset = 2 * c->border + geometry.width - width - 2 * c->titlebar->sw.border.width;
99 break;
100 case AlignCenter:
101 x_offset = (geometry.width - width) / 2;
102 break;
104 res->x = geometry.x + x_offset;
105 res->y = geometry.y + geometry.height + c->border;
106 res->width = width;
107 res->height = c->titlebar->sw.geometry.height;
108 break;
109 case Left:
110 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->sw.border.width);
111 switch(c->titlebar->align)
113 default:
114 break;
115 case AlignRight:
116 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->sw.border.width;
117 break;
118 case AlignCenter:
119 y_offset = (geometry.height - width) / 2;
120 break;
122 res->x = geometry.x - c->titlebar->sw.geometry.width + c->border;
123 res->y = geometry.y + y_offset;
124 res->width = c->titlebar->sw.geometry.width;
125 res->height = width;
126 break;
127 case Right:
128 width = MAX(1, geometry.height + 2 * c->border - 2 * c->titlebar->sw.border.width);
129 switch(c->titlebar->align)
131 default:
132 break;
133 case AlignRight:
134 y_offset = 2 * c->border + geometry.height - width - 2 * c->titlebar->sw.border.width;
135 break;
136 case AlignCenter:
137 y_offset = (geometry.height - width) / 2;
138 break;
140 res->x = geometry.x + geometry.width + c->border;
141 res->y = geometry.y + y_offset;
142 res->width = c->titlebar->sw.geometry.width;
143 res->height = width;
144 break;
148 /** Create a new titlebar (DEPRECATED).
149 * \param L The Lua VM state.
150 * \return The number of value pushed.
152 * \luastack
153 * \lparam A table with values: align, position, fg, bg, border_width,
154 * border_color, width and height.
155 * \lreturn A brand new titlebar.
157 static int
158 luaA_titlebar_new(lua_State *L)
160 deprecate();
162 return luaA_wibox_new(L);
165 /** Detach a wibox titlebar from its client.
166 * \param c The client.
168 void
169 titlebar_client_detach(client_t *c)
171 /* If client has a titlebar, kick it out. */
172 if(c && c->titlebar)
174 wibox_unref(&c->titlebar);
175 simplewindow_wipe(&c->titlebar->sw);
176 c->titlebar->type = WIBOX_TYPE_NORMAL;
177 c->titlebar->screen = SCREEN_UNDEF;
178 c->titlebar = NULL;
179 client_need_arrange(c);
180 client_stack();
184 /** Attach a wibox to a client as its titlebar.
185 * \param c The client.
186 * \param t The wibox/titlebar.
188 void
189 titlebar_client_attach(client_t *c, wibox_t *t)
191 titlebar_client_detach(c);
193 if(c && t)
195 area_t wingeom;
197 /* check if titlebar is already on a client */
198 titlebar_client_detach(client_getbytitlebar(t));
200 c->titlebar = wibox_ref(&t);
201 t->type = WIBOX_TYPE_TITLEBAR;
202 t->screen = c->screen;
204 switch(t->position)
206 case Floating:
207 t->position = Top;
208 case Top:
209 case Bottom:
210 if(!t->sw.geometry.height)
211 t->sw.geometry.height = 1.5 * globalconf.font->height;
212 break;
213 case Left:
214 case Right:
215 if(!t->sw.geometry.width)
216 t->sw.geometry.width = 1.5 * globalconf.font->height;
217 break;
221 titlebar_geometry_compute(c, c->geometry, &wingeom);
223 simplewindow_init(&t->sw, c->phys_screen,
224 wingeom, 0, t->sw.orientation,
225 &t->sw.ctx.fg, &t->sw.ctx.bg);
226 simplewindow_border_color_set(&t->sw, &t->sw.border.color);
228 t->need_update = true;
230 if(t->isvisible)
231 xcb_map_window(globalconf.connection, t->sw.window);
233 client_need_arrange(c);
234 client_stack();
238 /** Titlebar newindex.
239 * \param L The Lua VM state.
240 * \param titlebar The wibox titlebar.
241 * \param tok The attribute token.
242 * \return The number of elements pushed on stack.
245 luaA_titlebar_newindex(lua_State *L, wibox_t *titlebar, awesome_token_t tok)
247 client_t *c = NULL;
249 switch(tok)
251 position_t position;
252 int i;
253 size_t len;
254 const char *buf;
256 case A_TK_ALIGN:
257 if((buf = luaL_checklstring(L, 3, &len)))
258 titlebar->align = draw_align_fromstr(buf, len);
259 else
260 return 0;
261 break;
262 case A_TK_BORDER_WIDTH:
263 if((i = luaL_checknumber(L, 3)) >= 0)
264 simplewindow_border_width_set(&titlebar->sw, i);
265 else
266 return 0;
267 break;
268 case A_TK_BORDER_COLOR:
269 if((buf = luaL_checklstring(L, 3, &len)))
270 if(xcolor_init_reply(xcolor_init_unchecked(&titlebar->sw.border.color, buf, len)))
271 simplewindow_border_color_set(&titlebar->sw, &titlebar->sw.border.color);
272 return 0;
273 case A_TK_POSITION:
274 buf = luaL_checklstring(L, 3, &len);
275 position = position_fromstr(buf, len);
276 if(position != titlebar->position)
278 switch(position)
280 case Left:
281 switch(titlebar->position)
283 int tmp;
284 case Left:
285 case Right:
286 break;
287 case Top:
288 case Bottom:
289 case Floating:
290 tmp = titlebar->sw.geometry.width;
291 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
292 titlebar->sw.geometry.height = tmp;
293 break;
295 simplewindow_orientation_set(&titlebar->sw, North);
296 break;
297 case Right:
298 switch(titlebar->position)
300 int tmp;
301 case Left:
302 case Right:
303 break;
304 case Top:
305 case Bottom:
306 case Floating:
307 tmp = titlebar->sw.geometry.width;
308 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
309 titlebar->sw.geometry.height = tmp;
310 break;
312 simplewindow_orientation_set(&titlebar->sw, South);
313 break;
314 case Top:
315 case Bottom:
316 case Floating:
317 switch(titlebar->position)
319 int tmp;
320 case Left:
321 case Right:
322 tmp = titlebar->sw.geometry.width;
323 titlebar->sw.geometry.width = titlebar->sw.geometry.height;
324 titlebar->sw.geometry.height = tmp;
325 break;
326 case Top:
327 case Bottom:
328 case Floating:
329 break;
331 simplewindow_orientation_set(&titlebar->sw, East);
332 break;
334 titlebar->position = position;
335 if((c = client_getbytitlebar(titlebar)))
337 titlebar_update_geometry_floating(c);
338 /* call geometry hook for client because some like to
339 * set titlebar width in that hook, which make sense */
340 hooks_property(c, "geometry");
343 break;
344 default:
345 return 0;
348 if((c || (c = client_getbytitlebar(titlebar))))
349 client_need_arrange(c);
351 return 0;
354 const struct luaL_reg awesome_titlebar_methods[] =
356 { "__call", luaA_titlebar_new },
357 { NULL, NULL }
359 const struct luaL_reg awesome_titlebar_meta[] =
361 { NULL, NULL }
364 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80