home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
x
/
volume10
/
xprompt2
/
part01
/
xprompt.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-29
|
10KB
|
345 lines
/*
xprompt.c
Better version of xprompt.
by Mike & Bob
Last edited: Wed Jun 27 17:40:54 1990 by mjm (Michael Murphy) on lightning
Copyright (C) 1990 Michael Murphy and Robert Forsman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
char ego[] = "Copyright (C) 1990 Michael Murphy and Robert Forsman\n";
#include "patchlevel.h"
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Label.h>
Display *dpy;
int scrn;
Widget master, form;
XtAppContext app_con;
Pixel origBorderColor;
struct promptentry {
Widget box, label, input;
char *prompt, *reply;
struct promptentry *next, *prev;
};
struct promptentry *pehead = NULL, *petail = NULL, *active;
struct XPromptResources {
int rlen;
Boolean grab;
Pixel hl;
} xpres;
#define offset(field) XtOffset (struct XPromptResources*, field)
static XrmOptionDescRec Options [] = {
{"-rlen", "replyLength", XrmoptionSepArg, NULL},
{"-ibw", "*input.borderWidth", XrmoptionSepArg, NULL},
{"-grab", "grabKeyboard", XrmoptionNoArg, "true"},
{"-nograb", "grabKeyboard", XrmoptionNoArg, "false"},
{"-pfn", "*label.font", XrmoptionSepArg, NULL},
{"-rfn", "*input*font", XrmoptionSepArg, NULL},
{"-hl", "focusHighlightColor", XrmoptionSepArg, NULL},
{"-p", "ignoreme", XrmoptionSkipArg, NULL},
{"-r", "ignoremetoo", XrmoptionSkipArg, NULL},
};
static XtResource Resources [] = {
{"replyLength", "ReplyLength", XtRInt, sizeof(int), offset (rlen),
XtRImmediate, (XtPointer)80},
{"grabKeyboard", "GrabKeyboard", XtRBoolean, sizeof(Boolean), offset (grab),
XtRImmediate, (XtPointer)True},
{"focusHighlightColor", XtCBorderColor, XtRPixel, sizeof(Pixel), offset (hl),
XtRString, XtDefaultBackground},
};
void BailApplication ();
void ExitApplicationAndPrint ();
void GrabKeyboardAfterVisible ();
void AdvanceField ();
static XtActionsRec Actions [] = {
{"bail-application", BailApplication},
{"exit-application-and-print", ExitApplicationAndPrint},
{"grab-keyboard-after-visible", GrabKeyboardAfterVisible},
{"advance-field", AdvanceField},
};
static char *FallbackResources [] = {
"XPrompt*AllowShellResize: true",
"XPrompt*Text.resize: both",
"XPrompt*Text.resizable: true",
"XPrompt*Form.borderWidth: 0",
"XPrompt.xprompt.translations: #override\
<Visible>: grab-keyboard-after-visible()\n",
"XPrompt*input.translations: #override\
<Key>Return: advance-field(1,0)\\n\\\n\
Ctrl<Key>m: advance-field(1,0)\\n\\\n\
Ctrl<Key>n: advance-field(1,1)\\n\\\n\
!<Key>Tab: advance-field(1,1)\\n\\\n\
Ctrl<Key>p: advance-field(-1,1)\\n\\\n\
!Shift<Key>Tab: advance-field(-1,1)\\n\\\n\
Ctrl<Key>c: bail-application()\n",
NULL
};
void BailApplication (w, event, argv, argc)
Widget w;
XEvent *event;
String *argv;
Cardinal *argc;
{
XtDestroyApplicationContext (app_con);
exit(0);
}
void ExitApplicationAndPrint (w, event, argv, argc)
Widget w;
XEvent *event;
String *argv;
Cardinal *argc;
{
char *reply;
for (active=pehead; active != NULL; active=active->next) {
XtVaGetValues (active->input, XtNstring, &reply, 0);
printf ("%s\n", reply);
}
exit (0);
}
void GrabKeyboardAfterVisible (w, event, argv, argc)
/* Grab the stupid keyboard focus after window is made visible. */
Widget w;
XEvent *event;
String *argv;
Cardinal *argc;
{
if (event->type == VisibilityNotify && XtIsRealized (master)) {
if (event->xvisibility.state != VisibilityFullyObscured) {
XtSetKeyboardFocus (master, active->input);
if (xpres.grab) {
XtGrabKeyboard (active->input, True,
GrabModeAsync, GrabModeAsync, CurrentTime);
}
XtVaGetValues (active->input, XtNborderColor, &origBorderColor, 0);
XtVaSetValues (active->input, XtNdisplayCaret, (XtPointer)True,
XtNborderColor, xpres.hl,
0);
}
}
}
void AdvanceField (w, event, argv, argc)
Widget w;
XEvent *event;
String *argv;
Cardinal *argc;
{
int deltafield, i;
Boolean wrap;
if (*argc > 0) {
deltafield = atoi (argv[0]);
if (*argc > 1) {
wrap = atoi (argv[1]) != 0;
} else {
wrap = True;
}
} else {
deltafield = 1;
wrap = True;
}
XtVaSetValues (active->input, XtNdisplayCaret, (XtPointer)False,
XtNborderColor, origBorderColor, 0);
if (deltafield < 0) {
for (i=deltafield; i<0; i++) {
active = active->prev;
if (active == NULL) {
if (wrap) {
active = petail;
} else {
break;
}
}
}
} else {
for (i=deltafield; i>0; i--) {
active = active->next;
if (active == NULL) {
if (wrap) {
active = pehead;
} else {
break;
}
}
}
}
if (active == NULL)
ExitApplicationAndPrint (w, event, argv, argc);
event->type = VisibilityNotify;
event->xvisibility.state = VisibilityUnobscured;
GrabKeyboardAfterVisible (w, event, NULL, NULL);
}
void AddPromptToWidget (pe)
struct promptentry* pe;
{
XFontStruct *font;
Dimension width, height;
Position bmargin, tmargin, lmargin, rmargin;
pe->box = XtVaCreateManagedWidget
("form", formWidgetClass, form,
XtNresizable, (XtPointer)True,
0);
if (pe->prev != NULL) {
XtVaSetValues (pe->box, XtNfromVert, pe->prev->box, 0);
}
pe->label = XtVaCreateManagedWidget
("label", labelWidgetClass, pe->box,
XtNbottom, XtChainTop,
XtNtop, XtChainTop,
XtNright, XtChainLeft,
XtNleft, XtChainLeft,
XtNlabel, pe->prompt,
0);
pe->input = XtVaCreateManagedWidget
("input", asciiTextWidgetClass, pe->box,
XtNbottom, XtChainBottom,
XtNtop, XtChainTop,
XtNright, XtChainRight,
XtNleft, XtChainLeft,
XtNfromHoriz, pe->label,
XtNstring, pe->reply,
XtNeditType, XawtextEdit,
XtNresizable, (XtPointer)True,
XtNdisplayCaret, (XtPointer)False,
XtNinsertPosition, (pe->reply == NULL)? 0 : strlen (pe->reply),
0);
XtVaGetValues (pe->input, XtNfont, &font,
XtNbottomMargin, &bmargin, XtNtopMargin, &tmargin,
XtNleftMargin, &lmargin, XtNrightMargin, &rmargin, 0);
width = (font->max_bounds.rbearing - font->min_bounds.lbearing) * xpres.rlen
+ lmargin + rmargin;
height = font->max_bounds.ascent + font->max_bounds.descent + tmargin + bmargin;
XtVaSetValues (pe->input, XtNwidth, width, XtNheight, height, 0);
}
void usage (progname)
char *progname;
{
fprintf (stderr,"Usage: %s [flags] {-p prompt [-r reply]} ...\n\n",
progname);
fprintf (stderr, " where flags is one or more of: \n");
fprintf (stderr, " -rlen <length> (Maximum length of user's reply: default 80)\n");
fprintf (stderr, " -ibw <bwidth> (Border width of inside window: default 1)\n");
fprintf (stderr, " -grab (Grab keyboard)\n");
fprintf (stderr, " -nograb (Don't grab keyboard)\n");
fprintf (stderr, " -pfn <font> (Prompt font)\n");
fprintf (stderr, " -rfn <font> (Reply font)\n");
fprintf (stderr, " -hl <color> (Input highlight color)\n");
fprintf (stderr, "\n The default prompt is \"?\"\n");
exit (1);
}
main (argc, argv)
int argc;
char **argv;
{
int i, p;
struct promptentry *temp;
Widget orig;
String origGeom;
orig = XtVaAppInitialize (&app_con, "XPrompt", Options, XtNumber(Options),
&argc, argv, FallbackResources, 0);
XtGetApplicationResources (orig, &xpres, Resources, XtNumber(Resources),
NULL, 0);
XtAppAddActions (app_con, Actions, XtNumber(Actions));
master = XtVaCreatePopupShell ("xprompt", applicationShellWidgetClass, orig, 0);
XtVaGetValues (orig, XtNgeometry, &origGeom, 0);
XtVaSetValues (master, XtNgeometry, origGeom, 0);
form = XtVaCreateManagedWidget ("form", formWidgetClass, master, 0);
if (argc <= 1) {
/* no args given, use default prompt */
static struct promptentry defaultprompt = {
0, 0, 0, "?", NULL, NULL, NULL };
pehead = petail = &defaultprompt;
AddPromptToWidget (&defaultprompt);
}
for (i=1; i<argc; i+=2) {
if (strcmp ("-p", argv[i]) != 0 || i+1 >= argc) {
usage (argv[0]);
}
temp = (void*) malloc (sizeof (struct promptentry));
temp->prompt = argv[i+1];
if (i+3 < argc && strcmp ("-r", argv[i+2]) == 0) {
temp->reply = argv[i+3];
i += 2;
} else {
temp->reply = NULL;
}
temp->prev = petail;
temp->next = NULL;
if (temp->prev != NULL) {
temp->prev->next = temp;
} else {
pehead = temp;
}
petail = temp;
AddPromptToWidget (temp);
}
active = pehead;
XtRealizeWidget (master);
XtPopup (master, XtGrabExclusive);
XtAppMainLoop (app_con);
}