status.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * status.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. /** Check rectangles blocks in str and return properties
  34. * --> \b[x;y;width;height;#color]\
  35. *\param r StatusRec pointer, rectangles properties
  36. *\param str String
  37. *\return n Length of r
  38. */
  39. int
  40. statustext_rectangle(StatusRec *r, char *str)
  41. {
  42. char as;
  43. int n, i, j, k;
  44. for(i = j = n = 0; i < strlen(str); ++i, ++j)
  45. if(sscanf(&str[i], "\\b[%d;%d;%d;%d;#%x]%c", &r[n].x, &r[n].y, &r[n].w, &r[n].h, &r[n].color, &as) == 6
  46. && as == '\\')
  47. for(++n, ++i, --j; str[i] != as || str[i - 1] != ']'; ++i);
  48. else if(j != i)
  49. str[j] = str[i];
  50. for(k = j; k < i; str[k++] = 0);
  51. return n;
  52. }
  53. /** Check text blocks in str and return properties
  54. * --> \s[x;y;#color;text]\
  55. *\param s StatusText pointer, text properties
  56. *\param str String
  57. *\return n Length of s
  58. */
  59. int
  60. statustext_text(StatusText *s, char *str)
  61. {
  62. char as;
  63. int n, i, j, k;
  64. for(i = j = n = 0; i < strlen(str); ++i, ++j)
  65. if(sscanf(&str[i], "\\s[%d;%d;%7[^;];%512[^]]]%c", &s[n].x, &s[n].y, s[n].color, s[n].text, &as) == 5
  66. && as == '\\')
  67. for(++n, ++i, --j; str[i] != as || str[i - 1] != ']'; ++i);
  68. else if(j != i)
  69. str[j] = str[i];
  70. for(k = j; k < i; str[k++] = 0);
  71. return n;
  72. }
  73. #ifdef HAVE_IMLIB
  74. /** Check images blocks in str and return properties
  75. * --> \i[x;y;w;h;name]\
  76. *\param im ImageAttr pointer, image properties
  77. *\param str String
  78. *\return n Lenght of i
  79. */
  80. int
  81. statustext_image(ImageAttr *im, char *str)
  82. {
  83. char as;
  84. int n, i, j, k;
  85. for(i = j = n = 0; i < strlen(str); ++i, ++j)
  86. if(sscanf(&str[i], "\\i[%d;%d;%d;%d;%512[^]]]%c", &im[n].x, &im[n].y, &im[n].w, &im[n].h, im[n].name, &as) == 6
  87. && as == '\\')
  88. for(++n, ++i, --j; str[i] != as || str[i - 1] != ']'; ++i);
  89. else if(j != i)
  90. str[j] = str[i];
  91. for(k = j; k < i; str[k++] = 0);
  92. return n;
  93. }
  94. #endif /* HAVE_IMLIB */
  95. /** Draw normal text and colored normal text
  96. * --> \#color\ text in color
  97. *\param sc Screen
  98. *\param str String
  99. */
  100. void
  101. statustext_normal(int sc, char *str)
  102. {
  103. char strwc[MAXSTATUS] = { 0 };
  104. char buf[MAXSTATUS] = { 0 };
  105. char col[8] = { 0 };
  106. int n, i, j, k;
  107. for(i = j = n = 0; i < strlen(str); ++i, ++j)
  108. if(str[i] == '\\' && str[i + 1] == '#' && str[i + 8] == '\\')
  109. {
  110. ++n;
  111. i += 8;
  112. --j;
  113. }
  114. else
  115. strwc[j] = str[i];
  116. /* Draw normal text without any blocks */
  117. draw_text(infobar[sc].bar->dr, (sgeo[sc].width - SHADH) - textw(strwc),
  118. FHINFOBAR, infobar[sc].bar->fg, 0, strwc);
  119. if(n)
  120. {
  121. strcpy(buf, strwc);
  122. for(i = k = 0; i < strlen(str); ++i, ++k)
  123. if(str[i] == '\\' && str[i + 1] == '#' && str[i + 8] == '\\')
  124. {
  125. /* Store current color in col[] */
  126. for(j = 0, ++i; str[i] != '\\'; col[j++] = str[i++]);
  127. /* Draw a rectangle with the bar color to draw the text properly */
  128. draw_rectangle(infobar[sc].bar->dr, (sgeo[sc].width - SHADH) - textw(&buf[k]),
  129. 0, INFOBARH - (sgeo[sc].width - SHADH) - textw(&buf[k]),
  130. INFOBARH, conf.colors.bar);
  131. /* Draw text with its color */
  132. draw_text(infobar[sc].bar->dr, (sgeo[sc].width - SHADH) - textw(&buf[k]),
  133. FHINFOBAR, col, 0, &buf[k]);
  134. strcpy(buf, strwc);
  135. ++i;
  136. }
  137. }
  138. return;
  139. }
  140. /** Handle statustext and draw all things in infobar of specified screen
  141. *\param sc Screen number
  142. *\param str String
  143. */
  144. void
  145. statustext_handle(int sc, char *str)
  146. {
  147. char *lastst;
  148. int i, nr, ns, len;
  149. StatusRec r[128];
  150. StatusText s[128];
  151. /* If the str == the current statustext, return (not needed) */
  152. if(!str)
  153. return;
  154. barwin_refresh_color(infobar[sc].bar);
  155. /* save last status text address (for free at the end) */
  156. lastst = infobar[sc].statustext;
  157. infobar[sc].statustext = _strdup(str);
  158. len = ((strlen(str) > MAXSTATUS) ? MAXSTATUS : strlen(str));
  159. /* Store rectangles, located text & images properties. */
  160. nr = statustext_rectangle(r, str);
  161. ns = statustext_text(s, str);
  162. /* Draw normal text (and possibly colored with \#color\ blocks) */
  163. statustext_normal(sc, str);
  164. /* Draw rectangles with stored properties. */
  165. for(i = 0; i < nr; ++i)
  166. draw_rectangle(infobar[sc].bar->dr, r[i].x, r[i].y, r[i].w, r[i].h, r[i].color);
  167. /* Draw located text with stored properties. */
  168. for(i = 0; i < ns; ++i)
  169. draw_text(infobar[sc].bar->dr, s[i].x, s[i].y, s[i].color, 0, s[i].text);
  170. barwin_refresh(infobar[sc].bar);
  171. free(lastst);
  172. return;
  173. }