uLib  User mode C/C++ extended API library for Win32 programmers.
UtilFunc.cpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode library.
3 // Module: Utility functions (hence the u in uLib)
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 // This file had grown so large it's now split it into smaller units.
9 
10 // > IoUtil.cpp - I/O related functions.
11 // > UserUtil.cpp - User32 related functions.
12 // > GdiUtil.cpp - GDI32 related functions.
13 // > KernelUtil.cpp - Kernel32 related functions.
14 // > SecUtil.cpp - Security related functions. Se also UmLsa.cpp
15 // > ShellUtil.cpp - Shell32 related functions.
16 
17 #include <uLib/Common.h>
18 #include <uLib/UtilFunc.h>
19 #include <uLib/Debug.h>
20 #include <uLib/StrFunc.h>
21 
22 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 // These will return some basic struct types on the fly.
24 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 
26 POINT MkPoint( LONG x, LONG y ) {
27  POINT p = { x,y };
28  return p;
29  }
30 
31 POINTS MkPointS( SHORT x, SHORT y ) {
32  POINTS p = { x,y };
33  return p;
34  }
35 
36 SIZE MkSize( LONG cx, LONG cy ) {
37  SIZE s = { cx,cy };
38  return s;
39  }
40 
41 SIZES MkSizeS( SHORT cx, SHORT cy ) {
42  SIZES s = { cx,cy };
43  return s;
44  }
45 
46 RECT MkRect( LONG L, LONG T, LONG R, LONG B ) {
47  RECT r = { L,T,R,B };
48  return r;
49  }
50 
51 RECTS MkRectS( SHORT L, SHORT T, SHORT R, SHORT B ) {
52  RECTS r = { L,T,R,B };
53  return r;
54  }
55 
56 // Point conversion for mouse hit test;
57 // E.g: POINT pt = Point( LITERAL( POINTS, lParam ));
58 // See also POINTSTOPOINT() and POINTTOPOINTS()
59 
60 POINT MkPoint( const POINTS& p ) { // extern "C++"
61  POINT pt = { p.x, p.y };
62  return pt;
63  }
64 POINTS MkPointS( const POINT& p ) { // extern "C++"
65  POINTS pt = { (short)p.x, (short)p.y };
66  return pt;
67  }
68 
69 // Point equality comparators
70 
71 bool operator == ( POINT P1, POINT P2 ) { // extern "C++"
72  return LITERAL( UINT64,P1 ) == LITERAL( UINT64,P2 );
73  }
74 bool operator != ( POINT P1, POINT P2 ) { // extern "C++"
75  return LITERAL( UINT64,P1 ) != LITERAL( UINT64,P2 );
76  }
77 bool operator == ( POINTS P1, POINTS P2 ) { // extern "C++"
78  return LITERAL( DWORD,P1 ) == LITERAL( DWORD,P2 );
79  }
80 bool operator != ( POINTS P1, POINTS P2 ) { // extern "C++"
81  return LITERAL( DWORD,P1 ) != LITERAL( DWORD,P2 );
82  }
83 
84 // Some rectangle conversions
85 
86 LPRECT AbsToDimRect( LPRECT r ) {
87  r->right -= r->left; // _out r.right = width
88  r->bottom -= r->top; // _out r.bottom = height
89  return r;
90  }
91 
92 LPRECT DimToAbsRect( LPRECT r ) {
93  r->right += r->left; // _out r.right = x2
94  r->bottom += r->top; // _out r.bottom = y2
95  return r;
96  }
97 
98 LPRECT AbsToRelRect( LPRECT R ) {
99  OffsetRect( R, -(R->left), -(R->top) );
100  return R;
101  }
102 
103 LPRECT NegateRect( LPRECT R ) // Change sign of all rect fields
104 {
105  R->top = -R->top;
106  R->bottom = -R->bottom;
107  R->left = -R->left;
108  R->right = -R->right;
109  return R;
110 }
111 
112 // Fit one rect inside another with preserved aspect ratio.
113 
114 void FitRect( PRECT pRc, PRECT pBounds, OUT PRECT pOut )
115 {
116  SIZE ext, bounds, fit;
117  ext.cx = RECT_WIDTH( *pRc );
118  ext.cy = RECT_HEIGHT( *pRc );
119  bounds.cx = RECT_WIDTH( *pBounds );
120  bounds.cy = RECT_HEIGHT( *pBounds );
121 
122  if ((ext.cx != bounds.cx) || (ext.cy != bounds.cy)) // pRc must be scaled
123  {
124  // [REF] https://stackoverflow.com/questions/1373035/how-do-i-scale-one-rectangle-to-the-maximum-size-possible-within-another-rectang
125  float scalex = float(bounds.cx) / ext.cx;
126  float scaley = float(bounds.cy) / ext.cy;
127  // Scale by same factor to preserve aspect ratio.
128  float scale = min_<float>( scalex, scaley );
129  fit.cx = long( scale * ext.cx );
130  fit.cy = long( scale * ext.cy );
131  }
132  else fit = ext;
133 
134  long x = pBounds->left + ((bounds.cx - fit.cx) / 2);
135  long y = pBounds->top + ((bounds.cy - fit.cy) / 2);
136  SetRect( pOut, x, y, x + fit.cx, y + fit.cy );
137 }
138 
139 // Some special legacy rect operations.
140 
141 void DiffRect( PRECT Borders, LPCRECT Outer, LPCRECT Inner )
142 {
143  Borders->top = Inner->top - Outer->top;
144  Borders->bottom = Inner->bottom - Outer->bottom;
145  Borders->left = Inner->left - Outer->left;
146  Borders->right = Inner->right - Outer->right;
147 
148 }
149 
150 void AbsDiffRect( PRECT Borders, LPCRECT Outer, LPCRECT Inner ) // Without sign.
151 {
152  Borders->top = abs( Inner->top - Outer->top );
153  Borders->bottom = abs( Inner->bottom - Outer->bottom );
154  Borders->left = abs( Inner->left - Outer->left );
155  Borders->right = abs( Inner->right - Outer->right );
156 }
157 
158 void InflateBorders( PRECT Target, LPCRECT Borders )
159 {
160  // Refer to DiffRect() to comprehend.
161  Target->top -= Borders->top;
162  Target->bottom -= Borders->bottom;
163  Target->left -= Borders->left;
164  Target->right -= Borders->right;
165 }
166 
167 void AbsInflateBorders( PRECT Target, LPCRECT Borders )
168 {
169  Target->top -= Borders->top;
170  Target->bottom += Borders->bottom;
171  Target->left -= Borders->left;
172  Target->right += Borders->right;
173 }
174 
175 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176 // EngUnits convert a value to a multiple of 10^3 (engineering units).
177 // Probably belongs in a math module, but uLib don't have one.
178 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179 
180 long double EngUnits( long double Value, int* pExpo )
181 // Note: MSVC does /not/ support long double, they're trucated to measly double.
182 // If you need long double in MSVC, buy Love's excellent xMath library.
183 {
184  long double val = Value;
185  int expo = 0;
186  bool neg = (val < 0);
187  if (neg) val = -val;
188 
189  if (val > 1.0)
190  {
191  while( val > 1000.0 ) {
192  expo += 3;
193  val *= 0.001;
194  }
195  }
196  else if (val < 1.0)
197  {
198  while( val < 1.0 ) {
199  expo -= 3;
200  val *= 1000.0;
201  }
202  }
203  if (neg) val = -val;
204 
205  if (pExpo) *pExpo = expo;
206  return val;
207 }
208 
209 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210 // EOF
unsigned long DWORD
Definition: Common.h:414
Definition: Common.h:494
Definition: Common.h:504
RECT MkRect(LONG L, LONG T, LONG R, LONG B)
Definition: UtilFunc.cpp:46
void InflateBorders(PRECT Target, LPCRECT Borders)
Definition: UtilFunc.cpp:158
#define RECT_WIDTH(r)
Definition: UtilFunc.h:161
long double EngUnits(long double Value, int *pExpo)
Definition: UtilFunc.cpp:180
LPRECT DimToAbsRect(LPRECT r)
Definition: UtilFunc.cpp:92
LPRECT AbsToRelRect(LPRECT R)
Definition: UtilFunc.cpp:98
void DiffRect(PRECT Borders, LPCRECT Outer, LPCRECT Inner)
Definition: UtilFunc.cpp:141
#define RECT_HEIGHT(r)
Definition: UtilFunc.h:162
bool operator !=(POINT P1, POINT P2)
Definition: UtilFunc.cpp:74
SIZES MkSizeS(SHORT cx, SHORT cy)
Definition: UtilFunc.cpp:41
void AbsDiffRect(PRECT Borders, LPCRECT Outer, LPCRECT Inner)
Definition: UtilFunc.cpp:150
POINTS MkPointS(SHORT x, SHORT y)
Definition: UtilFunc.cpp:31
LPRECT NegateRect(LPRECT R)
Definition: UtilFunc.cpp:103
unsigned __int64 UINT64
Definition: Common.h:400
SIZE MkSize(LONG cx, LONG cy)
Definition: UtilFunc.cpp:36
Debug and error handling support.
Common include; Added types, small "ubiquitous" utilities, et c.
bool operator==(POINT P1, POINT P2)
Definition: UtilFunc.cpp:71
#define LITERAL(type, x)
Definition: Common.h:969
POINT MkPoint(LONG x, LONG y)
Definition: UtilFunc.cpp:26
LPRECT AbsToDimRect(LPRECT r)
Definition: UtilFunc.cpp:86
void AbsInflateBorders(PRECT Target, LPCRECT Borders)
Definition: UtilFunc.cpp:167
RECTS MkRectS(SHORT L, SHORT T, SHORT R, SHORT B)
Definition: UtilFunc.cpp:51
void FitRect(PRECT pRc, PRECT pBounds, OUT PRECT pOut)
Definition: UtilFunc.cpp:114