ASPiK SDK
win32support.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __win32support__
6 #define __win32support__
7 
8 #include "../../vstguibase.h"
9 
10 #if WINDOWS
11 
12 #include "../../cbitmap.h"
13 #include "../../optional.h"
14 
15 #include <algorithm>
16 using std::min;
17 using std::max;
18 #include <windows.h>
19 #if defined (WINAPI_FAMILY_SYSTEM)
20 #include <versionhelpers.h>
21 #endif
22 
23 #include <objidl.h>
24 #include <gdiplus.h>
25 
26 interface ID2D1Factory;
27 interface IDWriteFactory;
28 interface IWICImagingFactory;
29 
30 struct VstKeyCode;
31 
32 namespace VSTGUI {
33 
34 #define VSTGUI_STRCMP wcscmp
35 #define VSTGUI_STRCPY wcscpy
36 #define VSTGUI_SPRINTF wsprintf
37 #define VSTGUI_STRRCHR wcschr
38 #define VSTGUI_STRICMP _wcsicmp
39 #define VSTGUI_STRLEN wcslen
40 #define VSTGUI_STRCAT wcscat
41 
42 class CDrawContext;
43 
44 extern HINSTANCE GetInstance ();
45 #ifndef VERSIONHELPERAPI
46 extern const OSVERSIONINFOEX& getSystemVersion();
47 extern const bool IsWindowsVistaOrGreater();
48 #endif
49 extern ID2D1Factory* getD2DFactory ();
50 extern IWICImagingFactory* getWICImageingFactory ();
51 extern void useD2D ();
52 extern void unuseD2D ();
53 extern IDWriteFactory* getDWriteFactory ();
54 extern CDrawContext* createDrawContext (HWND window, HDC device, const CRect& surfaceRect);
55 extern void useD2DHardwareRenderer (bool state);
56 extern Optional<VstKeyCode> keyMessageToKeyCode (WPARAM wParam, LPARAM lParam);
57 
59 class GDIPlusGlobals : public AtomicReferenceCounted
60 {
61 public:
62  static void enter ();
63  static void exit ();
64 protected:
65  GDIPlusGlobals ();
66  ~GDIPlusGlobals () noexcept;
67 
68  static GDIPlusGlobals* gInstance;
69  ULONG_PTR gdiplusToken;
70 };
71 
72 class UTF8StringHelper
73 {
74 public:
75  UTF8StringHelper (const char* utf8Str) : utf8Str (utf8Str), allocWideStr (0), allocStrIsWide (true) {}
76  UTF8StringHelper (const WCHAR* wideStr) : wideStr (wideStr), allocUTF8Str (0), allocStrIsWide (false) {}
77  ~UTF8StringHelper () noexcept
78  {
79  if (allocUTF8Str)
80  std::free (allocUTF8Str);
81  }
82 
83  operator const char* () { return getUTF8String (); }
84  operator const WCHAR*() { return getWideString (); }
85 
86  const WCHAR* getWideString ()
87  {
88  if (!allocStrIsWide)
89  return wideStr;
90  else
91  {
92  if (!allocWideStr && utf8Str)
93  {
94  int numChars = MultiByteToWideChar (CP_UTF8, 0, utf8Str, -1, 0, 0);
95  allocWideStr = (WCHAR*)::std::malloc ((static_cast<size_t> (numChars)+1)*2);
96  if (MultiByteToWideChar (CP_UTF8, 0, utf8Str, -1, allocWideStr, numChars) == 0)
97  {
98  allocWideStr[0] = 0;
99  }
100  }
101  return allocWideStr;
102  }
103  }
104  const char* getUTF8String ()
105  {
106  if (allocStrIsWide)
107  return utf8Str;
108  else
109  {
110  if (!allocUTF8Str && wideStr)
111  {
112  int allocSize = WideCharToMultiByte (CP_UTF8, 0, wideStr, -1, 0, 0, 0, 0);
113  allocUTF8Str = (char*)::std::malloc (static_cast<size_t> (allocSize)+1);
114  if (WideCharToMultiByte (CP_UTF8, 0, wideStr, -1, allocUTF8Str, allocSize, 0, 0) == 0)
115  {
116  allocUTF8Str[0] = 0;
117  }
118  }
119  return allocUTF8Str;
120  }
121  }
122 protected:
123  union {
124  const char* utf8Str;
125  const WCHAR* wideStr;
126  };
127  union {
128  WCHAR* allocWideStr;
129  char* allocUTF8Str;
130  };
131 
132  bool allocStrIsWide;
133 };
134 
135 class ResourceStream : public IStream
136 {
137 public:
138  ResourceStream ();
139  ~ResourceStream () noexcept = default;
140 
141  bool open (const CResourceDescription& resourceDesc, const char* type);
142 
143  virtual HRESULT STDMETHODCALLTYPE Read (void *pv, ULONG cb, ULONG *pcbRead);
144  virtual HRESULT STDMETHODCALLTYPE Write (const void *pv, ULONG cb, ULONG *pcbWritten);
145  virtual HRESULT STDMETHODCALLTYPE Seek (LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition);
146  virtual HRESULT STDMETHODCALLTYPE SetSize (ULARGE_INTEGER libNewSize);
147  virtual HRESULT STDMETHODCALLTYPE CopyTo (IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten);
148  virtual HRESULT STDMETHODCALLTYPE Commit (DWORD grfCommitFlags);
149  virtual HRESULT STDMETHODCALLTYPE Revert (void);
150  virtual HRESULT STDMETHODCALLTYPE LockRegion (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
151  virtual HRESULT STDMETHODCALLTYPE UnlockRegion (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
152  virtual HRESULT STDMETHODCALLTYPE Stat (STATSTG *pstatstg, DWORD grfStatFlag);
153  virtual HRESULT STDMETHODCALLTYPE Clone (IStream **ppstm);
154  virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
155  virtual ULONG STDMETHODCALLTYPE AddRef(void);
156  virtual ULONG STDMETHODCALLTYPE Release(void);
157 
158 protected:
159  HGLOBAL resData;
160  uint32_t streamPos;
161  uint32_t resSize;
162  LONG _refcount;
163 };
164 
166 
167 } // namespace
168 
169 #endif // WINDOWS
170 
171 #endif
Definition: vstkeycode.h:12
Definition: customcontrols.cpp:8