File: idetextconvlistedit.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 (363 lines) | stat: -rw-r--r-- 10,494 bytes parent folder | download | duplicates (8)
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
363
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Mattias Gaertner

  Abstract:
    An editor for a list of TCustomTextConverterTool.
}
unit IDETextConvListEdit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ClipBrd, Buttons, ExtCtrls,
  IDETextConverter, ObjectInspector,
  IDETextConvListAdd, h2passtrconsts;

type

  { TTextConvListEditor }

  TTextConvListEditor = class(TForm)
    AddToolButton: TButton;
    CloneButton: TButton;
    PasteButton: TButton;
    CopyToolButton: TButton;
    MoveToolDownButton: TButton;
    MoveToolUpButton: TButton;
    DeleteToolButton: TButton;
    ToolsSplitter: TSplitter;
    ToolsPanel: TPanel;
    ToolsListBox: TListBox;
    UpDownSplitter: TSplitter;
    ToolsLabel: TLabel;
    PropertyGrid: TCustomPropertiesGrid;
    procedure AddToolButtonClick(Sender: TObject);
    procedure CloneButtonClick(Sender: TObject);
    procedure CopyToolButtonClick(Sender: TObject);
    procedure DeleteToolButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure MoveToolDownButtonClick(Sender: TObject);
    procedure MoveToolUpButtonClick(Sender: TObject);
    procedure PasteButtonClick(Sender: TObject);
    procedure PropertyGridModified(Sender: TObject);
    procedure ToolsListBoxSelectionChange(Sender: TObject; User: Boolean);
  private
    FListOfTools: TComponent;
    FModified: boolean;
    FOnModified: TNotifyEvent;
    procedure SetListOfTools(const AValue: TComponent);
    procedure SetModified(const AValue: boolean);
    procedure MoveSelection(Offset: integer);
    function GetCurrentTool: TCustomTextConverterTool;
    procedure MakeToolCaptionAndNameUnique(NewTool: TCustomTextConverterTool);
  public
    procedure SelectTool(Tool: TCustomTextConverterTool);
    function CreateTool(ToolClass: TCustomTextConverterToolClass
                        ): TCustomTextConverterTool;
    procedure UpdateAll;
    procedure UpdateToolsListBox;
    procedure UpdateButtons;
    property ListOfTools: TComponent read FListOfTools write SetListOfTools;
    property Modified: boolean read FModified write SetModified;
    property OnModified: TNotifyEvent read FOnModified write FOnModified;
  end;

var
  TextConvListEditor: TTextConvListEditor;


implementation

{$R idetextconvlistedit.lfm}

{ TTextConvListEditor }

procedure TTextConvListEditor.FormCreate(Sender: TObject);
begin
  Caption := h2pTextConversionToolsEditor;
  ToolsLabel.Caption := h2pTools;
  
  // buttons
  AddToolButton.Caption := h2pAddNewTool;
  CloneButton.Caption := h2pAddACopy;
  PasteButton.Caption := h2pAddFromClipboard;
  CopyToolButton.Caption := h2pCopyToolToClipboard;
  MoveToolDownButton.Caption := h2pMoveDown;
  MoveToolUpButton.Caption := h2pMoveUp;
  DeleteToolButton.Caption := h2pDeleteTool;

  PropertyGrid:=TCustomPropertiesGrid.Create(Self);
  PropertyGrid.Align:=alBottom;
  PropertyGrid.AnchorToNeighbour(akTop,0,UpDownSplitter);
  PropertyGrid.Parent:=Self;
  PropertyGrid.OnModified:=@PropertyGridModified;
  
  UpdateButtons;
end;

procedure TTextConvListEditor.MoveToolDownButtonClick(Sender: TObject);
begin
  MoveSelection(+1);
end;

procedure TTextConvListEditor.MoveToolUpButtonClick(Sender: TObject);
begin
  MoveSelection(-1);
end;

procedure TTextConvListEditor.PasteButtonClick(Sender: TObject);
var
  NewComponent: TComponent;
  NewTool: TCustomTextConverterTool;
