SynchronizeChecklist.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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(TListItem * Item)
  1227. {
  1228. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  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. TListItem * Item1 = DebugNotNull(ListView->FindData(0, const_cast<TSynchronizeChecklist::TItem *>(MoveItems.first), true, false));
  1284. TListItem * Item2 = DebugNotNull(ListView->FindData(0, const_cast<TSynchronizeChecklist::TItem *>(MoveItems.second), true, false));
  1285. DeleteItem(Item1);
  1286. DeleteItem(Item2);
  1287. UpdateControls();
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. void __fastcall TSynchronizeChecklistDialog::CheckDirectory(bool Check)
  1291. {
  1292. std::unique_ptr<TStringList> Directories(new TStringList());
  1293. TListItem * Item = ListView->Selected;
  1294. while (Item != NULL)
  1295. {
  1296. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1297. // It does not matter if we use local or remote directory
  1298. Directories->Add(IncludeTrailingBackslash(ChecklistItem->Local.Directory));
  1299. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1300. }
  1301. TAutoFlag ChangingItemMassSwitch(FChangingItemMass);
  1302. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1303. {
  1304. TListItem * Item = ListView->Items->Item[Index];
  1305. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1306. UnicodeString Directory = IncludeTrailingBackslash(ChecklistItem->Local.Directory);
  1307. for (int Index2 = 0; Index2 < Directories->Count; Index2++)
  1308. {
  1309. if (StartsText(Directories->Strings[Index2], Directory))
  1310. {
  1311. Item->Checked = Check;
  1312. }
  1313. }
  1314. }
  1315. ChangingItemMassSwitch.Release();
  1316. UpdateControls();
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void __fastcall TSynchronizeChecklistDialog::CheckDirectoryActionExecute(TObject *)
  1320. {
  1321. CheckDirectory(true);
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TSynchronizeChecklistDialog::UncheckDirectoryActionExecute(TObject *)
  1325. {
  1326. CheckDirectory(false);
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TSynchronizeChecklistDialog::DoBrowse(TOperationSide Side)
  1330. {
  1331. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Selected);
  1332. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1333. FOnSynchronizeBrowse(Side, Action, ChecklistItem);
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TSynchronizeChecklistDialog::BrowseLocalActionExecute(TObject *)
  1337. {
  1338. DoBrowse(osLocal);
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. void __fastcall TSynchronizeChecklistDialog::BrowseRemoteActionExecute(TObject *)
  1342. {
  1343. DoBrowse(osRemote);
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TSynchronizeChecklistDialog::KeyDown(Word & Key, TShiftState Shift)
  1347. {
  1348. TShortCut KeyShortCut = ShortCut(Key, Shift);
  1349. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  1350. if (IsCustomShortCut(CustomShortCut))
  1351. {
  1352. TTBXItem * MenuItem = new TTBXItem(this);
  1353. CustomCommandsAction->ActionComponent = MenuItem;
  1354. CustomCommandsAction->Execute();
  1355. for (int Index = 0; Index < MenuItem->Count; Index++)
  1356. {
  1357. TTBCustomItem * Item = MenuItem->Items[Index];
  1358. if (Item->Enabled && (Item->ShortCut == CustomShortCut))
  1359. {
  1360. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  1361. Item->Click();
  1362. Key = 0;
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. TForm::KeyDown(Key, Shift);
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. void __fastcall TSynchronizeChecklistDialog::ListViewRecreate(TObject *)
  1371. {
  1372. FChecklistToListViewMap.clear();
  1373. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1374. {
  1375. TListItem * Item = ListView->Items->Item[Index];
  1376. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1377. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  1378. }
  1379. }
  1380. //---------------------------------------------------------------------------
  1381. void __fastcall TSynchronizeChecklistDialog::ToolsMenuButtonClick(TObject *)
  1382. {
  1383. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TSynchronizeChecklistDialog::FindMoveCandidateActionExecute(TObject *)
  1387. {
  1388. TIEListViewColProperties * ColProperties = GetColProperties();
  1389. int Sort = (ColProperties->SortAscending ? 1 : -1) * ColProperties->SortColumn;
  1390. if (FMoveCandidatesValidForSort != Sort)
  1391. {
  1392. FMoveCandidatesFileName.clear();
  1393. FMoveCandidatesSize.clear();
  1394. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1395. {
  1396. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Items->Item[Index]);
  1397. if (ChecklistItem->IsLocalOnly() || ChecklistItem->IsRemoteOnly())
  1398. {
  1399. if (ChecklistItem->IsDirectory)
  1400. {
  1401. FMoveCandidatesFileName[ChecklistItem->GetFileName().LowerCase()].push_back(ChecklistItem);
  1402. }
  1403. else
  1404. {
  1405. FMoveCandidatesSize[ChecklistItem->GetBaseSize()].push_back(ChecklistItem);
  1406. }
  1407. }
  1408. }
  1409. FMoveCandidatesValidForSort = Sort;
  1410. }
  1411. bool Found = false;
  1412. if (!FMoveCandidatesFileName.empty() || !FMoveCandidatesSize.empty())
  1413. {
  1414. TListItem * ItemFocused = ListView->ItemFocused;
  1415. TListItem * Item;
  1416. if (ItemFocused != NULL)
  1417. {
  1418. Item = ItemFocused;
  1419. }
  1420. // Can we have a selection without focus?
  1421. else if (DebugAlwaysFalse(ListView->Selected != NULL))
  1422. {
  1423. Item = ListView->Selected;
  1424. }
  1425. else if (ListView->Items->Count > 0)
  1426. {
  1427. Item = ListView->Items->Item[0];
  1428. }
  1429. else
  1430. {
  1431. DebugFail(); // the action is disabled when there are no items
  1432. Item = NULL;
  1433. }
  1434. TListItem * FirstItem = NULL;
  1435. while (!Found && (Item != NULL))
  1436. {
  1437. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1438. TSynchronizeChecklist::TAction OppositeAction = GetOppositeMoveAction(ChecklistItem->Action);
  1439. if ((OppositeAction != TSynchronizeChecklist::saNone) &&
  1440. // For focused item, we search pair even if the focused item is "delete" action,
  1441. // but when searching the next items, consider "transfer" actions only
  1442. ((Item == ItemFocused) || IsTransferNewAction(ChecklistItem->Action)))
  1443. {
  1444. TChecklistItems Candidates;
  1445. if (ChecklistItem->IsDirectory)
  1446. {
  1447. UnicodeString FileName = ChecklistItem->GetFileName().LowerCase();
  1448. TMoveCandidatesFileNameMap::const_iterator I = FMoveCandidatesFileName.find(FileName);
  1449. if (I != FMoveCandidatesFileName.end())
  1450. {
  1451. const TChecklistItems & NameCandidates = I->second;
  1452. for (size_t I = 0; I < NameCandidates.size(); I++)
  1453. {
  1454. const TSynchronizeChecklist::TItem * ChecklistItem2 = NameCandidates[I];
  1455. if ((ChecklistItem2->Action == OppositeAction) &&
  1456. DebugAlwaysTrue(ChecklistItem2->IsDirectory))
  1457. {
  1458. Candidates.push_back(ChecklistItem2);
  1459. }
  1460. }
  1461. }
  1462. }
  1463. else
  1464. {
  1465. __int64 Size = ChecklistItem->GetBaseSize();
  1466. TMoveCandidatesSizeMap::const_iterator I = FMoveCandidatesSize.find(Size);
  1467. if (I != FMoveCandidatesSize.end())
  1468. {
  1469. UnicodeString FileName = ChecklistItem->GetFileName();
  1470. const TChecklistItems & SizeCandidates = I->second;
  1471. for (size_t I = 0; I < SizeCandidates.size(); I++)
  1472. {
  1473. const TSynchronizeChecklist::TItem * ChecklistItem2 = SizeCandidates[I];
  1474. if ((ChecklistItem2->Action == OppositeAction) &&
  1475. DebugAlwaysTrue(!ChecklistItem2->IsDirectory))
  1476. {
  1477. bool IsCandidate;
  1478. // in addition to the same size, also the same file filename (although possibly different directory)
  1479. if (SameText(FileName, ChecklistItem2->GetFileName()))
  1480. {
  1481. IsCandidate = true;
  1482. }
  1483. // or different filename but the same directory (in addition to the same size)
  1484. else if ((ChecklistItem->Action == TSynchronizeChecklist::saDeleteLocal) ||
  1485. (ChecklistItem->Action == TSynchronizeChecklist::saDownloadNew))
  1486. {
  1487. IsCandidate = SamePaths(ChecklistItem->Local.Directory, ChecklistItem2->Local.Directory);
  1488. }
  1489. else if ((ChecklistItem->Action == TSynchronizeChecklist::saDeleteRemote) ||
  1490. (ChecklistItem->Action == TSynchronizeChecklist::saUploadNew))
  1491. {
  1492. IsCandidate = UnixSamePath(ChecklistItem->Remote.Directory, ChecklistItem2->Remote.Directory);
  1493. }
  1494. else
  1495. {
  1496. DebugFail();
  1497. IsCandidate = false;
  1498. }
  1499. if (IsCandidate)
  1500. {
  1501. Candidates.push_back(ChecklistItem2);
  1502. }
  1503. }
  1504. }
  1505. }
  1506. }
  1507. if (!Candidates.empty())
  1508. {
  1509. const TSynchronizeChecklist::TItem * ChecklistItem2 = Candidates[0];
  1510. if ((FirstItem == NULL) && Item->Selected && (ListView->SelCount == 2))
  1511. {
  1512. TListItem * NextSelected = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1513. if (NextSelected == NULL)
  1514. {
  1515. NextSelected = ListView->Selected; // Shorthand for GetNextItem(NULL, sdAll, isSelected)
  1516. }
  1517. TChecklistItems::const_iterator I = std::find(Candidates.begin(), Candidates.end(), GetChecklistItem(NextSelected));
  1518. if (I < Candidates.end() - 1)
  1519. {
  1520. ChecklistItem2 = *(I + 1);
  1521. }
  1522. else if (I == Candidates.end() - 1)
  1523. {
  1524. ChecklistItem2 = NULL;
  1525. }
  1526. }
  1527. if (ChecklistItem2 != NULL)
  1528. {
  1529. TListItem * Item2 = FChecklistToListViewMap[ChecklistItem2];
  1530. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1531. {
  1532. TListItem * ItemI = ListView->Items->Item[Index];
  1533. ItemI->Selected = (ItemI == Item) || (ItemI == Item2);
  1534. }
  1535. ListView->ItemFocused = Item;
  1536. // IsItemVisible returns true even on partial visibility,
  1537. // so it is still worth trying MakeVisible to make them completelly visible
  1538. bool FlickerExpected = !ListView->IsItemVisible(Item) || !ListView->IsItemVisible(Item2);
  1539. if (FlickerExpected)
  1540. {
  1541. // does not seem to have any effect
  1542. DisableRedraw(ListView);
  1543. }
  1544. try
  1545. {
  1546. // even if the item turns to be invisible in the end, it at least scrolls the view towards it
  1547. Item2->MakeVisible(false);
  1548. // Should do minimal scroll needed to make the first item whole visible, but not hiding the second
  1549. Item->MakeVisible(false);
  1550. }
  1551. __finally
  1552. {
  1553. if (FlickerExpected)
  1554. {
  1555. EnableRedraw(ListView);
  1556. }
  1557. }
  1558. Found = true;
  1559. }
  1560. }
  1561. }
  1562. // Allow going through the first item twice, to make sure we roll over its previous pair candidates
  1563. if (Item == FirstItem)
  1564. {
  1565. Item = NULL;
  1566. }
  1567. else
  1568. {
  1569. if (FirstItem == NULL)
  1570. {
  1571. FirstItem = Item;
  1572. }
  1573. Item = ListView->GetNextItem(Item, sdAll, TItemStates());
  1574. if (Item == NULL)
  1575. {
  1576. Item = ListView->Items->Item[0];
  1577. }
  1578. }
  1579. }
  1580. }
  1581. if (!Found)
  1582. {
  1583. MessageBeep(MB_ICONHAND);
  1584. }
  1585. }
  1586. //---------------------------------------------------------------------------