{"id":4,"date":"2008-03-19T12:37:00","date_gmt":"2008-03-19T17:37:00","guid":{"rendered":"http:\/\/coherentpdf.com\/blog\/?p=4"},"modified":"2008-06-30T04:35:46","modified_gmt":"2008-06-30T09:35:46","slug":"a-proper-gui-for-ocaml-part-one","status":"publish","type":"post","link":"https:\/\/coherentpdf.com\/blog\/?p=4","title":{"rendered":"A Proper GUI for OCaml (Part One)"},"content":{"rendered":"<p>Some time ago, I wrote a GUI for a large <a href=\"http:\/\/caml.inria.fr\/\">OCaml<\/a> project in C++ using <a href=\"http:\/\/www.wxwidgets.org\/\">WxWidgets<\/a>, linked to OCaml in a single executable. It was hard and slow to write and crashed all over the place.<\/p>\n<p>So I&#8217;ve redone it in a particularly low-tech way. Consider the following:<\/p>\n<ul>\n<li>A GUI layer in pure Python, using the <a href=\"http:\/\/www.wxpython.org\/\">wxPython<\/a> library<\/li>\n<li>A GUI library written in pure OCaml<\/li>\n<\/ul>\n<p>We start the two in separate processes, and have them talk via a socket. <em>Sounds horrible &#8211; works fine. <\/em><\/p>\n<p><em><\/em><br \/>Advantages of this as a long-term approach to the building of a proper OCaml GUI library:<\/p>\n<ul>\n<li>Pure. No FFI involved<\/li>\n<li>Wxwidgets is very good. It&#8217;s a whole platform and a mature one<\/li>\n<li>Cross-platform by default. Native appearance on Windows, Unix, OS X<\/li>\n<li>Effort required by OCaml community lower than for any other option<\/li>\n<li>Build single executables for release with <a href=\"http:\/\/www.py2exe.org\/\">py2exe<\/a> and py2app<\/li>\n<li>Write OCaml interfaces to hundreds of python libraries<\/li>\n<\/ul>\n<p>Disadvantages (please add more in the comments!)<\/p>\n<ul>\n<li>Python and OCaml processes can fail independently<\/li>\n<li>Might need to work on getting executable sizes down<\/li>\n<li>Asynchronous event loops need care: work required to ensure mouse events don&#8217;t get out of sync etc &#8211; but this is fairly easy to overcome.<\/li>\n<\/ul>\n<p>Here is a screenshot of our <a href=\"http:\/\/www.coherentgraphics.co.uk\/\">rendering engine <\/a>using this approach on the Mac, on Windows and under Ubuntu.<em><\/em><\/p>\n<p><em><\/em><em><\/em><\/p>\n<p><img decoding=\"async\" style=\"DISPLAY: block; MARGIN: 0px auto 10px; WIDTH: 400px; CURSOR: hand; TEXT-ALIGN: center\" alt=\"\" src=\"http:\/\/www.coherentgraphics.co.uk\/mac.png\" border=\"0\" \/> <img decoding=\"async\" style=\"DISPLAY: block; MARGIN: 0px auto 10px; WIDTH: 400px; CURSOR: hand; TEXT-ALIGN: center\" alt=\"\" src=\"http:\/\/www.coherentgraphics.co.uk\/windows.png\" border=\"0\" \/><br \/><img decoding=\"async\" style=\"DISPLAY: block; MARGIN: 0px auto 10px; WIDTH: 400px; CURSOR: hand; TEXT-ALIGN: center\" alt=\"\" src=\"http:\/\/www.coherentgraphics.co.uk\/linux.png\" border=\"0\" \/><\/p>\n<p>I don&#8217;t have the time to write a generic OCaml \/ wxpython library at the moment, so I&#8217;m just going to stick with my special purpose implementation in our software for now. <\/p>\n<p>In Part Two, I&#8217;ll release the basic libraries:<\/p>\n<ul>\n<li>mltalk.py, pytalk.ml (Establishing a connection between Ocaml and Python processes)<\/li>\n<li>camlpy.ml pycaml.py (Marshalling and Unmarshalling Ocaml and Python data) <\/li>\n<li>Wxgui.ml (Event polling and synchronous events)<\/li>\n<li>main.ml, main.py (Main programs) <\/li>\n<li>Scripts for py2exe and py2app<\/li>\n<\/ul>\n<p>Hopefully someone will have the time to build a proper library from this example.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I wrote a GUI for a large OCaml project in C++ using WxWidgets, linked to OCaml in a single executable. It was hard and slow to write and crashed all over the place. So I&#8217;ve redone it &hellip; <a href=\"https:\/\/coherentpdf.com\/blog\/?p=4\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,5,14],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-gui","tag-ocaml","tag-wxpython"],"_links":{"self":[{"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4"}],"version-history":[{"count":0,"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coherentpdf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}