-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.cpp
2281 lines (1937 loc) · 56.8 KB
/
Table.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
MeOS - Orienteering Software
Copyright (C) 2009-2015 Melin Software HB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Melin Software HB - [email protected] - www.melin.nu
Stigbergsvägen 7, SE-75242 UPPSALA, Sweden
************************************************************************/
#include "stdafx.h"
#include "Table.h"
#include "gdioutput.h"
#include "meos_util.h"
#include <exception>
#include <algorithm>
#include <set>
#include "oEvent.h"
#include "localizer.h"
#include <cassert>
#include "gdiconstants.h"
#include "meosexception.h"
extern HINSTANCE hInst;
const char *tId="_TABLE_SEL";
const Table *TableSortIndex::table = 0;
Table::Table(oEvent *oe_, int rowH,
const string &name, const string &iName)
{
commandLock = false;
oe=oe_;
tableName=name;
internalName = iName;
nTitles=0;
PrevSort=-1;
baseRowHeight=rowH;
rowHeight = 0;
highRow=-1;
highCol=-1;
colSelected=-1;
editRow=-1;
editCol=-1;
drawFilterLabel=false;
currentSortColumn=-1;
hEdit=0;
hdcCompatible=0;
hbmStored=0;
hdcCompatibleCell = 0;
hbmStoredCell = 0;
partialCell = false;
startSelect = false;
ownerCounter = 0;
clearCellSelection(0);
tableProp = -1;
dataPointer = -1;
clearOnHide = true;
doAutoSelectColumns = true;
generator = 0;
generatorPtr = 0;
}
Table::~Table(void)
{
assert(ownerCounter == 0);
if (hEdit)
DestroyWindow(hEdit);
}
void Table::releaseOwnership() {
ownerCounter--;
if (ownerCounter == 0)
delete this;
}
void Table::clearCellSelection(gdioutput *gdi) {
upperRow = -1;
lowerRow = -1;
upperCol = -1;
lowerCol = -1;
if (gdi) {
HDC hDC = GetDC(gdi->getTarget());
clearSelectionBitmap(gdi, hDC);
ReleaseDC(gdi->getTarget(), hDC);
}
}
int Table::addColumn(const string &Title, int width, bool isnum, bool formatRight) {
ColInfo ri;
strcpy_s(ri.name, lang.tl(Title).c_str());
ri.baseWidth = width;
ri.width = 0;
ri.padWidthZeroSort = 0;
ri.isnumeric = isnum;
ri.formatRight = formatRight;
Titles.push_back(ri);
columns.push_back(nTitles);
nTitles++;
return Titles.size()-1;
}
int Table::addColumnPaddedSort(const string &title, int width, int padding, bool formatRight) {
ColInfo ri;
strcpy_s(ri.name, lang.tl(title).c_str());
ri.baseWidth = width;
ri.width = 0;
ri.padWidthZeroSort = padding;
ri.isnumeric = false;
ri.formatRight = formatRight;
Titles.push_back(ri);
columns.push_back(nTitles);
nTitles++;
return Titles.size()-1;
}
void Table::moveColumn(int src, int target)
{
if (src==target)
return;
vector<int>::iterator it_s=find(columns.begin(), columns.end(), src);
vector<int>::iterator it_t=find(columns.begin(), columns.end(), target);
if (it_s!=columns.end()) {
if (it_s<it_t) {
columns.erase(it_s);
it_t=find(columns.begin(), columns.end(), target);
if (it_t!=columns.end())
++it_t;
columns.insert(it_t, src);
}
else {
columns.erase(it_s);
it_t=find(columns.begin(), columns.end(), target);
columns.insert(it_t, src);
}
}
}
void Table::reserve(size_t siz) {
Data.reserve(siz+3);
sortIndex.reserve(siz+3);
idToRow.resize(siz+3);
}
TableRow *Table::getRowById(int rowId) {
int ix;
if (idToRow.lookup(rowId, ix)) {
return &Data[ix];
}
return 0;
}
void Table::addRow(int rowId, oBase *object)
{
int ix;
if (rowId>0 && idToRow.lookup(rowId, ix)) {
dataPointer = ix;
return;
}
TableRow tr(nTitles, object);
tr.height=rowHeight;
tr.id = rowId;
TableSortIndex tsi;
if (Data.empty()) {
sortIndex.clear();
tsi.index=0;
sortIndex.push_back(tsi);
tsi.index=1;
sortIndex.push_back(tsi);
tsi.index=2;
Data.resize(2, tr);
for (unsigned i=0;i<nTitles;i++) {
Data[0].cells[i].contents=Titles[i].name;
Data[1].cells[i].contents="...";
Data[0].cells[i].canEdit=false;
Data[0].cells[i].type=cellEdit;
Data[0].cells[i].owner=0;
Data[1].cells[i].canEdit=false;
Data[1].cells[i].type=cellEdit;
Data[1].cells[i].owner=0;
}
}
else {
//tsi.index=sortIndex.size();
tsi.index = Data.size();
}
if (rowId>0)
idToRow[rowId]=Data.size();
dataPointer = Data.size();
sortIndex.push_back(tsi);
Data.push_back(tr);
}
void Table::set(int column, oBase &owner, int id, const string &data, bool canEdit, CellType type)
{
if (dataPointer >= Data.size() || dataPointer<2)
throw std::exception("Internal table error: wrong data pointer");
TableRow &row=Data[dataPointer];
TableCell &cell=row.cells[column];
cell.contents=data;
cell.owner=&owner;
cell.id=id;
cell.canEdit=canEdit;
cell.type=type;
}
void Table::filter(int col, const string &filt, bool forceFilter)
{
const string &oldFilter=Titles[col].filter;
vector<TableSortIndex> baseIndex;
if (filt==oldFilter && (!forceFilter || filt.empty()))
return;
else if (strncmp(oldFilter.c_str(), filt.c_str(), oldFilter.length())==0) {
//Filter more...
baseIndex.resize(2);
baseIndex[0]=sortIndex[0];
baseIndex[1]=sortIndex[1];
swap(baseIndex, sortIndex);
Titles[col].filter=filt;
}
else {
//Filter less -> refilter all!
Titles[col].filter=filt;
sortIndex.resize(Data.size());
for (size_t k=0; k<Data.size();k++) {
sortIndex[k].index = k;
}
for (unsigned k=0;k<nTitles;k++) {
filter(k, Titles[k].filter, true);
}
PrevSort = -1;
return;
}
char filt_lc[1024];
strcpy_s(filt_lc, filt.c_str());
CharLowerBuff(filt_lc, filt.length());
sortIndex.resize(2);
for (size_t k=2;k<baseIndex.size();k++) {
if (filterMatchString(Data[baseIndex[k].index].cells[col].contents, filt_lc))
sortIndex.push_back(baseIndex[k]);
}
}
bool Table::compareRow(int indexA, int indexB) const {
const TableRow &a = Data[indexA];
const TableRow &b = Data[indexB];
if (a.intKey != b.intKey)
return a.intKey < b.intKey;
else
return Data[indexA].key < Data[indexB].key;
}
void Table::sort(int col)
{
bool reverse = col < 0;
if (col < 0)
col = -(10+col);
if (sortIndex.size()<2)
return;
currentSortColumn=col;
if (PrevSort!=col && PrevSort!=-(10+col)) {
if (Titles[col].isnumeric) {
bool hasDeci = false;
for(size_t k=2; k<sortIndex.size(); k++){
Data[sortIndex[k].index].key.clear();
const char *str = Data[sortIndex[k].index].cells[col].contents.c_str();
int i = 0;
while (str[i] != 0 && str[i] != ':' && str[i] != ',' && str[i] != '.')
i++;
if (str[i]) {
hasDeci = true;
break;
}
i = 0;
while (str[i] != 0 && (str[i] < '0' || str[i] > '9'))
i++;
Data[sortIndex[k].index].intKey = atoi(str + i);
}
if (hasDeci) { // Times etc.
for(size_t k=2; k<sortIndex.size(); k++){
Data[sortIndex[k].index].key.clear();
const char *str = Data[sortIndex[k].index].cells[col].contents.c_str();
int i = 0;
while (str[i] != 0 && (str[i] < '0' || str[i] > '9'))
i++;
int key = 0;
while (str[i] >= '0' && str[i] <= '9') {
key = key * 10 + (str[i] - '0');
i++;
}
if (str[i] == ':' || str[i]==',' || str[i] == '.' || (str[i] == '-' && key != 0)) {
bool valid = true;
for (int j = 1; j <= 4; j++) {
if (valid && str[i+j] >= '0' && str[i+j] <= '9')
key = key * 10 + (str[i+j] - '0');
else {
key *= 10;
valid = false;
}
}
}
else {
key *= 10000;
}
Data[sortIndex[k].index].intKey = key;
}
}
}
else {
if (Titles[col].padWidthZeroSort) {
for (size_t k=2; k<sortIndex.size(); k++) {
string &key = Data[sortIndex[k].index].key;
const string &contents = Data[sortIndex[k].index].cells[col].contents;
if (contents.length() < unsigned(Titles[col].padWidthZeroSort)) {
key.resize(Titles[col].padWidthZeroSort+1);
int cl = Titles[col].padWidthZeroSort-contents.length();
for (int i = 0; i < cl; i++)
key[i] = '0';
for (int i = cl; i < Titles[col].padWidthZeroSort; i++)
key[i] = contents[i-cl];
}
else
key = contents;
//key = Data[sortIndex[k].index].cells[col].contents;
const BYTE *strBuff = (const BYTE *)key.c_str();
CharUpperBuff(LPSTR(strBuff), key.size());
int &intKey = Data[sortIndex[k].index].intKey;
intKey = unsigned(strBuff[0])<<16;
if (key.length() > 1) {
intKey |= unsigned(strBuff[1])<<8;
intKey |= unsigned(strBuff[2]);
}
}
}
else {
for (size_t k=2; k<sortIndex.size(); k++) {
string &key = Data[sortIndex[k].index].key;
key = Data[sortIndex[k].index].cells[col].contents;
const BYTE *strBuff = (const BYTE *)key.c_str();
CharUpperBuff(LPSTR(strBuff), key.size());
int &intKey = Data[sortIndex[k].index].intKey;
intKey = unsigned(strBuff[0])<<16;
if (key.length() > 1) {
intKey |= unsigned(strBuff[1])<<8;
intKey |= unsigned(strBuff[2]);
}
}
}
}
assert(TableSortIndex::table == 0);
TableSortIndex::table = this;
//DWORD sStart = GetTickCount();
std::stable_sort(sortIndex.begin()+2, sortIndex.end());
//DWORD sEnd = GetTickCount();
//string st = itos(sEnd-sStart);
TableSortIndex::table = 0;
PrevSort=col;
if (reverse)
std::reverse(sortIndex.begin()+2, sortIndex.end());
}
else {
std::reverse(sortIndex.begin()+2, sortIndex.end());
if (PrevSort==col)
PrevSort=-(10+col);
else
PrevSort=col;
}
}
int TablesCB(gdioutput *gdi, int type, void *data)
{
if (type!=GUI_LINK || gdi->Tables.empty())
return 0;
TableInfo &tai=gdi->Tables.front();
Table *t=tai.table;
TextInfo *ti=(TextInfo *)data;
if (ti->id.substr(0,4)=="sort"){
int col=atoi(ti->id.substr(4).c_str());
t->sort(col);
}
gdi->refresh();
//gdi->Restore();
//t->Render(*gdi);
//*/
return 0;
}
void Table::getDimension(gdioutput &gdi, int &dx, int &dy, bool filteredResult) const
{
rowHeight = gdi.scaleLength(baseRowHeight);
if (filteredResult)
dy = rowHeight * (1+sortIndex.size());
else
dy = rowHeight * (1+max<size_t>(2,Data.size()));
dx=1;
for(size_t i=0;i<columns.size();i++) {
Titles[columns[i]].width = gdi.scaleLength(Titles[columns[i]].baseWidth);
dx += Titles[columns[i]].width + 1;
}
}
void Table::redrawCell(gdioutput &gdi, HDC hDC, int c, int r)
{
if (unsigned(r)<Data.size() && unsigned(c)<unsigned(nTitles)) {
const TableRow &row=Data[r];
const TableCell &cell=row.cells[c];
RECT rc=cell.absPos;
draw(gdi, hDC, table_xp+gdi.OffsetX, table_yp+gdi.OffsetY, rc);
}
}
void Table::startMoveCell(HDC hDC, const TableCell &cell)
{
RECT rc=cell.absPos;
int cx=rc.right-rc.left+1;
int cy=rc.bottom-rc.top;
hdcCompatible = CreateCompatibleDC(hDC);
hbmStored = CreateCompatibleBitmap(hDC, cx, cy);
// Select the bitmaps into the compatible DC.
SelectObject(hdcCompatible, hbmStored);
lastX=rc.left;
lastY=rc.top;
BitBlt(hdcCompatible, 0, 0, cx, cy, hDC, lastX, lastY, SRCCOPY);
}
void Table::restoreCell(HDC hDC, const TableCell &cell)
{
const RECT &rc=cell.absPos;
int cx=rc.right-rc.left+1;
int cy=rc.bottom-rc.top;
if (hdcCompatible) {
//Restore bitmap
BitBlt(hDC, lastX, lastY, cx, cy, hdcCompatible, 0, 0, SRCCOPY);
}
}
void Table::moveCell(HDC hDC, gdioutput &gdi, const TableCell &cell, int dx, int dy)
{
RECT rc=cell.absPos;
rc.left+=dx; rc.right+=dx;
rc.top+=dy; rc.bottom+=dy;
int cx=rc.right-rc.left+1;
int cy=rc.bottom-rc.top;
if (hdcCompatible) {
lastX=rc.left;
lastY=rc.top;
//Take new
BitBlt(hdcCompatible, 0, 0, cx, cy, hDC, lastX, lastY , SRCCOPY);
}
else {
startMoveCell(hDC, cell);
dx=0;
dy=0;
}
highlightCell(hDC, gdi, cell, RGB(255, 0,0), dx, dy);
}
void Table::stopMoveCell(HDC hDC, const TableCell &cell, int dx, int dy)
{
if (hdcCompatible) {
restoreCell(hDC, cell);
/* RECT rc=cell.absPos;
rc.left+=dx; rc.right+=dx;
rc.top+=dy; rc.bottom+=dy;
int cx=rc.right-rc.left+2;
int cy=rc.bottom-rc.top+2;
//Restore bitmap
BitBlt(hDC, lastX, lastY, cx, cy, hdcCompatible, 0, 0, SRCCOPY);
*/
DeleteDC(hdcCompatible);
hdcCompatible=0;
}
if (hbmStored) {
DeleteObject(hbmStored);
hbmStored=0;
}
}
bool Table::mouseMove(gdioutput &gdi, int x, int y)
{
int row=getRow(y);
int col=-1;
if (row!=-1)
col=getColumn(x);
HWND hWnd=gdi.getTarget();
if (colSelected!=-1) {
TableCell &cell=Data[0].cells[colSelected];
HDC hDC=GetDC(hWnd);
restoreCell(hDC, cell);
if (col!=highCol) {
if (unsigned(highRow)<Data.size() && unsigned(highCol)<nTitles)
redrawCell(gdi, hDC, highCol, highRow);
DWORD c=RGB(240, 200, 140);
if (unsigned(col)<nTitles)
highlightCell(hDC, gdi, Data[0].cells[col], c, 0,0);
}
//highlightCell(hDC, cell, RGB(255,0,0), x-startX, y-startY);
moveCell(hDC, gdi, cell, x-startX, y-startY);
ReleaseDC(hWnd, hDC);
highRow=0;
highCol=col;
return false;
}
RECT rc;
GetClientRect(hWnd, &rc);
if (x<=rc.left || x>=rc.right || y<rc.top || y>rc.bottom)
row=-1;
bool ret = false;
if (startSelect) {
int c = getColumn(x, true);
if (c != -1)
upperCol = c;
c = getRow(y, true);
if (c != -1 && c>=0) {
upperRow = max<int>(c, 2);
}
HDC hDC=GetDC(hWnd);
if (unsigned(highRow)<Data.size() && unsigned(highCol)<Titles.size())
redrawCell(gdi, hDC, highCol, highRow);
highRow = -1;
drawSelection(gdi, hDC, false);
ReleaseDC(hWnd, hDC);
scrollToCell(gdi, upperRow, upperCol);
ret = true;
}
else if (row>=0 && col>=0) {
POINT pt = {x, y};
ClientToScreen(hWnd, &pt);
HWND hUnder = WindowFromPoint(pt);
if (hUnder == hWnd) {
//int index=sortIndex[row].index;
TableRow &trow=Data[row];
TableCell &cell=trow.cells[col];
if (highRow!=row || highCol!=col) {
HDC hDC=GetDC(hWnd);
if (unsigned(highRow)<Data.size() && unsigned(highCol)<Titles.size())
redrawCell(gdi, hDC, highCol, highRow);
if (row >= 2) {
DWORD c;
if (cell.canEdit)
c=RGB(240, 240, 150);
else
c=RGB(240, 200, 140);
highlightCell(hDC, gdi, cell, c, 0,0);
}
ReleaseDC(hWnd, hDC);
SetCapture(hWnd);
highCol=col;
highRow=row;
}
ret = true;
}
}
if (ret)
return true;
if (unsigned(highRow)<Data.size() && unsigned(highCol)<Titles.size()) {
ReleaseCapture();
HDC hDC=GetDC(hWnd);
redrawCell(gdi, hDC, highCol, highRow);
ReleaseDC(hWnd, hDC);
highRow=-1;
}
return false;
}
bool Table::mouseLeftUp(gdioutput &gdi, int x, int y)
{
if (colSelected!=-1) {
if (hdcCompatible) {
TableCell &cell=Data[0].cells[colSelected];
HWND hWnd=gdi.getTarget();
HDC hDC=GetDC(hWnd);
stopMoveCell(hDC, cell, x-startX, y-startY);
ReleaseDC(hWnd, hDC);
//return true;
}
if (highRow==0 && colSelected==highCol) {
colSelected=-1;
gdi.setWaitCursor(true);
sort(highCol);
gdi.setWaitCursor(false);
gdi.refresh();
mouseMove(gdi, x, y);
return true;
}
else {
moveColumn(colSelected, highCol);
InvalidateRect(gdi.getTarget(), 0, false);
colSelected=-1;
return true;
}
}
else {
upperCol = getColumn(x);
upperRow = getRow(y);
startSelect = false;
ReleaseCapture();
}
colSelected=-1;
return false;
}
int tblSelectionCB(gdioutput *gdi, int type, void *data)
{
if (type == GUI_LISTBOX) {
ListBoxInfo lbi = *static_cast<ListBoxInfo *>(data);
//TableCell &cell = *static_cast<TableCell *>(lbi.getExtra());
Table &t = *static_cast<Table *>(lbi.getExtra());
t.selection(*gdi, lbi.text, lbi.data);
}
return 0;
}
void Table::selection(gdioutput &gdi, const string &text, int data) {
if (size_t(selectionRow) >= Data.size() || size_t(selectionCol) >= Titles.size())
throw std::exception("Index out of bounds.");
TableCell &cell = Data[selectionRow].cells[selectionCol];
int id = Data[selectionRow].id;
string output = cell.contents;
cell.owner->inputData(cell.id, text, data, output, false);
cell.contents = output;
reloadRow(id);
RECT rc;
getRowRect(selectionRow, rc);
InvalidateRect(gdi.getTarget(), &rc, false);
}
#ifndef MEOSDB
bool Table::keyCommand(gdioutput &gdi, KeyCommandCode code) {
if (commandLock)
return false;
commandLock = true;
try {
if (code == KC_COPY)
exportClipboard(gdi);
else if (code == KC_PASTE) {
importClipboard(gdi);
}else if (code == KC_DELETE) {
deleteSelection(gdi);
}
else if (code == KC_REFRESH) {
gdi.setWaitCursor(true);
update();
autoAdjust(gdi);
gdi.refresh();
}
else if (code == KC_INSERT) {
insertRow(gdi);
gdi.refresh();
}
else if (code == KC_PRINT) {
gdioutput gdiPrint("temp", gdi.getScale(), gdi.getEncoding());
gdiPrint.clearPage(false);
gdiPrint.print(getEvent(), this);
}
}
catch (...) {
commandLock = false;
throw;
}
commandLock = false;
return false;
}
#endif
bool Table::deleteSelection(gdioutput &gdi) {
int r1, r2;
getRowRange(r1, r2);
if (r1 != -1 && r2 != -1 && r1<=r2) {
if (!gdi.ask("Vill du radera X rader från tabellen?#" + itos(r2-r1+1)))
return false;
gdi.setWaitCursor(true);
int failed = deleteRows(r1, r2);
gdi.refresh();
gdi.setWaitCursor(false);
if (failed > 0)
gdi.alert("X rader kunde inte raderas.#" + itos(failed));
}
return true;
}
void Table::hide(gdioutput &gdi) {
try {
destroyEditControl(gdi);
}
catch (std::exception &ex) {
gdi.alert(ex.what());
}
clearCellSelection(0);
ReleaseCapture();
if (clearOnHide) {
Data.clear();
sortIndex.clear();
idToRow.clear();
clear();
}
}
void Table::clear() {
Data.clear();
sortIndex.clear();
idToRow.clear();
}
bool Table::destroyEditControl(gdioutput &gdi) {
colSelected=-1;
if (hEdit) {
try {
if (!enter(gdi))
return false;
}
catch (std::exception &) {
if (hEdit) {
DestroyWindow(hEdit);
hEdit=0;
}
throw;
}
if (hEdit) {
DestroyWindow(hEdit);
hEdit=0;
}
}
gdi.removeControl(tId);
if (drawFilterLabel) {
drawFilterLabel=false;
gdi.refresh();
}
return true;
}
bool Table::mouseLeftDown(gdioutput &gdi, int x, int y) {
clearCellSelection(&gdi);
if (!destroyEditControl(gdi))
return false;
if (highRow==0) {
colSelected=highCol;
startX=x;
startY=y;
//sort(highCol);
//gdi.refresh();
//mouseMove(gdi, x, y);
}
else if (highRow==1) {
//filter(highCol, "lots");
RECT rc=Data[1].cells[columns[0]].absPos;
//rc.right=rc.left+tableWidth;
editRow=highRow;
editCol=highCol;
hEdit=CreateWindowEx(0, "EDIT", Titles[highCol].filter.c_str(),
WS_TABSTOP|WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL|WS_BORDER,
rc.left+105, rc.top, tableWidth-105, (rc.bottom-rc.top-1), gdi.getTarget(),
0, hInst, 0);
drawFilterLabel=true;
SendMessage(hEdit, EM_SETSEL, 0, -1);
SetFocus(hEdit);
SendMessage(hEdit, WM_SETFONT, (WPARAM) gdi.getGUIFont(), 0);
gdi.refresh();
}
else {
SetFocus(gdi.getHWND());
SetCapture(gdi.getTarget());
lowerCol = getColumn(x);
lowerRow = getRow(y);
startSelect = true;
}
return false;
}
bool Table::mouseLeftDblClick(gdioutput &gdi, int x, int y)
{
clearCellSelection(&gdi);
if (!destroyEditControl(gdi))
return false;
if (unsigned(highRow)>=Data.size() || unsigned(highCol)>=Titles.size()) {
return false;
}
if (highRow != 0 && highRow != 1) {
if (editCell(gdi, highRow, highCol))
return true;
}
return false;
}
bool Table::editCell(gdioutput &gdi, int row, int col) {
TableCell &cell = Data[row].cells[col];
if (cell.type == cellAction) {
ReleaseCapture();
gdi.makeEvent("CellAction", internalName, cell.id, cell.owner, false);
return true;
}
if (!cell.canEdit) {
MessageBeep(-1);
return true;
}
ReleaseCapture();
editRow = row;
editCol = col;
RECT &rc=cell.absPos;
if (cell.type == cellSelection || cell.type == cellCombo) {
selectionRow = row;
selectionCol = col;
vector< pair<string, size_t> > out;
size_t selected = 0;
cell.owner->fillInput(cell.id, out, selected);
int width = 40;
for (size_t k = 0; k<out.size(); k++)
width = max<int>(width, 8*out[k].first.length());
if (cell.type == cellSelection) {
gdi.addSelection(rc.left+gdi.OffsetX, rc.top+gdi.OffsetY, tId,
max<int>(int((rc.right-rc.left+1)/gdi.scale), width), (rc.bottom-rc.top)*10,
tblSelectionCB).setExtra(this);
}
else {
gdi.addCombo(rc.left+gdi.OffsetX, rc.top+gdi.OffsetY, tId,
max<int>(int((rc.right-rc.left+1)/gdi.scale), width), (rc.bottom-rc.top)*10,
tblSelectionCB).setExtra(this);
}
gdi.addItem(tId, out);
gdi.selectItemByData(tId, selected);
return true;
}
else if (cell.type==cellEdit) {
hEdit=CreateWindowEx(0, "EDIT", cell.contents.c_str(),
WS_TABSTOP|WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL|WS_BORDER,
rc.left, rc.top, rc.right-rc.left+1, (rc.bottom-rc.top), gdi.getTarget(),
0, hInst, 0);
SendMessage(hEdit, EM_SETSEL, 0, -1);
SetFocus(hEdit);
SendMessage(hEdit, WM_SETFONT, (WPARAM) gdi.getGUIFont(), 0);
return true;
}
return false;
}
RGBQUAD RGBA(BYTE r, BYTE g, BYTE b, BYTE a) {
RGBQUAD q = {b,g,r,a};
return q;
}
RGBQUAD RGBA(DWORD c) {
RGBQUAD q = {GetBValue(c),GetGValue(c),GetRValue(c),0};
return q;
}
RGBQUAD transform(RGBQUAD src, double scale) {
src.rgbRed = BYTE(min(src.rgbRed * scale, 255.0));
src.rgbGreen = BYTE(min(src.rgbGreen * scale, 255.0));
src.rgbBlue = BYTE(min(src.rgbBlue * scale, 255.0));
return src;
}
void gradRect(HDC hDC, int left, int top, int right, int bottom,
bool horizontal, RGBQUAD c1, RGBQUAD c2) {
TRIVERTEX vert[2];
vert [0] .x = left;
vert [0] .y = top;
vert [0] .Red = 0xff00 & (DWORD(c1.rgbRed)<<8);
vert [0] .Green = 0xff00 & (DWORD(c1.rgbGreen)<<8);
vert [0] .Blue = 0xff00 & (DWORD(c1.rgbBlue)<<8);
vert [0] .Alpha = 0xff00 & (DWORD(c1.rgbReserved)<<8);
vert [1] .x = right;
vert [1] .y = bottom;
vert [1] .Red = 0xff00 & (DWORD(c2.rgbRed)<<8);
vert [1] .Green = 0xff00 & (DWORD(c2.rgbGreen)<<8);
vert [1] .Blue = 0xff00 & (DWORD(c2.rgbBlue)<<8);
vert [1] .Alpha = 0xff00 & (DWORD(c2.rgbReserved)<<8);
GRADIENT_RECT gr[1];
gr[0].UpperLeft=0;
gr[0].LowerRight=1;
if (horizontal)
GradientFill(hDC,vert, 2, gr, 1,GRADIENT_FILL_RECT_H);
else
GradientFill(hDC,vert, 2, gr, 1,GRADIENT_FILL_RECT_V);
}
void Table::initEmpty() {
if (Data.empty()) {
addRow(0,0);
Data.resize(2, TableRow(nTitles,0));
sortIndex.resize(2);
}