home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- // Object Scripting
- // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
- //
- // SPPMAN.SPP: Script Manager. Allows you to specify scripts for
- // autoloading, and add scripts to IDE menus. Provides a script directory
- // showing script names, descriptions, and autoload and load statuses.
- // The directory window lets you load and unload scripts, edit them in
- // the specified editor, and edit the Script Manager data file.
- //
- // USE: Set values in sppman.cfg, and add the scripts you want managed to
- // the Script Manager data file, sppman.dat. Load the Script Manager to
- // load any scripts marked for autoloading, and to add scripts to IDE
- // menus. To display the Script Manager directory, run sppdir.spp. Click
- // a script for a menu of commands. To autoload sppman.spp on startup,
- // see the online Help topic "Loading scripts."
- //
- // FILES: SPPMAN.DAT, SPPMAN.CFG, SPPDIR.SPP, MSG.SPP, FILE.SPP, MISC.SPP
- //
- // NOTES: All spp*.* files must reside in the same directory.
- //--------------------------------------------------------------------------
- print typeid(module());
-
- //
- // IDE imports.
- //
- import IDE;
- import scriptEngine;
-
- //
- // Load support module(s).
- //
- if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
- if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
- if (!scriptEngine.IsLoaded("misc")) scriptEngine.Load("misc");
-
- //
- // Constants for the script directory list window.
- //
- #define MENU_CAPT "Script Manager directory (Click a script for commands)"
- #define MENU_X 30
- #define MENU_Y 10
- #define MENU_H 385
- #define MENU_W 675
-
- //
- // Constants for the script directory local menu.
- //
- #define LOAD "Load"
- #define EDIT "Edit"
- #define UNLOAD "Unload"
- #define EDIT_DAT "Edit Script Manager data file"
- #define CANCEL "Cancel"
- #define CLOSE "Close directory"
-
- //
- // Constants for making columnar data in a list window.
- //
- #define NAME_LEN 18
- #define DESCR_LEN 35
-
- sTmp = new String(); // General purpose string object.
- Msg = new TMsg(); // Meassage object.
-
- ModuleDir = GetModuleDir(typeid(module())); // Directory of this script.
-
- Directory = new ListWindow(MENU_X, MENU_Y, MENU_H, MENU_W, MENU_CAPT,
- FALSE, FALSE, NULL);
-
- import "kernel32.dll" {
- int WinExec (char *, int);
- }
-
- export SppMan; // Script Manager object.
-
- sppman()
- {
- //
- // Instantiate a script manager object and set up scripts. This
- // autoloads specified scripts and adds them to menus. SPPDIR.SPP
- // runs the script directory.
- //
- SppMan = new TSppMan();
- SppMan.SetUpScripts();
- }
-
- //
- // class TScript
- // ~~~~~ ~~~~~~~
- // TScript encapsulates script-related information and tasks.
- //
- class TScript(name, descr, menuItem, autoload, hasLikenamedFcn) {
- declare Name = name;
- declare Descr = descr;
- declare MenuItem = menuItem;
- declare Autoload = autoload;
-
- declare HasLikenamedFcn = hasLikenamedFcn;
-
- //
- // Loads a script. Uses like-named function if there is one, or
- // scriptEngine.Load if there isn't.
- //
- Load()
- {
- if (HasLikenamedFcn) {
- sTmp.Text = Name;
- sTmp.Lower();
- declare cmd = sTmp.SubString(0, sTmp.Index(".spp") - 1).Text + "();";
- IDE.ScriptRun(cmd);
- }
- else {
- scriptEngine.Load(Name);
- }
- }
-
- //
- // Unloads a script. Returns TRUE on success; FALSE otherwise.
- //
- Unload()
- {
- if (scriptEngine.Unload(Name)) {
- return TRUE;
- }
- Msg.Error("Could not unload script file " + Name);
- return FALSE;
- }
-
- //
- // Assigns the script to the Script menu.
- //
- AssignMenuItem()
- {
- sTmp.Text = MenuItem;
- sTmp.Lower();
- if (sTmp.Text != "no menu") {
-
- //
- // Create a launch command with Load or a like-named function, e.g.
- // sppman().
- //
- declare cmd;
- if (HasLikenamedFcn) {
- sTmp.Text = Name;
- sTmp.Lower();
- cmd = sTmp.SubString(0, sTmp.Index(".sp") - 1).Text + "();";
- }
- else {
- cmd = "scriptEngine.Load(\"" + Name + "\");";
- }
-
- // Create the item. Note the return value of assign_to... is incorrect.
- //
- if (!scriptEngine.IsLoaded("menuhook")) {
- scriptEngine.Load("menuhook");
- }
- if (!assign_to_view_menu("IDE", MenuItem, cmd, Descr)) {
- //Msg.Error("Could not add " + Name + " to the script menu.");
- }
- }
- }
-
- //
- // Loads the script into the configured editor if it is found on the
- // script path.
- //
- Edit(userEditor)
- {
- // Parse the script path.
- //
- declare sppDirs = new array[];
- declare position = 0;
- declare i = 0;
- sTmp.Text = scriptEngine.ScriptPath;
- do {
- sTmp = sTmp.Trim();
- position = sTmp.Index(";");
- sppDirs[i] = sTmp.SubString(0, position - 1).Text;
- sTmp = sTmp.SubString(position);
- i++;
- } while (position);
-
- // Look for the script and edit when first found.
- //
- declare dir;
- iterate (dir; sppDirs) {
- if (FileExists(dir + "\\" + Name)) {
- if (userEditor == "IDE") {
- if (!IDE.FileOpen(dir + "\\" + Name, "EditText")) {
- Msg.Error("Could not edit file " + Name);
- }
- }
- else {
- if (WinExec(userEditor + " " + dir + "\\" + Name, 1) <= 31) {
- Msg.Error("Could not edit file " + Name);
- }
- }
- return;
- }
- }
- Msg.Info(Name +
- " could not be edited because it is not on the script path.");
- }
- }
-
- //
- // class TSppMan
- // ~~~~~ ~~~~~~~
- // TSppMan encapsulates script management functionality.
- //
- class TSppMan() {
- declare Lines = new array[];
- declare Scripts = new array[];
- declare NumScripts = 0;
- declare UserEditor;
-
- // Load configuration values.
- //
- declare CFGFile = new TConfigFile(ModuleDir + "\\sppman.cfg");
- UserEditor = CFGFile.GetValue("Editor", "IDE");
- CFGFile.Close();
-
- // Load the management system data.
- //
- declare file = new TFlatFile(ModuleDir + "\\sppman.dat");
- Lines = file.GetLines();
- file.Close();
-
- //
- // Parse the lines of date and create script objects. Ignore comment
- // lines ("/" in column 1) and blank lines.
- //
- declare line;
- iterate (line; Lines) {
- sTmp.Text = line;
- if (sTmp.Index("//") != 1 && sTmp.Text != "") {
- declare values = new array[];
- declare position = 0;
- declare i = 0;
- do {
- position = sTmp.Index(",");
- values[i] = sTmp.SubString(0, position - 1).Text;
- sTmp = sTmp.SubString(position);
- i++;
- } while (position);
-
- Scripts[NumScripts++] = new TScript(values[0], values[1], values[2],
- values[3], values[4]);
- }
- }
-
- //
- // Assigns menus and autoloads scripts.
- //
- SetUpScripts()
- {
- declare script;
- iterate (script; Scripts) {
- script.AssignMenuItem();
- if (script.Autoload == "1" || script.Autoload == "TRUE") {
- script.Load();
- }
- }
- }
-
- //
- // Displays the script directory.
- //
- DisplayDirectory()
- {
- Directory.Hidden = TRUE;
- Directory.Execute();
- Directory.Clear();
- for (declare i = 0; i < NumScripts; i++) {
- declare Item;
- Item = FixStr(Scripts[i].Name, NAME_LEN) + " " +
- FixStr(Scripts[i].Descr, DESCR_LEN) + " " +
- (Scripts[i].Autoload ? "Autoload" : " -- ") + " " +
- (scriptEngine.IsLoaded(Scripts[i].Name) ? "Loaded" : " -- ");
- Directory.Add(Item, Directory.Count);
- }
- IDE.KeyboardManager.SendKeys("{VK_HOME}", TRUE);
- Directory.Hidden = FALSE;
- }
-
- //
- // Returns a string that is truncated or padded to a given length.
- //
- FixStr(str, len)
- {
- sTmp.Text = str;
- sTmp = sTmp.SubString(0, len);
- while (sTmp.Length < len) sTmp.Text += " ";
- return sTmp.Text;
- }
-
- //
- // Loads the Script Manager data file in the editor.
- //
- EditDataFile()
- {
- if (!IDE.FileOpen(ModuleDir + "\\sppman.dat", "EditText")) {
- Msg.Error("Could not open the Script Manager data file.");
- }
- }
- }
-
- //
- // Create a local menu for directory commands.
- //
- on Directory:>LeftClick(x, y)
- {
- declare items = {LOAD, EDIT, UNLOAD, EDIT_DAT, CANCEL, CLOSE};
- declare popup = new PopupMenu(x, y, items);
-
- switch (popup.Track()) {
- case LOAD:
- SppMan.Scripts[.CurrentIndex].Load();
- .Close();
- break;
-
- case EDIT:
- SppMan.Scripts[.CurrentIndex].Edit(SppMan.UserEditor);
- .Close();
- break;
-
- case UNLOAD:
- SppMan.Scripts[.CurrentIndex].Unload();
- .Close();
- sppdir();
- break;
-
- case EDIT_DAT:
- SppMan.EditDataFile();
- .Close();
- break;
-
- case CANCEL:
- delete popup;
- break;
-
- case CLOSE:
- .Close();
- break;
- }
- }
-
- //
- // Load a script if the user chooses it. Even with the local menu logic
- // this can happen with the keyboard.
- //
- on Directory:>Accept()
- {
- SppMan.Scripts[.CurrentIndex].Load();
- .Close();
- }
-
-