-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyListCtrl.cpp
134 lines (108 loc) · 3.42 KB
/
MyListCtrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/////////////////////////////////////////////////////////////////////////////
// Name: MyListCtrl.cpp
// Purpose: My wxWidgets app
// Author: Imie Nazwisko <[email protected]>
// Created: 2018-01-01
// Copyright: (c) 2018 by Imie Nazwisko
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "MyListCtrl.h"
BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
EVT_LIST_COL_CLICK(wxID_ANY, MyListCtrl::OnSort)
END_EVENT_TABLE()
int wxCALLBACK DoSort(wxIntPtr item1, wxIntPtr item2, MyListCtrl::SortData* data)
{
wxString str1 = data->list->GetItemText(item1, data->column);
wxString str2 = data->list->GetItemText(item2, data->column);
int result = data->list->string_compare_fn(str1, str2);
if (data->direction == MyListCtrl::SORT_DESCENDING) result = -result;
return result;
}
MyListCtrl::MyListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int style)
: wxListCtrl(parent, id, pos, size, style)
{
last_col = -1;
direction = SORT_ASCENDING;
}
void MyListCtrl::OnSort(wxListEvent& event)
{
// Ustawienie kierunku
if (direction == SORT_ASCENDING) direction = SORT_DESCENDING;
else direction = SORT_ASCENDING;
// Ustawienie kolumn
current_col = event.GetColumn();
if (last_col != current_col) direction = SORT_ASCENDING;
// Sortowanie
Sort(current_col, direction);
}
bool MyListCtrl::Sort(int column, int dir)
{
// Testy
if (this->GetColumnCount() == 0) return true;
if (this->GetItemCount() == 0) return true;
if (dir == SORT_UNKNOWN) dir = SORT_ASCENDING;
// ustawienie ikony kierunku sortowania
if (this->GetImageList(wxIMAGE_LIST_SMALL))
{
wxListItem col;
col.SetMask(wxLIST_MASK_IMAGE);
if (column != last_col && last_col != -1)
{
col.SetImage(SORT_UNKNOWN);
this->SetColumn(last_col, col);
}
col.SetImage(dir);
this->SetColumn(column, col);
}
// Aktualizacja zmiennych
last_col = column;
// Sortowanie
SortData *sd = new SortData(this, column, dir);
bool result = this->SortItems((wxListCtrlCompare) DoSort, (wxIntPtr)sd);
delete sd;
// Przeindeksowanie
if (result)
{
for (int i = 0; i < this->GetItemCount(); i++)
{
this->SetItemData(i, i);
}
}
return result;
}
void MyListCtrl::GenerateSampleData(int cols, int rows, int default_icon, bool sort)
{
if (cols == 0 || rows == 0) return;
if (!this->InReportView()) return;
this->ClearAll();
// Ikona g³ównego elementu
int icon = -1;
if (this->GetImageList(wxIMAGE_LIST_SMALL)) icon = default_icon;
// Kolumny
for (int i = 0; i < cols; i++)
{
wxListItem tmp;
tmp.SetText(wxString::Format(wxT("Kolumna %d"), i));
this->InsertColumn(i, tmp);
this->SetColumnWidth(i, 100);
}
// Wiersze
for (int i = 0; i < rows; i++)
{
this->InsertItem(i, wxString::Format(wxT("Wiersz %d"), i), icon);
for (int j = 1; j < cols; j++)
{
this->SetItem(i, j, wxString::Format(wxT("Dane %d,%d"), i, j), -1);
}
}
if (sort)
{
// Indeksowanie
for (int i = 0; i < this->GetItemCount(); i++)
{
this->SetItemData(i, i);
}
// Sortowanie
this->Sort(0, SORT_ASCENDING);
}
}