home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 206.lha / Flist_v1.2 / Sources / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-28  |  14.6 KB  |  522 lines

  1. /**************************************************************************
  2. *     M E N U   E X A M P L E    P R O G R A M   -   JOHN DRAPER
  3. *   (originally it was anyway)
  4. *
  5. *  Modified (mangled) by Steve Berry 7/77/88
  6. *
  7. ***************************************************************************/
  8.  
  9. #include <libraries/arpbase.h>
  10. #include "flist.h"
  11.  
  12. void *OpenLibrary();
  13. struct Window *OpenWindow();
  14. struct IntuiMessage *GetMsg();
  15. struct Window *BuildSysRequest();
  16. extern void Cleanup(),scrprt(),getkey();
  17. extern struct Gadget pg;
  18. extern struct Window *winptr;
  19.  
  20. extern struct IntuiText TRUEtext;
  21. extern struct Window *winptr;
  22. extern struct Screen *scrptr;
  23. extern struct RastPort *rp;
  24. extern struct FileLock *lock, olddir;
  25. extern long line, numfiles, collum, first;
  26. extern char (*fname[MAXDIR])[FCHARS];
  27. extern char (*finfo[MAXDIR])[4];
  28. extern char conline[256];
  29. long top;
  30.  
  31. /***************************************************************************
  32.                      M E N U      D E F I N I T I O N S
  33. ***************************************************************************/
  34.  
  35. #define NUM_MENUS 3
  36.  
  37. /* Copies of this structure will get "stamped" into many more */
  38. /* Allocated later */
  39.  
  40. struct IntuiText generic = {
  41.   0, 1,                        /* Bluepen, Whitepen */
  42.   JAM2, 5,                     /* mode,  LeftEdge */
  43.   0, NL,                       /* Top (to be filled later), Font */
  44.   NL,                          /* Name (to be filled later) */
  45.   NL                           /* Next one */
  46. };
  47.  
  48. /* Menu numbers */
  49.  
  50. #define PROJ_MENU 0
  51. #define DIR_MENU  1
  52. #define SORT_MENU 2
  53.  
  54. /* Menu Widths */
  55.  
  56. #define FIL_WIDTH   80
  57. #define DIR_WIDTH   100
  58. #define SORT_WIDTH  120
  59. #define ITEM_WIDTH  150
  60. #define REQ_WIDTH   100
  61.  
  62. int item_widths[ NUM_MENUS ] = {150,250,150};
  63.  
  64. /* All items in these menus has the following flags set */
  65.  
  66. #define BOX_FILL    ITEMTEXT | ITEMENABLED | HIGHBOX     
  67. #define BLACK_FILL  COMMSEQ | ITEMTEXT | ITEMENABLED | HIGHCOMP
  68. #define HAS_CHECKS  COMMSEQ | ITEMTEXT | ITEMENABLED | HIGHCOMP | CHECKIT
  69.  
  70.                             /** FILE MENU ITEMS  **/
  71. #define NUM_FILE_ITEMS  4
  72. #define DO_ITEM         0
  73. #define ICON_ITEM       1
  74. #define ABOUT_ITEM      2
  75. #define QUIT_ITEM       3
  76.  
  77. struct MenuItem  file_items[NUM_FILE_ITEMS];
  78. struct IntuiText file_names[NUM_FILE_ITEMS];
  79.  
  80. char  *filemenu_names[] = {
  81.    "Execute REXX",
  82.    "Iconify",
  83.    "About",
  84.    "Quit"
  85. };
  86.  
  87. char *filemenu_short[] = {
  88.     "E",
  89.     "I",
  90.     "B",
  91.     "Q"
  92. };
  93.  
  94. struct Menu fmenu = {
  95.   NL,                        /* Pointer to next menu, fill in later */
  96.   0, 0, FIL_WIDTH, 10,       /* LeftEdge, TopEdge, Width, Height */
  97.   MENUENABLED,               /* FLAGS */
  98.   "Project",                 /* Menu name */
  99.   NL                         /* First item structure */
  100. };
  101.  
  102. /************ Next menu ****************/
  103.  
  104.                             /** FILE MENU ITEMS  **/
  105. #define NUM_DIR_ITEMS   7
  106. #define PARENT_ITEM     0
  107. #define CHANGE_ITEM     1
  108. #define ARP_ITEM        2
  109. #define REGET_ITEM      3
  110. #define KILL_ITEM       4
  111. #define RENAME_ITEM     5
  112. #define MAKEDIR_ITEM    6
  113.  
  114. struct MenuItem  dir_items[NUM_DIR_ITEMS];
  115. struct IntuiText dir_names[NUM_DIR_ITEMS];
  116.  
  117. char  *dirmenu_names[] = {
  118.     "  Parent Directory",
  119.     "  Change Directory",
  120.     "  ARP Change to Volume",
  121.     "  Get Directory",
  122.     "  Kill File",
  123.     "  Rename File",
  124.     "  Make Dir"
  125. };
  126.  
  127. char *dirmenu_short[] = {
  128.     "P",
  129.     "D",
  130.     "A",
  131.     "G",
  132.     "K",
  133.     "R",
  134.     "N"
  135. };
  136.  
  137. struct Menu dmenu = {
  138.   NL,                        /* Pointer to next menu, fill in later */
  139.   100, 0, DIR_WIDTH, 10,     /* LeftEdge, TopEdge, Width, Height */
  140.   MENUENABLED,               /* FLAGS */
  141.   "Directory",               /* Menu name */
  142.   NL                         /* First item structure */
  143. };
  144.  
  145. /************ Next menu ****************/
  146.  
  147.                             /** FILE MENU ITEMS  **/
  148. #define NUM_SORT_ITEMS  5
  149. #define ALPHABET_ITEM   0
  150. #define SIZE_ITEM       1
  151. #define TIME_ITEM       2
  152. #define PATTERN_ITEM    3
  153. #define DAY_ITEM        4
  154.  
  155. struct MenuItem  sort_items[NUM_SORT_ITEMS];
  156. struct IntuiText sort_names[NUM_SORT_ITEMS];
  157.  
  158. char  *sortmenu_names[] = {
  159.     "Alphabeticly",
  160.     "By Size",
  161.     "By Time",
  162.     "By Pattern",
  163.     "By Day"
  164. };
  165.  
  166. char *sortmenu_short[] = {
  167.     "S",
  168.     "Z",
  169.     "T",
  170.     "O",
  171.     "Y"
  172. };
  173.  
  174. struct Menu smenu = {
  175.   NL,                        /* Pointer to next menu, fill in later */
  176.   200, 0, SORT_WIDTH, 10,    /* LeftEdge, TopEdge, Width, Height */
  177.   MENUENABLED,               /* FLAGS */
  178.   "Sort List",           /* Menu name */
  179.   NL                         /* First item structure */
  180. };
  181.  
  182. /***************************************************************************
  183.                    AUTO REQUEST INTUITEXT STRUCTURES
  184. ***************************************************************************/
  185.  
  186. struct IntuiText Auto3 = {
  187.    REDP,  WHTP,
  188.    JAM1,  15,
  189.    35,    NL,
  190.    "Freely Redistributable for noncomercial purposes. ", NULL
  191. };
  192.  
  193. struct IntuiText Auto2 = {
  194.    REDP,  WHTP,
  195.    JAM1,  15,
  196.    17,    NL,
  197.    " Inspired by the PD Flist on IBM mainframes ", &Auto3
  198. };
  199.  
  200. struct IntuiText AboutText = {
  201.    REDP,  WHTP,
  202.    JAM1,  20,
  203.    5,    NL,
  204.    " Flist was written by Steve (Raz) Berry ", &Auto2  
  205. };
  206.  
  207. /***************************************************************************
  208.                   M A I N     P R O G R A M     M O D U L E
  209. ***************************************************************************/
  210. long mousey, mousex;
  211.  
  212. menu()
  213. {
  214.     ULONG    class;     /* Message class from Intuition                  */
  215.     USHORT   code;      /* Menu code info from Intuition                 */
  216.     struct MenuItem *item;
  217.     long     gadown=FALSE;
  218.     struct IntuiMessage *message;
  219.     struct Gadget *igad;
  220.     register short  prevy=0;
  221.     register int i,temp;
  222.     char buf[256];
  223.  
  224.     mousey = 0;
  225.     mousex = 0;
  226.  
  227.     fmenu.FirstItem = &file_items[0];
  228.     fmenu.NextMenu = &dmenu;
  229.     dmenu.NextMenu = &smenu;
  230.  
  231.     NewMenu( &fmenu, filemenu_names, filemenu_short, file_items, file_names,
  232.             NUM_FILE_ITEMS, item_widths[0], BLACK_FILL);
  233.  
  234.     dmenu.FirstItem = &dir_items[0];
  235.  
  236.     NewMenu( &dmenu, dirmenu_names, dirmenu_short, dir_items, dir_names,
  237.             NUM_DIR_ITEMS, item_widths[1], HAS_CHECKS);
  238.  
  239.     smenu.FirstItem = &sort_items[0];
  240.  
  241.     NewMenu( &smenu, sortmenu_names, sortmenu_short, sort_items, sort_names,
  242.             NUM_SORT_ITEMS, item_widths[2], BLACK_FILL);
  243.  
  244. /*    sort_items[0].MutualExclude = 0xfffe; */
  245.  
  246.     top = 0;     /* this is the pointer to the first file on the screen */
  247.   
  248.     SetMenuStrip(winptr, &fmenu);      /* Set up the menu here */
  249.  
  250.     ShowTitle(scrptr,TRUE);      /* Do show the screen Title. */
  251.  
  252.     sort(0);                     /* sort the list alphabetically */
  253.     refresh();
  254.     drawcur();
  255.  
  256.     for (;;)
  257.     {
  258.       if ((message = (struct IntuiMessage *)GetMsg(winptr->UserPort)) == 0L)  {
  259.           Wait(1L<<winptr->UserPort->mp_SigBit);
  260.           continue;
  261.       }
  262.  
  263.         class = message->Class;
  264.         code = message->Code;
  265.         igad = (struct Gadget *)message->IAddress;
  266.         mousey = message->MouseY;
  267.         mousex = message->MouseX;
  268.  
  269.         switch (class) {
  270.  
  271.             case RAWKEY:
  272.                 getkey(message);    /* message is replied to in routine */
  273.                 break;          
  274.  
  275.             case MOUSEMOVE: 
  276.                 ReplyMsg(message);
  277.                 if(gadown){
  278. #ifdef KILLEXTRA
  279.                         /* get rid of any extra messages */
  280.  
  281.                         while((message = (struct IntuiMessage *)GetMsg(winptr->UserPort)) != 0L) {
  282.                                 mousey = message->MouseY;
  283.                                 ReplyMsg(message);
  284.                                 if((prevy<mousey)&&(line<numfiles))
  285.                                 top++;
  286.                                 if((prevy>mousey)&&(top>0))
  287.                                         top--;
  288.                                 prevy = mousey;  
  289.                         } 
  290. #endif
  291.                         if((prevy<mousey)&&(line<numfiles-1)){
  292.                                         line++;
  293.                                         top++;
  294.                         }
  295.                         if((prevy>mousey)&&(top>0)){
  296.                                 if (line>0)
  297.                                         line--;
  298.                                 top--;
  299.                         }
  300.                         prevy = mousey;    
  301.                         temp = numfiles-top;
  302.                         scrprt(temp,top);
  303.                 }
  304.                 break;
  305.                 
  306.         case GADGETUP: 
  307.                 ReplyMsg(message);
  308.                 drawline(collum,TRUE);
  309.                 drawcur();
  310.                 gadown = FALSE;
  311.                 break;
  312.  
  313.         case GADGETDOWN:
  314.                 ReplyMsg(message);
  315.                 blankcur();
  316.                 switch ( i = igad->GadgetID) {
  317.                     case FGAD: /* the prop gadget here... */
  318.                         gadown = TRUE;
  319.                         strcpy(buf,conline);
  320.                         conline[first] = 0;
  321.                         drawline(collum,TRUE);
  322.                         strcpy(conline,buf);
  323.                         drawline(collum,TRUE);
  324.                         break;
  325.                 }
  326.                 break;
  327.  
  328.         case CLOSEWINDOW: 
  329.                 ReplyMsg(message);
  330.                 Cleanup();
  331.                 exit(0);
  332.                 break;
  333.  
  334.         case MENUPICK:
  335.                 ReplyMsg(message);
  336.                 blankcur();
  337.                 while (code != MENUNULL) {
  338. #ifdef DEBUG
  339.     sprintf(buf,"Code is %ld Menunum is %ld",code, MENUNUM(code));
  340.     auto_req(buf);
  341. #endif
  342.                     domenu(MENUNUM(code), ITEMNUM(code),
  343.                         SUBNUM(code));
  344.                     item = ItemAddress(&fmenu, MENUNUM(code));
  345.                     if (item->NextSelect == code)
  346.                         break;
  347.                     code = item->NextSelect;
  348.                     item->NextSelect = MENUNULL;
  349.                 }
  350.                 drawcur();
  351.                 break;
  352.  
  353.         case MOUSEBUTTONS: 
  354.                 ReplyMsg(message);
  355.                 break;
  356.  
  357.         }   /* Case */
  358.    }  /* for */
  359. }   /* End of Main */
  360.  
  361. /***************************************************************************
  362.                    C R E A T E    A    N E W    M E N U
  363. ***************************************************************************/
  364. NewMenu ( menu, item_names,  item_short, menu_items, menu_text, 
  365.         num_items, Mwidth, flag)
  366.  
  367. struct Menu      *menu;           /* Menu structure                       */
  368. char             *item_names[];   /* Pointer to array of item names       */
  369. char             *item_short[];   /* Pointer to array of shortcut char    */
  370. struct MenuItem  menu_items[];    /* pointer to array of structures       */
  371. struct IntuiText menu_text[];     /* Pointer to array of text structures  */
  372. int               num_items;      /* Number of items                      */
  373. int               Mwidth;         /* Menu Width */
  374. long                flag;         /* Special Item flag for ALL items */
  375. {
  376.     int i;
  377.     int height = 0;
  378.  
  379.     for (i=0; i< num_items; i++) {
  380.  
  381.         menu_text[i] = generic;              /* stamp generic template */
  382.         menu_text[i].IText = item_names[i];          /* mv string ptrs */
  383.         menu_items[i].NextItem = &menu_items[i+1];  /* Lnk to nxt item */
  384.         menu_items[i].TopEdge = 10 * i;            /* Top rect of item */
  385.         menu_items[i].LeftEdge = 2;
  386.         menu_items[i].Height = 8;
  387.         menu_items[i].ItemFill = (APTR)&menu_text[i];
  388.         menu_items[i].Width = Mwidth;
  389.         menu_items[i].MutualExclude = 0;
  390.         menu_items[i].Command = item_short[i][0];
  391.         menu_items[i].Flags = (USHORT)flag;
  392.         menu_items[i].SubItem = NULL;
  393.         menu_items[i].NextSelect = MENUNULL;
  394.         height += 10;
  395.     }
  396.     menu_items[num_items-1].NextItem = NULL;
  397.     menu->Height = height;
  398. }
  399.  
  400. /* DO THE MENU SELECTIONS */
  401.  
  402. domenu(menu, item, subitem)
  403. int  menu, item, subitem;
  404. {
  405.     char buf[256];
  406.     int i;
  407.  
  408.     switch (menu) {
  409.  
  410.         case PROJ_MENU: 
  411.             switch (item) {
  412.  
  413.                case  DO_ITEM: 
  414.                     dorexx();
  415.                     break;
  416.  
  417.                case  ICON_ITEM: 
  418.                     tticon();
  419.                     break;
  420.  
  421.                case  QUIT_ITEM:     
  422.                     if(scrptr->FirstWindow->NextWindow != NULL) 
  423.                         auto_req("Kill all other Applications first");
  424.                     else {
  425.                         Cleanup();
  426.                         exit(0);
  427.                     }
  428.                     break;
  429.  
  430.                case  ABOUT_ITEM: 
  431.                     AutoRequest(winptr, &AboutText, &TRUEtext, &TRUEtext, 
  432.                                 0L, 0L, 450L, 80L );
  433.                     break;
  434.             } 
  435.             break;
  436.  
  437.         case DIR_MENU:
  438.  
  439.             for (i=0;i<NUM_DIR_ITEMS;i++)
  440.                 dir_items[i].Flags = HAS_CHECKS;
  441.  
  442.             switch (item) {
  443.  
  444.                 case PARENT_ITEM:
  445.                     parent();
  446.                     refresh();
  447.                     break;
  448.  
  449.                 case CHANGE_ITEM:
  450.                     if (*finfo[line][0] < 0){
  451.                         sprintf(buf,"%s Not a directory",fname[line]);
  452.                         auto_req(buf);
  453.                     }else {
  454.                         changedir(fname[line]);
  455.                         refresh();
  456.                     }
  457.                     break;
  458.  
  459.                 case ARP_ITEM:
  460.                     if (Wbargs() == 0){
  461.                         UnLock(olddir);
  462.                         refresh();
  463.                     }
  464.                     break;
  465.  
  466.                 case REGET_ITEM:
  467.                     getdir();
  468.                     refresh();
  469.                     break;
  470.  
  471.                 case KILL_ITEM:
  472.                     kill(fname[line]);
  473.                     refresh();
  474.                     break;
  475.  
  476.                 case RENAME_ITEM:
  477.                     rename_file(fname[line]);
  478.                     refresh();
  479.                     break;
  480.  
  481.                 case MAKEDIR_ITEM:
  482.                     make_dir();
  483.                     refresh();
  484.                     break;
  485.             }
  486.             break;
  487.  
  488.         case SORT_MENU:
  489.         
  490.             switch (item) {
  491.  
  492.                 case ALPHABET_ITEM:
  493.                     sort(0);
  494.                     refresh();
  495.                     break;
  496.  
  497.                 case SIZE_ITEM:
  498.                     sort(1);
  499.                     refresh();
  500.                     break;
  501.  
  502.                 case TIME_ITEM:
  503.                     sort(2);
  504.                     refresh();
  505.                     break;
  506.  
  507.                 case PATTERN_ITEM:
  508.                     sort(3);
  509.                     refresh();
  510.                     break;
  511.  
  512.                 case DAY_ITEM:
  513.                     sort(4);
  514.                     refresh();
  515.                     break;
  516.  
  517.             }
  518.             break;
  519.     }
  520. }
  521.  
  522.