[executecmd] migrate executecmd to use the gadgets in libami

This removes the gadget specific code in executecmd and calls
the routines I've added in libami.
This commit is contained in:
Adrian Chadd
2022-04-09 17:34:43 -07:00
parent 0e5c81f6e3
commit 48f15595a4

View File

@@ -21,6 +21,8 @@
#endif
#include "drawinfo.h"
#include "gadget_button.h"
#include "gadget_textinput.h"
#ifdef AMIGAOS
#include <pragmas/xlib_pragmas.h>
@@ -30,27 +32,14 @@ extern struct Library *XLibBase;
#define MAX_CMD_CHARS 256
#define VISIBLE_CMD_CHARS 35
#define BOT_SPACE 4
#define TEXT_SIDE 8
#define BUT_SIDE 12
#define TOP_SPACE 4
#define INT_SPACE 7
#define BUT_VSPACE 2
#define BUT_HSPACE 8
static const char ok_txt[]="Ok", cancel_txt[]="Cancel";
static const char enter_txt[]="Enter Command and its Arguments:";
static const char cmd_txt[]="Command:";
static int selected=0, depressed=0, stractive=1;
static Window button[3];
static const char *buttxt[]={ NULL, ok_txt, cancel_txt };
char cmdline[MAX_CMD_CHARS+1];
int buf_len=0;
int cur_pos=0;
int left_pos=0;
int cur_x=6;
struct gadget_button *buttons[3];
struct gadget_textinput *text_input;
char *progname;
@@ -58,368 +47,121 @@ Display *dpy;
struct DrawInfo dri;
Window root, mainwin, strwin;
Window root, mainwin;
GC gc;
int strgadw, strgadh, fh, mainw, mainh, butw;
int strgadw, strgadh;
int fh;
int mainw, mainh, butw;
#ifdef USE_FONTSETS
static XIM xim = (XIM) NULL;
static XIC xic = (XIC) NULL;
#endif
int getchoice(Window w)
/*
* Loop through the button list and see if we find
* a button. Return 0 if we don't find a button.
*
* Yeah, ideally we'd do this in some gadget / intuition
* UI layer thing, but we're not there yet.
*/
static int
getchoice(Window w)
{
int i;
for(i=1; i<3; i++)
if(button[i]==w)
return i;
return 0;
}
void refresh_button(Window w, const char *txt, int idx)
{
int h=fh+2*BUT_VSPACE, l=strlen(txt);
#ifdef USE_FONTSETS
int tw=XmbTextEscapement(dri.dri_FontSet, txt, l);
#else
int tw=XTextWidth(dri.dri_Font, txt, l);
#endif
XSetForeground(dpy, gc, dri.dri_Pens[TEXTPEN]);
#ifdef USE_FONTSETS
XmbDrawString(dpy, w, dri.dri_FontSet, gc, (butw-tw)>>1,
dri.dri_Ascent+BUT_VSPACE, txt, l);
#else
XDrawString(dpy, w, gc, (butw-tw)>>1,
dri.dri_Ascent+BUT_VSPACE, txt, l);
#endif
XSetForeground(dpy, gc, dri.dri_Pens[(selected==idx && depressed)?
SHADOWPEN:SHINEPEN]);
XDrawLine(dpy, w, gc, 0, 0, butw-2, 0);
XDrawLine(dpy, w, gc, 0, 0, 0, h-2);
XSetForeground(dpy, gc, dri.dri_Pens[(selected==idx && depressed)?
SHINEPEN:SHADOWPEN]);
XDrawLine(dpy, w, gc, 1, h-1, butw-1, h-1);
XDrawLine(dpy, w, gc, butw-1, 1, butw-1, h-1);
XSetForeground(dpy, gc, dri.dri_Pens[BACKGROUNDPEN]);
XDrawPoint(dpy, w, gc, butw-1, 0);
XDrawPoint(dpy, w, gc, 0, h-1);
}
void refresh_main(void)
{
int w;
XSetForeground(dpy, gc, dri.dri_Pens[TEXTPEN]);
#ifdef USE_FONTSETS
XmbDrawString(dpy, mainwin, dri.dri_FontSet, gc, TEXT_SIDE,
TOP_SPACE+dri.dri_Ascent, enter_txt, strlen(enter_txt));
#else
XDrawString(dpy, mainwin, gc, TEXT_SIDE, TOP_SPACE+dri.dri_Ascent,
enter_txt, strlen(enter_txt));
#endif
XSetForeground(dpy, gc, dri.dri_Pens[HIGHLIGHTTEXTPEN]);
#ifdef USE_FONTSETS
w=XmbTextEscapement(dri.dri_FontSet, cmd_txt, strlen(cmd_txt));
XmbDrawString(dpy, mainwin, dri.dri_FontSet, gc,
mainw-strgadw-w-TEXT_SIDE-BUT_SIDE,
TOP_SPACE+fh+INT_SPACE+dri.dri_Ascent,
cmd_txt, strlen(cmd_txt));
#else
w=XTextWidth(dri.dri_Font, cmd_txt, strlen(cmd_txt));
XDrawString(dpy, mainwin, gc, mainw-strgadw-w-TEXT_SIDE-BUT_SIDE,
TOP_SPACE+fh+INT_SPACE+dri.dri_Ascent,
cmd_txt, strlen(cmd_txt));
#endif
}
void refresh_str_text(void)
{
int l, mx=6;
XSetForeground(dpy, gc, dri.dri_Pens[TEXTPEN]);
if(buf_len>left_pos) {
#ifdef USE_FONTSETS
int w, c;
for(l=0; l<buf_len-left_pos; ) {
c=mbrlen(cmdline+left_pos+l, buf_len-left_pos-l, NULL);
w=6+XmbTextEscapement(dri.dri_FontSet, cmdline+left_pos, l+c);
if(w>strgadw-6)
break;
mx=w;
l+=c;
}
XmbDrawImageString(dpy, strwin, dri.dri_FontSet, gc, 6, 3+dri.dri_Ascent,
cmdline+left_pos, l);
#else
mx+=XTextWidth(dri.dri_Font, cmdline+left_pos, l=buf_len-left_pos);
while(mx>strgadw-6)
mx-=XTextWidth(dri.dri_Font, cmdline+left_pos+--l, 1);
XDrawImageString(dpy, strwin, gc, 6, 3+dri.dri_Ascent,
cmdline+left_pos, l);
#endif
}
XSetForeground(dpy, gc, dri.dri_Pens[BACKGROUNDPEN]);
XFillRectangle(dpy, strwin, gc, mx, 3, strgadw-mx-6, fh);
if(stractive) {
if(cur_pos<buf_len) {
XSetBackground(dpy, gc, ~0);
#ifdef USE_FONTSETS
l=mbrlen(cmdline+cur_pos, buf_len-cur_pos, NULL);
XmbDrawImageString(dpy, strwin, dri.dri_FontSet, gc, cur_x,
3+dri.dri_Ascent, cmdline+cur_pos, l);
#else
XDrawImageString(dpy, strwin, gc, cur_x, 3+dri.dri_Ascent,
cmdline+cur_pos, 1);
#endif
XSetBackground(dpy, gc, dri.dri_Pens[BACKGROUNDPEN]);
} else {
XSetForeground(dpy, gc, ~0);
#ifdef USE_FONTSETS
XFillRectangle(dpy, strwin, gc, cur_x, 3,
XExtentsOfFontSet(dri.dri_FontSet)->
max_logical_extent.width, fh);
#else
XFillRectangle(dpy, strwin, gc, cur_x, 3,
dri.dri_Font->max_bounds.width, fh);
#endif
}
}
}
void refresh_str(void)
{
refresh_str_text();
XSetForeground(dpy, gc, dri.dri_Pens[SHINEPEN]);
XDrawLine(dpy, strwin, gc, 0, strgadh-1, 0, 0);
XDrawLine(dpy, strwin, gc, 0, 0, strgadw-2, 0);
XDrawLine(dpy, strwin, gc, 3, strgadh-2, strgadw-4, strgadh-2);
XDrawLine(dpy, strwin, gc, strgadw-4, strgadh-2, strgadw-4, 2);
XDrawLine(dpy, strwin, gc, 1, 1, 1, strgadh-2);
XDrawLine(dpy, strwin, gc, strgadw-3, 1, strgadw-3, strgadh-2);
XSetForeground(dpy, gc, dri.dri_Pens[SHADOWPEN]);
XDrawLine(dpy, strwin, gc, 1, strgadh-1, strgadw-1, strgadh-1);
XDrawLine(dpy, strwin, gc, strgadw-1, strgadh-1, strgadw-1, 0);
XDrawLine(dpy, strwin, gc, 3, strgadh-3, 3, 1);
XDrawLine(dpy, strwin, gc, 3, 1, strgadw-4, 1);
XDrawLine(dpy, strwin, gc, 2, 1, 2, strgadh-2);
XDrawLine(dpy, strwin, gc, strgadw-2, 1, strgadw-2, strgadh-2);
}
void strkey(XKeyEvent *e)
{
void endchoice(void);
#ifdef USE_FONTSETS
Status stat;
#else
static XComposeStatus stat;
#endif
KeySym ks;
char buf[256];
int x, i, n;
#ifndef USE_FONTSETS
n=XLookupString(e, buf, sizeof(buf), &ks, &stat);
#else
n=XmbLookupString(xic, e, buf, sizeof(buf), &ks, &stat);
if(stat == XLookupKeySym || stat == XLookupBoth)
#endif
switch(ks) {
case XK_Return:
case XK_Linefeed:
selected=1;
endchoice();
break;
case XK_Left:
if(cur_pos) {
#ifdef USE_FONTSETS
int p=cur_pos;
// int z;
while(p>0) {
--p;
if(((int)mbrlen(cmdline+p, cur_pos-p, NULL))>0) {
cur_pos=p;
break;
int i;
for (i=1; i<3; i++) {
if (buttons[i]->w == w)
return i;
}
}
return 0;
}
static void
refresh_main(void)
{
int w;
XSetForeground(dpy, gc, dri.dri_Pens[TEXTPEN]);
#ifdef USE_FONTSETS
XmbDrawString(dpy, mainwin, dri.dri_FontSet, gc, TEXT_SIDE,
TOP_SPACE+dri.dri_Ascent, enter_txt, strlen(enter_txt));
#else
--cur_pos;
XDrawString(dpy, mainwin, gc, TEXT_SIDE, TOP_SPACE+dri.dri_Ascent,
enter_txt, strlen(enter_txt));
#endif
}
break;
case XK_Right:
if(cur_pos<buf_len) {
XSetForeground(dpy, gc, dri.dri_Pens[HIGHLIGHTTEXTPEN]);
#ifdef USE_FONTSETS
int l=mbrlen(cmdline+cur_pos, buf_len-cur_pos, NULL);
if(l>0)
cur_pos+=l;
w = XmbTextEscapement(dri.dri_FontSet, cmd_txt, strlen(cmd_txt));
XmbDrawString(dpy, mainwin, dri.dri_FontSet, gc,
mainw-strgadw-w-TEXT_SIDE-BUT_SIDE,
TOP_SPACE+fh+INT_SPACE+dri.dri_Ascent,
cmd_txt, strlen(cmd_txt));
#else
cur_pos++;
w = XTextWidth(dri.dri_Font, cmd_txt, strlen(cmd_txt));
XDrawString(dpy, mainwin, gc, mainw-strgadw-w-TEXT_SIDE-BUT_SIDE,
TOP_SPACE+fh+INT_SPACE+dri.dri_Ascent,
cmd_txt, strlen(cmd_txt));
#endif
}
break;
case XK_Begin:
cur_pos=0;
break;
case XK_End:
cur_pos=buf_len;
break;
case XK_Delete:
if(cur_pos<buf_len) {
int l=1;
#ifdef USE_FONTSETS
l=mbrlen(cmdline+cur_pos, buf_len-cur_pos, NULL);
if(l<=0)
break;
#endif
buf_len-=l;
for(x=cur_pos; x<buf_len; x++)
cmdline[x]=cmdline[x+l];
cmdline[x] = 0;
} else XBell(dpy, 100);
break;
case XK_BackSpace:
if(cur_pos>0) {
int l=1;
#ifdef USE_FONTSETS
int p=cur_pos;
while(p>0) {
--p;
if(((int)mbrlen(cmdline+p, cur_pos-p, NULL))>0) {
l=cur_pos-p;
break;
}
static void
toggle(int c)
{
if (c == 0)
return;
gadget_button_toggle(buttons[c]);
}
static void
abortchoice(void)
{
if(depressed) {
depressed=0;
if (selected > 0) {
gadget_button_set_depressed(buttons[selected], 0);
}
toggle(selected);
}
}
#endif
buf_len-=l;
for(x=(cur_pos-=l); x<buf_len; x++)
cmdline[x]=cmdline[x+l];
cmdline[x] = 0;
} else XBell(dpy, 100);
break;
#ifdef USE_FONTSETS
default:
if(stat == XLookupBoth)
stat = XLookupChars;
}
if(stat == XLookupChars) {
#else
default:
#endif
for(i=0; i<n && buf_len<MAX_CMD_CHARS; i++) {
for(x=buf_len; x>cur_pos; --x)
cmdline[x]=cmdline[x-1];
cmdline[cur_pos++]=buf[i];
buf_len++;
}
if(i<n)
XBell(dpy, 100);
}
if(cur_pos<left_pos)
left_pos=cur_pos;
cur_x=6;
#ifdef USE_FONTSETS
if(cur_pos>left_pos)
cur_x+=XmbTextEscapement(dri.dri_FontSet, cmdline+left_pos, cur_pos-left_pos);
if(cur_pos<buf_len) {
int l=mbrlen(cmdline+cur_pos, buf_len-cur_pos, NULL);
x=XmbTextEscapement(dri.dri_FontSet, cmdline+cur_pos, l);
} else
x=XExtentsOfFontSet(dri.dri_FontSet)->max_logical_extent.width;
#else
if(cur_pos>left_pos)
cur_x+=XTextWidth(dri.dri_Font, cmdline+left_pos, cur_pos-left_pos);
if(cur_pos<buf_len)
x=XTextWidth(dri.dri_Font, cmdline+cur_pos, 1);
else
x=dri.dri_Font->max_bounds.width;
#endif
if((x+=cur_x-(strgadw-6))>0) {
cur_x-=x;
while(x>0) {
#ifdef USE_FONTSETS
int l=mbrlen(cmdline+left_pos, buf_len-left_pos, NULL);
x-=XmbTextEscapement(dri.dri_FontSet, cmdline+left_pos, l);
left_pos += l;
#else
x-=XTextWidth(dri.dri_Font, cmdline+left_pos++, 1);
#endif
}
cur_x+=x;
}
refresh_str_text();
selected = 0;
}
void strbutton(XButtonEvent *e)
static void
endchoice(void)
{
int w, l=1;
stractive=1;
cur_pos=left_pos;
cur_x=6;
while(cur_x<e->x && cur_pos<buf_len) {
int c=selected;
abortchoice();
XCloseDisplay(dpy);
if(c==1) {
system(text_input->buf);
}
exit(0);
}
int
main(int argc, char *argv[])
{
XWindowAttributes attr;
static XSizeHints size_hints;
static XTextProperty txtprop1, txtprop2;
int w2, c;
#ifdef USE_FONTSETS
l=mbrlen(cmdline+cur_pos, buf_len-cur_pos, NULL);
if(l<=0)
break;
w=XmbTextEscapement(dri.dri_FontSet, cmdline+cur_pos, l);
#else
w=XTextWidth(dri.dri_Font, cmdline+cur_pos, 1);
char *p;
setlocale(LC_CTYPE, "");
#endif
if(cur_x+w>e->x)
break;
cur_x+=w;
cur_pos+=l;
}
refresh_str();
}
progname=argv[0];
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "%s: cannot connect to X server %s\n",
progname, XDisplayName(NULL));
exit(1);
}
void toggle(int c)
{
XSetWindowBackground(dpy, button[c], dri.dri_Pens[(depressed&&c==selected)?
FILLPEN:BACKGROUNDPEN]);
XClearWindow(dpy, button[c]);
refresh_button(button[c], buttxt[c], c);
}
void abortchoice()
{
if(depressed) {
depressed=0;
toggle(selected);
}
selected=0;
}
void endchoice()
{
int c=selected;
abortchoice();
XCloseDisplay(dpy);
if(c==1)
system(cmdline);
exit(0);
}
int main(int argc, char *argv[])
{
XWindowAttributes attr;
static XSizeHints size_hints;
static XTextProperty txtprop1, txtprop2;
Window ok, cancel;
int w2, c;
#ifdef USE_FONTSETS
char *p;
setlocale(LC_CTYPE, "");
#endif
progname=argv[0];
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "%s: cannot connect to X server %s\n", progname,
XDisplayName(NULL));
exit(1);
}
root = RootWindow(dpy, DefaultScreen(dpy));
XGetWindowAttributes(dpy, root, &attr);
init_dri(&dri, dpy, root, attr.colormap, False);
root = RootWindow(dpy, DefaultScreen(dpy));
XGetWindowAttributes(dpy, root, &attr);
init_dri(&dri, dpy, root, attr.colormap, False);
#ifdef USE_FONTSETS
strgadw=VISIBLE_CMD_CHARS*XExtentsOfFontSet(dri.dri_FontSet)->
@@ -458,39 +200,6 @@ int main(int argc, char *argv[])
mainw=w2;
mainh=3*fh+TOP_SPACE+BOT_SPACE+2*INT_SPACE+2*BUT_VSPACE;
mainwin=XCreateSimpleWindow(dpy, root, 20, 20, mainw, mainh, 1,
dri.dri_Pens[SHADOWPEN],
dri.dri_Pens[BACKGROUNDPEN]);
strwin=XCreateSimpleWindow(dpy, mainwin, mainw-BUT_SIDE-strgadw,
TOP_SPACE+fh+INT_SPACE-3,
strgadw, strgadh, 0,
dri.dri_Pens[SHADOWPEN],
dri.dri_Pens[BACKGROUNDPEN]);
ok=XCreateSimpleWindow(dpy, mainwin, BUT_SIDE,
mainh-BOT_SPACE-2*BUT_VSPACE-fh,
butw, fh+2*BUT_VSPACE, 0,
dri.dri_Pens[SHADOWPEN],
dri.dri_Pens[BACKGROUNDPEN]);
cancel=XCreateSimpleWindow(dpy, mainwin, mainw-butw-BUT_SIDE,
mainh-BOT_SPACE-2*BUT_VSPACE-fh,
butw, fh+2*BUT_VSPACE, 0,
dri.dri_Pens[SHADOWPEN],
dri.dri_Pens[BACKGROUNDPEN]);
button[0]=None;
button[1]=ok;
button[2]=cancel;
XSelectInput(dpy, mainwin, ExposureMask|KeyPressMask|ButtonPressMask);
XSelectInput(dpy, strwin, ExposureMask|ButtonPressMask);
XSelectInput(dpy, ok, ExposureMask|ButtonPressMask|ButtonReleaseMask|
EnterWindowMask|LeaveWindowMask);
XSelectInput(dpy, cancel, ExposureMask|ButtonPressMask|ButtonReleaseMask|
EnterWindowMask|LeaveWindowMask);
gc=XCreateGC(dpy, mainwin, 0, NULL);
XSetBackground(dpy, gc, dri.dri_Pens[BACKGROUNDPEN]);
#ifndef USE_FONTSETS
XSetFont(dpy, gc, dri.dri_Font->fid);
#endif
#ifdef USE_FONTSETS
if ((p = XSetLocaleModifiers("@im=none")) != NULL && *p)
@@ -510,6 +219,40 @@ int main(int argc, char *argv[])
exit(1);
#endif
/* Main window */
mainwin=XCreateSimpleWindow(dpy, root, 20, 20, mainw, mainh, 1,
dri.dri_Pens[SHADOWPEN],
dri.dri_Pens[BACKGROUNDPEN]);
gc=XCreateGC(dpy, mainwin, 0, NULL);
XSetBackground(dpy, gc, dri.dri_Pens[BACKGROUNDPEN]);
#ifndef USE_FONTSETS
XSetFont(dpy, gc, dri.dri_Font->fid);
#endif
/* Text input window */
text_input = gadget_textinput_create(dpy, &dri, gc, mainwin,
mainw-BUT_SIDE-strgadw,
TOP_SPACE+fh+INT_SPACE-3,
strgadw, strgadh, 256);
#ifdef USE_FONTSETS
gadget_textinput_set_xic(text_input, xic);
#endif
/* Create OK button */
buttons[1] = gadget_button_init(dpy, &dri, gc, mainwin,
BUT_SIDE, mainh-BOT_SPACE-2*BUT_VSPACE-fh, /* x, y */
butw, fh+2*BUT_VSPACE); /* width, height */
gadget_button_set_text(buttons[1], ok_txt);
/* Create cancel button */
buttons[2] = gadget_button_init(dpy, &dri, gc, mainwin,
mainw-butw-BUT_SIDE, mainh-BOT_SPACE-2*BUT_VSPACE-fh, /* x, y */
butw, fh+2*BUT_VSPACE); /* width, height */
gadget_button_set_text(buttons[2], cancel_txt);
XSelectInput(dpy, mainwin, ExposureMask|KeyPressMask|ButtonPressMask);
size_hints.flags = PResizeInc;
txtprop1.value=(unsigned char *)"Execute a File";
txtprop2.value=(unsigned char *)"ExecuteCmd";
@@ -532,37 +275,51 @@ int main(int argc, char *argv[])
if(!event.xexpose.count) {
if(event.xexpose.window == mainwin)
refresh_main();
else if(event.xexpose.window == strwin)
refresh_str();
else if(event.xexpose.window == ok)
refresh_button(ok, ok_txt, 1);
else if(event.xexpose.window == cancel)
refresh_button(cancel, cancel_txt, 2);
else if(event.xexpose.window == text_input->w)
gadget_textinput_repaint(text_input);
else if(event.xexpose.window == buttons[1]->w) {
gadget_button_refresh(buttons[1]);
} else if(event.xexpose.window == buttons[2]->w) {
gadget_button_refresh(buttons[2]);
}
}
case LeaveNotify:
if(depressed && event.xcrossing.window==button[selected]) {
depressed=0;
toggle(selected);
if (depressed) {
if ((selected > 0) && event.xcrossing.window == buttons[selected]->w) {
depressed = 0;
gadget_button_set_depressed(buttons[selected], 0);
toggle(selected);
}
}
break;
case EnterNotify:
if((!depressed) && selected && event.xcrossing.window==button[selected]) {
depressed=1;
toggle(selected);
if((!depressed) && selected && event.xcrossing.window == buttons[selected]->w) {
depressed = 1;
gadget_button_set_depressed(buttons[selected], 1);
toggle(selected);
}
break;
case ButtonPress:
if(event.xbutton.button==Button1) {
if(stractive && event.xbutton.window!=strwin) {
if(stractive && event.xbutton.window!= text_input->w) {
stractive=0;
refresh_str();
gadget_textinput_selected(text_input, 0);
gadget_textinput_repaint(text_input);
}
if((c=getchoice(event.xbutton.window))) {
abortchoice();
depressed=1;
toggle(selected=c);
} else if(event.xbutton.window==strwin)
strbutton(&event.xbutton);
depressed = 1;
selected = c;
if (selected > 0) {
gadget_button_set_depressed(buttons[selected], 1);
}
toggle(selected);
} else if(event.xbutton.window== text_input->w) {
gadget_textinput_selected(text_input, 1);
gadget_textinput_buttonevent(text_input, &event.xbutton);
gadget_textinput_repaint(text_input);
}
}
break;
case ButtonRelease:
@@ -574,8 +331,15 @@ int main(int argc, char *argv[])
}
break;
case KeyPress:
if(stractive)
strkey(&event.xkey);
if(stractive) {
gadget_textinput_keyevent(text_input, &event.xkey);
gadget_textinput_repaint(text_input);
if (gadget_textinput_crlf(text_input)) {
selected = 1;
endchoice();
exit(1);
}
}
}
}
}