home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 579a.lha / FindDisk_v3.3 / src.LZH / src / fd3src / intuimain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-25  |  13.8 KB  |  529 lines

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