RVClasses.pas
上传用户:daoqigc
上传日期:2021-04-20
资源大小:2795k
文件大小:7k
源码类别:

RichEdit

开发平台:

Delphi

  1. {*******************************************************}
  2. {                                                       }
  3. {       RichView                                        }
  4. {       Some basic classes for RichView.                }
  5. {                                                       }
  6. {       Copyright (c) Sergey Tkachenko                  }
  7. {       svt@trichview.com                               }
  8. {       http://www.trichview.com                        }
  9. {                                                       }
  10. {*******************************************************}
  11. unit RVClasses;
  12. interface
  13. uses Classes, Graphics;
  14. {$I RV_Defs.inc}
  15. type
  16.   {
  17.     List of TObjects. Automatically frees objects on deletion
  18.   }
  19.   TRVList = class (TList)
  20.     public
  21.       procedure Clear; {$IFDEF RICHVIEWDEF4}override;{$ENDIF}
  22.       procedure Delete(Index: Integer);
  23.       procedure DeleteAsPointer(Index: Integer);
  24.       destructor Destroy; override;
  25.   end;
  26.   {
  27.     List of integers
  28.   }
  29.   TRVIntegerList = class(TList)
  30.     private
  31.       function Get(Index: Integer): Integer;
  32.       procedure Put(Index: Integer; const Value: Integer);
  33.     public
  34.       constructor CreateEx(Count, Value: Integer);
  35.       constructor CreateCopy(Source:TRVIntegerList);
  36.       procedure Sort;
  37.       procedure InitWith(Value, Count: Integer);
  38.       procedure Fill(Value: Integer);
  39.       procedure Add(Value: Integer);
  40.       function AddUnique(Value: Integer): Integer;
  41.       procedure Insert(Index, Value: Integer);
  42.       procedure Assign(Source:TRVIntegerList);
  43.       property Items[Index: Integer]: Integer read Get write Put; default;
  44.   end;
  45.   {
  46.     List of TColors
  47.   }
  48.   TRVColorList = class (TRVIntegerList)
  49.     public
  50.       procedure AddUnique(Value: Integer);    
  51.   end;
  52. implementation
  53. function SortIntegers(Item1, Item2: Pointer): Integer;
  54. begin
  55.   Result := Integer(Item1)-Integer(Item2);
  56. end;
  57. {================================= TRVList ====================================}
  58. { Removes all items from the list. Frees all objects.                          }
  59. procedure TRVList.Clear;
  60. var i: Integer;
  61. begin
  62.    for i := 0 to Count-1 do
  63.      TObject(Items[i]).Free;
  64.    inherited Clear;
  65. end;
  66. {------------------------------------------------------------------------------}
  67. { Deletes the item with Index. Frees the item's object.                        }
  68. procedure TRVList.Delete(Index: Integer);
  69. begin
  70.   TObject(Items[Index]).Free;
  71.   inherited Delete(Index);
  72. end;
  73. {------------------------------------------------------------------------------}
  74. { Deletes the item Index without freeing its object.                           }
  75. procedure TRVList.DeleteAsPointer(Index: Integer);
  76. begin
  77.   inherited Delete(Index);
  78. end;
  79. {------------------------------------------------------------------------------}
  80. { Destructor.                                                                  }
  81. destructor TRVList.Destroy;
  82. begin
  83.   Clear;
  84.   inherited Destroy;
  85. end;
  86. {============================== TRVIntegerList ================================}
  87. { Adds a new value                                                             }
  88. procedure TRVIntegerList.Add(Value: Integer);
  89. begin
  90.   inherited Add(Pointer(Value));
  91. end;
  92. {------------------------------------------------------------------------------}
  93. { Inserts a new value                                                          }                                                         
  94. procedure TRVIntegerList.Insert(Index, Value: Integer);
  95. begin
  96.   inherited Insert(Index, Pointer(Value));
  97. end;
  98. {------------------------------------------------------------------------------}
  99. { Accessing Items[Index]                                                       }
  100. function TRVIntegerList.Get(Index: Integer): Integer;
  101. begin
  102.   Result := Integer(inherited Get(Index));
  103. end;
  104. {------------------------------------------------------------------------------}
  105. { Assigning Items[Index]                                                       }
  106. procedure TRVIntegerList.Put(Index: Integer; const Value: Integer);
  107. begin
  108.   inherited Put(Index, Pointer(Value));
  109. end;
  110. {------------------------------------------------------------------------------}
  111. { Creates a new list with Count items having Value                             }
  112. constructor TRVIntegerList.CreateEx(Count, Value: Integer);
  113. begin
  114.   inherited Create;
  115.   Capacity := Count;
  116.   while Count>0 do begin
  117.     Add(Value);
  118.     dec(Count);
  119.   end;
  120. end;
  121. {------------------------------------------------------------------------------}
  122. { Creates a copy of Source                                                     }
  123. constructor TRVIntegerList.CreateCopy(Source: TRVIntegerList);
  124. begin
  125.   inherited Create;
  126.   Assign(Source);
  127. end;
  128. {------------------------------------------------------------------------------}
  129. { Adds Value if it does not exist in the list. In any case, returns its index  }
  130. function TRVIntegerList.AddUnique(Value: Integer): Integer;
  131. begin
  132.   Result := IndexOf(Pointer(Value));
  133.   if Result=-1 then begin
  134.     inherited Add(Pointer(Value));
  135.     Result := Count-1;
  136.   end;
  137. end;
  138. {------------------------------------------------------------------------------}
  139. { Makes Self a copy of Source.                                                 }
  140. procedure TRVIntegerList.Assign(Source: TRVIntegerList);
  141. var i: Integer;
  142. begin
  143.   Clear;
  144.   Capacity := Source.Count;
  145.   for i := 0 to Source.Count-1 do
  146.     Add(Source.Items[i]);
  147. end;
  148. {------------------------------------------------------------------------------}
  149. { Adds Count items equal to Value                                              }
  150. procedure TRVIntegerList.InitWith(Value, Count: Integer);
  151. var i: Integer;
  152. begin
  153.   Clear;
  154.   Capacity := Count;
  155.   for i := 0 to Count-1 do
  156.     Add(Value);
  157. end;
  158. {------------------------------------------------------------------------------}
  159. { Assigns Value to all items                                                   } 
  160. procedure TRVIntegerList.Fill(Value: Integer);
  161. var i: Integer;
  162. begin
  163.   for i := 0 to Count-1 do
  164.     Items[i] := Value;
  165. end;
  166. {------------------------------------------------------------------------------}
  167. { Ascending sort                                                               }
  168. procedure TRVIntegerList.Sort;
  169. begin
  170.   inherited Sort(SortIntegers);
  171. end;
  172. {================================= TRVColorList ===============================}
  173. { The same as in TRVIntegerList, but does not allow adding clNone.             }
  174. procedure TRVColorList.AddUnique(Value: Integer);
  175. begin
  176.   if Value<>clNone then
  177.     inherited AddUnique(Value);
  178. end;
  179. end.