summaryrefslogtreecommitdiff
path: root/library/CustomGrid.cpp
blob: 93f2107a35541ff954c9866143784b5be73c88e3 (plain)
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
#include "customGrid.h"
#include "globalFunctions.h"
#include "resources.h"
#include <wx/dc.h>

const unsigned int MinimumRows = 15;

//class containing pure grid data: basically the same as wxGridStringTable, but adds cell formatting
class CustomGridTableBase : public wxGridStringTable
{
public:
    CustomGridTableBase(int numRows, int numCols) :
            wxGridStringTable(numRows, numCols),
            lightBlue(80, 110, 255),
            gridIdentifier(0),
            gridRefUI(0),
            gridData(0),
            lastNrRows(MinimumRows) {}

    ~CustomGridTableBase() {}


    void setGridDataTable(GridView* gridRefUI, FileCompareResult* gridData)
    {
        this->gridRefUI = gridRefUI;
        this->gridData  = gridData;
    }

    void SetGridIdentifier(int id)
    {
        gridIdentifier = id;
    }

//###########################################################################
//grid standard input output methods, redirected directly to gridData to improve performance

    virtual int GetNumberRows()
    {
        if (gridRefUI)
            return max(gridRefUI->size(), MinimumRows);
        return MinimumRows; //grid is initialized with this number of rows
    }

    virtual bool IsEmptyCell( int row, int col )
    {
        return (GetValue(row, col) == wxEmptyString);
    }


    inline
    wxString evaluateCmpResult(const CompareFilesResult result, const bool selectedForSynchronization)
    {
        if (selectedForSynchronization)
            switch (result)
            {
            case FILE_LEFT_SIDE_ONLY:
                return wxT("<|");
                break;
            case FILE_RIGHT_SIDE_ONLY:
                return wxT("|>");
                break;
            case FILE_RIGHT_NEWER:
                return wxT(">>");
                break;
            case FILE_LEFT_NEWER:
                return wxT("<<");
                break;
            case FILE_DIFFERENT:
                return wxT("!=");
                break;
            case FILE_EQUAL:
                return wxT("==");
                break;
            default:
                assert (false);
                return wxEmptyString;
            }
        else return wxT("(-)");
    }


    virtual wxString GetValue(int row, int col)
    {
        if (gridRefUI)
        {
            if (unsigned(row) < gridRefUI->size())
            {
                const FileCompareLine& gridLine = (*gridData)[(*gridRefUI)[row]];
                wxString fileSize; //tmp string

                switch (gridIdentifier)
                {
                case 1:
                    if (col < 4)
                    {
                        if (gridLine.fileDescrLeft.objType == TYPE_DIRECTORY)
                        {
                            switch (col)
                            {
                            case 0: //filename
                                return wxEmptyString;
                            case 1: //relative path
                                return gridLine.fileDescrLeft.relFilename;
                            case 2: //file size
                                return _("<Directory>");
                            case 3: //date
                                return gridLine.fileDescrLeft.lastWriteTime;
                            }
                        }
                        else if (gridLine.fileDescrLeft.objType == TYPE_FILE)
                        {
                            switch (col)
                            {
                            case 0: //filename
                                return gridLine.fileDescrLeft.relFilename.AfterLast(GlobalResources::fileNameSeparator);
                            case 1: //relative path
                                return gridLine.fileDescrLeft.relFilename.BeforeLast(GlobalResources::fileNameSeparator);
                            case 2: //file size
                                return globalFunctions::includeNumberSeparator(fileSize = gridLine.fileDescrLeft.fileSize.ToString());
                            case 3: //date
                                return gridLine.fileDescrLeft.lastWriteTime;
                            }
                        }
                    }
                    break;

                case 2:
                    if (col < 4)
                    {
                        if (gridLine.fileDescrRight.objType == TYPE_DIRECTORY)
                        {
                            switch (col)
                            {
                            case 0: //filename
                                return wxEmptyString;
                            case 1: //relative path
                                return gridLine.fileDescrRight.relFilename;
                            case 2: //file size
                                return _("<Directory>");
                            case 3: //date
                                return gridLine.fileDescrRight.lastWriteTime;
                            }
                        }
                        else if (gridLine.fileDescrRight.objType == TYPE_FILE)
                        {
                            switch (col)
                            {
                            case 0: //filename
                                return gridLine.fileDescrRight.relFilename.AfterLast(GlobalResources::fileNameSeparator);
                            case 1: //relative path
                                return gridLine.fileDescrRight.relFilename.BeforeLast(GlobalResources::fileNameSeparator);
                            case 2: //file size
                                return globalFunctions::includeNumberSeparator(fileSize = gridLine.fileDescrRight.fileSize.ToString());
                            case 3: //date
                                return gridLine.fileDescrRight.lastWriteTime;
                            }
                        }
                    }
                    break;

                case 3:
                    if (col < 1)
                    {
                        return evaluateCmpResult(gridLine.cmpResult, gridLine.selectedForSynchronization);;
                    }
                    break;

                default:
                    break;
                }
            }
        }
        //if data is not found:
        return wxEmptyString;
    }

