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
/*
* PROJECT:     ReactOS Character Map
* LICENSE:     GPL - See COPYING in the top level directory
* FILE:        base/applications/charmap/MainWindow.cpp
* PURPOSE:     Implements the main dialog window
* COPYRIGHT:   Copyright 2015 Ged Murphy <gedmurphy@reactos.org>
*/


#include "precomp.h"
#include "MainWindow.h"


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

#define ID_ABOUT    0x1

HINSTANCE g_hInstance = NULL;


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

CCharMapWindow::CCharMapWindow(void) :
    m_hMainWnd(NULL),
    m_hStatusBar(NULL),
    m_CmdShow(0),
    m_hRichEd(NULL),
    m_GridView(nullptr)
{
    m_GridView = new CGridView();<--- Class 'CCharMapWindow' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'CCharMapWindow' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
}

CCharMapWindow::~CCharMapWindow(void)
{
}

bool
CCharMapWindow::Create(_In_ HINSTANCE hInst,
                       _In_ int nCmdShow)
{
    INITCOMMONCONTROLSEX icex;
    CAtlStringW szAppName;
    int Ret = 1;

    // Store the instance
    g_hInstance = hInst;
    m_CmdShow = nCmdShow;

    // Initialize common controls
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_BAR_CLASSES | ICC_COOL_CLASSES;
    InitCommonControlsEx(&icex);

    // Load the application name
    if (szAppName.LoadStringW(g_hInstance, IDS_TITLE))
    {
        // Initialize the main window
        if (Initialize(szAppName, nCmdShow))
        {
            // Run the application
            Ret = Run();

            // Uninitialize the main window
            Uninitialize();
        }
    }

    return (Ret == 0);
}



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

bool
CCharMapWindow::Initialize(_In_z_ LPCTSTR lpCaption,
                           _In_ int nCmdShow)
{
    // The dialog has a rich edit text box
    m_hRichEd = LoadLibraryW(L"riched20.DLL");
    if (m_hRichEd == NULL) return false;

    return !!(CreateDialogParamW(g_hInstance,
                                 MAKEINTRESOURCE(IDD_CHARMAP),
                                 NULL,
                                 DialogProc,
                                 (LPARAM)this));
}

void
CCharMapWindow::Uninitialize(void)
{
    if (m_hRichEd)
        FreeLibrary(m_hRichEd);
}

int
CCharMapWindow::Run(void)
{
    MSG Msg;

    // Pump the message queue 
    while (GetMessageW(&Msg, NULL, 0, 0) != 0)
    {
        TranslateMessage(&Msg);
        DispatchMessageW(&Msg);
    }

    return 0;
}

void
CCharMapWindow::UpdateStatusBar(_In_ bool InMenuLoop)
{
    SendMessageW(m_hStatusBar,
                 SB_SIMPLE,
                 (WPARAM)InMenuLoop,
                 0);
}

bool
CCharMapWindow::CreateStatusBar(void)
{
    int StatWidths[] = { 110, -1 }; // widths of status bar
    bool bRet = FALSE;

    // Create the status bar
    m_hStatusBar = CreateWindowExW(0,
                                   STATUSCLASSNAME,
                                   NULL,
                                   WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
                                   0, 0, 0, 0,
                                   m_hMainWnd,
                                   (HMENU)IDD_STATUSBAR,
                                   g_hInstance,
                                   NULL);
    if (m_hStatusBar)
    {
        // Create the sections
        bRet = (SendMessageW(m_hStatusBar,
                             SB_SETPARTS,
                             sizeof(StatWidths) / sizeof(int),
                             (LPARAM)StatWidths) != 0);

        // Set the status bar for multiple parts output
        SendMessage(m_hStatusBar, SB_SIMPLE, (WPARAM)FALSE, (LPARAM)0);
    }

    return bRet;
}

bool
CCharMapWindow::StatusBarLoadString(_In_ HWND hStatusBar,
                                    _In_ INT PartId,
                                    _In_ HINSTANCE hInstance,
                                    _In_ UINT uID)
{
    CAtlStringW szMessage;
    bool bRet = false;

    // Load the string from the resource
    if (szMessage.LoadStringW(hInstance, uID))
    {
        // Display it on the status bar
        bRet = (SendMessageW(hStatusBar,
                             SB_SETTEXT,
                             (WPARAM)PartId,
                             (LPARAM)szMessage.GetBuffer()) != 0);
    }

    return bRet;
}

