ref: bac60f84fa0916fdeb2a0165cd1738404e825438
parent: 60929ada1a066c49396d48bf45cf347a50f8e363
author: Ethan Long <[email protected]>
date: Tue Aug 3 07:49:53 EDT 2021
Made some progress on the option drawing program
--- /dev/null
+++ b/src/ime/options
@@ -1,0 +1,4 @@
+Option 1
+Option 2
+Option 3
+Option 4
--- a/src/optshow.c
+++ b/src/optshow.c
@@ -21,7 +21,7 @@
void clockproc(void*);
void optdraw(Image*, Image*, Point, Rune*);
void readinproc(void*);
-void relocate(Rune*);
+void resize(Point);
void usage(void);
enum
@@ -30,7 +30,6 @@
};
Channel *timer;
-int top = 0;
Biobuf bin, bout;
@@ -57,16 +56,18 @@
textco = 0x000000FF;
ARGBEGIN{
- case 't':
- top = 1;
- break;
- case 'b':
- top = 0;
- break;
case 's':
Binit(&bin, 0, OREAD);
Binit(&bout, 1, OWRITE);
break;
+ case 'f':
+ if (open(argv[1], OREAD) == -1){
+ sysfatal("File \"%s\" does not exist", argv[1]);
+ }
+ bin = *Bopen(argv[1], OREAD);
+ //Binit(&bin, 0, OREAD);
+ Binit(&bout, 1, OWRITE);
+ break;
default:
usage();
exits("usage");
@@ -73,7 +74,7 @@
}ARGEND;
if(initdraw(nil, nil, argv0) < 0)
- sysfatal("%s: %r", argv0);
+ sysfatal("%r");
bg = allocimage(display, Rect(0,0,1,1), RGB24, 1, bgco);
text = allocimage(display, Rect(0,0,screen->r.max.x,font->height), RGB24, 0, textco);
if(bg == nil || text == nil)
@@ -85,7 +86,7 @@
buf = L"No input recieved";
optdraw(bg, text, textpos, buf);
- if((mctl = initmouse(nil, screen)) == nil)
+ if((mctl = initmouse(nil, display->image)) == nil)
sysfatal("%s: %r", argv0);
enum{INPUT, MOUSE, CLOCK, NONE};
@@ -108,11 +109,9 @@
while(disp){
switch(alt(alts)){
case CLOCK:
- relocate(buf);
optdraw(bg, text, textpos, buf);
break;
case INPUT:
- relocate(buf);
if(!(recv(alts[0].c, input))){
sysfatal("%s: Unsuccessful retrieval of input\n%r", argv0);
}
@@ -120,7 +119,6 @@
optdraw(bg, text, textpos, buf);
break;
case MOUSE:
- relocate(buf);
if(getwindow(display, Refnone) < 0)
sysfatal("%s: %r", argv0);
textpos.x = screen->r.min.x;
@@ -157,9 +155,45 @@
void
optdraw(Image *bg, Image *text, Point textpos, Rune *opt)
{
+ Point p;
+ int ht, textht, row, col, textwid;
+ ulong textco;
+ Rune items[5][64];
+
+ textco = text->chan;
+
draw(screen, screen->r, bg, nil, ZP);
- runestring(screen, textpos, text, ZP, font, opt);
+ freeimage(text);
+
+ ht = 0;
+ col = 0;
+ for(int i=col; opt[i] != 0; i++){
+ if (opt[i] == '\n'){
+ //items[ht][col] = 0;
+ items[ht+1][0] = 0;
+ ht ++;
+ col = 0;
+ }else{
+ items[ht][col] = opt[i];
+ items[ht][col+1] = 0;
+ col++;
+ }
+ }
+ ht ++;
+ textht = ht * font->height;
+ text = allocimage(display, Rect(0,0,screen->r.max.x,textht), RGB24, 0, textco);
+
+ row = 0;
+ textwid = 0;
+ while(ht - row > 0){
+ p = runestring(screen, textpos, text, ZP, font, items[row]);
+ if (p.x > textwid)
+ textwid = p.x;
+ row ++;
+ textpos.y = textpos.y + font->height + 2;
+ }
flushimage(display, Refnone);
+ resize(Pt(textwid + 20, screen->r.min.y + textht));
}
void
@@ -184,29 +218,23 @@
}
void
-relocate(Rune *buf)
+resize(Point p)
{
Point min, max;
- int wp, wid, ht, strlen;
+ int wp;
- wid = 0;
+ if ((p.x < screen->r.min.x) || (p.y < screen->r.min.y))
+ sysfatal("%s: resize given invalid dimensions\n%r", argv0);
- for(strlen=0; buf[strlen] != 0; strlen++)
- if((wid = strlen * (font->width + 4)) < 100)
- wid = 100;
-
- ht = font->height * 2;
-
if((wp = open("/dev/wctl", ORDWR)) < 0)
sysfatal("%s: Couldn't open wctl\n %r", argv0);
- if(top){
- min = Pt(0,0);
- max = Pt(wid, ht);
- }else{
- min = Pt(0, display->image->r.max.y - ht);
- max = Pt(wid, display->image->r.max.y);
- }
+ min = screen->r.min;
+ min = Pt(min.x - 4, min.y - 4);
+ max = p;
+
+ if((max.x - min.x) < 100)
+ max.x = min.x + 100;
fprint(wp, "resize -r %d %d %d %d", min.x, min.y, max.x, max.y);
close(wp);