{"id":6680,"date":"2016-09-02T14:07:56","date_gmt":"2016-09-02T18:07:56","guid":{"rendered":"http:\/\/blog.originlab.com\/?p=6680"},"modified":"2016-12-30T13:32:16","modified_gmt":"2016-12-30T18:32:16","slug":"json-oc-html","status":"publish","type":"post","link":"https:\/\/blog.originlab.com\/ja\/json-oc-html","title":{"rendered":"Build a JavaScript dialog and Call Origin C in the Upcoming Origin 2017"},"content":{"rendered":"<p>Building an Origin C based dialog is challenging in two aspects:<\/p>\n<ol>\n<li>The complexity of C++ based coding and Origin C specific implementations<\/li>\n<li>The need to build a resource DLL using Visual Studio<\/li>\n<\/ol>\n<p>In Origin2017, we have a new mechanism to create a dialog without a resource DLL.\u00a0Such a dialog will be\u00a0built by html &amp; JavaScript, like a web page. By this way developers will have more flexibility of dialog design and style.<\/p>\n<p>Documentation on this feature is available <a href=\"http:\/\/www.originlab.com\/doc\/OriginC\/guide\/HTML-Dialog\">online<\/a>.<\/p>\n<p>In this post, we will show you how to build a simple calculator using this new Origin feature. This example will focus on showing JavaScript code calling Origin C functions. The reverse call, Origin C calling JavaScript, is also supported in Origin 2017, but will not be covered in this simple example.<\/p>\n<p>Before we started, we need to build a calculator interface by HTML. For example:<\/p>\n<pre title=\"SimpleCalc.html\" class=\"lang:js decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;title&gt;Origin Calculator&lt;\/title&gt;\r\n\t\t&lt;meta name=&quot;viewport&quot; content=&quot;initial-scale=1.0&quot;&gt;\r\n    \t        &lt;meta charset=&quot;utf-8&quot;&gt;\r\n    \t&lt;script&gt;\r\n    \t&lt;\/script&gt;\r\n\r\n\t&lt;\/head&gt;\r\n\r\n\t&lt;body&gt;\r\n\t\t&lt;div id=&quot;Equation&quot; style=&quot;border:solid;height:20px;&quot;&gt;&lt;\/div&gt;\r\n\t\t&lt;div id=&quot;Ops&quot;&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnPlus&quot; value=&quot;+&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnMinus&quot; value=&quot;-&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnMultiply&quot; value=&quot;X&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnDiv&quot; value=&quot;\/&quot; onclick=&quot;&quot; \/&gt;\r\n        &lt;\/div&gt;\r\n        &lt;\/br&gt;\r\n        &lt;div id=&quot;Num789&quot;&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn7&quot; value=&quot;7&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn8&quot; value=&quot;8&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn9&quot; value=&quot;9&quot; onclick=&quot;&quot; \/&gt;\r\n        &lt;\/div&gt;\r\n        &lt;div id=&quot;Num456&quot;&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn4&quot; value=&quot;4&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn5&quot; value=&quot;5&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn6&quot; value=&quot;6&quot; onclick=&quot;&quot; \/&gt;\r\n        &lt;\/div&gt;\r\n        &lt;div id=&quot;Num123&quot;&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn1&quot; value=&quot;1&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn2&quot; value=&quot;2&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btn3&quot; value=&quot;3&quot; onclick=&quot;&quot; \/&gt;\r\n        &lt;\/div&gt;\r\n        &lt;\/br&gt;\r\n        &lt;div&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnCalculate&quot; value=&quot;Calculate&quot; onclick=&quot;&quot; \/&gt;\r\n        \t&lt;input type=&quot;button&quot; id=&quot;btnClear&quot; value=&quot;Clear&quot; onclick=&quot;&quot; \/&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>From the interface design, we will need three types of functions: Add a char to the end of the equation, calculate the equation and clear the box.<\/p>\n<p>Let&#8217;s go back to Origin and create a blank cpp file. First, we need to include two extra header files, the one called <em><strong>HTMLDlg.h<\/strong><\/em> is new in Origin2017 and that&#8217;s the one that hooks Origin and your HTML dialog together.<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;Origin.h&gt;\r\n#include &lt;..\\OriginLab\\DialogEx.h&gt;\r\n#include &lt;..\\OriginLab\\HTMLDlg.h&gt;<\/pre>\n<p>Now let&#8217;s create the dialog class<\/p>\n<pre class=\"lang:c++ decode:true\">class OriginCalculatorDlg: public HTMLDlg {\r\npublic:\r\n\tstring GetInitURL();\r\n\tstring GetDialogTitle();\r\nprotected:\r\n\tstring AddOp(string str, string strOp);\r\n        string AddNum(string str, int NewNum);\r\n        double Calculate(string str);\r\n        string Clear();\r\n};\r\n<\/pre>\n<p>The first public method is the key to link the dialog to this source code, in this example, we used a special char (<em><strong>__FILE__<\/strong><\/em>) represents the same folder location where the current cpp file is.<\/p>\n<pre class=\"lang:c++ decode:true\">string OriginCalculatorDlg::GetInitURL() {\r\n\tstring strPath = GetFilePath(__FILE__);\r\n\tstrPath += \"SimpleCalc.html\";    \/\/html file in same folder as this cpp file\r\n\treturn strPath;\r\n}<\/pre>\n<p>Then provide the title of the dialog by:<\/p>\n<pre class=\"lang:c++ decode:true\">string OriginCalculatorDlg::GetDialogTitle() {\r\n\tstring strTitle(\"Simple Calculator\");\r\n\treturn strTitle;\r\n}<\/pre>\n<p>We separated the adding char to the end function to two cases, string for operations and numeric for numbers, just for showing how to pass different variable type form html dialog to OriginC:<\/p>\n<pre class=\"lang:c++ decode:true\">string OriginCalculatorDlg::AddOp(string str, string strOp) {\r\n\treturn str + \" \" + strOp + \" \";\r\n}\r\n\r\nstring OriginCalculatorDlg::AddNum(string str, int NewNum) {\r\n\treturn str + ftoa(NewNum);\r\n}<\/pre>\n<p>Then, define the calculation and clear function:<\/p>\n<pre class=\"lang:c++ decode:true\">double OriginCalculatorDlg::Calculate(string str) {\r\n\tdouble dd;\r\n\tLT_evaluate(str ,&amp;dd); \r\n\treturn dd;\r\n}\r\n\r\nstring OriginCalculatorDlg::Clear(void) {\r\n\treturn \"\";\r\n}<\/pre>\n<p>Finally, add the following special macro so that the JavaScript in the HTML can locate where those functions are.<\/p>\n<pre class=\"lang:c++ decode:true\">public:\r\n\tDECLARE_DISPATCH_MAP<\/pre>\n<p>After the class declaration, list the detailed function list.<\/p>\n<pre class=\"lang:c++ decode:true\">BEGIN_DISPATCH_MAP(OriginCalculatorDlg, HTMLDlg)\r\n\tDISP_FUNCTION(OriginCalculatorDlg, AddOp, VTS_STR, VTS_STR VTS_STR)    \/\/ DISP_FUNCTION(ClassName, MethodName, OutputType, InputType1 InputType2 ...)\r\n\tDISP_FUNCTION(OriginCalculatorDlg, AddNum, VTS_STR, VTS_STR VTS_I4)\r\n\tDISP_FUNCTION(OriginCalculatorDlg, Calculate, VTS_R8, VTS_STR)\r\n\tDISP_FUNCTION(OriginCalculatorDlg, Clear, VTS_STR, VTS_VOID)\r\nEND_DISPATCH_MAP<\/pre>\n<p>In the html JavaScript, use <em><strong>window.external.ExtCall(FunctionName, Input1, Input2, &#8230;)<\/strong><\/em> to call the Origin C functions in the header.<\/p>\n<pre class=\"lang:js decode:true\">&lt;script&gt;\r\n    \tvar PlusOp = \"+\";\r\n    \tvar MinusOp = \"-\";\r\n    \tvar MultiplyOp = \"*\";\r\n    \tvar DivOp = \"\/\";\r\n\r\n    \tfunction Invoke_AddOp(strOp) {\r\n    \t\tvar OriginStr = document.getElementById('Equation').innerHTML;\r\n    \t\tvar NewStr = window.external.ExtCall(\"AddOp\", OriginStr, strOp);\r\n   \t\tdocument.getElementById('Equation').innerHTML = NewStr;\r\n    \t}\r\n\r\n    \tfunction Invoke_AddNum(NewNum) {\r\n    \t\tvar OriginStr = document.getElementById('Equation').innerHTML;\r\n    \t\tvar NewStr = window.external.ExtCall(\"AddNum\", OriginStr, NewNum);\r\n    \t\tdocument.getElementById('Equation').innerHTML = NewStr;\r\n    \t}\r\n\r\n    \tfunction Invoke_Cal() {\r\n    \t\tvar Str = document.getElementById('Equation').innerHTML;\r\n    \t\tvar Result = window.external.ExtCall(\"Calculate\", Str);\r\n    \t\tdocument.getElementById('Equation').innerHTML = Result;\r\n    \t}\r\n\r\n    \tfunction Invoke_Clear() {\r\n    \t\tdocument.getElementById('Equation').innerHTML = window.external.ExtCall(\"Clear\");\r\n    \t}\r\n\r\n&lt;\/script&gt;<\/pre>\n<p>Then you can call those functions by the buttons you created. For example:<\/p>\n<pre class=\"lang:default decode:true\">&lt;input type=\"button\" id=\"btnPlus\" value=\"+\" onclick=\"Invoke_AddOp(PlusOp)\" \/&gt;\r\n...\r\n&lt;input type=\"button\" id=\"btn7\" value=\"7\" onclick=\"Invoke_AddNum(7)\" \/&gt;\r\n...\r\n&lt;input type=\"button\" id=\"btnCalculate\" value=\"Calculate\" onclick=\"Invoke_Cal()\" \/&gt;<\/pre>\n<p>Now we have hooked the html and Origin C together. Create a main function at the end of the cpp file to launch the dialog.<\/p>\n<pre class=\"lang:c++ decode:true\">void calc() {\r\n\tOriginCalculatorDlg dlg;\r\n\tdlg.DoModalEx(GetWindow());\r\n}<\/pre>\n<p>You can <a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/Simple-Calculator.zip\"> download this zip<\/a> to see the code, or download more examples <a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/11\/HTMLDlgExamples.zip\">here.<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Building an Origin C based dialog is challenging in two &hellip; <\/p>","protected":false},"author":14,"featured_media":6712,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-6680","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-oc"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7DvU2-1JK","jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/09\/OriginCalculator.png","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/comments?post=6680"}],"version-history":[{"count":39,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6680\/revisions"}],"predecessor-version":[{"id":7567,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6680\/revisions\/7567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media\/6712"}],"wp:attachment":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media?parent=6680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/categories?post=6680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/tags?post=6680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}