SynchronizeChecklist.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "WinInterface.h"
  6. #include "SynchronizeChecklist.h"
  7. #include <Terminal.h>
  8. #include <TextsWin.h>
  9. #include <CoreMain.h>
  10. #include <VCLCommon.h>
  11. #include <Tools.h>
  12. #include <BaseUtils.hpp>
  13. #include <Math.hpp>
  14. #include <WinConfiguration.h>
  15. #include <GUITools.h>
  16. #include <TerminalManager.h>
  17. #include <System.IOUtils.hpp>
  18. #include <System.StrUtils.hpp>
  19. #include <algorithm>
  20. //---------------------------------------------------------------------
  21. #pragma link "IEListView"
  22. #pragma link "NortonLikeListView"
  23. #pragma link "PngImageList"
  24. #pragma resource "*.dfm"
  25. //---------------------------------------------------------------------
  26. const int ImageColumnIndex = 4;
  27. //---------------------------------------------------------------------
  28. bool __fastcall DoSynchronizeChecklistDialog(TSynchronizeChecklist * Checklist,
  29. TSynchronizeMode Mode, int Params,
  30. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  31. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  32. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  33. TSynchronizeBrowseEvent OnSynchronizeBrowse, void * Token)
  34. {
  35. std::unique_ptr<TSynchronizeChecklistDialog> Dialog(
  36. new TSynchronizeChecklistDialog(
  37. Application, Mode, Params, LocalDirectory, RemoteDirectory, OnCustomCommandMenu, OnSynchronize,
  38. OnSynchronizeChecklistCalculateSize, OnSynchronizeMove, OnSynchronizeBrowse, Token));
  39. return Dialog->Execute(Checklist);
  40. }
  41. //---------------------------------------------------------------------
  42. __fastcall TSynchronizeChecklistDialog::TSynchronizeChecklistDialog(
  43. TComponent * AOwner, TSynchronizeMode Mode, int Params,
  44. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  45. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  46. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  47. TSynchronizeBrowseEvent OnSynchronizeBrowse, void * Token)
  48. : TForm(AOwner)
  49. {
  50. FFormRestored = false;
  51. FMode = Mode;
  52. FParams = Params;
  53. FLocalDirectory = ExcludeTrailingBackslash(LocalDirectory);
  54. FRemoteDirectory = UnixExcludeTrailingBackslash(RemoteDirectory);
  55. FOnCustomCommandMenu = OnCustomCommandMenu;
  56. FOnSynchronizeChecklistCalculateSize = OnSynchronizeChecklistCalculateSize;
  57. FOnSynchronizeMove = OnSynchronizeMove;
  58. FOnSynchronizeBrowse = OnSynchronizeBrowse;
  59. DebugAssert(OnSynchronize != NULL);
  60. FOnSynchronize = OnSynchronize;
  61. FToken = Token;
  62. UseSystemSettings(this);
  63. UseDesktopFont(ListView);
  64. UseDesktopFont(StatusBar);
  65. FChecklist = NULL;
  66. FChangingItem = NULL;
  67. FChangingItemIgnore = false;
  68. FChangingItemMass = false;
  69. FSynchronizing = false;
  70. FMoveCandidatesValidForSort = MaxInt;
  71. FDirectories = 0;
  72. SelectScaledImageList(ActionImages);
  73. FOrigListViewWindowProc = ListView->WindowProc;
  74. ListView->WindowProc = ListViewWindowProc;
  75. FOrigStatusBarWindowProc = StatusBar->WindowProc;
  76. StatusBar->WindowProc = StatusBarWindowProc;
  77. UpdateImages();
  78. CustomCommandsAction->Visible = (FOnCustomCommandMenu != NULL);
  79. // button visibility cannot be bound to action visibility
  80. CustomCommandsButton2->Visible = CustomCommandsAction->Visible;
  81. MenuButton(CustomCommandsButton2);
  82. MenuButton(ToolsMenuButton);
  83. }
  84. //---------------------------------------------------------------------
  85. __fastcall TSynchronizeChecklistDialog::~TSynchronizeChecklistDialog()
  86. {
  87. StatusBar->WindowProc = FOrigStatusBarWindowProc;
  88. ListView->WindowProc = FOrigListViewWindowProc;
  89. }
  90. //---------------------------------------------------------------------
  91. bool __fastcall TSynchronizeChecklistDialog::Execute(TSynchronizeChecklist * Checklist)
  92. {
  93. FChecklist = Checklist;
  94. bool Result = (ShowModal() == DefaultResult(this));
  95. if (Result)
  96. {
  97. TSynchronizeChecklistConfiguration FormConfiguration =
  98. CustomWinConfiguration->SynchronizeChecklist;
  99. FormConfiguration.ListParams = ListView->ColProperties->ParamsStr;
  100. UnicodeString WindowParams = FormConfiguration.WindowParams;
  101. // if there is no main window, keep previous "custom pos" indication,
  102. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  103. if (!IsMainFormLike(this))
  104. {
  105. CustomPos = (Application->MainForm->BoundsRect != BoundsRect);
  106. }
  107. FormConfiguration.WindowParams =
  108. FORMAT(L"%d;%s", ((CustomPos ? 1 : 0), StoreForm(this)));
  109. CustomWinConfiguration->SynchronizeChecklist = FormConfiguration;
  110. }
  111. if (FException.get() != NULL)
  112. {
  113. RethrowException(FException.get());
  114. }
  115. return Result;
  116. }
  117. //---------------------------------------------------------------------
  118. void __fastcall TSynchronizeChecklistDialog::UpdateCaption()
  119. {
  120. TTerminalManager * Manager = TTerminalManager::Instance();
  121. UnicodeString Title = Manager->GetAppProgressTitle();
  122. UnicodeString StatusTitle = LoadStr(FSynchronizing ? SYNCHRONIZE_PROGRESS_SYNCHRONIZE2 : SYNCHRONIZE_CHECKLIST_CAPTION);
  123. if (Title.Pos(StatusTitle) <= 0)
  124. {
  125. AddToList(Title, StatusTitle, TitleSeparator);
  126. }
  127. Caption = Manager->FormatFormCaptionWithSession(this, Title);
  128. }
  129. //---------------------------------------------------------------------
  130. void __fastcall TSynchronizeChecklistDialog::UpdateControls()
  131. {
  132. UpdateCaption();
  133. StatusBar->Invalidate();
  134. bool AllChecked = true;
  135. bool AllUnchecked = true;
  136. bool AnyBoth = false;
  137. bool AnyNonBoth = false;
  138. bool AnyDirectory = false;
  139. TListItem * Item = ListView->Selected;
  140. while (Item != NULL)
  141. {
  142. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  143. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  144. if ((Action == TSynchronizeChecklist::saUploadUpdate) ||
  145. (Action == TSynchronizeChecklist::saDownloadUpdate))
  146. {
  147. AnyBoth = true;
  148. }
  149. else
  150. {
  151. AnyNonBoth = true;
  152. }
  153. if (Item->Checked)
  154. {
  155. AllUnchecked = false;
  156. }
  157. else
  158. {
  159. AllChecked = false;
  160. }
  161. if (ChecklistItem->IsDirectory)
  162. {
  163. AnyDirectory = true;
  164. }
  165. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  166. }
  167. EnableControl(OkButton, (FChecked[0] > 0) && !FSynchronizing);
  168. EnableControl(CancelButton, !FSynchronizing);
  169. EnableControl(HelpButton, !FSynchronizing);
  170. CheckAction->Enabled = !AllChecked && !FSynchronizing;
  171. UncheckAction->Enabled = !AllUnchecked && !FSynchronizing;
  172. CheckAllAction->Enabled = (FChecked[0] < FTotals[0]) && !FSynchronizing;
  173. UncheckAllAction->Enabled = (FChecked[0] > 0) && !FSynchronizing;
  174. CheckDirectoryAction->Enabled = CheckAllAction->Enabled; // sic
  175. UncheckDirectoryAction->Enabled = UncheckAllAction->Enabled; // sic
  176. CustomCommandsAction->Enabled = AnyBoth && !AnyNonBoth && DebugAlwaysTrue(!FSynchronizing);
  177. ReverseAction->Enabled = (ListView->SelCount > 0) && DebugAlwaysTrue(!FSynchronizing);
  178. MoveAction->Enabled = (GetMoveItems() != TSynchronizeMoveItems());
  179. CalculateSizeAction->Enabled = (ListView->SelCount > 0) && AnyDirectory && DebugAlwaysTrue(!FSynchronizing);
  180. CalculateSizeAllAction->Enabled = (FDirectories > 0) && !FSynchronizing;
  181. TSynchronizeChecklist::TAction SelectedItemAction =
  182. (ListView->SelCount == 1) ? GetChecklistItemAction(GetChecklistItem(ListView->Selected)) : TSynchronizeChecklist::saNone;
  183. BrowseLocalAction->Enabled = (ListView->SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteRemote);
  184. BrowseRemoteAction->Enabled = (ListView->SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteLocal);
  185. SelectAllAction->Enabled = (ListView->SelCount < ListView->Items->Count) && !FSynchronizing;
  186. FindMoveCandidateAction->Enabled = (ListView->Items->Count > 0) && !FSynchronizing;
  187. }
  188. //---------------------------------------------------------------------------
  189. bool __fastcall TSynchronizeChecklistDialog::GetWindowParams(UnicodeString & WindowParams)
  190. {
  191. WindowParams = CustomWinConfiguration->SynchronizeChecklist.WindowParams;
  192. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  193. return CustomPos || IsMainFormLike(this);
  194. }
  195. //---------------------------------------------------------------------------
  196. void __fastcall TSynchronizeChecklistDialog::CreateParams(TCreateParams & Params)
  197. {
  198. UnicodeString WindowParams;
  199. if (GetWindowParams(WindowParams))
  200. {
  201. // This is only to set correct TForm::Position. Actual bounds are set later after DPI scaling
  202. RestoreForm(WindowParams, this, true);
  203. }
  204. TForm::CreateParams(Params);
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TSynchronizeChecklistDialog::HelpButtonClick(TObject * /*Sender*/)
  208. {
  209. FormHelp(this);
  210. }
  211. //---------------------------------------------------------------------------
  212. void __fastcall TSynchronizeChecklistDialog::AddSubItem(TListItem * Item, int & Index, const UnicodeString & S)
  213. {
  214. if (Index < Item->SubItems->Count)
  215. {
  216. Item->SubItems->Strings[Index] = S;
  217. }
  218. else
  219. {
  220. DebugAssert(Index == Item->SubItems->Count);
  221. Item->SubItems->Add(S);
  222. }
  223. Index++;
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TSynchronizeChecklistDialog::LoadItem(TListItem * Item)
  227. {
  228. UnicodeString S;
  229. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  230. if (ChecklistItem->ImageIndex >= 0)
  231. {
  232. Item->ImageIndex = ChecklistItem->ImageIndex;
  233. }
  234. else
  235. {
  236. Item->ImageIndex = FakeFileImageIndex(ChecklistItem->GetFileName(),
  237. FLAGMASK(ChecklistItem->IsDirectory, FILE_ATTRIBUTE_DIRECTORY));
  238. }
  239. S = ChecklistItem->GetFileName();
  240. if (ChecklistItem->IsDirectory)
  241. {
  242. S = IncludeTrailingBackslash(S);
  243. }
  244. Item->Caption = S;
  245. int Index = 0;
  246. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  247. if (Action == TSynchronizeChecklist::saDeleteRemote)
  248. {
  249. AddSubItem(Item, Index, L"");
  250. AddSubItem(Item, Index, L"");
  251. AddSubItem(Item, Index, L"");
  252. }
  253. else
  254. {
  255. S = ChecklistItem->Local.Directory;
  256. if (AnsiSameText(FLocalDirectory, S.SubString(1, FLocalDirectory.Length())))
  257. {
  258. S[1] = L'.';
  259. S.Delete(2, FLocalDirectory.Length() - 1);
  260. }
  261. else
  262. {
  263. DebugFail();
  264. }
  265. AddSubItem(Item, Index, S);
  266. if (Action == TSynchronizeChecklist::saDownloadNew)
  267. {
  268. AddSubItem(Item, Index, L"");
  269. AddSubItem(Item, Index, L"");
  270. }
  271. else
  272. {
  273. if (!ChecklistItem->HasSize())
  274. {
  275. AddSubItem(Item, Index, L"");
  276. }
  277. else
  278. {
  279. AddSubItem(Item, Index,
  280. FormatPanelBytes(ChecklistItem->Local.Size, WinConfiguration->FormatSizeBytes));
  281. }
  282. AddSubItem(Item, Index,
  283. UserModificationStr(ChecklistItem->Local.Modification,
  284. ChecklistItem->Local.ModificationFmt));
  285. }
  286. }
  287. AddSubItem(Item, Index, L"");
  288. DebugAssert(Index == ImageColumnIndex);
  289. if (Action == TSynchronizeChecklist::saDeleteLocal)
  290. {
  291. AddSubItem(Item, Index, L"");
  292. AddSubItem(Item, Index, L"");
  293. AddSubItem(Item, Index, L"");
  294. }
  295. else
  296. {
  297. S = ChecklistItem->Remote.Directory;
  298. if (AnsiSameText(FRemoteDirectory, S.SubString(1, FRemoteDirectory.Length())))
  299. {
  300. S[1] = L'.';
  301. S.Delete(2, FRemoteDirectory.Length() - 1);
  302. }
  303. else
  304. {
  305. DebugFail();
  306. }
  307. AddSubItem(Item, Index, S);
  308. if (Action == TSynchronizeChecklist::saUploadNew)
  309. {
  310. AddSubItem(Item, Index, L"");
  311. AddSubItem(Item, Index, L"");
  312. }
  313. else
  314. {
  315. if (!ChecklistItem->HasSize())
  316. {
  317. AddSubItem(Item, Index, L"");
  318. }
  319. else
  320. {
  321. AddSubItem(Item, Index,
  322. FormatPanelBytes(ChecklistItem->Remote.Size, WinConfiguration->FormatSizeBytes));
  323. }
  324. AddSubItem(Item, Index, UserModificationStr(ChecklistItem->Remote.Modification,
  325. ChecklistItem->Remote.ModificationFmt));
  326. }
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TSynchronizeChecklistDialog::CountItemSize(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  331. {
  332. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  333. int ActionIndex = int(Action);
  334. __int64 ItemSize = ChecklistItem->GetSize(Action);
  335. FCheckedSize[ActionIndex] += Factor * ItemSize;
  336. FCheckedSize[0] += Factor * ItemSize;
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TSynchronizeChecklistDialog::CountItem(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  340. {
  341. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  342. FChecked[ActionIndex] += Factor;
  343. FChecked[0] += Factor;
  344. CountItemSize(ChecklistItem, Factor);
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TSynchronizeChecklistDialog::CountItemTotal(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  348. {
  349. FTotals[0] += Factor;
  350. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  351. FTotals[ActionIndex] += Factor;
  352. if (ChecklistItem->IsDirectory)
  353. {
  354. FDirectories += Factor;
  355. }
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TSynchronizeChecklistDialog::LoadList()
  359. {
  360. memset(&FTotals, 0, sizeof(FTotals));
  361. memset(&FChecked, 0, sizeof(FChecked));
  362. memset(&FCheckedSize, 0, sizeof(FCheckedSize));
  363. FDirectories = 0;
  364. ListView->Items->BeginUpdate();
  365. try
  366. {
  367. ListView->Items->Clear();
  368. for (int Index = 0; Index < FChecklist->Count; Index++)
  369. {
  370. const TSynchronizeChecklist::TItem * ChecklistItem =
  371. FChecklist->Item[ListView->Items->Count];
  372. FChangingItemIgnore = true;
  373. try
  374. {
  375. TListItem * Item = ListView->Items->Add();
  376. TSynchronizeChecklist::TAction Action = ChecklistItem->Action;
  377. FActions.insert(std::make_pair(ChecklistItem, Action));
  378. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  379. Item->Data = const_cast<TSynchronizeChecklist::TItem *>(ChecklistItem);
  380. Item->Checked = ChecklistItem->Checked;
  381. LoadItem(Item);
  382. }
  383. __finally
  384. {
  385. FChangingItemIgnore = false;
  386. }
  387. CountItemTotal(ChecklistItem, 1);
  388. if (ChecklistItem->Checked)
  389. {
  390. CountItem(ChecklistItem, 1);
  391. }
  392. }
  393. }
  394. __finally
  395. {
  396. ListView->Items->EndUpdate();
  397. }
  398. ListView->AlphaSort();
  399. UpdateControls();
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TSynchronizeChecklistDialog::FormShow(TObject * /*Sender*/)
  403. {
  404. // Moved here from CreateParams (see also TEditorForm::CreateParams), because there it breaks per-monitor DPI.
  405. // For example BoundsRect is matched to the main form too soon, so it gets rescaled later.
  406. // Also it happens before constructor, what causes UseDesktopFont-flagged controls to rescale twice.
  407. // But Position is already set in the CreateParams, as it cannot be set here anymore.
  408. if (!FFormRestored)
  409. {
  410. FFormRestored = True;
  411. UnicodeString WindowParams;
  412. if (GetWindowParams(WindowParams))
  413. {
  414. RestoreForm(WindowParams, this);
  415. }
  416. else
  417. {
  418. BoundsRect = Application->MainForm->BoundsRect;
  419. }
  420. FlashOnBackground();
  421. }
  422. ListView->ColProperties->ParamsStr = CustomWinConfiguration->SynchronizeChecklist.ListParams;
  423. LoadList();
  424. UpdateStatusBarSize();
  425. }
  426. //---------------------------------------------------------------------------
  427. TRect __fastcall TSynchronizeChecklistDialog::GetColumnHeaderRect(int Index)
  428. {
  429. HWND HeaderHandle = ListView_GetHeader(ListView->Handle);
  430. TRect R;
  431. Header_GetItemRect(HeaderHandle, Index, &R);
  432. // Can be simplified using GetScrollPos
  433. TScrollInfo ScrollInfo;
  434. ZeroMemory(&ScrollInfo, sizeof(ScrollInfo));
  435. ScrollInfo.cbSize = sizeof(ScrollInfo);
  436. ScrollInfo.fMask = SIF_POS;
  437. GetScrollInfo(ListView->Handle, SB_HORZ, &ScrollInfo);
  438. R.Left -= ScrollInfo.nPos;
  439. R.Right -= ScrollInfo.nPos;
  440. return R;
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TSynchronizeChecklistDialog::ListViewWindowProc(TMessage & Message)
  444. {
  445. if (Message.Msg == CN_NOTIFY)
  446. {
  447. TWMNotify & NotifyMessage = reinterpret_cast<TWMNotify &>(Message);
  448. if (NotifyMessage.NMHdr->code == NM_CUSTOMDRAW)
  449. {
  450. // workaround
  451. // Due to a bug in VCL, OnAdvancedCustomDrawSubItem is not called for any
  452. // other stage except for cdPrePaint. So we must call it ourselves.
  453. TNMLVCustomDraw * CustomDraw =
  454. reinterpret_cast<TNMLVCustomDraw *>(NotifyMessage.NMHdr);
  455. if (FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEM) &&
  456. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_SUBITEM) &&
  457. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEMPOSTPAINT) &&
  458. (CustomDraw->iSubItem == ImageColumnIndex) &&
  459. (ActionImages->Width <= ListView->Columns->Items[CustomDraw->iSubItem]->Width))
  460. {
  461. TListItem * Item = ListView->Items->Item[CustomDraw->nmcd.dwItemSpec];
  462. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  463. TRect HeaderR = GetColumnHeaderRect(CustomDraw->iSubItem);
  464. TRect R = Item->DisplayRect(drBounds);
  465. R.Left = HeaderR.Left + (HeaderR.Width() - ActionImages->Width) / 2;
  466. R.Right = HeaderR.Right;
  467. // workaround
  468. // doing this from ListViewAdvancedCustomDraw corrupts list view on Windows 7
  469. ImageList_Draw(reinterpret_cast<HIMAGELIST>(ActionImages->Handle),
  470. int(GetChecklistItemAction(ChecklistItem)), CustomDraw->nmcd.hdc,
  471. R.Left, ((R.Top + R.Bottom - ActionImages->Height) / 2), ILD_TRANSPARENT);
  472. }
  473. }
  474. FOrigListViewWindowProc(Message);
  475. }
  476. else if (Message.Msg == CM_HINTSHOW)
  477. {
  478. ListViewHintShow(reinterpret_cast<TCMHintShow &>(Message));
  479. }
  480. else if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  481. {
  482. Message.Result = 1;
  483. }
  484. else
  485. {
  486. FOrigListViewWindowProc(Message);
  487. }
  488. }
  489. //---------------------------------------------------------------------------
  490. void __fastcall TSynchronizeChecklistDialog::ListViewHintShow(TCMHintShow & HintShow)
  491. {
  492. TLVHitTestInfo HitTest;
  493. HitTest.pt = HintShow.HintInfo->CursorPos;
  494. int Index = ListView_SubItemHitTest(ListView->Handle, &HitTest);
  495. if ((Index >= 0) && (HitTest.iSubItem == ImageColumnIndex))
  496. {
  497. TListItem * Item = ListView->Items->Item[Index];
  498. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  499. int ActionHint = 0;
  500. switch (int(GetChecklistItemAction(ChecklistItem)))
  501. {
  502. case TSynchronizeChecklist::saUploadNew:
  503. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_NEW;
  504. break;
  505. case TSynchronizeChecklist::saDownloadNew:
  506. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_NEW;
  507. break;
  508. case TSynchronizeChecklist::saUploadUpdate:
  509. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_UPDATE;
  510. break;
  511. case TSynchronizeChecklist::saDownloadUpdate:
  512. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_UPDATE;
  513. break;
  514. case TSynchronizeChecklist::saDeleteRemote:
  515. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_REMOTE;
  516. break;
  517. case TSynchronizeChecklist::saDeleteLocal:
  518. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_LOCAL;
  519. break;
  520. }
  521. if (DebugAlwaysTrue(ActionHint != 0))
  522. {
  523. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (LoadStr(ActionHint), LoadStr(SYNCHRONIZE_CHECKLIST_REVERSE)));
  524. ListView_GetSubItemRect(ListView->Handle, Index, ImageColumnIndex, LVIR_BOUNDS, &HintShow.HintInfo->CursorRect);
  525. HintShow.Result = 0;
  526. }
  527. }
  528. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  529. }
  530. //---------------------------------------------------------------------------
  531. void __fastcall TSynchronizeChecklistDialog::StatusBarHintShow(TCMHintShow & HintShow)
  532. {
  533. int IPanel = PanelAt(HintShow.HintInfo->CursorPos.x);
  534. if (IPanel >= 0)
  535. {
  536. TStatusPanel * Panel = StatusBar->Panels->Items[IPanel];
  537. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (Panel->Text, StatusBar->Hint));
  538. HintShow.HintInfo->CursorRect.Left = 0;
  539. while (IPanel > 0)
  540. {
  541. IPanel--;
  542. HintShow.HintInfo->CursorRect.Left += StatusBar->Panels->Items[IPanel]->Width;
  543. }
  544. HintShow.HintInfo->CursorRect.Top = 0;
  545. HintShow.HintInfo->CursorRect.Bottom = StatusBar->ClientHeight;
  546. HintShow.HintInfo->CursorRect.Right = HintShow.HintInfo->CursorRect.Left + Panel->Width;
  547. HintShow.Result = 0;
  548. }
  549. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  550. }
  551. //---------------------------------------------------------------------------
  552. void __fastcall TSynchronizeChecklistDialog::StatusBarWindowProc(TMessage & Message)
  553. {
  554. if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  555. {
  556. Message.Result = 1;
  557. }
  558. else if (Message.Msg == CM_HINTSHOW)
  559. {
  560. StatusBarHintShow(reinterpret_cast<TCMHintShow &>(Message));
  561. }
  562. else
  563. {
  564. FOrigStatusBarWindowProc(Message);
  565. }
  566. }
  567. //---------------------------------------------------------------------------
  568. void __fastcall TSynchronizeChecklistDialog::ListViewAdvancedCustomDrawSubItem(
  569. TCustomListView * /*Sender*/, TListItem * /*Item*/, int /*SubItem*/,
  570. TCustomDrawState /*State*/, TCustomDrawStage /*Stage*/, bool & /*DefaultDraw*/)
  571. {
  572. // this is just fake handler that makes the list view request custom draw notification above
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TSynchronizeChecklistDialog::StatusBarDrawPanel(
  576. TStatusBar * StatusBar, TStatusPanel * Panel, const TRect & Rect)
  577. {
  578. bool Possible;
  579. int ActionIndex = Panel->Index;
  580. TSynchronizeChecklist::TAction Action = TSynchronizeChecklist::TAction(ActionIndex);
  581. if (FTotals[ActionIndex] > 0)
  582. {
  583. // if direction is overriden to an action that is otherwise not possible
  584. // in given synchronization mode, we still want to show the stats
  585. Possible = true;
  586. }
  587. else
  588. {
  589. switch (Action)
  590. {
  591. case TSynchronizeChecklist::saNone:
  592. Possible = true;
  593. break;
  594. case TSynchronizeChecklist::saUploadNew:
  595. Possible = ((FMode == smRemote) || (FMode == smBoth)) &&
  596. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  597. break;
  598. case TSynchronizeChecklist::saDownloadNew:
  599. Possible = ((FMode == smLocal) || (FMode == smBoth)) &&
  600. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  601. break;
  602. case TSynchronizeChecklist::saUploadUpdate:
  603. Possible =
  604. ((FMode == smRemote) || (FMode == smBoth)) &&
  605. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  606. break;
  607. case TSynchronizeChecklist::saDownloadUpdate:
  608. Possible =
  609. ((FMode == smLocal) || (FMode == smBoth)) &&
  610. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  611. break;
  612. case TSynchronizeChecklist::saDeleteRemote:
  613. Possible = (FMode == smRemote) &&
  614. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  615. break;
  616. case TSynchronizeChecklist::saDeleteLocal:
  617. Possible = (FMode == smLocal) &&
  618. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  619. break;
  620. default:
  621. DebugFail();
  622. Possible = false;
  623. break;
  624. }
  625. }
  626. UnicodeString PanelText;
  627. if (Possible)
  628. {
  629. PanelText = FORMAT(LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 1),
  630. (FormatNumber(FChecked[ActionIndex]),
  631. FormatNumber(FTotals[ActionIndex])));
  632. if ((FChecked[ActionIndex] > 0) &&
  633. ((ActionIndex == 0) || !TSynchronizeChecklist::IsItemSizeIrrelevant(Action)))
  634. {
  635. PanelText += FORMAT(L" (%s)", (FormatBytes(FCheckedSize[ActionIndex])));
  636. }
  637. }
  638. else
  639. {
  640. PanelText = LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 2);
  641. }
  642. int TextHeight = StatusBar->Canvas->TextHeight(PanelText);
  643. int X = Rect.Left + ActionImages->Width + 4;
  644. int Y = (Rect.Top + Rect.Bottom - TextHeight) / 2;
  645. StatusBar->Canvas->TextRect(Rect, X, Y, PanelText);
  646. X = Rect.Left + 1;
  647. Y = ((Rect.Top + Rect.Bottom - ActionImages->Height) / 2);
  648. int ImageIndex = ActionIndex;
  649. ActionImages->Draw(StatusBar->Canvas, X, Y, ImageIndex, Possible);
  650. }
  651. //---------------------------------------------------------------------------
  652. int __fastcall TSynchronizeChecklistDialog::PanelCount()
  653. {
  654. // last "panel" is technical
  655. return StatusBar->Panels->Count - 1;
  656. }
  657. //---------------------------------------------------------------------------
  658. int __fastcall TSynchronizeChecklistDialog::PanelAt(int X)
  659. {
  660. int Result = 0;
  661. while ((X > StatusBar->Panels->Items[Result]->Width) &&
  662. (Result < PanelCount()))
  663. {
  664. X -= StatusBar->Panels->Items[Result]->Width;
  665. Result++;
  666. }
  667. return ((Result < StatusBar->Panels->Count - 1) ? Result : -1);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TSynchronizeChecklistDialog::ListViewChange(
  671. TObject * /*Sender*/, TListItem * Item, TItemChange Change)
  672. {
  673. if ((Change == ctState) && (FChangingItem == Item) && (FChangingItem != NULL))
  674. {
  675. if (!FChangingItemIgnore)
  676. {
  677. DebugAssert(Item->Data != NULL);
  678. if ((FChangingItemChecked != Item->Checked) && (Item->Data != NULL))
  679. {
  680. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  681. CountItem(ChecklistItem, Item->Checked ? 1 : -1);
  682. if (!FChangingItemMass)
  683. {
  684. UpdateControls();
  685. }
  686. }
  687. }
  688. FChangingItem = NULL;
  689. }
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TSynchronizeChecklistDialog::ListViewChanging(
  693. TObject * /*Sender*/, TListItem * Item, TItemChange Change,
  694. bool & /*AllowChange*/)
  695. {
  696. if (Change == ctState)
  697. {
  698. FChangingItem = Item;
  699. FChangingItemChecked = Item->Checked;
  700. }
  701. else
  702. {
  703. DebugAssert(FChangingItem == NULL);
  704. FChangingItem = NULL;
  705. }
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TSynchronizeChecklistDialog::CheckAll(bool Check)
  709. {
  710. FChangingItemMass = true;
  711. try
  712. {
  713. for (int Index = 0; Index < ListView->Items->Count; Index++)
  714. {
  715. ListView->Items->Item[Index]->Checked = Check;
  716. }
  717. }
  718. __finally
  719. {
  720. FChangingItemMass = false;
  721. }
  722. UpdateControls();
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TSynchronizeChecklistDialog::CheckAllActionExecute(TObject * /*Sender*/)
  726. {
  727. CheckAll(true);
  728. }
  729. //---------------------------------------------------------------------------
  730. void __fastcall TSynchronizeChecklistDialog::UncheckAllActionExecute(TObject * /*Sender*/)
  731. {
  732. CheckAll(false);
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TSynchronizeChecklistDialog::Check(bool Check)
  736. {
  737. FChangingItemMass = true;
  738. try
  739. {
  740. TListItem * Item = ListView->Selected;
  741. while (Item != NULL)
  742. {
  743. Item->Checked = Check;
  744. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  745. }
  746. }
  747. __finally
  748. {
  749. FChangingItemMass = false;
  750. }
  751. UpdateControls();
  752. }
  753. //---------------------------------------------------------------------------
  754. void __fastcall TSynchronizeChecklistDialog::CheckActionExecute(TObject * /*Sender*/)
  755. {
  756. Check(true);
  757. }
  758. //---------------------------------------------------------------------------
  759. void __fastcall TSynchronizeChecklistDialog::UncheckActionExecute(TObject * /*Sender*/)
  760. {
  761. Check(false);
  762. }
  763. //---------------------------------------------------------------------------
  764. void __fastcall TSynchronizeChecklistDialog::ListViewSelectItem(
  765. TObject * /*Sender*/, TListItem * /*Item*/, bool /*Selected*/)
  766. {
  767. // Delayed update of button status in case many items are being selected at once
  768. // Also change of selection causes buttons to flash, as for short period of time,
  769. // no item is selected
  770. UpdateTimer->Enabled = true;
  771. }
  772. //---------------------------------------------------------------------------
  773. void __fastcall TSynchronizeChecklistDialog::UpdateTimerTimer(
  774. TObject * /*Sender*/)
  775. {
  776. UpdateTimer->Enabled = false;
  777. UpdateControls();
  778. }
  779. //---------------------------------------------------------------------------
  780. TListItem * __fastcall TSynchronizeChecklistDialog::SelectAll(bool Select, int Action,
  781. bool OnlyTheAction)
  782. {
  783. TListItem * Result = NULL;
  784. for (int Index = 0; Index < ListView->Items->Count; Index++)
  785. {
  786. TListItem * Item = ListView->Items->Item[Index];
  787. if (Action == 0)
  788. {
  789. Item->Selected = Select;
  790. if (Result == NULL)
  791. {
  792. Result = Item;
  793. }
  794. }
  795. else
  796. {
  797. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  798. bool WantedAction = (int(GetChecklistItemAction(ChecklistItem)) == Action);
  799. if (WantedAction || !OnlyTheAction)
  800. {
  801. Item->Selected = Select && WantedAction;
  802. if (WantedAction && (Result == NULL))
  803. {
  804. Result = Item;
  805. }
  806. }
  807. }
  808. }
  809. return Result;
  810. }
  811. //---------------------------------------------------------------------------
  812. void __fastcall TSynchronizeChecklistDialog::SelectAllActionExecute(
  813. TObject * /*Sender*/)
  814. {
  815. SelectAll(true);
  816. }
  817. //---------------------------------------------------------------------------
  818. void __fastcall TSynchronizeChecklistDialog::StatusBarMouseDown(
  819. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState Shift, int X,
  820. int /*Y*/)
  821. {
  822. int IPanel = PanelAt(X);
  823. if (IPanel >= 0)
  824. {
  825. TListItem * Item = SelectAll(true, IPanel, Shift.Contains(ssCtrl));
  826. if (Item != NULL)
  827. {
  828. Item->MakeVisible(false);
  829. Item->Focused = true;
  830. ListView->SetFocus();
  831. }
  832. }
  833. }
  834. //---------------------------------------------------------------------------
  835. TIEListViewColProperties * TSynchronizeChecklistDialog::GetColProperties()
  836. {
  837. return dynamic_cast<TIEListViewColProperties *>(ListView->ColProperties);
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TSynchronizeChecklistDialog::ListViewCompare(
  841. TObject * /*Sender*/, TListItem * Item1, TListItem * Item2, int /*Data*/,
  842. int & Compare)
  843. {
  844. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(Item1);
  845. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(Item2);
  846. TIEListViewColProperties * ColProperties = GetColProperties();
  847. switch (ColProperties->SortColumn)
  848. {
  849. case 0: // name
  850. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  851. break;
  852. // sorting by local and remote dir is the same
  853. case 1: // local dir
  854. case 5: // remote dir
  855. Compare = 0; // default sorting
  856. break;
  857. case 2: // local size
  858. Compare = CompareNumber(ChecklistItem1->Local.Size, ChecklistItem2->Local.Size);
  859. break;
  860. case 3: // local changed
  861. Compare = CompareFileTime(ChecklistItem1->Local.Modification,
  862. ChecklistItem2->Local.Modification);
  863. break;
  864. case ImageColumnIndex: // action
  865. Compare = CompareNumber(GetChecklistItemAction(ChecklistItem1), GetChecklistItemAction(ChecklistItem2));
  866. break;
  867. case 6: // remote size
  868. Compare = CompareNumber(ChecklistItem1->Remote.Size, ChecklistItem2->Remote.Size);
  869. break;
  870. case 7: // remote changed
  871. Compare = CompareFileTime(ChecklistItem1->Remote.Modification,
  872. ChecklistItem2->Remote.Modification);
  873. break;
  874. }
  875. if (Compare == 0)
  876. {
  877. if (!ChecklistItem1->Local.Directory.IsEmpty())
  878. {
  879. Compare = AnsiCompareText(ChecklistItem1->Local.Directory, ChecklistItem2->Local.Directory);
  880. }
  881. else
  882. {
  883. DebugAssert(!ChecklistItem1->Remote.Directory.IsEmpty());
  884. Compare = AnsiCompareText(ChecklistItem1->Remote.Directory, ChecklistItem2->Remote.Directory);
  885. }
  886. if (Compare == 0)
  887. {
  888. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  889. }
  890. }
  891. if (!ColProperties->SortAscending)
  892. {
  893. Compare = -Compare;
  894. }
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TSynchronizeChecklistDialog::ListViewSecondaryColumnHeader(
  898. TCustomIEListView * /*Sender*/, int Index, int & SecondaryColumn)
  899. {
  900. // "remote dir" column is sorting alias for "local dir" column
  901. if (Index == 5)
  902. {
  903. SecondaryColumn = 1;
  904. }
  905. else
  906. {
  907. SecondaryColumn = -1;
  908. }
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TSynchronizeChecklistDialog::ListViewContextPopup(
  912. TObject * Sender, TPoint & MousePos, bool & Handled)
  913. {
  914. // to update source popup menu before TBX menu is created
  915. UpdateControls();
  916. MenuPopup(Sender, MousePos, Handled);
  917. }
  918. //---------------------------------------------------------------------------
  919. void __fastcall TSynchronizeChecklistDialog::CustomCommandsActionExecute(
  920. TObject * /*Sender*/)
  921. {
  922. TStrings * LocalFileList = new TStringList();
  923. TStrings * RemoteFileList = new TStringList();
  924. try
  925. {
  926. TListItem * Item = ListView->Selected;
  927. DebugAssert(Item != NULL);
  928. while (Item != NULL)
  929. {
  930. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  931. DebugAssert((GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saUploadUpdate) ||
  932. (GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saDownloadUpdate));
  933. DebugAssert(ChecklistItem->RemoteFile != NULL);
  934. UnicodeString LocalPath =
  935. IncludeTrailingBackslash(ChecklistItem->Local.Directory) +
  936. ChecklistItem->Local.FileName;
  937. LocalFileList->Add(LocalPath);
  938. UnicodeString RemotePath =
  939. UnixIncludeTrailingBackslash(ChecklistItem->Remote.Directory) +
  940. ChecklistItem->Remote.FileName;
  941. RemoteFileList->AddObject(RemotePath, ChecklistItem->RemoteFile);
  942. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  943. }
  944. }
  945. catch(...)
  946. {
  947. delete LocalFileList;
  948. delete RemoteFileList;
  949. throw;
  950. }
  951. DebugAssert(FOnCustomCommandMenu != NULL);
  952. FOnCustomCommandMenu(CustomCommandsAction, LocalFileList, RemoteFileList);
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TSynchronizeChecklistDialog::UpdateStatusBarSize()
  956. {
  957. int PanelWidth = Min(StatusBar->Width / PanelCount(), ScaleByTextHeight(this, 160));
  958. for (int Index = 0; Index < PanelCount(); Index++)
  959. {
  960. StatusBar->Panels->Items[Index]->Width = PanelWidth;
  961. }
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TSynchronizeChecklistDialog::StatusBarResize(TObject * /*Sender*/)
  965. {
  966. UpdateStatusBarSize();
  967. }
  968. //---------------------------------------------------------------------------
  969. const TSynchronizeChecklist::TItem * TSynchronizeChecklistDialog::GetChecklistItem(
  970. TListItem * Item)
  971. {
  972. return static_cast<const TSynchronizeChecklist::TItem *>(Item->Data);
  973. }
  974. //---------------------------------------------------------------------------
  975. TSynchronizeChecklist::TAction & TSynchronizeChecklistDialog::GetChecklistItemAction(
  976. const TSynchronizeChecklist::TItem * ChecklistItem)
  977. {
  978. TActions::iterator i = FActions.find(ChecklistItem);
  979. if (i == FActions.end())
  980. {
  981. throw EInvalidOperation(L"");
  982. }
  983. return i->second;
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TSynchronizeChecklistDialog::ReverseActionExecute(TObject * /*Sender*/)
  987. {
  988. TAutoFlag Flag(FChangingItemMass);
  989. TListItem * Item = ListView->Selected;
  990. while (Item != NULL)
  991. {
  992. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  993. TSynchronizeChecklist::TAction & Action = GetChecklistItemAction(ChecklistItem);
  994. TSynchronizeChecklist::TAction NewAction = TSynchronizeChecklist::Reverse(Action);
  995. if (DebugAlwaysTrue(Action != NewAction))
  996. {
  997. CountItemTotal(ChecklistItem, -1);
  998. if (Item->Checked)
  999. {
  1000. CountItem(ChecklistItem, -1);
  1001. }
  1002. Action = NewAction;
  1003. CountItemTotal(ChecklistItem, 1);
  1004. if (Item->Checked)
  1005. {
  1006. // item size may differ with action (0 for delete, but non-0 for new file transfer)
  1007. CountItem(ChecklistItem, 1);
  1008. }
  1009. LoadItem(Item);
  1010. }
  1011. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1012. }
  1013. Flag.Release();
  1014. UpdateControls();
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TSynchronizeChecklistDialog::ListViewClick(TObject * /*Sender*/)
  1018. {
  1019. TKeyboardState KeyState;
  1020. GetKeyboardState(KeyState);
  1021. TShiftState ShiftState = KeyboardStateToShiftState(KeyState);
  1022. // when selecting, do not reverse, even when user clicked on action column
  1023. if (!ShiftState.Contains(ssShift) && !ShiftState.Contains(ssCtrl))
  1024. {
  1025. TPoint P = ListView->ScreenToClient(Mouse->CursorPos);
  1026. TRect R = GetColumnHeaderRect(ImageColumnIndex);
  1027. if ((R.Left <= P.x) && (P.x <= R.Right))
  1028. {
  1029. // If no item was selected before the click, the action is not enabled yet here,
  1030. // and Execute would be noop, force update
  1031. UpdateControls();
  1032. ReverseAction->Execute();
  1033. }
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TSynchronizeChecklistDialog::Dispatch(void * AMessage)
  1038. {
  1039. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  1040. if (Message.Msg == WM_SYSCOMMAND)
  1041. {
  1042. if (!HandleMinimizeSysCommand(Message))
  1043. {
  1044. TForm::Dispatch(AMessage);
  1045. }
  1046. }
  1047. else if (Message.Msg == CM_DPICHANGED)
  1048. {
  1049. CMDpiChanged(Message);
  1050. }
  1051. else if (Message.Msg == WM_WANTS_MOUSEWHEEL_INACTIVE)
  1052. {
  1053. Message.Result = FSynchronizing ? 1 : 0;
  1054. }
  1055. else if (Message.Msg == WM_MANAGES_CAPTION)
  1056. {
  1057. // calling UpdateControls would cause status bar flicker
  1058. UpdateCaption();
  1059. Message.Result = 1;
  1060. }
  1061. else
  1062. {
  1063. TForm::Dispatch(AMessage);
  1064. }
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. void __fastcall TSynchronizeChecklistDialog::UpdateImages()
  1068. {
  1069. ListView->SmallImages = ShellImageListForControl(this, ilsSmall);
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TSynchronizeChecklistDialog::CMDpiChanged(TMessage & Message)
  1073. {
  1074. TForm::Dispatch(&Message);
  1075. UpdateImages();
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. void __fastcall TSynchronizeChecklistDialog::ProcessedItem(void * /*Token*/, const TSynchronizeChecklist::TItem * ChecklistItem)
  1079. {
  1080. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1081. DebugAssert(Item->Checked);
  1082. Item->Checked = false;
  1083. Item->MakeVisible(false);
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TSynchronizeChecklistDialog::UpdatedSynchronizationChecklistItems(
  1087. const TSynchronizeChecklist::TItemList & Items)
  1088. {
  1089. TSynchronizeChecklist::TItemList::const_iterator Iter = Items.begin();
  1090. while (Iter != Items.end())
  1091. {
  1092. const TSynchronizeChecklist::TItem * ChecklistItem = *Iter;
  1093. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1094. LoadItem(Item);
  1095. // When called from FOnSynchronize, we rely on a caller never to update size of an item that had size already,
  1096. // otherwise we get it counted twice here.
  1097. if (Item->Checked)
  1098. {
  1099. CountItemSize(ChecklistItem, 1);
  1100. }
  1101. Iter++;
  1102. }
  1103. UpdateControls();
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TSynchronizeChecklistDialog::OkButtonClick(TObject * /*Sender*/)
  1107. {
  1108. ListView->SelectAll(smNone);
  1109. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1110. {
  1111. TListItem * Item = ListView->Items->Item[Index];
  1112. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1113. FChecklist->Update(ChecklistItem, Item->Checked, GetChecklistItemAction(ChecklistItem));
  1114. }
  1115. TAutoFlag Flag(FSynchronizing);
  1116. UpdateControls();
  1117. try
  1118. {
  1119. FOnSynchronize(FToken, ProcessedItem, UpdatedSynchronizationChecklistItems);
  1120. }
  1121. catch (Exception & E)
  1122. {
  1123. FException.reset(CloneException(&E));
  1124. }
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void TSynchronizeChecklistDialog::CalculateSize(bool All)
  1128. {
  1129. TItemStates States;
  1130. if (!All)
  1131. {
  1132. States << isSelected;
  1133. }
  1134. TSynchronizeChecklist::TItemList Items;
  1135. TListItem * Item = NULL;
  1136. while ((Item = ListView->GetNextItem(Item, sdAll, States)) != NULL)
  1137. {
  1138. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1139. Items.push_back(ChecklistItem);
  1140. if (Item->Checked)
  1141. {
  1142. CountItemSize(ChecklistItem, -1);
  1143. }
  1144. }
  1145. try
  1146. {
  1147. FOnSynchronizeChecklistCalculateSize(FChecklist, Items, FToken);
  1148. }
  1149. __finally
  1150. {
  1151. UpdatedSynchronizationChecklistItems(Items);
  1152. }
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TSynchronizeChecklistDialog::CalculateSizeActionExecute(TObject *)
  1156. {
  1157. CalculateSize(false);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TSynchronizeChecklistDialog::CalculateSizeAllActionExecute(TObject *)
  1161. {
  1162. CalculateSize(true);
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. TSynchronizeChecklist::TAction TSynchronizeChecklistDialog::GetOppositeMoveAction(TSynchronizeChecklist::TAction Action1)
  1166. {
  1167. switch (Action1)
  1168. {
  1169. case TSynchronizeChecklist::saUploadNew:
  1170. return TSynchronizeChecklist::saDeleteRemote;
  1171. case TSynchronizeChecklist::saDownloadNew:
  1172. return TSynchronizeChecklist::saDeleteLocal;
  1173. case TSynchronizeChecklist::saDeleteRemote:
  1174. return TSynchronizeChecklist::saUploadNew;
  1175. case TSynchronizeChecklist::saDeleteLocal:
  1176. return TSynchronizeChecklist::saDownloadNew;
  1177. default:
  1178. return TSynchronizeChecklist::saNone;
  1179. }
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. bool TSynchronizeChecklistDialog::IsTransferNewAction(TSynchronizeChecklist::TAction Action)
  1183. {
  1184. return (Action == TSynchronizeChecklist::saUploadNew) || (Action == TSynchronizeChecklist::saDownloadNew);
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. TSynchronizeChecklistDialog::TSynchronizeMoveItems __fastcall TSynchronizeChecklistDialog::GetMoveItems()
  1188. {
  1189. if ((ListView->SelCount != 2) || DebugAlwaysFalse(FSynchronizing))
  1190. {
  1191. return TSynchronizeMoveItems();
  1192. }
  1193. else
  1194. {
  1195. TListItem * Item1 = ListView->Selected;
  1196. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(DebugNotNull(Item1));
  1197. TListItem * Item2 = ListView->GetNextItem(ListView->Selected, sdAll, TItemStates() << isSelected);
  1198. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(DebugNotNull(Item2));
  1199. if (ChecklistItem1->IsDirectory != ChecklistItem2->IsDirectory)
  1200. {
  1201. return TSynchronizeMoveItems();
  1202. }
  1203. else
  1204. {
  1205. TSynchronizeChecklist::TAction Action1 = GetChecklistItemAction(ChecklistItem1);
  1206. TSynchronizeChecklist::TAction Action2 = GetChecklistItemAction(ChecklistItem2);
  1207. if (GetOppositeMoveAction(Action1) == Action2)
  1208. {
  1209. if (IsTransferNewAction(Action1))
  1210. {
  1211. return TSynchronizeMoveItems(ChecklistItem1, ChecklistItem2);
  1212. }
  1213. else
  1214. {
  1215. return TSynchronizeMoveItems(ChecklistItem2, ChecklistItem1);
  1216. }
  1217. }
  1218. else
  1219. {
  1220. return TSynchronizeMoveItems();
  1221. }
  1222. }
  1223. }
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. void __fastcall TSynchronizeChecklistDialog::DeleteItem(const TSynchronizeChecklist::TItem * ChecklistItem)
  1227. {
  1228. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1229. CountItemTotal(ChecklistItem, -1);
  1230. if (Item->Checked)
  1231. {
  1232. CountItem(ChecklistItem, -1);
  1233. }
  1234. FActions.erase(ChecklistItem);
  1235. FChecklistToListViewMap.erase(ChecklistItem);
  1236. FChecklist->Delete(ChecklistItem);
  1237. int Index = Item->Index;
  1238. if (Item->Focused)
  1239. {
  1240. int FocusIndex = Index;
  1241. if (FocusIndex == ListView->Items->Count - 1)
  1242. {
  1243. FocusIndex--;
  1244. }
  1245. else
  1246. {
  1247. FocusIndex++;
  1248. }
  1249. ListView->ItemFocused = ListView->Items->Item[FocusIndex];
  1250. }
  1251. ListView->Items->Delete(Index);
  1252. FMoveCandidatesValidForSort = MaxInt; // can be optimized
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. void __fastcall TSynchronizeChecklistDialog::MoveActionExecute(TObject *)
  1256. {
  1257. TSynchronizeMoveItems MoveItems = GetMoveItems();
  1258. TSynchronizeChecklist::TAction Action2 = GetChecklistItemAction(MoveItems.second);
  1259. TOperationSide Side;
  1260. UnicodeString FileName;
  1261. UnicodeString NewFileName;
  1262. TRemoteFile * RemoteFile;
  1263. if (Action2 == TSynchronizeChecklist::saDeleteRemote)
  1264. {
  1265. Side = osRemote;
  1266. FileName = UnixCombinePaths(MoveItems.second->Remote.Directory, MoveItems.second->Remote.FileName);
  1267. NewFileName = UnixCombinePaths(MoveItems.first->Remote.Directory, MoveItems.first->Local.FileName);
  1268. RemoteFile = MoveItems.second->RemoteFile;
  1269. }
  1270. else if (Action2 == TSynchronizeChecklist::saDeleteLocal)
  1271. {
  1272. Side = osLocal;
  1273. FileName = CombinePaths(MoveItems.second->Local.Directory, MoveItems.second->Local.FileName);
  1274. NewFileName = CombinePaths(MoveItems.first->Local.Directory, MoveItems.first->Remote.FileName);
  1275. RemoteFile = NULL;
  1276. }
  1277. else
  1278. {
  1279. DebugFail();
  1280. Abort();
  1281. }
  1282. FOnSynchronizeMove(Side, FileName, NewFileName, RemoteFile);
  1283. DeleteItem(MoveItems.first);
  1284. DeleteItem(MoveItems.second);
  1285. UpdateControls();
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. void __fastcall TSynchronizeChecklistDialog::CheckDirectory(bool Check)
  1289. {
  1290. std::unique_ptr<TStringList> Directories(new TStringList());
  1291. TListItem * Item = ListView->Selected;
  1292. while (Item != NULL)
  1293. {
  1294. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1295. // It does not matter if we use local or remote directory
  1296. Directories->Add(IncludeTrailingBackslash(ChecklistItem->Local.Directory));
  1297. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1298. }
  1299. TAutoFlag ChangingItemMassSwitch(FChangingItemMass);
  1300. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1301. {
  1302. TListItem * Item = ListView->Items->Item[Index];
  1303. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1304. UnicodeString Directory = IncludeTrailingBackslash(ChecklistItem->Local.Directory);
  1305. for (int Index2 = 0; Index2 < Directories->Count; Index2++)
  1306. {
  1307. if (StartsText(Directories->Strings[Index2], Directory))
  1308. {
  1309. Item->Checked = Check;
  1310. }
  1311. }
  1312. }
  1313. ChangingItemMassSwitch.Release();
  1314. UpdateControls();
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall TSynchronizeChecklistDialog::CheckDirectoryActionExecute(TObject *)
  1318. {
  1319. CheckDirectory(true);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TSynchronizeChecklistDialog::UncheckDirectoryActionExecute(TObject *)
  1323. {
  1324. CheckDirectory(false);
  1325. }
  1326. //---------------------------------------------------------------------------
  1327. void __fastcall TSynchronizeChecklistDialog::DoBrowse(TOperationSide Side)
  1328. {
  1329. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Selected);
  1330. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1331. FOnSynchronizeBrowse(Side, Action, ChecklistItem);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TSynchronizeChecklistDialog::BrowseLocalActionExecute(TObject *)
  1335. {
  1336. DoBrowse(osLocal);
  1337. }
  1338. //---------------------------------------------------------------------------
  1339. void __fastcall TSynchronizeChecklistDialog::BrowseRemoteActionExecute(TObject *)
  1340. {
  1341. DoBrowse(osRemote);
  1342. }
  1343. //---------------------------------------------------------------------------
  1344. void __fastcall TSynchronizeChecklistDialog::KeyDown(Word & Key, TShiftState Shift)
  1345. {
  1346. TShortCut KeyShortCut = ShortCut(Key, Shift);
  1347. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  1348. if (IsCustomShortCut(CustomShortCut))
  1349. {
  1350. TTBXItem * MenuItem = new TTBXItem(this);
  1351. CustomCommandsAction->ActionComponent = MenuItem;
  1352. CustomCommandsAction->Execute();
  1353. for (int Index = 0; Index < MenuItem->Count; Index++)
  1354. {
  1355. TTBCustomItem * Item = MenuItem->Items[Index];
  1356. if (Item->Enabled && (Item->ShortCut == CustomShortCut))
  1357. {
  1358. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  1359. Item->Click();
  1360. Key = 0;
  1361. break;
  1362. }
  1363. }
  1364. }
  1365. TForm::KeyDown(Key, Shift);
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall TSynchronizeChecklistDialog::ListViewRecreate(TObject *)
  1369. {
  1370. FChecklistToListViewMap.clear();
  1371. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1372. {
  1373. TListItem * Item = ListView->Items->Item[Index];
  1374. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1375. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  1376. }
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TSynchronizeChecklistDialog::ToolsMenuButtonClick(TObject *)
  1380. {
  1381. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1382. }
  1383. //---------------------------------------------------------------------------
  1384. void __fastcall TSynchronizeChecklistDialog::FindMoveCandidateActionExecute(TObject *)
  1385. {
  1386. TIEListViewColProperties * ColProperties = GetColProperties();
  1387. int Sort = (ColProperties->SortAscending ? 1 : -1) * ColProperties->SortColumn;
  1388. if (FMoveCandidatesValidForSort != Sort)
  1389. {
  1390. FMoveCandidatesFileName.clear();
  1391. FMoveCandidatesSize.clear();
  1392. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1393. {
  1394. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Items->Item[Index]);
  1395. if (ChecklistItem->IsLocalOnly() || ChecklistItem->IsRemoteOnly())
  1396. {
  1397. if (ChecklistItem->IsDirectory)
  1398. {
  1399. FMoveCandidatesFileName[ChecklistItem->GetFileName().LowerCase()].push_back(ChecklistItem);
  1400. }
  1401. else
  1402. {
  1403. FMoveCandidatesSize[ChecklistItem->GetBaseSize()].push_back(ChecklistItem);
  1404. }
  1405. }
  1406. }
  1407. FMoveCandidatesValidForSort = Sort;
  1408. }
  1409. bool Found = false;
  1410. if (!FMoveCandidatesFileName.empty() || !FMoveCandidatesSize.empty())
  1411. {
  1412. TListItem * ItemFocused = ListView->ItemFocused;
  1413. TListItem * Item;
  1414. if (ItemFocused != NULL)
  1415. {
  1416. Item = ItemFocused;
  1417. }
  1418. // Can we have a selection without focus?
  1419. else if (DebugAlwaysFalse(ListView->Selected != NULL))
  1420. {
  1421. Item = ListView->Selected;
  1422. }
  1423. else if (ListView->Items->Count > 0)
  1424. {
  1425. Item = ListView->Items->Item[0];
  1426. }
  1427. else
  1428. {
  1429. DebugFail(); // the action is disabled when there are no items
  1430. Item = NULL;
  1431. }
  1432. TListItem * FirstItem = NULL;
  1433. while (!Found && (Item != NULL))
  1434. {
  1435. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1436. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1437. TSynchronizeChecklist::TAction OppositeAction = GetOppositeMoveAction(Action);
  1438. if ((OppositeAction != TSynchronizeChecklist::saNone) &&
  1439. // For focused item, we search pair even if the focused item is "delete" action,
  1440. // but when searching the next items, consider "transfer" actions only
  1441. ((Item == ItemFocused) || IsTransferNewAction(Action)))
  1442. {
  1443. TChecklistItems Candidates;
  1444. if (ChecklistItem->IsDirectory)
  1445. {
  1446. UnicodeString FileName = ChecklistItem->GetFileName().LowerCase();
  1447. TMoveCandidatesFileNameMap::const_iterator I = FMoveCandidatesFileName.find(FileName);
  1448. if (I != FMoveCandidatesFileName.end())
  1449. {
  1450. const TChecklistItems & NameCandidates = I->second;
  1451. for (size_t I = 0; I < NameCandidates.size(); I++)
  1452. {
  1453. const TSynchronizeChecklist::TItem * ChecklistItem2 = NameCandidates[I];
  1454. if ((GetChecklistItemAction(ChecklistItem2) == OppositeAction) &&
  1455. DebugAlwaysTrue(ChecklistItem2->IsDirectory))
  1456. {
  1457. Candidates.push_back(ChecklistItem2);
  1458. }
  1459. }
  1460. }
  1461. }
  1462. else
  1463. {
  1464. __int64 Size = ChecklistItem->GetBaseSize();
  1465. TMoveCandidatesSizeMap::const_iterator I = FMoveCandidatesSize.find(Size);
  1466. if (I != FMoveCandidatesSize.end())
  1467. {
  1468. UnicodeString FileName = ChecklistItem->GetFileName();
  1469. const TChecklistItems & SizeCandidates = I->second;
  1470. for (size_t I = 0; I < SizeCandidates.size(); I++)
  1471. {
  1472. const TSynchronizeChecklist::TItem * ChecklistItem2 = SizeCandidates[I];
  1473. if ((GetChecklistItemAction(ChecklistItem2) == OppositeAction) &&
  1474. DebugAlwaysTrue(!ChecklistItem2->IsDirectory))
  1475. {
  1476. bool IsCandidate;
  1477. // in addition to the same size, also the same file filename (although possibly different directory)
  1478. if (SameText(FileName, ChecklistItem2->GetFileName()))
  1479. {
  1480. IsCandidate = true;
  1481. }
  1482. // or different filename but the same directory (in addition to the same size)
  1483. else if ((Action == TSynchronizeChecklist::saDeleteLocal) ||
  1484. (Action == TSynchronizeChecklist::saDownloadNew))
  1485. {
  1486. IsCandidate = SamePaths(ChecklistItem->Local.Directory, ChecklistItem2->Local.Directory);
  1487. }
  1488. else if ((Action == TSynchronizeChecklist::saDeleteRemote) ||
  1489. (Action == TSynchronizeChecklist::saUploadNew))
  1490. {
  1491. IsCandidate = UnixSamePath(ChecklistItem->Remote.Directory, ChecklistItem2->Remote.Directory);
  1492. }
  1493. else
  1494. {
  1495. DebugFail();
  1496. IsCandidate = false;
  1497. }
  1498. if (IsCandidate)
  1499. {
  1500. Candidates.push_back(ChecklistItem2);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. if (!Candidates.empty())
  1507. {
  1508. const TSynchronizeChecklist::TItem * ChecklistItem2 = Candidates[0];
  1509. if ((FirstItem == NULL) && Item->Selected && (ListView->SelCount == 2))
  1510. {
  1511. TListItem * NextSelected = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1512. if (NextSelected == NULL)
  1513. {
  1514. NextSelected = ListView->Selected; // Shorthand for GetNextItem(NULL, sdAll, isSelected)
  1515. }
  1516. TChecklistItems::const_iterator I = std::find(Candidates.begin(), Candidates.end(), GetChecklistItem(NextSelected));
  1517. if (I < Candidates.end() - 1)
  1518. {
  1519. ChecklistItem2 = *(I + 1);
  1520. }
  1521. else if (I == Candidates.end() - 1)
  1522. {
  1523. ChecklistItem2 = NULL;
  1524. }
  1525. }
  1526. if (ChecklistItem2 != NULL)
  1527. {
  1528. TListItem * Item2 = FChecklistToListViewMap[ChecklistItem2];
  1529. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1530. {
  1531. TListItem * ItemI = ListView->Items->Item[Index];
  1532. ItemI->Selected = (ItemI == Item) || (ItemI == Item2);
  1533. }
  1534. ListView->ItemFocused = Item;
  1535. // IsItemVisible returns true even on partial visibility,
  1536. // so it is still worth trying MakeVisible to make them completelly visible
  1537. bool FlickerExpected = !ListView->IsItemVisible(Item) || !ListView->IsItemVisible(Item2);
  1538. if (FlickerExpected)
  1539. {
  1540. // does not seem to have any effect
  1541. DisableRedraw(ListView);
  1542. }
  1543. try
  1544. {
  1545. // even if the item turns to be invisible in the end, it at least scrolls the view towards it
  1546. Item2->MakeVisible(false);
  1547. // Should do minimal scroll needed to make the first item whole visible, but not hiding the second
  1548. Item->MakeVisible(false);
  1549. }
  1550. __finally
  1551. {
  1552. if (FlickerExpected)
  1553. {
  1554. EnableRedraw(ListView);
  1555. }
  1556. }
  1557. Found = true;
  1558. }
  1559. }
  1560. }
  1561. // Allow going through the first item twice, to make sure we roll over its previous pair candidates
  1562. if (Item == FirstItem)
  1563. {
  1564. Item = NULL;
  1565. }
  1566. else
  1567. {
  1568. if (FirstItem == NULL)
  1569. {
  1570. FirstItem = Item;
  1571. }
  1572. Item = ListView->GetNextItem(Item, sdAll, TItemStates());
  1573. if (Item == NULL)
  1574. {
  1575. Item = ListView->Items->Item[0];
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (!Found)
  1581. {
  1582. MessageBeep(MB_ICONHAND);
  1583. }
  1584. }
  1585. //---------------------------------------------------------------------------