BOOL
CCharMapWindow::OnCreate(_In_ HWND hDlg)
{
    m_hMainWnd = hDlg;

    if (!CreateStatusBar())
        return FALSE;

    if (!m_GridView->Create(hDlg))
        return FALSE;

    // Load an 'about' option into the system menu
    HMENU hSysMenu;
    hSysMenu = GetSystemMenu(m_hMainWnd, FALSE);
    if (hSysMenu != NULL)
    {
        CAtlStringW AboutText;
        if (AboutText.LoadStringW(IDS_ABOUT))
        {
            AppendMenuW(hSysMenu, MF_SEPARATOR, 0, NULL);
            AppendMenuW(hSysMenu, MF_STRING, ID_ABOUT, AboutText);
        }
    }

    // Add all the fonts to the 
    if (!CreateFontComboBox())
        return FALSE;

    ChangeMapFont();

    // Configure Richedit control for sending notification changes.
    DWORD evMask;
    evMask = SendDlgItemMessage(hDlg, IDC_TEXTBOX, EM_GETEVENTMASK, 0, 0);
    evMask |= ENM_CHANGE;
    SendDlgItemMessage(hDlg, IDC_TEXTBOX, EM_SETEVENTMASK, 0, (LPARAM)evMask);

    // Display the window according to the user request
    ShowWindow(m_hMainWnd, m_CmdShow);

    return TRUE;
}

BOOL
CCharMapWindow::OnSize(
    _In_ WPARAM wParam
    )
{
    RECT rcClient, rcStatus;
    INT lvHeight, iStatusHeight;

    // Resize the status bar
    SendMessage(m_hStatusBar, WM_SIZE, 0, 0);

    // Get the statusbar rect and save the height
    GetWindowRect(m_hStatusBar, &rcStatus);
    iStatusHeight = rcStatus.bottom - rcStatus.top;

    // Get the full client rect
    GetClientRect(m_hMainWnd, &rcClient);

    // Calculate the remaining height for the gridview
    lvHeight = rcClient.bottom - iStatusHeight;

    // Resize the grid view
    SendMessageW(m_GridView->GetHwnd(), WM_SIZE, wParam, 0);

    return TRUE;
}

BOOL
CCharMapWindow::OnNotify(_In_ LPARAM lParam)
{
    LPNMHDR NmHdr = (LPNMHDR)lParam;
    LRESULT Ret = 0;

    switch (NmHdr->code)
    {
    case NM_RCLICK:
    {
        break;
    }

    case NM_DBLCLK:
    case NM_RETURN:
    {
        break;
    }
    }

    return Ret;
}

BOOL
CCharMapWindow::OnContext(_In_ LPARAM lParam)
{
    return 0;// m_GridView->OnContextMenu(lParam);
}

BOOL
CCharMapWindow::OnCommand(_In_ WPARAM wParam,
                          _In_ LPARAM /*lParam*/)
{
    LRESULT RetCode = 0;
    WORD Msg;

    // Get the message
    Msg = LOWORD(wParam);

    switch (Msg)
    {
    case IDC_CHECK_ADVANCED:
        break;

    case IDC_FONTCOMBO:
        if (HIWORD(wParam) == CBN_SELCHANGE)
        {
            ChangeMapFont();
        }
        break;

    default:
        // We didn't handle it
        RetCode = -1;
        break;
    }

    return RetCode;
}

BOOL
CCharMapWindow::OnDestroy(void)
{
    // Clear the user data pointer
    SetWindowLongPtr(m_hMainWnd, GWLP_USERDATA, 0);

    // Break the message loop
    PostQuitMessage(0);

    return TRUE;
}

