O-Calc Pro Plugin – “Hello World”

Home » Plugin development » O-Calc Pro Plugin – “Hello World”
Plugin development No Comments

The default O-Calc Pro Plugin template will create a message box showing the message “Hello World”. Below are some explanations of the various code components of this simple plugin example.

Defining the Plugin Type:

  1. /// <summary>
  2. /// Declare the type of plugin as one of:
  3. ///         DOCKED_TAB
  4. ///         MENU_ITEM
  5. ///         BOTH_DOCKED_AND_MENU
  6. ///         CLEARANCE_SAG_PROVIDER
  7. /// </summary>
  8.     public PLUGIN_TYPE Type
  9.     {
  10.       get
  11.          {
  12.           return PLUGIN_TYPE.BOTH_DOCKED_AND_MENU;
  13.          }
  14.     }

There are four different plug-in types that can be defined. In the code snippet above, the plugin type as been defined as both a ‘Menu Item’ and a new ‘Docked Tab’.

Naming the Plugin:

  1. /// <summary>
  2. /// Declare the name of the plugin usd for synthesizing the registry keys ect
  3. /// </summary>
  4.     public String Name
  5.         {
  6.          get
  7.            {
  8.              return "MyOCalcProPlugin";
  9.             }
  10.         }

This will be the name of the menu item as it appears in the O-Calc Pro interface. To change the name of the resultant compiled plugin file that gets built to the location C:\Program Files (x86)\Osmose\O-Calc Pro\bin\Plugins, you would need to change the name of the Visual Studio Project. Note: the name of the compiled plugin file is what will be displayed in the O-Calc Pro ‘Manage Plugins’ list.

Specifying the Docked Tab Parameters :

  1. /// <summary>
  2. /// Add a tabbed form to the tabbed window (if the plugin type is
  3. /// PLUGIN_TYPE.DOCKED_TAB
  4. /// or
  5. /// PLUGIN_TYPE.BOTH_DOCKED_AND_MENU
  6. /// </summary>
  7. /// <param name="pPPLMain"></param>
  8.     public void AddForm(PPL_Lib.PPLMain pPPLMain)
  9.       {
  10.         . . .
  11.       }
  12.  
  13.      PluginForm cForm = null;
  14.      class PluginForm : WeifenLuo.WinFormsUI.Docking.DockContent
  15.      {
  16.        public PluginForm()
  17.        {
  18.           this.Name = "pluginForm";
  19.           this.Text = "Plugin";
  20.           System.Windows.Forms.Label helloLabel = new System.Windows.Forms.Label();
  21.           helloLabel.AutoSize = true;
  22.           helloLabel.Name = "helloLabel";
  23.           helloLabel.Text = "Hello World";
  24.           helloLabel.Location = new System.Drawing.Point(20, 20);
  25.           helloLabel.Visible = true;
  26.              Controls.Add(helloLabel);
  27.        }
  28.        public Guid cGuid;
  29.        protected override string GetPersistString()
  30.        {
  31.            return cGuid.ToString();
  32.        }

Here the developer can specify the various parameters associated with the docked tab, such as tab name, display text, and other features of the new docked tab that will appear within the O-Calc Pro interface.

Defining the location of the Menu Item:

  1. public void AddToMenu(PPL_Lib.PPLMain pPPLMain, System.Windows.Forms.ToolStrip pToolStrip)
  2.     {
  3.        //save the reference to the O-Calc Pro main
  4.        cPPLMain = pPPLMain;
  5.  
  6.        //create the toolstrip button
  7.        cToolStripMenuItemButton = new ToolStripMenuItem(Name);
  8.        cToolStripMenuItemButton.AutoToolTip = true;
  9.        cToolStripMenuItemButton.ToolTipText = Description;
  10.  
  11.        //find the dropdown menu we want to add the toolsrip button to
  12.        int itemindex = 0;
  13.        System.Diagnostics.Debug.Assert(pToolStrip.Items[itemindex] is ToolStripDropDownButton);
  14.        if (pToolStrip.Items[itemindex] is ToolStripDropDownButton)
  15.        {
  16.           ToolStripDropDownButton tsb = pToolStrip.Items[itemindex] as ToolStripDropDownButton;
  17.           System.Diagnostics.Debug.Assert(tsb.Text == "&File");
  18.           tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, cToolStripMenuItemButton);
  19.           tsb.DropDownItems.Insert(tsb.DropDownItems.Count - 1, new ToolStripSeparator());
  20.  
  21.           //add an event handler when the dropdown menu is opened to allow us
  22.           //to enable or disble the toolstrip button (optional)
  23.           tsb.DropDownOpened += tsb_DropDownOpened;
  24.       }

In the default code snippet above, the new menu item is placed at the bottom of the ‘File’ drop down menu. This menu item will be disabled until a pole has been loaded within the application.

  1. /// <summary>
  2. /// The menu containing our tool is being displayed.  Optionally
  3. /// enable or disable the toolstrip button depending on
  4. /// our criteria
  5. /// </summary>
  6. /// <param name="sender"></param>
  7. /// <param name="e"></param>
  8.     void tsb_DropDownOpened(object sender, EventArgs e)
  9.    {
  10.         bool enabled = false;
  11.         if (cPPLMain != null)
  12.        {
  13.            enabled = (cPPLMain.GetMainStructure() is PPLPole);
  14.        }
  15.         cToolStripMenuItemButton.Enabled = enabled;
  16.    }

Perform the Plugin Operation

  1. /// <summary>
  2. /// Perform the plugin tool's operation
  3. /// </summary>
  4. private void DoPluginOperation()
  5. {
  6.       try
  7.       {
  8.           PPLMessageBox.Show("Hello World!");
  9.       }
  10.       catch (Exception ex)
  11.       {
  12.           PPLMessageBox.Show(ex.Message, "Error in " + Name);
  13.       }
  14. }

In this example, clicking on the plugin menu item found in the File dropdown, will display the “Hello World” message box.

Below is a video that demonstrates this ‘Hello World’ plugin:

Facebooktwitterlinkedinmail