PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/ideintf/treeviewpropedit.pas

http://github.com/graemeg/lazarus
Pascal | 290 lines | 219 code | 46 blank | 25 comment | 16 complexity | 7ead0d201658d3e8c06bfd0eaf3e9d41 MD5 | raw file
  1{ Copyright (C) 2005
  2
  3 *****************************************************************************
  4 *                                                                           *
  5 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,    *
  6 *  for details about the copyright.                                         *
  7 *                                                                           *
  8 *  This program is distributed in the hope that it will be useful,          *
  9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
 11 *                                                                           *
 12 *****************************************************************************
 13
 14  Author: Lagunov Aleksey
 15
 16  Abstract:
 17    Property Editors for TTreeView.
 18}
 19
 20unit TreeViewPropEdit;
 21
 22{$mode objfpc}{$H+}
 23
 24interface
 25
 26uses
 27  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,
 28  PropEdits, Componenteditors, StdCtrls, ComCtrls, ObjInspStrConsts, ExtCtrls;
 29
 30type
 31
 32  { TTreeViewItemsEditorForm }
 33
 34  TTreeViewItemsEditorForm = class(TForm)
 35    BtnApply: TBitBtn;
 36    BtnCancel: TBitBtn;
 37    BtnHelp: TBitBtn;
 38    BtnOK: TBitBtn;
 39    BtnSave: TButton;
 40    BtnNewItem: TButton;
 41    BtnNewSubItem: TButton;
 42    BtnDelete: TButton;
 43    BtnLoad: TButton;
 44    edtText: TEdit;
 45    edtIndexImg: TEdit;
 46    edtIndexSel: TEdit;
 47    edtIndexState: TEdit;
 48    GroupBox1: TGroupBox;
 49    GroupBox2: TGroupBox;
 50    LabelText: TLabel;
 51    LabelImageIndex: TLabel;
 52    LabelSelectedIndex: TLabel;
 53    LabelStateIndex: TLabel;
 54    OpenDialog1: TOpenDialog;
 55    SaveDialog1: TSaveDialog;
 56    TreeView1: TTreeView;
 57    procedure BtnNewItemClick(Sender: TObject);
 58    procedure Edit1Change(Sender: TObject);
 59    procedure FormCreate(Sender: TObject);
 60    procedure TreeView1SelectionChanged(Sender: TObject);
 61    procedure btnApplyClick(Sender: TObject);
 62    procedure btnDeleteClick(Sender: TObject);
 63    procedure btnLoadClick(Sender: TObject);
 64    procedure btnSaveClick(Sender: TObject);
 65    procedure edtIndexStateEditingDone(Sender: TObject);
 66  private
 67    FTreeView: TTreeView;
 68    FModified: Boolean;
 69    procedure LoadFromTree(ATreeView:TTreeView);
 70    procedure SaveToTree;
 71  public
 72  end; 
 73
 74
 75type
 76  TTreeViewItemsProperty = class(TClassPropertyEditor)
 77  public
 78    procedure Edit; override;
 79    function  GetAttributes: TPropertyAttributes; override;
 80  end;
 81
 82  { TTreeViewComponentEditor }
 83
 84  TTreeViewComponentEditor = class(TComponentEditor)
 85  public
 86    procedure ExecuteVerb(Index: Integer); override;
 87    function GetVerb(Index: Integer): string; override;
 88    function GetVerbCount: Integer; override;
 89  end;
 90
 91implementation
 92
 93function EditTreeView(ATreeView: TTreeView):boolean;
 94var
 95  TreeViewItemsEditorForm: TTreeViewItemsEditorForm;
 96begin
 97  TreeViewItemsEditorForm:=TTreeViewItemsEditorForm.Create(Application);
 98  try
 99    TreeViewItemsEditorForm.LoadFromTree(ATreeView);
