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
/*
* PROJECT:     ReactOS Character Map
* LICENSE:     GPL - See COPYING in the top level directory
* FILE:        base/applications/charmap/GridView.cpp
* PURPOSE:     Class for for the window which contains the font matrix
* COPYRIGHT:   Copyright 2015 Ged Murphy <gedmurphy@reactos.org>
*/


#include "precomp.h"
#include "GridView.h"
#include "Cell.h"


/* DATA *****************************************************/

extern HINSTANCE g_hInstance;


/* PUBLIC METHODS **********************************************/

CGridView::CGridView() :<--- Member variable 'CGridView::m_Cells' is not initialized in the constructor.<--- Member variable 'CGridView::m_ActiveCell' is not initialized in the constructor.
    m_xNumCells(20),
    m_yNumCells(10),
    m_ScrollPosition(0),
    m_NumRows(0)
{
    m_szMapWndClass = L"CharGridWClass";
}

CGridView::~CGridView()
{
}

bool
CGridView::Create(
    _In_ HWND hParent
    )
{
    WNDCLASSW wc = { 0 };
    wc.style = CS_DBLCLKS;
    wc.lpfnWndProc = MapWndProc;
    wc.cbWndExtra = sizeof(CGridView *);
    wc.hInstance = g_hInstance;
    wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszClassName = m_szMapWndClass;

    if (RegisterClassW(&wc))
    {
        m_hwnd = CreateWindowExW(0,
                                 m_szMapWndClass,
                                 NULL,
                                 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL,
                                 0,0,0,0,
                                 hParent,
                                 NULL,
                                 g_hInstance,
                                 this);

    }

    return !!(m_hwnd != NULL);
}

bool
CGridView::SetFont(
    _In_ CAtlString& FontName
    )
{

    // Create a temporary container for the new font
    CurrentFont NewFont = { 0 };
    NewFont.FontName = FontName;

    // Get the DC for the full grid window
    HDC hdc;
    hdc = GetDC(m_hwnd);
    if (hdc == NULL) return false;

    // Setup the logfont structure
    NewFont.Font.lfHeight = 0; // This is set in WM_SIZE
    NewFont.Font.lfCharSet = DEFAULT_CHARSET;
    StringCchCopyW(NewFont.Font.lfFaceName, LF_FACESIZE, FontName);

    // Get a handle to the new font
    NewFont.hFont = CreateFontIndirectW(&NewFont.Font);
    if (NewFont.hFont == NULL)
    {
        ReleaseDC(m_hwnd, hdc);
        return false;
    }

    // Setup an array of all possible non-BMP indices
    WCHAR ch[MAX_GLYPHS];
    for (int i = 0; i < MAX_GLYPHS; i++)
        ch[i] = (WCHAR)i;

    HFONT hOldFont;
    hOldFont = (HFONT)SelectObject(hdc, NewFont.hFont);

    // Translate all the indices into glyphs
    WORD out[MAX_GLYPHS];
    DWORD Status;
    Status = GetGlyphIndicesW(hdc,
                              ch,
                              MAX_GLYPHS,
                              out,
                              GGI_MARK_NONEXISTING_GLYPHS);
    ReleaseDC(m_hwnd, hdc);
    if (Status == GDI_ERROR)
    {
        SelectObject(hdc, hOldFont);
        return false;
    }

    // Loop all the glyphs looking for valid ones
    // and store those in our font data
    int j = 0;
    for (int i = 0; i < MAX_GLYPHS; i++)
    {
        if (out[i] != 0xffff)
        {
            NewFont.ValidGlyphs[j] = ch[i];
            j++;
        }
    }
    NewFont.NumValidGlyphs = j;

    // Calculate the number of rows required to hold all glyphs
    m_NumRows = NewFont.NumValidGlyphs / m_xNumCells;
    if (NewFont.NumValidGlyphs % m_xNumCells)
        m_NumRows += 1;

    // Set the scrollbar in relation to the rows
    SetScrollRange(m_hwnd, SB_VERT, 0, m_NumRows - m_yNumCells, FALSE);

    // We're done, update the current font
    m_CurrentFont = NewFont;

    // We changed the font, we'll need to repaint the whole window
    InvalidateRect(m_hwnd,
                   NULL,
                   TRUE);

    return true;
}



/* PRIVATE METHODS **********************************************/

bool
CGridView::UpdateCellCoordinates(
    )
{
    // Go through all the cells and calculate
    // their coordinates within the grid 
    for (int y = 0; y < m_yNumCells; y++)
    for (int x = 0; x < m_xNumCells; x++)
    {
        RECT CellCoordinates;
        CellCoordinates.left = x * m_CellSize.cx;
        CellCoordinates.top = y * m_CellSize.cy;
        CellCoordinates.right = (x + 1) * m_CellSize.cx + 1;
        CellCoordinates.bottom = (y + 1) * m_CellSize.cy + 1;

        m_Cells[y][x]->SetCellCoordinates(CellCoordinates);
    }

    return true;
}

