foobar2000 SDK  2015-08-03
win32_dialog.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #ifdef _WIN32
4 
5 namespace dialog_helper {
6 
7 
8  INT_PTR CALLBACK dialog::DlgProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
9  {
10  dialog * p_this;
11  BOOL rv;
12  if (msg==WM_INITDIALOG)
13  {
14  p_this = reinterpret_cast<dialog*>(lp);
15  p_this->wnd = wnd;
16  SetWindowLongPtr(wnd,DWLP_USER,lp);
17 
18  if (p_this->m_is_modal) p_this->m_modal_scope.initialize(wnd);
19  }
20  else p_this = reinterpret_cast<dialog*>(GetWindowLongPtr(wnd,DWLP_USER));
21 
22  rv = p_this ? p_this->on_message(msg,wp,lp) : FALSE;
23 
24  if (msg==WM_DESTROY && p_this)
25  {
26  SetWindowLongPtr(wnd,DWLP_USER,0);
27 // p_this->wnd = 0;
28  }
29 
30  return rv;
31  }
32 
33 
34  int dialog::run_modal(unsigned id,HWND parent)
35  {
36  assert(wnd == 0);
37  if (wnd != 0) return -1;
38  m_is_modal = true;
39  return uDialogBox(id,parent,DlgProc,reinterpret_cast<LPARAM>(this));
40  }
41  HWND dialog::run_modeless(unsigned id,HWND parent)
42  {
43  assert(wnd == 0);
44  if (wnd != 0) return 0;
45  m_is_modal = false;
46  return uCreateDialog(id,parent,DlgProc,reinterpret_cast<LPARAM>(this));
47  }
48 
49  void dialog::end_dialog(int code)
50  {
51  assert(m_is_modal);
52  if (m_is_modal) uEndDialog(wnd,code);
53  }
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64  int dialog_modal::run(unsigned p_id,HWND p_parent,HINSTANCE p_instance)
65  {
66  int status;
67 
68  // note: uDialogBox() has its own modal scope, we don't want that to trigger
69  // if this is ever changed, move deinit to WM_DESTROY handler in DlgProc
70 
71  status = (int)DialogBoxParam(p_instance,MAKEINTRESOURCE(p_id),p_parent,DlgProc,reinterpret_cast<LPARAM>(this));
72 
74 
75  return status;
76  }
77 
78  void dialog_modal::end_dialog(int p_code)
79  {
80  EndDialog(m_wnd,p_code);
81  }
82 
83 
84  INT_PTR CALLBACK dialog_modal::DlgProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
85  {
86  dialog_modal * _this;
87  if (msg==WM_INITDIALOG)
88  {
89  _this = reinterpret_cast<dialog_modal*>(lp);
90  _this->m_wnd = wnd;
91  SetWindowLongPtr(wnd,DWLP_USER,lp);
92 
93  _this->m_modal_scope.initialize(wnd);
94  }
95  else _this = reinterpret_cast<dialog_modal*>(GetWindowLongPtr(wnd,DWLP_USER));
96 
97  assert(_this == 0 || _this->m_wnd == wnd);
98 
99  return _this ? _this->on_message(msg,wp,lp) : FALSE;
100  }
101 
102 
103  bool dialog_modeless::create(unsigned p_id,HWND p_parent,HINSTANCE p_instance) {
104  assert(!m_is_in_create);
105  if (m_is_in_create) return false;
106  pfc::vartoggle_t<bool> scope(m_is_in_create,true);
107  if (CreateDialogParam(p_instance,MAKEINTRESOURCE(p_id),p_parent,DlgProc,reinterpret_cast<LPARAM>(this)) == 0) return false;
108  return m_wnd != 0;
109  }
110 
112  assert(!m_is_in_create);
113  switch(m_destructor_status)
114  {
115  case destructor_none:
116  m_destructor_status = destructor_normal;
117  if (m_wnd != 0)
118  {
119  DestroyWindow(m_wnd);
120  m_wnd = 0;
121  }
122  break;
123  case destructor_fromwindow:
124  if (m_wnd != 0) SetWindowLongPtr(m_wnd,DWLP_USER,0);
125  break;
126  default:
127  //should never trigger
128  pfc::crash();
129  break;
130  }
131  }
132 
134  {
135  if (m_is_in_create)
136  {
137  m_wnd = 0;
138  }
139  else
140  switch(m_destructor_status)
141  {
142  case destructor_none:
143  m_destructor_status = destructor_fromwindow;
144  delete this;
145  break;
146  case destructor_fromwindow:
147  pfc::crash();
148  break;
149  default:
150  break;
151  }
152  }
153 
154  BOOL dialog_modeless::on_message_wrap(UINT msg,WPARAM wp,LPARAM lp)
155  {
156  if (m_destructor_status == destructor_none)
157  return on_message(msg,wp,lp);
158  else
159  return FALSE;
160  }
161 
162  INT_PTR CALLBACK dialog_modeless::DlgProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
163  {
164  dialog_modeless * thisptr;
165  BOOL rv;
166  if (msg == WM_INITDIALOG)
167  {
168  thisptr = reinterpret_cast<dialog_modeless*>(lp);
169  thisptr->m_wnd = wnd;
170  SetWindowLongPtr(wnd,DWLP_USER,lp);
172  }
173  else thisptr = reinterpret_cast<dialog_modeless*>(GetWindowLongPtr(wnd,DWLP_USER));
174 
175  rv = thisptr ? thisptr->on_message_wrap(msg,wp,lp) : FALSE;
176 
177  if (msg == WM_DESTROY)
179 
180  if (msg == WM_DESTROY && thisptr != 0)
181  thisptr->on_window_destruction();
182 
183  return rv;
184  }
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201  dialog_modeless_v2::dialog_modeless_v2(unsigned p_id,HWND p_parent,HINSTANCE p_instance,bool p_stealfocus) : m_wnd(0), m_status(status_construction), m_stealfocus(p_stealfocus)
202  {
203  WIN32_OP( CreateDialogParam(p_instance,MAKEINTRESOURCE(p_id),p_parent,DlgProc,reinterpret_cast<LPARAM>(this)) != NULL );
205  }
206 
208  {
209  bool is_window_being_destroyed = (m_status == status_destruction_requested);
211 
212  if (m_wnd != 0)
213  {
214  if (is_window_being_destroyed)
215  detach_window();
216  else
217  DestroyWindow(m_wnd);
218  }
219  }
220 
221  INT_PTR CALLBACK dialog_modeless_v2::DlgProc(HWND wnd,UINT msg,WPARAM wp,LPARAM lp)
222  {
223  dialog_modeless_v2 * thisptr;
224  BOOL rv = FALSE;
225  if (msg == WM_INITDIALOG)
226  {
227  thisptr = reinterpret_cast<dialog_modeless_v2*>(lp);
228  assert(thisptr->m_status == status_construction);
229  thisptr->m_wnd = wnd;
230  SetWindowLongPtr(wnd,DWLP_USER,lp);
231  if (GetWindowLong(wnd,GWL_STYLE) & WS_POPUP) {
233  }
234  }
235  else thisptr = reinterpret_cast<dialog_modeless_v2*>(GetWindowLongPtr(wnd,DWLP_USER));
236 
237  if (thisptr != NULL) rv = thisptr->on_message_internal(msg,wp,lp);
238 
239  if (msg == WM_DESTROY)
240  {
242  }
243 
244  return rv;
245  }
246 
247 
249  {
250  if (m_wnd != 0)
251  {
252  SetWindowLongPtr(m_wnd,DWLP_USER,0);
253  m_wnd = 0;
254  }
255  }
256 
257 
258  BOOL dialog_modeless_v2::on_message_internal(UINT msg,WPARAM wp,LPARAM lp)
259  {
261  {
262  if (msg == WM_DESTROY)
263  {
264  assert(m_status == status_lifetime);
266  delete this;
267  return TRUE;
268  }
269  else
270  return on_message(msg,wp,lp);
271  }
272  else if (m_status == status_construction)
273  {
274  if (msg == WM_INITDIALOG) return m_stealfocus ? TRUE : FALSE;
275  else return FALSE;
276  }
277  else return FALSE;
278  }
279 }
280 
281 HWND uCreateDialog(UINT id,HWND parent,DLGPROC proc,LPARAM param)
282 {
283  return CreateDialogParam(core_api::get_my_instance(),MAKEINTRESOURCE(id),parent,proc,param);
284 }
285 
286 int uDialogBox(UINT id,HWND parent,DLGPROC proc,LPARAM param)
287 {
288  return (int)DialogBoxParam(core_api::get_my_instance(),MAKEINTRESOURCE(id),parent,proc,param);
289 }
290 
291 #endif
void end_dialog(int p_code)
static INT_PTR CALLBACK DlgProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
void initialize(HWND p_wnd)
Initializes the scope with specified window handle.
Definition: shared.h:519
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
HWND uCreateDialog(UINT id, HWND parent, DLGPROC proc, LPARAM param)
Wrapper (provided mainly for old code), simplifies parameters compared to standard CreateDialog() by ...
BOOL on_message_wrap(UINT msg, WPARAM wp, LPARAM lp)
void deinitialize()
Definition: shared.h:530
This class is meant to be instantiated on-stack, as a local variable. Using new/delete operators inst...
Definition: win32_dialog.h:38
static INT_PTR CALLBACK DlgProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
Definition: win32_dialog.cpp:8
static void g_add(HWND p_wnd)
Static helper; see add().
static INT_PTR CALLBACK DlgProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
int uDialogBox(UINT id, HWND parent, DLGPROC proc, LPARAM param)
Wrapper (provided mainly for old code), simplifies parameters compared to standard DialogBox() by usi...
void end_dialog(int code)
static INT_PTR CALLBACK DlgProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
modal_dialog_scope m_modal_scope
Definition: win32_dialog.h:52
enum dialog_helper::dialog_modeless_v2::@24 m_status
dialog_modeless_v2(const dialog_modeless_v2 &)
void crash()
Definition: other.cpp:112
virtual BOOL on_message(UINT msg, WPARAM wp, LPARAM lp)=0
modal_dialog_scope m_modal_scope
Definition: win32_dialog.h:34
virtual BOOL on_message(UINT msg, WPARAM wp, LPARAM lp)
Definition: win32_dialog.h:100
void create(pfc::string_formatter &p_out, const t_entry_list &p_data)
Definition: cue_creator.cpp:44
static void g_remove(HWND p_wnd)
Static helper; see remove().
HINSTANCE get_my_instance()
Retrieves HINSTANCE of calling DLL.
This class is meant to be used with new/delete operators only. Destroying the window - outside create...
Definition: win32_dialog.h:69
BOOL on_message_internal(UINT msg, WPARAM wp, LPARAM lp)
virtual BOOL on_message(UINT msg, WPARAM wp, LPARAM lp)=0