-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMultilineList.cpp
1608 lines (1356 loc) · 39.6 KB
/
MultilineList.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
/*
* CMultilineList custom control
* Copyright (C) 2006 Dave Calkins (coder1024@gmail.com)
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
*/
#include "stdafx.h"
#include "MultilineList.h"
using namespace std;
// Win32 class name used for the window; if you're using the VS dialog editor you need
// to put this string in the Class property of the custom control
#define MULTILINELIST_CLASSNAME _T("CMultilineList")
// width of the internal borders used between cells/cols/rows
#define GRID_WIDTH 1
// spacing inside cells between the text and the cell borders
#define INNER_PADDING 2
// control IDs for child controls
#define CHILD_ID_HEADERCTRL 101
#define CHILD_ID_SCROLLBAR 102
#define CHILD_ID_SCROLLBARHORZ 103
// header control height is set to be the font height plus the below
#define HEADERCTRL_HEIGHT_EXTRA 6
// # pixels to scroll horizontally when using arrows
#define HORZSCROLL_PIXELS 25
IMPLEMENT_DYNAMIC(CMultilineList, CWnd)
BEGIN_MESSAGE_MAP(CMultilineList, CWnd)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_VSCROLL()
ON_WM_MOUSEWHEEL()
ON_WM_HSCROLL()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
////////////////////////////////////////////////////////////////////////////////
// PUBLIC
////////////////////////////////////////////////////////////////////////////////
CMultilineList::CMultilineList()
: m_nCols(0),
m_nRows(0),
m_viewYPos(0),
m_viewXPos(0),
m_markYPos(0),
m_markXPos(0),
m_curSelRow(-1),
m_IsMarkable(false),
m_nHighlightedRow(0),
m_sel(false),
m_selcol(0),
m_selrow(0)
{
RegisterWindowClass();
}
CMultilineList::~CMultilineList()
{
}
BOOL CMultilineList::Create(CWnd* pParentWnd, const RECT& rect, UINT nID,
DWORD dwStyle /*=WS_VISIBLE*/)
{
return CWnd::Create(MULTILINELIST_CLASSNAME, _T(""), dwStyle, rect,
pParentWnd, nID);
}
void CMultilineList::SetSize(int nCols, int nRows)
{
ASSERT(nCols >= 0);
ASSERT(nRows >= 0);
// if no change, do nothing
if ((nCols == m_nCols) &&
(nRows == m_nRows))
{
return;
}
// if the # cols or # rows is being reduced
if ((nCols < m_nCols) ||
(nRows < m_nRows))
{
// walk through all cells
map<pair<int,int>,Cell>::iterator i;
for (i = m_cells.begin(); i != m_cells.end(); )
{
int col = i->first.first;
int row = i->first.second;
// remove any cells outside the new dimensions
if ((col >= nCols) ||
(row >= nRows))
{
i = m_cells.erase(i);
}
else
{
++i;
}
}
}
// if the # cols is being reduced
if (nCols < m_nCols)
{
// walk through all columns
map<int,Column>::iterator j;
for (j = m_columns.begin(); j != m_columns.end(); )
{
int col = j->first;
// remove any columns outside the new dimensions
if (col >= nCols)
{
j = m_columns.erase(j);
}
else
{
++j;
}
}
// invalidate all row heights
m_rowHeights.clear();
}
// if the # rows is being reduced but # cols is not being reduced
// (since if the # cols was reduced all row heights would have been
// invalidated anyway)
if ((nRows < m_nRows) &&
(nCols >= m_nCols))
{
// walk through the calculated row heights
map<int,int>::iterator k;
for (k = m_rowHeights.begin(); k != m_rowHeights.end(); )
{
int row = k->first;
// remove (invalidate) calculated row heights corresponding to rows which are outside the new dimensions
if (row >= nRows)
{
k = m_rowHeights.erase(k);
}
else
{
++k;
}
}
}
// store new size
m_nCols = nCols;
m_nRows = nRows;
// update
Invalidate(FALSE);
CalculateRowHeights();
UpdateChildControls();
}
void CMultilineList::GetSize(int & nCols, int & nRows)
{
// return current size of the list
nCols = m_nCols;
nRows = m_nRows;
}
void CMultilineList::SetColHeading(int col, LPCTSTR heading)
{
ASSERT(col >= 0);
ASSERT(col < m_nCols);
ASSERT(heading != NULL);
// use existing column object (if there is one) or a new one
Column column;
map<int,Column>::iterator i = m_columns.find(col);
if (i != m_columns.end())
{
column = i->second;
// abort if no change
if (column.m_heading == CString(heading))
return;
}
// set column heading
column.m_heading = heading;
// store the column object back in the map
m_columns[col] = column;
// update
Invalidate(FALSE);
UpdateChildControls();
}
void CMultilineList::SetColWidth(int col, int width)
{
ASSERT(col >= 0);
ASSERT(col < m_nCols);
ASSERT(width >= 0);
// use existing column object (if there is one) or use default
Column column;
map<int,Column>::iterator i = m_columns.find(col);
if (i != m_columns.end())
{
column = i->second;
// abort if no change
if (column.m_width == width)
return;
}
// set column width
column.m_width = width;
// store the column object back in the map
m_columns[col] = column;
// invalidate all row heights
m_rowHeights.clear();
// update
Invalidate(FALSE);
CalculateRowHeights();
UpdateChildControls();
}
void CMultilineList::SetCellText(int col, int row, LPCTSTR text, int align, COLORREF color)
{
ASSERT(col >= 0);
ASSERT(row >= 0);
ASSERT(col < m_nCols);
ASSERT(row < m_nRows);
ASSERT(text != NULL);
// use existing cell object (if there is one) or a new one
Cell cell;
pair<int,int> coord = make_pair(col,row);
map<pair<int,int>,Cell>::iterator i = m_cells.find(coord);
if (i != m_cells.end())
{
cell = i->second;
// abort if no change
if (cell.m_text == CString(text))
return;
}
//set color
cell.m_TextColor = color;
// set cell text
cell.m_text = text;
cell.m_align = align;
// store the cell object back in the map
m_cells[coord] = cell;
// invalidate this row's height
m_rowHeights.erase(row);
// update
Invalidate(FALSE);
CalculateRowHeights();
UpdateChildControls();
}
void CMultilineList::SetCellMark(int col, int row, BOOL mark)
{
ASSERT(col >= 0);
ASSERT(row >= 0);
ASSERT(col < m_nCols);
ASSERT(row < m_nRows);
// use existing cell object (if there is one) or a new one
Cell cell;
pair<int,int> coord = make_pair(col,row);
map<pair<int,int>,Cell>::iterator i = m_cells.find(coord);
if (i != m_cells.end())
{
cell = i->second;
if (cell.m_marked == mark) return; // abort if no change
}
// set cell mark
if(!cell.m_disabled)
{
cell.m_marked = mark;
m_cells[coord] = cell; // store the cell object back in the map
}
// update
Invalidate(FALSE);
CalculateRowHeights();
UpdateChildControls();
}
void CMultilineList::SetCellBg(int col, int row, BOOL mark)
{
ASSERT(col >= 0);
ASSERT(row >= 0);
ASSERT(col < m_nCols);
ASSERT(row < m_nRows);
// use existing cell object (if there is one) or a new one
Cell cell;
pair<int,int> coord = make_pair(col,row);
map<pair<int,int>,Cell>::iterator i = m_cells.find(coord);
if (i != m_cells.end())
{
cell = i->second;
if (cell.m_marked2 == mark) return; // abort if no change
}
// set cell mark
if(!cell.m_disabled)
{
cell.m_marked2 = mark;
m_cells[coord] = cell; // store the cell object back in the map
}
// update
//Invalidate(FALSE);
//CalculateRowHeights();
//UpdateChildControls();
}
CString CMultilineList::GetCellText(int col, int row)
{
ASSERT(col >= 0);
ASSERT(row >= 0);
ASSERT(col < m_nCols);
ASSERT(row < m_nRows);
CString result;
// use existing cell object (if there is one) or use default
Cell cell;
map<pair<int,int>,Cell>::iterator i = m_cells.find(make_pair(col,row));
if (i != m_cells.end())
{
cell = i->second;
}
// return the cell text
result = cell.m_text;
return result;
}
void CMultilineList::SetSelRow(int row)
{
ASSERT(row >= 0);
ASSERT(row < m_nRows);
// force selected row to the specified value
m_curSelRow = row;
// update
Invalidate(FALSE);
UpdateChildControls();
}
int CMultilineList::GetSelRow()
{
// return currently selected row
return m_curSelRow;
}
int CMultilineList::GetRowFromPoint(CPoint pt)
{
int result = -1;
CalculateRowHeights();
// ensure point is inside content area
CRect cr;
GetContentRect(cr);
if (cr.PtInRect(pt))
{
// convert to list coords
int listYPos = m_viewYPos + (pt.y - cr.top);
// walk through the rows until we find one at the specified vertical location
int row = 0;
for (int yPos = 0; row < m_nRows; row++)
{
int thisRowHeight = m_rowHeights[row];
if ((yPos + thisRowHeight) > listYPos)
{
break;
}
else
{
yPos += thisRowHeight;
}
}
// did we find a row?
if (row < m_nRows)
{
result = row;
// //find col-cafvt custom
//for (int col = 0; col < m_nCols; col++)
//{
// Column column;
// std::map<int,Column>::iterator i = m_columns.find(col);
// if (i != m_columns.end()) column = i->second;
// if (pt.x > column.m_width)
// return result;
//}
}
}
return result;
}
void CMultilineList::EnsureRowIsVisible(int row)
{
ASSERT(row >= 0);
ASSERT(row < m_nRows);
// ensure row heights are up to date
CalculateRowHeights();
// find top coord of specified row
int rowYPos = 0;
for (int _row = 0; _row < m_nRows; _row++)
{
if (_row == row)
{
break;
}
else
{
rowYPos += m_rowHeights[_row];
}
}
// height of specified row
int rowHeight = m_rowHeights[row];
// content rect
CRect cr;
GetContentRect(cr);
// top of row is above visible area
if (rowYPos < m_viewYPos)
{
// scroll up so that top of view is the top of the row
m_viewYPos = rowYPos;
// update
Invalidate(FALSE);
UpdateChildControls();
}
// bottom of row is below visible area
else if ((rowYPos + rowHeight) > (m_viewYPos + cr.Height()))
{
// assuming row is smaller than the viewable area,
// scroll down so that bottom of view includes the bottom of the row
if (rowHeight < cr.Height())
{
m_viewYPos += ((rowYPos + rowHeight) - (m_viewYPos + cr.Height()));
}
else
{
// if row is taller than the viewable area, then just scroll so
// the top of the view is at the top of the row
m_viewYPos = rowYPos;
}
// update
Invalidate(FALSE);
UpdateChildControls();
}
}
////////////////////////////////////////////////////////////////////////////////
// PRIVATE
////////////////////////////////////////////////////////////////////////////////
BOOL CMultilineList::RegisterWindowClass()
{
HINSTANCE hInst = AfxGetInstanceHandle();
WNDCLASS wndcls;
ZeroMemory(&wndcls,sizeof(WNDCLASS));
if (!(::GetClassInfo(hInst, MULTILINELIST_CLASSNAME, &wndcls)))
{
wndcls.style = CS_DBLCLKS;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = MULTILINELIST_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
void CMultilineList::PreSubclassWindow()
{
// add a border
ModifyStyle(GetSafeHwnd(),0,WS_BORDER,0);
// KLUDGE - after adding the border above
// this seems to be necessary to get the border
// to initially be drawn
CRect wr;
GetWindowRect(wr);
GetParent()->ScreenToClient(wr);
wr.right -= 1;
MoveWindow(wr);
wr.right += 1;
MoveWindow(wr);
/////////////////////
PrepareOffscreenSurface();
// create GDI objects used in rendering
m_gridPen.CreatePen(PS_SOLID,GRID_WIDTH,RGB(200,220,220));//GetSysColor(COLOR_WINDOWTEXT));
//m_gridPen.CreatePen(PS_SOLID,GRID_WIDTH,GetSysColor(COLOR_WINDOWTEXT));
// create default font matching parent's font
LOGFONT lf;
GetParent()->GetFont()->GetLogFont(&lf);
//memset(&lf, 0x00, sizeof(lf));
//memcpy(lf.lfFaceName, TEXT("Microsoft Sans Serif"), 24);
//lf.lfWeight = FW_BOLD;
m_defaultFont.CreateFontIndirect(&lf);
m_headerFont.CreateFontIndirect(&lf);
// initialize child controls
CreateChildControls();
PositionChildControls();
CWnd::PreSubclassWindow();
}
void CMultilineList::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
PositionChildControls();
UpdateChildControls();
}
BOOL CMultilineList::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR * pNMHDR = (NMHDR*)lParam;
// header control notifications
if (wParam == CHILD_ID_HEADERCTRL)
{
NMHEADER * pNMHEADER = (NMHEADER*)pNMHDR;
// user is resizing a column
if (pNMHDR->code == HDN_TRACK)
{
// use existing column object (if there is one) or use default
Column column;
std::map<int,Column>::iterator i = m_columns.find(pNMHEADER->iItem);
if (i != m_columns.end())
{
column = i->second;
}
// update the width of the column object and store it back in the map
column.m_width = pNMHEADER->pitem->cxy;
m_columns[pNMHEADER->iItem] = column;
// update
Invalidate(FALSE);
UpdateChildControls();
}
// user has finished resizing a column
if (pNMHDR->code == HDN_ENDTRACK)
{
// use existing column object (if there is one) or use default
Column column;
std::map<int,Column>::iterator i = m_columns.find(pNMHEADER->iItem);
if (i != m_columns.end())
{
column = i->second;
}
// update the width of the column object and store it back in the map
column.m_width = pNMHEADER->pitem->cxy;
m_columns[pNMHEADER->iItem] = column;
// invalidate all row heights
m_rowHeights.clear();
// update
Invalidate(FALSE);
CalculateRowHeights();
UpdateChildControls();
}
}
return CWnd::OnNotify(wParam, lParam, pResult);
}
void CMultilineList::CreateChildControls()
{
m_headerCtrl.Create(
CCS_TOP|WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),
this,CHILD_ID_HEADERCTRL);
m_headerCtrl.SetFont(&m_headerFont);
m_scrollBar.Create(
SBS_RIGHTALIGN|SBS_VERT|WS_CHILD|WS_VISIBLE,
CRect(0,0,0,0),this,CHILD_ID_SCROLLBAR);
m_scrollBarHorz.Create(
SBS_BOTTOMALIGN|SBS_HORZ|WS_CHILD|WS_VISIBLE,
CRect(0,0,0,0),this,CHILD_ID_SCROLLBARHORZ);
}
void CMultilineList::PositionChildControls()
{
// ensure the child controls exist
if ((m_headerCtrl.GetSafeHwnd() == NULL) ||
(m_scrollBar.GetSafeHwnd() == NULL) ||
(m_scrollBarHorz.GetSafeHwnd() == NULL))
return;
CRect cr;
GetClientRect(cr);
// get text height
CDC * pDC = GetDC();
CFont * origFont = pDC->SelectObject(&m_headerFont);
TEXTMETRIC tm;
pDC->GetOutputTextMetrics(&tm);
pDC->SelectObject(origFont);
ReleaseDC(pDC);
// header control
CRect hdrRect = cr;
hdrRect.bottom = hdrRect.top + tm.tmHeight + HEADERCTRL_HEIGHT_EXTRA;
m_headerCtrl.MoveWindow(hdrRect);
// scrollbar
CRect sbRect = cr;
sbRect.left = sbRect.right - GetSystemMetrics(SM_CXVSCROLL);
sbRect.top = sbRect.top + GetSystemMetrics(SM_CYHSCROLL);
m_scrollBar.MoveWindow(sbRect);
// horiz scrollbar
CRect sbRectHorz = cr;
sbRectHorz.top = sbRectHorz.bottom - GetSystemMetrics(SM_CYHSCROLL);
if (m_scrollBar.IsWindowVisible())
{
sbRectHorz.right = sbRectHorz.right - GetSystemMetrics(SM_CXVSCROLL);
}
m_scrollBarHorz.MoveWindow(sbRectHorz);
// adjust left edge of the header control to be at the left edge of the list
// this causes the header control to scroll horizontally with the content
if (m_viewXPos > 0)
{
CRect contentRect;
GetContentRect(contentRect);
CRect hr;
m_headerCtrl.GetWindowRect(hr);
ScreenToClient(hr);
hr.left = contentRect.left - m_viewXPos;
m_headerCtrl.MoveWindow(hr);
}
}
void CMultilineList::UpdateChildControls()
{
// get content rect
CRect contentRect;
GetContentRect(contentRect);
// remove excess header control items
while (m_headerCtrl.GetItemCount() > m_nCols)
{
if (!m_headerCtrl.DeleteItem(m_headerCtrl.GetItemCount()-1))
break;
}
// add header control items if needed
while (m_headerCtrl.GetItemCount() < m_nCols)
{
HDITEM hdi;
ZeroMemory(&hdi,sizeof(HDITEM));
hdi.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
hdi.cxy = DEFAULT_COLUMN_WIDTH;
hdi.fmt = HDF_STRING | HDF_CENTER;
hdi.pszText = _T("");
hdi.cchTextMax = 0;
if (m_headerCtrl.InsertItem(m_headerCtrl.GetItemCount(),&hdi) < 0)
break;
}
// adjust left edge of the header control to be at the left edge of the list
// this makes the header control scroll horizontally with the list content
CRect hr;
m_headerCtrl.GetWindowRect(hr);
ScreenToClient(hr);
hr.left = contentRect.left - m_viewXPos;
m_headerCtrl.MoveWindow(hr);
// for each header control item
for (int col = 0; col < m_nCols; col++)
{
// use existing object (if there is one) or default
Column column;
map<int,Column>::iterator i = m_columns.find(col);
if (i != m_columns.end())
{
column = i->second;
}
HDITEM hdi;
ZeroMemory(&hdi,sizeof(HDITEM));
hdi.mask = HDI_TEXT | HDI_WIDTH;
// get current item attributes
if (m_headerCtrl.GetItem(col,&hdi))
{
// if different, update header control item
CString curHeading(hdi.pszText);
if ((curHeading != column.m_heading) ||
(hdi.cxy != column.m_width))
{
hdi.pszText = (LPTSTR)((LPCTSTR)(column.m_heading));
hdi.cchTextMax = column.m_heading.GetLength();
hdi.cxy = column.m_width;
m_headerCtrl.SetItem(col, &hdi);
}
}
}
// compute height of the entire list
int totalHeight = 0;
map<int,int>::iterator iRowHeights;
for (iRowHeights = m_rowHeights.begin(); iRowHeights != m_rowHeights.end(); ++iRowHeights)
{
totalHeight += iRowHeights->second;
}
// should the vert scrollbar be enabled?
if (totalHeight <= contentRect.Height())
{
// no, ensure its disabled
if (m_scrollBar.IsWindowEnabled())
{
m_scrollBar.EnableWindow(FALSE);
m_scrollBar.ShowWindow(SW_HIDE);
}
}
else
{
// ensure its enabled
if (!m_scrollBar.IsWindowEnabled())
{
m_scrollBar.EnableWindow(TRUE);
m_scrollBar.ShowWindow(SW_SHOW);
}
// correct values for scrollbar
int nMin = 0;
int nMax = totalHeight;
int nPage = contentRect.Height();
int nPos = m_viewYPos;
// get current scrollbar values
SCROLLINFO si;
ZeroMemory(&si,sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
m_scrollBar.GetScrollInfo(&si);
// if any of the values are different, update scrollbar
if ((nMin != si.nMin) ||
(nMax != si.nMax) ||
(nPage != si.nPage) ||
(nPos != si.nPos))
{
si.nMin = nMin;
si.nMax = nMax;
si.nPage = nPage;
si.nPos = nPos;
m_scrollBar.SetScrollInfo(&si);
}
}
// compute total width of the list
int totalWidth = 0;
for (int col = 0; col < m_nCols; col++)
{
// use existing object (if there is one) or default
Column column;
map<int,Column>::iterator iColumn = m_columns.find(col);
if (iColumn != m_columns.end())
{
column = iColumn->second;
}
totalWidth += column.m_width;
}
// should horz scrollbar be enabled?
if (totalWidth <= contentRect.Width())
{
// ensure its disabled and hidden
if (m_scrollBarHorz.IsWindowEnabled())
{
m_scrollBarHorz.EnableWindow(FALSE);
m_scrollBarHorz.ShowWindow(SW_HIDE);
}
}
else
{
// ensure its enabled and visible
if (!m_scrollBarHorz.IsWindowEnabled())
{
m_scrollBarHorz.EnableWindow(TRUE);
m_scrollBarHorz.ShowWindow(SW_SHOW);
}
// correct values for scrollbar
int nMin = 0;
int nMax = totalWidth;
int nPage = contentRect.Width();
int nPos = m_viewXPos;
// get current scrollbar values
SCROLLINFO si;
ZeroMemory(&si,sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
m_scrollBarHorz.GetScrollInfo(&si);
// if any of the values are different, update scrollbar
if ((nMin != si.nMin) ||
(nMax != si.nMax) ||
(nPage != si.nPage) ||
(nPos != si.nPos))
{
si.nMin = nMin;
si.nMax = nMax;
si.nPage = nPage;
si.nPos = nPos;
m_scrollBarHorz.SetScrollInfo(&si);
}
}
}
void CMultilineList::CalculateRowHeights()
{
CFont * origFont = m_offscreenDC.SelectObject(&m_defaultFont);
CBitmap * origBitmap = m_offscreenDC.SelectObject(&m_offscreenBitmap);
// for all rows
for (int row = 0; row < m_nRows; row++)
{
// if a row height is not already available for this row
map<int,int>::iterator iRow = m_rowHeights.find(row);
if (iRow == m_rowHeights.end())
{
int rowHeight = 0;
// compute heights of each cell, finding the tallest one
for (int col = 0; col < m_nCols; col++)
{
// get column object (if one exists) or use the default
Column column;
std::map<int,Column>::iterator iCol = m_columns.find(col);
if (iCol != m_columns.end())
{
column = iCol->second;
}
// get cell object (if one exists) or use the default
Cell cell;
map<pair<int,int>,Cell>::iterator iCell = m_cells.find(make_pair(col,row));
if (iCell != m_cells.end())
{
cell = iCell->second;
}
CRect textRect(0,0,column.m_width,0);
textRect.DeflateRect(INNER_PADDING,INNER_PADDING);
int origRight = textRect.right;
// compute cell height
m_offscreenDC.DrawTextEx(cell.m_text,textRect,
DT_CALCRECT|DT_LEFT|DT_NOPREFIX|DT_TOP|DT_WORDBREAK,
NULL);
textRect.right = origRight;
textRect.InflateRect(INNER_PADDING,INNER_PADDING);
textRect.bottom += GRID_WIDTH;
if (textRect.bottom > rowHeight)
{
rowHeight = textRect.bottom;
}
}
// store row height
m_rowHeights[row] = rowHeight;
}
}
m_offscreenDC.SelectObject(origBitmap);
m_offscreenDC.SelectObject(origFont);
}
void CMultilineList::GetContentRect(CRect & r)
{
// get geom of client area and child controls
CRect cr;
GetClientRect(cr);
CRect hdrRect;
m_headerCtrl.GetClientRect(hdrRect);
CRect sbRect;
m_scrollBar.GetClientRect(sbRect);
CRect sbRectHorz;
m_scrollBarHorz.GetClientRect(sbRectHorz);
// shrink for child controls
r = cr;
r.top += hdrRect.Height();
r.right -= sbRect.Width();
r.bottom -= sbRectHorz.Height();
// vert scroll bar not visible; extend area to cover where it would be
if (!m_scrollBar.IsWindowVisible())
{
r.right += sbRect.Width();
}
// horz scroll bar not visible; extend area to cover where it would be
if (!m_scrollBarHorz.IsWindowVisible())
{
r.bottom += sbRectHorz.Height();
}
}
BOOL CMultilineList::OnEraseBkgnd(CDC* pDC)
{
// do nothing; we don't want to erase the background because we
// use double-buffering and so overwrite the entire client area
// with every repaint
return TRUE;
}