foobar2000 SDK  2015-01-14
win-objects.cpp
Go to the documentation of this file.
1 #include "pfc.h"
2 
3 #ifdef _WIN32
4 
6  switch(p_code) {
7  case ERROR_CHILD_NOT_COMPLETE:
8  p_out = "Application cannot be run in Win32 mode.";
9  return TRUE;
10  case ERROR_INVALID_ORDINAL:
11  p_out = "Invalid ordinal.";
12  return TRUE;
13  case ERROR_INVALID_STARTING_CODESEG:
14  p_out = "Invalid code segment.";
15  return TRUE;
16  case ERROR_INVALID_STACKSEG:
17  p_out = "Invalid stack segment.";
18  return TRUE;
19  case ERROR_INVALID_MODULETYPE:
20  p_out = "Invalid module type.";
21  return TRUE;
22  case ERROR_INVALID_EXE_SIGNATURE:
23  p_out = "Invalid executable signature.";
24  return TRUE;
25  case ERROR_BAD_EXE_FORMAT:
26  p_out = "Not a valid Win32 application.";
27  return TRUE;
28  case ERROR_EXE_MACHINE_TYPE_MISMATCH:
29  p_out = "Machine type mismatch.";
30  return TRUE;
31  case ERROR_EXE_CANNOT_MODIFY_SIGNED_BINARY:
32  case ERROR_EXE_CANNOT_MODIFY_STRONG_SIGNED_BINARY:
33  p_out = "Unable to modify a signed binary.";
34  return TRUE;
35  default:
36  {
37  TCHAR temp[512];
38  if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,p_code,0,temp,_countof(temp),0) == 0) return FALSE;
39  for(t_size n=0;n<_countof(temp);n++) {
40  switch(temp[n]) {
41  case '\n':
42  case '\r':
43  temp[n] = ' ';
44  break;
45  }
46  }
47  p_out = stringcvt::string_utf8_from_os(temp,_countof(temp));
48  return TRUE;
49  }
50  break;
51  }
52 }
53 void pfc::winPrefixPath(pfc::string_base & out, const char * p_path) {
54  const char * prepend_header = "\\\\?\\";
55  const char * prepend_header_net = "\\\\?\\UNC\\";
56  if (pfc::strcmp_partial( p_path, prepend_header ) == 0) { out = p_path; return; }
57  out.reset();
58  if (pfc::strcmp_partial(p_path,"\\\\") != 0) {
59  out << prepend_header << p_path;
60  } else {
61  out << prepend_header_net << (p_path+2);
62  }
63 };
64 
65 void pfc::winUnPrefixPath(pfc::string_base & out, const char * p_path) {
66  const char * prepend_header = "\\\\?\\";
67  const char * prepend_header_net = "\\\\?\\UNC\\";
68  if (pfc::strcmp_partial(p_path, prepend_header_net) == 0) {
69  out = pfc::string_formatter() << "\\\\" << (p_path + strlen(prepend_header_net) );
70  return;
71  }
72  if (pfc::strcmp_partial(p_path, prepend_header) == 0) {
73  out = (p_path + strlen(prepend_header));
74  return;
75  }
76  out = p_path;
77 }
78 
80  LastErrorRevertScope revert;
81  if (p_code == 0) m_buffer = "Undefined error";
82  else if (!pfc::winFormatSystemErrorMessage(m_buffer,p_code)) m_buffer << "Unknown error code (" << (unsigned)p_code << ")";
83 }
84 
86  if (!pfc::winFormatSystemErrorMessage(m_buffer,(DWORD)p_code)) m_buffer = "Unknown error code";
87  stamp_hex(p_code);
88 }
89 format_hresult::format_hresult(HRESULT p_code, const char * msgOverride) {
90  m_buffer = msgOverride;
91  stamp_hex(p_code);
92 }
93 
94 void format_hresult::stamp_hex(HRESULT p_code) {
95  m_buffer << " (0x" << pfc::format_hex((t_uint32)p_code, 8) << ")";
96 }
97 
98 
99 void uAddWindowStyle(HWND p_wnd,LONG p_style) {
100  SetWindowLong(p_wnd,GWL_STYLE, GetWindowLong(p_wnd,GWL_STYLE) | p_style);
101 }
102 
103 void uRemoveWindowStyle(HWND p_wnd,LONG p_style) {
104  SetWindowLong(p_wnd,GWL_STYLE, GetWindowLong(p_wnd,GWL_STYLE) & ~p_style);
105 }
106 
107 void uAddWindowExStyle(HWND p_wnd,LONG p_style) {
108  SetWindowLong(p_wnd,GWL_EXSTYLE, GetWindowLong(p_wnd,GWL_EXSTYLE) | p_style);
109 }
110 
111 void uRemoveWindowExStyle(HWND p_wnd,LONG p_style) {
112  SetWindowLong(p_wnd,GWL_EXSTYLE, GetWindowLong(p_wnd,GWL_EXSTYLE) & ~p_style);
113 }
114 
115 unsigned MapDialogWidth(HWND p_dialog,unsigned p_value) {
116  RECT temp;
117  temp.left = 0; temp.right = p_value; temp.top = temp.bottom = 0;
118  if (!MapDialogRect(p_dialog,&temp)) return 0;
119  return temp.right;
120 }
121 
122 bool IsKeyPressed(unsigned vk) {
123  return (GetKeyState(vk) & 0x8000) ? true : false;
124 }
125 
128  unsigned ret = 0;
129  if (IsKeyPressed(VK_CONTROL)) ret |= MOD_CONTROL;
130  if (IsKeyPressed(VK_SHIFT)) ret |= MOD_SHIFT;
131  if (IsKeyPressed(VK_MENU)) ret |= MOD_ALT;
132  if (IsKeyPressed(VK_LWIN) || IsKeyPressed(VK_RWIN)) ret |= MOD_WIN;
133  return ret;
134 }
135 
136 
137 
138 bool CClipboardOpenScope::Open(HWND p_owner) {
139  Close();
140  if (OpenClipboard(p_owner)) {
141  m_open = true;
142  return true;
143  } else {
144  return false;
145  }
146 }
148  if (m_open) {
149  m_open = false;
150  CloseClipboard();
151  }
152 }
153 
154 
155 CGlobalLockScope::CGlobalLockScope(HGLOBAL p_handle) : m_handle(p_handle), m_ptr(GlobalLock(p_handle)) {
156  if (m_ptr == NULL) throw std::bad_alloc();
157 }
159  if (m_ptr != NULL) GlobalUnlock(m_handle);
160 }
161 
162 bool IsPointInsideControl(const POINT& pt, HWND wnd) {
163  HWND walk = WindowFromPoint(pt);
164  for(;;) {
165  if (walk == NULL) return false;
166  if (walk == wnd) return true;
167  if (GetWindowLong(walk,GWL_STYLE) & WS_POPUP) return false;
168  walk = GetParent(walk);
169  }
170 }
171 
172 bool IsWindowChildOf(HWND child, HWND parent) {
173  HWND walk = child;
174  while(walk != parent && walk != NULL && (GetWindowLong(walk,GWL_STYLE) & WS_CHILD) != 0) {
175  walk = GetParent(walk);
176  }
177  return walk == parent;
178 }
179 
181  if (m_menu != NULL) {
182  DestroyMenu(m_menu);
183  m_menu = NULL;
184  }
185 }
186 
188  release();
189  SetLastError(NO_ERROR);
190  m_menu = CreatePopupMenu();
191  if (m_menu == NULL) throw exception_win32(GetLastError());
192 }
193 
194 void win32_event::create(bool p_manualreset,bool p_initialstate) {
195  release();
196  SetLastError(NO_ERROR);
197  m_handle = CreateEvent(NULL,p_manualreset ? TRUE : FALSE, p_initialstate ? TRUE : FALSE,NULL);
198  if (m_handle == NULL) throw exception_win32(GetLastError());
199 }
200 
202  HANDLE temp = detach();
203  if (temp != NULL) CloseHandle(temp);
204 }
205 
206 
207 DWORD win32_event::g_calculate_wait_time(double p_seconds) {
208  DWORD time = 0;
209  if (p_seconds> 0) {
210  time = pfc::rint32(p_seconds * 1000.0);
211  if (time == 0) time = 1;
212  } else if (p_seconds < 0) {
213  time = INFINITE;
214  }
215  return time;
216 }
217 
219 bool win32_event::g_wait_for(HANDLE p_event,double p_timeout_seconds) {
220  SetLastError(NO_ERROR);
221  DWORD status = WaitForSingleObject(p_event,g_calculate_wait_time(p_timeout_seconds));
222  switch(status) {
223  case WAIT_FAILED:
224  throw exception_win32(GetLastError());
225  default:
226  throw pfc::exception_bug_check();
227  case WAIT_OBJECT_0:
228  return true;
229  case WAIT_TIMEOUT:
230  return false;
231  }
232 }
233 
234 void win32_event::set_state(bool p_state) {
235  PFC_ASSERT(m_handle != NULL);
236  if (p_state) SetEvent(m_handle);
237  else ResetEvent(m_handle);
238 }
239 
240 int win32_event::g_twoEventWait( HANDLE ev1, HANDLE ev2, double timeout ) {
241  HANDLE h[2] = {ev1, ev2};
242  switch(WaitForMultipleObjects(2, h, FALSE, g_calculate_wait_time( timeout ) )) {
243  default:
244  pfc::crash();
245  case WAIT_TIMEOUT:
246  return 0;
247  case WAIT_OBJECT_0:
248  return 1;
249  case WAIT_OBJECT_0 + 1:
250  return 2;
251  }
252 }
253 
254 int win32_event::g_twoEventWait( win32_event & ev1, win32_event & ev2, double timeout ) {
255  return g_twoEventWait( ev1.get_handle(), ev2.get_handle(), timeout );
256 }
257 
259  HICON temp = detach();
260  if (temp != NULL) DestroyIcon(temp);
261 }
262 
263 
264 void win32_accelerator::load(HINSTANCE p_inst,const TCHAR * p_id) {
265  release();
266  SetLastError(NO_ERROR);
267  m_accel = LoadAccelerators(p_inst,p_id);
268  if (m_accel == NULL) {
269  throw exception_win32(GetLastError());
270  }
271 }
272 
274  if (m_accel != NULL) {
275  DestroyAcceleratorTable(m_accel);
276  m_accel = NULL;
277  }
278 }
279 
280 void uSleepSeconds(double p_time,bool p_alertable) {
281  SleepEx(win32_event::g_calculate_wait_time(p_time),p_alertable ? TRUE : FALSE);
282 }
283 
284 
285 WORD GetWindowsVersionCode() throw() {
286  const DWORD ver = GetVersion();
287  return (WORD)HIBYTE(LOWORD(ver)) | ((WORD)LOBYTE(LOWORD(ver)) << 8);
288 }
289 
290 
291 namespace pfc {
292  bool isShiftKeyPressed() {
293  return IsKeyPressed(VK_SHIFT);
294  }
295  bool isCtrlKeyPressed() {
296  return IsKeyPressed(VK_CONTROL);
297  }
298  bool isAltKeyPressed() {
299  return IsKeyPressed(VK_MENU);
300  }
301 }
302 #endif
void release()
void stamp_hex(HRESULT p_code)
Definition: win-objects.cpp:94
void uSleepSeconds(double p_time, bool p_alertable)
pfc::string_formatter m_buffer
Definition: win-objects.h:39
void uAddWindowStyle(HWND p_wnd, LONG p_style)
Definition: win-objects.cpp:99
CGlobalLockScope(HGLOBAL p_handle)
BOOL winFormatSystemErrorMessage(pfc::string_base &p_out, DWORD p_code)
Definition: win-objects.cpp:5
void load(HINSTANCE p_inst, const TCHAR *p_id)
void create_popup()
void uRemoveWindowStyle(HWND p_wnd, LONG p_style)
unsigned MapDialogWidth(HWND p_dialog, unsigned p_value)
bool isShiftKeyPressed()
Definition: obj-c.mm:32
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
HANDLE get_handle() const
Definition: win-objects.h:129
HICON detach()
Definition: win-objects.h:168
bool isCtrlKeyPressed()
Definition: obj-c.mm:39
void release()
bool IsKeyPressed(unsigned vk)
bool isAltKeyPressed()
Definition: obj-c.mm:46
void winPrefixPath(pfc::string_base &out, const char *p_path)
Definition: win-objects.cpp:53
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
HGLOBAL m_handle
Definition: win-objects.h:79
static int g_twoEventWait(win32_event &ev1, win32_event &ev2, double timeout)
string_utf8_from_wide string_utf8_from_os
Definition: string_conv.h:517
format_hresult(HRESULT p_code)
Definition: win-objects.cpp:85
static bool g_wait_for(HANDLE p_event, double p_timeout_seconds)
Returns true when signaled, false on timeout.
void uAddWindowExStyle(HWND p_wnd, LONG p_style)
void winUnPrefixPath(pfc::string_base &out, const char *p_path)
Definition: win-objects.cpp:65
format_win32_error(DWORD p_code)
Definition: win-objects.cpp:79
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:614
void crash()
Definition: other.cpp:105
HANDLE m_handle
Definition: win-objects.h:152
HMENU m_menu
Definition: win-objects.h:117
t_int32 rint32(double p_val)
Definition: primitives.h:712
unsigned GetHotkeyModifierFlags()
Returns current modifier keys pressed, using win32 MOD_* flags.
void release()
WORD GetWindowsVersionCode()
static DWORD g_calculate_wait_time(double p_seconds)
bool IsPointInsideControl(const POINT &pt, HWND wnd)
bool IsWindowChildOf(HWND child, HWND parent)
void uRemoveWindowExStyle(HWND p_wnd, LONG p_style)
int strcmp_partial(const char *str, const char *substr)
Definition: string_base.h:1088
HANDLE detach()
Definition: win-objects.h:130
bool Open(HWND p_owner)
void create(bool p_manualreset, bool p_initialstate)
uint32_t t_uint32
Definition: int_types.h:5
pfc::string8 m_buffer
Definition: win-objects.h:27
void set_state(bool p_state)