NamedObjs.cpp 4.1 KB

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