games-request@tekred.TEK.COM (07/02/87)
Submitted by: keithp@copper.TEK.COM (Keith Packard) Comp.sources.games: Volume 1, Issue 72 Archive-name: xmille/Part04 [I slipped up and somehow missed the contents of the control sub-directory, so here is Part 4 of 3 :-) of xmille. On another note, I errored r.e. the Expires: line (that's what I get for not reading the man page myself) - it has been removed. Sorry for any inconvenience. -br] #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of archive 4 (of 4)." # Contents: control control/Makefile control/affirm.c control/button.c # control/co_class.h control/control.h control/dispatch.c # control/init.c control/prompted.c control/scrollbar.c # control/test.c # Wrapped by billr@tekred on Thu Jul 2 10:16:22 1987 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test ! -d control ; then echo shar: Creating directory \"control\" mkdir control fi if test -f control/Makefile -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/Makefile\" else echo shar: Extracting \"control/Makefile\" \(324 characters\) sed "s/^X//" >control/Makefile <<'END_OF_control/Makefile' XHDRS = control.h co_class.h X XOFILES = button.o scrollbar.o init.o affirm.o dispatch.o prompted.o X XSRCCOMMON = button.c scrollbar.c init.c affirm.c dispatch.c prompted.c X Xcontrol.a : $(OFILES) X ar cr $@ $? X ranlib $@ X Xtest: test.o $(OFILES) X $(CC) -o $@ test.o $(OFILES) -lX X Xcontrol.o : $(OFILES) X $(LD) -r -o $@ $(OFILES) X END_OF_control/Makefile if test 324 -ne `wc -c <control/Makefile`; then echo shar: \"control/Makefile\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/affirm.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/affirm.c\" else echo shar: Extracting \"control/affirm.c\" \(2535 characters\) sed "s/^X//" >control/affirm.c <<'END_OF_control/affirm.c' X/* X * affirm.c X * X * affirm an action (yes/no) X */ X X# include <X/Xlib.h> X# include "control.h" X# include "co_class.h" X# include <sys/time.h> X# include <assert.h> X X/* X * affirm window on screen X */ X X# define AFFIRM_Y 350 X# define AFFIRM_X 200 X# define AFFIRM_HP 50 /* padding */ X# define MIN_AFFIRM_W (CANCEL_X + 80) X# define LABEL_Y 25 X X# define OK_X (10) X# define OK_Y (AFFIRM_HP) X# define CANCEL_X (OK_X + 100) X# define CANCEL_Y (OK_Y) X X# define TEXT_OFF 5 X Xextern int co_inited; Xstatic char *label; Xstatic int affirm_h, affirm_w, label_x; Xstatic Window affirm, ok, cancel; X Xstatic int OKstate, CANCELstate; X Xint Xco_affirm(title) Xchar *title; X{ X XEvent rep; X int state, affirm_x; X int label_w; X int co_OKstate (), co_CANCELstate (), co_affirmEvent (), CmanageButton (); X int mask; X X if (!co_inited) X co_init(); X label = title; X label_w = XQueryWidth(label, co_font->id); X X if (MIN_AFFIRM_W+10 >= label_w) X affirm_w = MIN_AFFIRM_W+10; X else X affirm_w = label_w + 10; X X OKstate = 0; X CANCELstate = 0; X label_x = affirm_w / 2 - label_w / 2; X affirm_h = CANCEL_Y + 60; X affirm_x = AFFIRM_X - affirm_w/2; X affirm = XCreateWindow (RootWindow, affirm_x, AFFIRM_Y, X affirm_w, affirm_h, 1, co_border, co_background); X ok = CmapButton (affirm, OK_X, OK_Y, co_OK, co_OKstate); X cancel = CmapButton (affirm, CANCEL_X, CANCEL_Y, co_CANCEL, co_CANCELstate); X bindEvent (affirm, ExposeWindow, co_affirmEvent); X bindEvent (ok, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow, X CmanageButton); X bindEvent (cancel, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow, X CmanageButton); X XMapWindow (affirm); X X while (!OKstate && !CANCELstate) { X dispatch (); X } X X unbindEvent (affirm, ExposeWindow); X unbindEvent (ok, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow); X unbindEvent (cancel, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow); X XUnmapWindow (affirm); X CunmapButton (cancel); X CunmapButton (ok); X XDestroyWindow (affirm); X if (CANCELstate) X return 0; X if (OKstate) X return 1; X} X Xco_affirmEvent (rep) XXEvent *rep; X{ X switch (rep->type) { X case ExposeWindow: X redisplayLabel (); X } X} X Xco_OKstate (n) X{ X OKstate = 1; X} X Xco_CANCELstate (n) X{ X CANCELstate = 1; X} X Xstatic XredisplayLabel () X{ X XText (affirm, label_x, LABEL_Y, label, strlen (label), co_font->id, X co_fore, co_back); X XFlush (); X} END_OF_control/affirm.c if test 2535 -ne `wc -c <control/affirm.c`; then echo shar: \"control/affirm.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/button.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/button.c\" else echo shar: Extracting \"control/button.c\" \(4619 characters\) sed "s/^X//" >control/button.c <<'END_OF_control/button.c' X/* X * button.c X */ X X# include <X/Xlib.h> X# include "control.h" X Xstruct button { X Button button; X int width, height; X int x, y; X int font; X int forecolor, backcolor; X Pixmap background; X int borderwidth; X int vcount; X Vertex *vlist; X char *text; X}; X Xstruct perwindow { X struct button *b; X Window wd; X int (*notify)(); X int state; X}; X Xstatic XAssocTable *assocTable; Xstatic XAssocTable *perwindowTable; Xextern XAssocTable *XCreateAssocTable(); Xextern char *XLookUpAssoc(); Xstatic int nextButton; X XButton XCcreateButton (font, text, width, forecolor, backcolor, borderwidth) XFontInfo *font; Xchar *text; Xint forecolor, backcolor; X{ X char *malloc (); X struct button *b; X int round, off; X int hround; X int twidth; X X if (!assocTable) X assocTable = XCreateAssocTable (64); X b = (struct button *) malloc (sizeof (struct button)); X b->button = nextButton; X XMakeAssoc (assocTable, nextButton++, (char *) b); X twidth = XQueryWidth (text, font->id); X if (width <= 0) X b->width = twidth + font->width * 2; X else X b->width = width; X b->height = font->height * 2; X b->x = b->width / 2 - twidth / 2; X b->y = font->height/2; X b->font = font->id; X b->text = text; X b->forecolor = forecolor; X b->backcolor = backcolor; X b->background = XMakeTile (backcolor); X b->borderwidth = borderwidth; X b->vcount = 9; X b->vlist = (Vertex *) malloc (sizeof (Vertex) * 9); X X round = b->height / 8; X off = borderwidth; X X b->vlist[0].x = round; X b->vlist[0].y = 0; X b->vlist[0].flags = 0; X X b->vlist[1].x = (b->width - off) - round; X b->vlist[1].y = 0; X b->vlist[1].flags = 0; X X b->vlist[2].x = (b->width - off); X b->vlist[2].y = round; X b->vlist[2].flags = 0; X X b->vlist[3].x = (b->width - off); X b->vlist[3].y = (b->height - off) - round; X b->vlist[3].flags = 0; X X b->vlist[4].x = (b->width - off) - round; X b->vlist[4].y = (b->height - off); X b->vlist[4].flags = 0; X X b->vlist[5].x = round; X b->vlist[5].y = (b->height - off); X b->vlist[5].flags = 0; X X b->vlist[6].x = 0; X b->vlist[6].y = (b->height - off) - round; X b->vlist[6].flags = 0; X X b->vlist[7].x = 0; X b->vlist[7].y = round; X b->vlist[7].flags = 0; X X b->vlist[8].x = round; X b->vlist[8].y = 0; X b->vlist[8].flags = 0; X X return b->button; X} X XWindow XCmapButton (parent, x, y, button, notify) XWindow parent; Xint x, y; XButton button; Xint (*notify)(); X{ X struct button *b; X struct perwindow *p; X Window w; X X b = (struct button *) XLookUpAssoc (assocTable, button); X if (!b) X return 0; X w = XCreateWindow (parent, x, y, b->width, b->height, X 0, 0, b->background); X if (!w) X return 0; X XMapWindow (w); X if (!perwindowTable) X perwindowTable = XCreateAssocTable (64); X p = (struct perwindow *) malloc (sizeof (struct perwindow)); X XMakeAssoc (perwindowTable, w, (char *) p); X p->b = b; X p->wd = w; X p->notify = notify; X p->state = 0; X buttonOff (w, b, p); X return w; X} X XCunmapButton (w) XWindow w; X{ X struct button *b; X struct perwindow *p; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X XDeleteAssoc (perwindowTable, w); X free (p); X XDestroyWindow (w); X return 1; X} X XCmanageButton (rep) XXEvent *rep; X{ X struct perwindow *p; X struct button *b; X XMouseMovedEvent *me; X Window w; X X w = rep->window; X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X b = p->b; X switch (rep->type) { X case ExposeWindow: X if (p->state) X buttonOn (w, b, p); X else X buttonOff (w, b, p); X break; X case ButtonPressed: X buttonOn (w, b, p); X p->state = 1; X break; X case ButtonReleased: X if (p->state) X p->notify (w); X p->state = 0; X break; X case MouseMoved: X me = (XMouseMovedEvent *) rep; X if (me->x < 0 || me->x > b->width || X me->y < 0 || me->y > b->height) X if (p->state) { X buttonOff (w, b, p); X p->state = 0; X } X break; X case LeaveWindow: X if (p->state) { X buttonOff (w, b, p); X p->state = 0; X } X break; X } X} X XCredrawButton (w) XWindow w; X{ X struct perwindow *p; X struct button *b; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X b = p->b; X buttonOff (w, b, p); X} X Xstatic XbuttonOn (w, b) XWindow w; Xstruct button *b; X{ X XDrawFilled (w, b->vlist, b->vcount, b->forecolor, GXcopy, AllPlanes); X buttonText (w, b, b->backcolor, b->forecolor); X} X Xstatic XbuttonOff (w, b) XWindow w; Xstruct button *b; X{ X XPixSet (w, 0, 0, b->width, b->height, b->backcolor); X XDraw (w, b->vlist, b->vcount, b->borderwidth, b->borderwidth, X b->forecolor, GXcopy, AllPlanes); X buttonText (w, b, b->forecolor, b->backcolor); X} X Xstatic XbuttonText (w, b, fore, back) XWindow w; Xstruct button *b; Xint fore, back; X{ X XText (w, b->x, b->y, b->text, strlen (b->text), b->font, fore, back); X} END_OF_control/button.c if test 4619 -ne `wc -c <control/button.c`; then echo shar: \"control/button.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/co_class.h -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/co_class.h\" else echo shar: Extracting \"control/co_class.h\" \(282 characters\) sed "s/^X//" >control/co_class.h <<'END_OF_control/co_class.h' X/* X * object classes used by co routines - private to co! X */ X Xextern Button co_OK, co_CANCEL, co_NEW; Xextern Scrollbar co_SCROLL_LEFT_GUI, co_SCROLL_BOTTOM_GUI, co_SCROLL_LEFT_MENU; Xextern FontInfo *co_font; Xextern int co_fore, co_back; Xextern Pixmap co_background, co_border; END_OF_control/co_class.h if test 282 -ne `wc -c <control/co_class.h`; then echo shar: \"control/co_class.h\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/control.h -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/control.h\" else echo shar: Extracting \"control/control.h\" \(849 characters\) sed "s/^X//" >control/control.h <<'END_OF_control/control.h' X/* X * control.h X * X * defines for control manager X */ X X# define CONTROL_BUTTON 0 /* named button */ X# define CONTROL_SCROLL 1 /* scroll bar */ X# define CONTROL_RADIO 2 /* radio buttons */ X# define CONTROL_CHECK 3 /* check boxes */ X# define CONTROL_SIZE 4 /* resize box */ X# define CONTROL_EDIT 5 /* text edit box */ X Xtypedef int Button; X Xtypedef int Check; X Xtypedef int Editbox; X X# define EDIT_VOID 0 X# define EDIT_RETURN 1 X Xtypedef int Scrollbar; X X# define SCROLL_LEFT 0 X# define SCROLL_TOP 1 X# define SCROLL_RIGHT 2 X# define SCROLL_BOTTOM 3 X X# define SCROLLWIDTH 25 X X# define SCROLL_UP_BUTTON -1 X# define SCROLL_DOWN_BUTTON -2 X# define SCROLL_UP_AREA -3 X# define SCROLL_DOWN_AREA -4 X# define SCROLL_BUTTON_RELEASE -5 X X# define SCROLL_EVENTS (ButtonPressed|ButtonReleased|ExposeWindow| \ X RightDownMotion|LeftDownMotion|MiddleDownMotion) END_OF_control/control.h if test 849 -ne `wc -c <control/control.h`; then echo shar: \"control/control.h\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/dispatch.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/dispatch.c\" else echo shar: Extracting \"control/dispatch.c\" \(1615 characters\) sed "s/^X//" >control/dispatch.c <<'END_OF_control/dispatch.c' X/* X * dispatch.c X * X * pass out X events to appropriate recipiants X */ X X# include <X/Xlib.h> X Xstruct eventGroup { X struct eventGroup *prev; X Window w; X unsigned long e; X int (*f)(); X}; X Xstruct eventGroup *eventStack, *allocGroup(); X XbindEvent (window, eventMask, func) XWindow window; Xunsigned long eventMask; Xint (*func)(); X{ X struct eventGroup *g; X unsigned long allEvents; X X g = allocGroup (); X g->w = window; X g->e = eventMask; X g->f = func; X g->prev = eventStack; X eventStack = g; X allEvents = 0; X for (g = eventStack; g; g = g->prev) X if (g->w == window) X allEvents |= g->e; X XSelectInput (window, allEvents); X} X XunbindEvent (window, eventMask) XWindow window; Xunsigned long eventMask; X{ X struct eventGroup *g, *n, *p; X unsigned long t; X unsigned long remainingEvents; X X n = 0; X remainingEvents = 0; X for (g = eventStack; g; g = p) { X p = g->prev; X if (g->w == window) { X if (g->e & eventMask) { X t = eventMask; X eventMask &= ~g->e; X g->e &= ~t; X } X remainingEvents |= g->e; X if (g->e == 0) { X if (n) X n->prev = p; X else X eventStack = p; X freeGroup (g); X } X } X } X XSelectInput (window, remainingEvents); X} X XsendEvent (rep) XXEvent *rep; X{ X struct eventGroup *g; X X for (g = eventStack; g; g = g->prev) { X if (rep->window == g->w && (rep->type & g->e)) { X g->f (rep); X return; X } X } X} X Xdispatch () X{ X XEvent event; X X XNextEvent (&event); X sendEvent (&event); X} X Xstatic struct eventGroup * XallocGroup () X{ X char *malloc (); X X return (struct eventGroup *) malloc (sizeof (struct eventGroup)); X} X XfreeGroup (g) Xstruct eventGroup *g; X{ X free ((char *) g); X} END_OF_control/dispatch.c if test 1615 -ne `wc -c <control/dispatch.c`; then echo shar: \"control/dispatch.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/init.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/init.c\" else echo shar: Extracting \"control/init.c\" \(1076 characters\) sed "s/^X//" >control/init.c <<'END_OF_control/init.c' X/* X * init.c X */ X X# include <X/Xlib.h> X# include "control.h" X# include <assert.h> X# include "co_class.h" X# include <stdio.h> X XButton co_OK, co_CANCEL, co_NEW; XButton co_QUIT, co_SAVE; XScrollbar co_SCROLL_LEFT_MENU, co_SCROLL_BOTTOM_MENU; XFontInfo *co_font; Xint co_fore, co_back; XPixmap co_background, co_border; Xint co_inited; X Xco_init () X{ X Color hard_def, exact_def; X/* ui_getcolors (&co_fore, &co_back, &co_fore, &co_back); */ X XGetColor ("black", &hard_def, &exact_def); X co_fore = hard_def.pixel; X XGetColor ("white", &hard_def, &exact_def); X co_back = hard_def.pixel; X X co_background = XMakeTile (co_back); X co_border = XMakeTile (co_fore); X co_font = XOpenFont ("timrom12b"); X assert (co_font); X X co_OK = CcreateButton (co_font, "OK", 75, co_fore, co_back, 1); X co_CANCEL = CcreateButton (co_font, "NO", 75, co_fore, co_back, 1); X co_NEW = CcreateButton (co_font, "NEW", 75, co_fore, co_back, 1); X co_QUIT = CcreateButton (co_font, "QUIT", 75, co_fore, co_back, 1); X co_SAVE = CcreateButton (co_font, "SAVE", 75, co_fore, co_back, 1); X co_inited = 1; X return 0; X} END_OF_control/init.c if test 1076 -ne `wc -c <control/init.c`; then echo shar: \"control/init.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/prompted.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/prompted.c\" else echo shar: Extracting \"control/prompted.c\" \(4097 characters\) sed "s/^X//" >control/prompted.c <<'END_OF_control/prompted.c' X/* X * prompted.c X * X * get a string of text X */ X X# include <X/Xlib.h> X# include "control.h" X# include "co_class.h" X# include <sys/time.h> X# include <assert.h> X X/* X * prompted window on screen X */ X X# define PROMPTED_Y 350 X# define PROMPTED_HP 50 /* padding */ X# define MIN_PROMPTED_W (TEXTBOX_X + TEXTBOX_W + 20) X# define TEXTBOX_X (10) X# define TEXTBOX_Y (PROMPTED_HP) X# define TEXTBOX_W 150 X# define TEXTBOX_VP (10) X# define LABEL_Y 25 X X# define OK_X (10) X# define OK_Y (PROMPTED_HP * 2) X X# define TEXT_OFF 5 X Xextern int co_inited; Xstatic char *label; Xstatic int prompted_h, prompted_w, label_x; Xstatic Window prompted, textbox, ok; Xstatic char returnbuffer[256]; Xstatic char *retpointer; X Xstatic int prompted_done; X Xchar * Xco_prompted(title) Xchar *title; X{ X XEvent rep; X int state, prompted_x; X int label_w; X int co_OKstate (), co_promptedEvent (), CmanageButton (), textbox_event (); X int mask; X X if (!co_inited) X co_init(); X if (!co_font->widths) X co_font->widths = XFontWidths (co_font->id); X label = title; X label_w = XQueryWidth(label, co_font->id); X X if (MIN_PROMPTED_W+10 >= label_w) X prompted_w = MIN_PROMPTED_W+10; X else X prompted_w = label_w + 10; X X label_x = prompted_w / 2 - label_w / 2; X prompted_h = OK_Y + 60; X prompted_x = 512 - prompted_w/2; X prompted = XCreateWindow (RootWindow, prompted_x, PROMPTED_Y, X prompted_w, prompted_h, 1, co_border, co_background); X ok = CmapButton (prompted, OK_X, OK_Y, co_OK, co_OKstate); X textbox = XCreateWindow (prompted, TEXTBOX_X, TEXTBOX_Y, X TEXTBOX_W, co_font->height + TEXTBOX_VP * 2, 1, co_border, co_background); X bindEvent (prompted, ExposeWindow, co_promptedEvent); X bindEvent (ok, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow, X CmanageButton); X bindEvent (textbox, ExposeWindow|KeyPressed, textbox_event); X bindEvent (prompted, KeyPressed, textbox_event); X XMapWindow (textbox); X XMapWindow (prompted); X X prompted_done = 0; X retpointer = returnbuffer; X *retpointer = '\0'; X while (!prompted_done) { X dispatch (); X } X X unbindEvent (prompted, ExposeWindow); X unbindEvent (textbox, ExposeWindow|KeyPressed); X unbindEvent (prompted, KeyPressed); X unbindEvent (ok, ExposeWindow|ButtonPressed|ButtonReleased| X LeftDownMotion|RightDownMotion|MiddleDownMotion|LeaveWindow); X XUnmapWindow (prompted); X CunmapButton (ok); X XDestroyWindow (textbox); X XDestroyWindow (prompted); X *retpointer = '\0'; X return returnbuffer; X} X Xstatic Xco_promptedEvent (rep) XXEvent *rep; X{ X switch (rep->type) { X case ExposeWindow: X redisplayLabel (); X } X} X Xstatic Xco_OKstate (n) X{ X prompted_done = 1; X} X Xstatic XredisplayLabel () X{ X XText (prompted, label_x, LABEL_Y, label, strlen (label), co_font->id, X co_fore, co_back); X XFlush (); X} X Xstatic Xtextbox_event (rep) XXEvent *rep; X{ X char *buffer; X int count; X X switch (rep->type) { X case ExposeWindow: X redisplayText (); X break; X case KeyPressed: X buffer = XLookupMapping (rep, &count); X while (!prompted_done && count--) { X handle_char (*buffer++); X } X break; X } X} X Xstatic Xhandle_char (c) X{ X switch (c) { X case '\n': X case '\r': X *retpointer = '\0'; X prompted_done = 1; X break; X case '\b': X case '\0177': X if (retpointer > returnbuffer) { X c = *--retpointer; X *retpointer = '\0'; X draw_char (compute_width (returnbuffer), c, 0); X } X break; X case '\030': X case '\025': X draw_string (returnbuffer, 0); X retpointer = returnbuffer; X *retpointer = '\0'; X break; X default: X *retpointer = '\0'; X draw_char (compute_width (returnbuffer), c, 1); X *retpointer++ = c; X *retpointer = '\0'; X break; X } X} X Xstatic Xcompute_width (string) Xchar *string; X{ X if (co_font->widths) X return XStringWidth (string, co_font, 0, 0); X else X return XQueryWidth (string, co_font->id); X} X Xstatic Xdraw_char (pos, ch, on) X{ X XText (textbox, pos, TEXTBOX_VP, &ch, 1, co_font->id, X on ? co_fore : co_back, co_back); X} X Xstatic Xdraw_string (string, on) Xchar *string; X{ X XText (textbox, 0, TEXTBOX_VP, string, strlen(string), co_font->id, X on ? co_fore : co_back, co_back); X} X Xstatic XredisplayText () X{ X draw_string (returnbuffer, 1); X} END_OF_control/prompted.c if test 4097 -ne `wc -c <control/prompted.c`; then echo shar: \"control/prompted.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/scrollbar.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/scrollbar.c\" else echo shar: Extracting \"control/scrollbar.c\" \(17486 characters\) sed "s/^X//" >control/scrollbar.c <<'END_OF_control/scrollbar.c' X/* X * scrollbar.c X */ X X# include <X/Xlib.h> X# include "control.h" X Xstruct scrollbar { X Scrollbar scrollbar; X int forecolor, backcolor; X int orientation; X int borderwidth; X Pixmap border; X Pixmap background; X}; X Xstruct perwindow { X int length; X Window parent; X int locatorpos; X int locatormille; X int locatormax; X int locatormaxmille; X int state; X struct scrollbar *class; X int (*notify)(); X int (*onetrip)(); X}; X X/* X * possible values of ->state X */ X X# define GENERAL 0 X# define DRAGGING_LOCATOR 1 X# define DOWN 2 X# define DOWN_IN_DOWN_BOX 3 X# define DOWN_IN_UP_BOX 4 X Xstatic XAssocTable *perwindowTable; X Xstatic XAssocTable *assocTable; Xextern XAssocTable *XCreateAssocTable(); Xextern char *XLookUpAssoc(); Xstatic int nextScrollbar; X X# define minpos(p) (0) X# define maxpos(p) ((p)->length - 2 * ARROWWIDTH) X# define milletopos(m,p) ((m) * maxpos(p) / 1000) X# define postomille(l,p) ((l) * 1000 / maxpos (p)) X# define postoloc(l) ((l) + ARROWWIDTH) X# define loctopos(l) ((l) - ARROWWIDTH) X X# define ARROWHEIGHT SCROLLWIDTH X# define ARROWWIDTH SCROLLWIDTH X# define LOCATORWIDTH SCROLLWIDTH / 3 X Xstatic short arrowDownOff[50] = { X 0xffff, 0x01ff, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0xff80, 0x0003, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x0080, 0x0002, X 0x00f8, 0x003e, X 0x0010, 0x0010, X 0x0020, 0x0008, X 0x0040, 0x0004, X 0x0080, 0x0002, X 0x0100, 0x0001, X 0x8200, 0x0000, X 0x4400, 0x0000, X 0x2800, 0x0000, X 0x1000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X}; X Xstatic short arrowDownOn[50] = { X 0xffff, 0x01ff, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xff80, 0x0003, X 0xfff8, 0x003f, X 0xfff0, 0x001f, X 0xffe0, 0x000f, X 0xffc0, 0x0007, X 0xff80, 0x0003, X 0xff00, 0x0001, X 0xfe00, 0x0000, X 0x7c00, 0x0000, X 0x3800, 0x0000, X 0x1000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X 0x0000, 0x0000, X}; X Xstatic short arrowUpOff [50], arrowRightOff [50], arrowLeftOff [50]; Xstatic short arrowUpOn [50], arrowRightOn [50], arrowLeftOn [50]; X Xstatic Cursor dragCursor; Xstatic short dragBits[16]; X Xstatic int arrowsSet; X XScrollbar XCcreateScrollbar (orientation, forecolor, backcolor, X borderwidth, border, background) Xint orientation; Xint forecolor, backcolor; Xint borderwidth; XPixmap border; XPixmap background; X{ X char *malloc (); X struct scrollbar *s; X X if (!assocTable) X assocTable = XCreateAssocTable (8); X if (!arrowsSet) { X setArrows(); X arrowsSet = 1; X } X s = (struct scrollbar *) malloc (sizeof (struct scrollbar)); X nextScrollbar++; X s->scrollbar = nextScrollbar; X XMakeAssoc (assocTable, nextScrollbar, (char *) s); X s->forecolor = forecolor; X s->backcolor = backcolor; X s->orientation = orientation; X s->borderwidth = borderwidth; X s->border = border; X s->background = background; X return s->scrollbar; X} X XWindow XCmapScrollbar (parent, scrollbar, notify, onetrip) XWindow parent; XScrollbar scrollbar; Xint (*notify)(), (*onetrip)(); X{ X struct scrollbar *s; X struct perwindow *p; X Window w; X int x, y, width, height; X Window grandParent; X Window *siblings; X int nsiblings; X X s = (struct scrollbar *) XLookUpAssoc (assocTable, scrollbar); X if (!s) { X printf ("scroll bar %d doesn't exist\n", scrollbar); X return 0; X } X if (!XQueryTree (parent, &grandParent, &nsiblings, &siblings)) { X printf ("can't get tree\n"); X return 0; X } X if (siblings) X free (siblings); X if (!computePosition (parent, s, &x, &y, &width, &height)) X return 0; X w = XCreateWindow (grandParent, x, y, width, height, s->borderwidth, X s->border, s->background); X XMapWindow (w); X if (!perwindowTable) X perwindowTable = XCreateAssocTable (64); X p = (struct perwindow *) malloc (sizeof (struct perwindow)); X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X p->length = width; X break; X case SCROLL_RIGHT: X case SCROLL_LEFT: X p->length = height; X break; X } X p->state = GENERAL; X p->parent = parent; X p->class = s; X p->locatorpos = -1; X p->notify = notify; X p->onetrip = onetrip; X XMakeAssoc (perwindowTable, w, p); X drawArrows (w, s, p); X return w; X} X XCunmapScrollbar (w) XWindow w; X{ X struct perwindow *p; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X XDeleteAssoc (perwindowTable, w); X free (p); X XDestroyWindow (w); X return 1; X} X XCresizeScrollbar (w) XWindow w; X{ X struct perwindow *p; X struct scrollbar *s; X int x, y, width, height; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X s = p->class; X if (!computePosition (p->parent, s, &x, &y, &width, &height)) X return 0; X XConfigureWindow (w, x, y, width, height); X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X p->length = width; X break; X case SCROLL_RIGHT: X case SCROLL_LEFT: X p->length = height; X break; X } X if (p->locatorpos >= 0) X p->locatorpos = milletopos (p->locatormille, p); X if (p->locatormax >= 0) X p->locatormax = milletopos (p->locatormaxmille, p); X return 1; X} X XWindow XCgetScrollbarParent (w) XScrollbar w; X{ X struct perwindow *p; X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X return p->parent; X} X XCredrawScrollbar (w) XWindow w; X{ X struct scrollbar *s; X struct perwindow *p; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X s = p->class; X drawArrows (w, s, p); X moveLocator (w, s, p, p->locatorpos, p->locatormax); X return 1; X} X XCsetScrollbarLocator (w, permillemin, permillemax) XWindow w; Xint permillemin, permillemax; X{ X struct perwindow *p; X struct scrollbar *s; X X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X s = p->class; X if (permillemin < 0) X permillemin = 0; X if (permillemax > 1000) X permillemax = 1000; X moveLocator (w, s, p, X milletopos (permillemin, p), X milletopos (permillemax, p)); X return 1; X} X X# define NOWHERE 0 X# define IN_UP_BOX 1 X# define IN_DOWN_BOX 2 X# define IN_UP_AREA 3 X# define IN_DOWN_AREA 4 X# define IN_LOCATOR 5 X X/* X * used for dragging the locator around X */ X Xstatic int downmouse; /* mouse position at down click */ Xstatic int downloc; /* locator position at down click */ X XCmanageScrollbar (rep) XXEvent *rep; X{ X struct perwindow *p; X struct scrollbar *s; X int inwindow; X int region; X XButtonEvent *bevent; X XMouseMovedEvent *mevent; X int permille; X Window w; X X w = rep->window; X p = (struct perwindow *) XLookUpAssoc (perwindowTable, w); X if (!p) X return 0; X s = p->class; X switch (rep->type) { X case ExposeWindow: X CredrawScrollbar (w); X break; X case ButtonPressed: X switch (p->state) { X case GENERAL: X bevent = (XButtonEvent *) rep; X region = findRegion (s, p, bevent->x, bevent->y); X switch (region) { X case IN_LOCATOR: X p->state = DRAGGING_LOCATOR; X downloc = p->locatorpos; X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X downmouse = bevent->x; X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X downmouse = bevent->y; X break; X } X break; X case IN_UP_BOX: X p->state = DOWN_IN_UP_BOX; X highlightBox (w, s, p, p->state); X spinbutton (w, s, p, IN_UP_BOX); X break; X case IN_DOWN_BOX: X p->state = DOWN_IN_DOWN_BOX; X highlightBox (w, s, p, p->state); X spinbutton (w, s, p, IN_DOWN_BOX); X break; X case IN_DOWN_AREA: X case IN_UP_AREA: X p->state = DOWN; X switch (region) { X case IN_DOWN_AREA: X p->notify (p->parent, SCROLL_DOWN_AREA); X break; X case IN_UP_AREA: X p->notify (p->parent, SCROLL_UP_AREA); X break; X } X } X } X break; X case ButtonReleased: X switch (p->state) { X case DRAGGING_LOCATOR: X p->state = GENERAL; X break; X case DOWN_IN_DOWN_BOX: X case DOWN_IN_UP_BOX: X unhighlightBox (w, s, p, p->state); X case DOWN: X p->state = GENERAL; X break; X } X case MouseMoved: X switch (p->state) { X case DRAGGING_LOCATOR: X p->notify (p->parent, updateLocator (w, s, p)); X p->onetrip (p->parent); X break; X } X break; X } X} X Xstatic Xspinbutton (w, s, p, region) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; Xint region; X{ X int r; X X r = region; X for (;;) { X switch (p->state) { X case DOWN_IN_UP_BOX: X case DOWN_IN_DOWN_BOX: X if (r != region) { X unhighlightBox (w, s, p, p->state); X p->state = GENERAL; X return; X } X switch (region) { X case IN_UP_BOX: X p->notify (p->parent, SCROLL_UP_BUTTON); X p->onetrip (p->parent); X break; X case IN_DOWN_BOX: X p->notify (p->parent, SCROLL_DOWN_BUTTON); X p->onetrip (p->parent); X break; X } X break; X default: X return; X } X r = checkMouseRegion (w, s, p); X } X} X Xstatic XfindRegion (s, p, x, y) Xstruct scrollbar *s; Xstruct perwindow *p; Xint x,y; X{ X int loc; X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X if (y < 0 || SCROLLWIDTH <= y) X return NOWHERE; X loc = x; X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X if (x < 0 || SCROLLWIDTH <= x) X return NOWHERE; X loc = y; X break; X } X if (loc < 0 || loc >= p->length) X return NOWHERE; X if (loc <= ARROWWIDTH) X return IN_UP_BOX; X if (loc >= p->length - ARROWWIDTH) X return IN_DOWN_BOX; X if (loc < postoloc (p->locatorpos)) X return IN_UP_AREA; X if (loc > postoloc (p->locatormax)) X return IN_DOWN_AREA; X return IN_LOCATOR; X} X Xstatic XupdateLocator (w, s, p) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; X{ X int pos, loc; X int subw, x, y; X int len; X X XUpdateMouse (w, &x, &y, &subw); X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X loc = x; X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X loc = y; X break; X } X pos = loctopos (postoloc (downloc) + loc - downmouse); X if (pos < minpos(p)) X pos = minpos(p); X if (pos > maxpos(p)) X pos = maxpos (p); X return postomille (pos, p); X} X Xstatic XcheckMouseRegion (w, s, p) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; X{ X int x, y, subw, state; X int region; X X XQueryMouseButtons (w, &x, &y, &subw, &state); X X /* X * Note that if we are playing back a recording then the button X * state will not be as it was during the recording (pressed), X * so we short circuit it. X */ X X region = findRegion (s, p, x, y); X if (region == IN_UP_BOX && p->state == DOWN_IN_UP_BOX) X return IN_UP_BOX; X if (region == IN_DOWN_BOX && p->state == DOWN_IN_DOWN_BOX) X return IN_DOWN_BOX; X return NOWHERE; X} X Xstatic XcomputePosition (parent, s, xp, yp, wp, hp) XWindow parent; Xstruct scrollbar *s; Xint *xp, *yp, *wp, *hp; X{ X WindowInfo parentInfo; X X /* make sure the server has the most recent parent info */ X XSync (0); X if (!XQueryWindow (parent, &parentInfo)) { X printf ("can't get window info for %d\n", parent); X return 0; X } X switch (s->orientation) { X case SCROLL_TOP: X *xp = 0; X *yp = -SCROLLWIDTH - parentInfo.bdrwidth; X *wp = parentInfo.width; X *hp = SCROLLWIDTH; X break; X case SCROLL_BOTTOM: X *xp = 0; X *yp = parentInfo.height + parentInfo.bdrwidth; X *wp = parentInfo.width; X *hp = SCROLLWIDTH; X break; X case SCROLL_LEFT: X *xp = -SCROLLWIDTH - parentInfo.bdrwidth; X *yp = 0; X *wp = SCROLLWIDTH; X *hp = parentInfo.height; X break; X case SCROLL_RIGHT: X *xp = parentInfo.width + parentInfo.bdrwidth; X *yp = 0; X *wp = SCROLLWIDTH; X *hp = parentInfo.height; X break; X } X *xp += parentInfo.x; X *yp += parentInfo.y; X return 1; X} X Xstatic XdrawArrows (w, s, p) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; X{ X int x, y; X short *up, *down; X X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X up = arrowLeftOff; X down = arrowRightOff; X x = p->length - ARROWWIDTH; X y = 0; X switch (p->state) { X case DOWN_IN_DOWN_BOX: X down = arrowRightOn; X break; X case DOWN_IN_UP_BOX: X up = arrowLeftOn; X break; X } X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X up = arrowUpOff; X down = arrowDownOff; X x = 0; X y = p->length - ARROWWIDTH; X switch (p->state) { X case DOWN_IN_DOWN_BOX: X down = arrowDownOn; X break; X case DOWN_IN_UP_BOX: X up = arrowUpOn; X break; X } X break; X } X XBitmapBitsPut (w, 0, 0, X ARROWWIDTH, ARROWHEIGHT, X up, s->forecolor, s->backcolor, X 0, GXcopy, AllPlanes); X XBitmapBitsPut (w, x, y, X ARROWWIDTH, ARROWHEIGHT, X down, s->forecolor, s->backcolor, X 0, GXcopy, AllPlanes); X} X Xstatic XhighlightBox (w, s, p, state) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; Xint state; X{ X drawoneArrow (w, s, p, state, 1); X} X Xstatic XunhighlightBox (w, s, p, state) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; Xint state; X{ X drawoneArrow (w, s, p, state, 0); X} X Xstatic XdrawoneArrow (w, s, p, state, on) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; Xint state; Xint on; X{ X int x, y; X short *bitsup, *bitsdown, *bits; X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X x = p->length - ARROWWIDTH; X y = 0; X if (on) { X bitsup = arrowLeftOn; X bitsdown = arrowRightOn; X } else { X bitsup = arrowLeftOff; X bitsdown = arrowRightOff; X } X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X x = 0; X y = p->length - ARROWWIDTH; X if (on) { X bitsup = arrowUpOn; X bitsdown = arrowDownOn; X } else { X bitsup = arrowUpOff; X bitsdown = arrowDownOff; X } X break; X } X switch (state) { X case DOWN_IN_UP_BOX: X x = 0; X y = 0; X bits = bitsup; X break; X case DOWN_IN_DOWN_BOX: X bits = bitsdown; X break; X default: X return; X } X XBitmapBitsPut (w, x, y, ARROWWIDTH, ARROWHEIGHT, X bits, s->forecolor, s->backcolor, X 0, GXcopy, AllPlanes); X} X Xstatic XmoveLocator (w, s, p, pos, max) XWindow w; Xstruct scrollbar *s; Xstruct perwindow *p; Xint pos, max; X{ X int x, y, lw, lh; X int x0, y0, w0, h0; X int x1, y1, w1, h1; X int oldpos, oldmax, oldlen; X X if (max > maxpos (p)) X max = maxpos (p); X if (max < 0) X max = 0; X if (pos > maxpos (p)) X pos = maxpos (p); X if (pos < 0) X pos = 0; X oldpos = p->locatorpos; X oldmax = p->locatormax; X oldlen = oldmax - oldpos; X w0 = h0 = w1 = h1 = 0; X switch (s->orientation) { X case SCROLL_TOP: X case SCROLL_BOTTOM: X x = postoloc (pos); X y = SCROLLWIDTH / 2 - LOCATORWIDTH / 2; X y0 = y; X y1 = y; X lh = LOCATORWIDTH; X lw = max - pos; X h0 = lh; X h1 = lh; X if (pos < 0) { X /* X * erase the whole thing X */ X x0 = postoloc (oldpos); X w0 = oldlen; X } else if (p->locatorpos < 0) { X ; X } else { X if (pos > oldpos) { X /* X * erase before portion X */ X x0 = postoloc (oldpos); X w0 = pos - oldpos; X } X if (max < oldmax) { X /* X * erase after portion X */ X x1 = postoloc (max); X w1 = oldmax - max; X } X } X break; X case SCROLL_LEFT: X case SCROLL_RIGHT: X x = SCROLLWIDTH / 2 - LOCATORWIDTH / 2; X y = postoloc (pos); X x0 = x; X x1 = x; X lw = LOCATORWIDTH; X lh = max - pos; X w0 = lw; X w1 = lw; X if (pos < 0) { X /* X * erase the whole thing X */ X y0 = postoloc (oldpos); X h0 = oldlen; X } else if (p->locatorpos < 0) { X ; X } else { X if (pos > oldpos) { X /* X * erase before portion X */ X y0 = postoloc (oldpos); X h0 = pos - oldpos; X } X if (max < oldmax) { X /* X * erase after portion X */ X y1 = postoloc (max); X h1 = oldmax - max; X } X } X break; X } X if (pos >= 0) { X Vertex v[5]; X X v[0].x = x; v[0].y = y; v[0].flags = 0; X v[1].x = lw-1; v[1].y = 0; v[1].flags = VertexRelative; X v[2].x = 0; v[2].y = lh-1; v[2].flags = VertexRelative; X v[3].x = 1-lw; v[3].y = 0; v[3].flags = VertexRelative; X v[4].x = 0; v[4].y = 1-lh; v[4].flags = VertexRelative; X XDraw (w, v, 5, 1, 1, s->forecolor, GXcopy, AllPlanes); X XPixSet (w, x+1, y+1, lw-2, lh-2, s->backcolor); X } X if (oldpos >= 0) { X if (w0 != 0 && h0 != 0) X XTileSet (w, x0, y0, w0, h0, s->background); X if (w1 != 0 && h1 != 0) X XTileSet (w, x1, y1, w1, h1, s->background); X } X p->locatorpos = pos; X p->locatormax = max; X p->locatormille = postomille (p->locatorpos, p); X p->locatormaxmille = postomille (p->locatormax, p); X} X X# define getbit(array, bit, word) ((word < 0 || word >= 50) ? 0 : ((array[word] >> bit) & 01)) X Xstatic XsetArrows () X{ X int i; X int word, bit; X unsigned short tempLeftOn[2], tempRightOn[2]; X unsigned short tempLeftOff[2], tempRightOff[2]; X int on, off; X Bitmap dragmap; X int k; X X for (i = 0; i < ARROWHEIGHT; i++) X for (k = 0; k < 2; k++) { X arrowUpOff[i*2+k] = arrowDownOff[(ARROWHEIGHT-1-i)*2 + k]; X arrowUpOn[i*2+k] = arrowDownOn[(ARROWHEIGHT-1-i)*2 + k]; X } X for (i = 0; i < ARROWHEIGHT; i++) { X for (k = 0; k < 2; k++) { X tempLeftOn[k] = tempLeftOff[k] = 0; X tempRightOn[k] = tempRightOff[k] = 0; X } X for (k = 0; k < 2; k++) { X for (bit = 0; bit < 16; bit++) { X off = getbit (arrowDownOff, X i % 16, ((15-bit) + (1-k) * 16) * 2 + i/16); X on = getbit (arrowDownOn, X i % 16, ((15-bit) + (1-k) * 16) * 2 + i/16); X if (off) X tempRightOff[1-k] |= (1 << (15 - bit)); X if (on) X tempRightOn[1-k] |= (1 << (15 - bit)); X off = getbit (arrowUpOff, X i % 16, ((15-bit) + (1-k) * 16) * 2 + i/16); X on = getbit (arrowUpOn, X i % 16, ((15-bit) + (1-k) * 16) * 2 + i/16); X if (off) X tempLeftOff[1-k] |= (1 << (15 - bit)); X if (on) X tempLeftOn[1-k] |= (1 << (15 - bit)); X } X } X arrowLeftOn[i*2] = tempLeftOn[0]; X arrowLeftOn[i*2+1] = tempLeftOn[1]; X arrowLeftOff[i*2] = tempLeftOff[0]; X arrowLeftOff[i*2+1] = tempLeftOff[1]; X arrowRightOn[i*2] = tempRightOn[0]; X arrowRightOn[i*2+1] = tempRightOn[1]; X arrowRightOff[i*2] = tempRightOff[0]; X arrowRightOff[i*2+1] = tempRightOff[1]; X } X dragmap = XStoreBitmap (16, 16, dragBits); X dragCursor = XStoreCursor (dragmap, dragmap, 8, 8, 1, 0, GXnoop); X} END_OF_control/scrollbar.c if test 17486 -ne `wc -c <control/scrollbar.c`; then echo shar: \"control/scrollbar.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f control/test.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"control/test.c\" else echo shar: Extracting \"control/test.c\" \(82 characters\) sed "s/^X//" >control/test.c <<'END_OF_control/test.c' Xmain () X{ X XOpenDisplay (""); X while (!co_affirm ("Do you want to leave?")) X ; X} END_OF_control/test.c if test 82 -ne `wc -c <control/test.c`; then echo shar: \"control/test.c\" unpacked with wrong size! fi # end of overwriting check fi echo shar: End of archive 4 \(of 4\). cp /dev/null ark4isdone MISSING="" for I in 1 2 3 4 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have unpacked all 4 archives. rm -f ark[1-9]isdone else echo You still need to unpack the following archives: echo " " ${MISSING} fi ## End of shell archive. exiGGGX