{"id":4130,"date":"2022-01-15T11:52:09","date_gmt":"2022-01-15T11:52:09","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4130"},"modified":"2022-01-15T11:54:26","modified_gmt":"2022-01-15T11:54:26","slug":"10a-wxwidgets-wxtextdrop","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/15\/10a-wxwidgets-wxtextdrop\/","title":{"rendered":"10a. wxWidgets &#8211; wxTextDrop"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In the sample code below, we setup two wxListCtrl widgets. One of them is filled with string items. The other is empty. We can drag items from the first one to the other to populate the second list. In this case we are using <em>wxTextDropTarget<\/em><\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dragdrop.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 DragDrop: public wxFrame {\n\n\tpublic:\n\t\tDragDrop(const wxString&amp; title);\n\t\tvoid onDragInit(wxListEvent&amp; event); \n\tprivate:\n\t\twxListCtrl *mList1, *mList2;\t\n};\n\nclass DropTarget:public wxTextDropTarget {\n\n\tpublic:\n\t\tDropTarget(wxListCtrl *owner);\n\t\tvirtual bool OnDropText(wxCoord x, wxCoord y, const wxString&amp; data);\n\n\tprivate:\n\t  wxListCtrl *mOwner;\t\n};\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">dragdrop.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;dragdrop.h&quot;\n\nDragDrop::DragDrop(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\tmList1 = new wxListCtrl(panel, -1, wxDefaultPosition, wxDefaultSize, wxLC_LIST | wxLC_SINGLE_SEL);\n\tmList2 = new wxListCtrl(panel, -1, wxDefaultPosition, wxDefaultSize, wxLC_LIST);\n\n\thbox-&gt;Add(mList1, 1, wxALL | wxEXPAND, 10);\n\thbox-&gt;Add(mList2, 1, wxALL | wxEXPAND,10);\n\n\tDropTarget *drop = new DropTarget(mList2);\n\tmList2-&gt;SetDropTarget(drop);\n\n\twxString value = wxT(&quot;&quot;);\n\tfor(int i=1; i &lt;= 50; i++) {\n\t  value = &quot;Item &quot;;\n\t  value.Append(wxString::Format(wxT(&quot;%d&quot;),i));\n\t  mList1-&gt;InsertItem(i, value);\n\t}\n\n\tConnect(mList1-&gt;GetId(), wxEVT_LIST_BEGIN_DRAG, \n\t\t\twxListEventHandler(DragDrop::onDragInit));\n\tCenter();\n}\n\nvoid DragDrop::onDragInit(wxListEvent&amp; event) {\n\tlong sel = mList1-&gt;GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);\n\tif (sel &gt; -1) {\n\t\twxString text = mList1-&gt;GetItemText(sel,0);\n\t\twxTextDataObject tdo(text);\n\t\twxDropSource tds(tdo, mList1);\n\t\ttds.DoDragDrop(wxDrag_CopyOnly);\n\t}\n}\n\nDropTarget::DropTarget(wxListCtrl *owner) {\n  mOwner = owner;\n}\n\nbool DropTarget::OnDropText(wxCoord x, wxCoord y, const wxString&amp; data) {\n\tint count = mOwner-&gt;GetItemCount();\n\tmOwner-&gt;InsertItem(count, data);\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;dragdrop.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tDragDrop *app = new DragDrop(wxT(&quot;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-15-17-16-54.png\" alt=\"\" class=\"wp-image-4134\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-15-17-16-54.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-15-17-16-54-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=\"500\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-15-17-17-12.png\" alt=\"\" class=\"wp-image-4135\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-15-17-17-12.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-15-17-17-12-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW In the sample code below, we setup two wxListCtrl widgets. One of them is filled with string items. The other is empty. We can <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/15\/10a-wxwidgets-wxtextdrop\/\" title=\"10a. wxWidgets &#8211; wxTextDrop\">[&#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-4130","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\/4130","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=4130"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4130\/revisions"}],"predecessor-version":[{"id":4141,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4130\/revisions\/4141"}],"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=4130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}