home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 357.lha / Skel / skel.c < prev    next >
C/C++ Source or Header  |  1990-03-13  |  7KB  |  359 lines

  1. /*
  2.  *  Skeleton Workbench application program
  3.  *
  4.  *    Version 1
  5.  *
  6.  *    by Joel Swank 1-9-89
  7.  *
  8.  */
  9.  
  10. /********* INCLUDES ************************ */
  11.  
  12.  
  13. #include <exec/types.h>
  14. #include <exec/io.h>
  15. #include <exec/memory.h>
  16. #include <libraries/dos.h>
  17. #include <intuition/intuition.h>
  18.  
  19.  
  20. /* Text in helpwin.h  */
  21. extern struct IntuiText opfailtxt ;
  22. extern struct IntuiText oktxt ;
  23. extern struct IntuiText cantxt ;
  24. extern struct IntuiText retrytxt ;
  25.  
  26. /* Data in doargs.c   */
  27. extern char title[];
  28. extern char filename[];
  29.  
  30. /* Defines for setting/clearing GADGDISABLED flag */
  31. #define OffGad(gad) (gad).Flags = gad.Flags | GADGDISABLED;
  32. #define OnGad(gad) (gad).Flags = gad.Flags & ~(GADGDISABLED);
  33.  
  34.  
  35. struct IntuitionBase *IntuitionBase ;
  36. struct GfxBase *GfxBase ;
  37.  
  38. struct Window *wG = NULL;
  39. struct RastPort *rpG;
  40. struct IntuiMessage *message;    /* the message from the IDCMP */
  41.  
  42. struct Window *OpenWindow();
  43. void *OpenLibrary();
  44. struct IntuiMessage *GetMsg();
  45. struct MenuItem *ItemAddress();
  46.  
  47. /* get the PowerWindows 2.0 code */
  48. #include "skwindow.h"
  49.  
  50. /* internal modes */
  51. long delay = 10L;      /* draw speed control */
  52. char deftitle[]  /* Default Window title */
  53.       = "No Title Specified";
  54.  
  55.  
  56.  
  57. main(argc,argv)
  58. int argc;
  59. char *argv[];
  60. {
  61.     UWORD code;
  62.     ULONG class;
  63.     APTR object;
  64.  
  65.  
  66.     strcpy(title,deftitle);    /* set default title */
  67.  
  68.     /* Open the libraries */
  69.  
  70.     IntuitionBase = (struct IntuitionBase *)
  71.         OpenLibrary("intuition.library", 0L);
  72.     if (IntuitionBase == NULL)
  73.         {
  74.         done(11);
  75.         }
  76.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);
  77.     if (GfxBase == NULL)
  78.         {
  79.         done(10);
  80.         }
  81.  
  82.     /*   Get the arguments */
  83.     if (argc == 0) getWBargs();
  84.     else getCLIargs(argc,argv);
  85.  
  86.     /* Set up the Gadgets */
  87.     OffGad(Stop_Gad)
  88.  
  89.  
  90.     wG = OpenWindow(&NewWindowStructure1);    /* open the window */
  91.     if ( wG == NULL )
  92.         {
  93.         done(12);
  94.         }
  95.  
  96.     rpG = wG->RPort;    /* get a rastport pointer for the window */
  97.  
  98.     SetMenuStrip(wG,&MenuList1);    /* attach my Menu */
  99.  
  100.     do_title();      /* write the title */
  101.  
  102.  
  103.     /* Wait for some User interaction */
  104.     while(1)
  105.     {
  106.         WaitPort(wG->UserPort);
  107.             while( (message = (struct IntuiMessage *)
  108.                 GetMsg(wG->UserPort) ) != NULL)
  109.             {
  110.                 code = message->Code;  /* MENUNUM */
  111.                 object = message->IAddress;  /* Gadget */
  112.                 class = message->Class;  /* IDCMP Flags */
  113.                 ReplyMsg(message);  /* Free the sender */
  114.                 switch (class)
  115.                     {
  116.                     case CLOSEWINDOW :
  117.                         done(0);   /* close gadget clicked */
  118.                     case GADGETUP:
  119.                         if (object == (APTR) &Go_Gad)
  120.                             {
  121.                             do_it();
  122.                             break;
  123.                             }
  124.                         /* add more gadget checks here */
  125.                         break;
  126.                     case MENUPICK:    /* menu selection made */
  127.                         /* I get a NULL message whenever the user brings
  128.                            up the menu but doesn't select anything    */
  129.                         if (code != MENUNULL) do_pick((USHORT)code);
  130.                         break;
  131.                     }
  132.             }
  133.     } 
  134. }
  135.  
  136. /*
  137.  * Cleanup and exit
  138.  */
  139.  
  140. done(how)
  141. int how;
  142. {
  143.     if (wG) ClearMenuStrip(wG);
  144.     if (wG) CloseWindow(wG);
  145.     if (GfxBase != NULL) CloseLibrary(GfxBase);
  146.     if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  147.     exit(how);
  148.  
  149.  /* exit codes 
  150.   * 0  User requested
  151.   * 10 graphics lib open fail
  152.   * 11 intuition lib open fail
  153.   * 12 main window open fail
  154.   * 13 _abort() called
  155.   * 14 Bad CLI args
  156.   */
  157. }
  158.  
  159.  
  160. /*
  161.  * do_pick : handle chain of menu selections
  162.  */
  163.  
  164. do_pick(menunum)
  165. USHORT menunum;
  166. {
  167. struct MenuItem *item, *ItemAddress();
  168.     while (menunum != MENUNULL)
  169.         {
  170.         switch(MENUNUM(menunum))
  171.             {
  172.             case 0:     /* Project Menu */
  173.             switch(ITEMNUM(menunum))
  174.                 {
  175.                 case 0: /* Open */
  176.                     do_open();
  177.                     break;
  178.                 case 1: /* About */
  179.                     about();
  180.                     break;
  181.                 case 2: /* Help */
  182.                     help();
  183.                     break;
  184.                 case 3: /* Quit */
  185.                     done(0);
  186.                 }
  187.             break;
  188.             case 1:     /* Options Menu */
  189.             switch(ITEMNUM(menunum))
  190.                 {
  191.  
  192.                 /* Three mutually excluded items. Really nothing to do.
  193.                    intuition keeps track, I just read the CHECKED flag
  194.                    in the SubItem              */
  195.  
  196.                 case 0:     /* WB Color Number */
  197.                 switch(SUBNUM(menunum))
  198.                     {
  199.                     case 0:   /* 1 */
  200.                         break;
  201.                     case 1:   /* 2 */
  202.                         break;
  203.                     case 2:   /* 3 */
  204.                         break;
  205.                     }
  206.                 break;
  207.                 }
  208.             break;
  209.             default:    /* What's this garbage ? */
  210.                 menunum = MENUNULL;
  211.             }       /* end switch MENUNUM  */
  212.  
  213.         /* Get chain to next selection. NextSelect contains another item
  214.            when the user makes multiple menu selections             */
  215.         item = ItemAddress(&MenuList1,(long) menunum);
  216.         menunum = item->NextSelect;
  217.         }
  218. }
  219.  
  220.  
  221. /*
  222.  *  do something
  223.  */
  224.  
  225. do_it()
  226. {
  227.     ULONG class;
  228.     struct IntuiMessage *message;    /* the message from the IDCMP */
  229.     long color;
  230.  
  231.     /* get color from menu structure */
  232.     if (SubItem1.Flags &CHECKED) color = 1;
  233.     if (SubItem2.Flags &CHECKED) color = 2;
  234.     if (SubItem3.Flags &CHECKED) color = 3;
  235.  
  236.     off_gads(); /* disable gads */
  237.  
  238.     while (1)  /* do till stop  or close msg comes in */
  239.         {
  240.  
  241.         if ( (message = (struct IntuiMessage *)
  242.           GetMsg(wG->UserPort) ) != NULL)
  243.             {
  244.             class = message->Class;
  245.             ReplyMsg(message);
  246.             if (class == CLOSEWINDOW)  done(0); /* exit */
  247.             if (class == MENUPICK) continue; /* ignore menus */
  248.             break;    /* else STOP */
  249.             }
  250.  
  251.         /* Just something to do: Flash a couple of lines */
  252.  
  253.         SetAPen(rpG,color);
  254.         Move(rpG,50L,100L);
  255.         Draw(rpG,80L,100L);
  256.         if (delay) Delay(delay);
  257.  
  258.         SetAPen(rpG,0L);
  259.         Draw(rpG,50L,100L);
  260.         if (delay) Delay(delay);
  261.  
  262.         SetAPen(rpG,color);
  263.         Move(rpG,65L,115L);
  264.         Draw(rpG,65L,85L);
  265.         if (delay) Delay(delay);
  266.  
  267.         SetAPen(rpG,0L);
  268.         Draw(rpG,65L,115L);
  269.         SetAPen(rpG,color);
  270.         if (delay) Delay(delay);
  271.  
  272.         }
  273.  
  274.     on_gads(); /* enable gads */
  275. }
  276.  
  277. /*
  278.  * Sample open routine that does nothing but test an AutoRequest
  279.  */
  280.  
  281. do_open()
  282. {
  283.  
  284.     /* Put in a call to your favorite file requester */
  285.  
  286.     while (1 /* would normally attempt open here */ )
  287.         {
  288.         if (AutoRequest(wG,&opfailtxt,&retrytxt,&cantxt,
  289.             0L,0L,300L,75L)) continue;
  290.         return;
  291.         }
  292.  
  293. }
  294.  
  295.  
  296.  
  297. /*
  298.  * turn back on gadgets after doing it
  299.  */
  300.  
  301. on_gads()
  302. {
  303.     RemoveGList(wG,&GadgetList1,-1L); /* remove all gadgets */
  304.     OffGad(Stop_Gad);   /* disable STOP */
  305.     OnGad(Go_Gad);      /* Enable all the rest */
  306.     redraw_scr();         /* add back gadgets and refresh screen */
  307.     SetMenuStrip(wG,&MenuList1);    /* attach my Menu */
  308. }
  309.  
  310. /*
  311.  * disable gadgets while doing it
  312.  */
  313.  
  314. off_gads()
  315. {
  316.     ClearMenuStrip(wG);      /* disable menus */
  317.     RemoveGList(wG,&GadgetList1,-1L); /* remove all gadgets */
  318.     OnGad(Stop_Gad);     /* enable STOP */
  319.     OffGad(Go_Gad);      /* Disable all the rest */
  320.     redraw_scr();         /* add back gadgets and refresh screen */
  321. }
  322.  
  323. /*
  324.  * Redraw Window
  325.  *  Assumes Gadgets have been removed
  326.  */
  327.  
  328. redraw_scr()
  329. {
  330.     SetAPen(rpG,0L);
  331.     RectFill(rpG,2L,15L,(long)(wG->Width-3),(long)(wG->Height-2));
  332.     AddGList(wG,&GadgetList1,0L,-1L,NULL);
  333.     RefreshGList(&GadgetList1,wG,NULL,-1L);
  334.     do_title();
  335. }
  336.  
  337. /*
  338.  * Put up title line
  339.  */
  340.  
  341. do_title()
  342. {
  343.     long xpos, txtlen;
  344.     if (title[0] == '\0') return;
  345.     txtlen = (long) strlen(title);
  346.     xpos = (wG->Width - 4 - TextLength(rpG,title,txtlen)) / 2 + 2;
  347.     if (xpos < 3 ) xpos = 3;
  348.     SetAPen(rpG,3L);
  349.     Move(rpG,xpos,25L);
  350.     Text(rpG,title,txtlen);
  351. }
  352.  
  353. #ifdef AZTEC_C
  354. _abort()
  355. {
  356.     done(13);
  357. }
  358. #endif
  359.