barwin.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * barwin.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. /** Create a BarWindow
  34. * \param parent Parent window of the BarWindow
  35. * \param x X position
  36. * \param y Y position
  37. * \param w BarWindow Width
  38. * \param h BarWindow Height
  39. * \param color BarWindow color
  40. * \param entermask Bool for know if the EnterMask mask is needed
  41. * \return The BarWindow pointer
  42. */
  43. BarWindow*
  44. barwin_create(Window parent,
  45. int x,
  46. int y,
  47. uint w,
  48. uint h,
  49. uint bg,
  50. char *fg,
  51. Bool entermask,
  52. Bool stipple,
  53. Bool border)
  54. {
  55. XSetWindowAttributes at;
  56. BarWindow *bw;
  57. /* Allocate memory */
  58. bw = emalloc(1, sizeof(BarWindow));
  59. /* Barwin attributes */
  60. at.override_redirect = True;
  61. at.background_pixmap = ParentRelative;
  62. at.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  63. |ButtonMask|MouseMask
  64. |ExposureMask|VisibilityChangeMask
  65. |StructureNotifyMask|SubstructureRedirectMask;
  66. if(entermask)
  67. at.event_mask |= EnterWindowMask|LeaveWindowMask|FocusChangeMask;
  68. /* Create window */
  69. bw->win = XCreateWindow(dpy, parent, x, y, w, h, 0, DefaultDepth(dpy, SCREEN),
  70. CopyFromParent, DefaultVisual(dpy, SCREEN),
  71. CWOverrideRedirect | CWBackPixmap | CWEventMask, &at);
  72. bw->dr = XCreatePixmap(dpy, parent, w, h, DefaultDepth(dpy, SCREEN));
  73. /* His border */
  74. if(border)
  75. {
  76. bw->bord = True;
  77. CWIN(bw->border.left, bw->win, 0, 0, SHADH, h, 0, CWBackPixel, color_enlight(bg), &at);
  78. CWIN(bw->border.top, bw->win, 0, 0, w, SHADH, 0, CWBackPixel, color_enlight(bg), &at);
  79. CWIN(bw->border.bottom, bw->win, 0, h - SHADH, w, SHADH, 0, CWBackPixel, SHADC, &at);
  80. CWIN(bw->border.right, bw->win, w - SHADH, 0, SHADH, h, 0, CWBackPixel, SHADC, &at);
  81. }
  82. /* Property */
  83. bw->geo.x = x;
  84. bw->geo.y = y;
  85. bw->geo.width = w;
  86. bw->geo.height = h;
  87. bw->bg = bg;
  88. bw->fg = fg;
  89. bw->border.light = color_enlight(bg);
  90. bw->border.dark = SHADC;
  91. bw->stipple = stipple;
  92. bw->stipple_color = -1;
  93. return bw;
  94. }
  95. /** Draw text in a Barwindow
  96. */
  97. void
  98. barwin_draw_text(BarWindow *bw, int x, int y, char *text)
  99. {
  100. if(!text)
  101. return;
  102. /* Background color of the text if there is stipple */
  103. if(bw->stipple)
  104. draw_rectangle(bw->dr, x - 4, 0, textw(text) + 8, bw->geo.height, bw->bg);
  105. /* Draw text */
  106. draw_text(bw->dr, x, y, bw->fg, 0, text);
  107. barwin_refresh(bw);
  108. return;
  109. }
  110. /** Delete a BarWindow
  111. * \param bw BarWindow pointer
  112. */
  113. void
  114. barwin_delete(BarWindow *bw)
  115. {
  116. CHECK(bw);
  117. XSelectInput(dpy, bw->win, NoEventMask);
  118. XDestroyWindow(dpy, bw->win);
  119. XFreePixmap(dpy, bw->dr);
  120. free(bw);
  121. return;
  122. }
  123. /** Delete the BarWindow sub windows
  124. * \param bw BarWindow pointer
  125. */
  126. void
  127. barwin_delete_subwin(BarWindow *bw)
  128. {
  129. CHECK(bw);
  130. XDestroySubwindows(dpy, bw->win);
  131. return;
  132. }
  133. /** Map a BarWindow
  134. * \param bw BarWindow pointer
  135. */
  136. void
  137. barwin_map(BarWindow *bw)
  138. {
  139. CHECK(!bw->mapped);
  140. XMapWindow(dpy, bw->win);
  141. bw->mapped = True;
  142. return;
  143. }
  144. /** Map the subwindows of a BarWindow
  145. * Use for the BarWindow special border...
  146. * \param bw BarWindow pointer
  147. */
  148. void
  149. barwin_map_subwin(BarWindow *bw)
  150. {
  151. CHECK(bw);
  152. XMapSubwindows(dpy, bw->win);
  153. return;
  154. }
  155. /** Unmap a BarWindow
  156. * \param bw BarWindow pointer
  157. */
  158. void
  159. barwin_unmap(BarWindow *bw)
  160. {
  161. CHECK(bw->mapped);
  162. XUnmapWindow(dpy, bw->win);
  163. bw->mapped = False;
  164. return;
  165. }
  166. /** Unmap the BarWindow sub windows
  167. * \param bw BarWindow pointer
  168. */
  169. void
  170. barwin_unmap_subwin(BarWindow *bw)
  171. {
  172. CHECK(bw);
  173. XUnmapSubwindows(dpy, bw->win);
  174. return;
  175. }
  176. /** Move a BarWindow
  177. * \param bw BarWindow pointer
  178. * \param x X position
  179. * \param y Y poistion
  180. */
  181. void
  182. barwin_move(BarWindow *bw, int x, int y)
  183. {
  184. CHECK(bw);
  185. bw->geo.x = x;
  186. bw->geo.y = y;
  187. XMoveWindow(dpy, bw->win, x, y);
  188. return;
  189. }
  190. /** Resize a BarWindow
  191. * \param bw BarWindow pointer
  192. * \param w Width
  193. * \param h Height
  194. */
  195. void
  196. barwin_resize(BarWindow *bw, uint w, uint h)
  197. {
  198. CHECK(bw);
  199. bw->geo.width = w;
  200. bw->geo.height = h;
  201. XFreePixmap(dpy, bw->dr);
  202. /* Frame */
  203. bw->dr = XCreatePixmap(dpy, ROOT,
  204. w - ((bw->bord) ? SHADH : 0),
  205. h - ((bw->bord) ? SHADH : 0),
  206. DefaultDepth(dpy, SCREEN));
  207. XResizeWindow(dpy, bw->win, w, h);
  208. /* Border */
  209. if(bw->bord)
  210. {
  211. XResizeWindow(dpy, bw->border.left, SHADH, h);
  212. XResizeWindow(dpy, bw->border.top, w, SHADH);
  213. XResizeWindow(dpy, bw->border.bottom, w, SHADH);
  214. XMoveResizeWindow(dpy, bw->border.right, w - SHADH, 0, SHADH, h);
  215. }
  216. return;
  217. }
  218. /** Refresh the BarWindow Color
  219. * \param bw BarWindow pointer
  220. */
  221. void
  222. barwin_refresh_color(BarWindow *bw)
  223. {
  224. CHECK(bw);
  225. draw_rectangle(bw->dr, 0, 0, bw->geo.width, bw->geo.height, bw->bg);
  226. if(bw->stipple)
  227. {
  228. XSetForeground(dpy, gc_stipple, ((bw->stipple_color != -1) ? bw->stipple_color : getcolor(bw->fg)));
  229. XFillRectangle(dpy, bw->dr, gc_stipple, 3, 2, bw->geo.width - 6, bw->geo.height - 4);
  230. }
  231. if(bw->bord)
  232. {
  233. XSetWindowBackground(dpy, bw->border.left, bw->border.light);
  234. XSetWindowBackground(dpy, bw->border.top, bw->border.light);
  235. XSetWindowBackground(dpy, bw->border.bottom, bw->border.dark);
  236. XSetWindowBackground(dpy, bw->border.right, bw->border.dark);
  237. XClearWindow(dpy, bw->border.left);
  238. XClearWindow(dpy, bw->border.top);
  239. XClearWindow(dpy, bw->border.bottom);
  240. XClearWindow(dpy, bw->border.right);
  241. }
  242. return;
  243. }
  244. /** Refresh the BarWindow
  245. * \param bw BarWindow pointer
  246. */
  247. void
  248. barwin_refresh(BarWindow *bw)
  249. {
  250. if(!bw || !bw->dr || !bw->win)
  251. return;
  252. XCopyArea(dpy, bw->dr, bw->win, gc, 0, 0, bw->geo.width, bw->geo.height, 0, 0);
  253. return;
  254. }