Skip to content

Instantly share code, notes, and snippets.

@thegabriele97
Created November 29, 2018 23:12
Show Gist options
  • Save thegabriele97/b01cce1b28425c3a7d9a593d83676a9f to your computer and use it in GitHub Desktop.
Save thegabriele97/b01cce1b28425c3a7d9a593d83676a9f to your computer and use it in GitHub Desktop.
#include "DropTargetListCtrl.h"
DropTargetListCtrl::DropTargetListCtrl(wxListCtrl* _list) {
list = _list;
}
void DropTargetListCtrl::newDropEvent(int _index, wxPoint _start, ActivityList* _al) {
start = _start;
index = _index;
al = _al;
wxRect rect;
list->GetItemRect(index, rect, wxLIST_RECT_LABEL);
itemHeight = rect.GetHeight();
}
bool DropTargetListCtrl::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
int i = 0, swap_index, item_count;
//item_count is used to know how items are needed to be traslated
//i know how much pixel are dragged (start.y is where
item_count = (start.y - y) / itemHeight;
item_count *= (item_count < 0) ? -1 : 1; //abs
//this method will swap position between 2 sequential elements in the ActivityList
//the elements that will be swapped are [index-item_count, index] or [index, index+item_count]
//this is done using (start.y - y > 0) that knows if the mouse cursor
//is moved up or down
swap_index = (start.y - y > 0) ? index - item_count : index + item_count;
swap_index = (swap_index < 0) ? 0 : swap_index; //to beat a stupid user that try to move first element above it
//so index become swap_index other items position is mvoed up or down
//changing position of (index) element with its new position (swap_index) in the list
al->getActivity(data.ToStdString()).setPosition(swap_index); //data is current element name at (index)
//changing all other elements.
//if item is moved down, an example:
// * moving item with index=1 to swap_index=3
// * so, all elements <= index, are ignored
// * from elements > index and <= swap index (so from 2 to 3, included)
// * 2 => index++(so 2 => 1, because index=1, and index become 2)
// * 3 => index++(so 3 >= 2, ..)
// * now we have a list like {0, 3, 1, 2).. this will be sorted and drawed
//each item is scaled by one position down
if (start.y - y < 0) {
for (Activity& a : al->getActivityContainer()) {
if (i <= index) {
i++;
continue;
} else if (i <= swap_index) {
al->getActivity(a.getTitle()).setPosition(index++);
i++;
} else {
break;
}
}
} else {
//if item is moved up, an example:
// * moving item with index=3 to swap_index=1
// * so, all elements < swap_index, are ignored
// * from elements >= swap_index and < index (so from 1 included to 3 not included)
// * 1 => ++swap_index(ax)(so 1 => 2)
// * 2 => ++swap_index(ax)(so 2 => 3)
// * now we have a list like {0, 2, 3, 1).. this will be sorted and drawed
//each item is scaled by one position up
int ax = swap_index;
for (Activity& a : al->getActivityContainer()) {
if (i < swap_index) {
i++;
continue;
} else if (i >= swap_index && i < index) {
al->getActivity(a.getTitle()).setPosition(++ax);
i++;
} else {
break;
}
}
}
return true;
}
#ifndef DROPTARGETLISTCTRL_H
#define DROPTARGETLISTCTRL_H
#include <wx/listctrl.h>
#include <wx/dnd.h>
#include "Administrator.hpp"
class DropTargetListCtrl : public wxTextDropTarget {
public:
DropTargetListCtrl(wxListCtrl* _list);
void newDropEvent(int index, wxPoint _start, ActivityList* al);
virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data);
private:
int itemHeight;
wxListCtrl* list;
wxPoint start;
int index;
ActivityList* al;
};
#endif // DROPTARGETLISTCTRL_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment