Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <wx/wx.h>
- #include <wx/listbox.h>
- class TestBox : public wxListBox
- {
- private:
- wxColour* m_secondColor;
- bool m_isDragging;
- bool m_isPressing;
- bool m_hasScrolled;
- wxPoint m_lastPos;
- void paintLine(int y);
- void OnLeftDown(wxMouseEvent &event);
- void OnLeftUp(wxMouseEvent &event);
- void OnMove(wxMouseEvent &event);
- public:
- TestBox(wxWindow* parent, wxWindowID id);
- void appendToList(wxString item, int pos);
- DECLARE_EVENT_TABLE()
- };
- BEGIN_EVENT_TABLE(TestBox, wxListBox)
- EVT_LEFT_DOWN (TestBox::OnLeftDown)
- EVT_LEFT_UP (TestBox::OnLeftUp)
- EVT_MOTION (TestBox::OnMove)
- END_EVENT_TABLE()
- TestBox::TestBox(wxWindow* parent, wxWindowID id)
- : wxListBox(parent, id, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_EXTENDED | wxBORDER_SIMPLE | wxLB_OWNERDRAW, wxDefaultValidator, wxListBoxNameStr)
- {
- m_secondColor = new wxColour(235, 250, 255);
- m_isDragging = false;
- m_isPressing = false;
- m_hasScrolled = false;
- SetSize(610,430);
- }
- void TestBox::appendToList(wxString item, int pos)
- {
- DeselectAll();
- int newPos = Insert(item, pos);
- SetSelection(newPos);
- int size = GetCount();
- bool foundPlaying = false;
- for (int i = 0; i < size; i++) {
- GetItem(i)->SetBackgroundColour((i % 2 == 0 ? *m_secondColor : *wxWHITE));
- }
- }
- void TestBox::paintLine(int y)
- {
- wxClientDC dc(this);
- wxSize size = GetSize();
- int width = size.GetWidth();
- dc.SetPen(*wxGREY_PEN);
- dc.SetBrush(*wxGREY_BRUSH);
- dc.DrawLine(0, y, width, y);
- }
- void TestBox::OnLeftDown(wxMouseEvent &event)
- {
- m_isPressing = true;
- if(!m_isDragging) {
- m_lastPos = event.GetPosition();
- }
- event.Skip();
- }
- void TestBox::OnLeftUp(wxMouseEvent &event)
- {
- m_isPressing = false;
- if(m_isDragging) {
- m_isDragging = false;
- int scrollPos = GetScrollPos(wxVERTICAL);
- wxRect rect;
- wxArrayInt listIndex;
- static int itemHeight = -1;
- // If the itemHeight isn't set, set it here and now
- if (itemHeight == -1) {
- GetItemRect(0, rect);
- itemHeight = rect.height;
- }
- GetSelections(listIndex);
- if(listIndex.size() != 1) {
- m_hasScrolled = false;
- return;
- }
- int index = listIndex.Item(0);
- GetItemRect(index, rect);
- int currentY = rect.y;
- wxString item = GetString(index);
- this->Delete(index);
- if(m_hasScrolled) {
- index = scrollPos + ((event.GetY() + itemHeight/2) / itemHeight);
- m_hasScrolled = false;
- } else {
- index = HitTest(event.GetX(), (event.GetY() + itemHeight / 2) -1);
- }
- if (index > GetCount()) {
- index = GetCount();
- }
- int y = itemHeight * (index - scrollPos);
- if(y > currentY) {
- index--;
- }
- appendToList(item, index);
- }
- event.Skip();
- }
- void TestBox::OnMove(wxMouseEvent &event)
- {
- if(!m_isPressing && !m_isDragging) {
- return;
- }
- if (event.m_controlDown || event.m_shiftDown) {
- m_isPressing = false;
- m_isDragging = false;
- Refresh();
- Update();
- return;
- }
- if (!m_isDragging) {
- static const int dragMinX = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
- static const int dragMinY = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
- const wxPoint& pos = event.GetPosition();
- const wxPoint ofs = pos - m_lastPos;
- if ( abs(ofs.x) > dragMinX || abs(ofs.y) > dragMinY) {
- m_isDragging = true;
- }
- } else {
- static int lastY;
- static long lastTimestamp = 0;
- static int itemHeight = -1;
- // If the itemHeight isn't set, set it here and now
- if (itemHeight == -1) {
- wxRect rect;
- GetItemRect(0, rect);
- itemHeight = rect.height;
- }
- int y = event.GetY();
- int index = (y + itemHeight / 2) / itemHeight;
- if (index > GetCount())
- index = GetCount();
- y = itemHeight * index;
- // y > 25000 is a strange behaviour, happens when the cursor is outside the widget
- if (y <= 0 || y > 25000) {
- LineUp();
- m_hasScrolled = true;
- } else if (y >= GetSize().GetHeight()) {
- LineDown();
- m_hasScrolled = true;
- }
- if(event.GetTimestamp() - 60 > lastTimestamp && y != lastY) {
- Refresh();
- Update();
- paintLine(y);
- lastTimestamp = event.GetTimestamp();
- }
- lastY = y;
- return;
- }
- event.Skip();
- }
- class TestFrame : public wxFrame
- {
- public:
- TestFrame(const wxString& title)
- : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(640, 480))
- {
- wxPanel* panel = new wxPanel(this, wxID_ANY);
- TestBox* list = new TestBox(panel, wxID_ANY);
- for(int i = 0; i < 500; i++) {
- list->appendToList(wxString::Format("%d - testtesttesttesttesttesttesttest", i), list->GetCount());
- }
- list->SetSelection(0);
- Centre();
- }
- };
- class TestApp : public wxApp
- {
- public:
- bool OnInit()
- {
- TestFrame* window = new TestFrame("Test");
- SetTopWindow(window);
- window->Centre();
- window->Show();
- return true;
- }
- };
- DECLARE_APP(TestApp);
- IMPLEMENT_APP(TestApp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement