label.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <mouse.h>
  6. #include <jay.h>
  7. #include "fns.h"
  8. void _hoverLabel(Widget *w, Mouse *m);
  9. void _unhoverLabel(Widget *w);
  10. void _drawLabel(Widget *w, Image *dst);
  11. void _redrawLabel(Widget *w);
  12. void _setTextLabel(Label *l, const char *t);
  13. char *_getTextLabel(Label *l);
  14. static Rectangle autosizeLabel(Widget *w);
  15. void _clickLabel(Widget *w, Mouse *m);
  16. void _dclickLabel(Widget *w, Mouse *m);
  17. void _mPressDownLabel(Widget *w, Mouse *m);
  18. void _mPressUpLabel(Widget *w, Mouse *m);
  19. void _changeLabel(Widget *w);
  20. void _freeLabel(Widget *w);
  21. Widget *
  22. createLabel(char *id, int height, int width){
  23. Label *laux = malloc(sizeof(Label));
  24. if (laux == nil){
  25. return nil;
  26. }
  27. Widget *w = createWidget(id, height, width, LABEL, laux);
  28. if (w == nil){
  29. return nil;
  30. }
  31. laux->f = openfont(display, jayconfig->fontPath);
  32. if (laux->f == nil){
  33. return nil;
  34. }
  35. laux->t = nil;
  36. laux->backColor = jayconfig->mainBackColor;
  37. laux->textColor = jayconfig->mainTextColor;
  38. laux->border = createBorder(0, 0, 0);
  39. laux->w = w;
  40. laux->gettext=_getTextLabel;
  41. laux->setText=_setTextLabel;
  42. w->_hover = _hoverLabel;
  43. w->_unhover = _unhoverLabel;
  44. w->_draw=_drawLabel;
  45. w->_redraw=_redrawLabel;
  46. w->_resize=_simpleResize;
  47. w->_click=_clickLabel;
  48. w->_dclick=_dclickLabel;
  49. w->_mpressdown=_mPressDownLabel;
  50. w->_mpressup=_mPressUpLabel;
  51. w->_change=_changeLabel;
  52. w->freeWidget=_freeLabel;
  53. return w;
  54. }
  55. static int
  56. checkLabel(Widget *w){
  57. if (w == nil || w->t != LABEL || w->w == nil){
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. void
  63. _hoverLabel(Widget *w, Mouse *m){
  64. if(!checkLabel(w)){
  65. return;
  66. }
  67. w->hovered=1;
  68. if( w->hover != nil){
  69. w->hover(w);
  70. w->_redraw(w);
  71. }
  72. }
  73. void
  74. _unhoverLabel(Widget *w){
  75. if(!checkLabel(w)){
  76. return;
  77. }
  78. w->hovered = 0;
  79. if (w->unhover != nil){
  80. w->unhover(w);
  81. w->_redraw(w);
  82. }
  83. }
  84. void
  85. _drawLabel(Widget *w, Image *dst){
  86. if(!checkLabel(w) || w->father == nil || !w->visible || !w->father->visible){
  87. return;
  88. }
  89. Label *l = w->w;
  90. if(w->i != nil){
  91. freeimage(w->i);
  92. }
  93. if (w->height == 0 || w->width == 0){
  94. return;
  95. }
  96. if(w->height<0 || w->width<0){
  97. w->r = autosizeLabel(w);
  98. }
  99. w->i = allocimage(display, w->r, RGB24, 1, l->backColor);
  100. if (w->i == nil){
  101. sysfatal("_drawLabel: %r");
  102. }
  103. if(l->t != nil){
  104. Image *i = allocimage(display, Rect(0,0,1,1), RGB24, 1, l->textColor);
  105. Rectangle r = insetrect(w->r, l->border.size);
  106. Point s = stringsize(l->f, l->t);
  107. Point p = Pt(r.min.x, r.min.y + ((Dy(r)/2) - (s.y/2) + 1) );
  108. string(w->i, p, i, p, l->f, l->t);
  109. freeimage(i);
  110. }
  111. if (l->border.size > 0){
  112. if (l->border._3d){
  113. Image *i=allocimage(display, Rect(0,0,1,1), RGB24, 1, 0xCCCCCCFF);
  114. if(l->border.up){
  115. border3d(w->i, w->r, l->border.size, i, display->black, ZP);
  116. } else {
  117. border3d(w->i, w->r, l->border.size, display->black, i, ZP);
  118. }
  119. freeimage(i);
  120. } else {
  121. border(w->i, w->r, l->border.size, display->black, ZP);
  122. }
  123. }
  124. draw(dst, w->r, w->i, nil, w->p);
  125. if(w->draw != nil){
  126. w->draw(w);
  127. }
  128. flushimage(display, 1);
  129. }
  130. void
  131. _redrawLabel(Widget *w){
  132. if(!checkLabel(w) || w->father == nil || !w->visible || !w->father->visible){
  133. return;
  134. }
  135. if (w->autosize){
  136. Rectangle r = autosizeLabel(w);
  137. if(!eqrect(r, w->r)){
  138. // The geometry has changed, so we need to redraw all
  139. w->father->_redraw(w->father);
  140. }
  141. }
  142. w->_draw(w, screen);
  143. }
  144. void
  145. _setTextLabel(Label *l, const char *t){
  146. if (l->t != nil){
  147. if(strcmp(l->t, t) == 0){
  148. // If the text is equal, do nothing
  149. return;
  150. }
  151. free(l->t);
  152. }
  153. l->t=strdup(t);
  154. l->w->_change(l->w);
  155. l->w->_redraw(l->w);
  156. }
  157. char *
  158. _getTextLabel(Label *l){
  159. return strdup(l->t);
  160. }
  161. static Rectangle
  162. autosizeLabel(Widget *w){
  163. int fh, fw; //Final heigh, final width
  164. if(!checkLabel(w)){
  165. return ZR;
  166. }
  167. Label *l = w->w;
  168. fh = w->height;
  169. fw = w->width;
  170. if (l->t == nil || strcmp("", l->t) == 0){
  171. if (fh<0){
  172. fh=20;
  173. }
  174. if (fw<0){
  175. fw=12;
  176. }
  177. } else {
  178. Point p = stringsize(l->f, l->t);
  179. if (fh<0){
  180. fh = p.y + (2*l->border.size); //2*border because is a box, we have 2 sides in this axis.
  181. }
  182. if(fw<0){
  183. fw = p.x + (2*l->border.size); //2*border because is a box, we have 2 sides in this axis.
  184. }
  185. }
  186. return Rect(w->p.x, w->p.y, w->p.x + fw, w->p.y + fh);
  187. }
  188. void
  189. _clickLabel(Widget *w, Mouse *m){
  190. if(!checkLabel(w)){
  191. return;
  192. }
  193. if(w->click != nil){
  194. w->click(w, m);
  195. w->_redraw(w);
  196. }
  197. }
  198. void
  199. _dclickLabel(Widget *w, Mouse *m){
  200. if(!checkLabel(w)){
  201. return;
  202. }
  203. if(w->dclick != nil){
  204. w->dclick(w, m);
  205. w->_redraw(w);
  206. }
  207. }
  208. void
  209. _mPressDownLabel(Widget *w, Mouse *m){
  210. if(!checkLabel(w)){
  211. return;
  212. }
  213. if(w->mpressdown != nil){
  214. w->mpressdown(w, m);
  215. w->_redraw(w);
  216. }
  217. }
  218. void
  219. _mPressUpLabel(Widget *w, Mouse *m){
  220. if(!checkLabel(w)){
  221. return;
  222. }
  223. if(w->mpressup != nil){
  224. w->mpressup(w, m);
  225. w->_redraw(w);
  226. }
  227. }
  228. void
  229. _changeLabel(Widget *w){
  230. if(!checkLabel(w)){
  231. return;
  232. }
  233. if (w->change != nil){
  234. w->change(w);
  235. }
  236. }
  237. void
  238. _freeLabel(Widget *w){
  239. if (!checkLabel(w)){
  240. return;
  241. }
  242. Label *l = w->w;
  243. free(l->t);
  244. freefont(l->f);
  245. free(l);
  246. w->w=nil;
  247. freeWidget(w);
  248. }