foobar2000 SDK  2015-08-03
win-objects.h
Go to the documentation of this file.
1 namespace pfc {
3 
4  // Prefix filesystem paths with \\?\ and \\?\UNC where appropriate.
5  void winPrefixPath(pfc::string_base & out, const char * p_path);
6  // Reverse winPrefixPath
7  void winUnPrefixPath(pfc::string_base & out, const char * p_path);
8 }
9 
10 
12 public:
13  LastErrorRevertScope() : m_val(GetLastError()) {}
14  ~LastErrorRevertScope() {SetLastError(m_val);}
15 
16 private:
17  const DWORD m_val;
18 };
19 
21 public:
22  format_win32_error(DWORD p_code);
23 
24  const char * get_ptr() const {return m_buffer.get_ptr();}
25  operator const char*() const {return m_buffer.get_ptr();}
26 private:
28 };
29 
31 public:
32  format_hresult(HRESULT p_code);
33  format_hresult(HRESULT p_code, const char * msgOverride);
34 
35  const char * get_ptr() const {return m_buffer.get_ptr();}
36  operator const char*() const {return m_buffer.get_ptr();}
37 private:
38  void stamp_hex(HRESULT p_code);
40 };
41 
43  exception_win32(DWORD p_code) : std::exception(format_win32_error(p_code)), m_code(p_code) {}
44  DWORD get_code() const {return m_code;}
45 private:
46  DWORD m_code;
47 };
48 
49 #ifdef PFC_WINDOWS_DESKTOP_APP
50 
51 void uAddWindowStyle(HWND p_wnd,LONG p_style);
52 void uRemoveWindowStyle(HWND p_wnd,LONG p_style);
53 void uAddWindowExStyle(HWND p_wnd,LONG p_style);
54 void uRemoveWindowExStyle(HWND p_wnd,LONG p_style);
55 unsigned MapDialogWidth(HWND p_dialog,unsigned p_value);
56 bool IsKeyPressed(unsigned vk);
57 
59 unsigned GetHotkeyModifierFlags();
60 
62 public:
63  CClipboardOpenScope() : m_open(false) {}
64  ~CClipboardOpenScope() {Close();}
65  bool Open(HWND p_owner);
66  void Close();
67 private:
68  bool m_open;
69 
70  PFC_CLASS_NOT_COPYABLE_EX(CClipboardOpenScope)
71 };
72 
74 public:
75  CGlobalLockScope(HGLOBAL p_handle);
77  void * GetPtr() const {return m_ptr;}
78  t_size GetSize() const {return GlobalSize(m_handle);}
79 private:
80  void * m_ptr;
81  HGLOBAL m_handle;
82 
83  PFC_CLASS_NOT_COPYABLE_EX(CGlobalLockScope)
84 };
85 
86 template<typename TItem> class CGlobalLockScopeT {
87 public:
88  CGlobalLockScopeT(HGLOBAL handle) : m_scope(handle) {}
89  TItem * GetPtr() const {return reinterpret_cast<TItem*>(m_scope.GetPtr());}
90  t_size GetSize() const {
91  const t_size val = m_scope.GetSize();
92  PFC_ASSERT( val % sizeof(TItem) == 0 );
93  return val / sizeof(TItem);
94  }
95 private:
97 };
98 
99 bool IsPointInsideControl(const POINT& pt, HWND wnd);
100 bool IsWindowChildOf(HWND child, HWND parent);
101 
102 class win32_menu {
103 public:
104  win32_menu(HMENU p_initval) : m_menu(p_initval) {}
105  win32_menu() : m_menu(NULL) {}
106  ~win32_menu() {release();}
107  void release();
108  void set(HMENU p_menu) {release(); m_menu = p_menu;}
109  void create_popup();
110  HMENU get() const {return m_menu;}
111  HMENU detach() {return pfc::replace_t(m_menu,(HMENU)NULL);}
112 
113  bool is_valid() const {return m_menu != NULL;}
114 private:
115  win32_menu(const win32_menu &);
116  const win32_menu & operator=(const win32_menu &);
117 
118  HMENU m_menu;
119 };
120 
121 #endif
122 
123 class win32_event {
124 public:
125  win32_event() : m_handle(NULL) {}
126  ~win32_event() {release();}
127 
128  void create(bool p_manualreset,bool p_initialstate);
129 
130  void set(HANDLE p_handle) {release(); m_handle = p_handle;}
131  HANDLE get() const {return m_handle;}
132  HANDLE get_handle() const {return m_handle;}
133  HANDLE detach() {return pfc::replace_t(m_handle,(HANDLE)NULL);}
134  bool is_valid() const {return m_handle != NULL;}
135 
136  void release();
137 
139  bool wait_for(double p_timeout_seconds) {return g_wait_for(get(),p_timeout_seconds);}
140 
141  static DWORD g_calculate_wait_time(double p_seconds);
142 
144  static bool g_wait_for(HANDLE p_event,double p_timeout_seconds);
145 
146  void set_state(bool p_state);
147 
148  // Two-wait event functions, return 0 on timeout, 1 on evt1 set, 2 on evt2 set
149  static int g_twoEventWait( win32_event & ev1, win32_event & ev2, double timeout );
150  static int g_twoEventWait( HANDLE ev1, HANDLE ev2, double timeout );
151 private:
152  win32_event(const win32_event&);
153  const win32_event & operator=(const win32_event &);
154 
156 };
157 
158 void uSleepSeconds(double p_time,bool p_alertable);
159 
160 #ifdef PFC_WINDOWS_DESKTOP_APP
161 
162 class win32_icon {
163 public:
164  win32_icon(HICON p_initval) : m_icon(p_initval) {}
165  win32_icon() : m_icon(NULL) {}
166  ~win32_icon() {release();}
167 
168  void release();
169 
170  void set(HICON p_icon) {release(); m_icon = p_icon;}
171  HICON get() const {return m_icon;}
172  HICON detach() {return pfc::replace_t(m_icon,(HICON)NULL);}
173 
174  bool is_valid() const {return m_icon != NULL;}
175 
176 private:
177  win32_icon(const win32_icon&) {throw pfc::exception_not_implemented();}
178  const win32_icon & operator=(const win32_icon &) {throw pfc::exception_not_implemented();}
179 
180  HICON m_icon;
181 };
182 
184 public:
185  win32_accelerator() : m_accel(NULL) {}
186  ~win32_accelerator() {release();}
187  HACCEL get() const {return m_accel;}
188 
189  void load(HINSTANCE p_inst,const TCHAR * p_id);
190  void release();
191 private:
192  HACCEL m_accel;
193  PFC_CLASS_NOT_COPYABLE(win32_accelerator,win32_accelerator);
194 };
195 
197 public:
198  SelectObjectScope(HDC p_dc,HGDIOBJ p_obj) throw() : m_dc(p_dc), m_obj(SelectObject(p_dc,p_obj)) {}
199  ~SelectObjectScope() throw() {SelectObject(m_dc,m_obj);}
200 private:
201  PFC_CLASS_NOT_COPYABLE_EX(SelectObjectScope)
202  HDC m_dc;
203  HGDIOBJ m_obj;
204 };
205 
207 public:
208  OffsetWindowOrgScope(HDC dc, const POINT & pt) throw() : m_dc(dc), m_pt(pt) {
209  OffsetWindowOrgEx(m_dc, m_pt.x, m_pt.y, NULL);
210  }
212  OffsetWindowOrgEx(m_dc, -m_pt.x, -m_pt.y, NULL);
213  }
214 
215 private:
216  const HDC m_dc;
217  const POINT m_pt;
218 };
220 public:
221  DCStateScope(HDC p_dc) throw() : m_dc(p_dc) {
222  m_state = SaveDC(m_dc);
223  }
224  ~DCStateScope() throw() {
225  RestoreDC(m_dc,m_state);
226  }
227 private:
228  const HDC m_dc;
229  int m_state;
230 };
231 #endif // #ifdef PFC_WINDOWS_DESKTOP_APP
232 
234 public:
235  exception_com(HRESULT p_code) : std::exception(format_hresult(p_code)), m_code(p_code) {}
236  exception_com(HRESULT p_code, const char * msg) : std::exception(format_hresult(p_code, msg)), m_code(p_code) {}
237  HRESULT get_code() const {return m_code;}
238 private:
239  HRESULT m_code;
240 };
241 
242 #ifdef PFC_WINDOWS_DESKTOP_APP
243 
244 // Same format as _WIN32_WINNT macro.
245 WORD GetWindowsVersionCode() throw();
246 
247 #endif
248 
250 template<typename TBase> class ImplementCOMRefCounter : public TBase {
251 public:
253  ULONG STDMETHODCALLTYPE AddRef() {
254  return ++m_refcounter;
255  }
256  ULONG STDMETHODCALLTYPE Release() {
257  long val = --m_refcounter;
258  if (val == 0) delete this;
259  return val;
260  }
261 protected:
263 private:
265 };
266 
267 
268 
269 template<typename TPtr>
271 public:
272  CoTaskMemObject() : m_ptr() {}
273 
274  ~CoTaskMemObject() {CoTaskMemFree(m_ptr);}
275  void Reset() {CoTaskMemFree(pfc::replace_null_t(m_ptr));}
276  TPtr * Receive() {Reset(); return &m_ptr;}
277 
278  TPtr m_ptr;
279  PFC_CLASS_NOT_COPYABLE(CoTaskMemObject, CoTaskMemObject<TPtr> );
280 };
281 
282 
283 namespace pfc {
284  bool isShiftKeyPressed();
285  bool isCtrlKeyPressed();
286  bool isAltKeyPressed();
287 
288 
289  class winHandle {
290  public:
291  winHandle(HANDLE h_ = INVALID_HANDLE_VALUE) : h(h_) {}
292  ~winHandle() { Close(); }
293  void Close() {
294  if (h != INVALID_HANDLE_VALUE) { CloseHandle(h); h = INVALID_HANDLE_VALUE; }
295  }
296 
297  void Attach(HANDLE h_) { Close(); h = h_; }
298  HANDLE Detach() { HANDLE t = h; h = INVALID_HANDLE_VALUE; return t; }
299 
300  HANDLE Get() const { return h; }
301  operator HANDLE() const { return h; }
302 
304  private:
305  winHandle(const winHandle&);
306  void operator=(const winHandle&);
307  };
308 }
309 
win32_menu(HMENU p_initval)
Definition: win-objects.h:104
pfc::refcounter m_refcounter
Definition: win-objects.h:264
HANDLE Detach()
Definition: win-objects.h:298
pfc::string_formatter m_buffer
Definition: win-objects.h:39
const char * get_ptr() const
Definition: win-objects.h:24
t_size GetSize() const
Definition: win-objects.h:90
bool IsKeyPressed(unsigned vk)
BOOL winFormatSystemErrorMessage(pfc::string_base &p_out, DWORD p_code)
Definition: win-objects.cpp:7
bool is_valid() const
Definition: win-objects.h:113
win32_icon(const win32_icon &)
Definition: win-objects.h:177
t_type replace_null_t(t_type &p_var)
Definition: primitives.h:688
CGlobalLockScopeT(HGLOBAL handle)
Definition: win-objects.h:88
HMENU detach()
Definition: win-objects.h:111
bool isShiftKeyPressed()
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
HANDLE get_handle() const
Definition: win-objects.h:132
DCStateScope(HDC p_dc)
Definition: win-objects.h:221
STL namespace.
HICON detach()
Definition: win-objects.h:172
winHandle(HANDLE h_=INVALID_HANDLE_VALUE)
Definition: win-objects.h:291
bool isCtrlKeyPressed()
void uRemoveWindowStyle(HWND p_wnd, LONG p_style)
const HDC m_dc
Definition: win-objects.h:228
OffsetWindowOrgScope(HDC dc, const POINT &pt)
Definition: win-objects.h:208
const win32_icon & operator=(const win32_icon &)
Definition: win-objects.h:178
WORD GetWindowsVersionCode()
void uRemoveWindowExStyle(HWND p_wnd, LONG p_style)
bool isAltKeyPressed()
void * GetPtr() const
Definition: win-objects.h:77
bool IsWindowChildOf(HWND child, HWND parent)
t_type replace_t(t_type &p_var, const t_newval &p_newval)
Definition: primitives.h:682
void winPrefixPath(pfc::string_base &out, const char *p_path)
Definition: win-objects.cpp:55
bool is_valid() const
Definition: win-objects.h:174
TEMPLATE_CONSTRUCTOR_FORWARD_FLOOD(ImplementCOMRefCounter, TBase) ULONG STDMETHODCALLTYPE AddRef()
Definition: win-objects.h:252
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
HGLOBAL m_handle
Definition: win-objects.h:81
CGlobalLockScope m_scope
Definition: win-objects.h:96
void uSleepSeconds(double p_time, bool p_alertable)
void winUnPrefixPath(pfc::string_base &out, const char *p_path)
Definition: win-objects.cpp:67
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:615
exception_com(HRESULT p_code, const char *msg)
Definition: win-objects.h:236
exception_com(HRESULT p_code)
Definition: win-objects.h:235
HANDLE m_handle
Definition: win-objects.h:155
HMENU m_menu
Definition: win-objects.h:118
bool IsPointInsideControl(const POINT &pt, HWND wnd)
const DWORD m_val
Definition: win-objects.h:17
HANDLE Get() const
Definition: win-objects.h:300
void Attach(HANDLE h_)
Definition: win-objects.h:297
void create(pfc::string_formatter &p_out, const t_entry_list &p_data)
Definition: cue_creator.cpp:44
HRESULT get_code() const
Definition: win-objects.h:237
Simple implementation of a COM reference counter. The initial reference count is zero, so it can be used with pfc::com_ptr_t<> with plain operator=/constructor rather than attach().
Definition: win-objects.h:250
TItem * GetPtr() const
Definition: win-objects.h:89
std::exception exception
Definition: primitives.h:193
HRESULT m_code
Definition: win-objects.h:239
SelectObjectScope(HDC p_dc, HGDIOBJ p_obj)
Definition: win-objects.h:198
const char * get_ptr() const
Definition: win-objects.h:35
HANDLE detach()
Definition: win-objects.h:133
exception_win32(DWORD p_code)
Definition: win-objects.h:43
void uAddWindowStyle(HWND p_wnd, LONG p_style)
bool is_valid() const
Definition: win-objects.h:134
t_size GetSize() const
Definition: win-objects.h:78
HICON m_icon
Definition: win-objects.h:180
unsigned GetHotkeyModifierFlags()
Returns current modifier keys pressed, using win32 MOD_* flags.
DWORD get_code() const
Definition: win-objects.h:44
win32_icon(HICON p_initval)
Definition: win-objects.h:164
bool wait_for(double p_timeout_seconds)
Returns true when signaled, false on timeout.
Definition: win-objects.h:139
virtual ~ImplementCOMRefCounter()
Definition: win-objects.h:262
TPtr * Receive()
Definition: win-objects.h:276
ULONG STDMETHODCALLTYPE Release()
Definition: win-objects.h:256
void uAddWindowExStyle(HWND p_wnd, LONG p_style)
pfc::string8 m_buffer
Definition: win-objects.h:27
unsigned MapDialogWidth(HWND p_dialog, unsigned p_value)