    virtual void SetValue( int row, int col, const wxString& value )
    {
        assert (false); //should not be used, since values are retrieved directly from gridRefUI
    }
    virtual void Clear()
    {
        assert (false); // we don't want to use this, since the visible grid is directly connected to gridRefUI}
    }
    virtual bool InsertRows( size_t pos = 0, size_t numRows = 1 )
    {
        assert (false); // we don't want to use this, since the visible grid is directly connected to gridRefUI}
        return true;
    }
    virtual bool AppendRows( size_t numRows = 1 )
    {
        assert (false); // we don't want to use this, since the visible grid is directly connected to gridRefUI}
        return true;
    }
    virtual bool DeleteRows( size_t pos = 0, size_t numRows = 1 )
    {
        assert (false); // we don't want to use this, since the visible grid is directly connected to gridRefUI}
        return true;
    }

    //update dimensions of grid: no need for InsertRows, AppendRows, DeleteRows anymore!!!
    void updateGridSizes()
    {
        if (gridRefUI)
        {
            int currentNrRows = GetNumberRows();

            if (lastNrRows < currentNrRows)
            {
                if (GetView())
                {
                    wxGridTableMessage msg(this,
                                           wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
                                           currentNrRows - lastNrRows);

                    GetView()->ProcessTableMessage( msg );
                }
            }
            else if (lastNrRows > currentNrRows)
            {
                if (GetView())
                {
                    wxGridTableMessage msg(this,
                                           wxGRIDTABLE_NOTIFY_ROWS_DELETED,
                                           0,
                                           lastNrRows - currentNrRows);

                    GetView()->ProcessTableMessage( msg );
                }
            }
            lastNrRows = currentNrRows;
        }
    }
//###########################################################################


    bool markThisRow(int row) //rows that are filtered out are shown in different color
    {
        if (gridRefUI)
        {
            if (unsigned(row) < gridRefUI->size())
            {
                if (!(*gridData)[(*gridRefUI)[row]].selectedForSynchronization)
                    return true;
            }
        }
        return false;
    }


    wxGridCellAttr* GetAttr(int row, int col, wxGridCellAttr::wxAttrKind  kind)
    {
        wxGridCellAttr* result = wxGridTableBase::GetAttr(row, col, kind);

        //  MARK FILTERED ROWS:
        if (result)
        {   //if kind is not a cell or row attribute, we have to clone the cell attribute, since we don't want to change e.g. all column attribs
            if (result->GetKind() != wxGridCellAttr::Cell && result->GetKind() != wxGridCellAttr::Row)
            {
                wxGridCellAttr* attr = result->Clone();
                result->DecRef();
                result = attr;
            }

            if (markThisRow(row))
                result->SetBackgroundColour(lightBlue);
            else
                result->SetBackgroundColour(*wxWHITE);
        }
        else
        {
            result = new wxGridCellAttr;
            if (markThisRow(row))
                result->SetBackgroundColour(lightBlue);
        }

        return result;
    }

private:
    wxColour  lightBlue;
    int       gridIdentifier;
    GridView* gridRefUI; //(very fast) access to underlying grid data :)
    FileCompareResult* gridData;
    int       lastNrRows;
};



//########################################################################################################

CustomGrid::CustomGrid(wxWindow *parent,
                       wxWindowID id,
                       const wxPoint& pos,
                       const wxSize& size,
                       long style,
                       const wxString& name) :
        wxGrid(parent, id, pos, size, style, name),
        scrollbarsEnabled(true),
        m_grid1(0), m_grid2(0), m_grid3(0),
        gridDataTable(0),
        currentSortColumn(-1),
        sortMarker(0)
{}


CustomGrid::~CustomGrid() {}


bool CustomGrid::CreateGrid(int numRows, int numCols, wxGrid::wxGridSelectionModes selmode)
{
    //use custom wxGridTableBase class for management of large sets of formatted data.
    //This is done in CreateGrid instead of SetTable method since source code is generated and wxFormbuilder invokes CreatedGrid by default.

    gridDataTable = new CustomGridTableBase(numRows, numCols);
    SetTable(gridDataTable, true, selmode);  //give ownership to CustomGrid: gridDataTable is deleted automatically in CustomGrid destructor
    return true;
}


