DnD reorder listctrl

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
Stephen-P
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jan 22, 2005 9:07 pm

DnD reorder listctrl

Post by Stephen-P » Sat Jan 22, 2005 9:31 pm

Anyone have a snippet for using dnd within wxListctrl to reorder its' items? dev-cpp wx2.5.3 msw[/img]

Stephen-P
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jan 22, 2005 9:07 pm

Simple Solution

Post by Stephen-P » Sun Jan 30, 2005 12:28 am

Below is what I came up with for re-ordering a wxListctrl via mouse
I am too new (read that as c++ and wxWidgets newbie ) to figure out how to use actual dnd but this will work fine for a simple list like I have here. If someone knows how to do this using wxDnD please post a reply.

Code: Select all


//---------------------------------------------------------------------------
//
// Name:        wxdrgsmp.h
// Author:      Stephen Podhajecki
// Created:     01/18/2005 - 01/29/2005
// Copyright:
//
//---------------------------------------------------------------------------



#ifndef __WXDRGSMP_H
#define __WXDRGSMP_H

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

////Header Include Start
#include <wx/stattext.h>
#include <wx/listctrl.h>
#include <wx/panel.h>
////Header Include End

#define THIS_DIALOG_STYLE wxCAPTION | wxRESIZE_BORDER | wxSYSTEM_MENU | wxTHICK_FRAME | wxMINIMIZE_BOX | wxMAXIMIZE_BOX |  wxCLOSE_BOX
#define ID_WXLISTCTRL1 1002
#define ID_WXPANEL1 1001

class MainApp: public wxApp
{
  public:
      virtual bool OnInit();
      int OnQuit();
};

class MainFrame: public wxFrame
{
  public:
      MainFrame( wxWindow *parent, wxWindowID id = 1, const wxString &title = _T("newProgramFrame"),
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = THIS_DIALOG_STYLE);
         virtual ~MainFrame();
  private:
      DECLARE_EVENT_TABLE()
  public:
      public:
  ////GUI Control Declaration Start
	wxListCtrl *WxListCtrl1;
	wxPanel *WxPanel1;
    wxString ItemData;  
    wxPoint orgin;
    wxPoint offset;
    long dSource;
    long dTarget;
  ////GUI Control Declaration End
  public:
  ////Function dec
    void OnQuit(wxCommandEvent &event);
    void CreateGUIControls(void);
	void OnMouseEvent(wxMouseEvent& event);
    void InitWithListItems(int);
	void onBeginDrag(wxListEvent& event);
	void onSelected(wxListEvent& event);
    void GetPlace(wxPoint);
	int ArrangeList(void);
 ////Function dec end
 

      
};

enum
{
   ID_MAINWIN_QUIT = wxID_HIGHEST+1
};


#endif
// end of wxdrgsmp.h



//---------------------------------------------------------------------------
//
// Name:        wxdrgsmp.cpp
// Author:      Stephen Podhajecki
// Created:     01/18/2005 - 01/29/2005
// Copyright:
//
//---------------------------------------------------------------------------

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
   #include <wx/wx.h>
#endif

#include "wxdrgsmp.h"

IMPLEMENT_APP(MainApp)

bool MainApp::OnInit()
{
   MainFrame *win = new MainFrame(NULL),
	
	SetTopWindow(win);
	win->Show(TRUE);		
	return TRUE;


}

//----------------------------------------------------------------------------
// MainFrame
//----------------------------------------------------------------------------

    ////Event Table Start
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
   EVT_MENU(ID_MAINWIN_QUIT, MainFrame::OnQuit)
	////Manual Code Start
	////Manual Code End
	EVT_MOTION(MainFrame::OnMouseEvent)
	EVT_LEFT_UP(MainFrame::OnMouseEvent)
	EVT_LIST_ITEM_SELECTED(ID_WXLISTCTRL1,MainFrame::onSelected)
	EVT_LIST_BEGIN_DRAG(ID_WXLISTCTRL1,MainFrame::onBeginDrag)
END_EVENT_TABLE()
    ////Event Table End