INT_PTR CALLBACK
CCharMapWindow::DialogProc(
    _In_ HWND   hwndDlg,
    _In_ UINT   Msg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam
    )
{
    CCharMapWindow *This;
    LRESULT RetCode = 0;

    // Get the object pointer from window context
    This = (CCharMapWindow *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
    if (This == NULL)
    {
        // Check that this isn't a create message
        if (Msg != WM_INITDIALOG)
        {
            // Don't handle null info pointer
            return FALSE;
        }
    }

    switch (Msg)
    {
    case WM_INITDIALOG:
    {
        // Get the object pointer from the create param
        This = (CCharMapWindow *)lParam;

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

        // Call the create handler
        return This->OnCreate(hwndDlg);
    }

    case WM_SIZE:
    {
        return This->OnSize(wParam);
    }

    case WM_NOTIFY:
    {
        return This->OnNotify(lParam);
    }

    case WM_CONTEXTMENU:
    {
        return This->OnContext(lParam);
    }

    case WM_COMMAND:
    {
        return This->OnCommand(wParam, lParam);
    }

    case WM_SYSCOMMAND:
        switch (wParam)
        {
        case ID_ABOUT:
            // Apportion blame
            MessageBoxW(This->m_hMainWnd,
                        L"ReactOS Character Map\r\nCopyright Ged Murphy 2015",
                        L"About",
                        MB_OK | MB_APPLMODAL);
            break;
        }
        break;

    case WM_ENTERMENULOOP:
    {
        This->UpdateStatusBar(true);
        return TRUE;
    }

    case WM_EXITMENULOOP:
    {
        This->UpdateStatusBar(false);
        return TRUE;
    }

    case WM_CLOSE:
    {
        // Destroy the main window
        return DestroyWindow(hwndDlg);
    }


    case WM_DESTROY:
    {
        // Call the destroy handler
        return This->OnDestroy();
    }
    }

    return FALSE;
}

struct EnumFontParams
{
    CCharMapWindow *This;
    HWND hCombo;
};

int
CALLBACK
CCharMapWindow::EnumDisplayFont(ENUMLOGFONTEXW *lpelfe,
                                NEWTEXTMETRICEXW *lpntme,
                                DWORD FontType,
                                LPARAM lParam)
{
    EnumFontParams *Params = (EnumFontParams *)lParam;
    LPWSTR pszName = lpelfe->elfLogFont.lfFaceName;

    /* Skip rotated font */
    if (pszName[0] == L'@') return 1;

    /* make sure font doesn't already exist in our list */
    if (SendMessageW(Params->hCombo,
                     CB_FINDSTRINGEXACT,
                     0,
                     (LPARAM)pszName) == CB_ERR)
    {
        INT idx;
        idx = (INT)SendMessageW(Params->hCombo,
                                CB_ADDSTRING,
                                0,
                                (LPARAM)pszName);

        /* record the font's attributes (Fixedwidth and Truetype) */
        BOOL fFixed = (lpelfe->elfLogFont.lfPitchAndFamily & FIXED_PITCH) ? TRUE : FALSE;
        BOOL fTrueType = (lpelfe->elfLogFont.lfOutPrecision == OUT_STROKE_PRECIS) ? TRUE : FALSE;

        /* store this information in the list-item's userdata area */
        SendMessageW(Params->hCombo,
                     CB_SETITEMDATA,
                     idx,
                     MAKEWPARAM(fFixed, fTrueType));
    }

    return 1;
}


bool
CCharMapWindow::CreateFontComboBox()
{
    HWND hCombo;
    hCombo = GetDlgItem(m_hMainWnd, IDC_FONTCOMBO);

    NONCLIENTMETRICSW NonClientMetrics;
    NonClientMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
    SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
                          sizeof(NONCLIENTMETRICSW),
                          &NonClientMetrics,
                          0);

    // Get a handle to the font
    HFONT GuiFont;
    GuiFont = CreateFontIndirectW(&NonClientMetrics.lfMessageFont);

    // Set the font used in the combo box
    SendMessageW(hCombo,
                 WM_SETFONT,
                 (WPARAM)GuiFont,
                 0);

    // Set the fonts which we want to enumerate
    LOGFONTW FontsToEnum;
    ZeroMemory(&FontsToEnum, sizeof(LOGFONTW));
    FontsToEnum.lfCharSet = DEFAULT_CHARSET;

    // Set the params we want to pass to the callback
    EnumFontParams Params;
    Params.This = this;
    Params.hCombo = hCombo;

    // Get a DC for combo box
    HDC hdc;
    hdc = GetDC(hCombo);

    // Enumerate all the fonts
    int ret;
    ret = EnumFontFamiliesExW(hdc,
                              &FontsToEnum,
                              (FONTENUMPROCW)EnumDisplayFont,
                              (LPARAM)&Params,
                              0);

    ReleaseDC(hCombo, hdc);
    DeleteObject(GuiFont);

    // Select the first item in the list
    SendMessageW(hCombo,
                 CB_SETCURSEL,
                 0,
                 0);

    return (ret == 1);
}

bool
CCharMapWindow::ChangeMapFont(
    )
{
    HWND hCombo;
    hCombo = GetDlgItem(m_hMainWnd, IDC_FONTCOMBO);

    INT Length;
    Length = GetWindowTextLengthW(hCombo);
    if (!Length) return false;

    CAtlStringW FontName;
    FontName.Preallocate(Length);

    SendMessageW(hCombo,
                 WM_GETTEXT,
                 FontName.GetAllocLength(),
                 (LPARAM)FontName.GetBuffer());

    return m_GridView->SetFont(FontName);
}