LRESULT
CGridView::OnCreate(
    _In_ HWND hwnd,
    _In_ HWND hParent
    )
{
    m_hwnd = hwnd;
    m_hParent = hParent;

    // C++ doesn't allow : "CCells ***C = new CCell***[x * y]"
    // so we have to build the 2d array up manually
    m_Cells = new CCell**[m_yNumCells]; // rows
    for (int i = 0; i < m_yNumCells; i++)
        m_Cells[i] = new CCell*[m_xNumCells]; // columns

    for (int y = 0; y < m_yNumCells; y++)
    for (int x = 0; x < m_xNumCells; x++)
    {
        m_Cells[y][x] = new CCell(m_hwnd);
    }

    // Give the first cell focus
    SetCellFocus(m_Cells[0][0]);

    return 0;
}

LRESULT
CGridView::OnSize(
    _In_ INT Width,
    _In_ INT Height
    )
{
    // Get the client area of the main dialog
    RECT ParentRect;
    GetClientRect(m_hParent, &ParentRect);

    // Calculate the grid size using the parent
    m_ClientCoordinates.left = ParentRect.left + 25;
    m_ClientCoordinates.top = ParentRect.top + 50;
    m_ClientCoordinates.right = ParentRect.right - m_ClientCoordinates.left - 10;
    m_ClientCoordinates.bottom = ParentRect.bottom - m_ClientCoordinates.top - 70;

    // Resize the grid window
    SetWindowPos(m_hwnd,
                 NULL,
                 m_ClientCoordinates.left,
                 m_ClientCoordinates.top,
                 m_ClientCoordinates.right,
                 m_ClientCoordinates.bottom,
                 SWP_NOZORDER | SWP_SHOWWINDOW);

    // Get the client area we can draw on. The position we set above includes
    // a scrollbar which we obviously can't draw on. GetClientRect gives us
    // the size without the scroll, and it's more efficient than getting the
    // scroll metrics and calculating the size from that
    RECT ClientRect;
    GetClientRect(m_hwnd, &ClientRect);
    m_CellSize.cx = ClientRect.right / m_xNumCells;
    m_CellSize.cy = ClientRect.bottom / m_yNumCells;

    // Let all the cells know about their new coords
    UpdateCellCoordinates();

    // We scale the font size up or down depending on the cell size
    if (m_CurrentFont.hFont)
    {
        // Delete the existing font
        DeleteObject(m_CurrentFont.hFont);

        HDC hdc;
        hdc = GetDC(m_hwnd);
        if (hdc)
        {
            // Update the font size with respect to the cell size
            m_CurrentFont.Font.lfHeight = (m_CellSize.cy - 5);
            m_CurrentFont.hFont = CreateFontIndirectW(&m_CurrentFont.Font);
            ReleaseDC(m_hwnd, hdc);
        }
    }

    // Redraw the whole grid
    InvalidateRect(m_hwnd, &ClientRect, TRUE);

    return 0;
}

VOID
CGridView::OnVScroll(_In_ INT Value,
                     _In_ INT Pos)
{
    
    INT PrevScrollPosition = m_ScrollPosition;

    switch (Value)
    {
    case SB_LINEUP:
        m_ScrollPosition -= 1;
        break;

    case SB_LINEDOWN:
        m_ScrollPosition += 1;
        break;

    case SB_PAGEUP:
        m_ScrollPosition -= m_yNumCells;
        break;

    case SB_PAGEDOWN:
        m_ScrollPosition += m_yNumCells;
        break;

    case SB_THUMBTRACK:
        m_ScrollPosition = Pos;
        break;

    default:
        break;
    }

    // Make sure we don't scroll past row 0 or max rows
    m_ScrollPosition = max(0, m_ScrollPosition);
    m_ScrollPosition = min(m_ScrollPosition, m_NumRows);

    // Check if there's a difference from the previous position
    INT ScrollDiff;
    ScrollDiff = PrevScrollPosition - m_ScrollPosition;
    if (ScrollDiff)
    {
        // Set the new scrollbar position in the scroll box
        SetScrollPos(m_hwnd,
                     SB_VERT,
                     m_ScrollPosition,
                     TRUE);

        // Check if the scrollbar has moved more than the
        // number of visible rows (draged or paged)
        if (abs(ScrollDiff) < m_yNumCells)
        {
            RECT rect;
            GetClientRect(m_hwnd, &rect);

            // Scroll the visible cells which remain within the grid
            // and invalidate any new ones which appear from the top / bottom
            ScrollWindowEx(m_hwnd,
                           0,
                           ScrollDiff * m_CellSize.cy,
                           &rect,
                           &rect,
                           NULL,
                           NULL,
                           SW_INVALIDATE);
        }
        else
        {
            // All the cells need to be redrawn
            InvalidateRect(m_hwnd,
                           NULL,
                           TRUE);
        }
    }
}