begin
  if FListOfTools=nil then exit;
  try
    NewComponent:=nil;
    Clipboard.GetComponentAsText(NewComponent,
                              @TextConverterToolClasses.FindClass,FListOfTools);
    if NewComponent=nil then
      raise Exception.Create('nil');
    if not (NewComponent is TCustomTextConverterTool) then begin
      NewComponent.Free;
      raise Exception.Create(h2pNotATCustomTextConverterTool);
    end;
    NewTool:=TCustomTextConverterTool(NewComponent);
    MakeToolCaptionAndNameUnique(NewTool);
    Modified:=true;
    UpdateToolsListBox;
    SelectTool(NewTool);
  except
    on E: Exception do begin
      MessageDlg(h2pError,
        Format(h2pErrorConvertingClipboardTextToTextTool, [#13, E.Message]), mtError, [mbCancel], 0);
    end;
  end;
end;

procedure TTextConvListEditor.PropertyGridModified(Sender: TObject);
var
  Tool: TCustomTextConverterTool;
begin
  Tool:=GetCurrentTool;
  //DebugLn(['TTextConvListEditor.PropertyGridModified ',dbgsName(Tool)]);
  if Tool=nil then exit;
  MakeToolCaptionAndNameUnique(Tool);
  Modified:=true;
end;

procedure TTextConvListEditor.ToolsListBoxSelectionChange(Sender: TObject;
  User: Boolean);
var
  Tool: TCustomTextConverterTool;
begin
  if User then ;
  if csDestroying in ComponentState then exit;
  UpdateButtons;
  Tool:=GetCurrentTool;
  //DebugLn(['TTextConvListEditor.ToolsListBoxSelectionChange Tool=',dbgsName(Tool)]);
  PropertyGrid.TIObject:=Tool;
end;

procedure TTextConvListEditor.AddToolButtonClick(Sender: TObject);
var
  ToolClass: TCustomTextConverterToolClass;
begin
  if FListOfTools=nil then exit;
  if ShowIDETextConvListAddDlg(ToolClass)<>mrOk then exit;
  CreateTool(ToolClass);
end;

procedure TTextConvListEditor.CloneButtonClick(Sender: TObject);
var
  Tool: TCustomTextConverterTool;
  NewTool: TCustomTextConverterTool;
begin
  Tool:=GetCurrentTool;
  if Tool=nil then exit;
  NewTool:=TCustomTextConverterToolClass(Tool.ClassType).Create(FListOfTools);
  NewTool.Assign(Tool);
  MakeToolCaptionAndNameUnique(NewTool);
  Modified:=true;
  UpdateToolsListBox;
  SelectTool(NewTool);
end;

procedure TTextConvListEditor.CopyToolButtonClick(Sender: TObject);
var
  Tool: TCustomTextConverterTool;
begin
  Tool:=GetCurrentTool;
  if Tool=nil then exit;
  try
    Clipboard.SetComponentAsText(Tool);
  except
    on E: Exception do begin
      MessageDlg(h2pError,
        Format(h2pErrorConvertingPuttingToolOntoClipboard, [#13, E.Message]), mtError, [mbCancel], 0);
    end;
  end;
end;

procedure TTextConvListEditor.DeleteToolButtonClick(Sender: TObject);
var
  Tool: TCustomTextConverterTool;
  i: LongInt;
begin
  Tool:=GetCurrentTool;
  if Tool=nil then exit;
  if QuestionDlg(h2pConfirmDelete,
    Format(h2pDoYouReallyWantToDelete, [Tool.Caption]),
    mtConfirmation, [mrYes, h2pDelete, mrCancel], 0
    )<>mrYes
  then exit;
  i:=ToolsListBox.ItemIndex;
  PropertyGrid.TIObject:=nil;
  Tool.Free;
  Modified:=true;
  UpdateToolsListBox;
  if i>=ToolsListBox.Items.Count then
    i:=ToolsListBox.Items.Count-1;
  ToolsListBox.ItemIndex:=i;
end;

procedure TTextConvListEditor.SetListOfTools(const AValue: TComponent);
begin
  if (FListOfTools=AValue) then exit;
  FListOfTools:=AValue;
  PropertyGrid.TIObject:=nil;
  UpdateAll;
end;

procedure TTextConvListEditor.SetModified(const AValue: boolean);
begin
  if FModified=AValue then exit;
  FModified:=AValue;
  if FModified and Assigned(OnModified) then OnModified(Self);
end;

procedure TTextConvListEditor.UpdateAll;
begin
  UpdateToolsListBox;
  UpdateButtons;
end;

procedure TTextConvListEditor.UpdateToolsListBox;
var
  sl: TStringList;
  i: Integer;
  Tool: TCustomTextConverterTool;
  OldSelected: String;
begin
  sl:=TStringList.Create;
  if FListOfTools<>nil then begin
    for i:=0 to FListOfTools.ComponentCount-1 do begin
      Tool:=FListOfTools.Components[i] as TCustomTextConverterTool;
      sl.Add(Tool.Caption);
      //DebugLn(['TTextConvListEditor.UpdateToolsListBox Caption=',Tool.Caption,' ',dbgsName(Tool)]);
    end;
  end;
  //DebugLn(['TTextConvListEditor.UpdateToolsListBox ',sl.Count,' "',sl.Text,'"']);
  // save selection
  OldSelected:='';
  if ToolsListBox.ItemIndex>=0 then
    OldSelected:=ToolsListBox.Items[ToolsListBox.ItemIndex];
  // commit new list
  ToolsListBox.Items.Assign(sl);
  // restore selection
  if OldSelected<>'' then
    ToolsListBox.ItemIndex:=ToolsListBox.Items.IndexOf(OldSelected);
  sl.Free;
end;

procedure TTextConvListEditor.UpdateButtons;
var
  i: LongInt;
begin
  i:=ToolsListBox.ItemIndex;
  DeleteToolButton.Enabled:=(i>=0);
  MoveToolDownButton.Enabled:=(i<ToolsListBox.Items.Count-1);
  MoveToolUpButton.Enabled:=(i>0);
  CloneButton.Enabled:=(i>=0);
  PasteButton.Enabled:=true;
  CopyToolButton.Enabled:=(i>=0);
end;

procedure TTextConvListEditor.MoveSelection(Offset: integer);
var
  i: LongInt;
  Tool: TCustomTextConverterTool;
begin
  if FListOfTools=nil then exit;
  if Offset=0 then exit;
  i:=ToolsListBox.ItemIndex;
  if (i>=0) and (i<FListOfTools.ComponentCount)
  and (i+Offset>=0) and (i+Offset<FListOfTools.ComponentCount) then begin
    Tool:=FListOfTools.Components[i] as TCustomTextConverterTool;
    Tool.ComponentIndex:=Tool.ComponentIndex+Offset;
    Modified:=true;
    UpdateToolsListBox;
  end;
end;

function TTextConvListEditor.GetCurrentTool: TCustomTextConverterTool;
var
  i: LongInt;
begin
  Result:=nil;
  if FListOfTools=nil then exit;
  i:=ToolsListBox.ItemIndex;
  //DebugLn(['TTextConvListEditor.GetCurrentTool ',dbgsName(Self),' ToolsListBox.ItemIndex=',ToolsListBox.ItemIndex,' FListOfTools.ComponentCount=',FListOfTools.ComponentCount]);
  if (i<0) or (i>=FListOfTools.ComponentCount) then exit;
  Result:=TCustomTextConverterTool(FListOfTools.Components[i]);
end;

procedure TTextConvListEditor.MakeToolCaptionAndNameUnique(
  NewTool: TCustomTextConverterTool);
var
  i: Integer;
begin
  MakeToolNameUnique(FListOfTools,NewTool);
  MakeToolCaptionUnique(FListOfTools,NewTool);
  if (FListOfTools<>nil) then begin
    for i:=0 to FListOfTools.ComponentCount-1 do begin
      if FListOfTools.Components[i]=NewTool then begin
        if (i<ToolsListBox.Items.Count) then begin
          ToolsListBox.Items[i]:=NewTool.Caption;
        end;
      end;
    end;
  end;
end;

procedure TTextConvListEditor.SelectTool(Tool: TCustomTextConverterTool);
var
  i: LongInt;
begin
  if FListOfTools=nil then exit;
  if Tool.Owner<>FListOfTools then exit;
  i:=Tool.ComponentIndex;
  if (i<=0) or (i>=ToolsListBox.Items.Count) then exit;
  ToolsListBox.ItemIndex:=i;
end;

function TTextConvListEditor.CreateTool(ToolClass: TCustomTextConverterToolClass
  ): TCustomTextConverterTool;
begin
  Result:=nil;
  if FListOfTools=nil then exit;
  Result:=AddNewTextConverterTool(FListOfTools,ToolClass);
  Modified:=true;
  UpdateToolsListBox;
  SelectTool(Result);
end;

end.