scribble.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <mouse.h>
  6. #include <keyboard.h>
  7. #include <control.h>
  8. #include <scribble.h>
  9. typedef struct Scrib Scrib;
  10. struct Scrib
  11. {
  12. Control;
  13. int border;
  14. CImage *image;
  15. CImage *color;
  16. CImage *bordercolor;
  17. CFont *font;
  18. int align;
  19. int lastbut;
  20. char lastchar[8];
  21. Scribble *scrib;
  22. };
  23. enum{
  24. EAlign,
  25. EBorder,
  26. EBordercolor,
  27. EFocus,
  28. EFont,
  29. EHide,
  30. EImage,
  31. ELinecolor,
  32. ERect,
  33. EReveal,
  34. EShow,
  35. ESize,
  36. };
  37. static char *cmds[] = {
  38. [EAlign] = "align",
  39. [EBorder] = "border",
  40. [EBordercolor] ="bordercolor",
  41. [EFocus] = "focus",
  42. [EFont] = "font",
  43. [EHide] = "hide",
  44. [EImage] = "image",
  45. [ELinecolor] = "linecolor",
  46. [ERect] = "rect",
  47. [EReveal] = "reveal",
  48. [EShow] = "show",
  49. [ESize] = "size",
  50. nil
  51. };
  52. static void scribshow(Scrib*);
  53. static void scribchar(Scrib*, Rune);
  54. static void resetstroke(Scrib *w);
  55. static void displaystroke(Scrib *w);
  56. static void displaylast(Scrib *w);
  57. static void addpoint(Scrib *w, Point p);
  58. static void
  59. scribmouse(Control *c, Mouse *m)
  60. {
  61. Scrib *b;
  62. Rune r;
  63. b = (Scrib*)c;
  64. if (m->buttons & 0x1) {
  65. if ((b->lastbut & 0x1) == 0) {
  66. /* mouse went down */
  67. resetstroke(b);
  68. }
  69. /* mouse is down */
  70. if (ptinrect(m->xy,b->rect))
  71. addpoint(b, m->xy);
  72. } else if (b->lastbut & 0x1) {
  73. /* mouse went up */
  74. if (ptinrect(m->xy,b->rect)) {
  75. r = recognize(b->scrib);
  76. scribchar(b, r);
  77. scribshow(b);
  78. if (r) chanprint(b->event, b->format, b->name, r);
  79. }
  80. }
  81. b->lastbut = m->buttons;
  82. }
  83. static void
  84. scribfree(Control *c)
  85. {
  86. Scrib *b;
  87. b = (Scrib*)c;
  88. _putctlimage(b->image);
  89. _putctlimage(b->color);
  90. _putctlimage(b->bordercolor);
  91. _putctlfont(b->font);
  92. // scribblefree(b->scrib);
  93. }
  94. static void
  95. scribchar(Scrib *b, Rune r)
  96. {
  97. if(r == 0)
  98. b->lastchar[0] = '\0';
  99. else if(r == ' ')
  100. strcpy(b->lastchar, "' '");
  101. else if(r < ' ')
  102. sprint(b->lastchar, "ctl-%c", r+'@');
  103. else
  104. sprint(b->lastchar, "%C", r);
  105. }
  106. static void
  107. scribshow(Scrib *b)
  108. {
  109. Image *i;
  110. Rectangle r;
  111. char *mode;
  112. Scribble *s = b->scrib;
  113. char buf[32];
  114. if (b->hidden)
  115. return;
  116. if(b->border > 0){
  117. r = insetrect(b->rect, b->border);
  118. border(b->screen, b->rect, b->border, b->bordercolor->image, ZP);
  119. }else
  120. r = b->rect;
  121. i = b->image->image;
  122. draw(b->screen, r, i, nil, i->r.min);
  123. if (s->ctrlShift)
  124. mode = " ^C";
  125. else if (s->puncShift)
  126. mode = " #&^";
  127. else if (s->curCharSet == CS_DIGITS)
  128. mode = " 123";
  129. else if (s->capsLock)
  130. mode = " ABC";
  131. else if (s->tmpShift)
  132. mode = " Abc";
  133. else
  134. mode = " abc";
  135. snprint(buf, sizeof buf, "%s %s", mode, b->lastchar);
  136. string(b->screen, r.min, b->color->image, ZP, b->font->font, buf);
  137. flushimage(display, 1);
  138. }
  139. static void
  140. scribctl(Control *c, CParse *cp)
  141. {
  142. int cmd;
  143. Rectangle r;
  144. Scrib *b;
  145. b = (Scrib*)c;
  146. cmd = _ctllookup(cp->args[0], cmds, nelem(cmds));
  147. switch(cmd){
  148. default:
  149. abort();
  150. ctlerror("%q: unrecognized message '%s'", b->name, cp->str);
  151. break;
  152. case EAlign:
  153. _ctlargcount(b, cp, 2);
  154. b->align = _ctlalignment(cp->args[1]);
  155. break;
  156. case EBorder:
  157. _ctlargcount(b, cp, 2);
  158. if(cp->iargs[1] < 0)
  159. ctlerror("%q: bad border: %c", b->name, cp->str);
  160. b->border = cp->iargs[1];
  161. break;
  162. case EBordercolor:
  163. _ctlargcount(b, cp, 2);
  164. _setctlimage(b, &b->bordercolor, cp->args[1]);
  165. break;
  166. case EFocus:
  167. break;
  168. case EImage:
  169. _ctlargcount(b, cp, 2);
  170. _setctlimage(b, &b->image, cp->args[1]);
  171. break;
  172. case ELinecolor:
  173. _ctlargcount(b, cp, 2);
  174. _setctlimage(b, &b->bordercolor, cp->args[1]);
  175. break;
  176. case ERect:
  177. _ctlargcount(b, cp, 5);
  178. r.min.x = cp->iargs[1];
  179. r.min.y = cp->iargs[2];
  180. r.max.x = cp->iargs[3];
  181. r.max.y = cp->iargs[4];
  182. if(Dx(r)<0 || Dy(r)<0)
  183. ctlerror("%q: bad rectangle: %s", b->name, cp->str);
  184. b->rect = r;
  185. break;
  186. case EReveal:
  187. _ctlargcount(b, cp, 1);
  188. b->hidden = 0;
  189. scribshow(b);
  190. break;
  191. case EShow:
  192. _ctlargcount(b, cp, 1);
  193. scribshow(b);
  194. break;
  195. case EFont:
  196. _ctlargcount(b, cp, 2);
  197. _setctlfont(b, &b->font, cp->args[1]);
  198. break;
  199. case EHide:
  200. _ctlargcount(b, cp, 1);
  201. b->hidden = 1;
  202. break;
  203. case ESize:
  204. if (cp->nargs == 3)
  205. r.max = Pt(0x7fffffff, 0x7fffffff);
  206. else{
  207. _ctlargcount(b, cp, 5);
  208. r.max.x = cp->iargs[3];
  209. r.max.y = cp->iargs[4];
  210. }
  211. r.min.x = cp->iargs[1];
  212. r.min.y = cp->iargs[2];
  213. if(r.min.x<=0 || r.min.y<=0 || r.max.x<=0 || r.max.y<=0 || r.max.x < r.min.x || r.max.y < r.min.y)
  214. ctlerror("%q: bad sizes: %s", b->name, cp->str);
  215. b->size.min = r.min;
  216. b->size.max = r.max;
  217. break;
  218. }
  219. }
  220. static void
  221. resetstroke(Scrib *w)
  222. {
  223. Scribble *s = w->scrib;
  224. s->ps.npts = 0;
  225. scribshow(w);
  226. }
  227. static void
  228. displaystroke(Scrib *b)
  229. {
  230. Scribble *s = b->scrib;
  231. poly(b->screen, s->pt, s->ps.npts, Endsquare, Endsquare, 0, b->color->image, ZP);
  232. flushimage(display, 1);
  233. }
  234. static void
  235. displaylast(Scrib *w)
  236. {
  237. int npt;
  238. Scribble *s = w->scrib;
  239. npt = s->ps.npts;
  240. if (npt > 2)
  241. npt = 2;
  242. poly(w->screen, s->pt + (s->ps.npts - npt), npt, Endsquare, Endsquare,
  243. 0, w->color->image, ZP);
  244. flushimage(display, 1);
  245. }
  246. static void
  247. addpoint(Scrib *w, Point p)
  248. {
  249. pen_point *ppa;
  250. Point *pt;
  251. int ppasize;
  252. Scribble *s = w->scrib;
  253. if (s->ps.npts == s->ppasize) {
  254. ppasize = s->ppasize + 100;
  255. ppa = malloc ((sizeof (pen_point) + sizeof (Point)) * ppasize);
  256. if (!ppa)
  257. return;
  258. pt = (Point *) (ppa + ppasize);
  259. memmove(ppa, s->ps.pts, s->ppasize * sizeof (pen_point));
  260. memmove(pt, s->pt, s->ppasize * sizeof (Point));
  261. free (s->ps.pts);
  262. s->ps.pts = ppa;
  263. s->pt = pt;
  264. s->ppasize = ppasize;
  265. }
  266. ppa = &s->ps.pts[s->ps.npts];
  267. ppa->Point = p;
  268. pt = &s->pt[s->ps.npts];
  269. *pt = p;
  270. s->ps.npts++;
  271. displaylast(w);
  272. }
  273. Control*
  274. createscribble(Controlset *cs, char *name)
  275. {
  276. Scrib *b;
  277. b = (Scrib*)_createctl(cs, "scribble", sizeof(Scrib), name);
  278. b->image = _getctlimage("white");
  279. b->color = _getctlimage("black");
  280. b->bordercolor = _getctlimage("black");
  281. b->align = Aupperleft;
  282. b->format = ctlstrdup("%q: value 0x%x");
  283. b->font = _getctlfont("font");
  284. b->scrib = scribblealloc();
  285. b->lastbut = 0;
  286. b->bordercolor = _getctlimage("black");
  287. b->border = 0;
  288. b->ctl = scribctl;
  289. b->mouse = scribmouse;
  290. b->exit = scribfree;
  291. return (Control*)b;
  292. }