LRESULT
CGridView::OnPaint(
    _In_opt_ HDC hdc
    )
{
    PAINTSTRUCT PaintStruct = { 0 };
    HDC LocalHdc = NULL;
    BOOL bSuccess = FALSE;

    // Check if we were passed a DC
    if (hdc == NULL)
    {
        // We weren't, let's get one
        LocalHdc = BeginPaint(m_hwnd, &PaintStruct);
        if (LocalHdc) bSuccess = TRUE;
    }
    else
    {
        // Use the existing DC and just get the region to paint
        bSuccess = GetUpdateRect(m_hwnd,
                                 &PaintStruct.rcPaint,
                                 TRUE);
        if (bSuccess)
        {
            // Update the struct with the DC we were passed
            PaintStruct.hdc = (HDC)hdc;
        }
    }

    // Make sure we have a valid DC
    if (bSuccess)
    {
        // Paint the grid and chars
        DrawGrid(&PaintStruct);

        if (LocalHdc)
        {
            EndPaint(m_hwnd, &PaintStruct);
        }
    }

    return 0;
}

LRESULT
CALLBACK
CGridView::MapWndProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
{
    CGridView *This;
    LRESULT RetCode = 0;

    // Get the object pointer from window context
    This = (CGridView *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    if (This == NULL)
    {
        // Check that this isn't a create message
        if (uMsg != WM_CREATE)
        {
            // Don't handle null info pointer
            goto HandleDefaultMessage;
        }
    }

    switch (uMsg)
    {
    case WM_CREATE:
    {
        // Get the object pointer from the create param
        This = (CGridView *)((LPCREATESTRUCT)lParam)->lpCreateParams;

        // Store the pointer in the window's global user data
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)This);

        This->OnCreate(hwnd, ((LPCREATESTRUCTW)lParam)->hwndParent);
        break;
    }

    case WM_SIZE:
    {
        INT Width, Height;
        Width = LOWORD(lParam);
        Height = HIWORD(lParam);

        This->OnSize(Width, Height);
        break;
    }

    case WM_VSCROLL:
    {
        INT Value, Pos;
        Value = LOWORD(wParam);
        Pos = HIWORD(wParam);

        This->OnVScroll(Value, Pos);
        break;
    }

    case WM_PAINT:
    {
        This->OnPaint((HDC)wParam);
        break;
    }

    case WM_DESTROY:
    {
        This->DeleteCells();
        break;
    }

    default:
    {
HandleDefaultMessage:
        RetCode = DefWindowProcW(hwnd, uMsg, wParam, lParam);
        break;
    }
    }

    return RetCode;
}


void
CGridView::DrawGrid(
    _In_ LPPAINTSTRUCT PaintStruct
    )
{
    // Calculate which glyph to start at based on scroll position
    int i;
    i = m_xNumCells * m_ScrollPosition;

    // Make sure we have the correct font on the DC
    HFONT hOldFont;
    hOldFont = (HFONT)SelectFont(PaintStruct->hdc,
                                 m_CurrentFont.hFont);

    // Traverse all the cells
    for (int y = 0; y < m_yNumCells; y++)
    for (int x = 0; x < m_xNumCells; x++)
    {
        // Update the glyph for this cell
        WCHAR ch = (WCHAR)m_CurrentFont.ValidGlyphs[i];
        m_Cells[y][x]->SetChar(ch);

        // Tell it to paint itself
        m_Cells[y][x]->OnPaint(*PaintStruct);
        i++;
    }

    SelectObject(PaintStruct->hdc, hOldFont);

}

void
CGridView::DeleteCells()
{
    if (m_Cells == nullptr)
        return;

    // Free cells withing the 2d array
    for (int i = 0; i < m_yNumCells; i++)
        delete[] m_Cells[i];
    delete[] m_Cells;

    m_Cells = nullptr;
}

void
CGridView::SetCellFocus(
    _In_ CCell* NewActiveCell
    )
{
    if (m_ActiveCell)
    {
        // Remove focus from any existing cell
        m_ActiveCell->SetFocus(false);
        InvalidateRect(m_hwnd, m_ActiveCell->GetCellCoordinates(), TRUE);
    }

    // Set the new active cell and give it focus
    m_ActiveCell = NewActiveCell;
    m_ActiveCell->SetFocus(true);
}