100    if TreeViewItemsEditorForm.ShowModal = mrOk then
101      TreeViewItemsEditorForm.SaveToTree;
102    Result:=TreeViewItemsEditorForm.FModified;
103  finally
104    TreeViewItemsEditorForm.Free;
105  end;
106end;
107
108{ TTreeViewItemsEditorForm }
109
110procedure TTreeViewItemsEditorForm.BtnNewItemClick(Sender: TObject);
111var
112  S: String;
113begin
114  S := sccsTrEdtItem + IntToStr(TreeView1.Items.Count);
115  if (Sender as TComponent).Tag = 1 then
116    TreeView1.Selected := TreeView1.Items.Add(TreeView1.Selected, S)
117  else
118    TreeView1.Selected := TreeView1.Items.AddChild(TreeView1.Selected, S);
119
120  GroupBox2.Enabled := TreeView1.Items.Count > 0;
121  
122  edtText.SetFocus;
123  edtText.SelectAll;
124end;
125
126procedure TTreeViewItemsEditorForm.Edit1Change(Sender: TObject);
127begin
128  if Assigned(TreeView1.Selected) then
129    TreeView1.Selected.Text := edtText.Text;
130end;
131
132procedure TTreeViewItemsEditorForm.FormCreate(Sender: TObject);
133begin
134  Caption := sccsTrEdtCaption;
135
136  GroupBox1.Caption := sccsTrEdtGrpLCaption;
137  BtnNewItem.Caption := sccsTrEdtNewItem;
138  BtnNewSubItem.Caption := sccsTrEdtNewSubItem;
139  BtnDelete.Caption := sccsTrEdtDelete;
140  BtnLoad.Caption := sccsTrEdtLoad;
141  BtnSave.Caption := sccsTrEdtSave;
142  BtnApply.Caption := sccsTrEdtApply;
143
144  GroupBox2.Caption := sccsTrEdtGrpRCaption;
145  LabelText.Caption := sccsTrEdtLabelText;
146  LabelImageIndex.Caption := sccsTrEdtLabelImageIndex;
147  LabelSelectedIndex.Caption := sccsTrEdtLabelSelIndex;
148  LabelStateIndex.Caption := sccsTrEdtLabelStateIndex;
149  
150  OpenDialog1.Title := sccsTrEdtOpenDialog;
151  SaveDialog1.Title := sccsTrEdtSaveDialog;
152end;
153
154procedure TTreeViewItemsEditorForm.TreeView1SelectionChanged(Sender: TObject);
155begin
156  if Assigned(TreeView1.Selected) then
157  begin
158    edtText.Text := TreeView1.Selected.Text;
159    edtIndexImg.Text := IntToStr(TreeView1.Selected.ImageIndex);
160    edtIndexSel.Text := IntToStr(TreeView1.Selected.SelectedIndex);
161    edtIndexState.Text := IntToStr(TreeView1.Selected.StateIndex);
162  end;
163end;
164
165procedure TTreeViewItemsEditorForm.btnApplyClick(Sender: TObject);
166begin
167  SaveToTree;
168end;
169
170procedure TTreeViewItemsEditorForm.btnDeleteClick(Sender: TObject);
171var
172  TempNode: TTreeNode;
173begin
174  if Assigned(TreeView1.Selected) then
175  begin
176    TempNode := TreeView1.Selected.GetNextSibling;
177    if TempNode = nil then
178      TempNode := TreeView1.Selected.GetPrevSibling;
179    if TempNode = nil then
180      TempNode := TreeView1.Selected.Parent;
181      
182    TreeView1.Items.Delete(TreeView1.Selected);
183    
184    if TempNode <> nil then
185      TreeView1.Selected := TempNode;
186      
187    GroupBox2.Enabled := TreeView1.Items.Count > 0;
188    TreeView1.SetFocus;
189  end;
190end;
191
192procedure TTreeViewItemsEditorForm.btnLoadClick(Sender: TObject);
193begin
194  if OpenDialog1.Execute then
195  begin
196    TreeView1.LoadFromFile(OpenDialog1.FileName);
197    GroupBox2.Enabled := TreeView1.Items.Count > 0;
198  end;
199end;
200
201procedure TTreeViewItemsEditorForm.btnSaveClick(Sender: TObject);
202begin
203  if SaveDialog1.Execute then
204    TreeView1.SaveToFile(SaveDialog1.FileName);
205end;
206
207procedure TTreeViewItemsEditorForm.edtIndexStateEditingDone(Sender: TObject);
208begin
209  if Assigned(TreeView1.Selected) then
210  begin
211    TreeView1.Selected.ImageIndex := StrToIntDef(edtIndexImg.Text, -1);
212    TreeView1.Selected.SelectedIndex := StrToIntDef(edtIndexSel.Text, -1);
213    TreeView1.Selected.StateIndex := StrToIntDef(edtIndexState.Text, -1);
214    
215    edtIndexImg.Text := IntToStr(TreeView1.Selected.ImageIndex);
216    edtIndexSel.Text := IntToStr(TreeView1.Selected.SelectedIndex);
217    edtIndexState.Text := IntToStr(TreeView1.Selected.StateIndex);
218  end;
219end;
220
221procedure TTreeViewItemsEditorForm.LoadFromTree(ATreeView: TTreeView);
222begin
223  FTreeView := ATreeView;
224  if Assigned(ATreeView) then
225  begin
226    TreeView1.Images := ATreeView.Images;
227    TreeView1.StateImages := ATreeView.StateImages;
228    TreeView1.Items.Assign(ATreeView.Items);
229  end;
230  
231  GroupBox2.Enabled := TreeView1.Items.Count > 0;
232end;
233
234procedure TTreeViewItemsEditorForm.SaveToTree;
235begin
236  if Assigned(FTreeView) then
237  begin
238    FTreeView.Items.Assign(TreeView1.Items);
239    FModified := True;
240  end;
241end;
242
243{ TTreeViewItemsProperty }
244
245procedure TTreeViewItemsProperty.Edit;
246begin
247  if EditTreeView(GetComponent(0) as TTreeView) then
248    Modified;
249end;
250
251function TTreeViewItemsProperty.GetAttributes: TPropertyAttributes;
252begin
253  Result := [paDialog, paReadOnly, paRevertable];
254end;
255
256{ TTreeViewComponentEditor }
257procedure TTreeViewComponentEditor.ExecuteVerb(Index: Integer);
258var
259  Hook: TPropertyEditorHook;
260begin
261  If Index = 0 then
262  begin
263    GetHook(Hook);
264    if EditTreeView(GetComponent as TTreeView) then
265      if Assigned(Hook) then
266        Hook.Modified(Self);
267  end;
268end;
269
270function TTreeViewComponentEditor.GetVerb(Index: Integer): string;
271begin
272  Result := '';
273  If Index = 0 then
274    Result := sccsTrEdt;
275end;
276
277function TTreeViewComponentEditor.GetVerbCount: Integer;
278begin
279  Result := 1;
280end;
281
282initialization
283  {$I treeviewpropedit.lrs}
284
285  RegisterPropertyEditor(ClassTypeInfo(TTreeNodes), TTreeView, 'Items', TTreeViewItemsProperty);
286  RegisterComponentEditor(TTreeView,TTreeViewComponentEditor);
287  
288  
289end.
290