wind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include <plumb.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. int winid;
  14. void
  15. wininit(Window *w, Window *clone, Rectangle r)
  16. {
  17. Rectangle r1, br;
  18. File *f;
  19. Reffont *rf;
  20. Rune *rp;
  21. int nc;
  22. w->tag.w = w;
  23. w->body.w = w;
  24. w->id = ++winid;
  25. incref(w);
  26. w->ctlfid = ~0;
  27. w->utflastqid = -1;
  28. r1 = r;
  29. r1.max.y = r1.min.y + font->height;
  30. incref(&reffont);
  31. f = fileaddtext(nil, &w->tag);
  32. textinit(&w->tag, f, r1, &reffont, tagcols);
  33. w->tag.what = Tag;
  34. /* tag is a copy of the contents, not a tracked image */
  35. if(clone){
  36. textdelete(&w->tag, 0, w->tag.file->nc, TRUE);
  37. nc = clone->tag.file->nc;
  38. rp = runemalloc(nc);
  39. bufread(clone->tag.file, 0, rp, nc);
  40. textinsert(&w->tag, 0, rp, nc, TRUE);
  41. free(rp);
  42. filereset(w->tag.file);
  43. textsetselect(&w->tag, nc, nc);
  44. }
  45. r1 = r;
  46. r1.min.y += font->height + 1;
  47. if(r1.max.y < r1.min.y)
  48. r1.max.y = r1.min.y;
  49. f = nil;
  50. if(clone){
  51. f = clone->body.file;
  52. w->body.org = clone->body.org;
  53. w->isscratch = clone->isscratch;
  54. rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
  55. }else
  56. rf = rfget(FALSE, FALSE, FALSE, nil);
  57. f = fileaddtext(f, &w->body);
  58. w->body.what = Body;
  59. textinit(&w->body, f, r1, rf, textcols);
  60. r1.min.y -= 1;
  61. r1.max.y = r1.min.y+1;
  62. draw(screen, r1, tagcols[BORD], nil, ZP);
  63. textscrdraw(&w->body);
  64. w->r = r;
  65. w->r.max.y = w->body.r.max.y;
  66. br.min = w->tag.scrollr.min;
  67. br.max.x = br.min.x + Dx(button->r);
  68. br.max.y = br.min.y + Dy(button->r);
  69. draw(screen, br, button, nil, button->r.min);
  70. w->filemenu = TRUE;
  71. w->maxlines = w->body.maxlines;
  72. if(clone){
  73. w->dirty = clone->dirty;
  74. textsetselect(&w->body, clone->body.q0, clone->body.q1);
  75. winsettag(w);
  76. }
  77. }
  78. int
  79. winresize(Window *w, Rectangle r, int safe)
  80. {
  81. Rectangle r1;
  82. int y;
  83. Image *b;
  84. Rectangle br;
  85. r1 = r;
  86. r1.max.y = r1.min.y + font->height;
  87. y = r1.max.y;
  88. if(!safe || !eqrect(w->tag.r, r1)){
  89. y = textresize(&w->tag, r1);
  90. b = button;
  91. if(w->body.file->mod && !w->isdir && !w->isscratch)
  92. b = modbutton;
  93. br.min = w->tag.scrollr.min;
  94. br.max.x = br.min.x + Dx(b->r);
  95. br.max.y = br.min.y + Dy(b->r);
  96. draw(screen, br, b, nil, b->r.min);
  97. }
  98. if(!safe || !eqrect(w->body.r, r1)){
  99. if(y+1+font->height > r.max.y){ /* no body */
  100. r1.min.y = y;
  101. r1.max.y = y;
  102. textresize(&w->body, r1);
  103. w->r = r;
  104. w->r.max.y = y;
  105. return y;
  106. }
  107. r1 = r;
  108. r1.min.y = y;
  109. r1.max.y = y + 1;
  110. draw(screen, r1, tagcols[BORD], nil, ZP);
  111. r1.min.y = y + 1;
  112. r1.max.y = r.max.y;
  113. y = textresize(&w->body, r1);
  114. w->r = r;
  115. w->r.max.y = y;
  116. textscrdraw(&w->body);
  117. }
  118. w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines));
  119. return w->r.max.y;
  120. }
  121. void
  122. winlock1(Window *w, int owner)
  123. {
  124. incref(w);
  125. qlock(w);
  126. w->owner = owner;
  127. }
  128. void
  129. winlock(Window *w, int owner)
  130. {
  131. int i;
  132. File *f;
  133. f = w->body.file;
  134. for(i=0; i<f->ntext; i++)
  135. winlock1(f->text[i]->w, owner);
  136. }
  137. void
  138. winunlock(Window *w)
  139. {
  140. int i;
  141. File *f;
  142. f = w->body.file;
  143. for(i=0; i<f->ntext; i++){
  144. w = f->text[i]->w;
  145. w->owner = 0;
  146. qunlock(w);
  147. winclose(w);
  148. /* winclose() can change up f->text; beware */
  149. if(f->ntext>0 && w != f->text[i]->w)
  150. --i; /* winclose() deleted window */
  151. }
  152. }
  153. void
  154. winmousebut(Window *w)
  155. {
  156. moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
  157. }
  158. void
  159. windirfree(Window *w)
  160. {
  161. int i;
  162. Dirlist *dl;
  163. if(w->isdir){
  164. for(i=0; i<w->ndl; i++){
  165. dl = w->dlp[i];
  166. free(dl->r);
  167. free(dl);
  168. }
  169. free(w->dlp);
  170. }
  171. w->dlp = nil;
  172. w->ndl = 0;
  173. }
  174. void
  175. winclose(Window *w)
  176. {
  177. int i;
  178. if(decref(w) == 0){
  179. windirfree(w);
  180. textclose(&w->tag);
  181. textclose(&w->body);
  182. if(activewin == w)
  183. activewin = nil;
  184. for(i=0; i<w->nincl; i++)
  185. free(w->incl[i]);
  186. free(w->incl);
  187. free(w->events);
  188. free(w);
  189. }
  190. }
  191. void
  192. windelete(Window *w)
  193. {
  194. Xfid *x;
  195. x = w->eventx;
  196. if(x){
  197. w->nevents = 0;
  198. free(w->events);
  199. w->events = nil;
  200. w->eventx = nil;
  201. sendp(x->c, nil); /* wake him up */
  202. }
  203. }
  204. void
  205. winundo(Window *w, int isundo)
  206. {
  207. Text *body;
  208. int i;
  209. File *f;
  210. Window *v;
  211. w->utflastqid = -1;
  212. body = &w->body;
  213. fileundo(body->file, isundo, &body->q0, &body->q1);
  214. textshow(body, body->q0, body->q1, 1);
  215. f = body->file;
  216. for(i=0; i<f->ntext; i++){
  217. v = f->text[i]->w;
  218. v->dirty = (f->seq != v->putseq);
  219. if(v != w){
  220. v->body.q0 = v->body.p0+v->body.org;
  221. v->body.q1 = v->body.p1+v->body.org;
  222. }
  223. }
  224. winsettag(w);
  225. }
  226. void
  227. winsetname(Window *w, Rune *name, int n)
  228. {
  229. Text *t;
  230. Window *v;
  231. int i;
  232. t = &w->body;
  233. if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
  234. return;
  235. w->isscratch = FALSE;
  236. if(n>=6 && runeeq(L"/guide", 6, name+(n-6), 6))
  237. w->isscratch = TRUE;
  238. else if(n>=7 && runeeq(L"+Errors", 7, name+(n-7), 7))
  239. w->isscratch = TRUE;
  240. filesetname(t->file, name, n);
  241. for(i=0; i<t->file->ntext; i++){
  242. v = t->file->text[i]->w;
  243. winsettag(v);
  244. v->isscratch = w->isscratch;
  245. }
  246. }
  247. void
  248. wintype(Window *w, Text *t, Rune r)
  249. {
  250. int i;
  251. texttype(t, r);
  252. if(t->what == Body)
  253. for(i=0; i<t->file->ntext; i++)
  254. textscrdraw(t->file->text[i]);
  255. winsettag(w);
  256. }
  257. void
  258. wincleartag(Window *w)
  259. {
  260. int i, n;
  261. Rune *r;
  262. /* w must be committed */
  263. n = w->tag.file->nc;
  264. r = runemalloc(n);
  265. bufread(w->tag.file, 0, r, n);
  266. for(i=0; i<n; i++)
  267. if(r[i]==' ' || r[i]=='\t')
  268. break;
  269. for(; i<n; i++)
  270. if(r[i] == '|')
  271. break;
  272. if(i == n)
  273. return;
  274. i++;
  275. textdelete(&w->tag, i, n, TRUE);
  276. free(r);
  277. w->tag.file->mod = FALSE;
  278. if(w->tag.q0 > i)
  279. w->tag.q0 = i;
  280. if(w->tag.q1 > i)
  281. w->tag.q1 = i;
  282. textsetselect(&w->tag, w->tag.q0, w->tag.q1);
  283. }
  284. void
  285. winsettag1(Window *w)
  286. {
  287. int i, j, k, n, bar, dirty;
  288. Rune *new, *old, *r;
  289. Image *b;
  290. uint q0, q1;
  291. Rectangle br;
  292. /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
  293. if(w->tag.ncache!=0 || w->tag.file->mod)
  294. wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
  295. old = runemalloc(w->tag.file->nc+1);
  296. bufread(w->tag.file, 0, old, w->tag.file->nc);
  297. old[w->tag.file->nc] = '\0';
  298. for(i=0; i<w->tag.file->nc; i++)
  299. if(old[i]==' ' || old[i]=='\t')
  300. break;
  301. if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
  302. textdelete(&w->tag, 0, i, TRUE);
  303. textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
  304. free(old);
  305. old = runemalloc(w->tag.file->nc+1);
  306. bufread(w->tag.file, 0, old, w->tag.file->nc);
  307. old[w->tag.file->nc] = '\0';
  308. }
  309. new = runemalloc(w->body.file->nname+100);
  310. i = 0;
  311. runemove(new+i, w->body.file->name, w->body.file->nname);
  312. i += w->body.file->nname;
  313. runemove(new+i, L" Del Snarf", 10);
  314. i += 10;
  315. if(w->filemenu){
  316. if(w->body.file->delta.nc>0 || w->body.ncache){
  317. runemove(new+i, L" Undo", 5);
  318. i += 5;
  319. }
  320. if(w->body.file->epsilon.nc > 0){
  321. runemove(new+i, L" Redo", 5);
  322. i += 5;
  323. }
  324. dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
  325. if(!w->isdir && dirty){
  326. runemove(new+i, L" Put", 4);
  327. i += 4;
  328. }
  329. }
  330. if(w->isdir){
  331. runemove(new+i, L" Get", 4);
  332. i += 4;
  333. }
  334. runemove(new+i, L" |", 2);
  335. i += 2;
  336. r = runestrchr(old, '|');
  337. if(r)
  338. k = r-old+1;
  339. else{
  340. k = w->tag.file->nc;
  341. if(w->body.file->seq == 0){
  342. runemove(new+i, L" Look ", 6);
  343. i += 6;
  344. }
  345. }
  346. if(runeeq(new, i, old, k) == FALSE){
  347. n = k;
  348. if(n > i)
  349. n = i;
  350. for(j=0; j<n; j++)
  351. if(old[j] != new[j])
  352. break;
  353. q0 = w->tag.q0;
  354. q1 = w->tag.q1;
  355. textdelete(&w->tag, j, k, TRUE);
  356. textinsert(&w->tag, j, new+j, i-j, TRUE);
  357. /* try to preserve user selection */
  358. r = runestrchr(old, '|');
  359. if(r){
  360. bar = r-old;
  361. if(q0 > bar){
  362. bar = (runestrchr(new, '|')-new)-bar;
  363. w->tag.q0 = q0+bar;
  364. w->tag.q1 = q1+bar;
  365. }
  366. }
  367. }
  368. free(old);
  369. free(new);
  370. w->tag.file->mod = FALSE;
  371. n = w->tag.file->nc+w->tag.ncache;
  372. if(w->tag.q0 > n)
  373. w->tag.q0 = n;
  374. if(w->tag.q1 > n)
  375. w->tag.q1 = n;
  376. textsetselect(&w->tag, w->tag.q0, w->tag.q1);
  377. b = button;
  378. if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
  379. b = modbutton;
  380. br.min = w->tag.scrollr.min;
  381. br.max.x = br.min.x + Dx(b->r);
  382. br.max.y = br.min.y + Dy(b->r);
  383. draw(screen, br, b, nil, b->r.min);
  384. }
  385. void
  386. winsettag(Window *w)
  387. {
  388. int i;
  389. File *f;
  390. Window *v;
  391. f = w->body.file;
  392. for(i=0; i<f->ntext; i++){
  393. v = f->text[i]->w;
  394. if(v->col->safe || v->body.maxlines>0)
  395. winsettag1(v);
  396. }
  397. }
  398. void
  399. wincommit(Window *w, Text *t)
  400. {
  401. Rune *r;
  402. int i;
  403. File *f;
  404. textcommit(t, TRUE);
  405. f = t->file;
  406. if(f->ntext > 1)
  407. for(i=0; i<f->ntext; i++)
  408. textcommit(f->text[i], FALSE); /* no-op for t */
  409. if(t->what == Body)
  410. return;
  411. r = runemalloc(w->tag.file->nc);
  412. bufread(w->tag.file, 0, r, w->tag.file->nc);
  413. for(i=0; i<w->tag.file->nc; i++)
  414. if(r[i]==' ' || r[i]=='\t')
  415. break;
  416. if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
  417. seq++;
  418. filemark(w->body.file);
  419. w->body.file->mod = TRUE;
  420. w->dirty = TRUE;
  421. winsetname(w, r, i);
  422. winsettag(w);
  423. }
  424. free(r);
  425. }
  426. void
  427. winaddincl(Window *w, Rune *r, int n)
  428. {
  429. char *a;
  430. Dir *d;
  431. Runestr rs;
  432. a = runetobyte(r, n);
  433. d = dirstat(a);
  434. if(d == nil){
  435. if(a[0] == '/')
  436. goto Rescue;
  437. rs = dirname(&w->body, r, n);
  438. r = rs.r;
  439. n = rs.nr;
  440. free(a);
  441. a = runetobyte(r, n);
  442. d = dirstat(a);
  443. if(d == nil)
  444. goto Rescue;
  445. r = runerealloc(r, n+1);
  446. r[n] = 0;
  447. }
  448. free(a);
  449. if((d->qid.type&QTDIR) == 0){
  450. free(d);
  451. warning(nil, "%s: not a directory\n", a);
  452. free(r);
  453. return;
  454. }
  455. free(d);
  456. w->nincl++;
  457. w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
  458. memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
  459. w->incl[0] = runemalloc(n+1);
  460. runemove(w->incl[0], r, n);
  461. free(r);
  462. return;
  463. Rescue:
  464. warning(nil, "%s: %r\n", a);
  465. free(r);
  466. free(a);
  467. return;
  468. }
  469. int
  470. winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
  471. {
  472. if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
  473. return TRUE;
  474. if(!conservative && w->nopen[QWevent]>0)
  475. return TRUE;
  476. if(w->dirty){
  477. if(w->body.file->nname)
  478. warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
  479. else{
  480. if(w->body.file->nc < 100) /* don't whine if it's too small */
  481. return TRUE;
  482. warning(nil, "unnamed file modified\n");
  483. }
  484. w->dirty = FALSE;
  485. return FALSE;
  486. }
  487. return TRUE;
  488. }
  489. void
  490. winctlprint(Window *w, char *buf, int fonts)
  491. {
  492. int n;
  493. n = sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->nc,
  494. w->body.file->nc, w->isdir, w->dirty);
  495. if(fonts)
  496. sprint(buf+n, "%11d %s" , Dx(w->body.r), w->body.reffont->f->name);
  497. }
  498. void
  499. winevent(Window *w, char *fmt, ...)
  500. {
  501. int n;
  502. char *b;
  503. Xfid *x;
  504. va_list arg;
  505. if(w->nopen[QWevent] == 0)
  506. return;
  507. if(w->owner == 0)
  508. error("no window owner");
  509. va_start(arg, fmt);
  510. b = vsmprint(fmt, arg);
  511. va_end(arg);
  512. if(b == nil)
  513. error("vsmprint failed");
  514. n = strlen(b);
  515. w->events = realloc(w->events, w->nevents+1+n);
  516. w->events[w->nevents++] = w->owner;
  517. memmove(w->events+w->nevents, b, n);
  518. free(b);
  519. w->nevents += n;
  520. x = w->eventx;
  521. if(x){
  522. w->eventx = nil;
  523. sendp(x->c, nil);
  524. }
  525. }