File: listviewpropedit.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (362 lines) | stat: -rw-r--r-- 9,639 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

 Property editor for TListView objects

 Author: Olivier Guilbaud  (golivier@free.fr)
         Tomas Gregorovic
 
 History
   01/28/2003 OG - Create
   18/02/2003 OG - First release
   19/02/2003 OG - Add ObjInspStrConsts unit
   24/02/2003 OG - Replace TListBox with TTreeView
                   Include suItems property
   22/01/2006 TG - Dialog converted to lfm.
                   
   ToDo :
     Select the first item on show editor ... do not work :o(
}
unit ListViewPropEdit;

{$MODE OBJFPC}{$H+}

interface

uses
  Classes, SysUtils,
  // LCL
  Forms, Controls, ComCtrls, StdCtrls, Buttons, ButtonPanel,
  // IdeIntf
  PropEdits, ComponentEditors, ObjInspStrConsts, IDEWindowIntf;

type
  { TListViewItemsEditorForm }

  TListViewItemsEditorForm = class(TForm)
    BtnNewItem: TButton;
    BtnNewSubItem: TButton;
    BtnDelete: TButton;
    ButtonPanel: TButtonPanel;
    edtText: TEdit;
    edtIndexImg: TEdit;
    edtIndexState: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    LabelCaption: TLabel;
    LabelImageIndex: TLabel;
    LabelStateIndex: TLabel;
    TreeView1: TTreeView;
    procedure BtnNewItemClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TreeView1SelectionChanged(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure edtIndexStateEditingDone(Sender: TObject);
  private
    FListView: TListView;
    FModified: Boolean;
  public
    procedure LoadFromList(AListView: TListView);
    procedure SaveToList;
  end;

  { TListViewComponentEditor }

  TListViewComponentEditor = class(TComponentEditor)
  public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;

  {TListViewItemsPropertyEditor
   Property editor for the Items properties of TListView object.
   Brings up the dialog for editing items}
  TListViewItemsPropertyEditor = Class(TClassPropertyEditor)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
  
implementation

{$R *.lfm}

function EditListView(AListView: TListView): Boolean;
var
  ListViewEditorDlg: TListViewItemsEditorForm;
begin
  ListViewEditorDlg := TListViewItemsEditorForm.Create(Application);
  try
    ListViewEditorDlg.LoadFromList(AListView);

    if ListViewEditorDlg.ShowModal = mrOk then
      ListViewEditorDlg.SaveToList;
      
    Result := ListViewEditorDlg.FModified;
  finally
    ListViewEditorDlg.Free;
  end;
end;

{ TListViewItemsEditorForm }

procedure TListViewItemsEditorForm.FormCreate(Sender: TObject);
begin
  Caption  := sccsLvEdtCaption;

  GroupBox1.Caption := sccsLvEdtGrpLCaption;
  GroupBox2.Caption := sccsLvEdtGrpRCaption;

  BtnNewItem.Caption := sccsLvEdtNewItem;
  BtnNewSubItem.Caption := sccsLvEdtNewSubItem;
  BtnDelete.Caption := sccsLvEdtDelete;

  ButtonPanel.HelpButton.Caption := oisHelp;
  ButtonPanel.OKButton.Caption := oisOK;
  ButtonPanel.CancelButton.Caption := oisCancel;
  ButtonPanel.CloseButton.Caption := sccsLvEdtApply;
  ButtonPanel.CloseButton.Kind := bkCustom;
  ButtonPanel.CloseButton.Glyph := nil;
  ButtonPanel.CloseButton.ModalResult := mrNone;

  LabelCaption.Caption := sccsLvEdtLabelCaption;
  LabelImageIndex.Caption := sccsLvEdtLabelImageIndex;
  LabelStateIndex.Caption := sccsLvEdtLabelStateIndex;
  IDEDialogLayoutList.ApplyLayout(Self);
end;

procedure TListViewItemsEditorForm.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  IDEDialogLayoutList.SaveLayout(Self);
end;

procedure TListViewItemsEditorForm.BtnNewItemClick(Sender: TObject);
var
  S: String;
begin
  S := sccsLvEdtItem + IntToStr(TreeView1.Items.Count);
  if (Sender as TComponent).Tag = 1 then
    TreeView1.Selected := TreeView1.Items.Add(nil, S)
  else
  begin
    if (TreeView1.Selected=nil) or (TreeView1.Selected.Level = 0) then
      TreeView1.Selected := TreeView1.Items.AddChild(TreeView1.Selected, S)
    else
      TreeView1.Selected := TreeView1.Items.Add(TreeView1.Selected, S);
  end;

  GroupBox2.Enabled := TreeView1.Items.Count > 0;

  edtText.SetFocus;
  edtText.SelectAll;
end;

procedure TListViewItemsEditorForm.Edit1Change(Sender: TObject);
begin
  if Assigned(TreeView1.Selected) then
    TreeView1.Selected.Text := edtText.Text;
end;

procedure TListViewItemsEditorForm.TreeView1SelectionChanged(
  Sender: TObject);
begin
  if Assigned(TreeView1.Selected) then
  begin
    edtText.Text := TreeView1.Selected.Text;
    edtIndexImg.Text := IntToStr(TreeView1.Selected.ImageIndex);
    edtIndexState.Text := IntToStr(TreeView1.Selected.StateIndex);
  end;
end;

procedure TListViewItemsEditorForm.btnApplyClick(Sender: TObject);
begin
  SaveToList;
end;

procedure TListViewItemsEditorForm.btnDeleteClick(Sender: TObject);
var
  TempNode: TTreeNode;
begin
  if Assigned(TreeView1.Selected) then
  begin
    TempNode := TreeView1.Selected.GetNextSibling;
    if TempNode = nil then
      TempNode := TreeView1.Selected.GetPrevSibling;
    if TempNode = nil then
      TempNode := TreeView1.Selected.Parent;

    TreeView1.Items.Delete(TreeView1.Selected);

    if TempNode <> nil then
      TreeView1.Selected := TempNode;

    GroupBox2.Enabled := TreeView1.Items.Count > 0;
    TreeView1.SetFocus;
  end;
end;

procedure TListViewItemsEditorForm.edtIndexStateEditingDone(
  Sender: TObject);
begin
  if Assigned(TreeView1.Selected) then
  begin
    TreeView1.Selected.ImageIndex := StrToIntDef(edtIndexImg.Text, -1);
    TreeView1.Selected.StateIndex := StrToIntDef(edtIndexState.Text, -1);
    TreeView1.Selected.SelectedIndex := TreeView1.Selected.ImageIndex;

    edtIndexImg.Text := IntToStr(TreeView1.Selected.ImageIndex);
    edtIndexState.Text := IntToStr(TreeView1.Selected.StateIndex);
  end;
end;

procedure TListViewItemsEditorForm.LoadFromList(AListView: TListView);
var
  I, J: Integer;
  Node: TTreeNode;
begin
  FListView := AListView;
  if Assigned(AListView) then
  begin
    TreeView1.Images := AListView.SmallImages;
    TreeView1.StateImages := AListView.StateImages;
    
    TreeView1.Items.BeginUpdate;
    try
      TreeView1.Items.Clear;
      
      for I := 0 to AListView.Items.Count - 1 do
      begin
        Node := TreeView1.Items.Add(nil, AListView.Items[I].Caption);
        with Node do
        begin
          ImageIndex := AListView.Items[I].ImageIndex;
          StateIndex := AListView.Items[I].StateIndex;
          SelectedIndex := ImageIndex;
        end;

        //SubItems
        for J := 0 to AListView.Items[I].SubItems.Count - 1 do
        begin
          with TreeView1.Items.AddChild(Node, AListView.Items[I].SubItems[J]) do
          begin
            ImageIndex := AListView.Items[I].SubItemImages[J];
            SelectedIndex := ImageIndex;
          end;
        end;
      end;
    finally
      TreeView1.Items.EndUpdate;
    end;
  end;

  GroupBox2.Enabled := TreeView1.Items.Count > 0;
end;

procedure TListViewItemsEditorForm.SaveToList;
var
  I, J: Integer;
  Node: TTreeNode;
  Item: TListItem;
begin
  if Assigned(FListView) then
  begin
    FListView.BeginUpdate;
    try
      FListView.Items.Clear;

      //Recreate new items or modify
      for I := 0 to TreeView1.Items.Count - 1 do
      begin
        Node := TreeView1.Items[I];
        if Node.Level = 0 then
        begin
          Item := FListView.Items.Add;
          Item.Caption := Node.Text;
          Item.ImageIndex := Node.ImageIndex;
          Item.StateIndex := Node.StateIndex;

          //SubItems
          for J := 0 to Node.Count - 1 do
          begin
            Item.SubItems.Add(Node.Items[J].Text);
            Item.SubItemImages[J] := Node.Items[J].ImageIndex;
          end;
        end;
      end;
    finally
      FListView.EndUpdate;
    end;
        
    FModified := True;
  end;
end;

{ TListViewItemsPropertyEditor }

procedure TListViewItemsPropertyEditor.Edit;
begin
  if EditListView(GetComponent(0) as TListView) then Modified;
end;

function TListViewItemsPropertyEditor.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog, paReadOnly, paRevertable];
end;

{ TListViewComponentEditor }

procedure TListViewComponentEditor.ExecuteVerb(Index: Integer);
var
  Hook: TPropertyEditorHook;
  AListView: TListView;
begin
  AListView := GetComponent as TListView;
  case Index of
    0: 
    begin
      GetHook(Hook);
      if EditListView(AListView) then
        if Assigned(Hook) then Hook.Modified(Self);
    end;
    1:
    begin
      GetHook(Hook);
      EditCollection(AListView, AListView.Columns, 'Columns');
      if Assigned(Hook) then Hook.Modified(Self);
    end;
  end;
end;

function TListViewComponentEditor.GetVerb(Index: Integer): string;
begin
  Result := '';
  case Index of
    0: Result := sccsLvEdt;
    1: Result := sccsLvColEdt;
    else
      Result := '';
  end;
end;

function TListViewComponentEditor.GetVerbCount: Integer;
begin
  Result := 2;
end;

initialization
  //Register TListViewItemsPropertyEditor
  RegisterPropertyEditor(ClassTypeInfo(TListItems), TListView, 'Items',
    TListViewItemsPropertyEditor);

  //Register a component editor for TListView
  RegisterComponentEditor(TListView, TListViewComponentEditor);
  
end.