#undef DEBUG /************************************************************************ req.c This file contains a general-purpose that will prompt the user for a filename input. The program actually uses a window instead of a 'Requester' for greater flexibility. It will take control of your window's IDCMP Port when called. *** This material is copyright (c) 1986 by C. Heath of Microsmiths, Inc. Permission is granted to use these files in any way with the following exceptions: 1) The files shall not be posted on any telecommunications service, public or private, except for BIX until January 15, 1987. 2) The files may only be distributed in archive format, with no modifications. If you make any improvements on the file requester and would like to generally distribute them, please contact "cheath" on BIX, or write to: Microsmiths Inc, PO Box 561, Cambridge, MA 02140 3) The requester may be used in any commercial product, but must be in object code format. You are free to make modifications for use in your product. Permission is granted to Lattice, Inc, and to Manx, Inc, to include the source files in archive format. Thank you, and enjoy. ...cheath ************************************************************************/ #include #include #include #include #include #include #include "mandel.h" #define FAST register #define NL NULL #ifdef DEBUG # undef STATIC /* For the debugger's sake */ # define STATIC #endif extern TEXT *dmore(), *dinit(); STATIC struct FileLock *pdir = NL; STATIC struct FileInfoBlock *dir_info; STATIC struct Window *eW; /* Parent Window. Uck */ /* STATIC struct TextAttr MyFont = { "topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT }; */ #define MyFont Topaz80 /* FGAD requires a few unique gadget-ids, origined at FGAD */ /* In the unlikely event this conflicts with any of your gadgets, */ /* change this equate to allow 16 contiguous unique ID's */ #define FGAD 0x76c0L #define FCHARS FNAME_SIZE /* Number of chars allowed in file name */ #define DIR_SIZ DNAME_SIZE /* Number of chars in a dir name... */ #define MAX_STR DIR_SIZ+2L #define DENTS 5L /* Number of entries on screen */ /* It's not as simple as changing 'DENTS'... */ #define DSIZE FCHARS+1L /* Size of a directory entry */ #define DBUFSIZ 3000L /* Number of bytes to allocate for all ents */ #define BCOL 1L /* Background color */ #define FCOL 2L /* Foreground color */ #define RHGHT 120L #define RWDTH 320L #define ZWDTH 270L STATIC char ubuf[MAX_STR]; /* Undo buffer */ STATIC struct dirent { struct dirent *next; BOOL isfile; char dE[DSIZE+2]; }; STATIC struct dirent *FirstEntry; STATIC struct dirent *NextEntry; STATIC struct dirhead { struct dirent *next; }; STATIC struct dirhead ListHead; STATIC long curent,maxent; STATIC BOOL more; STATIC BOOL redisplay_flag; STATIC struct Window *wRq = NL; /* Requester window */ STATIC struct RastPort *wRp; /* Requester "Hailing" prompt */ STATIC struct IntuiText oTxt = { 2,2,JAM1,10,11,NL, NL ,NL}; STATIC struct IntuiText saydir = { 0,1,JAM2,0,1,&MyFont,(UBYTE *)"(dir) ",NL}; STATIC struct IntuiText rname[DENTS] = { /* File name list */ { 2,1,JAM2,48,1,NL,NL, NL }, { 2,1,JAM2,48,1,NL,NL, NL }, { 2,1,JAM2,48,1,NL,NL, NL }, { 2,1,JAM2,48,1,NL,NL, NL }, { 2,1,JAM2,48,1,NL,NL, NL } }; /* Display for file name ... */ STATIC SHORT oXY2[] = { -2,-2, RWDTH-78,-2, RWDTH-78,9, -2,9, -2,-2}; STATIC struct Border thebd = { 0,0, 2,3,JAM1, 5,oXY2, NL}; STATIC struct IntuiText otxt = { 2,2,JAM1,-40,0,&MyFont,(UBYTE *)"file",NL}; STATIC struct StringInfo osx = { NL ,ubuf, NL,DSIZE,NL,NL,NL,NL,NL,NL,NL,NL,NL}; STATIC struct Gadget ogx = { NL, 60,RHGHT-35,RWDTH-80 ,10, /* File name gadget */ GADGHCOMP, RELVERIFY , STRGADGET, (APTR)&thebd,NL,&otxt,NL,(APTR)&osx, FGAD+11,NL }; STATIC struct Gadget oga = { &ogx, 10,70, ZWDTH,10, /* Gadgets For */ GADGHCOMP, RELVERIFY, BOOLGADGET, /* Directory entries */ NL,NL, &rname[4] ,NL,NL, FGAD+10,NL }; STATIC struct Gadget og9 = { &oga, 10,60, ZWDTH,10, GADGHCOMP, RELVERIFY, BOOLGADGET, NL,NL, &rname[3] ,NL,NL, FGAD+9,NL }; STATIC struct Gadget og8 = { &og9, 10,50, ZWDTH,10, GADGHCOMP, RELVERIFY, BOOLGADGET, NL,NL, &rname[2] ,NL,NL, FGAD+8,NL }; STATIC struct Gadget og7 = { &og8, 10,40, ZWDTH,10, GADGHCOMP, RELVERIFY, BOOLGADGET, NL,NL, &rname[1] ,NL,NL, FGAD+7,NL }; STATIC struct Gadget og6 = { &og7, 10,30, ZWDTH,10, GADGHCOMP, RELVERIFY, BOOLGADGET, NL,NL, &rname[0] ,NL,NL, FGAD+6,NL }; /* Gadgets for requester */ STATIC SHORT oXY3[] = { 0,0, 50,0, 50,9, 0,9, 0,0}; STATIC SHORT oXY4[] = { 2,-2, 48,-2, 52,2, 52,7, 48,11, 2,11, -2,7, -2,2, 2,-2}; STATIC struct Border obd2 = { 0,0, 2,3,JAM1, 9,oXY4, NL}; STATIC struct Border obd1 = { 0,0, 3,2,JAM1, 5,oXY3, &obd2}; /* OTAY / CANCEL box */ STATIC struct IntuiText ot1 = { 0,0,JAM1,1,1,&MyFont,(UBYTE *)" OK ",NL}; STATIC struct IntuiText ot2 = { 0,0,JAM1,1,1,&MyFont,(UBYTE *)"Cancel",NL}; STATIC struct IntuiText dtxt = { 2,2,JAM1,-60,0,NL,(UBYTE *)"drawer",NL}; STATIC struct StringInfo os5 = { NL ,ubuf, NL,DIR_SIZ,NL,NL,NL,NL,NL,NL,NL,NL,NL}; STATIC struct Gadget og5 = { &og6, RWDTH/2-80,19,190,10, /* Directory */ GADGHCOMP, RELVERIFY, STRGADGET, NL,NL,&dtxt,NL,(APTR)&os5, FGAD+5,NL }; STATIC struct Image cc_img; STATIC struct PropInfo cc_prop = { AUTOKNOB | FREEVERT, 0,0, 0,MAXBODY,0,0,0,0,0,0 }; STATIC struct Gadget og3 = { &og5,RWDTH-39,20,20,60, /* Scroll Bar */ GADGHNONE,GADGIMMEDIATE | FOLLOWMOUSE, PROPGADGET, (APTR)&cc_img,NL,NL,NL,(APTR)&cc_prop,FGAD+3,NL }; STATIC struct Gadget og2 = { &og3, RWDTH-70,RHGHT-20, 50,10, /* CANCEL */ GADGHCOMP, RELVERIFY, BOOLGADGET, (APTR)&obd1,NL, &ot2,NL,NL, FGAD+2,NL }; STATIC struct Gadget og1 = { &og2, 20,RHGHT-20, 50,10, /* OTAY */ GADGHCOMP, /* Flags */ RELVERIFY, /* Activation */ BOOLGADGET, (APTR)&obd1,NL, /* GadgetRender, SelectRender */ &ot1,NL,NL, /* IntuiText, MutualExclude,SpecialInfo */ FGAD+1,NL }; /* Gadget Id, UserData */ /* Open a requester "Window" */ STATIC struct NewWindow NewFiles = { 160, 30, RWDTH,RHGHT, BCOL,FCOL, NL, /* Fill in AFTER opening ... */ SMART_REFRESH | ACTIVATE | RMBTRAP | WINDOWDRAG, &og1,NL,NL, NL, NL, RWDTH,RHGHT,RWDTH,RHGHT, WBENCHSCREEN }; IMPORT struct Library *IntuitionBase; /*************************************************** * get_fname(window,hail,ddef,ddir); * * Displays a window/requester that * gets a file name for device,directory,default file, extension * * Calling args: * window: Window making the request * screen: Screen, if NULL assumed workbench * (* Deleted by Olaf Seibert 6 May 1987 *) * hail: Text prompt at top of requester * ddef: Input default file-name. Has NO DIRECTORY OR EXTENSION. * ddir: Directory of file, may be null /* Set a file-requester with prompt 'hail' */ char *get_fname(cW,hail,ddef,ddir) struct Window *cW; /* Calling Window */ UBYTE *hail; /* Hailing prompt */ char *ddef; /* Propable file-name */ char *ddir; /* Directory in which to search */ { struct Screen *screen; /* screen .... got from the window */ FAST struct IntuiMessage *imes; /* Wait for message in HERE */ FAST struct Gadget *igad; /* Get Gadjet Mumbo Jumbo */ FAST long i,class; FAST TEXT *pnam; FAST char *retval; FAST BOOL keepon; if ( ! (eW = cW) ) return NL; osx.Buffer = ddef; /* Set default file name */ os5.Buffer = ddir; /* Set default device name */ for ( i=0; iTitle; if ((dir_info = AllocMem((long)sizeof(struct FileInfoBlock),0L)) == NULL) return NL; /* Modified by Olaf Seibert 6 may 1987 */ screen = cW->WScreen; NewFiles.Screen = screen; NewFiles.Type = screen->Flags & SCREENTYPE; NewFiles.LeftEdge = (screen->Width - RWDTH) / 2; NewFiles.TopEdge = (screen->Height - RHGHT) / 2; cc_prop.VertBody = MAXBODY; /* if (screen) (* User supplied a screen *) { NewFiles.Type = CUSTOMSCREEN; NewFiles.Screen = screen; } */ if ( ! (FirstEntry = (struct dirent *)AllocMem((long)DBUFSIZ,0L)) || ! (wRq = (struct Window *)OpenWindow( &NewFiles )) ) { if ( FirstEntry ) FreeMem(FirstEntry,(long)DBUFSIZ); /* notify("Can't Open Requester..."); */ FreeMem(dir_info,(long)sizeof(struct FileInfoBlock)); return NL; } SetWindowTitles(wRq, (CPTR) -1, hail); /* Set up directory, notify any errors... */ if ( pnam = dinit(ddir) ) notify(pnam); /* This optional line will activate a string gadget, which gets messed */ /* up by RefreshGadgets ... (intuition bug ?) */ /* if ( IntuitionBase->lib_Version > 32 ) { ActivateGadget(&ogx,wRq,0L); } */ wRp = wRq->RPort; wRq->UserPort = eW->UserPort; ModifyIDCMP(wRq,(long)(MOUSEBUTTONS | GADGETDOWN | GADGETUP | MOUSEMOVE)); SetAPen(wRp,1L); RectFill(wRp,4L,10L,(long)(RWDTH-5),(long)(RHGHT-4)); oTxt.IText = hail; /* Set calling arg */ oTxt.LeftEdge = (RWDTH - IntuiTextLength(&oTxt)) >> 1L; PrintIText(wRp,&oTxt,0L,0L); RefreshGadgets(&og1,wRq,(long)NL); for ( retval= NL, keepon=TRUE; keepon ; ) { while ( ! (imes=(struct IntuiMessage *)GetMsg(wRq->UserPort)) ) { if ( redisplay_flag ) { /* update in-gadget filenames */ i = ((maxent-DENTS) * (ULONG)cc_prop.VertPot + MAXBODY/2) / MAXBODY; if ( i > maxent-DENTS ) { i = maxent-DENTS; if ( i < 0 ) i = 0; } curent = i; display_names(); redisplay_flag = FALSE; } /* End If redisplay_flag */ if ( more ) { if (pnam = dmore()) /* Continue to read the directory */ notify(pnam); /* Yucko error */ /* if ( maxent <= DENTS ) redisplay_flag = TRUE; */ } else /* not more */ WaitPort(wRq->UserPort); } /* End While There Is No Message */ igad = (struct Gadget *)imes->IAddress; class = imes->Class; ReplyMsg(imes); switch (class) { case MOUSEMOVE: redisplay_flag = TRUE; break; case GADGETUP: case GADGETDOWN: switch ( i = igad->GadgetID) { case FGAD+6: case FGAD+7: case FGAD+8: case FGAD+9: case FGAD+10: /* Replace file or directory name */ pnam = rname[i - (FGAD+6)].IText; if ( rname[igad->GadgetID - (FGAD+6)].NextText == NL ) { RemoveGadget(wRq,&ogx); for (i=0; ifib_DirEntryType < 0L ) return "Bizzare Alert!!"; more = TRUE; return dmore(); } STATIC TEXT *dmore() { FAST struct dirent *p_D = NextEntry; FAST struct dirent *ptr = (struct dirent *)&ListHead; FAST struct dirent *plink; FAST TEXT *p_mung; FAST long i; FAST unsigned long VertBody; /* KosmoSoft */ if ( ! more ) return NL; if ( ExNext( pdir, dir_info ) ) { if ( (ULONG)p_D >= ((ULONG)FirstEntry + (ULONG)DBUFSIZ - (ULONG)sizeof(struct dirent)) ) { more = FALSE; return "Directory Truncated!"; } /* Here you can add a file/directory filter */ /* filename text string is at &p_D->dE[0] */ p_D->isfile = ( dir_info->fib_DirEntryType < 0L ); p_mung = &p_D->dE[0]; for ( i=0; ifib_FileName[i]) ) break; i = (long)p_mung; NextEntry = (struct dirent *)( (i+5L) & ~3L ); for ( i=maxent++; i>=0; i--) { /* Start at the top and walk down until the new name */ /* is 'smaller' than the next one (???) */ if ( ! (plink = ptr->next) ) break; if ( alpha_lower(p_D,plink) ) break; ptr = plink; } /* Insert the new name in front of the next one */ p_D->next = plink; ptr->next = p_D; redisplay_flag = maxent - i <= curent + DENTS; /* Adjust the scroll bar */ if (maxent <= DENTS) VertBody = MAXBODY; else VertBody = (ULONG) MAXBODY * DENTS / maxent; ModifyProp(&og3, wRq, NULL, (ULONG) cc_prop.Flags, 0L, (ULONG) cc_prop.VertPot, 0L, VertBody); return NL; } else return ( IoErr() == ERROR_NO_MORE_ENTRIES) ? (char *)(more = (long)NL) : "Error Reading Directory!!!"; } /* dedicated alphabetizing function for dmore() */ STATIC alpha_lower(snew,sold) struct dirent *snew,*sold; { FAST struct dirent *pnew = snew; FAST TEXT *ps1,*ps2, c,d; if ( pnew->isfile == sold->isfile) { ps1 = &pnew->dE[0]; ps2 = &sold->dE[0]; while ( (c=*ps1++) ) { if ( c > (d=*ps2++) ) return FALSE; else if ( c < d ) break; } return TRUE; } return !pnew->isfile; /* Directories first */ } /* Display directory stuff */ STATIC display_names() { FAST long i,new; FAST long x,y; FAST struct dirent *ohboy = (struct dirent *)&ListHead; new = curent; for ( i=0; inext; y = 20L; for (i=0; inext; rname[i].IText = &ohboy->dE[0]; if ( ohboy->isfile ) PrintIText(wRp,&rname[i],10L,y); else { rname[i].LeftEdge = 48; PrintIText(wRp,&saydir,10L,y); PrintIText(wRp,&rname[i],10L,y); rname[i].NextText = &saydir; } x = wRp->cp_x; } if ( x < ZWDTH+10 ) RectFill(wRp,x,y,(long)(ZWDTH+10),(long)(y+8L)); } } /************************************************** * rfnam() * Combines dir, plus name into dir */ STATIC rfnam(dir,fil_nam) char *dir,*fil_nam; { FAST char *pdst = dir; FAST char *psrc = fil_nam; FAST char c = ':'; while ( *pdst ) c = *pdst++; if ( c != ':') *pdst++ = '/'; while ( *pdst++ = *psrc++ ) ; } STATIC struct IntuiText b_txt = { 0,1,JAM2, 5,20,NL,NL, NL}; STATIC struct IntuiText p_txt = { 0,1,JAM2, 5,3,NL,"OK", NL}; /****************************************************************/ /* notify(txt) */ /* Prompts for Yes/No response */ STATIC notify(txt) TEXT *txt; { b_txt.IText = txt; AutoRequest(wRq,&b_txt,0L,&p_txt,0L,0L, (long)(IntuiTextLength(&b_txt)+50L),70L); } /* CloseWindowSafely() * This module should be used whenever you are sharing an IDCMP * message port with more than one window. Rather than calling CloseWindow(), * you should use CloseWindowSafely(). This will keep Intuition from * Guru Meditation, and thus is considered a good thing. You should still * use CloseWindow for the very last window you close. * The reason this is needed, is because Intuition will re-claim * any outstanding messages for a window when the window is closed. But... * it can't take them out of your message port. Thus, you will receive * invalid messages and bad things will happen. Very bad things. * This code is a slightly condensed version of the same routine * written by Neil Katin of Amiga for the April '86 Commodore Developers * Newsletter, Amiga Mail (tm). */ /* #include #include #include #include #include #include */ void CloseWindowSafely( p_wind ) struct Window *p_wind; { register struct IntuiMessage *msg; register struct IntuiMessage *succ; register struct Window *win = p_wind; register struct MsgPort *mp = (struct MsgPort *)win->UserPort; Forbid(); msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head; while ( succ=(struct IntuiMessage *)msg->ExecMessage.mn_Node.ln_Succ ) { if ( msg->IDCMPWindow == win ) { Remove ( msg ); ReplyMsg( msg ); } msg = succ; } win->UserPort = NULL; ModifyIDCMP( win, 0L ); Permit(); CloseWindow( win ); }