tweak.c 38 KB

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