ref: 2add3945e0628832881898b6a3d41d2470b6ff24
parent: 3ebee4724b965ab973a5ba9632b02deacb07c55d
author: kvik <kvik@a-b.xyz>
date: Thu Feb 20 18:36:32 EST 2020
Rework window management
--- a/samterm/main.c
+++ b/samterm/main.c
@@ -235,37 +235,65 @@
getmouse();
}
- int
-getr(Rectangle *rp, int full)
- {
- Point p;
- Rectangle r;
-
- if(full){
- *rp = screen->r;
- r = cmd.l[cmd.front].entire;
- rp->min.y = r.max.y;
- }else{
- *rp = getrect(3, mousectl);
- if(Dx(*rp) <= 5 && Dy(*rp) <= 5){
- p = rp->min;
- r = cmd.l[cmd.front].entire;
- *rp = screen->r;
- if(cmd.nwin==1){
- if (p.y <= r.min.y)
- rp->max.y = r.min.y;
- else if (p.y >= r.max.y)
- rp->min.y = r.max.y;
- if (p.x <= r.min.x)
- rp->max.x = r.min.x;
- else if (p.x >= r.max.x)
- rp->min.x = r.max.x;
- }
- }
- }
- return rectclip(rp, screen->r) &&
- rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
- }
+Rectangle
+inflatepoint(Point p)
+{
+ Rectangle *c;
+ Rectangle r;
+
+ r = screen->r;
+ c = &cmd.l[cmd.front].entire;
+ if(ptinrect(p, *c))
+ return r;
+ // L
+ if(p.x < c->min.x)
+ r.max.x = c->min.x;
+ // R
+ else if(p.x >= c->max.x)
+ r.min.x = c->max.x;
+ // M
+ else{
+ r.min.x = c->min.x;
+ r.max.x = c->max.x;
+ // A
+ if(p.y <= c->min.y)
+ r.max.y = c->min.y;
+ // B
+ else
+ r.min.y = c->max.y;
+ }
+ return r;
+}
+
+Rectangle
+defaultrect(void)
+{
+ Rectangle *c;
+ Rectangle L, M, R;
+
+ c = &cmd.l[cmd.front].entire;
+ L = inflatepoint(Pt(c->min.x - 1, c->min.y));
+ M = inflatepoint(Pt(c->min.x, c->max.y));
+ R = inflatepoint(Pt(c->max.x + 1, c->min.y));
+ if(Dx(L) >= Dx(M) && Dx(L) >= Dx(R))
+ return L;
+ else if(Dx(M) > Dx(L) && Dx(M) > Dx(R))
+ return M;
+ return R;
+}
+
+int
+promptrect(Rectangle *r)
+{
+ *r = getrect(3, mousectl);
+ if(eqrect(*r, Rect(0,0,0,0)))
+ return 0;
+ if(Dx(*r) < 5 && Dy(*r) < 5)
+ *r = inflatepoint(r->min);
+ if(rectclip(r, screen->r) == 0)
+ *r = defaultrect();
+ return 1;
+}
void
snarf(Text *t, int w)
--- a/samterm/menu.c
+++ b/samterm/menu.c
@@ -119,6 +119,7 @@
int m, i;
Text *t;
+ l = flwhich(mousep->xy);
mw = -1;
switch(m = menuhit(3, mousectl, &menu3, nil)){
case -1:
@@ -125,32 +126,22 @@
break;
case New:
- if(!hostlock)
- sweeptext(1, 0);
+ if(hostlock)
+ break;
+ sweeptext(1, 0);
break;
case Zerox:
- if(!hostlock)
- duplicate(which, which->entire, which->f.font, 0);
- break;
-
case Resize:
- if(!hostlock){
- setcursor(mousectl, &bullseye);
- buttons(Down);
- if((mousep->buttons&4) && (l = flwhich(mousep->xy)) && getr(&r, 0))
- duplicate(l, r, l->f.font, m==Resize);
- else
- setcursor(mousectl, cursor);
- buttons(Up);
- }
+ if(hostlock || l == nil)
+ break;
+ if(promptrect(&r))
+ duplicate(l, r, l->f.font, m == Resize);
break;
case Close:
- if(hostlock)
+ if(hostlock || l == nil)
break;
- if((l = flwhich(mousep->xy)) == nil)
- break;
t=(Text *)l->user1;
if (t->nwin>1)
closeup(l);
@@ -161,10 +152,8 @@
break;
case Write:
- if(hostlock)
+ if(hostlock || l == nil)
break;
- if((l = flwhich(mousep->xy)) == nil)
- break;
outTs(Twrite, ((Text *)l->user1)->tag);
setlock();
break;
@@ -186,7 +175,6 @@
}
}
-
Text *
sweeptext(int new, int tag)
{
@@ -193,23 +181,25 @@
Rectangle r;
Text *t;
- if(getr(&r, 1) && (t = malloc(sizeof(Text)))){
- memset((void*)t, 0, sizeof(Text));
- current((Flayer *)0);
- flnew(&t->l[0], gettext, 0, (char *)t);
- flinit(&t->l[0], r, font, maincols); /*bnl*/
- t->nwin = 1;
+ if((t = mallocz(sizeof(*t), 1)) == nil)
+ return nil;
+ if(new)
+ r = inflatepoint(mousep->xy);
+ else
+ r = defaultrect();
+ current((Flayer *)0);
+ flnew(&t->l[0], gettext, 0, (char *)t);
+ flinit(&t->l[0], r, font, maincols); /*bnl*/
+ t->nwin = 1;
+ rinit(&t->rasp);
+ if(new)
+ startnewfile(Tstartnewfile, t);
+ else{
rinit(&t->rasp);
- if(new)
- startnewfile(Tstartnewfile, t);
- else{
- rinit(&t->rasp);
- t->tag = tag;
- startfile(t);
- }
- return t;
+ t->tag = tag;
+ startfile(t);
}
- return 0;
+ return t;
}
int
--- a/samterm/samterm.h
+++ b/samterm/samterm.h
@@ -119,7 +119,6 @@
int center(Flayer*, long);
int xmenuhit(int, Menu*);
void buttons(int);
-int getr(Rectangle*, int);
void current(Flayer*);
void duplicate(Flayer*, Rectangle, Font*, int);
void startfile(Text*);
@@ -163,6 +162,9 @@
void dumperrmsg(int, int, int, int);
int screensize(int*,int*);
void getmouse(void);
+Rectangle inflatepoint(Point);
+Rectangle defaultrect(void);
+int promptrect(Rectangle*);
#include "mesg.h"