Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
8.6 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. LOCPCT.H
  5. History:
  6. --*/
  7. #ifndef LOCUTIL__LocPct_H__INCLUDED
  8. #define LOCUTIL__LocPct_H__INCLUDED
  9. // Classes in this header file
  10. class CLocPercentHelper;
  11. class CLocPercentFrame;
  12. //
  13. // The CLocPercentHelper class can help in building acurate
  14. // percentage complete messages for complicated processes.
  15. //
  16. // Here is how the CLocPercentHelper works.
  17. //
  18. // The CLocPercentHelper class deals with "frames" of work. Each frame
  19. // is 100% of a unit of work. A CLocPercentHelper will always
  20. // start off with 1 frame. If you want to use these functions
  21. // you first need to call PercentSetUnits passing a number that will
  22. // represent 100% complete. For example if you need to process 4 items
  23. // you could set this to 4. After you process each item you would
  24. // call PercentAddValue. Correct status messages would be sent
  25. // indicating you are 1/4, 2/4, 3/4, and 4/4 done.
  26. // This processing comes in handy when you break up the work
  27. // in sub functions, or "frames" of work. Each function only
  28. // knows about what it needs to do.
  29. // Say in the resource example you call a function to handle each
  30. // resource. Each time the handler is called it is given 1/4
  31. // of the total time. The handler can break up its time however
  32. // it likes without knowing how much total time there is.
  33. // Say the sub function needs to do 10 things. It calls PercentSetUnits(10).
  34. // It then calls PercentAddValue as each of the 10 things are
  35. // accomplished. The total percent will reflect that 100% of this
  36. // sub function is really only 1/4 of the total percent. The sub function
  37. // only needs to worry about what it knows it has to do.
  38. // The sub function can assign part of its work to other functions
  39. // by creating frames for them. There is no limit to the number
  40. // of frames.
  41. //
  42. // Override the virtual function void OnSendPercentage(UINT nPct)
  43. // in your subclass of CLocPercentHelper to do what you want
  44. // with the percent calculated from the helper.
  45. // Example:
  46. /*
  47. CLocPercentHelper pctHelp;
  48. pctHelp.PercentSetUnits(4); //assume 4 items to process
  49. do
  50. {
  51. pctHelp.PercentPushFrame(1); //Set up a new Frame equal
  52. //to 1 of my units of work.
  53. //In this case 1/4 of the
  54. //total time.
  55. //All of the Percent... functions
  56. //called made now deal with
  57. //this new frame.
  58. HandleItem(pctHelp);
  59. pctHelp.PersentPopFrame(); //Remove the frame created
  60. //and mark the amount
  61. //of time it was equal to
  62. //completed.
  63. }
  64. while (more items)
  65. -----------------------------------------------------------------------
  66. HandleItem(CLocPercentHelper& pctHelp) function
  67. pctHelp.PercentSetUnits(10); //Assume this is a dialog resource
  68. //with 10 controls.
  69. //This function divides up
  70. //the work it needs to do in
  71. //a way that makes sence for it.
  72. //
  73. //When this "frame" is at 100%
  74. //the total percentage is still
  75. //just 1/4 of the total time
  76. //since this frame was given 1/4
  77. //of the total time from the caller.
  78. do
  79. {
  80. // This function can assign part of its processing
  81. // to another function by calling PercentPushFrame also.
  82. HandleControl();
  83. pctHelp.PercentAddValue(); //Send a message to the
  84. //handler indicating the
  85. //current percentage.
  86. //The object will calculate
  87. //the total percent based on
  88. //the current stack of frames.
  89. }
  90. while (more controls)
  91. */
  92. //
  93. // CLocPercentFrame represents a working unit of progress.
  94. // The progress model implemented with the CLocPercentHelper will
  95. // support unlimited levels of work units.
  96. //
  97. // This class is a helper class used only by CLocPercentHelper
  98. //
  99. #pragma warning(disable: 4275) // non dll-interface class 'foo' used
  100. // as base for dll-interface class 'bar'
  101. class LTAPIENTRY CLocPercentFrame : public CObject
  102. {
  103. friend CLocPercentHelper;
  104. protected:
  105. CLocPercentFrame();
  106. CLocPercentFrame(CLocPercentFrame* pParent, UINT nValueInParent);
  107. void SetComplete();
  108. // Force this frame to represent 100%.
  109. void AddValue(UINT nValue);
  110. // Add nValue to the internal value.
  111. // The internal value will never be greater than
  112. // the internal units.
  113. void SetValue(UINT nValue);
  114. // Set the internal value.
  115. // The internal value will never be greater than
  116. // the internal units.
  117. void SetUnits(UINT nUnits);
  118. // Set the internal units
  119. UINT m_nUnits; //Number that represents 100%
  120. UINT m_nValue; //Number that represent how far done
  121. //this frame is.
  122. CLocPercentFrame* m_pParent; //Pointer to the parent frame
  123. UINT m_nValueInParent; //How much this frame is worth
  124. //in the parents context.
  125. void MemberInit();
  126. // Initialize member values
  127. };
  128. //
  129. // List of frames in the helper
  130. //
  131. class LTAPIENTRY CLocPercentFrameList : public CTypedPtrList<CPtrList, CLocPercentFrame*>
  132. {
  133. };
  134. class LTAPIENTRY CLocPercentHelper : public CObject
  135. {
  136. public:
  137. CLocPercentHelper();
  138. virtual ~CLocPercentHelper();
  139. void PercentSetUnits(UINT nUnits, BOOL bReport = FALSE);
  140. // Set the units of the current frame.
  141. // Calculate and report the total % done through
  142. // OnSendPercentage if bReport is TRUE.
  143. void PercentSetValue(UINT nValue, BOOL bReport = TRUE);
  144. // Set the value of the current frame.
  145. // Calculate and report the total % done through
  146. // OnSendPercentage if bReport is TRUE.
  147. void PercentAddValue(UINT nValue = 1, BOOL bReport = TRUE);
  148. // Add nValue to the value of the current frame.
  149. // Calculate and report the total % done through
  150. // OnSendPercentage if bReport is TRUE.
  151. void PercentSetComplete(BOOL bReport = TRUE);
  152. // Set the current frame complete.
  153. // Calculate and report the total % done through
  154. // OnSendPercentage if bReport is TRUE.
  155. void PercentForceAllComplete(BOOL bReport = TRUE);
  156. // Force all frames complete.
  157. // Calculate and report 100% done through
  158. // OnSendPercentage if bReport is TRUE.
  159. void PercentPushFrame(UINT nValueInParent = 1);
  160. // Create a new frame and assign in nValueInParent
  161. // All Percent... calls made after this call deal with
  162. // the new frame.
  163. void PercentPopFrame(BOOL bReport = TRUE);
  164. // Set the current frame complete and add the current
  165. // frames valueInParent to its parent frame.
  166. // The current frames parent is now the current frame.
  167. // Calculate and report the total % done through
  168. // OnSendPercentage if bReport is TRUE.
  169. void PercentSetStrict(BOOL bOnOff = TRUE);
  170. // Strict behavior means the helper will ASSERT (_DEBUG only) if
  171. // the calculated percent is over 100%. This can happen
  172. // if the unit values assigned to frames are not truly what
  173. // the process does. If you are unable to set acurate
  174. // unit values and the program quesses, you can turn
  175. // strict off.
  176. BOOL PercentIsStrict();
  177. // Return TRUE or FALSE if strict is on.
  178. protected:
  179. // Support for Progress Reporting
  180. CLocPercentFrame m_FrameMain; //The main frame always
  181. //present. This frame
  182. //will never have a parent.
  183. CLocPercentFrameList m_FrameList; //List of open frames.
  184. CLocPercentFrame* m_pCurrentFrame; //Pointer to the current
  185. //frame
  186. BOOL m_bStrict; //Strict on will ASSERT if
  187. //total % gets over 100
  188. void SendPercentage();
  189. // Calculates the percentage based on the current frame
  190. // Calles OnSendPercentage with the calulated value.
  191. void SafeDeleteFrame(CLocPercentFrame* pFrame);
  192. // Safely deletes a frame making sure the pFrame is
  193. // not m_FrameMain.
  194. virtual void OnSendPercentage(UINT nPct);
  195. // Callback function for subclasses to do what they
  196. // want with the percentage. Default implementation
  197. // does nothing.
  198. };
  199. #pragma warning(default: 4275)
  200. //
  201. // Helper class with a CProgressiveObject
  202. //
  203. class LTAPIENTRY CLocPctProgress : public CLocPercentHelper
  204. {
  205. public:
  206. CLocPctProgress();
  207. CLocPctProgress(CProgressiveObject* pProgObj);
  208. void SetProgressiveObject(CProgressiveObject* pProgObj);
  209. protected:
  210. virtual void OnSendPercentage(UINT nPct);
  211. CProgressiveObject* m_pProgObj;
  212. };
  213. #endif // LOCUTIL__LocPct_H__INCLUDED