{"id":4142,"date":"2022-01-19T11:15:20","date_gmt":"2022-01-19T11:15:20","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4142"},"modified":"2022-01-19T11:17:07","modified_gmt":"2022-01-19T11:17:07","slug":"10b-wxwidgets-wxfiledrop","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/19\/10b-wxwidgets-wxfiledrop\/","title":{"rendered":"10b. wxWidgets &#8211; wxFileDrop"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>In this section we look at an example of dragging and dropping files. Unlike dragging and dropping a text object where we need a drag source as well as a target, for file dropping, we only need a drop target. The file will be dragged from outside the application. At a time, multiple files can be selected and dropped, but in the sample code we are only going to handle the first file in the drag queue. Once the drop is done, it loads the contents of the file and displays it in a <em>wxTextCtrl<\/em>.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">filedragdrop.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n   #include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/dnd.h&gt;\t\n#include &lt;wx\/listctrl.h&gt;\n\nclass FileDragDrop: public wxFrame {\n\n\tpublic:\n\t\tFileDragDrop(const wxString&amp; title);\n\tprivate:\n\t\twxTextCtrl *mText;\t\n};\n\nclass FileDropTarget:public wxFileDropTarget {\n\n\tpublic:\n\t\tFileDropTarget(wxTextCtrl *txt);\n\t\tvirtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString &amp;files);\n\n\tprivate:\n\t  wxTextCtrl *mText;\t\n};\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">filedragdrop.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;filedragdrop.h&quot;\n\nFileDragDrop::FileDragDrop(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n\t\n\twxPanel *panel = new wxPanel(this,-1);\t\n\twxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n\tpanel-&gt;SetSizer(hbox);\n\n\t\n\tmText = new wxTextCtrl(panel, -1, wxT(&quot;Drag any text file here&quot;), wxDefaultPosition, wxDefaultSize,\n\t\t       \twxTE_PROCESS_ENTER | wxTE_MULTILINE);\t\n\n\thbox-&gt;Add(mText, 1, wxALL | wxEXPAND, 4);\n\n\tFileDropTarget *drop = new FileDropTarget(mText);\n\tmText-&gt;SetDropTarget(drop);\n\n\tCenter();\n}\n\nFileDropTarget::FileDropTarget(wxTextCtrl *txt) {\n   mText = txt;\n}\n\nbool FileDropTarget::OnDropFiles(wxCoord x, wxCoord y, const wxArrayString  &amp;files) {\n\tmText-&gt;LoadFile(files&#x5B;0]);\n\t\n\treturn true;\n}\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;filedragdrop.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tFileDragDrop *app = new FileDragDrop(wxT(&quot;File Drag And Drop&quot;));\n\tapp-&gt;Show(true);\n\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-04.png\" alt=\"\" class=\"wp-image-4144\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-04.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-04-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"341\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-27.png\" alt=\"\" class=\"wp-image-4145\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-27.png 671w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-27-620x315.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-19-16-43-27-300x152.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW In this section we look at an example of dragging and dropping files. Unlike dragging and dropping a text object where we need a <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/19\/10b-wxwidgets-wxfiledrop\/\" title=\"10b. wxWidgets &#8211; wxFileDrop\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4142"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4142\/revisions"}],"predecessor-version":[{"id":4148,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4142\/revisions\/4148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}