ReactOS  0.4.15-dev-3453-gff89651
layout.h
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS headers
3  * LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
4  * PURPOSE: The layout engine of resizable dialog boxes / windows
5  * COPYRIGHT: Copyright 2020-2021 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  */
7 #pragma once
8 #include <assert.h>
9 
10 typedef struct LAYOUT_INFO {
12  UINT m_uEdges; /* BF_* flags */
16 } LAYOUT_INFO;
17 
18 typedef struct LAYOUT_DATA {
23 } LAYOUT_DATA;
24 
25 static __inline void
27 {
28  if (bEnableResize)
29  {
30  GetSystemMenu(pData->m_hwndParent, TRUE); /* revert */
31  }
32  else
33  {
34  HMENU hSysMenu = GetSystemMenu(pData->m_hwndParent, FALSE);
36  RemoveMenu(hSysMenu, SC_SIZE, MF_BYCOMMAND);
38  }
39 }
40 
41 static __inline HDWP
43 {
44  if (!IsWindowVisible(pData->m_hwndGrip))
45  return hDwp;
46 
49  RECT rcClient;
50  GetClientRect(pData->m_hwndParent, &rcClient);
51 
52  if (hDwp)
53  {
54  hDwp = DeferWindowPos(hDwp, pData->m_hwndGrip, NULL,
55  rcClient.right - size.cx, rcClient.bottom - size.cy,
56  size.cx, size.cy, uFlags);
57  }
58  else
59  {
60  SetWindowPos(pData->m_hwndGrip, NULL,
61  rcClient.right - size.cx, rcClient.bottom - size.cy,
62  size.cx, size.cy, uFlags);
63  }
64  return hDwp;
65 }
66 
67 static __inline void
69 {
72  DWORD style = GetWindowLongPtrW(pData->m_hwndParent, GWL_STYLE);
73  DWORD new_style = (bShow ? (style | WS_SIZEBOX) : (style & ~WS_SIZEBOX));
74  if (style != new_style)
75  {
76  SetWindowLongPtrW(pData->m_hwndParent, GWL_STYLE, new_style); /* change style */
77  SetWindowPos(pData->m_hwndParent, NULL, 0, 0, 0, 0, uSWP); /* frame changed */
78  }
79 
80  if (!bShow)
81  {
82  ShowWindow(pData->m_hwndGrip, SW_HIDE);
83  return;
84  }
85 
86  if (pData->m_hwndGrip == NULL)
87  {
89  pData->m_hwndGrip = CreateWindowExW(0, L"SCROLLBAR", NULL, style,
90  0, 0, 0, 0, pData->m_hwndParent,
92  }
94  ShowWindow(pData->m_hwndGrip, SW_SHOWNOACTIVATE);
95 }
96 
97 static __inline void
98 _layout_GetPercents(LPRECT prcPercents, UINT uEdges)
99 {
100  prcPercents->left = (uEdges & BF_LEFT) ? 0 : 100;
101  prcPercents->right = (uEdges & BF_RIGHT) ? 100 : 0;
102  prcPercents->top = (uEdges & BF_TOP) ? 0 : 100;
103  prcPercents->bottom = (uEdges & BF_BOTTOM) ? 100 : 0;
104 }
105 
106 static __inline HDWP
107 _layout_DoMoveItem(LAYOUT_DATA *pData, HDWP hDwp, const LAYOUT_INFO *pLayout,
108  const RECT *rcClient)
109 {
110  RECT rcChild, NewRect, rcPercents;
111  LONG nWidth, nHeight;
112 
113  if (!GetWindowRect(pLayout->m_hwndCtrl, &rcChild))
114  return hDwp;
115  MapWindowPoints(NULL, pData->m_hwndParent, (LPPOINT)&rcChild, 2);
116 
117  nWidth = rcClient->right - rcClient->left;
118  nHeight = rcClient->bottom - rcClient->top;
119 
120  _layout_GetPercents(&rcPercents, pLayout->m_uEdges);
121  NewRect.left = pLayout->m_margin1.cx + nWidth * rcPercents.left / 100;
122  NewRect.top = pLayout->m_margin1.cy + nHeight * rcPercents.top / 100;
123  NewRect.right = pLayout->m_margin2.cx + nWidth * rcPercents.right / 100;
124  NewRect.bottom = pLayout->m_margin2.cy + nHeight * rcPercents.bottom / 100;
125 
126  if (!EqualRect(&NewRect, &rcChild))
127  {
128  hDwp = DeferWindowPos(hDwp, pLayout->m_hwndCtrl, NULL, NewRect.left, NewRect.top,
129  NewRect.right - NewRect.left, NewRect.bottom - NewRect.top,
131  }
132  return hDwp;
133 }
134 
135 static __inline void
137 {
138  RECT rcClient;
139  UINT iItem;
140  HDWP hDwp = BeginDeferWindowPos(pData->m_cLayouts + 1);
141  if (hDwp == NULL)
142  return;
143 
144  GetClientRect(pData->m_hwndParent, &rcClient);
145 
146  for (iItem = 0; iItem < pData->m_cLayouts; ++iItem)
147  hDwp = _layout_DoMoveItem(pData, hDwp, &pData->m_pLayouts[iItem], &rcClient);
148 
149  hDwp = _layout_MoveGrip(pData, hDwp);
150  EndDeferWindowPos(hDwp);
151 }
152 
153 static __inline void
155 {
156  RECT rcClient, rcChild, rcPercents;
157  LONG nWidth, nHeight;
158  UINT iItem;
159 
160  GetClientRect(pData->m_hwndParent, &rcClient);
161  nWidth = rcClient.right - rcClient.left;
162  nHeight = rcClient.bottom - rcClient.top;
163 
164  for (iItem = 0; iItem < pData->m_cLayouts; ++iItem)
165  {
166  LAYOUT_INFO *pInfo = &pData->m_pLayouts[iItem];
167  if (pInfo->m_hwndCtrl == NULL)
168  {
169  pInfo->m_hwndCtrl = GetDlgItem(pData->m_hwndParent, pInfo->m_nCtrlID);
170  if (pInfo->m_hwndCtrl == NULL)
171  continue;
172  }
173 
174  GetWindowRect(pInfo->m_hwndCtrl, &rcChild);
175  MapWindowPoints(NULL, pData->m_hwndParent, (LPPOINT)&rcChild, 2);
176 
177  _layout_GetPercents(&rcPercents, pInfo->m_uEdges);
178  pInfo->m_margin1.cx = rcChild.left - nWidth * rcPercents.left / 100;
179  pInfo->m_margin1.cy = rcChild.top - nHeight * rcPercents.top / 100;
180  pInfo->m_margin2.cx = rcChild.right - nWidth * rcPercents.right / 100;
181  pInfo->m_margin2.cy = rcChild.bottom - nHeight * rcPercents.bottom / 100;
182  }
183 }
184 
185 /* NOTE: Please call LayoutUpdate on parent's WM_SIZE. */
186 static __inline void
187 LayoutUpdate(HWND ignored1, LAYOUT_DATA *pData, LPCVOID ignored2, UINT ignored3)
188 {
189  UNREFERENCED_PARAMETER(ignored1);
190  UNREFERENCED_PARAMETER(ignored2);
191  UNREFERENCED_PARAMETER(ignored3);
192  if (pData == NULL || !pData->m_hwndParent)
193  return;
194  assert(IsWindow(pData->m_hwndParent));
196 }
197 
198 static __inline void
200 {
203 }
204 
205 static __inline LAYOUT_DATA *
206 LayoutInit(HWND hwndParent, const LAYOUT_INFO *pLayouts, INT cLayouts)
207 {
208  BOOL bShowGrip;
209  SIZE_T cb;
211  if (pData == NULL)
212  {
213  assert(0);
214  return NULL;
215  }
216 
217  if (cLayouts < 0) /* NOTE: If cLayouts was negative, then don't show size grip */
218  {
219  cLayouts = -cLayouts;
220  bShowGrip = FALSE;
221  }
222  else
223  {
224  bShowGrip = TRUE;
225  }
226 
227  cb = cLayouts * sizeof(LAYOUT_INFO);
228  pData->m_cLayouts = cLayouts;
229  pData->m_pLayouts = (LAYOUT_INFO *)HeapAlloc(GetProcessHeap(), 0, cb);
230  if (pData->m_pLayouts == NULL)
231  {
232  assert(0);
234  return NULL;
235  }
236  memcpy(pData->m_pLayouts, pLayouts, cb);
237 
239 
240  pData->m_hwndParent = hwndParent;
241 
242  pData->m_hwndGrip = NULL;
243  if (bShowGrip)
244  LayoutShowGrip(pData, bShowGrip);
245 
247  return pData;
248 }
249 
250 static __inline void
252 {
253  if (!pData)
254  return;
255  HeapFree(GetProcessHeap(), 0, pData->m_pLayouts);
257 }
#define WS_CLIPSIBLINGS
Definition: pedump.c:618
static __inline HDWP _layout_DoMoveItem(LAYOUT_DATA *pData, HDWP hDwp, const LAYOUT_INFO *pLayout, const RECT *rcClient)
Definition: layout.h:107
HDWP WINAPI BeginDeferWindowPos(_In_ int)
#define SM_CYHSCROLL
Definition: winuser.h:956
#define MF_BYCOMMAND
Definition: winuser.h:202
static __inline void LayoutDestroy(LAYOUT_DATA *pData)
Definition: layout.h:251
#define SC_RESTORE
Definition: winuser.h:2578
#define SW_SHOWNOACTIVATE
Definition: winuser.h:768
int WINAPI MapWindowPoints(_In_opt_ HWND hWndFrom, _In_opt_ HWND hWndTo, _Inout_updates_(cPoints) LPPOINT lpPoints, _In_ UINT cPoints)
BOOL WINAPI IsWindow(_In_opt_ HWND)
struct LAYOUT_INFO LAYOUT_INFO
#define TRUE
Definition: types.h:120
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define SW_HIDE
Definition: winuser.h:762
static __inline void _layout_ModifySystemMenu(LAYOUT_DATA *pData, BOOL bEnableResize)
Definition: layout.h:26
#define BF_BOTTOM
Definition: winuser.h:457
LONG top
Definition: windef.h:307
static __inline void _layout_InitLayouts(LAYOUT_DATA *pData)
Definition: layout.h:154
#define assert(x)
Definition: debug.h:53
#define WS_SIZEBOX
Definition: pedump.c:642
#define WS_CHILD
Definition: pedump.c:617
#define GetWindowLongPtrW
Definition: winuser.h:4809
LONG left
Definition: windef.h:306
#define SWP_NOZORDER
Definition: winuser.h:1237
UINT uFlags
Definition: api.c:59
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
LONG right
Definition: windef.h:308
int32_t INT
Definition: typedefs.h:58
SIZE m_margin1
Definition: layout.h:14
#define SWP_NOCOPYBITS
Definition: winuser.h:1233
#define L(x)
Definition: ntvdm.h:50
#define FALSE
Definition: types.h:117
static __inline void LayoutUpdate(HWND ignored1, LAYOUT_DATA *pData, LPCVOID ignored2, UINT ignored3)
Definition: layout.h:187
static __inline LAYOUT_DATA * LayoutInit(HWND hwndParent, const LAYOUT_INFO *pLayouts, INT cLayouts)
Definition: layout.h:206
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
BOOL WINAPI EndDeferWindowPos(_In_ HDWP)
UINT m_nCtrlID
Definition: layout.h:11
#define SC_MAXIMIZE
Definition: winuser.h:2568
HWND m_hwndGrip
Definition: layout.h:20
HMENU WINAPI GetSystemMenu(_In_ HWND, _In_ BOOL)
static HWND hwndParent
Definition: cryptui.c:300
LONG cx
Definition: windef.h:334
#define SBS_SIZEGRIP
Definition: winuser.h:332
GLsizeiptr size
Definition: glext.h:5919
HDWP WINAPI DeferWindowPos(_In_ HDWP, _In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
UINT m_uEdges
Definition: layout.h:12
#define BF_RIGHT
Definition: winuser.h:456
#define SWP_NOACTIVATE
Definition: winuser.h:1232
HWND m_hwndCtrl
Definition: layout.h:13
HWND m_hwndParent
Definition: layout.h:19
int WINAPI GetSystemMetrics(_In_ int)
unsigned long DWORD
Definition: ntddk_ex.h:95
SIZE m_margin2
Definition: layout.h:15
LAYOUT_INFO * m_pLayouts
Definition: layout.h:21
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)
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define SWP_FRAMECHANGED
Definition: winuser.h:1230
static __inline void LayoutEnableResize(LAYOUT_DATA *pData, BOOL bEnable)
Definition: layout.h:199
#define SWP_NOSIZE
Definition: winuser.h:1235
#define GWL_STYLE
Definition: winuser.h:846
static __inline void LayoutShowGrip(LAYOUT_DATA *pData, BOOL bShow)
Definition: layout.h:68
ULONG_PTR SIZE_T
Definition: typedefs.h:80
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
_In_ BOOL bEnable
Definition: winddi.h:3426
BOOL WINAPI EqualRect(_In_ LPCRECT, _In_ LPCRECT)
BOOL WINAPI RemoveMenu(_In_ HMENU, _In_ UINT, _In_ UINT)
#define SC_SIZE
Definition: winuser.h:2564
#define BF_TOP
Definition: winuser.h:455
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
#define SM_CXVSCROLL
Definition: winuser.h:955
static __inline HDWP _layout_MoveGrip(LAYOUT_DATA *pData, HDWP hDwp OPTIONAL)
Definition: layout.h:42
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
CONST void * LPCVOID
Definition: windef.h:191
static __inline void _layout_ArrangeLayout(LAYOUT_DATA *pData)
Definition: layout.h:136
#define SWP_NOREPOSITION
Definition: winuser.h:1240
BOOL WINAPI IsWindowVisible(_In_ HWND)
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
LONG bottom
Definition: windef.h:309
static __inline void _layout_GetPercents(LPRECT prcPercents, UINT uEdges)
Definition: layout.h:98
#define SWP_NOMOVE
Definition: winuser.h:1234
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
BOOL WINAPI SetWindowPos(_In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
#define SetWindowLongPtrW
Definition: winuser.h:5326
struct LAYOUT_DATA LAYOUT_DATA
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
Arabic default style
Definition: afstyles.h:93
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:60
LONG cy
Definition: windef.h:335
#define HeapFree(x, y, z)
Definition: compat.h:594
UINT m_cLayouts
Definition: layout.h:22
#define BF_LEFT
Definition: winuser.h:454
#define SWP_NOOWNERZORDER
Definition: winuser.h:1239
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68