如何在两个(或更多)wxListCtrl(wxGTK)之间拖放?

时间:2013-06-23 10:48:14

标签: c++ wxwidgets

我正在尝试组织一个wxListCtrl派生的控件(xList),它支持项目之间的DnD(LC_REPORT视图)。所以,我抓住了BEGIN_DRAG事件

Connect(wxEVT_COMMAND_LIST_BEGIN_DRAG,
        (wxObjectEventFunction)&xList::OnBeginDrag
       );

OnBeginDrag函数的设计方式是为xListlist)的每个实例捕捉鼠标移动和鼠标左按钮事件:

list->Connect(wxEVT_MOTION,
              wxMouseEventHandler(xList::OnMoveDrag)
             );
list->Connect(wxEVT_LEFT_UP,
              wxMouseEventHandler(xList::OnEndDrag)
             );

(和OnEndDrag将它们全部断开连接)。当我有单xList个实例(一个面板)时,它可以很好地工作,但是当我有两个时,看起来像运动和左上方事件只捕获我开始拖动的面板:我可以DnD在单个面板内,但是当我将鼠标从一个面板拖动到另一个面板时,它仍然像xList::OnMoveDrag一样仍然适用于第一个面板。我错过了什么?

是否分别为每个小部件处理wxEVT_MOTION?如果是这样,为什么程序会像这样。如果没有,为什么总是处理小部件我开始拖动,而不是最后连接的?

这是一个示例代码(尽可能简单),以显示正在发生的事情:

#include <wx/wx.h>
#include <vector>

class xList;

// class to store group of xList to DnD between
class DnDxList
   {public:
       void BeginDrag ();
       void EndDrag ();
       void AddList (xList* l) {list.push_back (l); }; // register new xList object
    private:
       std::vector<xList*> list;
   };


class xList: public wxListCtrl
   {public:
       xList (DnDxList& dnd,
              wxWindow *parent,
              wxWindowID winid = wxID_ANY,
              const wxPoint& pos = wxDefaultPosition,
              const wxSize& size = wxDefaultSize,
              long style = wxLC_ICON,
              const wxValidator &validator = wxDefaultValidator,
              const wxString &name = wxListCtrlNameStr
             );
       virtual ~xList () {};
       void OnBeginDrag(wxListEvent& event);
       void OnEndDrag(wxMouseEvent& event);
       void OnMoveDrag(wxMouseEvent& event);

       DnDxList& dndsource; // keep reference to common DnDxList object
   };


void DnDxList::BeginDrag () // connect motion and left-up events for all lists in group
   {for (std::vector<xList*>::const_iterator i = list.begin(); i != list.end(); i++)
        {(*i)->Connect(wxEVT_MOTION,
                           wxMouseEventHandler(xList::OnMoveDrag)
                      );
         (*i)->Connect(wxEVT_LEFT_UP,
                       wxMouseEventHandler(xList::OnEndDrag)
                      );
        }
   };

void DnDxList::EndDrag () // disconnect all
   {for (std::vector<xList*>::const_iterator i = list.begin(); i != list.end(); i++)
        {(*i)->Disconnect(wxEVT_MOTION,
                          wxMouseEventHandler(xList::OnMoveDrag)
                         );
         (*i)->Disconnect(wxEVT_LEFT_UP,
                          wxMouseEventHandler(xList::OnEndDrag)
                         );
        }
   }




xList::xList (DnDxList& dnd,
              wxWindow *parent,
              wxWindowID winid,
              const wxPoint& pos,
              const wxSize& size,
              long style,
              const wxValidator &validator,
              const wxString &name
             ): wxListCtrl (parent, winid, pos, size, style, validator, name),
                dndsource (dnd)
   {Connect(wxEVT_COMMAND_LIST_BEGIN_DRAG,
            (wxObjectEventFunction)&xList::OnBeginDrag
           );
    dndsource.AddList (this);
   };


void xList::OnBeginDrag(wxListEvent& event) // begin drag
   {SetCursor(wxCursor(wxCURSOR_HAND));
    dndsource.BeginDrag();
   }

