home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SCRPTEXM.PAK / SPPMAN.SPP < prev    next >
Encoding:
Text File  |  1997-05-06  |  9.2 KB  |  358 lines

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // SPPMAN.SPP: Script Manager. Allows you to specify scripts for
  6. //   autoloading, and add scripts to IDE menus. Provides a script directory
  7. //   showing script names, descriptions, and autoload and load statuses.
  8. //   The directory window lets you load and unload scripts, edit them in
  9. //   the specified editor, and edit the Script Manager data file.
  10. //
  11. // USE: Set values in sppman.cfg, and add the scripts you want managed to
  12. //   the Script Manager data file, sppman.dat. Load the Script Manager to
  13. //   load any scripts marked for autoloading, and to add scripts to IDE
  14. //   menus. To display the Script Manager directory, run sppdir.spp. Click
  15. //   a script for a menu of commands. To autoload sppman.spp on startup,
  16. //   see the online Help topic "Loading scripts."
  17. //
  18. // FILES: SPPMAN.DAT, SPPMAN.CFG, SPPDIR.SPP, MSG.SPP, FILE.SPP, MISC.SPP
  19. //
  20. // NOTES: All spp*.* files must reside in the same directory.
  21. //--------------------------------------------------------------------------
  22. print typeid(module());
  23.  
  24. //
  25. // IDE imports.
  26. //
  27. import IDE;
  28. import scriptEngine;
  29.  
  30. //
  31. // Load support module(s).
  32. //
  33. if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
  34. if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
  35. if (!scriptEngine.IsLoaded("misc")) scriptEngine.Load("misc");
  36.  
  37. //
  38. // Constants for the script directory list window.
  39. //
  40. #define MENU_CAPT  "Script Manager directory   (Click a script for commands)"
  41. #define MENU_X     30
  42. #define MENU_Y     10
  43. #define MENU_H     385
  44. #define MENU_W     675
  45.  
  46. //
  47. // Constants for the script directory local menu.
  48. //
  49. #define LOAD         "Load"
  50. #define EDIT         "Edit"
  51. #define UNLOAD       "Unload"
  52. #define EDIT_DAT     "Edit Script Manager data file"
  53. #define CANCEL       "Cancel"
  54. #define CLOSE        "Close directory"
  55.  
  56. //
  57. // Constants for making columnar data in a list window.
  58. //
  59. #define NAME_LEN     18
  60. #define DESCR_LEN    35
  61.  
  62. sTmp = new String();  // General purpose string object.
  63. Msg = new TMsg();     // Meassage object.
  64.  
  65. ModuleDir = GetModuleDir(typeid(module()));  // Directory of this script.
  66.  
  67. Directory = new ListWindow(MENU_X, MENU_Y, MENU_H, MENU_W, MENU_CAPT,
  68.                            FALSE, FALSE, NULL);
  69.  
  70. import "kernel32.dll" {
  71.   int    WinExec    (char *, int);
  72. }
  73.  
  74. export SppMan;  // Script Manager object.
  75.  
  76. sppman()
  77. {
  78.   //
  79.   // Instantiate a script manager object and set up scripts. This
  80.   // autoloads specified scripts and adds them to menus. SPPDIR.SPP
  81.   // runs the script directory.
  82.   //
  83.   SppMan = new TSppMan();
  84.   SppMan.SetUpScripts();
  85. }
  86.  
  87. //
  88. // class TScript
  89. // ~~~~~ ~~~~~~~
  90. // TScript encapsulates script-related information and tasks.
  91. //
  92. class TScript(name, descr, menuItem, autoload, hasLikenamedFcn) {
  93.   declare Name     = name;
  94.   declare Descr    = descr;
  95.   declare MenuItem = menuItem;
  96.   declare Autoload = autoload;
  97.  
  98.   declare HasLikenamedFcn = hasLikenamedFcn;
  99.  
  100.   //
  101.   // Loads a script. Uses like-named function if there is one, or
  102.   // scriptEngine.Load if there isn't.
  103.   //
  104.   Load()
  105.   {
  106.     if (HasLikenamedFcn) {
  107.       sTmp.Text = Name;
  108.       sTmp.Lower();
  109.       declare cmd = sTmp.SubString(0, sTmp.Index(".spp") - 1).Text + "();";
  110.       IDE.ScriptRun(cmd);
  111.     }
  112.     else {
  113.       scriptEngine.Load(Name);
  114.     }
  115.   }
  116.  
  117.   //
  118.   // Unloads a script. Returns TRUE on success; FALSE otherwise.
  119.   //
  120.   Unload()
  121.   {
  122.     if (scriptEngine.Unload(Name)) {
  123.       return TRUE;
  124.     }
  125.     Msg.Error("Could not unload script file " + Name);
  126.     return FALSE;
  127.   }
  128.  
  129.   //
  130.   // Assigns the script to the Script menu.
  131.   //
  132.   AssignMenuItem()
  133.   {
  134.     sTmp.Text = MenuItem;
  135.     sTmp.Lower();
  136.     if (sTmp.Text != "no menu") {
  137.  
  138.       //
  139.       // Create a launch command with Load or a like-named function, e.g.
  140.       // sppman().
  141.       //
  142.       declare cmd;
  143.       if (HasLikenamedFcn) {
  144.         sTmp.Text = Name;
  145.         sTmp.Lower();
  146.         cmd = sTmp.SubString(0, sTmp.Index(".sp") - 1).Text + "();";
  147.       }
  148.       else {
  149.         cmd = "scriptEngine.Load(\"" + Name + "\");";
  150.       }
  151.  
  152.       // Create the item. Note the return value of assign_to... is incorrect.
  153.       //
  154.       if (!scriptEngine.IsLoaded("menuhook")) {
  155.         scriptEngine.Load("menuhook");
  156.       }
  157.       if (!assign_to_view_menu("IDE", MenuItem, cmd, Descr)) {
  158.         //Msg.Error("Could not add " + Name + " to the script menu.");
  159.       }
  160.     }
  161.   }
  162.  
  163.   //
  164.   // Loads the script into the configured editor if it is found on the
  165.   // script path.
  166.   //
  167.   Edit(userEditor)
  168.   {
  169.     // Parse the script path.
  170.     //
  171.     declare sppDirs = new array[];
  172.     declare position = 0;
  173.     declare i = 0;
  174.     sTmp.Text = scriptEngine.ScriptPath;
  175.     do {
  176.       sTmp = sTmp.Trim();
  177.       position = sTmp.Index(";");
  178.       sppDirs[i] = sTmp.SubString(0, position - 1).Text;
  179.       sTmp = sTmp.SubString(position);
  180.       i++;
  181.     } while (position);
  182.  
  183.     // Look for the script and edit when first found.
  184.     //
  185.     declare dir;
  186.     iterate (dir; sppDirs) {
  187.       if (FileExists(dir + "\\" + Name)) {
  188.         if (userEditor == "IDE") {
  189.           if (!IDE.FileOpen(dir + "\\" + Name, "EditText")) {
  190.             Msg.Error("Could not edit file " + Name);
  191.           }
  192.         }
  193.         else {
  194.           if (WinExec(userEditor + " " + dir + "\\" + Name, 1) <= 31) {
  195.             Msg.Error("Could not edit file " + Name);
  196.           }
  197.         }
  198.         return;
  199.       }
  200.     }
  201.     Msg.Info(Name +
  202.              " could not be edited because it is not on the script path.");
  203.   }
  204. }
  205.  
  206. //
  207. // class TSppMan
  208. // ~~~~~ ~~~~~~~
  209. // TSppMan encapsulates script management functionality.
  210. //
  211. class TSppMan() {
  212.   declare Lines = new array[];
  213.   declare Scripts = new array[];
  214.   declare NumScripts = 0;
  215.   declare UserEditor;
  216.  
  217.   // Load configuration values.
  218.   //
  219.   declare CFGFile = new TConfigFile(ModuleDir + "\\sppman.cfg");
  220.   UserEditor = CFGFile.GetValue("Editor", "IDE");
  221.   CFGFile.Close();
  222.  
  223.   // Load the management system data. 
  224.   //
  225.   declare file = new TFlatFile(ModuleDir + "\\sppman.dat");
  226.   Lines = file.GetLines();
  227.   file.Close();
  228.  
  229.   //
  230.   // Parse the lines of date and create script objects. Ignore comment
  231.   // lines ("/" in column 1) and blank lines.
  232.   //
  233.   declare line;
  234.   iterate (line; Lines) {
  235.     sTmp.Text = line;
  236.     if (sTmp.Index("//") != 1 && sTmp.Text != "") {
  237.       declare values = new array[];
  238.       declare position = 0;
  239.       declare i = 0;
  240.       do {
  241.         position = sTmp.Index(",");
  242.         values[i] = sTmp.SubString(0, position - 1).Text;
  243.         sTmp = sTmp.SubString(position);
  244.         i++;
  245.       } while (position);
  246.  
  247.       Scripts[NumScripts++] = new TScript(values[0], values[1], values[2],
  248.                                           values[3], values[4]);
  249.     }
  250.   }
  251.  
  252.   //
  253.   // Assigns menus and autoloads scripts.
  254.   //
  255.   SetUpScripts()
  256.   {
  257.     declare script;
  258.     iterate (script; Scripts) {
  259.       script.AssignMenuItem();
  260.       if (script.Autoload == "1" || script.Autoload == "TRUE") {
  261.         script.Load();
  262.       }
  263.     }
  264.   }
  265.  
  266.   //
  267.   // Displays the script directory.
  268.   //
  269.   DisplayDirectory()
  270.   {
  271.     Directory.Hidden = TRUE;
  272.     Directory.Execute();
  273.     Directory.Clear();
  274.     for (declare i = 0; i < NumScripts; i++) {
  275.       declare Item;
  276.       Item = FixStr(Scripts[i].Name, NAME_LEN) + " " +
  277.              FixStr(Scripts[i].Descr, DESCR_LEN) + " " +
  278.              (Scripts[i].Autoload ? "Autoload" : "   --   ") + " " +
  279.              (scriptEngine.IsLoaded(Scripts[i].Name) ? "Loaded" : "  --  ");
  280.       Directory.Add(Item, Directory.Count);
  281.     }
  282.     IDE.KeyboardManager.SendKeys("{VK_HOME}", TRUE);
  283.     Directory.Hidden = FALSE;
  284.   }
  285.  
  286.   //
  287.   // Returns a string that is truncated or padded to a given length.
  288.   //
  289.   FixStr(str, len)
  290.   {
  291.     sTmp.Text = str;
  292.     sTmp = sTmp.SubString(0, len);
  293.     while (sTmp.Length < len) sTmp.Text += " ";
  294.     return sTmp.Text;
  295.   }
  296.  
  297.   //
  298.   // Loads the Script Manager data file in the editor.
  299.   //
  300.   EditDataFile()
  301.   {
  302.     if (!IDE.FileOpen(ModuleDir + "\\sppman.dat", "EditText")) {
  303.       Msg.Error("Could not open the Script Manager data file.");
  304.     }
  305.   }
  306. }
  307.  
  308. //
  309. // Create a local menu for directory commands.
  310. //
  311. on Directory:>LeftClick(x, y)
  312. {
  313.   declare items = {LOAD, EDIT, UNLOAD, EDIT_DAT, CANCEL, CLOSE};
  314.   declare popup = new PopupMenu(x, y, items);
  315.  
  316.   switch (popup.Track()) {
  317.     case LOAD:
  318.       SppMan.Scripts[.CurrentIndex].Load();
  319.       .Close();
  320.       break;
  321.  
  322.     case EDIT:
  323.       SppMan.Scripts[.CurrentIndex].Edit(SppMan.UserEditor);
  324.       .Close();
  325.       break;
  326.  
  327.     case UNLOAD:
  328.       SppMan.Scripts[.CurrentIndex].Unload();
  329.       .Close();
  330.       sppdir();   
  331.       break;
  332.  
  333.     case EDIT_DAT:
  334.       SppMan.EditDataFile();
  335.       .Close();
  336.       break;
  337.  
  338.     case CANCEL:
  339.       delete popup;
  340.       break;
  341.  
  342.     case CLOSE:
  343.       .Close();
  344.       break;
  345.   }
  346. }
  347.  
  348. //
  349. // Load a script if the user chooses it. Even with the local menu logic
  350. // this can happen with the keyboard.
  351. //
  352. on Directory:>Accept()
  353. {
  354.   SppMan.Scripts[.CurrentIndex].Load();
  355.   .Close();
  356. }
  357.  
  358.