File: changeparentdlg.pas

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 (275 lines) | stat: -rw-r--r-- 6,885 bytes parent folder | download | duplicates (2)
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
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: FTurtle

  Abstract:
    Dialog for choosing new parent name.
}

unit ChangeParentDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, strutils,
  // LCL
  Forms, Controls, StdCtrls, ButtonPanel,
  // LazControls
  ListFilterEdit,
  // IdeIntf
  ObjInspStrConsts, PropEditUtils, IDEImagesIntf, IDEWindowIntf;

type

  { TChangeParentDlg }

  TChangeParentDlg = class(TForm)
    ButtonPanel: TButtonPanel;
    chShowClasses: TCheckBox;
    lblSelectedControls: TLabel;
    lblCurentParents: TLabel;
    ListBox: TListBox;
    ListFilterEdit: TListFilterEdit;
    procedure chShowClassesClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ListBoxDblClick(Sender: TObject);
    procedure ListBoxSelectionChange(Sender: TObject; User: boolean);
    procedure ListFilterEditAfterFilter(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
  private
    class var
      FSavedWidth: Integer;
      FSavedHeight: Integer;
      FSavedShowClasses: Boolean;
  private
    FCandidates: TFPList;
    FIgnoredCandidateName: string;
    function GetSelectedItem: string;
    procedure RefreshList;
    procedure SetSelection(ASelection: TPersistentSelectionList);
    procedure UpdateOKButtonState;
  public
    function ShowModal: Integer; override;
  public
    property SelectedItem: string read GetSelectedItem;
    property Selection: TPersistentSelectionList write SetSelection;
    property Candidates: TFPList write FCandidates;
  end;

function ShowChangeParentDlg(ASelection: TPersistentSelectionList;
  ACandidates: TFPList; out ANewParentName: string): Boolean;


implementation

{$R *.lfm}

{ TChangeParentDlg }

const
  colon = ': ';

function ShowChangeParentDlg(ASelection: TPersistentSelectionList;
  ACandidates: TFPList; out ANewParentName: string): Boolean;
begin
  if not Assigned(ASelection) or not Assigned(ACandidates) then
    Exit(False);

  with TChangeParentDlg.Create(nil) do
  try
    Selection := ASelection;
    Candidates := ACandidates;
    Result := (ShowModal = mrOK);
    if Result then
      ANewParentName := SelectedItem;
  finally
    Free;
  end;
end;


procedure TChangeParentDlg.FormCreate(Sender: TObject);
begin
  Constraints.MinHeight := 250;
  Constraints.MinWidth := 175;

  Height := FSavedHeight;  // see "initialization"
  Width := FSavedWidth;
  chShowClasses.Checked := FSavedShowClasses;

  Caption := oisChangeParent;
  chShowClasses.Caption := oisShowClasses;

  IDEDialogLayoutList.ApplyLayout(Self);
end;

{$HINTS OFF}
procedure TChangeParentDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  FSavedHeight := Height;
  FSavedWidth := Width;
  FSavedShowClasses := chShowClasses.Checked;
  IDEDialogLayoutList.SaveLayout(Self);
end;
{$HINTS ON}

procedure TChangeParentDlg.chShowClassesClick(Sender: TObject);
begin
  if Assigned(FCandidates) then
    RefreshList;
end;

procedure TChangeParentDlg.ListBoxDblClick(Sender: TObject);
begin
  ButtonPanel.OKButton.Click;
end;

{$HINTS OFF}
procedure TChangeParentDlg.ListBoxSelectionChange(Sender: TObject; User: boolean);
begin
  UpdateOKButtonState;
end;
{$HINTS ON}

procedure TChangeParentDlg.ListFilterEditAfterFilter(Sender: TObject);
begin
  UpdateOKButtonState;
end;

procedure TChangeParentDlg.OKButtonClick(Sender: TObject);
begin
  if ListBox.ItemIndex < 0 then
    ModalResult := mrNone;
end;

function TChangeParentDlg.GetSelectedItem: string;
var
  n: Integer;
begin
  if ListBox.ItemIndex < 0 then
    Exit('');
  Result := ListBox.Items[ListBox.ItemIndex];
  n := Pos(colon, Result);
  if n>0 then
    SetLength(Result, n-1);
end;

procedure TChangeParentDlg.RefreshList;
var
  i: Integer;
  OldIndex: Integer;

  function MakeItem(ACandidate: TWinControl): string;
  begin
    if chShowClasses.Checked then
      Result := ACandidate.Name + colon + ACandidate.ClassName
    else
      Result := ACandidate.Name;
  end;

  function IsIgnoredName: Boolean;
  begin
    Result := (TWinControl(FCandidates.Items[i]).Name = FIgnoredCandidateName);
  end;

begin
  OldIndex := ListBox.ItemIndex;
  ListFilterEdit.FilteredListbox := nil;
  ListBox.Items.Clear;
  for i:=0 to FCandidates.Count-1 do
    if not IsIgnoredName then
      ListBox.Items.Add(MakeItem(TWinControl(FCandidates.Items[i])));
  ListBox.ItemIndex := OldIndex;  // if list was filtered it may select other item
  ListFilterEdit.FilteredListbox := ListBox;
  ListFilterEdit.Text := '';
  UpdateOKButtonState;
end;

procedure TChangeParentDlg.SetSelection(ASelection: TPersistentSelectionList);
var
  i, ControlsCount: Integer;
  sControls, sParents: string;
  CurParentNameList: TStringList;

  procedure AddControlName(AControlName: string);
  begin
    Inc(ControlsCount);
    if ControlsCount = 1 then
      sControls := AControlName
    else
      sControls := sControls + ', ' + AControlName;
  end;

  procedure TryAddParentName(AParentName: string);
  begin
    if CurParentNameList.IndexOf(AParentName) < 0 then
      CurParentNameList.Append(AParentName);
  end;

  procedure SetIgnoredCandidateName;
  var
    j: Integer;
  begin
    FIgnoredCandidateName := CurParentNameList[0];
    for j:=1 to CurParentNameList.Count-1 do
      if FIgnoredCandidateName <> CurParentNameList[j] then
      begin
        FIgnoredCandidateName := '';
        Break;
      end;
  end;

begin
  ControlsCount := 0;
  CurParentNameList := TStringList.Create;

  for i:=0 to ASelection.Count-1 do
    if ASelection.Items[i] is TControl then
      begin
        AddControlName(TControl(ASelection.Items[i]).Name);
        TryAddParentName(TControl(ASelection.Items[i]).Parent.Name);
      end;

  sControls := IfThen(ControlsCount > 1, oisSelectedControls, oisSelectedControl) +
    ': ' + sControls;

  if CurParentNameList.Count > 0 then
  begin
    sParents := IfThen(CurParentNameList.Count > 1, oisCurrentParents, oisCurrentParent) +
      ': ' + CurParentNameList[0];
    for i:=1 to CurParentNameList.Count-1 do
      sParents := sParents + ', ' + CurParentNameList[i];
    SetIgnoredCandidateName;
  end;

  lblSelectedControls.Caption := sControls;
  lblCurentParents.Caption := sParents;

  CurParentNameList.Free;
end;

procedure TChangeParentDlg.UpdateOKButtonState;
begin
  ButtonPanel.OKButton.Enabled := (ListBox.ItemIndex > -1);
end;

function TChangeParentDlg.ShowModal: Integer;
begin
  RefreshList;
  Result := inherited ShowModal;
end;

initialization
  TChangeParentDlg.FSavedWidth := 250;
  TChangeParentDlg.FSavedHeight := 390;
  TChangeParentDlg.FSavedShowClasses := False;

end.