foobar2000 SDK  2015-08-03
GDIUtils.h
Go to the documentation of this file.
1 static HBITMAP CreateDIB24(CSize size) {
2  struct {
3  BITMAPINFOHEADER bmi;
4  } bi = {};
5  bi.bmi.biSize = sizeof(bi.bmi);
6  bi.bmi.biWidth = size.cx;
7  bi.bmi.biHeight = size.cy;
8  bi.bmi.biPlanes = 1;
9  bi.bmi.biBitCount = 24;
10  bi.bmi.biCompression = BI_RGB;
11  void * bitsPtr;
12  return CreateDIBSection(NULL, reinterpret_cast<const BITMAPINFO*>(&bi), DIB_RGB_COLORS,&bitsPtr,0,0);
13 }
14 
15 static HBITMAP CreateDIB16(CSize size) {
16  struct {
17  BITMAPINFOHEADER bmi;
18  } bi = {};
19  bi.bmi.biSize = sizeof(bi.bmi);
20  bi.bmi.biWidth = size.cx;
21  bi.bmi.biHeight = size.cy;
22  bi.bmi.biPlanes = 1;
23  bi.bmi.biBitCount = 16;
24  bi.bmi.biCompression = BI_RGB;
25  void * bitsPtr;
26  return CreateDIBSection(NULL, reinterpret_cast<const BITMAPINFO*>(&bi), DIB_RGB_COLORS,&bitsPtr,0,0);
27 }
28 
29 static HBITMAP CreateDIB8(CSize size, const COLORREF palette[256]) {
30  struct {
31  BITMAPINFOHEADER bmi;
32  COLORREF colors[256];
33  } bi = { };
34  for(int c = 0; c < 256; ++c ) bi.colors[c] = palette[c];
35  bi.bmi.biSize = sizeof(bi.bmi);
36  bi.bmi.biWidth = size.cx;
37  bi.bmi.biHeight = size.cy;
38  bi.bmi.biPlanes = 1;
39  bi.bmi.biBitCount = 8;
40  bi.bmi.biCompression = BI_RGB;
41  bi.bmi.biClrUsed = 256;
42  void * bitsPtr;
43  return CreateDIBSection(NULL, reinterpret_cast<const BITMAPINFO*>(&bi), DIB_RGB_COLORS,&bitsPtr,0,0);
44 }
45 
46 static void CreateScaledFont(CFont & out, CFontHandle in, double scale) {
47  LOGFONT lf;
48  WIN32_OP_D( in.GetLogFont(lf) );
49  int temp = pfc::rint32(scale * lf.lfHeight);
50  if (temp == 0) temp = pfc::sgn_t(lf.lfHeight);
51  lf.lfHeight = temp;
52  WIN32_OP_D( out.CreateFontIndirect(&lf) != NULL );
53 }
54 
55 static void CreateScaledFontEx(CFont & out, CFontHandle in, double scale, int weight) {
56  LOGFONT lf;
57  WIN32_OP_D( in.GetLogFont(lf) );
58  int temp = pfc::rint32(scale * lf.lfHeight);
59  if (temp == 0) temp = pfc::sgn_t(lf.lfHeight);
60  lf.lfHeight = temp;
61  lf.lfWeight = weight;
62  WIN32_OP_D( out.CreateFontIndirect(&lf) != NULL );
63 }
64 
65 static void CreatePreferencesHeaderFont(CFont & out, CWindow source) {
66  CreateScaledFontEx(out, source.GetFont(), 1.3, FW_BOLD);
67 }
68 
69 static void CreatePreferencesHeaderFont2(CFont & out, CWindow source) {
70  CreateScaledFontEx(out, source.GetFont(), 1.1, FW_BOLD);
71 }
72 
73 template<typename TCtrl>
74 class CAltFontCtrl : public TCtrl {
75 public:
76  void Initialize(CWindow wnd, double scale, int weight) {
77  CreateScaledFontEx(m_font, wnd.GetFont(), scale, weight);
78  _initWnd(wnd);
79  }
80  void MakeHeader(CWindow wnd) {
82  _initWnd(wnd);
83  }
84  void MakeHeader2(CWindow wnd) {
86  _initWnd(wnd);
87  }
88 private:
89  void _initWnd(CWindow wnd) {
90  SubclassWindow(wnd); SetFont(m_font);
91  }
92  CFont m_font;
93 };
94 
95 class CFontScaled : public CFont {
96 public:
97  CFontScaled(HFONT _in, double scale) {
98  CreateScaledFont(*this, _in, scale);
99  }
100 };
101 
103 public:
104  DCClipRgnScope(HDC dc) : m_dc(dc) {
105  m_dc.GetClipRgn(m_rgn);
106  }
108  m_dc.SelectClipRgn(m_rgn);
109  }
110 
111  HRGN OldVal() const throw() {return m_rgn;}
112 
113  PFC_CLASS_NOT_COPYABLE_EX(DCClipRgnScope)
114 private:
115  CDCHandle m_dc;
116  CRgn m_rgn;
117 };
118 
119 
120 static HBRUSH MakeTempBrush(HDC dc, COLORREF color) throw() {
121  SetDCBrushColor(dc, color); return (HBRUSH) GetStockObject(DC_BRUSH);
122 }
123 
124 class CDCBrush : public CBrushHandle {
125 public:
126  CDCBrush(HDC dc, COLORREF color) throw() {
127  m_dc = dc;
128  m_oldCol = m_dc.SetDCBrushColor(color);
129  m_hBrush = (HBRUSH) GetStockObject(DC_BRUSH);
130  }
131  ~CDCBrush() throw() {
132  m_dc.SetDCBrushColor(m_oldCol);
133  }
134  PFC_CLASS_NOT_COPYABLE_EX(CDCBrush)
135 private:
136  CDCHandle m_dc;
137  COLORREF m_oldCol;
138 };
139 
140 class CDCPen : public CPenHandle {
141 public:
142  CDCPen(HDC dc, COLORREF color) throw() {
143  m_dc = dc;
144  m_oldCol = m_dc.SetDCPenColor(color);
145  m_hPen = (HPEN) GetStockObject(DC_PEN);
146  }
147  ~CDCPen() throw() {
148  m_dc.SetDCPenColor(m_oldCol);
149  }
150 private:
151  CDCHandle m_dc;
152  COLORREF m_oldCol;
153 };
154 
155 
156 class CBackBuffer : public CDC {
157 public:
158  CBackBuffer() : m_bitmapOld(NULL), m_curSize(0,0) {
159  CreateCompatibleDC(NULL);
160  ATLASSERT(m_hDC != NULL);
161  }
163  Dispose();
164  }
165  void Attach(HBITMAP bmp, CSize size) {
166  Dispose();
167  m_bitmap.Attach(bmp); m_curSize = size;
168  m_bitmapOld = SelectBitmap(m_bitmap);
169  }
170  void Attach(HBITMAP bmp) {
171  CSize size;
172  bool state = CBitmapHandle(bmp).GetSize(size);
173  ATLASSERT(state);
174  Attach(bmp, size);
175  }
176  BOOL Allocate(CSize size, HDC dcCompatible = NULL) {
177  if (m_hDC == NULL) return FALSE;
178  if (m_curSize == size) return TRUE;
179  Dispose();
180  HBITMAP temp;
181  if (dcCompatible == NULL) {
182  temp = CreateDIB24(size);
183  } else {
184  temp = CreateCompatibleBitmap(dcCompatible, size.cx, size.cy);
185  }
186  if (temp == NULL) return FALSE;
187  Attach(temp);
188  return TRUE;
189  }
190 
191  void Dispose() {
192  if (m_bitmap != NULL) {
193  SelectBitmap(m_bitmapOld); m_bitmapOld = NULL;
194  m_bitmap.DeleteObject();
195  }
196  m_curSize = CSize(0,0);
197  }
198  BOOL GetBitmapPtr(t_uint8 * & ptr, t_ssize & lineWidth) {
199  if (m_bitmap == NULL) return FALSE;
200  BITMAP bmp = {};
201  if (!m_bitmap.GetBitmap(bmp)) return FALSE;
202  lineWidth = bmp.bmWidthBytes;
203  ptr = reinterpret_cast<t_uint8*>(bmp.bmBits);
204  return TRUE;
205  }
206  CSize GetSize() const { return m_curSize; }
207 
208  PFC_CLASS_NOT_COPYABLE_EX(CBackBuffer)
209 private:
210  CSize m_curSize;
211  CBitmap m_bitmap;
212  HBITMAP m_bitmapOld;
213 };
214 
215 class CBackBufferScope : public CDCHandle {
216 public:
217  CBackBufferScope(HDC hDC, HDC hDCBB, const CRect & rcPaint) : CDCHandle(hDCBB), m_dcOrig(hDC), m_rcPaint(rcPaint)
218  {
219  GetClipRgn(m_clipRgnOld);
220  CRgn temp;
221  if (m_dcOrig.GetClipRgn(temp) == 1) {
222  if (m_clipRgnOld != NULL) temp.CombineRgn(m_clipRgnOld,RGN_AND);
223  SelectClipRgn(temp);
224  }
225  IntersectClipRect(rcPaint);
226  }
227 
229  {
230  m_dcOrig.BitBlt(m_rcPaint.left,m_rcPaint.top,m_rcPaint.Width(),m_rcPaint.Height(),m_hDC,m_rcPaint.left,m_rcPaint.top,SRCCOPY);
231  SelectClipRgn(m_clipRgnOld);
232  }
233 private:
234  const CRect m_rcPaint;
235  CDCHandle m_dcOrig;
237 };
CFont m_font
Definition: GDIUtils.h:92
int sgn_t(const T &p_val)
Definition: primitives.h:669
uint8_t t_uint8
Definition: int_types.h:9
static void CreatePreferencesHeaderFont(CFont &out, CWindow source)
Definition: GDIUtils.h:65
CDCPen(HDC dc, COLORREF color)
Definition: GDIUtils.h:142
void SHARED_EXPORT scale(const audio_sample *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
p_source/p_output can point to same buffer
CFontScaled(HFONT _in, double scale)
Definition: GDIUtils.h:97
static void CreatePreferencesHeaderFont2(CFont &out, CWindow source)
Definition: GDIUtils.h:69
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
void Initialize(CWindow wnd, double scale, int weight)
Definition: GDIUtils.h:76
HRGN OldVal() const
Definition: GDIUtils.h:111
void Attach(HBITMAP bmp)
Definition: GDIUtils.h:170
~CDCPen()
Definition: GDIUtils.h:147
const CRect m_rcPaint
Definition: GDIUtils.h:234
static HBITMAP CreateDIB16(CSize size)
Definition: GDIUtils.h:15
void MakeHeader(CWindow wnd)
Definition: GDIUtils.h:80
CDCBrush(HDC dc, COLORREF color)
Definition: GDIUtils.h:126
CDCHandle m_dcOrig
Definition: GDIUtils.h:235
CSize GetSize() const
Definition: GDIUtils.h:206
static void CreateScaledFont(CFont &out, CFontHandle in, double scale)
Definition: GDIUtils.h:46
CDCHandle m_dc
Definition: GDIUtils.h:151
t_int32 rint32(double p_val)
Definition: other.cpp:209
~CBackBuffer()
Definition: GDIUtils.h:162
static HBRUSH MakeTempBrush(HDC dc, COLORREF color)
Definition: GDIUtils.h:120
~CDCBrush()
Definition: GDIUtils.h:131
static void CreateScaledFontEx(CFont &out, CFontHandle in, double scale, int weight)
Definition: GDIUtils.h:55
CBackBufferScope(HDC hDC, HDC hDCBB, const CRect &rcPaint)
Definition: GDIUtils.h:217
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
Definition: int_types.h:49
DCClipRgnScope(HDC dc)
Definition: GDIUtils.h:104
void Attach(HBITMAP bmp, CSize size)
Definition: GDIUtils.h:165
COLORREF m_oldCol
Definition: GDIUtils.h:152
static HBITMAP CreateDIB8(CSize size, const COLORREF palette[256])
Definition: GDIUtils.h:29
void _initWnd(CWindow wnd)
Definition: GDIUtils.h:89
BOOL GetBitmapPtr(t_uint8 *&ptr, t_ssize &lineWidth)
Definition: GDIUtils.h:198
static HBITMAP CreateDIB24(CSize size)
Definition: GDIUtils.h:1
void MakeHeader2(CWindow wnd)
Definition: GDIUtils.h:84
BOOL Allocate(CSize size, HDC dcCompatible=NULL)
Definition: GDIUtils.h:176
void Dispose()
Definition: GDIUtils.h:191