infobar.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "wmfs.h"
  6. #include "draw.h"
  7. #include "infobar.h"
  8. #include "barwin.h"
  9. #include "util.h"
  10. #include "tag.h"
  11. static void infobar_elem_tag_init(struct element *e);
  12. static void infobar_elem_tag_update(struct element *e);
  13. const struct elem_funcs
  14. {
  15. char c;
  16. void (*func_init)(struct element *e);
  17. void (*func_update)(struct element *e);
  18. } elem_funcs[] =
  19. {
  20. { 't', infobar_elem_tag_init, infobar_elem_tag_update },
  21. /* { 'l', infobar_elem_layout_init, infobar_elem_layout_update },
  22. { 's', infobar_elem_selbar_init, infobar_elem_selbar_update },
  23. { 'S', infobar_elem_status_init, infobar_elem_status_update },*/
  24. { '\0', NULL, NULL }
  25. };
  26. static void
  27. infobar_elem_tag_init(struct element *e)
  28. {
  29. struct tag *t;
  30. struct barwin *b, *prev = NULL;
  31. struct geo g = { 0, 0, 0, 0 };
  32. int s, j;
  33. infobar_elem_placement(e);
  34. j = e->geo.x;
  35. e->geo.h -= (e->infobar->theme->tags_border_width << 1);
  36. TAILQ_FOREACH(t, &e->infobar->screen->tags, next)
  37. {
  38. s = draw_textw(e->infobar->theme, t->name) + PAD;
  39. /* Init barwin */
  40. b = barwin_new(e->infobar->bar->win, j, 0, s, e->geo.h, 0, 0, false);
  41. /* Set border */
  42. if(e->infobar->theme->tags_border_width)
  43. {
  44. XSetWindowBorder(W->dpy, b->win, e->infobar->theme->tags_border_col);
  45. XSetWindowBorderWidth(W->dpy, b->win, e->infobar->theme->tags_border_width);
  46. }
  47. b->ptr = (void*)t;
  48. barwin_map(b);
  49. /* TODO: refer to tag element configuration */
  50. barwin_mousebind_new(b, Button1, false, g, uicb_tag_set_with_name, (Uicb)t->name);
  51. barwin_mousebind_new(b, Button4, false, g, uicb_tag_next, NULL);
  52. barwin_mousebind_new(b, Button5, false, g, uicb_tag_prev, NULL);
  53. /* insert_tail with SLIST */
  54. if(SLIST_EMPTY(&e->bars))
  55. SLIST_INSERT_HEAD(&e->bars, b, enext);
  56. else
  57. SLIST_INSERT_AFTER(prev, b, enext);
  58. prev = b;
  59. j += s;
  60. }
  61. e->infobar->screen->elemupdate |= FLAGINT(ElemTag);
  62. e->geo.w = j;
  63. }
  64. static void
  65. infobar_elem_tag_update(struct element *e)
  66. {
  67. struct tag *t, *sel = e->infobar->screen->seltag;
  68. struct barwin *b;
  69. SLIST_FOREACH(b, &e->bars, enext)
  70. {
  71. t = (struct tag*)b->ptr;
  72. /* Selected */
  73. /* TODO: color from conf */
  74. if(t == sel)
  75. {
  76. b->fg = e->infobar->theme->tags_s.fg;
  77. b->bg = e->infobar->theme->tags_s.bg;
  78. }
  79. else
  80. {
  81. b->fg = e->infobar->theme->tags_n.fg;
  82. b->bg = e->infobar->theme->tags_n.bg;
  83. }
  84. barwin_refresh_color(b);
  85. draw_text(b->dr, e->infobar->theme, (PAD >> 1),
  86. TEXTY(e->infobar->theme, e->geo.h), b->fg, t->name);
  87. barwin_refresh(b);
  88. }
  89. }
  90. static void
  91. infobar_elem_init(struct infobar *i)
  92. {
  93. struct element *e;
  94. int n, j;
  95. TAILQ_INIT(&i->elements);
  96. for(n = 0; n < strlen(i->elemorder); ++n)
  97. {
  98. for(j = 0; j < LEN(elem_funcs); ++j)
  99. if(elem_funcs[j].c == i->elemorder[n])
  100. {
  101. e = xcalloc(1, sizeof(struct element));
  102. SLIST_INIT(&e->bars);
  103. e->infobar = i;
  104. e->type = j;
  105. e->func_init = elem_funcs[j].func_init;
  106. e->func_update = elem_funcs[j].func_update;
  107. TAILQ_INSERT_TAIL(&i->elements, e, next);
  108. e->func_init(e);
  109. break;
  110. }
  111. }
  112. }
  113. void
  114. infobar_elem_update(struct infobar *i)
  115. {
  116. struct element *e;
  117. TAILQ_FOREACH(e, &i->elements, next)
  118. if(i->screen->elemupdate & FLAGINT(e->type))
  119. e->func_update(e);
  120. }
  121. void
  122. infobar_elem_remove(struct element *e)
  123. {
  124. struct barwin *b;
  125. TAILQ_REMOVE(&e->infobar->elements, e, next);
  126. while(!SLIST_EMPTY(&e->bars))
  127. {
  128. b = SLIST_FIRST(&e->bars);
  129. SLIST_REMOVE_HEAD(&e->bars, enext);
  130. barwin_remove(b);
  131. }
  132. }
  133. struct infobar*
  134. infobar_new(struct screen *s, struct theme *theme, Barpos pos, const char *elem)
  135. {
  136. bool map;
  137. struct infobar *i = (struct infobar*)xcalloc(1, sizeof(struct infobar));
  138. i->screen = s;
  139. i->theme = theme;
  140. i->elemorder = xstrdup(elem);
  141. map = infobar_placement(i, pos);
  142. /* struct barwin create */
  143. i->bar = barwin_new(W->root, i->geo.x, i->geo.y, i->geo.w, i->geo.h,
  144. theme->bars.fg, theme->bars.bg, false);
  145. SLIST_INSERT_HEAD(&s->infobars, i, next);
  146. /* struct elements */
  147. infobar_elem_init(i);
  148. /* Render, only if pos is Top or Bottom */
  149. if(!map)
  150. return i;
  151. barwin_map(i->bar);
  152. barwin_map_subwin(i->bar);
  153. barwin_refresh_color(i->bar);
  154. infobar_refresh(i);
  155. return i;
  156. }
  157. void
  158. infobar_refresh(struct infobar *i)
  159. {
  160. infobar_elem_update(i);
  161. barwin_refresh(i->bar);
  162. }
  163. void
  164. infobar_remove(struct infobar *i)
  165. {
  166. struct element *e;
  167. free(i->elemorder);
  168. TAILQ_FOREACH(e, &i->elements, next)
  169. infobar_elem_remove(e);
  170. barwin_remove(i->bar);
  171. SLIST_REMOVE(&i->screen->infobars, i, infobar, next);
  172. free(i);
  173. }
  174. void
  175. infobar_free(struct screen *s)
  176. {
  177. struct infobar *i;
  178. while(!SLIST_EMPTY(&s->infobars))
  179. {
  180. i = SLIST_FIRST(&s->infobars);
  181. /* SLIST_REMOVE is done by infobar_remove */
  182. infobar_remove(i);
  183. }
  184. }