home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 455.lha / finddisk_v3.1 / fd3src / intuimain.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-10  |  13.4 KB  |  504 lines

  1.  
  2. /*                            FindDisk3.1
  3.  
  4.      Author      : Ross MacGregor
  5.      Date        : 10/04/88
  6.      Last Update : 13/08/89
  7.      Comments    : This is a public domain program.                        */
  8.  
  9. /* Thanks must go to Radical Eye's dfc from which much of the following
  10.    intuition stuff was ported */
  11.  
  12. #include <intuition/intuition.h>
  13. #include <functions.h>
  14. #include <exec/exec.h>
  15. #include <exec/execbase.h>
  16. #include <libraries/dosextens.h>
  17.  
  18. #define MAXSTRGADL 40
  19. #define MAXPATHLEN 80  /*  *** ALSO DEFINED IN fd3.c *** */
  20.  
  21. #define TITLE "FindDisk3.1"
  22. #define BLURB "   PDSoft 1989  Ross MacGregor"
  23. /*
  24.  *   These are the various globals this routine uses.
  25.  */
  26. struct StandardPacket *gpacket ;   /* packet to send various things */
  27. struct IntuiMessage *message ;     /* the message we are working on */
  28. struct Gadget *gadad ;             /* address of gadget from message */
  29. struct Window *window ;            /* our window */
  30. struct MsgPort *port ;             /* I/O port for dos communication */
  31. struct IntuitionBase *IntuitionBase ;
  32.                                    /* we need Intuition */
  33. struct GfxBase *GfxBase ;          /* and graphics */
  34.  
  35. long output;
  36. char strbuf[MAXSTRGADL+1] ;            /* this buffer holds fd input */
  37. int  newcheck;
  38. int  shdsp,subdir;
  39.  
  40. extern char *nullstr;
  41. extern char dlpath[];
  42.  
  43. /*
  44.  *   Always use topaz 80, and let's define an intuition buffer for our
  45.  *   general string writing stuff.
  46.  */
  47. struct TextAttr myfont = {(STRPTR)"topaz.font", TOPAZ_EIGHTY, 0, 0 };
  48. struct IntuiText intuitext = {1, 0, JAM2, 0, 0, &myfont} ;
  49. /*
  50.  *   Some defines setting the sizes of various things.  Play with these to
  51.  *   resize things.  Too bad string gadgets still have problems with large
  52.  *   fonts.
  53.  */
  54.  
  55. #define WIDEGADG 85
  56. #define SWITCHGADG 134
  57. #define STRGADG 263
  58. #define CURGADG 180
  59. #define GADGHEIGHT 13
  60.  
  61. #define LINE1 13
  62. #define LINE2 LINE1+13
  63. #define LINE3 LINE2+16
  64. #define LINE4 LINE3+GADGHEIGHT
  65. #define LINE5 LINE4+GADGHEIGHT+6
  66. #define LINE6 LINE5+GADGHEIGHT
  67.  
  68. #define COL1 4
  69. #define COL2 COL1+WIDEGADG+10
  70. #define COL3 COL2+WIDEGADG+10
  71. #define MID  COL2+45
  72.  
  73. #define STRSTART COL1
  74.  
  75. #define WINDOWHEIGHT LINE6+GADGHEIGHT+4
  76. #define WINDOWWIDTH COL3+WIDEGADG+4
  77.  
  78. /*
  79.  *   Now, some defines to assist in declaring a few things, and
  80.  *   centering strings, and the like.  These macros make the Intuition
  81.  *   crap a lot easier to put together.
  82.  */
  83. #define makeintuitext(name,string,size) struct IntuiText name={1,0,JAM2,\
  84.    (1+size-8*(sizeof(string)-1))/2,3,&myfont,(UBYTE*)string}
  85. #define makebox(name,tname,width,height,off) short tname[]={0,0,width,0,\
  86.    width,height,0,height,0,0};\
  87.    struct Border name={off,off,1,0,JAM2,5,tname}
  88. #define makegadg(name,prev,xpos,ypos,text,ch) struct Gadget name={prev,\
  89.    xpos,ypos,WIDEGADG,GADGHEIGHT,GADGHCOMP,RELVERIFY,BOOLGADGET,\
  90.    (APTR)&widebox,NULL,&text,NULL,NULL,ch}
  91. #define makeswgadg(name,prev,xpos,ypos,text,ch) struct Gadget name={prev,\
  92.    xpos,ypos,SWITCHGADG,GADGHEIGHT,GADGHCOMP,RELVERIFY,BOOLGADGET,\
  93.    (APTR)&swbox,NULL,&text,NULL,NULL,ch}
  94. /*
  95.  *   Now we declare all of our structures with the above macros.
  96.  *   First, the text for our gadgets, and then the gadgets.  No sweat.
  97.  */
  98.  
  99. makeintuitext(sdirtext,"SaveDir",WIDEGADG);
  100. makeintuitext(updatetext,"UpdateDL",WIDEGADG);
  101. makeintuitext(loadtext,"LoadDL",WIDEGADG);
  102. makeintuitext(savetext,"SaveDL",WIDEGADG);
  103. makeintuitext(newtext,"NewDL",WIDEGADG);
  104. makeintuitext(deletetext,"RemDir",WIDEGADG);
  105. makeintuitext(subdirtext, "Sub-Directories",SWITCHGADG);
  106. makeintuitext(rootdirtext,"Root Directory ",SWITCHGADG);
  107. makeintuitext(stddsptext,"Standard Display",SWITCHGADG);
  108. makeintuitext(shdsptext, "   Short Form   ",SWITCHGADG);
  109.  
  110.  
  111. makebox(swbox,tn4,SWITCHGADG-1,GADGHEIGHT-1,0);
  112. makebox(widebox,tn2,WIDEGADG-1,GADGHEIGHT-1,0);
  113. makebox(strbox,tn3,STRGADG-1,GADGHEIGHT-1,-2);
  114. makebox(curbox,tn1,CURGADG-1,GADGHEIGHT-3,-2);
  115.  
  116. makeswgadg(dirgadg,NULL,COL1+1,LINE6,rootdirtext,'w');
  117. makeswgadg(dspgadg,&dirgadg,MID,LINE6,stddsptext,'x');
  118. makegadg(sdirgadg,&dspgadg,COL1,LINE3,sdirtext,'d');
  119. makegadg(updategadg,&sdirgadg,COL1,LINE4,updatetext,'u');
  120. makegadg(loadgadg,&updategadg,COL2,LINE3,loadtext,'l');
  121. makegadg(savegadg,&loadgadg,COL2,LINE4,savetext,'s');
  122. makegadg(newgadg,&savegadg,COL3,LINE3,newtext,'n');
  123. makegadg(delgadg,&newgadg,COL3,LINE4,deletetext,'r');
  124.  
  125. /*
  126.  *   We need two last gadgets, the string gadgets, and their associated
  127.  *   special info structure.
  128.  */
  129. struct StringInfo strinfo={(UBYTE*)strbuf,NULL,0,MAXSTRGADL};
  130. struct Gadget strgadg={&delgadg,STRSTART+6,LINE2,STRGADG,
  131.    GADGHEIGHT-5,GADGHCOMP,STRINGCENTER|RELVERIFY,STRGADGET,(APTR)&strbox,
  132.    NULL,NULL,NULL,(APTR)&strinfo,'f'};
  133.  
  134. struct StringInfo curinfo={(UBYTE*)dlpath,NULL,0,MAXPATHLEN};
  135. struct Gadget curgadg={&strgadg,COL2+1,LINE5,CURGADG,
  136.    GADGHEIGHT-5,GADGHCOMP,RELVERIFY,STRGADGET,(APTR)&curbox,
  137.    NULL,NULL,NULL,(APTR)&curinfo,'c'};
  138.  
  139. /*
  140.  *   Now we have our window structure.  Initially not resizeable.
  141.  */
  142. struct NewWindow newwindow = {200,20,WINDOWWIDTH,WINDOWHEIGHT,0,1,
  143.    CLOSEWINDOW|ACTIVEWINDOW|GADGETDOWN|GADGETUP,
  144.    WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|SMART_REFRESH|ACTIVATE,
  145.    &curgadg,NULL,(UBYTE *)TITLE,NULL,NULL,-1,-1,-1,-1,WBENCHSCREEN};
  146.  
  147.  
  148. void cleanup(), setargs();
  149.  
  150. #define print(s) draw(to34(s,nullstr), COL1, LINE1)
  151.  
  152. void draw(s, x, y)
  153. char *s ;
  154. int x, y ;
  155. {
  156.    intuitext.IText = (UBYTE *)s ;
  157.    PrintIText(window->RPort, &intuitext, (long)x, (long)y) ;
  158. }
  159.  
  160. /*
  161.  *   This routine looks at the global message and returns a character
  162.  *   indicating the selected gadget.  This gives us easy equivalence
  163.  *   of gadgets to vanillakeys, for instance.  This also replymsg()'s
  164.  *   the message.
  165.  */
  166. int getop() {
  167.    register long class ;
  168.    register int op ;
  169.    short code ;
  170.  
  171.    class = message->Class ;
  172.    code = message->Code ;
  173.    op = ' ' ;
  174.    gadad = (struct Gadget *)(message->IAddress) ;
  175.    ReplyMsg(message) ;
  176.    message = NULL ;
  177.    if (class == CLOSEWINDOW)
  178.       op = 'q' ;
  179.    else if (class == GADGETDOWN || class == GADGETUP)
  180.       op = gadad->GadgetID ;
  181.    else if (class == ACTIVEWINDOW)
  182.    {
  183.       op = 'a';
  184.       gadad = NULL ;
  185.    }
  186.    return(op) ;
  187. }
  188.  
  189. int disposemsgs() {
  190.    register int op = 0 ;
  191.  
  192.    while (message = (struct IntuiMessage *)
  193.                         GetMsg(window->UserPort))
  194.       op = getop() ;
  195.    return(op) ;
  196. }
  197.  
  198.  
  199. /*
  200.  *   This routine pads 2 strings out to 34 characters.  Used to write to
  201.  *   the top line of the window.
  202.  */
  203. static char ibuf[34] ;
  204. char *to34(s1,s2)
  205. register char *s1, *s2 ;
  206. {
  207.    register int i = 0 ;
  208.    register char *p = ibuf ;
  209.  
  210.    while (*s1 != 0)
  211.       p[i++] = *s1++ ;
  212.    while (*s2 != 0)
  213.       p[i++] = *s2++ ;
  214.    while (i < 34)
  215.       p[i++] = ' ' ;
  216.    return(p) ;
  217. }
  218.  
  219. extern char *errorstr;
  220. void error(s)
  221. register char *s;
  222. {
  223.    if (*s == '!' || !window) {
  224.       if (output) {
  225.          Write(output, errorstr, (long)strlen(errorstr)) ;
  226.          Write(output, "\n",1L);
  227.       }
  228.       cleanup() ;
  229.    }
  230.    DisplayBeep(NULL) ;
  231.    draw(to34(errorstr,s), COL1, LINE1) ;
  232.    errorstr=nullstr;
  233. }
  234.  
  235.  
  236. /*
  237.  *   This is our exit routine.  It frees the drives, deletes the ports,
  238.  *   buffers, closes the window, and libraries.  Then it exits.
  239.  */
  240. void cleanup()
  241. {
  242.    register int i ;
  243.  
  244.    if (gpacket)
  245.       FreeMem(gpacket, (long)sizeof(struct StandardPacket)) ;
  246.    if (port)
  247.       DeletePort(port) ;
  248.    if (window)
  249.       CloseWindow(window) ;
  250.    if (GfxBase)
  251.       CloseLibrary(GfxBase) ;
  252.    if (IntuitionBase)
  253.       CloseLibrary(IntuitionBase) ;
  254.    exit(0) ;
  255. }
  256.  
  257.  
  258. /*
  259.  *   This routine turns on a particular gadget.
  260.  */
  261. void turnon(g)
  262. register struct Gadget *g ;
  263. {
  264.    if (g->Flags & GADGDISABLED) {
  265.       SetAPen(window->RPort, 0L) ;
  266.       RectFill(window->RPort, (long)g->LeftEdge, (long)g->TopEdge,
  267.                               (long)g->LeftEdge+g->Width-1,
  268.                               (long)g->TopEdge+g->Height-1) ;
  269.       SetAPen(window->RPort, 1L) ;
  270.       OnGadget(g, window, NULL) ;
  271.    }
  272. }
  273.  
  274. /*
  275.  *   This routine turns off a particular gadget.
  276.  */
  277. void turnoff(g)
  278. register struct Gadget *g ;
  279. {
  280.    if (!(g->Flags & GADGDISABLED))
  281.       OffGadget(g, window, NULL) ;
  282. }
  283.  
  284.  
  285. /* Here at the main we pull in variables and call routines frow fd3.c */
  286.  
  287. extern char *dldisk, *dltemp, *sdir_pat;
  288. extern char **glargv;
  289. extern int  glargc, con_print_line;
  290. char *yaargv[17];
  291.  
  292. char *getenv(), *strchr(), *firstchar();
  293. struct DeviceNode *finddosnode();
  294. void get_dlpath();
  295.  
  296. void main(argc, argv)
  297. int argc ;
  298. char *argv[] ;
  299. {
  300.   int op, rc ;
  301.   char *p;
  302.  
  303.   output = (long)Output() ;
  304.   errorstr=nullstr;
  305.  
  306.   glargv=argv;
  307.   glargc=argc;
  308.  
  309.   if( finddosnode("ENV") )
  310.   {
  311.     dldisk=getenv("env:dldisk",dldisk);
  312.     dltemp=getenv("env:dltemp",dltemp);
  313.     sdir_pat=getenv("env:dldrive",sdir_pat);
  314.   }
  315.   get_dlpath();
  316.  
  317.   if( argc>1 )
  318.   {
  319.     if( finddisk() )
  320.       error("!");
  321.     return;
  322.   }
  323.  
  324. /*
  325.  *   We try and open things up!  First intuition, then graphics,
  326.  *   then our window and an I/O port.  If any of these fail, we simply
  327.  *   exit.
  328.  */
  329.   if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary(
  330.         "intuition.library",33L))!=NULL &&
  331.       (GfxBase = (struct GfxBase *)OpenLibrary(
  332.         "graphics.library",0L))!=NULL &&
  333.         (window=OpenWindow(&newwindow))!=NULL &&
  334.       (port=CreatePort(0L, 0L)) &&
  335.       (gpacket=(struct StandardPacket *)AllocMem(
  336.               (long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR)))
  337.   {
  338.  
  339.     if( errorstr!=nullstr )
  340.       error(nullstr);
  341.     print(BLURB);
  342.     draw("CurrentDL:",COL1+6,LINE5);
  343.     if( !strcmp(dldisk,dlpath) )
  344.       turnoff(&savegadg);
  345.     if( tmpsize()<10 )
  346.       turnoff(&updategadg);
  347.  
  348. /*
  349.  *   Wait for a message.  After getting one,
  350.  *   we drop into a case statement keying off what message it was.
  351.  */
  352.     while (1)
  353.     {
  354.  
  355.        RefreshGadgets(window->FirstGadget, window, NULL) ;
  356.        while ((message = (struct IntuiMessage *)
  357.                       GetMsg(window->UserPort))==NULL)
  358.           WaitPort(window->UserPort) ;
  359.        op = getop() ;
  360.        rc=1;
  361.        switch(op)
  362.        {
  363.          case 'd' : if( rc=sdir() )
  364.                       error(" SaveDir failed");
  365.                     else
  366.                       turnon(&updategadg);
  367.                     break;
  368.  
  369.          case 'u' : if( rc=update() )
  370.                       error(" Update failed");
  371.                     else
  372.                       turnoff(&updategadg);
  373.                     break;
  374.  
  375.          case 'n': print(" NewDL: Are you sure (y/n) ?");
  376.                    ActivateGadget(&strgadg, window, NULL) ;
  377.                    newcheck=1;
  378.                    break;
  379.  
  380.          case 'a' :  if (gadad == NULL )
  381.                        ActivateGadget(&strgadg, window, NULL) ;
  382.                      break ;
  383.  
  384.          case 'l' : if( rc=loaddl() )
  385.                       error(" LoadDL failed");
  386.                     else
  387.                       turnon(&savegadg);
  388.                     break;
  389.  
  390.          case 's' : if( rc=savedl() )
  391.                     {
  392.                       error(" SaveDL failed");
  393.                       break;
  394.                     }
  395.                     turnoff(&updategadg);
  396.                     turnoff(&savegadg);
  397.                     break;
  398.  
  399.          case 'r' : if( rc=remove() )
  400.                       if( rc==1)
  401.                         error(" Specify volume to be removed:");
  402.                       else
  403.                         error(" RemDir failed");
  404.                     break;
  405.  
  406.          case 'f' : if( newcheck )
  407.                     {
  408.                       rc=1;
  409.                       newcheck=0;
  410.                       if( *(firstchar(strbuf))=='y' )
  411.                         if( newdl() )
  412.                           print(" NewDL failed");
  413.                         else
  414.                           print(" Empty DiskList created.");
  415.                       else
  416.                         print(" NewDL aborted.");
  417.                       break;
  418.                     }
  419.  
  420.                     if( *(p=firstchar(strbuf)) )
  421.                     {
  422.                       print(" Searching...");
  423.                       setargs(p);
  424.                       if( rc=finddisk() )
  425.                       {
  426.                         error(nullstr);
  427.                         break;
  428.                       }
  429.                       if( !con_print_line )
  430.                       {
  431.                         print(" Not found.");
  432.                         rc=1;
  433.                       }
  434.                     }
  435.                     else
  436.                     {
  437.                       error(" No search strings given.");
  438.                       rc=1;
  439.                     }
  440.                     break;
  441.  
  442.          case 'w':  subdir=!subdir;
  443.                     RemoveGadget(window,&dirgadg);
  444.                     if( subdir )
  445.                       dirgadg.GadgetText=&subdirtext;
  446.                     else
  447.                       dirgadg.GadgetText=&rootdirtext;
  448.                     AddGadget(window,&dirgadg);
  449.                     RefreshGadgets(&dirgadg, window, NULL) ;
  450.                     break;
  451.  
  452.          case 'x':  shdsp=!shdsp;
  453.                     RemoveGadget(window,&dspgadg);
  454.                     if( shdsp )
  455.                       dspgadg.GadgetText=&shdsptext;
  456.                     else
  457.                       dspgadg.GadgetText=&stddsptext;
  458.                     AddGadget(window,&dspgadg);
  459.                     RefreshGadgets(&dspgadg, window, NULL) ;
  460.                     break;
  461.  
  462.        }
  463.  
  464.        if( !rc )
  465.          print(BLURB);
  466.  
  467.        *strbuf='\0';
  468.        if (op == 'q')
  469.             break ;
  470.  
  471.     } /* while */
  472.  
  473.   }
  474.   else
  475.   {
  476.     errorstr="Couldn't open window";
  477.     error("!");
  478.   }
  479.   cleanup() ;
  480. }
  481.  
  482. void setargs(s)
  483.   char *s;
  484. {
  485.   char *p;
  486.  
  487.   for(glargc=1; glargc<=17; glargc++)
  488.     if( p=strchr(s,' ') )
  489.     {
  490.       *p='\0';
  491.       yaargv[glargc]=s;
  492.       if( *(s=firstchar(p+1))=='\0' )
  493.         break;
  494.     }
  495.     else
  496.     {
  497.       yaargv[glargc]=s;
  498.       break;
  499.     }
  500.   glargv=yaargv;
  501.   glargc++;
  502. }
  503.  
  504.