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.9 KiB

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