ReactOS  0.4.12-dev-57-g7050ac4
MainWindow.cpp
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS Character Map
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/charmap/MainWindow.cpp
5 * PURPOSE: Implements the main dialog window
6 * COPYRIGHT: Copyright 2015 Ged Murphy <gedmurphy@reactos.org>
7 */
8 
9 
10 #include "precomp.h"
11 #include "MainWindow.h"
12 
13 
14 /* DATA *****************************************************/
15 
16 #define ID_ABOUT 0x1
17 
19 
20 
21 /* PUBLIC METHODS **********************************************/
22 
24  m_hMainWnd(NULL),
25  m_hStatusBar(NULL),
26  m_CmdShow(0),
27  m_hRichEd(NULL),
28  m_GridView(nullptr)
29 {
30  m_GridView = new CGridView();
31 }
32 
34 {
35 }
36 
37 bool
39  _In_ int nCmdShow)
40 {
43  int Ret = 1;
44 
45  // Store the instance
46  g_hInstance = hInst;
47  m_CmdShow = nCmdShow;
48 
49  // Initialize common controls
50  icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
52  InitCommonControlsEx(&icex);
53 
54  // Load the application name
55  if (szAppName.LoadStringW(g_hInstance, IDS_TITLE))
56  {
57  // Initialize the main window
58  if (Initialize(szAppName, nCmdShow))
59  {
60  // Run the application
61  Ret = Run();
62 
63  // Uninitialize the main window
64  Uninitialize();
65  }
66  }
67 
68  return (Ret == 0);
69 }
70 
71 
72 
73 /* PRIVATE METHODS **********************************************/
74 
75 bool
77  _In_ int nCmdShow)
78 {
79  // The dialog has a rich edit text box
80  m_hRichEd = LoadLibraryW(L"riched20.DLL");
81  if (m_hRichEd == NULL) return false;
82 
83  return !!(CreateDialogParamW(g_hInstance,
85  NULL,
86  DialogProc,
87  (LPARAM)this));
88 }
89 
90 void
92 {
93  if (m_hRichEd)
95 }
96 
97 int
99 {
100  MSG Msg;
101 
102  // Pump the message queue
103  while (GetMessageW(&Msg, NULL, 0, 0) != 0)
104  {
105  TranslateMessage(&Msg);
106  DispatchMessageW(&Msg);
107  }
108 
109  return 0;
110 }
111 
112 void
114 {
116  SB_SIMPLE,
117  (WPARAM)InMenuLoop,
118  0);
119 }
120 
121 bool
123 {
124  int StatWidths[] = { 110, -1 }; // widths of status bar
125  bool bRet = FALSE;
126 
127  // Create the status bar
130  NULL,
132  0, 0, 0, 0,
133  m_hMainWnd,
135  g_hInstance,
136  NULL);
137  if (m_hStatusBar)
138  {
139  // Create the sections
140  bRet = (SendMessageW(m_hStatusBar,
141  SB_SETPARTS,
142  sizeof(StatWidths) / sizeof(int),
143  (LPARAM)StatWidths) != 0);
144 
145  // Set the status bar for multiple parts output
147  }
148 
149  return bRet;
150 }
151 
152 bool
154  _In_ INT PartId,
156  _In_ UINT uID)
157 {
158  CAtlStringW szMessage;
159  bool bRet = false;
160 
161  // Load the string from the resource
162  if (szMessage.LoadStringW(hInstance, uID))
163  {
164  // Display it on the status bar
165  bRet = (SendMessageW(hStatusBar,
166  SB_SETTEXT,
167  (WPARAM)PartId,
168  (LPARAM)szMessage.GetBuffer()) != 0);
169  }
170 
171  return bRet;
172 }
173 
174 BOOL
176 {
177  m_hMainWnd = hDlg;
178 
179  if (!CreateStatusBar())
180  return FALSE;
181 
182  if (!m_GridView->Create(hDlg))
183  return FALSE;
184 
185  // Load an 'about' option into the system menu
186  HMENU hSysMenu;
187  hSysMenu = GetSystemMenu(m_hMainWnd, FALSE);
188  if (hSysMenu != NULL)
189  {
190  CAtlStringW AboutText;
191  if (AboutText.LoadStringW(IDS_ABOUT))
192  {
193  AppendMenuW(hSysMenu, MF_SEPARATOR, 0, NULL);
194  AppendMenuW(hSysMenu, MF_STRING, ID_ABOUT, AboutText);
195  }
196  }
197 
198  // Add all the fonts to the
199  if (!CreateFontComboBox())
200  return FALSE;
201 
202  ChangeMapFont();
203 
204  // Configure Richedit control for sending notification changes.
205  DWORD evMask;
206  evMask = SendDlgItemMessage(hDlg, IDC_TEXTBOX, EM_GETEVENTMASK, 0, 0);
207  evMask |= ENM_CHANGE;
209 
210  // Display the window according to the user request
212 
213  return TRUE;
214 }
215 
216 BOOL
219  )
220 {
221  RECT rcClient, rcStatus;
222  INT lvHeight, iStatusHeight;
223 
224  // Resize the status bar
226 
227  // Get the statusbar rect and save the height
228  GetWindowRect(m_hStatusBar, &rcStatus);
229  iStatusHeight = rcStatus.bottom - rcStatus.top;
230 
231  // Get the full client rect
232  GetClientRect(m_hMainWnd, &rcClient);
233 
234  // Calculate the remaining height for the gridview
235  lvHeight = rcClient.bottom - iStatusHeight;
236 
237  // Resize the grid view
239 
240  return TRUE;
241 }
242 
243 BOOL
245 {
246  LPNMHDR NmHdr = (LPNMHDR)lParam;
247  LRESULT Ret = 0;
248 
249  switch (NmHdr->code)
250  {
251  case NM_RCLICK:
252  {
253  break;
254  }
255 
256  case NM_DBLCLK:
257  case NM_RETURN:
258  {
259  break;
260  }
261  }
262 
263  return Ret;
264 }
265 
266 BOOL
268 {
269  return 0;// m_GridView->OnContextMenu(lParam);
270 }
271 
272 BOOL
274  _In_ LPARAM /*lParam*/)
275 {
276  LRESULT RetCode = 0;
277  WORD Msg;
278 
279  // Get the message
280  Msg = LOWORD(wParam);
281 
282  switch (Msg)
283  {
284  case IDC_CHECK_ADVANCED:
285  break;
286 
287  case IDC_FONTCOMBO:
288  if (HIWORD(wParam) == CBN_SELCHANGE)
289  {
290  ChangeMapFont();
291  }
292  break;
293 
294  default:
295  // We didn't handle it
296  RetCode = -1;
297  break;
298  }
299 
300  return RetCode;
301 }
302 
303 BOOL
305 {
306  // Clear the user data pointer
308 
309  // Break the message loop
310  PostQuitMessage(0);
311 
312  return TRUE;
313 }
314 
317  _In_ HWND hwndDlg,
318  _In_ UINT Msg,
321  )
322 {
324  LRESULT RetCode = 0;
325 
326  // Get the object pointer from window context
327  This = (CCharMapWindow *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
328  if (This == NULL)
329  {
330  // Check that this isn't a create message
331  if (Msg != WM_INITDIALOG)
332  {
333  // Don't handle null info pointer
334  return FALSE;
335  }
336  }
337 
338  switch (Msg)
339  {
340  case WM_INITDIALOG:
341  {
342  // Get the object pointer from the create param
343  This = (CCharMapWindow *)lParam;
344 
345  // Store the pointer in the window's global user data
346  SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)This);
347 
348  // Call the create handler
349  return This->OnCreate(hwndDlg);
350  }
351 
352  case WM_SIZE:
353  {
354  return This->OnSize(wParam);
355  }
356 
357  case WM_NOTIFY:
358  {
359  return This->OnNotify(lParam);
360  }
361 
362  case WM_CONTEXTMENU:
363  {
364  return This->OnContext(lParam);
365  }
366 
367  case WM_COMMAND:
368  {
369  return This->OnCommand(wParam, lParam);
370  }
371 
372  case WM_SYSCOMMAND:
373  switch (wParam)
374  {
375  case ID_ABOUT:
376  // Apportion blame
377  MessageBoxW(This->m_hMainWnd,
378  L"ReactOS Character Map\r\nCopyright Ged Murphy 2015",
379  L"About",
380  MB_OK | MB_APPLMODAL);
381  break;
382  }
383  break;
384 
385  case WM_ENTERMENULOOP:
386  {
387  This->UpdateStatusBar(true);
388  return TRUE;
389  }
390 
391  case WM_EXITMENULOOP:
392  {
393  This->UpdateStatusBar(false);
394  return TRUE;
395  }
396 
397  case WM_CLOSE:
398  {
399  // Destroy the main window
400  return DestroyWindow(hwndDlg);
401  }
402 
403 
404  case WM_DESTROY:
405  {
406  // Call the destroy handler
407  return This->OnDestroy();
408  }
409  }
410 
411  return FALSE;
412 }
413 
415 {
418 };
419 
420 int
421 CALLBACK
423  NEWTEXTMETRICEXW *lpntme,
424  DWORD FontType,
425  LPARAM lParam)
426 {
427  EnumFontParams *Params = (EnumFontParams *)lParam;
428  LPWSTR pszName = lpelfe->elfLogFont.lfFaceName;
429 
430  /* Skip rotated font */
431  if (pszName[0] == L'@') return 1;
432 
433  /* make sure font doesn't already exist in our list */
434  if (SendMessageW(Params->hCombo,
436  0,
437  (LPARAM)pszName) == CB_ERR)
438  {
439  INT idx;
440  idx = (INT)SendMessageW(Params->hCombo,
441  CB_ADDSTRING,
442  0,
443  (LPARAM)pszName);
444 
445  /* record the font's attributes (Fixedwidth and Truetype) */
446  BOOL fFixed = (lpelfe->elfLogFont.lfPitchAndFamily & FIXED_PITCH) ? TRUE : FALSE;
447  BOOL fTrueType = (lpelfe->elfLogFont.lfOutPrecision == OUT_STROKE_PRECIS) ? TRUE : FALSE;
448 
449  /* store this information in the list-item's userdata area */
450  SendMessageW(Params->hCombo,
452  idx,
453  MAKEWPARAM(fFixed, fTrueType));
454  }
455 
456  return 1;
457 }
458 
459 
460 bool
462 {
463  HWND hCombo;
465 
466  NONCLIENTMETRICSW NonClientMetrics;
467  NonClientMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
468  SystemParametersInfoW(SPI_GETNONCLIENTMETRICS,
469  sizeof(NONCLIENTMETRICSW),
470  &NonClientMetrics,
471  0);
472 
473  // Get a handle to the font
474  HFONT GuiFont;
475  GuiFont = CreateFontIndirectW(&NonClientMetrics.lfMessageFont);
476 
477  // Set the font used in the combo box
478  SendMessageW(hCombo,
479  WM_SETFONT,
480  (WPARAM)GuiFont,
481  0);
482 
483  // Set the fonts which we want to enumerate
484  LOGFONTW FontsToEnum;
485  ZeroMemory(&FontsToEnum, sizeof(LOGFONTW));
486  FontsToEnum.lfCharSet = DEFAULT_CHARSET;
487 
488  // Set the params we want to pass to the callback
489  EnumFontParams Params;
490  Params.This = this;
491  Params.hCombo = hCombo;
492 
493  // Get a DC for combo box
494  HDC hdc;
495  hdc = GetDC(hCombo);
496 
497  // Enumerate all the fonts
498  int ret;
499  ret = EnumFontFamiliesExW(hdc,
500  &FontsToEnum,
502  (LPARAM)&Params,
503  0);
504 
505  ReleaseDC(hCombo, hdc);
506  DeleteObject(GuiFont);
507 
508  // Select the first item in the list
509  SendMessageW(hCombo,
510  CB_SETCURSEL,
511  0,
512  0);
513 
514  return (ret == 1);
515 }
516 
517 bool
519  )
520 {
521  HWND hCombo;
523 
524  INT Length;
525  Length = GetWindowTextLengthW(hCombo);
526  if (!Length) return false;
527 
528  CAtlStringW FontName;
529  FontName.Preallocate(Length);
530 
531  SendMessageW(hCombo,
532  WM_GETTEXT,
533  FontName.GetAllocLength(),
534  (LPARAM)FontName.GetBuffer());
535 
536  return m_GridView->SetFont(FontName);
537 }
#define HDC
Definition: msvc.h:22
#define CB_SETITEMDATA
Definition: winuser.h:1927
#define MAKEINTRESOURCE
Definition: winuser.h:591
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define TRUE
Definition: types.h:120
#define MAKEWPARAM(l, h)
Definition: winuser.h:3916
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
BYTE lfPitchAndFamily
Definition: dimm.idl:71
#define MB_APPLMODAL
Definition: winuser.h:786
bool Create(_In_ HWND hParent)
Definition: GridView.cpp:36
#define WM_CONTEXTMENU
Definition: richedit.h:64
bool Create(_In_ HINSTANCE hInst, _In_ int nCmdShow)
Definition: MainWindow.cpp:38
#define EM_GETEVENTMASK
Definition: richedit.h:92
#define IDC_CHECK_ADVANCED
Definition: resource.h:26
HDC WINAPI GetDC(_In_opt_ HWND)
#define IDC_TEXTBOX
Definition: resource.h:15
int WINAPI EnumFontFamiliesExW(_In_ HDC, _In_ PLOGFONTW, _In_ FONTENUMPROCW, _In_ LPARAM, _In_ DWORD)
HINSTANCE g_hInstance
Definition: MainWindow.cpp:18
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
#define WM_GETTEXT
Definition: winuser.h:1600
BYTE lfCharSet
Definition: dimm.idl:67
#define CALLBACK
Definition: compat.h:27
#define MF_STRING
Definition: winuser.h:138
LONG top
Definition: windef.h:297
bool SetFont(_In_ CAtlString &FontName)
Definition: GridView.cpp:67
BOOL OnCreate(_In_ HWND hwnd)
Definition: MainWindow.cpp:175
#define ZeroMemory
Definition: winbase.h:1635
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
HWND m_hMainWnd
Definition: MainWindow.h:6
#define WS_CHILD
Definition: pedump.c:617
#define SendDlgItemMessage
Definition: winuser.h:5708
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
int32_t INT_PTR
Definition: typedefs.h:62
#define DEFAULT_CHARSET
Definition: wingdi.h:382
#define STATUSCLASSNAME
Definition: commctrl.h:1909
BOOL WINAPI DestroyWindow(_In_ HWND)
struct tagINITCOMMONCONTROLSEX INITCOMMONCONTROLSEX
int32_t INT
Definition: typedefs.h:56
#define SetWindowLongPtr
Definition: treelist.c:70
WPARAM wParam
Definition: combotst.c:138
struct tagNMHDR * LPNMHDR
struct @1571 Msg[]
UINT_PTR WPARAM
Definition: windef.h:207
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
#define IDS_ABOUT
Definition: resource.h:29
UINT code
Definition: winuser.h:3112
CCharMapWindow(void)
Definition: MainWindow.cpp:23
HINSTANCE hInstance
Definition: charmap.c:20
CCharMapWindow * This
Definition: MainWindow.cpp:416
LRESULT WINAPI DispatchMessageW(_In_ const MSG *)
static INT_PTR CALLBACK DialogProc(_In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
Definition: MainWindow.cpp:316
#define IDD_STATUSBAR
Definition: resource.h:9
LONG_PTR LPARAM
Definition: windef.h:208
#define FIXED_PITCH
Definition: wingdi.h:442
#define WM_ENTERMENULOOP
Definition: winuser.h:1780
#define ICC_COOL_CLASSES
Definition: commctrl.h:69
BOOL OnContext(_In_ LPARAM lParam)
Definition: MainWindow.cpp:267
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
#define INT(a)
Definition: assyntax.h:463
unsigned int idx
Definition: utils.c:41
#define GWLP_USERDATA
Definition: treelist.c:63
#define SB_SIMPLE
Definition: commctrl.h:1928
#define CB_ADDSTRING
Definition: winuser.h:1897
HDC hdc
Definition: msvc.h:53
#define WM_EXITMENULOOP
Definition: winuser.h:1781
HMENU WINAPI GetSystemMenu(_In_ HWND, _In_ BOOL)
#define LoadLibraryW(x)
Definition: compat.h:404
smooth NULL
Definition: ftsmooth.c:416
HFONT WINAPI CreateFontIndirectW(_In_ const LOGFONTW *)
#define MF_SEPARATOR
Definition: winuser.h:137
#define EM_SETEVENTMASK
Definition: richedit.h:102
#define CB_ERR
Definition: winuser.h:2389
#define WM_DESTROY
Definition: winuser.h:1591
BYTE lfOutPrecision
Definition: dimm.idl:68
BOOL WINAPI SystemParametersInfoW(_In_ UINT, _In_ UINT, _Inout_opt_ PVOID, _In_ UINT)
bool CreateFontComboBox()
Definition: MainWindow.cpp:461
#define ICC_BAR_CLASSES
Definition: commctrl.h:60
#define FreeLibrary(x)
Definition: compat.h:405
#define IDS_TITLE
Definition: resource.h:30
unsigned int BOOL
Definition: ntddk_ex.h:94
int WINAPI MessageBoxW(_In_opt_ HWND, _In_opt_ LPCWSTR, _In_opt_ LPCWSTR, _In_ UINT)
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
int GetAllocLength() const
Definition: atlsimpstr.h:351
#define SBARS_SIZEGRIP
Definition: commctrl.h:1893
#define WM_SIZE
Definition: winuser.h:1593
~CCharMapWindow(void)
Definition: MainWindow.cpp:33
#define WM_CLOSE
Definition: winuser.h:1603
int WINAPI GetWindowTextLengthW(_In_ HWND)
unsigned int UINT
Definition: ndis.h:50
#define OUT_STROKE_PRECIS
Definition: wingdi.h:416
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
HWND hStatusBar
Definition: main.cpp:35
HWND m_hStatusBar
Definition: MainWindow.h:7
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
HMODULE m_hRichEd
Definition: MainWindow.h:9
void Uninitialize(void)
Definition: MainWindow.cpp:91
int ret
static const WCHAR L[]
Definition: oid.c:1087
#define ENM_CHANGE
Definition: richedit.h:468
HWND WINAPI CreateWindowExW(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpClassName, _In_opt_ LPCWSTR lpWindowName, _In_ DWORD dwStyle, _In_ int X, _In_ int Y, _In_ int nWidth, _In_ int nHeight, _In_opt_ HWND hWndParent, _In_opt_ HMENU hMenu, _In_opt_ HINSTANCE hInstance, _In_opt_ LPVOID lpParam)
BOOL WINAPI InitCommonControlsEx(const INITCOMMONCONTROLSEX *lpInitCtrls)
Definition: commctrl.c:893
#define SB_SETTEXT
Definition: commctrl.h:1919
bool Initialize(_In_z_ LPCTSTR lpCaption, _In_ int nCmdShow)
Definition: MainWindow.cpp:76
#define CB_SETCURSEL
Definition: winuser.h:1922
#define WM_COMMAND
Definition: winuser.h:1716
static int CALLBACK EnumDisplayFont(ENUMLOGFONTEXW *lpelfe, NEWTEXTMETRICEXW *lpntme, DWORD FontType, LPARAM lParam)
Definition: MainWindow.cpp:422
#define IDC_FONTCOMBO
Definition: resource.h:12
void UpdateStatusBar(_In_ bool InMenuLoop)
Definition: MainWindow.cpp:113
TCHAR szAppName[128]
Definition: solitaire.cpp:16
#define _In_
Definition: no_sal2.h:204
#define NM_RETURN
Definition: commctrl.h:132
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
#define IDD_CHARMAP
Definition: resource.h:7
HINSTANCE hInst
Definition: dxdiag.c:13
#define NM_DBLCLK
Definition: commctrl.h:131
#define _In_z_
Definition: no_sal2.h:239
FARPROC FONTENUMPROCW
Definition: wingdi.h:2874
#define NM_RCLICK
Definition: commctrl.h:133
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
bool StatusBarLoadString(_In_ HWND hStatusBar, _In_ INT PartId, _In_ HINSTANCE hInstance, _In_ UINT uID)
Definition: MainWindow.cpp:153
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define MB_OK
Definition: winuser.h:784
#define SB_SETPARTS
Definition: commctrl.h:1924
#define CBN_SELCHANGE
Definition: winuser.h:1940
BOOL WINAPI GetMessageW(_Out_ LPMSG, _In_opt_ HWND, _In_ UINT, _In_ UINT)
bool CreateStatusBar(void)
Definition: MainWindow.cpp:122
#define WM_SETFONT
Definition: winuser.h:1632
BOOL OnCommand(_In_ WPARAM wParam, LPARAM lParam)
Definition: MainWindow.cpp:273
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define SendMessage
Definition: winuser.h:5709
LOGFONTW elfLogFont
Definition: wingdi.h:2679
CGridView * m_GridView
Definition: MainWindow.h:11
HWND WINAPI CreateDialogParamW(_In_opt_ HINSTANCE, _In_ LPCWSTR, _In_opt_ HWND, _In_opt_ DLGPROC, _In_ LPARAM)
WCHAR lfFaceName[LF_FACESIZE]
Definition: dimm.idl:72
#define HIWORD(l)
Definition: typedefs.h:246
LONG bottom
Definition: windef.h:299
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
BOOL OnSize(void)
Definition: MainWindow.cpp:217
HWND GetHwnd()
Definition: GridView.h:49
#define GetWindowLongPtr
Definition: treelist.c:73
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL OnNotify(_In_ LPARAM lParam)
Definition: MainWindow.cpp:244
LONG_PTR LRESULT
Definition: windef.h:209
bool ChangeMapFont()
Definition: MainWindow.cpp:518
#define WS_VISIBLE
Definition: pedump.c:620
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:60
#define WM_SYSCOMMAND
Definition: winuser.h:1717
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define WM_NOTIFY
Definition: richedit.h:61
BOOL OnDestroy(void)
Definition: MainWindow.cpp:304
#define LPARAM
Definition: msvc.h:38
#define ID_ABOUT
Definition: MainWindow.cpp:16
#define CB_FINDSTRINGEXACT
Definition: winuser.h:1901