CFugue
UPDialog.h
1 #ifndef __UNIVERSALPROGRESSDIALOG_H_A0B4F977__97BA__43c0__83A2__6B64410E92E2
2 #define __UNIVERSALPROGRESSDIALOG_H_A0B4F977__97BA__43c0__83A2__6B64410E92E2
3 
4 #include <TChar.h>
5 #include "InitCommonControls.h"
6 
7 #define CUPDIALOG_CONTROL_CLASSES (ICC_PROGRESS_CLASS) //We are using Progress bar Control
8 
9 class CUPDialog;
10 typedef struct _CUPDialogUserProcData CUPDUPDATA;
11 typedef bool (*LP_CUPDIALOG_USERPROC)(const CUPDUPDATA*);
12 
13 typedef struct _ProgressThreadData
14 {
15 protected:
17  {
18  ZeroMemory(this,sizeof(_ProgressThreadData));
19  }
20 public:
21  HWND hThreadWnd; //The Dialog that Created the Thread !!
22  LPVOID pUserProcParam; //Parameter that shoud be sent to the UserProc
23  bool bAlive; //Indicates the Thread State Alive/Dead
24  bool bTerminate; //Thread Monitors this value to Know if it has to Terminate itself
25 
26  LP_CUPDIALOG_USERPROC m_lpUserProc; //User Progress Procedure - Called by ProgressDialogBox From the ThreadProc
27 
28  enum //These would be used by Thread - Should be inSync with DlgProc Values !!
29  {
30  WM_DISABLECONTROLS = (WM_USER+1234),
31  WM_ENABLECONTROLS,
32  WM_PROGRESSTHREADCOMPLETED,
33  WM_PROGRESSBARUPDATE,
34  WM_PROGRESSTEXTUPDATE,
35  WM_CANCELPROGRESSTHREAD
36  };
37 
39 
41 
43 {
44  friend class CUPDialog;
45 private:
46  _CUPDialogUserProcData() { ZeroMemory(this,sizeof(CUPDUPDATA)); }
47 
48 public:
49  // Use this to retreive the lpUserProcParam supplied as part of CUPDialog constructor
50  inline LPVOID GetAppData() const { return this->pUserProcParam; }
51 
52  // Call this regularily inside the lengthy procedure to check if user has requested for cancelling the job
53  inline bool ShouldTerminate() const { return this->bTerminate; }
54 
55  // Call this frequently inside the lengthy procedure to set the progress text
56  inline void SetProgress(LPCTSTR lpszProgressText) const
57  {
58  if(::IsWindow(this->hThreadWnd) && this->bTerminate == false)
59  ::SendMessage(this->hThreadWnd,_ProgressThreadData::WM_PROGRESSTEXTUPDATE,0,(LPARAM)lpszProgressText);
60  }
61  // Call this frequently inside the lengthy procedure to set the progress bar position
62  inline void SetProgress(UINT_PTR dwProgressbarPos) const
63  {
64  if(::IsWindow(this->hThreadWnd) && this->bTerminate == false)
65  ::SendMessage(this->hThreadWnd,_ProgressThreadData::WM_PROGRESSBARUPDATE,dwProgressbarPos,0);
66  }
67  // Call this frequently inside the lengthy procedure to set the progress bar position and text
68  inline void SetProgress(LPCTSTR lpszProgressText,UINT_PTR dwProgressbarPos) const
69  {
70  SetProgress(lpszProgressText);
71  SetProgress(dwProgressbarPos);
72  }
73  // Use this to enable or disable the Cancel button on the progress dialog.
74  // bAllow = true enables the cancel button (thus user can terminate the job)
75  // bAllow = false disables the cancel button (and thus user cannot terminate the job)
76  inline void AllowCancel(bool bAllow) const
77  {
78  if(::IsWindow(this->hThreadWnd) && this->bTerminate == false)
79  ::SendMessage(this->hThreadWnd,bAllow?_ProgressThreadData::WM_ENABLECONTROLS:_ProgressThreadData::WM_DISABLECONTROLS,0,0);
80  }
81  // Modifies the Progress Dialog Caption
82  inline void SetDialogCaption(LPCTSTR lpszDialogCaption) const
83  {
84  if(::IsWindow(this->hThreadWnd) && this->bTerminate == false)
85  ::SendMessage(this->hThreadWnd,WM_SETTEXT,0,(LPARAM)lpszDialogCaption);
86  }
87 };
88 
89 
90 class CUPDialog : CInitCommonControls<CUPDIALOG_CONTROL_CLASSES>
91 {
92  HWND m_hParentWnd; //The Window that Requested the Progress Operation; Needed to Create the DialogBox
93 
94  HINSTANCE m_hInst; //HINSTANCE of the Module that holds the Dialog Template. (If Dialog template is NULL, this will be ignored.)
95 
96  LPCTSTR m_lpszTemplateName; //Dialog Template that should be used to display the progress dialog. (If NULL, built-in template will be used)
97 
98  int m_nStaticControlId; //Static Control Id
99 
100  int m_nProgressBarControlId;//Progressbar Control Id
101 
102  int m_nCancelButtonId; //Cancel button Control Id
103 
104  DWORD m_dwTerminateDelay; //Amount of time to wait after signaling the termination, in MilliSeconds.
105 
106  bool m_bAllowCancel; //Should the Dialog allow PreEmtption by user before Completion?
107 
108  HANDLE m_hThread; //Holds the Handle to the Created Thread
109 
110  TCHAR m_szDialogCaption[256]; //Fill up with the Title of the DialogBox
111 
112  CUPDUPDATA m_ThreadData;
113 
114  friend INT_PTR CALLBACK ProgressDlgProc(HWND,UINT,WPARAM,LPARAM); //The Windows Message Procedure for the DialogBox
115 
116  void Cleanup();
117 
118 public:
119  // Constructs a CUPDialog object. But does not yet display the DialogBox. (To display the dialog box use DoModal())
120  // hParentWnd: Parent window for the dialog box
121  // lpUserProc: Lengthy procedure that should be executed
122  // lpUserProcParam: Data that should be supplied to lpUserProc. (Accessible through CUPDUPDATA::GetAppData() in lpUserProc)
123  // lpszDlgTitle: Title of the Dialog box
124  // bAllowCancel: Is user allowed to cancel this dialog box? (Enables or Disables the Cancel button based on this value)
125  CUPDialog(HWND hParentWnd,LP_CUPDIALOG_USERPROC lpUserProc,LPVOID lpUserProcParam,LPCTSTR lpszDlgTitle=_T("Please Wait.."),bool bAllowCancel=true);
126 
127  // SetDialogTemplate allows any custom dialox box to be used as the progress dialog.
128  // Make sure that the custom dialog has one static control, one progress bar control and one cancel button.
129  // hInst: Module instance where the Dialog Template can be found
130  // lpTemplateName: Template that describes the custom dialog box
131  // StaticControlId: Identifier of the Static Control on the dialog box. Used for displaying the Progress Messages
132  // ProgressBarControlId: Identifier of the Progressbar Control on the Dialog Box
133  // CancelButtonId: Identifier of the Cancel Button Control on the Dialog Box.
134  inline void SetDialogTemplate(HINSTANCE hInst, LPCTSTR lpTemplateName, int StaticControlId, int ProgressBarControlId, int CancelButtonId)
135  {
136  m_hInst = hInst;
137 
138  m_lpszTemplateName = lpTemplateName;
139 
140  m_nStaticControlId = StaticControlId;
141 
142  m_nProgressBarControlId = ProgressBarControlId;
143 
144  m_nCancelButtonId = CancelButtonId;
145  }
146 
147  // SetTerminationDelay sets the amount of time the thread should be allowed to run after receiving a termination
148  // request from user before it is forcefully destoryed with TerminateThread.
149  // Default is 500Ms.
150  inline void SetTerminationDelay(DWORD dwMilliSeconds)
151  {
152  m_dwTerminateDelay = dwMilliSeconds;
153  }
154 
155  virtual ~CUPDialog();
156 
157  // DoModal invokes the Modal Dialog.
158  // If SetDialogTemplate has been called before, then the supplied template will be used. Else in-built template will be used.
159  // Return values:
160  // (ReturnValue==IDABORT) => Unable to Create Thread
161  // (ReturnValue==IDOK) => Sucessful;
162  // (LOWORD(ReturnValue)==IDCANCEL && HIWORD(ReturnValue)==0) => Some Error in UserProc;
163  // (LOWORD(ReturnValue)==IDCANCEL && HIWORD(ReturnValue)==1) => User Cancelled the Dialog;
164  INT_PTR DoModal();
165 
166  // Invoked after CUPDialog has completed its operation on a message.
167  // if bProcessed is TRUE then CUPDialog has processed this message.
168  // if bProcessed is FALSE then CUPDialog did not process this message (and is likely to be sent to the Default DialogProc).
169  // When bProcessed is FALSE,
170  // Return TRUE if you have processed the message.
171  // Return FALSE, to let the CUPDialog send it to the default dialg proc.
172  // When bProcessed is TRUE, the return value from this function is ignored (as the message has already been processed by CUPDialog).
173  virtual INT_PTR OnMessage(HWND hDlg,UINT Message,WPARAM wParam,LPARAM lParam, BOOL bProcessed)
174  {
175  // Note that not all messages come here. Especially the WM_SETFONT that comes before the WM_INITDIALOG
176  // CUPDialog uses SetWindowLongPtr() on the Dialog handle in WM_INITDIALOG.
177  // Take care not to make recursive SendMessage calls.
178  return FALSE; // Return false to continue any Default processing
179  }
180 };
181 
182 #endif

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem