windw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #include "lib9.h"
  2. #include "draw.h"
  3. #include "tk.h"
  4. #include "canvs.h"
  5. #include "textw.h"
  6. #include "kernel.h"
  7. TkCtxt*
  8. tknewctxt(Display *d)
  9. {
  10. TkCtxt *c;
  11. c = malloc(sizeof(TkCtxt));
  12. if(c == nil)
  13. return nil;
  14. c->lock = libqlalloc();
  15. if(c->lock == nil){
  16. free(c);
  17. return nil;
  18. }
  19. if (tkextnnewctxt(c) != 0) {
  20. free(c->lock);
  21. free(c);
  22. return nil;
  23. }
  24. c->display = d;
  25. return c;
  26. }
  27. void
  28. tkfreectxt(TkCtxt *c)
  29. {
  30. int locked;
  31. Display *d;
  32. if(c == nil)
  33. return;
  34. tkextnfreectxt(c);
  35. d = c->display;
  36. locked = lockdisplay(d);
  37. tkfreecolcache(c);
  38. freeimage(c->i);
  39. freeimage(c->ia);
  40. if(locked)
  41. unlockdisplay(d);
  42. libqlfree(c->lock);
  43. free(c);
  44. }
  45. Image*
  46. tkitmp(TkEnv *e, Point p, int fillcol)
  47. {
  48. Image *i, **ip;
  49. TkTop *t;
  50. TkCtxt *ti;
  51. Display *d;
  52. Rectangle r;
  53. ulong pix;
  54. int alpha;
  55. t = e->top;
  56. ti = t->ctxt;
  57. d = t->display;
  58. pix = e->colors[fillcol];
  59. alpha = (pix & 0xff) != 0xff;
  60. ip = alpha ? &ti->ia : &ti->i;
  61. if(*ip != nil) {
  62. i = *ip;
  63. if(p.x <= i->r.max.x && p.y <= i->r.max.y) {
  64. r.min = ZP;
  65. r.max = p;
  66. if (alpha)
  67. drawop(i, r, nil, nil, ZP, Clear);
  68. draw(i, r, tkgc(e, fillcol), nil, ZP);
  69. return i;
  70. }
  71. r = i->r;
  72. freeimage(i);
  73. if(p.x < r.max.x)
  74. p.x = r.max.x;
  75. if(p.y < r.max.y)
  76. p.y = r.max.y;
  77. }
  78. r.min = ZP;
  79. r.max = p;
  80. *ip = allocimage(d, r, alpha?RGBA32:d->image->chan, 0, pix);
  81. return *ip;
  82. }
  83. void
  84. tkgeomchg(Tk *tk, TkGeom *g, int bd)
  85. {
  86. int w, h;
  87. void (*geomfn)(Tk*);
  88. if(memcmp(&tk->req, g, sizeof(TkGeom)) == 0 && bd == tk->borderwidth)
  89. return;
  90. geomfn = tkmethod[tk->type]->geom;
  91. if(geomfn != nil)
  92. geomfn(tk);
  93. if(tk->master != nil) {
  94. tkpackqit(tk->master);
  95. tkrunpack(tk->env->top);
  96. }
  97. else
  98. if(tk->geom != nil) {
  99. w = tk->req.width;
  100. h = tk->req.height;
  101. tk->req.width = 0;
  102. tk->req.height = 0;
  103. tk->geom(tk, tk->act.x, tk->act.y, w, h);
  104. if (tk->slave) {
  105. tkpackqit(tk);
  106. tkrunpack(tk->env->top);
  107. }
  108. }
  109. tkdeliver(tk, TkConfigure, g);
  110. }
  111. /*
  112. * return the widget within tk with by point p (in widget coords)
  113. */
  114. Tk*
  115. tkinwindow(Tk *tk, Point p, int descend)
  116. {
  117. Tk *f;
  118. Point q;
  119. if (ptinrect(p, tkrect(tk, 1)) == 0)
  120. return nil;
  121. for (;;) {
  122. if (descend && tkmethod[tk->type]->inwindow != nil)
  123. f = tkmethod[tk->type]->inwindow(tk, &p);
  124. else {
  125. q = p;
  126. for (f = tk->slave; f; f = f->next) {
  127. q.x = p.x - (f->act.x + f->borderwidth);
  128. q.y = p.y - (f->act.y + f->borderwidth);
  129. if (ptinrect(q, tkrect(f, 1)))
  130. break;
  131. }
  132. p = q;
  133. }
  134. if (f == nil || f == tk)
  135. return tk;
  136. tk = f;
  137. }
  138. }
  139. Tk*
  140. tkfindfocus(TkTop *t, int x, int y, int descend)
  141. {
  142. Point p, q;
  143. Tk *tk, *f;
  144. TkWin *tkw;
  145. p.x = x;
  146. p.y = y;
  147. for(f = t->windows; f != nil; f = TKobj(TkWin, f)->next) {
  148. assert(f->flag&Tkwindow);
  149. if(f->flag & Tkmapped) {
  150. tkw = TKobj(TkWin, f);
  151. q.x = p.x - (tkw->act.x+f->borderwidth);
  152. q.y = p.y - (tkw->act.y+f->borderwidth);
  153. tk = tkinwindow(f, q, descend);
  154. if(tk != nil)
  155. return tk;
  156. }
  157. }
  158. return nil;
  159. }
  160. void
  161. tkmovewin(Tk *tk, Point p)
  162. {
  163. TkWin *tkw;
  164. if((tk->flag & Tkwindow) == 0)
  165. return;
  166. tkw = TKobj(TkWin, tk);
  167. if(! eqpt(p, tkw->req)){
  168. tkw->req = p;
  169. tkw->changed = 1;
  170. }
  171. }
  172. void
  173. tkmoveresize(Tk *tk, int x, int y, int w, int h)
  174. {
  175. TkWin *tkw;
  176. USED(x);
  177. USED(y);
  178. assert(tk->flag&Tkwindow);
  179. tkw = TKobj(TkWin, tk);
  180. if(w < 0)
  181. w = 0;
  182. if(h < 0)
  183. h = 0;
  184. //print("moveresize %s %d %d +[%d %d], callerpc %lux\n", tk->name->name, x, y, w, h, getcallerpc(&tk));
  185. tk->req.width = w;
  186. tk->req.height = h;
  187. tk->act = tk->req;
  188. /* XXX perhaps should actually suspend the window here? */
  189. tkw->changed = 1;
  190. }
  191. static void
  192. tkexterncreatewin(Tk *tk, Rectangle r)
  193. {
  194. TkWin *tkw;
  195. TkTop *top;
  196. char *name;
  197. top = tk->env->top;
  198. tkw = TKobj(TkWin, tk);
  199. /*
  200. * for a choicebutton menu, use the name of the choicebutton which created it
  201. */
  202. if(tk->name == nil){
  203. name = tkw->cbname;
  204. assert(name != nil);
  205. } else
  206. name = tk->name->name;
  207. tkw->reqid++;
  208. tkwreq(top, "!reshape %s %d %d %d %d %d", name, tkw->reqid, r.min.x, r.min.y, r.max.x, r.max.y);
  209. tkw->changed = 0;
  210. tk->flag |= Tksuspended;
  211. }
  212. /*
  213. * return non-zero if the window size has changed (XXX choose better return value/function name!)
  214. */
  215. int
  216. tkupdatewinsize(Tk *tk)
  217. {
  218. TkWin *tkw;
  219. Image *previ;
  220. Rectangle r, or;
  221. int bw2;
  222. tkw = TKobj(TkWin, tk);
  223. bw2 = 2*tk->borderwidth;
  224. r.min.x = tkw->req.x;
  225. r.min.y = tkw->req.y;
  226. r.max.x = r.min.x + tk->act.width + bw2;
  227. r.max.y = r.min.y + tk->act.height + bw2;
  228. previ = tkw->image;
  229. if(previ != nil){
  230. or.min.x = tkw->act.x;
  231. or.min.y = tkw->act.y;
  232. or.max.x = tkw->act.x + Dx(previ->r);
  233. or.max.y = tkw->act.y + Dy(previ->r);
  234. if(eqrect(or, r))
  235. return 0;
  236. }
  237. tkexterncreatewin(tk, r);
  238. return 1;
  239. }
  240. static char*
  241. tkdrawslaves1(Tk *tk, Point orig, Image *dst, int *dirty)
  242. {
  243. Tk *f;
  244. char *e = nil;
  245. Point worig;
  246. Rectangle r, oclip;
  247. worig.x = orig.x + tk->act.x + tk->borderwidth;
  248. worig.y = orig.y + tk->act.y + tk->borderwidth;
  249. r = rectaddpt(tk->dirty, worig);
  250. if (Dx(r) > 0 && rectXrect(r, dst->clipr)) {
  251. e = tkmethod[tk->type]->draw(tk, orig);
  252. tk->dirty = bbnil;
  253. *dirty = 1;
  254. }
  255. if(e != nil)
  256. return e;
  257. /*
  258. * grids need clipping
  259. * XXX BUG: they can't, 'cos text widgets don't clip appropriately.
  260. */
  261. if (tk->grid != nil) {
  262. r = rectaddpt(tkrect(tk, 0), worig);
  263. if (rectclip(&r, dst->clipr) == 0)
  264. return nil;
  265. oclip = dst->clipr;
  266. replclipr(dst, 0, r);
  267. }
  268. for(f = tk->slave; e == nil && f; f = f->next)
  269. e = tkdrawslaves1(f, worig, dst, dirty);
  270. if (tk->grid != nil)
  271. replclipr(dst, 0, oclip);
  272. return e;
  273. }
  274. char*
  275. tkdrawslaves(Tk *tk, Point orig, int *dirty)
  276. {
  277. Image *i;
  278. char *e;
  279. i = tkimageof(tk);
  280. if (i == nil)
  281. return nil;
  282. e = tkdrawslaves1(tk, orig, i, dirty);
  283. return e;
  284. }
  285. char*
  286. tkupdate(TkTop *t)
  287. {
  288. Tk* tk;
  289. int locked;
  290. TkWin *tkw;
  291. Display *d;
  292. char *e;
  293. int dirty = 0;
  294. if(t->noupdate)
  295. return nil;
  296. d = t->display;
  297. locked = lockdisplay(d);
  298. tk = t->windows;
  299. while(tk) {
  300. tkw = TKobj(TkWin, tk);
  301. if((tk->flag & (Tkmapped|Tksuspended)) == Tkmapped) {
  302. if (tkupdatewinsize(tk) == 0){
  303. e = tkdrawslaves(tk, ZP, &dirty);
  304. if(e != nil)
  305. return e;
  306. }
  307. }
  308. tk = tkw->next;
  309. }
  310. if (dirty || t->dirty) {
  311. flushimage(d, 1);
  312. t->dirty = 0;
  313. }
  314. if(locked)
  315. unlockdisplay(d);
  316. return nil;
  317. }
  318. int
  319. tkischild(Tk *tk, Tk *child)
  320. {
  321. while(child != nil && child != tk){
  322. if(child->master)
  323. child = child->master;
  324. else
  325. child = child->parent;
  326. }
  327. return child == tk;
  328. }
  329. void
  330. tksetbits(Tk *tk, int mask)
  331. {
  332. tk->flag |= mask;
  333. for(tk = tk->slave; tk; tk = tk->next)
  334. tksetbits(tk, mask);
  335. }
  336. char*
  337. tkmap(Tk *tk)
  338. {
  339. /*
  340. is this necessary?
  341. tkw = TKobj(TkWin, tk);
  342. if(tkw->image != nil)
  343. tkwreq(tk->env->top, "raise %s", tk->name->name);
  344. */
  345. if(tk->flag & Tkmapped)
  346. return nil;
  347. tk->flag |= Tkmapped;
  348. tkmoveresize(tk, 0, 0, tk->act.width, tk->act.height);
  349. tkdeliver(tk, TkMap, nil);
  350. return nil;
  351. //tkupdate(tk->env->top);
  352. }
  353. void
  354. tkunmap(Tk *tk)
  355. {
  356. TkTop *t;
  357. TkCtxt *c;
  358. while(tk->master)
  359. tk = tk->master;
  360. if((tk->flag & Tkmapped) == 0)
  361. return;
  362. t = tk->env->top;
  363. c = t->ctxt;
  364. if(tkischild(tk, c->mgrab))
  365. tksetmgrab(t, nil);
  366. if(tkischild(tk, c->entered)){
  367. tkdeliver(c->entered, TkLeave, nil);
  368. c->entered = nil;
  369. }
  370. if(tk == t->root)
  371. tksetglobalfocus(t, 0);
  372. tk->flag &= ~(Tkmapped|Tksuspended);
  373. tkdestroywinimage(tk);
  374. tkdeliver(tk, TkUnmap, nil);
  375. tkenterleave(t);
  376. /* XXX should unmap menus too */
  377. }
  378. Image*
  379. tkimageof(Tk *tk)
  380. {
  381. while(tk) {
  382. if(tk->flag & Tkwindow)
  383. return TKobj(TkWin, tk)->image;
  384. if(tk->parent != nil) {
  385. tk = tk->parent;
  386. switch(tk->type) {
  387. case TKmenu:
  388. return TKobj(TkWin, tk)->image;
  389. case TKcanvas:
  390. return TKobj(TkCanvas, tk)->image;
  391. case TKtext:
  392. return TKobj(TkText, tk)->image;
  393. }
  394. abort();
  395. }
  396. tk = tk->master;
  397. }
  398. return nil;
  399. }
  400. void
  401. tktopopt(Tk *tk, char *opt)
  402. {
  403. TkTop *t;
  404. TkWin *tkw;
  405. TkOptab tko[4];
  406. tkw = TKobj(TkWin, tk);
  407. t = tk->env->top;
  408. tko[0].ptr = tkw;
  409. tko[0].optab = tktop;
  410. tko[1].ptr = tk;
  411. tko[1].optab = tkgeneric;
  412. tko[2].ptr = t;
  413. tko[2].optab = tktopdbg;
  414. tko[3].ptr = nil;
  415. tkparse(t, opt, tko, nil);
  416. }
  417. /* general compare - compare top-left corners, y takes priority */
  418. static int
  419. tkfcmpgen(void *ap, void *bp)
  420. {
  421. TkWinfo *a = ap, *b = bp;
  422. if (a->r.min.y > b->r.min.y)
  423. return 1;
  424. if (a->r.min.y < b->r.min.y)
  425. return -1;
  426. if (a->r.min.x > b->r.min.x)
  427. return 1;
  428. if (a->r.min.x < b->r.min.x)
  429. return -1;
  430. return 0;
  431. }
  432. /* compare x-coords only */
  433. static int
  434. tkfcmpx(void *ap, void *bp)
  435. {
  436. TkWinfo *a = ap, *b = bp;
  437. return a->r.min.x - b->r.min.x;
  438. }
  439. /* compare y-coords only */
  440. static int
  441. tkfcmpy(void *ap, void *bp)
  442. {
  443. TkWinfo *a = ap, *b = bp;
  444. return a->r.min.y - b->r.min.y;
  445. }
  446. static void
  447. tkfintervalintersect(int min1, int max1, int min2, int max2, int *min, int *max)
  448. {
  449. if (min1 < min2)
  450. min1 = min2;
  451. if (max1 > max2)
  452. max1 = max2;
  453. if (max1 > min1) {
  454. *min = min1;
  455. *max = max1;
  456. } else
  457. *max = *min; /* no intersection */
  458. }
  459. void
  460. tksortfocusorder(TkWinfo *inf, int n)
  461. {
  462. int i;
  463. Rectangle overlap, r;
  464. int (*cmpfn)(void*, void*);
  465. overlap = inf[0].r;
  466. for (i = 0; i < n; i++) {
  467. r = inf[i].r;
  468. tkfintervalintersect(overlap.min.x, overlap.max.x,
  469. r.min.x, r.max.x, &overlap.min.x, &overlap.max.x);
  470. tkfintervalintersect(overlap.min.y, overlap.max.y,
  471. r.min.y, r.max.y, &overlap.min.y, &overlap.max.y);
  472. }
  473. if (Dx(overlap) > 0)
  474. cmpfn = tkfcmpy;
  475. else if (Dy(overlap) > 0)
  476. cmpfn = tkfcmpx;
  477. else
  478. cmpfn = tkfcmpgen;
  479. qsort(inf, n, sizeof(*inf), cmpfn);
  480. }
  481. void
  482. tkappendfocusorder(Tk *tk)
  483. {
  484. TkTop *tkt;
  485. tkt = tk->env->top;
  486. if (tk->flag & Tktakefocus)
  487. tkt->focusorder[tkt->nfocus++] = tk;
  488. if (tkmethod[tk->type]->focusorder != nil)
  489. tkmethod[tk->type]->focusorder(tk);
  490. }
  491. void
  492. tkbuildfocusorder(TkTop *tkt)
  493. {
  494. Tk *tk;
  495. int n;
  496. if (tkt->focusorder != nil)
  497. free(tkt->focusorder);
  498. n = 0;
  499. for (tk = tkt->root; tk != nil; tk = tk->siblings)
  500. if (tk->flag & Tktakefocus)
  501. n++;
  502. if (n == 0) {
  503. tkt->focusorder = nil;
  504. return;
  505. }
  506. tkt->focusorder = malloc(sizeof(*tkt->focusorder) * n);
  507. tkt->nfocus = 0;
  508. if (tkt->focusorder == nil)
  509. return;
  510. tkappendfocusorder(tkt->root);
  511. }
  512. void
  513. tkdirtyfocusorder(TkTop *tkt)
  514. {
  515. free(tkt->focusorder);
  516. tkt->focusorder = nil;
  517. tkt->nfocus = 0;
  518. }
  519. #define O(t, e) ((long)(&((t*)0)->e))
  520. #define OA(t, e) ((long)(((t*)0)->e))
  521. typedef struct TkSee TkSee;
  522. struct TkSee {
  523. int r[4];
  524. int p[2];
  525. int query;
  526. };
  527. static
  528. TkOption seeopts[] = {
  529. "rectangle", OPTfrac, OA(TkSee, r), IAUX(4),
  530. "point", OPTfrac, OA(TkSee, p), IAUX(2),
  531. "where", OPTbool, O(TkSee, query), nil,
  532. nil
  533. };
  534. char*
  535. tkseecmd(TkTop *t, char *arg, char **ret)
  536. {
  537. TkOptab tko[2];
  538. TkSee opts;
  539. TkName *names;
  540. Tk *tk;
  541. char *e;
  542. Rectangle vr;
  543. Point vp;
  544. opts.r[0] = bbnil.min.x;
  545. opts.r[1] = bbnil.min.y;
  546. opts.r[2] = bbnil.max.x;
  547. opts.r[3] = bbnil.max.y;
  548. opts.p[0] = bbnil.max.x;
  549. opts.p[1] = bbnil.max.y;
  550. opts.query = 0;
  551. tko[0].ptr = &opts;
  552. tko[0].optab = seeopts;
  553. tko[1].ptr = nil;
  554. names = nil;
  555. e = tkparse(t, arg, tko, &names);
  556. if (e != nil)
  557. return e;
  558. if (names == nil)
  559. return TkBadwp;
  560. tk = tklook(t, names->name, 0);
  561. tkfreename(names);
  562. if (tk == nil)
  563. return TkBadwp;
  564. if (opts.query) {
  565. if (!tkvisiblerect(tk, &vr))
  566. return nil;
  567. /* XXX should this be converted into screen coords? */
  568. return tkvalue(ret, "%d %d %d %d", vr.min.x, vr.min.y, vr.max.x, vr.max.y);
  569. }
  570. vr.min.x = opts.r[0];
  571. vr.min.y = opts.r[1];
  572. vr.max.x = opts.r[2];
  573. vr.max.y = opts.r[3];
  574. vp.x = opts.p[0];
  575. vp.y = opts.p[1];
  576. if (eqrect(vr, bbnil))
  577. vr = tkrect(tk, 1);
  578. if (eqpt(vp, bbnil.max))
  579. vp = vr.min;
  580. tksee(tk, vr, vp);
  581. return nil;
  582. }
  583. /*
  584. * make rectangle r in widget tk visible if possible;
  585. * if not possible, at least make point p visible.
  586. */
  587. void
  588. tksee(Tk *tk, Rectangle r, Point p)
  589. {
  590. Point g;
  591. //print("tksee %R, %P in %s\n", r, p, tk->name->name);
  592. g = Pt(tk->borderwidth, tk->borderwidth);
  593. if(tk->parent != nil) {
  594. g = addpt(g, tkmethod[tk->parent->type]->relpos(tk));
  595. tk = tk->parent;
  596. } else {
  597. g.x += tk->act.x;
  598. g.y += tk->act.y;
  599. tk = tk->master;
  600. }
  601. r = rectaddpt(r, g);
  602. p = addpt(p, g);
  603. while (tk != nil) {
  604. if (tkmethod[tk->type]->see != nil){
  605. //print("see r %R, p %P in %s\n", r, p, tk->name->name);
  606. tkmethod[tk->type]->see(tk, &r, &p);
  607. //print("now r %R, p %P\n", r, p);
  608. }
  609. g = Pt(tk->borderwidth, tk->borderwidth);
  610. if (tk->parent != nil) {
  611. g = addpt(g, tkmethod[tk->parent->type]->relpos(tk));
  612. tk = tk->parent;
  613. } else {
  614. g.x += tk->act.x;
  615. g.y += tk->act.y;
  616. tk = tk->master;
  617. }
  618. r = rectaddpt(r, g);
  619. p = addpt(p, g);
  620. }
  621. }