controller.cpp
上传用户:dfhlxjd
上传日期:2007-01-07
资源大小:12k
文件大小:4k
源码类别:

Shell编程

开发平台:

Visual C++

  1. //----------------------------------------
  2. // (c) Reliable Software 1997
  3. //----------------------------------------
  4. #include <windows.h>
  5. #include <iomanip.h>
  6. #include "controls.h"
  7. #include "controller.h"
  8. #include "main.h"
  9. #include "shell95.h"
  10. #include "visitor.h"
  11. Controller::Controller (HWND hwnd)
  12. : _hwnd (hwnd),
  13. _pathField (hwnd, IDC_PATHNAME),
  14. _browse (hwnd, IDC_BROWSE),
  15. _calculate (hwnd, IDC_CALCULATE),
  16. _resultField (hwnd, IDC_RESULT),
  17. _exit (hwnd, IDC_EXIT),
  18. _clear (hwnd, IDC_NEW)
  19. {
  20.     _pathField.SetFocus();
  21. _resultField.SetString("-- No directory selected --");
  22. }
  23. class PathSelector
  24. {
  25. public:
  26. PathSelector(HWND hwnd)
  27. {
  28. _path [0] = '';
  29. Desktop desktop;
  30. ShPath browseRoot (desktop);
  31. if (browseRoot.IsOK ())
  32. {
  33.     FolderBrowser browser (hwnd,
  34.                           browseRoot,
  35.                           BIF_RETURNONLYFSDIRS,
  36.                           "Select folder to analyze");
  37.     if (browser.IsOK ())
  38.     {
  39.         strcpy (_path, browser.GetPath ());
  40.     }
  41. }
  42. }
  43. bool IsValid() const {return _path[0] != '';}
  44. char const * GetPath() const {return _path;}
  45. private:
  46. char _path[MAX_PATH + 1];
  47. };
  48. void Controller::Command (HWND hwnd, int controlID, int command)
  49. {
  50.     switch (controlID)
  51.     {
  52.         case IDC_PATHNAME:
  53.             if (_pathField.IsChanged(command))
  54.             {
  55.                 if (_pathField.GetLength())
  56. {
  57.                     _calculate.Enable();
  58. _clear.Enable();
  59.                 }
  60.                 else
  61. {
  62.                     _calculate.Disable();
  63. _clear.Disable();
  64. }
  65.             }
  66.             break;
  67. case IDC_BROWSE:
  68.             if (command == BN_CLICKED)
  69. {
  70. PathSelector userPath(hwnd);
  71. if (userPath.IsValid())
  72.                 {
  73.                     _pathField.SetString(userPath.GetPath());
  74.                     _resultField.Clear();
  75. }
  76. }
  77. break;
  78. case IDC_CALCULATE:
  79.             if (command == BN_CLICKED)
  80. {
  81.                 try
  82.                 {
  83.     BeginNewCalculation();
  84.     // do the real work
  85.                     char path[MAX_PATH + 1];
  86.                     _pathField.GetString(path, MAX_PATH);
  87. if (path[0] == '.')
  88. {
  89. _calculate.Disable();
  90. MessageBox(hwnd, "TreeSizer does not support relative paths in this version.", "Our problem ...", MB_OK|MB_ICONEXCLAMATION);
  91. }
  92. else
  93. {
  94.                     ULONG size = CalculateFolderSize(path);
  95.     // and present the result
  96.     ostrstream result;
  97.     CreateResultString(size, result);
  98.     _resultField.SetString(result.str());
  99. }
  100.                 }
  101.                 catch (WinException & e)
  102.                 {
  103.                     MessageBox (0, e.GetMessage (), "Exception", MB_ICONEXCLAMATION | MB_OK);
  104.                 }
  105.                 catch (...)
  106.                 {}
  107.                 _clear.Enable();
  108. _calculate.Enable();
  109.             }
  110. break;
  111. case IDC_EXIT:
  112.             if (command == BN_CLICKED)
  113.         SendMessage (hwnd, WM_CLOSE, 0, 0L);
  114. break;
  115. case IDC_NEW:
  116. _pathField.Clear();
  117. BeginNewCalculation();
  118. break;
  119.     }
  120. }
  121. class MouseWait
  122. {
  123. public:
  124. MouseWait()
  125. {
  126. _previous = SetCursor(LoadCursor(0, IDC_WAIT));
  127. }
  128. ~MouseWait()
  129. {
  130. if (_previous)
  131. SetCursor(_previous);
  132. }
  133. private:
  134. HCURSOR _previous;
  135. };
  136. ULONG Controller::CalculateFolderSize(char const * folderName)
  137. {
  138. MouseWait mw;
  139. Sizer sizer;
  140. Traversal traversal(folderName, sizer);
  141. return sizer.GetTotalSize();
  142. }
  143. void Controller::BeginNewCalculation()
  144. {
  145. _calculate.Disable();
  146. _resultField.Clear();
  147.     _clear.Disable();
  148. }
  149. void Controller::CreateResultString(ULONG size, ostrstream & ostr)
  150. {
  151. ostr << size << " bytes";
  152. if (size > 1024)
  153. {
  154.     char *suffixes[] = {"", " KB", " MB", " GB", " TB"};
  155.     int numSuffixes = 5;
  156.     int suffix;
  157.     double realSize = static_cast<double> (size);
  158.     for (suffix = 0; suffix < numSuffixes; suffix++)
  159.     {
  160.             if (realSize <= 1024)
  161.                 break;
  162.         realSize /= 1024;
  163.     }
  164.   ostr << "  ";
  165.         ostr.precision(3);
  166.         ostr << realSize << suffixes[suffix];
  167.   }
  168. ostr << '';
  169. }