void CustomGrid::deactivateScrollbars()
{
    scrollbarsEnabled = false;
}


//overwrite virtual method to finally get rid of the scrollbars
void CustomGrid::SetScrollbar(int orientation, int position, int thumbSize, int range, bool refresh)
{
    if (scrollbarsEnabled)
        wxWindow::SetScrollbar(orientation, position, thumbSize, range, refresh);
    else
        wxWindow::SetScrollbar(orientation, 0, 0, 0, refresh);
}


//ensure that all grids are properly aligned: add some extra window space to grids that have no horizontal scrollbar
void CustomGrid::adjustGridHeights() //m_grid1, m_grid2, m_grid3 are not NULL in this context
{
    int y1 = 0;
    int y2 = 0;
    int y3 = 0;
    int dummy = 0;

    m_grid1->GetViewStart(&dummy, &y1);
    m_grid2->GetViewStart(&dummy, &y2);
    m_grid3->GetViewStart(&dummy, &y3);

    if (y1 != y2 || y2 != y3)
    {
        int yMax = max(y1, max(y2, y3));

        if (leadingPanel == 1)  //do not handle case (y1 == yMax) here!!! Avoid back coupling!
            m_grid1->SetMargins(0, 0);
        else if (y1 < yMax)
            m_grid1->SetMargins(0, 50);

        if (leadingPanel == 2)
            m_grid2->SetMargins(0, 0);
        else if (y2 < yMax)
            m_grid2->SetMargins(0, 50);

        if (leadingPanel == 3)
            m_grid3->SetMargins(0, 0);
        else if (y3 < yMax)
            m_grid3->SetMargins(0, 50);

        m_grid1->ForceRefresh();
        m_grid2->ForceRefresh();
        m_grid3->ForceRefresh();
    }
}


//this method is called when grid view changes: useful for parallel updating of multiple grids
void CustomGrid::DoPrepareDC(wxDC& dc)
{
    wxScrollHelper::DoPrepareDC(dc);

    int x, y = 0;
    if (leadingPanel == 1 && this == m_grid1)   //avoid back coupling
    {
        GetViewStart(&x, &y);
        m_grid2->Scroll(x, y);
        m_grid3->Scroll(-1, y); //scroll in y-direction only
        adjustGridHeights(); //keep here to ensure m_grid1, m_grid2, m_grid3 != NULL
    }
    else if (leadingPanel == 2 && this == m_grid2)   //avoid back coupling
    {
        GetViewStart(&x, &y);
        m_grid1->Scroll(x, y);
        m_grid3->Scroll(-1, y);
        adjustGridHeights(); //keep here to ensure m_grid1, m_grid2, m_grid3 != NULL
    }
    else if (leadingPanel == 3 && this == m_grid3)   //avoid back coupling
    {
        GetViewStart(&x, &y);
        m_grid1->Scroll(-1, y);
        m_grid2->Scroll(-1, y);
        adjustGridHeights(); //keep here to ensure m_grid1, m_grid2, m_grid3 != NULL
    }
}


//these classes will scroll together, hence the name ;)
void CustomGrid::setScrollFriends(CustomGrid* grid1, CustomGrid* grid2, CustomGrid* grid3)
{
    assert(grid1);
    assert(grid2);
    assert(grid3);

    m_grid1 = grid1;
    m_grid2 = grid2;
    m_grid3 = grid3;

    assert(gridDataTable);
    if (this == m_grid1)
        gridDataTable->SetGridIdentifier(1);
    else if (this == m_grid2)
        gridDataTable->SetGridIdentifier(2);
    else if (this == m_grid3)
        gridDataTable->SetGridIdentifier(3);
    else
        assert (false);
}


void CustomGrid::setGridDataTable(GridView* gridRefUI, FileCompareResult* gridData)
{   //set underlying grid data
    assert(gridDataTable);
    gridDataTable->setGridDataTable(gridRefUI, gridData);
}


void CustomGrid::updateGridSizes()
{
    assert(gridDataTable);
    gridDataTable->updateGridSizes();
}


void CustomGrid::setSortMarker(const int sortColumn, const wxBitmap* bitmap)
{
    currentSortColumn = sortColumn;
    sortMarker        = bitmap;
}


void CustomGrid::DrawColLabel(wxDC& dc, int col)
{
    assert(0 <= col && col < 4);

    wxGrid::DrawColLabel(dc, col);

    if (col == currentSortColumn)
    {
        dc.DrawBitmap(*sortMarker, GetColRight(col) - 16 - 2, 2, true); //respect 2-pixel border
    }
}
bgstack15