Читать «Советы по Delphi. Версия 1.0.6» онлайн - страница 47

Валентин Озеров

var

 Item: TTVItem;

begin

 Result:= False;

 if ANode = nil then Exit;

 with Item do begin

  mask:= TVIF_STATE;

  hItem:= ANode.ItemId;

  if TreeView_GetItem(Handle, Item) then Result:= (state and TVIS_BOLD) <> 0;

 end;

end;

procedure THETreeView.SetSortType(Value: TSortType);

begin

 if SortType <> Value then begin

FSortType:= Value;

  if ((SortType in [stData, stBoth]) and Assigned(OnCompare)) or (SortType in [stText, stBoth]) then AlphaSort;

 end;

end;

procedure THETreeView.LoadFromFile(const AFileName: string);

var

 AList: TStringList;

begin

 AList:= TStringList.Create;

 Items.BeginUpdate;

 try

  AList.LoadFromFile(AFileName);

  SetItemList(AList);

 finally

  Items.EndUpdate;

  AList.Free;

 end;

end;

procedure THETreeView.SaveToFile(const AFileName: string);

var

 AList: TStringList;

begin

 AList:= TStringList.Create;

 try

  GetItemList(AList);

  AList.SaveToFile(AFileName);

 finally

  AList.Free;

 end;

end;

procedure THETreeView.SetItemList(AList: TStrings);

var

 ALevel, AOldLevel, i, Cnt: Integer;

 S: string;

 ANewStr: string;

 AParentNode: TTreeNode;

 TmpSort: TSortType;

 function GetBufStart(Buffer: PChar; var ALevel: Integer): PChar;

 begin

  ALevel:= 0;

  while Buffer^ in [' ', #9] do begin

   Inc(Buffer);

   Inc(ALevel);

  end;

  Result:= Buffer;

 end;

begin

 //Удаление всех элементов – в обычной ситуации подошло бы Items.Clear, но уж очень медленно

 SendMessage(handle, TVM_DELETEITEM, 0, Longint(TVI_ROOT));

 AOldLevel:= 0;

 AParentNode:= nil;

 //Снятие флага сортировки

 TmpSort:= SortType;

 SortType:= stNone;

 try

  for Cnt := 0 to AList.Count-1 do begin

   S:= AList[Cnt];

   if (length(s) = 1) and (s[1] = chr($1a)) then break;

   ANewStr:= GetBufStart(PChar(S), ALevel);

   if (ALevel > AOldLevel) or (AParentNode = nil) then begin

    if ALevel - AOldLevel > 1 then raise Exception.Create('Неверный уровень TreeNode');

   end else begin

    for i:= AOldLevel downto ALevel do begin

     AParentNode:= AParentNode.Parent;

     if (AParentNode = nil) and (i - ALevel > 0) then raise Exception.Create('Неверный уровень TreeNode');

    end;

   end;

   AParentNode:= Items.AddChild(AParentNode, ANewStr);

   AOldLevel:= ALevel;

  end;

 finally

  //Возвращаем исходный флаг сортировки…

  SortType:= TmpSort;

 end;

end;

procedure THETreeView.GetItemList(AList: TStrings);

var

 i, Cnt: integer;

 ANode: TTreeNode;

begin

 AList.Clear;

 Cnt:= Items.Count -1;

 ANode:= Items.GetFirstNode;

 for i:= 0 to Cnt do begin

  AList.Add(GetItemText(ANode));

  ANode:= ANode.GetNext;

 end;

end;

function THETreeView.GetItemText(ANode: TTreeNode): string;

begin

 Result:= StringOfChar(' ', ANode.Level) + ANode.Text;

end;

function THETreeView.AlphaSort: Boolean;

var

 I: Integer;

begin

 if HandleAllocated then begin

  Result:= CustomSort(nil, 0);

 end else Result:= False;

end;

function eView.CustomSort(SortProc: TTVCompare; Data: Longint): Boolean;

var

 SortCB: TTVSortCB;

 I: Integer;

 Node: TTreeNode;

begin

 Result:= False;

 if HandleAllocated then begin

  with SortCB do begin

   if not Assigned(SortProc) then lpfnCompare:= @DefaultTreeViewSort