MainFrame::MainFrame( wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style )
    : wxFrame( parent, id, title, position, size, style)
{

    CreateGUIControls();
   
}

MainFrame::~MainFrame()
{
    
} 

void MainFrame::CreateGUIControls(void)
{
    ////GUI Items Creation Start

	WxPanel1 =  new wxPanel(this, ID_WXPANEL1, wxPoint(0,0),wxSize(316,307) );

	WxListCtrl1 =  new wxListCtrl(WxPanel1, ID_WXLISTCTRL1,wxPoint(51,11),wxSize(88,188)  , wxLC_LIST |  wxLC_SINGLE_SEL);
/// To DO:  need to find how to get control properties instead of explict value
///         declare below.

// listctrl orgin in main window.
	 orgin.x=51;
	 orgin.y=11;

	this->SetSize(8,8,324,334);
	this->SetTitle(_("Mouse ReOrder of wxListCtrl"));
	this->Center();
	this->SetIcon(wxNullIcon);
    InitWithListItems(10);
    CreateStatusBar(2);
    SetStatusText("Simple Drag Drop Test");
    ////GUI Items Creation End
}

void MainFrame::OnQuit(wxCommandEvent & WXUNUSED(event))
{
    //Close(TRUE);
    Destroy();
}
 
void MainFrame::InitWithListItems(int NUM_ITEMS)
{
    for ( int i = 0; i < NUM_ITEMS; i++ )
    {
        WxListCtrl1->InsertItem(i, wxString::Format(_T("Item %d"), i));
    }
}  

/*
 * WxListCtrl1BeginDrag
 */
void MainFrame::onBeginDrag(wxListEvent& event)
{
   // only capture mouse events on drag
   CaptureMouse();
	
    wxPoint pos = event.GetPoint();
    wxString str;
    str.Printf( wxT("Drag Begin: %d,%d"), pos.x, pos.y );
    this->SetStatusText( str,0 );
    
}

/*
 * WxListCtrl1Selected
 */
void MainFrame::onSelected(wxListEvent& event)
{
       
       
        dSource = event.GetIndex();
        wxString str;
        str.Printf(wxT("Sel Item: %ld "),dSource);
        str += "Data:";
        str +=event.GetText();	
        SetStatusText(str,1 );
    
 
}
void MainFrame::GetPlace( wxPoint mPoint)
{
    // gets point in respect to wxlistctrl
    offset= mPoint - orgin;
}

int MainFrame::ArrangeList(void)
{
    if (dSource == dTarget) {return -1;}
    wxListItem SourceItem;

//  have to setmask along with setid inorder to work??
    SourceItem.SetMask(wxLIST_MASK_TEXT);
    SourceItem.SetId(dSource);
    WxListCtrl1->GetItem(SourceItem);
    WxListCtrl1->DeleteItem(dSource);
    WxListCtrl1->InsertItem(dTarget,SourceItem.GetText());
    
    
//   wxMessageBox("Arrange List Done","WxListCtrl",wxOK |wxICON_INFORMATION );
    return 0;
}
void MainFrame::OnMouseEvent(wxMouseEvent& event)
{
// only two events get captured because onBeginDrag sets up capture

    if( event.LeftUp())
    {
        wxString str1;
        GetPlace(event.GetPosition());
    	int flags;
    	dTarget = WxListCtrl1->HitTest( offset, flags );
    	
     if ( dTarget> -1 && (flags & wxLIST_HITTEST_ONITEM))
        	{
        	    str1.Printf( wxT("Drop Succed:%ld  Flag:%d"),dTarget,flags);
        	    int a =ArrangeList();
        	}    
        else
            {
                str1.Printf( wxT("Drop Failed: %d,%d"), offset.x, offset.y );
            }
            SetStatusText(str1,1);
            
            // not dragging, don't need mouse capture
            ReleaseMouse();
        }                      
   if (event.Dragging())
    {
        wxString str;
        GetPlace(event.GetPosition());
        str.Printf( wxT("Dragging: %d,%d"), offset.x, offset.y );
        this->SetStatusText( str,0 );
    }
        
  
}

Post Reply