SynchronizeProgress.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //---------------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include "SynchronizeProgress.h"
  5. #include <Progress.h>
  6. //---------------------------------------------------------------------------
  7. #pragma link "PathLabel"
  8. #pragma link "PngImageList"
  9. #pragma link "TB2Dock"
  10. #pragma link "TB2Item"
  11. #pragma link "TB2Toolbar"
  12. #pragma link "TBX"
  13. #pragma resource "*.dfm"
  14. //---------------------------------------------------------------------------
  15. // Used for comparing only
  16. __fastcall TSynchronizeProgressForm::TSynchronizeProgressForm(TComponent * Owner, bool AllowMinimize, int Files)
  17. : TForm(Owner)
  18. {
  19. FStarted = false;
  20. FCanceled = false;
  21. FShowAsModalStorage = NULL;
  22. FMinimizedByMe = false;
  23. bool KnowsTotalFiles = (Files >= 0);
  24. OperationProgress->Style = (KnowsTotalFiles ? pbstNormal : pbstMarquee);
  25. OperationProgress->Max = (KnowsTotalFiles ? Files : 1);
  26. TimeLeftLabelLabel->Visible = KnowsTotalFiles;
  27. TimeLeftLabel->Visible = KnowsTotalFiles;
  28. StartTimeLabelLabel->Visible = !KnowsTotalFiles;
  29. StartTimeLabel->Visible = !KnowsTotalFiles;
  30. UseSystemSettings(this);
  31. HideComponentsPanel(this);
  32. SelectScaledImageList(ImageList);
  33. if (!AllowMinimize)
  34. {
  35. MinimizeItem->Visible = false;
  36. }
  37. else
  38. {
  39. SetGlobalMinimizeHandler(this, GlobalMinimize);
  40. }
  41. FFrameAnimation.Init(AnimationPaintBox, L"SynchronizeDirectories");
  42. }
  43. //---------------------------------------------------------------------------
  44. __fastcall TSynchronizeProgressForm::~TSynchronizeProgressForm()
  45. {
  46. ClearGlobalMinimizeHandler(GlobalMinimize);
  47. ReleaseAsModal(this, FShowAsModalStorage);
  48. UnhookFormActivation(this);
  49. if (IsApplicationMinimized() && FMinimizedByMe)
  50. {
  51. ShowNotification(
  52. NULL, MainInstructions(LoadStr(BALLOON_OPERATION_COMPLETE)),
  53. qtInformation);
  54. }
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TSynchronizeProgressForm::Start()
  58. {
  59. FStarted = true;
  60. FStartTime = Now();
  61. UpdateTimer->Enabled = true;
  62. StartTimeLabel->Caption = FStartTime.TimeString();
  63. OperationProgress->Position = OperationProgress->Min;
  64. UpdateControls();
  65. if (!IsApplicationMinimized())
  66. {
  67. // Do not show the progress when the application is minimized,
  68. // otherwise the form popups up unminimized.
  69. // Quick and dirty hack: with this form, we do not support showing it
  70. // once the application restores,
  71. // otherwise we would have to synchronize it somehow with the TProgressForm,
  72. // not to show it over the TProgressForm
  73. // See solution in TMessageForm::CMShowingChanged.
  74. ShowAsModal(this, FShowAsModalStorage);
  75. HookFormActivation(this);
  76. }
  77. FFrameAnimation.Start();
  78. }
  79. //---------------------------------------------------------------------------
  80. int __fastcall TSynchronizeProgressForm::SetData(
  81. const UnicodeString & Directory1, const UnicodeString & Directory2, int Progress, bool & Continue)
  82. {
  83. DebugAssert(FStarted);
  84. LeftDirectoryLabel->Caption = Directory1;
  85. RightDirectoryLabel->Caption = Directory2;
  86. OperationProgress->Position = Progress;
  87. Continue = !FCanceled;
  88. UpdateControls();
  89. Application->ProcessMessages();
  90. return CalculateProgress();
  91. }
  92. //---------------------------------------------------------------------------
  93. int __fastcall TSynchronizeProgressForm::CalculateProgress()
  94. {
  95. return (((OperationProgress->Style == pbstMarquee) || (OperationProgress->Max == 0)) ? -1 : ((OperationProgress->Position * 100) / OperationProgress->Max));
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TSynchronizeProgressForm::UpdateControls()
  99. {
  100. TDateTime Elapsed;
  101. UnicodeString ACaption = FormatFormCaption(this, LoadStr(SYNCHRONIZE_PROGRESS_COMPARE));
  102. if (FStarted)
  103. {
  104. Elapsed = Now() - FStartTime;
  105. int Progress = CalculateProgress();
  106. if (Progress >= 0)
  107. {
  108. ACaption = FORMAT(L"%d%% %s", (Progress, ACaption));
  109. }
  110. }
  111. else
  112. {
  113. Elapsed = EncodeTimeVerbose(0, 0, 0, 0);
  114. }
  115. Caption = ACaption;
  116. TimeElapsedLabel->Caption = FormatDateTimeSpan(Elapsed);
  117. UnicodeString TimeLeftCaption;
  118. int Position = OperationProgress->Position;
  119. if (FStarted && CanShowTimeEstimate(FStartTime) && (Position > 0))
  120. {
  121. TDateTime TimeLeft = TDateTime(double(double(Elapsed) * (OperationProgress->Max - Position) / Position));
  122. TimeLeftCaption = FormatDateTimeSpan(TimeLeft);
  123. }
  124. else
  125. {
  126. TimeLeftCaption = LoadStr(PROGRESS_TIME_LEFT_CALCULATING);
  127. }
  128. TimeLeftLabel->Caption = TimeLeftCaption;
  129. CancelItem->Enabled = !FCanceled;
  130. TProgressForm::SizeToolbar(this, Toolbar, Dock, ToolbarPanel);
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TSynchronizeProgressForm::CancelOperation()
  134. {
  135. FCanceled = true;
  136. UpdateControls();
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TSynchronizeProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  140. {
  141. UpdateControls();
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TSynchronizeProgressForm::GlobalMinimize(TObject * /*Sender*/)
  145. {
  146. ApplicationMinimize();
  147. FMinimizedByMe = true;
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TSynchronizeProgressForm::CMDialogKey(TCMDialogKey & Message)
  151. {
  152. if (Message.CharCode == VK_TAB)
  153. {
  154. Toolbar->KeyboardOpen(L'\0', false);
  155. Message.Result = 1;
  156. }
  157. else
  158. {
  159. TForm::Dispatch(&Message);
  160. }
  161. }
  162. //---------------------------------------------------------------------------
  163. void __fastcall TSynchronizeProgressForm::Dispatch(void * AMessage)
  164. {
  165. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  166. if (Message.Msg == WM_CLOSE)
  167. {
  168. CancelOperation();
  169. }
  170. else if (Message.Msg == CM_DIALOGKEY)
  171. {
  172. CMDialogKey(reinterpret_cast<TCMDialogKey &>(Message));
  173. }
  174. else
  175. {
  176. TForm::Dispatch(AMessage);
  177. }
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TSynchronizeProgressForm::CancelItemClick(TObject * /*Sender*/)
  181. {
  182. CancelOperation();
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TSynchronizeProgressForm::MinimizeItemClick(TObject * Sender)
  186. {
  187. CallGlobalMinimizeHandler(Sender);
  188. }
  189. //---------------------------------------------------------------------------