1.1.6 Creating Custom Dialog

X-Functions support custom dialog created in a dialog builder as interface. If the user wants to use the customized dialog, and also wants to support Auto Update in the dialog, or wants to open the dialog from the Origin menu, then they can create an X-Function with customized dialog. And then can create an OPX file to share it to others.

Creating a Dailog Resource DLL

Here is an example to show you how to build a resource-only DLL in Visual Studio 2008 or 2010 which is accessible in Origin C. Follow the section Creating a Resource-Only DLL, we will get two files: Welcome.dll and resource.h.

Creating X-Function

  1. Copy the above two files Welcome.dll and resource.h to Origin <User files Folder\OriginC\>. Note: the resource DLL file and resource.h file should be saved in the same folder.
  2. Press F10 to open X-Function Builder.
  3. Type "CustomDlg" as the X-Function name and set the X-Function variables as below:
    CustomDlg.png
  4. Click the Tree View buttonOcguide xf treeview button.png to switch to Tree View, and choose Usage Context -> Menus -> Auto GetN Dialog to Custom. Then type the custom dialog function name, for example "OpenCustomDlg", into the Custom Dialog Function edit box. Click Save button to save this X-Function to the default folder under User Files folder and then close X-Function Builder. Note:X-Function should be save to the X-Functions folder under OriginExe folder or User Files Folder
  5. Create a new header file named CustomDlg.h and save it to the "OriginC" folder. Type the following code into this file. The code in the CustomDialog construct function is used to show how to get the X-Function settings from the X-Function to the dialog, and how to set back the settings from the dialog to the X-Function by way of the Origin C TreeNode variable.
    #include <..\Originlab\DialogEx.h>
    #include "resource.h"
    // file name of the resource without extension, here stand for Welcome.dll, 
    //and it assume the DLL is in the same folder as this .h file 
    #define STR_DLG_RESOURCE_DLL  "Welcome"
    
    // the file name of resource DLL
    #ifdef _OWIN64
    #define STR_DLG_RESOURCE_DLL  "Welcome_64" // 64 bit DLL
    #else //!_OWIN64
    #define STR_DLG_RESOURCE_DLL  "Welcome" // 32 bit DLL
    #endif// _OWIN64
     
     
    class CustomDialog : public ResizeDialog
    {
    public:
        // Important: IDD_MY_DIALOG is the dialog resource ID.
        // STR_DLG_RESOURCE_DLL is the name of the dialog resource DLL file. 
        CustomDialog(TreeNode& trXFSettings) : ResizeDialog( IDD_MY_DIALOG, STR_DLG_RESOURCE_DLL )
        {
        	m_trXFSettings = trXFSettings;
        	
        }
     
        int DoModalEx(HWND hParent = NULL)
        {
        	InitMsgMap();
     
        	return ResizeDialog::DoModal(hParent);
        }  
        
    protected:
    EVENTS_BEGIN
    	ON_INIT(OnInitDialog)
    	ON_OK(OnOK)
    EVENTS_END
     
    	BOOL OnInitDialog()
    	{		
    		ResizeDialog::OnInitDialog();	
    		Edit	ed;
    		ed = GetDlgItem(IDC_EDIT1);
    		// init dialog control with xf setting	
    		ed.Text = ftoa(m_trXFSettings.factor.dVal); 
      		return TRUE;
    	} 
     
    	BOOL OnOK()
    	{
    		Edit	ed;
    		ed = GetDlgItem(IDC_EDIT1);
    		m_trXFSettings.factor.dVal = atof(ed.Text);
    		
    		// If the input is 0, it will be changed to 1 automatically.
    		if ( m_trXFSettings.factor.dVal == 0 )
    			m_trXFSettings.factor.dVal = 1;
    
    		return TRUE;
    	}
     
    private:
    	TreeNode 	m_trXFSettings;
    };
  6. Open Origin Code Builder, in the menu choose File -> Open, select your X-Function, make sure the Add to Workspace check box is checked in the Open dialog, and click OK. CustomDlg.XFC is added to the User folder in Origin C Workspace view and shows the function body in Edit view.
  7. Add the additional header file after the line //put additional include files here, as in the following.
    //put additional include files here
    #include "CustomDlg.h"
  8. Add a function to open the custom dialog after the line //put your own support static functions here, as below. The function name is input into the Tree View Custom Dialog Function edit box. The function interface, return type, and argument list should remain the same, as in the following.
    //put your own support static functions here
    bool OpenCustomDlg(TreeNode& tr, PEVENT_FUNC pfn, LPCSTR lpcszTitle, LPCSTR 
    lpcszDescription, HWND hWndParent)
    {
    	CustomDialog dlg(tr);
    	if( dlg.DoModal(GetWindow()) )
    	{
    		tr.AutoUpdate.nVal = AU_AUTO; // Default is 2(Manual)
    		return true;    
    	}
    	else // click Cancel button on dialog
    	{
    		return false; // will not call main function
    	}
    }
  9. Put the following code into the X-Function's main function - CustomDlg function
    out = in * factor;
    
    // put comments like "Col(A)*8" on the Comments label of output column
    DataRange drIn;
    drIn = in.GetSourceDataRange();
    	
    Worksheet wks;
    int c1, c2;
    drIn.GetRange(wks, c1, c2);
    Column col(wks, c1);
    	
    if( wks && col )
    {	
    	string strOutputComment;
    	strOutputComment.Format("Col(%s)*%g", col.GetName(), factor);
    	out.SetOutputComment(strOutputComment);
    }
  10. Click Ocguide XF Compile Button.PNG button.

Using X-Function

  1. Create a worksheet with two column and fill some different data.
  2. Highlight column A and Run "CustomDlg -d" in the Command Window.
  3. Fill "8" to Factor.
  4. Click the OK button to close the dialog.
  5. A new column will be created with an Auto Update lock (Column(A)*factor).
  6. If the input is 0, it will be changed to 1 automatically.
  7. Changing any data in column A will trigger Auto Update to update the output column.

How to share your custom dialog with others

You can use Package Manager to distribute it as a single package file to other Origin users. Then he can install your application by dropping the package file into Origin directly.

  1. Select menu Tool->Package Manager. Click Add Files button to add the four files. CustomDlg.XFC, CustomDlg.h, resource.h, Welcome.dll.
  2. Select File-> Save to save them as "CustomDlg.opx" file and share it with other users.