void xList::OnMoveDrag(wxMouseEvent& event) 
   {std::cout << "Movie: " << this << std::endl;  // to show the object for which the move event is called for
   }

void xList::OnEndDrag(wxMouseEvent& event) 
   {std::cout << "End: " << this << std::endl;
    dndsource.EndDrag();
    SetCursor(wxCursor(*wxSTANDARD_CURSOR));
   }


class xFrame: public wxFrame
   {
    public:
        xFrame (const wxString& title,
                const wxPoint& pos,
                const wxSize& size
               );
        ~xFrame () { }
    private:

        void OnExit(wxCommandEvent& event);
        DECLARE_EVENT_TABLE();

        DnDxList* dndxlist;
        xList* lp;
        xList* rp;
        wxPanel* panel;
   };

BEGIN_EVENT_TABLE(xFrame, wxFrame)
    EVT_MENU(wxID_EXIT, xFrame::OnExit)
END_EVENT_TABLE()


xFrame::xFrame(const wxString& title,
               const wxPoint& pos,
               const wxSize& size
              ): wxFrame(NULL, wxID_ANY, title, pos, size)
  {
   panel = new wxPanel(this);
   wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);

   // create common DnDxList
   dndxlist = new DnDxList(); 
   // create two panels
   lp = new xList (*dndxlist,
                   panel,
                   wxID_ANY,
                   wxDefaultPosition,
                   wxDefaultSize,
                   wxLC_REPORT | wxLC_SINGLE_SEL
                  );
   rp = new xList (*dndxlist,
                   panel,
                   wxID_ANY,
                   wxDefaultPosition,
                   wxDefaultSize,
                   wxLC_REPORT | wxLC_SINGLE_SEL
                  );

   // some contents
   lp->InsertColumn(0, _("A"));
   lp->InsertColumn(1, _("B"));
   lp->InsertColumn(2, _("C"));
   lp->InsertColumn(3, _("D"));

   lp->SetColumnWidth(0, 100);
   lp->SetColumnWidth(1, 100);
   lp->SetColumnWidth(2, 100);
   lp->SetColumnWidth(3, 100);


   for (long i = 0; i < 100; i++)
      {lp->InsertItem(i, 1);
       for (int j = 0; j < 4; j++)
          {wxString s;
           s << _("lp [") << i << _(", ") << j << _("]");
           lp->SetItem (i, j, s);
          }
      }

   rp->InsertColumn(0, _("A"));
   rp->InsertColumn(1, _("B"));
   rp->InsertColumn(2, _("C"));
   rp->InsertColumn(3, _("D"));

   rp->SetColumnWidth(0, 100);
   rp->SetColumnWidth(1, 100);
   rp->SetColumnWidth(2, 100);
   rp->SetColumnWidth(3, 100);


   for (long i = 0; i < 100; i++)
      {rp->InsertItem(i, 1);
       for (int j = 0; j < 4; j++)
          {wxString s;
           s << _("rp [") << i << _(", ") << j << _("]");
           rp->SetItem (i, j, s);
          }
      }

   sizer->Add(lp,1, wxEXPAND | wxALL, 10);
   sizer->Add(rp,1, wxEXPAND | wxALL, 10);
   panel->SetSizer(sizer);


  }

void xFrame::OnExit(wxCommandEvent& event)
  {
   Close( true );
  }


class xApp: public wxApp
   {
    public:
       virtual bool OnInit();
   };

IMPLEMENT_APP(xApp);


bool xApp::OnInit()
   {
    xFrame *frame = new xFrame(_("Frame"), wxPoint(50, 50), wxSize(450, 340) );
    frame->Show(true);
    return true;
   }

查看控制台输出,可以发现鼠标移动事件和鼠标左移事件始终调用同一对象的方法,其中拖动开始,而不是对象鼠标实际上是。

1 个答案:

答案 0 :(得分:0)

wxListCtrl - 特定的拖动事件确实只是用于拖动同一控件中的项目。要在不同控件之间拖放内容,甚至在不同应用程序之间拖放,您需要使用常规wxDragSourcewxDropTarget类。