home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 337.lha / Helper_v.07 / gads.mod < prev    next >
Encoding:
Modula Implementation  |  1990-01-26  |  19.0 KB  |  545 lines

  1. IMPLEMENTATION MODULE Gads;
  2.  
  3. (*======================================================================*)
  4. (*                        Helper version v0.07                          *)
  5. (*======================================================================*)
  6. (*           Copyright © 1989 Mike Cargal, All Rights Reserved          *)
  7. (*======================================================================*)
  8. (*      Version: 0.07           Author : Mike Cargal                    *)
  9. (*      Date   : 20-Jun-89      Changes: Original                       *)
  10. (*======================================================================*)
  11.  
  12. FROM SYSTEM         IMPORT ADR, STRPTR, BYTE, ADDRESS, TSIZE;
  13. FROM EasyWindows    IMPORT CreateWindow;
  14. FROM EasyIDCMP      IMPORT ProcTable, ProcessEvents;
  15. FROM EasyGadgets    IMPORT currentList, AddBorders, AddDropShadow,
  16.                            listFailed, DisposeList, StartList,
  17.                            AddBoolGadget, nextShadowFrontPen,
  18.                            nextIntuiFrontPen, currentGadget,
  19.                            AddStrGadget, nextBoolActivation,
  20.                            nextID, nextIntuiDrawMode, nextIntuiBackPen,
  21.                            nextFlags, nextIntuiTopEdge;
  22. FROM Intuition      IMPORT WindowFlags, IDCMPFlags, WindowPtr,
  23.                            WindowFlagSet, IDCMPFlagSet, IntuiMessage,
  24.                            CloseWindow, GadgetPtr, ActivateWindow,
  25.                            RefreshGadgets, ActivationFlags,
  26.                            RefreshGList, MoveWindow, Screen,
  27.                            GadgetFlagSet, GadgetFlags, Gadget,
  28.                            ActivationFlagSet, ActivateGadget,
  29.                            WBenchScreen, GetScreenData;
  30. FROM DOS            IMPORT Open, Close, Read, Write, FileLock,
  31.                            FileHandle, ModeReadWrite, ModeNewFile;
  32. FROM ArpMisc        IMPORT FileRequester, FileRequest, FRFunction,
  33.                            FRFuncFlags, FRFuncFlagSet, 
  34.                            CloseWindowSafely, ArpAllocFreq,
  35.                            FileRequesterPtr;
  36. FROM ArpDOS         IMPORT TackOn;
  37. FROM Rasters        IMPORT Jam2;                           
  38. FROM Strings        IMPORT AppendSubStr, AssignStr, CompareStr, Equal;
  39.                 (*  IMPORT Debug; *)
  40. (*$O-,$R-*)                   
  41. TYPE 
  42.  
  43.   Configuration =
  44.     RECORD
  45.       Vsn      : ARRAY [0..13] OF CHAR; (* Version ID *)
  46.       ScreenX,
  47.       ScreenY  : INTEGER;               (* Helper window coordinates *)
  48.       Gads     : GadArray;              (* Helper Gadget configuration *)
  49.     END;  
  50.     
  51. VAR
  52.   wp           : WindowPtr;
  53.   Config       : Configuration;
  54.   GadList      : GadgetPtr;
  55.   fr           : FileRequesterPtr;
  56.    
  57. (*******************************************************************)
  58.  
  59. PROCEDURE GetFile(text, Dir : STRPTR) : STRPTR;
  60.  
  61.   (*-------------------------------------------------------------\
  62.    |                                                             |
  63.    |  Use Arp File requester to get file name.                   |
  64.    |       text - Title to appear at top of requester            |
  65.    |       Dir  - Directory in which requester should initiate.  |
  66.    |              NIL = Use Current Directory of Process.        |
  67.    |                                                             |
  68.    |    Returns - STRPTR to full path name of requested file.    |
  69.    |                                                             |
  70.    \-------------------------------------------------------------*)
  71.  
  72. VAR
  73.   FileName : STRPTR;
  74.  
  75. BEGIN
  76.  
  77.   WITH fr^ DO
  78.     frHail      := text;
  79.     frFile^     := ''; 
  80.     frWindow    := WindowPtr(NIL);
  81.     frFuncFlags := FRFuncFlagSet{DoColor};
  82.     frFunction  := FRFunction(NIL);
  83.     AssignStr(frDir^,Dir^);
  84.   END; 
  85.  
  86.   FileName := FileRequest(fr);
  87.   TackOn(fr^.frDir,FileName);
  88.   RETURN(fr^.frDir);
  89.     
  90. END GetFile;
  91.  
  92. (*******************************************************************)
  93.  
  94. (*$L+*)
  95. PROCEDURE HandleCloseWindow(VAR im : IntuiMessage) : INTEGER;
  96.  
  97.   (*-------------------------------------------------------------\
  98.    |                                                             |
  99.    |  Handle Close Window IDCMP message for EasyIDCMP.           |
  100.    |                                                             |
  101.    |    I have nothing special to do, so just return non 0       |
  102.    |      to let EasyIDCMP know I'm through.                     |
  103.    |                                                             |
  104.    \-------------------------------------------------------------*)
  105.  
  106. BEGIN
  107.   RETURN(1)
  108. END HandleCloseWindow;
  109. (*$L-*) 
  110.  
  111. (*******************************************************************)
  112.  
  113. PROCEDURE HandleGadgetUp(VAR im : IntuiMessage;
  114.                              gp : GadgetPtr)       : INTEGER;
  115.  
  116.   (*-------------------------------------------------------------\
  117.    |                                                             |
  118.    |  Gadgets are numbered:    0  1  2                           |
  119.    |                           3  4  5                           |
  120.    |                           6  7  8...                        |
  121.    |                                                             |
  122.    |   Therefore line is GadgetID DIV 3 and the type Gadget is   |
  123.    |     GadgetID MOD 3                                          |
  124.    |                                                             |
  125.    |     0 - String gadget for button label.  Change Gads.Label  |
  126.    |           and activate command string gadget.               |
  127.    |                                                             |
  128.    |     1 - String gadget for command to execute.  Change       |
  129.    |           Gads.Command.                                     |
  130.    |                                                             |
  131.    |     2 - Append File boolean gadget. Set Gads.AppendFile     |
  132.    |           according to Gadget selected flag.                |
  133.    |                                                             |
  134.    |                                                             |
  135.    \-------------------------------------------------------------*)
  136.  
  137. VAR
  138.   Activated : BOOLEAN;
  139.   i         : CARDINAL;
  140.   
  141. BEGIN
  142.  
  143.   i := gp^.GadgetID DIV 3;
  144.   
  145.   CASE (gp^.GadgetID MOD 3) OF
  146.     0 : AssignStr(Config.Gads[i].Label,gp^.SpecialInfoS^.Buffer^);
  147.         Activated := ActivateGadget(gp^.NextGadget,wp,NIL) 
  148.     
  149.   | 1 : AssignStr(Config.Gads[i].Command,gp^.SpecialInfoS^.Buffer^);
  150.         AppendSubStr(Config.Gads[i].Command," ")
  151.  
  152.   | 2 : Config.Gads[i].AppendFile := Selected IN gp^.Flags;
  153.  
  154.   END;    
  155.  
  156.   RETURN(0);
  157.  
  158. END HandleGadgetUp;
  159.  
  160. (*******************************************************************)
  161.  
  162. PROCEDURE AddDefineGadgets();
  163.  
  164.   (*-------------------------------------------------------------\
  165.    |                                                             |
  166.    |  Set up gadgets to allow user definition of Helper buttons  |
  167.    |    and their responses.                                     |
  168.    |                                                             |
  169.    \-------------------------------------------------------------*)
  170.  
  171. VAR
  172.   i : INTEGER;
  173.  
  174. BEGIN
  175.  
  176.   StartList;
  177.  
  178. (*
  179.   nextIntuiFrontPen  := 3;
  180.   nextShadowFrontPen := 1;
  181. *)
  182.   nextIntuiTopEdge   := 0;
  183.   nextBoolActivation := ActivationFlagSet{RelVerify,ToggleSelect};
  184.  
  185.   FOR i := 0 TO 11 DO
  186.     EXCL(nextFlags,Selected);
  187.     AddStrGadget(10,i*12+15,60,8,6,Config.Gads[i].Label);
  188.     AddBorders;
  189.     AddStrGadget(85,i*12+15,285,8,100,Config.Gads[i].Command);
  190.     AddBorders;
  191.     
  192.     IF Config.Gads[i].AppendFile THEN
  193.       INCL(nextFlags,Selected)
  194.     ELSE
  195.       EXCL(nextFlags,Selected)
  196.     END;
  197.     
  198.     AddBoolGadget(385,i*12+15,100,8,"Append File");
  199.     AddBorders;
  200.     
  201.   END;
  202.   
  203. END AddDefineGadgets;
  204.  
  205. (*******************************************************************)
  206.  
  207. PROCEDURE AddHelperGadgets();
  208.  
  209.   (*-------------------------------------------------------------\
  210.    |                                                             |
  211.    |  Add Helper button gadgets to Helper window.                |
  212.    |                                                             |
  213.    \-------------------------------------------------------------*)
  214.                     
  215. VAR  
  216.   i : INTEGER;
  217.                     
  218. BEGIN
  219.  
  220.   StartList;
  221.   nextIntuiDrawMode  := Jam2;
  222.   nextIntuiBackPen   := 0;
  223. (*
  224.   nextIntuiFrontPen  := 3;
  225.   nextShadowFrontPen := 1;
  226. *)
  227.   FOR i := 0 TO 11 DO
  228.     AddBoolGadget((i MOD 3)*70+8,
  229.                   (i DIV 3)*20+16,
  230.                   55, 14,
  231.                   Config.Gads[i].Label); 
  232.     AddBorders;
  233.     AddDropShadow;
  234.   END;
  235.  
  236. END AddHelperGadgets;
  237.  
  238. (*******************************************************************)
  239.  
  240. PROCEDURE LoadGads(wp : WindowPtr);
  241.  
  242.   (*-------------------------------------------------------------\
  243.    |                                                             |
  244.    |  Present user with File requester to select configuration   |
  245.    |   file, and then load user configuration from selected      |
  246.    |   file.  Configuration file is verified against version     |
  247.    |   number of Helper so that non-configuration files and      |
  248.    |   incompatible configuration files will be rejected.        |
  249.    |                                                             |
  250.    |  Return success or failure of Configuration load.           |
  251.    |                                                             |
  252.    |  NOTE: file requester goes initially to S: since            |
  253.    |   requesting configuration file.                            |
  254.    |                                                             |
  255.    \-------------------------------------------------------------*)
  256.  
  257. VAR 
  258.   TempConfig     : Configuration;  
  259.   MyFileHandle   : FileHandle;
  260.   Length         : LONGINT;
  261.  
  262. BEGIN
  263.  
  264.   MyFileHandle := Open(GetFile(ADR(" <*> Load Helper Buttons <*> "),
  265.                                ADR("S:")),
  266.                        ModeReadWrite);
  267.   IF MyFileHandle # NIL THEN 
  268.     Length := Read(MyFileHandle,ADR(TempConfig),TSIZE(Configuration));
  269.     Close(MyFileHandle);
  270.  
  271.     IF CompareStr(Version,TempConfig.Vsn) = Equal THEN
  272.       Config := TempConfig;
  273.       RefreshGadgets(GadList,wp,NIL);
  274.       MoveWindow(wp,
  275.                  Config.ScreenX-wp^.LeftEdge,
  276.                  Config.ScreenY-wp^.TopEdge)
  277.     END;
  278.   END;
  279.   
  280. END LoadGads;
  281.  
  282. (*******************************************************************)
  283.  
  284. PROCEDURE SaveGads(wp : WindowPtr);
  285.  
  286.   (*-------------------------------------------------------------\
  287.    |                                                             |
  288.    |  Present user with File requester (inititially in S:) to    |
  289.    |   select configuration file. Save current configuration     |
  290.    |   to selected file.                                         |
  291.    |                                                             |
  292.    \-------------------------------------------------------------*)
  293.  
  294. VAR 
  295.   MyFileHandle : FileHandle;
  296.   Length       : LONGINT;
  297.  
  298. BEGIN
  299.  
  300.   Config.ScreenX := wp^.LeftEdge;
  301.   Config.ScreenY := wp^.TopEdge;
  302.  
  303.   MyFileHandle := Open(GetFile(ADR(" <*> Save Helper Buttons <*> "),
  304.                                ADR("S:")),
  305.                        ModeNewFile);
  306.   IF MyFileHandle # NIL THEN 
  307.     Length := Write(MyFileHandle,ADR(Config),TSIZE(Configuration));
  308.     Close(MyFileHandle)
  309.   END;
  310.   
  311. END SaveGads;
  312.  
  313. (*******************************************************************)
  314.  
  315. PROCEDURE SaveDefaults(wp : WindowPtr);
  316.  
  317.   (*-------------------------------------------------------------\
  318.    |                                                             |
  319.    |  Save current configuration to default configuration file   |
  320.    |   (S:Helper.cnf).  This file will be read automatically     |
  321.    |   when Helper is started up to obtain users default         |
  322.    |   configuration.                                            |
  323.    |                                                             |
  324.    \-------------------------------------------------------------*)
  325.  
  326. VAR 
  327.   MyFileHandle : FileHandle;
  328.   Length       : LONGINT;
  329.  
  330. BEGIN
  331.  
  332.   Config.ScreenX := wp^.LeftEdge;
  333.   Config.ScreenY := wp^.TopEdge;
  334.   MyFileHandle := Open(DefConfig,ModeNewFile);
  335.   IF MyFileHandle # NIL THEN 
  336.     Length := Write(MyFileHandle,ADR(Config),TSIZE(Configuration));
  337.     Close(MyFileHandle)
  338.   END;
  339.   
  340. END SaveDefaults;
  341.  
  342. (*******************************************************************)
  343.  
  344. PROCEDURE ChangeGads(Helperwp : WindowPtr);
  345.  
  346.   (*-------------------------------------------------------------\
  347.    |                                                             |
  348.    |  Open Window with Helper button definition gadgets.         |
  349.    |                                                             |
  350.    |  Use EasyIDCMP to respond to user gadget selection and      |
  351.    |    modify user configuration.                               |
  352.    |                                                             |
  353.    \-------------------------------------------------------------*)
  354.  
  355. VAR 
  356.   MyProcTable  : ProcTable;
  357.   dummy        : INTEGER;
  358.  
  359. BEGIN
  360.  
  361.   AddDefineGadgets;
  362.     
  363.   IF NOT listFailed THEN 
  364.     wp := CreateWindow(70,15,500,165,
  365.                        ' <*> Helper Define <*>  ©1989, Mike Cargal',
  366.                        IDCMPFlagSet{GadgetUp,
  367.                                     CloseWindowFlag},
  368.                        WindowFlagSet{WindowDrag,
  369.                                      WindowDepth,
  370.                                      WindowClose,
  371.                                      WindowActive},
  372.                        NIL,currentList);
  373.     IF wp # NIL THEN
  374.       WITH MyProcTable DO
  375.         WaitForEvent := TRUE;
  376.         CloseWindow  := HandleCloseWindow;
  377.         GadgetUp     := HandleGadgetUp;
  378.       END;   
  379.       dummy := ProcessEvents(wp,MyProcTable);
  380.       CloseWindowSafely(wp,NIL);
  381.       DisposeList(currentList);
  382.       RefreshGadgets(GadList,Helperwp,NIL)
  383.     END;
  384.   END;
  385. END ChangeGads;
  386.  
  387. (*******************************************************************)
  388.  
  389. PROCEDURE AddGad(i       : CARDINAL;
  390.                  cmd     : ARRAY OF CHAR;
  391.                  lb      : ARRAY OF CHAR;
  392.                  append  : BOOLEAN);
  393.  
  394.   (*-------------------------------------------------------------\
  395.    |                                                             |
  396.    |   Add Helper "default defaults" to Gads structure.          |
  397.    |                                                             |
  398.    |      i      - Array member                                  |
  399.    |      cmd    - command to issue in response to button        |
  400.    |      lb     - Label to place on Helper button               |
  401.    |      append - BOOLEAN indicator of whether or not           |
  402.    |                 to present file requester to append         |
  403.    |                 file name                                   |
  404.    |                                                             |
  405.    \-------------------------------------------------------------*)
  406.                  
  407. BEGIN
  408.  
  409.   AssignStr(Config.Gads[i].Command,cmd);
  410.   AssignStr(Config.Gads[i].Label,lb);
  411.   Config.Gads[i].AppendFile := append;
  412.  
  413. END AddGad;
  414.  
  415. (*******************************************************************)
  416.  
  417. PROCEDURE OpenHelperWindow() : WindowPtr;
  418.  
  419.   (*-------------------------------------------------------------\
  420.    |                                                             |
  421.    |   Open Helper Gadget window returning pointer to main       |
  422.    |     main program.                                           |
  423.    |                                                             |
  424.    |   Set up gadgets, Open window AND return WindowPtr if       |
  425.    |     successful.                                             |
  426.    |                                                             |
  427.    \-------------------------------------------------------------*)
  428.  
  429. VAR
  430.   wp         : WindowPtr;
  431.   ScreenData : Screen;  
  432.  
  433. BEGIN
  434.  
  435.   (* Verify that window position from config is valid for current screen *)  
  436.  
  437.   IF GetScreenData(ADR(ScreenData),TSIZE(Screen),WBenchScreen,NIL) THEN 
  438.     IF Config.ScreenX + 220 > ScreenData.Width THEN 
  439.       Config.ScreenX := ScreenData.Width - 220 
  440.     END;
  441.     IF Config.ScreenY + 100 > ScreenData.Height THEN
  442.       Config.ScreenY := ScreenData.Height - 100
  443.     END;
  444.   END;      
  445.  
  446.   wp := NIL;
  447.   AddHelperGadgets;
  448.   IF NOT listFailed THEN 
  449.     GadList := currentList;
  450.  
  451.     wp := CreateWindow(Config.ScreenX,Config.ScreenY,220,100,
  452.                        Version,
  453.                        IDCMPFlagSet{GadgetUp,
  454.                                     CloseWindowFlag,
  455.                                     MenuPick},
  456.                        WindowFlagSet{WindowDrag,
  457.                                      WindowDepth,
  458.                                      WindowClose},
  459.                        NIL,GadList);                   
  460.   END;
  461.   RETURN(wp)
  462. END OpenHelperWindow;
  463.  
  464. (*******************************************************************)
  465.  
  466. PROCEDURE CloseHelperWindow(wp : WindowPtr);
  467.   (*-------------------------------------------------------------\
  468.    |                                                             |
  469.    |   Close Helper gadget window and free memory allocated      |
  470.    |     for gadgets.                                            |
  471.    |                                                             |
  472.    \-------------------------------------------------------------*)
  473.  
  474. BEGIN
  475.  
  476.   CloseWindowSafely(wp,NIL);
  477.   DisposeList(GadList);
  478.  
  479. END CloseHelperWindow;
  480.  
  481. (*******************************************************************)
  482.  
  483. PROCEDURE InitGads() : GadPtr;
  484.  
  485.   (*-------------------------------------------------------------\
  486.    |                                                             |
  487.    |  Attempt to open S:Helper.cnf to read in user's default     |
  488.    |    configuration.                                           |
  489.    |                                                             |
  490.    |  If default configuration file is not located, then         |
  491.    |    assign "default default" values to Helper buttons        |
  492.    |                                                             |
  493.    \-------------------------------------------------------------*)
  494.  
  495. CONST
  496.   AddFile = TRUE;
  497.   NoFile  = FALSE;
  498.  
  499. VAR 
  500.   MyFileHandle : FileHandle;
  501.   Length       : LONGINT;
  502.   TempConfig   : Configuration;  
  503.   HaveConfig   : BOOLEAN;
  504.     
  505. BEGIN 
  506.   
  507.   HaveConfig := FALSE; 
  508.   MyFileHandle := Open(DefConfig,ModeReadWrite);
  509.   IF MyFileHandle # NIL THEN 
  510.     Length := Read(MyFileHandle,ADR(TempConfig),TSIZE(Configuration));
  511.     IF CompareStr(TempConfig.Vsn,Version) = Equal THEN
  512.       Config := TempConfig;
  513.       HaveConfig := TRUE;
  514.     END;
  515.     Close(MyFileHandle);
  516.   END;
  517.   IF NOT HaveConfig THEN
  518.     Config.Vsn     := Version;
  519.     Config.ScreenX := 420;
  520.     Config.ScreenY := 11;
  521.     AddGad( 0,'CD '       ,'  CD  ',AddFile);
  522.     AddGad( 1,'CD / '     ,'Parent',NoFile );
  523.     AddGad( 2,'Dir '      ,' Dir  ',NoFile );
  524.     AddGad( 3,'Run '      ,' Run  ',AddFile);
  525.     AddGad( 4,'Ed '       ,' Edit ',AddFile);
  526.     AddGad( 5,'Avail '    ,'Avail ',NoFile );
  527.     AddGad( 6,'Delete '   ,'Delete',AddFile);
  528.     AddGad( 7,'Type '     ,' Type ',AddFile);
  529.     AddGad( 8,'Type >PRT:','Print ',AddFile);
  530.     AddGad( 9,''          ,' Exec ',AddFile);
  531.     AddGad(10,'Date '     ,' Date ',NoFile );
  532.     AddGad(11,'EndCLI '   ,'EndCLI',NoFile );
  533.   END;
  534.   RETURN(ADR(Config.Gads));
  535. END InitGads;
  536.  
  537. (*********************************************************************)
  538.  
  539. BEGIN 
  540.  
  541.   fr        := ArpAllocFreq();
  542.   DefConfig := ADR("s:helper.cnf");
  543.  
  544. END Gads.
  545.