NamedObjs.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "NamedObjs.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. int __fastcall NamedObjectSortProc(void * Item1, void * Item2)
  10. {
  11. return static_cast<TNamedObject *>(Item1)->Compare(static_cast<TNamedObject *>(Item2));
  12. }
  13. //--- TNamedObject ----------------------------------------------------------
  14. __fastcall TNamedObject::TNamedObject(UnicodeString AName)
  15. {
  16. Name = AName;
  17. }
  18. //---------------------------------------------------------------------------
  19. void __fastcall TNamedObject::SetName(UnicodeString value)
  20. {
  21. FHidden = (value.SubString(1, TNamedObjectList::HiddenPrefix.Length()) == TNamedObjectList::HiddenPrefix);
  22. FName = value;
  23. }
  24. //---------------------------------------------------------------------------
  25. int __fastcall TNamedObject::Compare(TNamedObject * Other)
  26. {
  27. int Result;
  28. if (Hidden && !Other->Hidden)
  29. {
  30. Result = -1;
  31. }
  32. else if (!Hidden && Other->Hidden)
  33. {
  34. Result = 1;
  35. }
  36. else
  37. {
  38. Result = CompareLogicalText(Name, Other->Name);
  39. }
  40. return Result;
  41. }
  42. //---------------------------------------------------------------------------
  43. bool __fastcall TNamedObject::IsSameName(const UnicodeString & AName)
  44. {
  45. return (Name.CompareIC(AName) == 0);
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TNamedObject::MakeUniqueIn(TNamedObjectList * List)
  49. {
  50. // This object can't be item of list, it would create infinite loop
  51. if (List && (List->IndexOf(this) == -1))
  52. while (List->FindByName(Name))
  53. {
  54. Integer N = 0, P;
  55. // If name already contains number parenthesis remove it (and remember it)
  56. if ((Name[Name.Length()] == L')') && ((P = Name.LastDelimiter(L'(')) > 0))
  57. try
  58. {
  59. N = StrToInt(Name.SubString(P + 1, Name.Length() - P - 1));
  60. Name.Delete(P, Name.Length() - P + 1);
  61. Name = Name.TrimRight();
  62. }
  63. catch (Exception &E)
  64. {
  65. N = 0;
  66. }
  67. Name += L" (" + IntToStr(N+1) + L")";
  68. }
  69. }
  70. //--- TNamedObjectList ------------------------------------------------------
  71. const UnicodeString TNamedObjectList::HiddenPrefix = L"_!_";
  72. //---------------------------------------------------------------------------
  73. __fastcall TNamedObjectList::TNamedObjectList():
  74. TObjectList()
  75. {
  76. AutoSort = True;
  77. FHiddenCount = 0;
  78. }
  79. //---------------------------------------------------------------------------
  80. TNamedObject * __fastcall TNamedObjectList::AtObject(Integer Index)
  81. {
  82. return (TNamedObject *)Items[Index+FHiddenCount];
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TNamedObjectList::Recount()
  86. {
  87. int i = 0;
  88. while ((i < TObjectList::Count) && ((TNamedObject *)Items[i])->Hidden) i++;
  89. FHiddenCount = i;
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TNamedObjectList::AlphaSort()
  93. {
  94. Sort(NamedObjectSortProc);
  95. Recount();
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TNamedObjectList::Notify(void *Ptr, TListNotification Action)
  99. {
  100. TObjectList::Notify(Ptr, Action);
  101. if (Action == lnAdded)
  102. {
  103. FHiddenCount = -1;
  104. if (AutoSort)
  105. {
  106. AlphaSort();
  107. }
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. TNamedObject * __fastcall TNamedObjectList::FindByName(const UnicodeString & Name)
  112. {
  113. // this should/can be optimized when list is sorted
  114. for (Integer Index = 0; Index < TObjectList::Count; Index++)
  115. {
  116. TNamedObject * NamedObject = AtObject(Index);
  117. if (NamedObject->IsSameName(Name))
  118. {
  119. return NamedObject;
  120. }
  121. }
  122. return NULL;
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TNamedObjectList::SetCount(int value)
  126. {
  127. TObjectList::SetCount(value/*+HiddenCount*/);
  128. }
  129. //---------------------------------------------------------------------------
  130. int __fastcall TNamedObjectList::GetCount()
  131. {
  132. assert(FHiddenCount >= 0);
  133. return TObjectList::Count - FHiddenCount;
  134. }
  135. //---------------------------------------------------------------------------
  136. int __fastcall TNamedObjectList::GetCountIncludingHidden()
  137. {
  138. assert(FHiddenCount >= 0);
  139. return TObjectList::Count;
  140. }