tweak.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <cursor.h>
  5. #include <event.h>
  6. #include <bio.h>
  7. typedef struct Thing Thing;
  8. struct Thing
  9. {
  10. Image *b;
  11. Subfont *s;
  12. char *name; /* file name */
  13. int face; /* is 48x48 face file or cursor file*/
  14. Rectangle r; /* drawing region */
  15. Rectangle tr; /* text region */
  16. Rectangle er; /* entire region */
  17. long c; /* character number in subfont */
  18. int mod; /* modified */
  19. int mag; /* magnification */
  20. Rune off; /* offset for subfont indices */
  21. Thing *parent; /* thing of which i'm an edit */
  22. Thing *next;
  23. };
  24. enum
  25. {
  26. Border = 1,
  27. Up = 1,
  28. Down = 0,
  29. Mag = 4,
  30. Maxmag = 10,
  31. };
  32. enum
  33. {
  34. NORMAL =0,
  35. FACE =1,
  36. CURSOR =2
  37. };
  38. enum
  39. {
  40. Mopen,
  41. Mread,
  42. Mwrite,
  43. Mcopy,
  44. Mchar,
  45. Mpixels,
  46. Mclose,
  47. Mexit,
  48. };
  49. enum
  50. {
  51. Blue = 54,
  52. };
  53. char *menu3str[] = {
  54. [Mopen] "open",
  55. [Mread] "read",
  56. [Mwrite] "write",
  57. [Mcopy] "copy",
  58. [Mchar] "char",
  59. [Mpixels] "pixels",
  60. [Mclose] "close",
  61. [Mexit] "exit",
  62. 0,
  63. };
  64. Menu menu3 = {
  65. menu3str
  66. };
  67. Cursor sweep0 = {
  68. {-7, -7},
  69. {0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0,
  70. 0x03, 0xC0, 0x03, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,
  71. 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xC0, 0x03, 0xC0,
  72. 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0},
  73. {0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
  74. 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x7F, 0xFE,
  75. 0x7F, 0xFE, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
  76. 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00}
  77. };
  78. Cursor box = {
  79. {-7, -7},
  80. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  81. 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
  82. 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
  83. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  84. {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
  85. 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
  86. 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
  87. 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
  88. };
  89. Cursor sight = {
  90. {-7, -7},
  91. {0x1F, 0xF8, 0x3F, 0xFC, 0x7F, 0xFE, 0xFB, 0xDF,
  92. 0xF3, 0xCF, 0xE3, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF,
  93. 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0xC7, 0xF3, 0xCF,
  94. 0x7B, 0xDF, 0x7F, 0xFE, 0x3F, 0xFC, 0x1F, 0xF8,},
  95. {0x00, 0x00, 0x0F, 0xF0, 0x31, 0x8C, 0x21, 0x84,
  96. 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x7F, 0xFE,
  97. 0x7F, 0xFE, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
  98. 0x21, 0x84, 0x31, 0x8C, 0x0F, 0xF0, 0x00, 0x00,}
  99. };
  100. Cursor pixel = {
  101. {-7, -7},
  102. {0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe, 0xf8, 0x1f,
  103. 0xf0, 0x0f, 0xe0, 0x07, 0xe0, 0x07, 0xfe, 0x7f,
  104. 0xfe, 0x7f, 0xe0, 0x07, 0xe0, 0x07, 0xf0, 0x0f,
  105. 0x78, 0x1f, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, },
  106. {0x00, 0x00, 0x0f, 0xf0, 0x31, 0x8c, 0x21, 0x84,
  107. 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x40, 0x02,
  108. 0x40, 0x02, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
  109. 0x21, 0x84, 0x31, 0x8c, 0x0f, 0xf0, 0x00, 0x00, }
  110. };
  111. Cursor busy = {
  112. {-7, -7},
  113. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x0c, 0x00, 0x8e, 0x1d, 0xc7,
  115. 0xff, 0xe3, 0xff, 0xf3, 0xff, 0xff, 0x7f, 0xfe,
  116. 0x3f, 0xf8, 0x17, 0xf0, 0x03, 0xe0, 0x00, 0x00,},
  117. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x82,
  119. 0x04, 0x41, 0xff, 0xe1, 0x5f, 0xf1, 0x3f, 0xfe,
  120. 0x17, 0xf0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00,}
  121. };
  122. Cursor skull = {
  123. {-7,-7},
  124. {0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0xe7, 0xe7,
  125. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc, 0x1f, 0xf8,
  126. 0x0f, 0xf0, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xff,
  127. 0xef, 0xf7, 0xc7, 0xe3, 0x00, 0x00, 0x00, 0x00,},
  128. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03,
  129. 0xE7, 0xE7, 0x3F, 0xFC, 0x0F, 0xF0, 0x0D, 0xB0,
  130. 0x07, 0xE0, 0x06, 0x60, 0x37, 0xEC, 0xE4, 0x27,
  131. 0xC3, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
  132. };
  133. Rectangle cntlr; /* control region */
  134. Rectangle editr; /* editing region */
  135. Rectangle textr; /* text region */
  136. Thing *thing;
  137. Mouse mouse;
  138. char hex[] = "0123456789abcdefABCDEF";
  139. jmp_buf err;
  140. char *file;
  141. int mag;
  142. int but1val = 0;
  143. int but2val = 255;
  144. int invert = 0;
  145. Image *values[256];
  146. Image *greyvalues[256];
  147. uchar data[8192];
  148. Thing* tget(char*);
  149. void mesg(char*, ...);
  150. void drawthing(Thing*, int);
  151. void select(void);
  152. void menu(void);
  153. void error(Display*, char*);
  154. void buttons(int);
  155. void drawall(void);
  156. void tclose1(Thing*);
  157. void
  158. main(int argc, char *argv[])
  159. {
  160. int i;
  161. Event e;
  162. Thing *t;
  163. mag = Mag;
  164. if(initdraw(error, 0, "tweak") < 0){
  165. fprint(2, "tweak: initdraw failed: %r\n");
  166. exits("initdraw");
  167. }
  168. for(i=0; i<256; i++){
  169. values[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, cmap2rgba(i));
  170. greyvalues[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, (i<<24)|(i<<16)|(i<<8)|0xFF);
  171. if(values[i] == 0 || greyvalues[i] == 0)
  172. drawerror(display, "can't allocate image");
  173. }
  174. einit(Emouse|Ekeyboard);
  175. eresized(0);
  176. i = 1;
  177. setjmp(err);
  178. for(; i<argc; i++){
  179. file = argv[i];
  180. t = tget(argv[i]);
  181. if(t)
  182. drawthing(t, 1);
  183. flushimage(display, 1);
  184. }
  185. file = 0;
  186. setjmp(err);
  187. for(;;)
  188. switch(event(&e)){
  189. case Ekeyboard:
  190. break;
  191. case Emouse:
  192. mouse = e.mouse;
  193. if(mouse.buttons & 3){
  194. select();
  195. break;
  196. }
  197. if(mouse.buttons & 4)
  198. menu();
  199. }
  200. }
  201. void
  202. error(Display*, char *s)
  203. {
  204. if(file)
  205. mesg("can't read %s: %s: %r", file, s);
  206. else
  207. mesg("/dev/bitblt error: %s", s);
  208. if(err[0])
  209. longjmp(err, 1);
  210. exits(s);
  211. }
  212. void
  213. redraw(Thing *t)
  214. {
  215. Thing *nt;
  216. Point p;
  217. if(thing==0 || thing==t)
  218. draw(screen, editr, display->white, nil, ZP);
  219. if(thing == 0)
  220. return;
  221. if(thing != t){
  222. for(nt=thing; nt->next!=t; nt=nt->next)
  223. ;
  224. draw(screen, Rect(screen->r.min.x, nt->er.max.y, editr.max.x, editr.max.y),
  225. display->white, nil, ZP);
  226. }
  227. for(nt=t; nt; nt=nt->next){
  228. drawthing(nt, 0);
  229. if(nt->next == 0){
  230. p = Pt(editr.min.x, nt->er.max.y);
  231. draw(screen, Rpt(p, editr.max), display->white, nil, ZP);
  232. }
  233. }
  234. mesg("");
  235. }
  236. void
  237. eresized(int new)
  238. {
  239. if(new && getwindow(display, Refnone) < 0)
  240. error(display, "can't reattach to window");
  241. cntlr = insetrect(screen->clipr, 1);
  242. editr = cntlr;
  243. textr = editr;
  244. textr.min.y = textr.max.y - font->height;
  245. cntlr.max.y = cntlr.min.y + font->height;
  246. editr.min.y = cntlr.max.y+1;
  247. editr.max.y = textr.min.y-1;
  248. draw(screen, screen->clipr, display->white, nil, ZP);
  249. draw(screen, Rect(editr.min.x, editr.max.y, editr.max.x+1, editr.max.y+1), display->black, nil, ZP);
  250. replclipr(screen, 0, editr);
  251. drawall();
  252. }
  253. void
  254. mesgstr(Point p, int line, char *s)
  255. {
  256. Rectangle c, r;
  257. r.min = p;
  258. r.min.y += line*font->height;
  259. r.max.y = r.min.y+font->height;
  260. r.max.x = editr.max.x;
  261. c = screen->clipr;
  262. replclipr(screen, 0, r);
  263. draw(screen, r, values[0xDD], nil, ZP);
  264. r.min.x++;
  265. string(screen, r.min, display->black, ZP, font, s);
  266. replclipr(screen, 0, c);
  267. flushimage(display, 1);
  268. }
  269. void
  270. mesg(char *fmt, ...)
  271. {
  272. char buf[1024];
  273. va_list arg;
  274. va_start(arg, fmt);
  275. vseprint(buf, buf+sizeof(buf), fmt, arg);
  276. va_end(arg);
  277. mesgstr(textr.min, 0, buf);
  278. }
  279. void
  280. tmesg(Thing *t, int line, char *fmt, ...)
  281. {
  282. char buf[1024];
  283. va_list arg;
  284. va_start(arg, fmt);
  285. vseprint(buf, buf+sizeof(buf), fmt, arg);
  286. va_end(arg);
  287. mesgstr(t->tr.min, line, buf);
  288. }
  289. void
  290. scntl(char *l)
  291. {
  292. sprint(l, "mag: %d but1: %d but2: %d invert-on-copy: %c", mag, but1val, but2val, "ny"[invert]);
  293. }
  294. void
  295. cntl(void)
  296. {
  297. char buf[256];
  298. scntl(buf);
  299. mesgstr(cntlr.min, 0, buf);
  300. }
  301. void
  302. stext(Thing *t, char *l0, char *l1)
  303. {
  304. Fontchar *fc;
  305. char buf[256];
  306. l1[0] = 0;
  307. sprint(buf, "depth:%d r:%d %d %d %d ",
  308. t->b->depth, t->b->r.min.x, t->b->r.min.y,
  309. t->b->r.max.x, t->b->r.max.y);
  310. if(t->parent)
  311. sprint(buf+strlen(buf), "mag: %d ", t->mag);
  312. sprint(l0, "%s file: %s", buf, t->name);
  313. if(t->c >= 0){
  314. fc = &t->parent->s->info[t->c];
  315. sprint(l1, "c(hex): %x c(char): %C x: %d "
  316. "top: %d bottom: %d left: %d width: %d iwidth: %d",
  317. (int)(t->c+t->parent->off), (int)(t->c+t->parent->off),
  318. fc->x, fc->top, fc->bottom, fc->left,
  319. fc->width, Dx(t->b->r));
  320. }else if(t->s)
  321. sprint(l1, "offset(hex): %ux n:%d height:%d ascent:%d",
  322. t->off, t->s->n, t->s->height, t->s->ascent);
  323. }
  324. void
  325. text(Thing *t)
  326. {
  327. char l0[256], l1[256];
  328. stext(t, l0, l1);
  329. tmesg(t, 0, l0);
  330. if(l1[0])
  331. tmesg(t, 1, l1);
  332. }
  333. void
  334. drawall(void)
  335. {
  336. Thing *t;
  337. cntl();
  338. for(t=thing; t; t=t->next)
  339. drawthing(t, 0);
  340. }
  341. int
  342. value(Image *b, int x)
  343. {
  344. int v, l, w;
  345. uchar mask;
  346. w = b->depth;
  347. if(w > 8){
  348. mesg("ldepth too large");
  349. return 0;
  350. }
  351. l = log2[w];
  352. mask = (1<<w)-1; /* ones at right end of word */
  353. x -= b->r.min.x&~(7>>l); /* adjust x relative to first pixel */
  354. v = data[x>>(3-l)];
  355. v >>= ((7>>l)<<l) - ((x&(7>>l))<<l); /* pixel at right end of word */
  356. v &= mask; /* pixel at right end of word */
  357. return v;
  358. }
  359. int
  360. bvalue(int v, int d)
  361. {
  362. v &= (1<<d)-1;
  363. if(d > screen->depth)
  364. v >>= d - screen->depth;
  365. else
  366. while(d < screen->depth && d < 8){
  367. v |= v << d;
  368. d <<= 1;
  369. }
  370. if(v<0 || v>255){
  371. mesg("internal error: bad color");
  372. return Blue;
  373. }
  374. return v;
  375. }
  376. void
  377. drawthing(Thing *nt, int link)
  378. {
  379. int nl, nf, i, x, y, sx, sy, fdx, dx, dy, v;
  380. Thing *t;
  381. Subfont *s;
  382. Image *b, *col;
  383. Point p, p1, p2;
  384. if(link){
  385. nt->next = 0;
  386. if(thing == 0){
  387. thing = nt;
  388. y = editr.min.y;
  389. }else{
  390. for(t=thing; t->next; t=t->next)
  391. ;
  392. t->next = nt;
  393. y = t->er.max.y;
  394. }
  395. }else{
  396. if(thing == nt)
  397. y = editr.min.y;
  398. else{
  399. for(t=thing; t->next!=nt; t=t->next)
  400. ;
  401. y = t->er.max.y;
  402. }
  403. }
  404. s = nt->s;
  405. b = nt->b;
  406. nl = font->height;
  407. if(s || nt->c>=0)
  408. nl += font->height;
  409. fdx = Dx(editr) - 2*Border;
  410. dx = Dx(b->r);
  411. dy = Dy(b->r);
  412. if(nt->mag > 1){
  413. dx *= nt->mag;
  414. dy *= nt->mag;
  415. fdx -= fdx%nt->mag;
  416. }
  417. nf = 1 + dx/fdx;
  418. nt->er.min.y = y;
  419. nt->er.min.x = editr.min.x;
  420. nt->er.max.x = nt->er.min.x + Border + dx + Border;
  421. if(nt->er.max.x > editr.max.x)
  422. nt->er.max.x = editr.max.x;
  423. nt->er.max.y = nt->er.min.y + Border + nf*(dy+Border);
  424. nt->r = insetrect(nt->er, Border);
  425. nt->er.max.x = editr.max.x;
  426. draw(screen, nt->er, display->white, nil, ZP);
  427. for(i=0; i<nf; i++){
  428. p1 = Pt(nt->r.min.x-1, nt->r.min.y+i*(Border+dy));
  429. /* draw portion of bitmap */
  430. p = Pt(p1.x+1, p1.y);
  431. if(nt->mag == 1)
  432. draw(screen, Rect(p.x, p.y, p.x+fdx+Dx(b->r), p.y+Dy(b->r)),
  433. b, nil, Pt(b->r.min.x+i*fdx, b->r.min.y));
  434. else{
  435. for(y=b->r.min.y; y<b->r.max.y; y++){
  436. sy = p.y+(y-b->r.min.y)*nt->mag;
  437. unloadimage(b, Rect(b->r.min.x, y, b->r.max.x, y+1), data, sizeof data);
  438. for(x=b->r.min.x+i*(fdx/nt->mag); x<b->r.max.x; x++){
  439. sx = p.x+(x-i*(fdx/nt->mag)-b->r.min.x)*nt->mag;
  440. if(sx >= nt->r.max.x)
  441. break;
  442. v = bvalue(value(b, x), b->depth);
  443. if(v == 255)
  444. continue;
  445. if(b->chan == GREY8)
  446. draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
  447. greyvalues[v], nil, ZP);
  448. else
  449. draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
  450. values[v], nil, ZP);
  451. }
  452. }
  453. }
  454. /* line down left */
  455. if(i == 0)
  456. col = display->black;
  457. else
  458. col = display->white;
  459. draw(screen, Rect(p1.x, p1.y, p1.x+1, p1.y+dy+Border), col, nil, ZP);
  460. /* line across top */
  461. draw(screen, Rect(p1.x, p1.y-1, nt->r.max.x+Border, p1.y), display->black, nil, ZP);
  462. p2 = p1;
  463. if(i == nf-1){
  464. p2.x += 1 + dx%fdx;
  465. col = display->black;
  466. }else{
  467. p2.x = nt->r.max.x;
  468. col = display->white;
  469. }
  470. /* line down right */
  471. draw(screen, Rect(p2.x, p2.y, p2.x+1, p2.y+dy+Border), col, nil, ZP);
  472. /* line across bottom */
  473. if(i == nf-1){
  474. p1.y += Border+dy;
  475. draw(screen, Rect(p1.x, p1.y-1, p2.x,p1.y), display->black, nil, ZP);
  476. }
  477. }
  478. nt->tr.min.x = editr.min.x;
  479. nt->tr.max.x = editr.max.x;
  480. nt->tr.min.y = nt->er.max.y + Border;
  481. nt->tr.max.y = nt->tr.min.y + nl;
  482. nt->er.max.y = nt->tr.max.y + Border;
  483. text(nt);
  484. }
  485. int
  486. tohex(int c)
  487. {
  488. if('0'<=c && c<='9')
  489. return c - '0';
  490. if('a'<=c && c<='f')
  491. return 10 + (c - 'a');
  492. if('A'<=c && c<='F')
  493. return 10 + (c - 'A');
  494. }
  495. Thing*
  496. tget(char *file)
  497. {
  498. int i, j, fd, face, x, y, c, chan;
  499. Image *b;
  500. Subfont *s;
  501. Thing *t;
  502. Dir *d;
  503. jmp_buf oerr;
  504. uchar buf[256];
  505. char *data;
  506. buf[0] = '\0';
  507. errstr((char*)buf, sizeof buf); /* flush pending error message */
  508. memmove(oerr, err, sizeof err);
  509. d = nil;
  510. if(setjmp(err)){
  511. Err:
  512. free(d);
  513. memmove(err, oerr, sizeof err);
  514. return 0;
  515. }
  516. fd = open(file, OREAD);
  517. if(fd < 0){
  518. mesg("can't open %s: %r", file);
  519. goto Err;
  520. }
  521. d = dirfstat(fd);
  522. if(d == nil){
  523. mesg("can't stat bitmap file %s: %r", file);
  524. close(fd);
  525. goto Err;
  526. }
  527. if(read(fd, buf, 11) != 11){
  528. mesg("can't read %s: %r", file);
  529. close(fd);
  530. goto Err;
  531. }
  532. seek(fd, 0, 0);
  533. data = (char*)buf;
  534. if(*data == '{')
  535. data++;
  536. if(memcmp(data, "0x", 2)==0 && data[4]==','){
  537. /*
  538. * cursor file
  539. */
  540. face = CURSOR;
  541. s = 0;
  542. data = malloc(d->length+1);
  543. if(data == 0){
  544. mesg("can't malloc buffer: %r");
  545. close(fd);
  546. goto Err;
  547. }
  548. data[d->length] = 0;
  549. if(read(fd, data, d->length) != d->length){
  550. mesg("can't read cursor file %s: %r", file);
  551. close(fd);
  552. goto Err;
  553. }
  554. b = allocimage(display, Rect(0, 0, 16, 32), GREY1, 0, DNofill);
  555. if(b == 0){
  556. mesg("image alloc failed file %s: %r", file);
  557. free(data);
  558. close(fd);
  559. goto Err;
  560. }
  561. i = 0;
  562. for(x=0;x<64; ){
  563. if((c=data[i]) == '\0')
  564. goto ill;
  565. if(c=='0' && data[i+1] == 'x'){
  566. i += 2;
  567. continue;
  568. }
  569. if(strchr(hex, c)){
  570. buf[x++] = (tohex(c)<<4) | tohex(data[i+1]);
  571. i += 2;
  572. continue;
  573. }
  574. i++;
  575. }
  576. loadimage(b, Rect(0, 0, 16, 32), buf, sizeof buf);
  577. free(data);
  578. }else if(memcmp(buf, "0x", 2)==0){
  579. /*
  580. * face file
  581. */
  582. face = FACE;
  583. s = 0;
  584. data = malloc(d->length+1);
  585. if(data == 0){
  586. mesg("can't malloc buffer: %r");
  587. close(fd);
  588. goto Err;
  589. }
  590. data[d->length] = 0;
  591. if(read(fd, data, d->length) != d->length){
  592. mesg("can't read bitmap file %s: %r", file);
  593. close(fd);
  594. goto Err;
  595. }
  596. for(y=0,i=0; i<d->length; i++)
  597. if(data[i] == '\n')
  598. y++;
  599. if(y == 0){
  600. ill:
  601. mesg("ill-formed face file %s", file);
  602. close(fd);
  603. free(data);
  604. goto Err;
  605. }
  606. for(x=0,i=0; (c=data[i])!='\n'; ){
  607. if(c==',' || c==' ' || c=='\t'){
  608. i++;
  609. continue;
  610. }
  611. if(c=='0' && data[i+1] == 'x'){
  612. i += 2;
  613. continue;
  614. }
  615. if(strchr(hex, c)){
  616. x += 4;
  617. i++;
  618. continue;
  619. }
  620. goto ill;
  621. }
  622. if(x % y)
  623. goto ill;
  624. switch(x / y){
  625. default:
  626. goto ill;
  627. case 1:
  628. chan = GREY1;
  629. break;
  630. case 2:
  631. chan = GREY2;
  632. break;
  633. case 4:
  634. chan = GREY4;
  635. break;
  636. case 8:
  637. chan = CMAP8;
  638. break;
  639. }
  640. b = allocimage(display, Rect(0, 0, y, y), chan, 0, -1);
  641. if(b == 0){
  642. mesg("image alloc failed file %s: %r", file);
  643. free(data);
  644. close(fd);
  645. goto Err;
  646. }
  647. i = 0;
  648. for(j=0; j<y; j++){
  649. for(x=0; (c=data[i])!='\n'; ){
  650. if(c=='0' && data[i+1] == 'x'){
  651. i += 2;
  652. continue;
  653. }
  654. if(strchr(hex, c)){
  655. buf[x++] = ~((tohex(c)<<4) | tohex(data[i+1]));
  656. i += 2;
  657. continue;
  658. }
  659. i++;
  660. }
  661. i++;
  662. loadimage(b, Rect(0, j, y, j+1), buf, sizeof buf);
  663. }
  664. free(data);
  665. }else{
  666. face = NORMAL;
  667. s = 0;
  668. b = readimage(display, fd, 0);
  669. if(b == 0){
  670. mesg("can't read bitmap file %s: %r", file);
  671. close(fd);
  672. goto Err;
  673. }
  674. if(seek(fd, 0, 1) < d->length)
  675. s = readsubfonti(display, file, fd, b, 0);
  676. }
  677. close(fd);
  678. t = malloc(sizeof(Thing));
  679. if(t == 0){
  680. nomem:
  681. mesg("malloc failed: %r");
  682. if(s)
  683. freesubfont(s);
  684. else
  685. freeimage(b);
  686. goto Err;
  687. }
  688. t->name = strdup(file);
  689. if(t->name == 0){
  690. free(t);
  691. goto nomem;
  692. }
  693. t->b = b;
  694. t->s = s;
  695. t->face = face;
  696. t->mod = 0;
  697. t->parent = 0;
  698. t->c = -1;
  699. t->mag = 1;
  700. t->off = 0;
  701. memmove(err, oerr, sizeof err);
  702. return t;
  703. }
  704. int
  705. atline(int x, Point p, char *line, char *buf)
  706. {
  707. char *s, *c, *word, *hit;
  708. int w, wasblank;
  709. Rune r;
  710. wasblank = 1;
  711. hit = 0;
  712. word = 0;
  713. for(s=line; *s; s+=w){
  714. w = chartorune(&r, s);
  715. x += runestringnwidth(font, &r, 1);
  716. if(wasblank && r!=' ')
  717. word = s;
  718. wasblank = 0;
  719. if(r == ' '){
  720. if(x >= p.x)
  721. break;
  722. wasblank = 1;
  723. }
  724. if(r == ':')
  725. hit = word;
  726. }
  727. if(x < p.x)
  728. return 0;
  729. c = utfrune(hit, ':');
  730. strncpy(buf, hit, c-hit);
  731. buf[c-hit] = 0;
  732. return 1;
  733. }
  734. int
  735. attext(Thing *t, Point p, char *buf)
  736. {
  737. char l0[256], l1[256];
  738. if(!ptinrect(p, t->tr))
  739. return 0;
  740. stext(t, l0, l1);
  741. if(p.y < t->tr.min.y+font->height)
  742. return atline(t->r.min.x, p, l0, buf);
  743. else
  744. return atline(t->r.min.x, p, l1, buf);
  745. }
  746. int
  747. type(char *buf, char *tag)
  748. {
  749. Rune r;
  750. char *p;
  751. esetcursor(&busy);
  752. p = buf;
  753. for(;;){
  754. *p = 0;
  755. mesg("%s: %s", tag, buf);
  756. r = ekbd();
  757. switch(r){
  758. case '\n':
  759. mesg("");
  760. esetcursor(0);
  761. return p-buf;
  762. case 0x15: /* control-U */
  763. p = buf;
  764. break;
  765. case '\b':
  766. if(p > buf)
  767. --p;
  768. break;
  769. default:
  770. p += runetochar(p, &r);
  771. }
  772. }
  773. return 0; /* shut up compiler */
  774. }
  775. void
  776. textedit(Thing *t, char *tag)
  777. {
  778. char buf[256];
  779. char *s;
  780. Image *b;
  781. Subfont *f;
  782. Fontchar *fc, *nfc;
  783. Rectangle r;
  784. ulong chan;
  785. int i, ld, d, w, c, doredraw, fdx, x;
  786. Thing *nt;
  787. buttons(Up);
  788. if(type(buf, tag) == 0)
  789. return;
  790. if(strcmp(tag, "file") == 0){
  791. for(s=buf; *s; s++)
  792. if(*s <= ' '){
  793. mesg("illegal file name");
  794. return;
  795. }
  796. if(strcmp(t->name, buf) != 0){
  797. if(t->parent)
  798. t->parent->mod = 1;
  799. else
  800. t->mod = 1;
  801. }
  802. for(nt=thing; nt; nt=nt->next)
  803. if(t==nt || t->parent==nt || nt->parent==t){
  804. free(nt->name);
  805. nt->name = strdup(buf);
  806. if(nt->name == 0){
  807. mesg("malloc failed: %r");
  808. return;
  809. }
  810. text(nt);
  811. }
  812. return;
  813. }
  814. if(strcmp(tag, "depth") == 0){
  815. if(buf[0]<'0' || '9'<buf[0] || (d=atoi(buf))<0 || d>8 || log2[d]<0){
  816. mesg("illegal ldepth");
  817. return;
  818. }
  819. if(d == t->b->depth)
  820. return;
  821. if(t->parent)
  822. t->parent->mod = 1;
  823. else
  824. t->mod = 1;
  825. if(d == 8)
  826. chan = CMAP8;
  827. else
  828. chan = CHAN1(CGrey, d);
  829. for(nt=thing; nt; nt=nt->next){
  830. if(nt!=t && nt!=t->parent && nt->parent!=t)
  831. continue;
  832. b = allocimage(display, nt->b->r, chan, 0, 0);
  833. if(b == 0){
  834. nobmem:
  835. mesg("image alloc failed: %r");
  836. return;
  837. }
  838. draw(b, b->r, nt->b, nil, nt->b->r.min);
  839. freeimage(nt->b);
  840. nt->b = b;
  841. if(nt->s){
  842. b = allocimage(display, nt->b->r, chan, 0, -1);
  843. if(b == 0)
  844. goto nobmem;
  845. draw(b, b->r, nt->b, nil, nt->b->r.min);
  846. f = allocsubfont(t->name, nt->s->n, nt->s->height, nt->s->ascent, nt->s->info, b);
  847. if(f == 0){
  848. nofmem:
  849. freeimage(b);
  850. mesg("can't make subfont: %r");
  851. return;
  852. }
  853. nt->s->info = 0; /* prevent it being freed */
  854. nt->s->bits = 0;
  855. freesubfont(nt->s);
  856. nt->s = f;
  857. }
  858. drawthing(nt, 0);
  859. }
  860. return;
  861. }
  862. if(strcmp(tag, "mag") == 0){
  863. if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<=0 || ld>Maxmag){
  864. mesg("illegal magnification");
  865. return;
  866. }
  867. if(t->mag == ld)
  868. return;
  869. t->mag = ld;
  870. redraw(t);
  871. return;
  872. }
  873. if(strcmp(tag, "r") == 0){
  874. if(t->s){
  875. mesg("can't change rectangle of subfont\n");
  876. return;
  877. }
  878. s = buf;
  879. r.min.x = strtoul(s, &s, 0);
  880. r.min.y = strtoul(s, &s, 0);
  881. r.max.x = strtoul(s, &s, 0);
  882. r.max.y = strtoul(s, &s, 0);
  883. if(Dx(r)<=0 || Dy(r)<=0){
  884. mesg("illegal rectangle");
  885. return;
  886. }
  887. if(t->parent)
  888. t = t->parent;
  889. for(nt=thing; nt; nt=nt->next){
  890. if(nt->parent==t && !rectinrect(nt->b->r, r))
  891. tclose1(nt);
  892. }
  893. b = allocimage(display, r, t->b->chan, 0, 0);
  894. if(b == 0)
  895. goto nobmem;
  896. draw(b, r, t->b, nil, r.min);
  897. freeimage(t->b);
  898. t->b = b;
  899. b = allocimage(display, r, t->b->chan, 0, 0);
  900. if(b == 0)
  901. goto nobmem;
  902. redraw(t);
  903. t->mod = 1;
  904. return;
  905. }
  906. if(strcmp(tag, "ascent") == 0){
  907. if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0 || ld>t->s->height){
  908. mesg("illegal ascent");
  909. return;
  910. }
  911. if(t->s->ascent == ld)
  912. return;
  913. t->s->ascent = ld;
  914. text(t);
  915. t->mod = 1;
  916. return;
  917. }
  918. if(strcmp(tag, "height") == 0){
  919. if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
  920. mesg("illegal height");
  921. return;
  922. }
  923. if(t->s->height == ld)
  924. return;
  925. t->s->height = ld;
  926. text(t);
  927. t->mod = 1;
  928. return;
  929. }
  930. if(strcmp(tag, "left")==0 || strcmp(tag, "width") == 0){
  931. if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
  932. mesg("illegal value");
  933. return;
  934. }
  935. fc = &t->parent->s->info[t->c];
  936. if(strcmp(tag, "left")==0){
  937. if(fc->left == ld)
  938. return;
  939. fc->left = ld;
  940. }else{
  941. if(fc->width == ld)
  942. return;
  943. fc->width = ld;
  944. }
  945. text(t);
  946. t->parent->mod = 1;
  947. return;
  948. }
  949. if(strcmp(tag, "offset(hex)") == 0){
  950. if(!strchr(hex, buf[0])){
  951. illoff:
  952. mesg("illegal offset");
  953. return;
  954. }
  955. s = 0;
  956. ld = strtoul(buf, &s, 16);
  957. if(*s)
  958. goto illoff;
  959. t->off = ld;
  960. text(t);
  961. for(nt=thing; nt; nt=nt->next)
  962. if(nt->parent == t)
  963. text(nt);
  964. return;
  965. }
  966. if(strcmp(tag, "n") == 0){
  967. if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<=0){
  968. mesg("illegal n");
  969. return;
  970. }
  971. f = t->s;
  972. if(w == f->n)
  973. return;
  974. doredraw = 0;
  975. again:
  976. for(nt=thing; nt; nt=nt->next)
  977. if(nt->parent == t){
  978. doredraw = 1;
  979. tclose1(nt);
  980. goto again;
  981. }
  982. r = t->b->r;
  983. if(w < f->n)
  984. r.max.x = f->info[w].x;
  985. b = allocimage(display, r, t->b->chan, 0, 0);
  986. if(b == 0)
  987. goto nobmem;
  988. draw(b, b->r, t->b, nil, r.min);
  989. fdx = Dx(editr) - 2*Border;
  990. if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
  991. doredraw = 1;
  992. freeimage(t->b);
  993. t->b = b;
  994. b = allocimage(display, r, t->b->chan, 0, 0);
  995. if(b == 0)
  996. goto nobmem;
  997. draw(b, b->r, t->b, nil, r.min);
  998. nfc = malloc((w+1)*sizeof(Fontchar));
  999. if(nfc == 0){
  1000. mesg("malloc failed");
  1001. freeimage(b);
  1002. return;
  1003. }
  1004. fc = f->info;
  1005. for(i=0; i<=w && i<=f->n; i++)
  1006. nfc[i] = fc[i];
  1007. if(w+1 < i)
  1008. memset(nfc+i, 0, ((w+1)-i)*sizeof(Fontchar));
  1009. x = fc[f->n].x;
  1010. for(; i<=w; i++)
  1011. nfc[i].x = x;
  1012. f = allocsubfont(t->name, w, f->height, f->ascent, nfc, b);
  1013. if(f == 0)
  1014. goto nofmem;
  1015. t->s->bits = nil; /* don't free it */
  1016. freesubfont(t->s);
  1017. f->info = nfc;
  1018. t->s = f;
  1019. if(doredraw)
  1020. redraw(thing);
  1021. else
  1022. drawthing(t, 0);
  1023. t->mod = 1;
  1024. return;
  1025. }
  1026. if(strcmp(tag, "iwidth") == 0){
  1027. if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<0){
  1028. mesg("illegal iwidth");
  1029. return;
  1030. }
  1031. w -= Dx(t->b->r);
  1032. if(w == 0)
  1033. return;
  1034. r = t->parent->b->r;
  1035. r.max.x += w;
  1036. c = t->c;
  1037. t = t->parent;
  1038. f = t->s;
  1039. b = allocimage(display, r, t->b->chan, 0, 0);
  1040. if(b == 0)
  1041. goto nobmem;
  1042. fc = &f->info[c];
  1043. draw(b, Rect(b->r.min.x, b->r.min.y,
  1044. b->r.min.x+(fc[1].x-t->b->r.min.x), b->r.min.y+Dy(t->b->r)),
  1045. t->b, nil, t->b->r.min);
  1046. draw(b, Rect(fc[1].x+w, b->r.min.y, w+t->b->r.max.x, b->r.min.y+Dy(t->b->r)),
  1047. t->b, nil, Pt(fc[1].x, t->b->r.min.y));
  1048. fdx = Dx(editr) - 2*Border;
  1049. doredraw = 0;
  1050. if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
  1051. doredraw = 1;
  1052. freeimage(t->b);
  1053. t->b = b;
  1054. b = allocimage(display, r, t->b->chan, 0, 0);
  1055. if(b == 0)
  1056. goto nobmem;
  1057. draw(b, b->r, t->b, nil, t->b->r.min);
  1058. fc = &f->info[c+1];
  1059. for(i=c+1; i<=f->n; i++, fc++)
  1060. fc->x += w;
  1061. f = allocsubfont(t->name, f->n, f->height, f->ascent,
  1062. f->info, b);
  1063. if(f == 0)
  1064. goto nofmem;
  1065. /* t->s and f share info; free carefully */
  1066. fc = f->info;
  1067. t->s->bits = nil;
  1068. t->s->info = 0;
  1069. freesubfont(t->s);
  1070. f->info = fc;
  1071. t->s = f;
  1072. if(doredraw)
  1073. redraw(t);
  1074. else
  1075. drawthing(t, 0);
  1076. /* redraw all affected chars */
  1077. for(nt=thing; nt; nt=nt->next){
  1078. if(nt->parent!=t || nt->c<c)
  1079. continue;
  1080. fc = &f->info[nt->c];
  1081. r.min.x = fc[0].x;
  1082. r.min.y = nt->b->r.min.y;
  1083. r.max.x = fc[1].x;
  1084. r.max.y = nt->b->r.max.y;
  1085. b = allocimage(display, r, nt->b->chan, 0, 0);
  1086. if(b == 0)
  1087. goto nobmem;
  1088. draw(b, r, t->b, nil, r.min);
  1089. doredraw = 0;
  1090. if(Dx(nt->b->r)/fdx != Dx(b->r)/fdx)
  1091. doredraw = 1;
  1092. freeimage(nt->b);
  1093. nt->b = b;
  1094. if(c != nt->c)
  1095. text(nt);
  1096. else{
  1097. if(doredraw)
  1098. redraw(nt);
  1099. else
  1100. drawthing(nt, 0);
  1101. }
  1102. }
  1103. t->mod = 1;
  1104. return;
  1105. }
  1106. mesg("cannot edit %s in file %s", tag, t->name);
  1107. }
  1108. void
  1109. cntledit(char *tag)
  1110. {
  1111. char buf[256];
  1112. ulong l;
  1113. buttons(Up);
  1114. if(type(buf, tag) == 0)
  1115. return;
  1116. if(strcmp(tag, "mag") == 0){
  1117. if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<=0 || l>Maxmag){
  1118. mesg("illegal magnification");
  1119. return;
  1120. }
  1121. mag = l;
  1122. cntl();
  1123. return;
  1124. }
  1125. if(strcmp(tag, "but1")==0
  1126. || strcmp(tag, "but2")==0){
  1127. if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<0 || l>255){
  1128. mesg("illegal value");
  1129. return;
  1130. }
  1131. if(strcmp(tag, "but1") == 0)
  1132. but1val = l;
  1133. else if(strcmp(tag, "but2") == 0)
  1134. but2val = l;
  1135. cntl();
  1136. return;
  1137. }
  1138. if(strcmp(tag, "invert-on-copy")==0){
  1139. if(buf[0]=='y' || buf[0]=='1')
  1140. invert = 1;
  1141. else if(buf[0]=='n' || buf[0]=='0')
  1142. invert = 0;
  1143. else{
  1144. mesg("illegal value");
  1145. return;
  1146. }
  1147. cntl();
  1148. return;
  1149. }
  1150. mesg("cannot edit %s", tag);
  1151. }
  1152. void
  1153. buttons(int ud)
  1154. {
  1155. while((mouse.buttons==0) != ud)
  1156. mouse = emouse();
  1157. }
  1158. Point
  1159. screenpt(Thing *t, Point realp)
  1160. {
  1161. int fdx, n;
  1162. Point p;
  1163. fdx = Dx(editr)-2*Border;
  1164. if(t->mag > 1)
  1165. fdx -= fdx%t->mag;
  1166. p = mulpt(subpt(realp, t->b->r.min), t->mag);
  1167. if(fdx < Dx(t->b->r)*t->mag){
  1168. n = p.x/fdx;
  1169. p.y += n * (Dy(t->b->r)*t->mag+Border);
  1170. p.x -= n * fdx;
  1171. }
  1172. p = addpt(p, t->r.min);
  1173. return p;
  1174. }
  1175. Point
  1176. realpt(Thing *t, Point screenp)
  1177. {
  1178. int fdx, n, dy;
  1179. Point p;
  1180. fdx = (Dx(editr)-2*Border);
  1181. if(t->mag > 1)
  1182. fdx -= fdx%t->mag;
  1183. p.y = screenp.y-t->r.min.y;
  1184. p.x = 0;
  1185. if(fdx < Dx(t->b->r)*t->mag){
  1186. dy = Dy(t->b->r)*t->mag+Border;
  1187. n = (p.y/dy);
  1188. p.x = n * fdx;
  1189. p.y -= n * dy;
  1190. }
  1191. p.x += screenp.x-t->r.min.x;
  1192. p = addpt(divpt(p, t->mag), t->b->r.min);
  1193. return p;
  1194. }
  1195. int
  1196. sweep(int but, Rectangle *r)
  1197. {
  1198. Thing *t;
  1199. Point p, q, lastq;
  1200. esetcursor(&sweep0);
  1201. buttons(Down);
  1202. if(mouse.buttons != (1<<(but-1))){
  1203. buttons(Up);
  1204. esetcursor(0);
  1205. return 0;
  1206. }
  1207. p = mouse.xy;
  1208. for(t=thing; t; t=t->next)
  1209. if(ptinrect(p, t->r))
  1210. break;
  1211. if(t)
  1212. p = screenpt(t, realpt(t, p));
  1213. r->min = p;
  1214. r->max = p;
  1215. esetcursor(&box);
  1216. lastq = ZP;
  1217. while(mouse.buttons == (1<<(but-1))){
  1218. edrawgetrect(insetrect(*r, -Borderwidth), 1);
  1219. mouse = emouse();
  1220. edrawgetrect(insetrect(*r, -Borderwidth), 0);
  1221. q = mouse.xy;
  1222. if(t)
  1223. q = screenpt(t, realpt(t, q));
  1224. if(eqpt(q, lastq))
  1225. continue;
  1226. *r = canonrect(Rpt(p, q));
  1227. lastq = q;
  1228. }
  1229. esetcursor(0);
  1230. if(mouse.buttons){
  1231. buttons(Up);
  1232. return 0;
  1233. }
  1234. return 1;
  1235. }
  1236. void
  1237. openedit(Thing *t, Point pt, int c)
  1238. {
  1239. int x, y;
  1240. Point p;
  1241. Rectangle r;
  1242. Rectangle br;
  1243. Fontchar *fc;
  1244. Thing *nt;
  1245. if(t->b->depth > 8){
  1246. mesg("image has depth %d; can't handle >8", t->b->depth);
  1247. return;
  1248. }
  1249. br = t->b->r;
  1250. if(t->s == 0){
  1251. c = -1;
  1252. /* if big enough to bother, sweep box */
  1253. if(Dx(br)<=16 && Dy(br)<=16)
  1254. r = br;
  1255. else{
  1256. if(!sweep(1, &r))
  1257. return;
  1258. r = rectaddpt(r, subpt(br.min, t->r.min));
  1259. if(!rectclip(&r, br))
  1260. return;
  1261. if(Dx(br) <= 8){
  1262. r.min.x = br.min.x;
  1263. r.max.x = br.max.x;
  1264. }else if(Dx(r) < 4){
  1265. toosmall:
  1266. mesg("rectangle too small");
  1267. return;
  1268. }
  1269. if(Dy(br) <= 8){
  1270. r.min.y = br.min.y;
  1271. r.max.y = br.max.y;
  1272. }else if(Dy(r) < 4)
  1273. goto toosmall;
  1274. }
  1275. }else if(c >= 0){
  1276. fc = &t->s->info[c];
  1277. r.min.x = fc[0].x;
  1278. r.min.y = br.min.y;
  1279. r.max.x = fc[1].x;
  1280. r.max.y = br.min.y + Dy(br);
  1281. }else{
  1282. /* just point at character */
  1283. fc = t->s->info;
  1284. p = addpt(pt, subpt(br.min, t->r.min));
  1285. x = br.min.x;
  1286. y = br.min.y;
  1287. for(c=0; c<t->s->n; c++,fc++){
  1288. again:
  1289. r.min.x = x;
  1290. r.min.y = y;
  1291. r.max.x = x + fc[1].x - fc[0].x;
  1292. r.max.y = y + Dy(br);
  1293. if(ptinrect(p, r))
  1294. goto found;
  1295. if(r.max.x >= br.min.x+Dx(t->r)){
  1296. x -= Dx(t->r);
  1297. y += t->s->height;
  1298. if(fc[1].x > fc[0].x)
  1299. goto again;
  1300. }
  1301. x += fc[1].x - fc[0].x;
  1302. }
  1303. return;
  1304. found:
  1305. r = br;
  1306. r.min.x = fc[0].x;
  1307. r.max.x = fc[1].x;
  1308. }
  1309. nt = malloc(sizeof(Thing));
  1310. if(nt == 0){
  1311. nomem:
  1312. mesg("can't allocate: %r");
  1313. return;
  1314. }
  1315. memset(nt, 0, sizeof(Thing));
  1316. nt->c = c;
  1317. nt->b = allocimage(display, r, t->b->chan, 0, DNofill);
  1318. if(nt->b == 0){
  1319. free(nt);
  1320. goto nomem;
  1321. }
  1322. draw(nt->b, r, t->b, nil, r.min);
  1323. nt->name = strdup(t->name);
  1324. if(nt->name == 0){
  1325. freeimage(nt->b);
  1326. free(nt);
  1327. goto nomem;
  1328. }
  1329. nt->parent = t;
  1330. nt->mag = mag;
  1331. drawthing(nt, 1);
  1332. }
  1333. void
  1334. ckinfo(Thing *t, Rectangle mod)
  1335. {
  1336. int i, j, k, top, bot, n, zero;
  1337. Fontchar *fc;
  1338. Rectangle r;
  1339. Image *b;
  1340. Thing *nt;
  1341. if(t->parent)
  1342. t = t->parent;
  1343. if(t->s==0 || Dy(t->b->r)==0)
  1344. return;
  1345. b = 0;
  1346. /* check bounding boxes */
  1347. fc = &t->s->info[0];
  1348. r.min.y = t->b->r.min.y;
  1349. r.max.y = t->b->r.max.y;
  1350. for(i=0; i<t->s->n; i++, fc++){
  1351. r.min.x = fc[0].x;
  1352. r.max.x = fc[1].x;
  1353. if(!rectXrect(mod, r))
  1354. continue;
  1355. if(b==0 || Dx(b->r)<Dx(r)){
  1356. if(b)
  1357. freeimage(b);
  1358. b = allocimage(display, rectsubpt(r, r.min), t->b->chan, 0, 0);
  1359. if(b == 0){
  1360. mesg("can't alloc image");
  1361. break;
  1362. }
  1363. }
  1364. draw(b, b->r, display->white, nil, ZP);
  1365. draw(b, b->r, t->b, nil, r.min);
  1366. top = 100000;
  1367. bot = 0;
  1368. n = 2+((Dx(r)/8)*t->b->depth);
  1369. for(j=0; j<b->r.max.y; j++){
  1370. memset(data, 0, n);
  1371. unloadimage(b, Rect(b->r.min.x, j, b->r.max.x, j+1), data, sizeof data);
  1372. zero = 1;
  1373. for(k=0; k<n; k++)
  1374. if(data[k]){
  1375. zero = 0;
  1376. break;
  1377. }
  1378. if(!zero){
  1379. if(top > j)
  1380. top = j;
  1381. bot = j+1;
  1382. }
  1383. }
  1384. if(top > j)
  1385. top = 0;
  1386. if(top!=fc->top || bot!=fc->bottom){
  1387. fc->top = top;
  1388. fc->bottom = bot;
  1389. for(nt=thing; nt; nt=nt->next)
  1390. if(nt->parent==t && nt->c==i)
  1391. text(nt);
  1392. }
  1393. }
  1394. if(b)
  1395. freeimage(b);
  1396. }
  1397. void
  1398. twidpix(Thing *t, Point p, int set)
  1399. {
  1400. Image *b, *v;
  1401. int c;
  1402. b = t->b;
  1403. if(!ptinrect(p, b->r))
  1404. return;
  1405. if(set)
  1406. c = but1val;
  1407. else
  1408. c = but2val;
  1409. if(b->chan == GREY8)
  1410. v = greyvalues[c];
  1411. else
  1412. v = values[c];
  1413. draw(b, Rect(p.x, p.y, p.x+1, p.y+1), v, nil, ZP);
  1414. p = screenpt(t, p);
  1415. draw(screen, Rect(p.x, p.y, p.x+t->mag, p.y+t->mag), v, nil, ZP);
  1416. }
  1417. void
  1418. twiddle(Thing *t)
  1419. {
  1420. int set;
  1421. Point p, lastp;
  1422. Image *b;
  1423. Thing *nt;
  1424. Rectangle mod;
  1425. if(mouse.buttons!=1 && mouse.buttons!=2){
  1426. buttons(Up);
  1427. return;
  1428. }
  1429. set = mouse.buttons==1;
  1430. b = t->b;
  1431. lastp = addpt(b->r.min, Pt(-1, -1));
  1432. mod = Rpt(addpt(b->r.max, Pt(1, 1)), lastp);
  1433. while(mouse.buttons){
  1434. p = realpt(t, mouse.xy);
  1435. if(!eqpt(p, lastp)){
  1436. lastp = p;
  1437. if(ptinrect(p, b->r)){
  1438. for(nt=thing; nt; nt=nt->next)
  1439. if(nt->parent==t->parent || nt==t->parent)
  1440. twidpix(nt, p, set);
  1441. if(t->parent)
  1442. t->parent->mod = 1;
  1443. else
  1444. t->mod = 1;
  1445. if(p.x < mod.min.x)
  1446. mod.min.x = p.x;
  1447. if(p.y < mod.min.y)
  1448. mod.min.y = p.y;
  1449. if(p.x >= mod.max.x)
  1450. mod.max.x = p.x+1;
  1451. if(p.y >= mod.max.y)
  1452. mod.max.y = p.y+1;
  1453. }
  1454. }
  1455. mouse = emouse();
  1456. }
  1457. ckinfo(t, mod);
  1458. }
  1459. void
  1460. select(void)
  1461. {
  1462. Thing *t;
  1463. char line[128], buf[128];
  1464. Point p;
  1465. if(ptinrect(mouse.xy, cntlr)){
  1466. scntl(line);
  1467. if(atline(cntlr.min.x, mouse.xy, line, buf)){
  1468. if(mouse.buttons == 1)
  1469. cntledit(buf);
  1470. else
  1471. buttons(Up);
  1472. return;
  1473. }
  1474. return;
  1475. }
  1476. for(t=thing; t; t=t->next){
  1477. if(attext(t, mouse.xy, buf)){
  1478. if(mouse.buttons == 1)
  1479. textedit(t, buf);
  1480. else
  1481. buttons(Up);
  1482. return;
  1483. }
  1484. if(ptinrect(mouse.xy, t->r)){
  1485. if(t->parent == 0){
  1486. if(mouse.buttons == 1){
  1487. p = mouse.xy;
  1488. buttons(Up);
  1489. openedit(t, p, -1);
  1490. }else
  1491. buttons(Up);
  1492. return;
  1493. }
  1494. twiddle(t);
  1495. return;
  1496. }
  1497. }
  1498. }
  1499. void
  1500. twrite(Thing *t)
  1501. {
  1502. int i, j, x, y, fd, ws, ld;
  1503. Biobuf buf;
  1504. Rectangle r;
  1505. if(t->parent)
  1506. t = t->parent;
  1507. esetcursor(&busy);
  1508. fd = create(t->name, OWRITE, 0666);
  1509. if(fd < 0){
  1510. mesg("can't write %s: %r", t->name);
  1511. return;
  1512. }
  1513. if(t->face && t->b->depth <= 4){
  1514. r = t->b->r;
  1515. ld = log2[t->b->depth];
  1516. /* This heuristic reflects peculiarly different formats */
  1517. ws = 4;
  1518. if(t->face == 2) /* cursor file */
  1519. ws = 1;
  1520. else if(Dx(r)<32 || ld==0)
  1521. ws = 2;
  1522. Binit(&buf, fd, OWRITE);
  1523. if(t->face == CURSOR)
  1524. Bprint(&buf, "{");
  1525. for(y=r.min.y; y<r.max.y; y++){
  1526. unloadimage(t->b, Rect(r.min.x, y, r.max.x, y+1), data, sizeof data);
  1527. j = 0;
  1528. for(x=r.min.x; x<r.max.x; j+=ws,x+=ws*8>>ld){
  1529. Bprint(&buf, "0x");
  1530. for(i=0; i<ws; i++)
  1531. Bprint(&buf, "%.2x", data[i+j]);
  1532. Bprint(&buf, ", ");
  1533. }
  1534. if(t->face == CURSOR){
  1535. switch(y){
  1536. case 3: case 7: case 11: case 19: case 23: case 27:
  1537. Bprint(&buf, "\n ");
  1538. break;
  1539. case 15:
  1540. Bprint(&buf, "},\n{");
  1541. break;
  1542. case 31:
  1543. Bprint(&buf, "}\n");
  1544. break;
  1545. }
  1546. }else
  1547. Bprint(&buf, "\n");
  1548. }
  1549. Bterm(&buf);
  1550. }else
  1551. if(writeimage(fd, t->b, 0)<0 || (t->s && writesubfont(fd, t->s)<0)){
  1552. close(fd);
  1553. mesg("can't write %s: %r", t->name);
  1554. }
  1555. t->mod = 0;
  1556. close(fd);
  1557. mesg("wrote %s", t->name);
  1558. }
  1559. void
  1560. tpixels(void)
  1561. {
  1562. Thing *t;
  1563. Point p, lastp;
  1564. esetcursor(&pixel);
  1565. for(;;){
  1566. buttons(Down);
  1567. if(mouse.buttons != 4)
  1568. break;
  1569. for(t=thing; t; t=t->next){
  1570. lastp = Pt(-1, -1);
  1571. if(ptinrect(mouse.xy, t->r)){
  1572. while(ptinrect(mouse.xy, t->r) && mouse.buttons==4){
  1573. p = realpt(t, mouse.xy);
  1574. if(!eqpt(p, lastp)){
  1575. if(p.y != lastp.y)
  1576. unloadimage(t->b, Rect(t->b->r.min.x, p.y, t->b->r.max.x, p.y+1), data, sizeof data);
  1577. mesg("[%d,%d] = %d=0x%ux", p.x, p.y, value(t->b, p.x), value(t->b, p.x));
  1578. lastp = p;
  1579. }
  1580. mouse = emouse();
  1581. }
  1582. goto Continue;
  1583. }
  1584. }
  1585. mouse = emouse();
  1586. Continue:;
  1587. }
  1588. buttons(Up);
  1589. esetcursor(0);
  1590. }
  1591. void
  1592. tclose1(Thing *t)
  1593. {
  1594. Thing *nt;
  1595. if(t == thing)
  1596. thing = t->next;
  1597. else{
  1598. for(nt=thing; nt->next!=t; nt=nt->next)
  1599. ;
  1600. nt->next = t->next;
  1601. }
  1602. do
  1603. for(nt=thing; nt; nt=nt->next)
  1604. if(nt->parent == t){
  1605. tclose1(nt);
  1606. break;
  1607. }
  1608. while(nt);
  1609. if(t->s)
  1610. freesubfont(t->s);
  1611. else
  1612. freeimage(t->b);
  1613. free(t->name);
  1614. free(t);
  1615. }
  1616. void
  1617. tclose(Thing *t)
  1618. {
  1619. Thing *ct;
  1620. if(t->mod){
  1621. mesg("%s modified", t->name);
  1622. t->mod = 0;
  1623. return;
  1624. }
  1625. /* fiddle to save redrawing unmoved things */
  1626. if(t == thing)
  1627. ct = 0;
  1628. else
  1629. for(ct=thing; ct; ct=ct->next)
  1630. if(ct->next==t || ct->next->parent==t)
  1631. break;
  1632. tclose1(t);
  1633. if(ct)
  1634. ct = ct->next;
  1635. else
  1636. ct = thing;
  1637. redraw(ct);
  1638. }
  1639. void
  1640. tread(Thing *t)
  1641. {
  1642. Thing *nt, *new;
  1643. Fontchar *i;
  1644. Rectangle r;
  1645. int nclosed;
  1646. if(t->parent)
  1647. t = t->parent;
  1648. new = tget(t->name);
  1649. if(new == 0)
  1650. return;
  1651. nclosed = 0;
  1652. again:
  1653. for(nt=thing; nt; nt=nt->next)
  1654. if(nt->parent == t){
  1655. if(!rectinrect(nt->b->r, new->b->r)
  1656. || new->b->depth!=nt->b->depth){
  1657. closeit:
  1658. nclosed++;
  1659. nt->parent = 0;
  1660. tclose1(nt);
  1661. goto again;
  1662. }
  1663. if((t->s==0) != (new->s==0))
  1664. goto closeit;
  1665. if((t->face==0) != (new->face==0))
  1666. goto closeit;
  1667. if(t->s){ /* check same char */
  1668. if(nt->c >= new->s->n)
  1669. goto closeit;
  1670. i = &new->s->info[nt->c];
  1671. r.min.x = i[0].x;
  1672. r.max.x = i[1].x;
  1673. r.min.y = new->b->r.min.y;
  1674. r.max.y = new->b->r.max.y;
  1675. if(!eqrect(r, nt->b->r))
  1676. goto closeit;
  1677. }
  1678. nt->parent = new;
  1679. draw(nt->b, nt->b->r, new->b, nil, nt->b->r.min);
  1680. }
  1681. new->next = t->next;
  1682. if(t == thing)
  1683. thing = new;
  1684. else{
  1685. for(nt=thing; nt->next!=t; nt=nt->next)
  1686. ;
  1687. nt->next = new;
  1688. }
  1689. if(t->s)
  1690. freesubfont(t->s);
  1691. else
  1692. freeimage(t->b);
  1693. free(t->name);
  1694. free(t);
  1695. for(nt=thing; nt; nt=nt->next)
  1696. if(nt==new || nt->parent==new)
  1697. if(nclosed == 0)
  1698. drawthing(nt, 0); /* can draw in place */
  1699. else{
  1700. redraw(nt); /* must redraw all below */
  1701. break;
  1702. }
  1703. }
  1704. void
  1705. tchar(Thing *t)
  1706. {
  1707. char buf[256], *p;
  1708. Rune r;
  1709. ulong c, d;
  1710. if(t->s == 0){
  1711. t = t->parent;
  1712. if(t==0 || t->s==0){
  1713. mesg("not a subfont");
  1714. return;
  1715. }
  1716. }
  1717. if(type(buf, "char (hex or character or hex-hex)") == 0)
  1718. return;
  1719. if(utflen(buf) == 1){
  1720. chartorune(&r, buf);
  1721. c = r;
  1722. d = r;
  1723. }else{
  1724. if(!strchr(hex, buf[0])){
  1725. mesg("illegal hex character");
  1726. return;
  1727. }
  1728. c = strtoul(buf, 0, 16);
  1729. d = c;
  1730. p = utfrune(buf, '-');
  1731. if(p){
  1732. d = strtoul(p+1, 0, 16);
  1733. if(d < c){
  1734. mesg("invalid range");
  1735. return;
  1736. }
  1737. }
  1738. }
  1739. c -= t->off;
  1740. d -= t->off;
  1741. while(c <= d){
  1742. if(c<0 || c>=t->s->n){
  1743. mesg("0x%lux not in font %s", c+t->off, t->name);
  1744. return;
  1745. }
  1746. openedit(t, Pt(0, 0), c);
  1747. c++;
  1748. }
  1749. }
  1750. void
  1751. apply(void (*f)(Thing*))
  1752. {
  1753. Thing *t;
  1754. esetcursor(&sight);
  1755. buttons(Down);
  1756. if(mouse.buttons == 4)
  1757. for(t=thing; t; t=t->next)
  1758. if(ptinrect(mouse.xy, t->er)){
  1759. buttons(Up);
  1760. f(t);
  1761. break;
  1762. }
  1763. buttons(Up);
  1764. esetcursor(0);
  1765. }
  1766. int
  1767. complement(Image *t)
  1768. {
  1769. int i, n;
  1770. uchar *buf;
  1771. n = Dy(t->r)*bytesperline(t->r, t->depth);
  1772. buf = malloc(n);
  1773. if(buf == 0)
  1774. return 0;
  1775. unloadimage(t, t->r, buf, n);
  1776. for(i=0; i<n; i++)
  1777. buf[i] = ~buf[i];
  1778. loadimage(t, t->r, buf, n);
  1779. free(buf);
  1780. return 1;
  1781. }
  1782. void
  1783. copy(void)
  1784. {
  1785. Thing *st, *dt, *nt;
  1786. Rectangle sr, dr, fr;
  1787. Image *tmp;
  1788. Point p1, p2;
  1789. int but, up;
  1790. if(!sweep(3, &sr))
  1791. return;
  1792. for(st=thing; st; st=st->next)
  1793. if(rectXrect(sr, st->r))
  1794. break;
  1795. if(st == 0)
  1796. return;
  1797. /* click gives full rectangle */
  1798. if(Dx(sr)<4 && Dy(sr)<4)
  1799. sr = st->r;
  1800. rectclip(&sr, st->r);
  1801. p1 = realpt(st, sr.min);
  1802. p2 = realpt(st, Pt(sr.min.x, sr.max.y));
  1803. up = 0;
  1804. if(p1.x != p2.x){ /* swept across a fold */
  1805. onafold:
  1806. mesg("sweep spans a fold");
  1807. goto Return;
  1808. }
  1809. p2 = realpt(st, sr.max);
  1810. sr.min = p1;
  1811. sr.max = p2;
  1812. fr.min = screenpt(st, sr.min);
  1813. fr.max = screenpt(st, sr.max);
  1814. p1 = subpt(p2, p1); /* diagonal */
  1815. if(p1.x==0 || p1.y==0)
  1816. return;
  1817. border(screen, fr, -1, values[Blue], ZP);
  1818. esetcursor(&box);
  1819. for(; mouse.buttons==0; mouse=emouse()){
  1820. for(dt=thing; dt; dt=dt->next)
  1821. if(ptinrect(mouse.xy, dt->er))
  1822. break;
  1823. if(up)
  1824. edrawgetrect(insetrect(dr, -Borderwidth), 0);
  1825. up = 0;
  1826. if(dt == 0)
  1827. continue;
  1828. dr.max = screenpt(dt, realpt(dt, mouse.xy));
  1829. dr.min = subpt(dr.max, mulpt(p1, dt->mag));
  1830. if(!rectXrect(dr, dt->r))
  1831. continue;
  1832. edrawgetrect(insetrect(dr, -Borderwidth), 1);
  1833. up = 1;
  1834. }
  1835. /* if up==1, we had a hit */
  1836. esetcursor(0);
  1837. if(up)
  1838. edrawgetrect(insetrect(dr, -Borderwidth), 0);
  1839. but = mouse.buttons;
  1840. buttons(Up);
  1841. if(!up || but!=4)
  1842. goto Return;
  1843. dt = 0;
  1844. for(nt=thing; nt; nt=nt->next)
  1845. if(rectXrect(dr, nt->r)){
  1846. if(dt){
  1847. mesg("ambiguous sweep");
  1848. return;
  1849. }
  1850. dt = nt;
  1851. }
  1852. if(dt == 0)
  1853. goto Return;
  1854. p1 = realpt(dt, dr.min);
  1855. p2 = realpt(dt, Pt(dr.min.x, dr.max.y));
  1856. if(p1.x != p2.x)
  1857. goto onafold;
  1858. p2 = realpt(dt, dr.max);
  1859. dr.min = p1;
  1860. dr.max = p2;
  1861. if(invert){
  1862. tmp = allocimage(display, dr, dt->b->chan, 0, 255);
  1863. if(tmp == 0){
  1864. nomem:
  1865. mesg("can't allocate temporary");
  1866. goto Return;
  1867. }
  1868. draw(tmp, dr, st->b, nil, sr.min);
  1869. if(!complement(tmp))
  1870. goto nomem;
  1871. draw(dt->b, dr, tmp, nil, dr.min);
  1872. freeimage(tmp);
  1873. }else
  1874. draw(dt->b, dr, st->b, nil, sr.min);
  1875. if(dt->parent){
  1876. draw(dt->parent->b, dr, dt->b, nil, dr.min);
  1877. dt = dt->parent;
  1878. }
  1879. drawthing(dt, 0);
  1880. for(nt=thing; nt; nt=nt->next)
  1881. if(nt->parent==dt && rectXrect(dr, nt->b->r)){
  1882. draw(nt->b, dr, dt->b, nil, dr.min);
  1883. drawthing(nt, 0);
  1884. }
  1885. ckinfo(dt, dr);
  1886. dt->mod = 1;
  1887. Return:
  1888. /* clear blue box */
  1889. drawthing(st, 0);
  1890. }
  1891. void
  1892. menu(void)
  1893. {
  1894. Thing *t;
  1895. char *mod;
  1896. int sel;
  1897. char buf[256];
  1898. sel = emenuhit(3, &mouse, &menu3);
  1899. switch(sel){
  1900. case Mopen:
  1901. if(type(buf, "file")){
  1902. t = tget(buf);
  1903. if(t)
  1904. drawthing(t, 1);
  1905. }
  1906. break;
  1907. case Mwrite:
  1908. apply(twrite);
  1909. break;
  1910. case Mread:
  1911. apply(tread);
  1912. break;
  1913. case Mchar:
  1914. apply(tchar);
  1915. break;
  1916. case Mcopy:
  1917. copy();
  1918. break;
  1919. case Mpixels:
  1920. tpixels();
  1921. break;
  1922. case Mclose:
  1923. apply(tclose);
  1924. break;
  1925. case Mexit:
  1926. mod = 0;
  1927. for(t=thing; t; t=t->next)
  1928. if(t->mod){
  1929. mod = t->name;
  1930. t->mod = 0;
  1931. }
  1932. if(mod){
  1933. mesg("%s modified", mod);
  1934. break;
  1935. }
  1936. esetcursor(&skull);
  1937. buttons(Down);
  1938. if(mouse.buttons == 4){
  1939. buttons(Up);
  1940. exits(0);
  1941. }
  1942. buttons(Up);
  1943. esetcursor(0);
  1944. break;
  1945. }
  1946. }