tweak.c 39 KB

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