Files
lazarus-ccr/components/industrialstuff/source/indlcddisplay_editor.pas

213 lines
4.9 KiB
ObjectPascal

unit indLCDDisplay_Editor;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, PropEdits, ComponentEditors,
indLCDDisplay;
type
TLCDDisplayCharDefsPropertyEditor = class(TPersistentPropertyEditor)
public
procedure Edit; override;
procedure ExecuteVerb(Index: Integer); override;
function GetAttributes: TPropertyAttributes; override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
function LCDDisplay: TLCDDisplay;
end;
TLCDDisplayComponentEditor = class(TComponentEditor)
private
procedure EditLines;
public
procedure Edit; override;
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
function LCDDisplay: TLCDDisplay;
end;
procedure EditCharDefs(ALCDDisplay: TLCDDisplay);
implementation
uses
Controls, StdCtrls, Dialogs, ButtonPanel, Forms,
indLCDDisplay_EditorForm;
{ Opens the char def editor. }
procedure EditCharDefs(ALCDDisplay: TLCDDisplay);
var
F: TLCDCharDefsEditor;
begin
F := TLCDCharDefsEditor.Create(nil);
try
F.Position := poScreenCenter;
F.LCDDisplay := TLCDDisplay(ALCDDisplay);
F.ShowModal; // Cancel has been handled by the editor form.
finally
F.Free;
end;
end;
{ Loads the char defs of the specified LCDDisplay from an xml file. }
procedure LoadCharDefsFromFile(ALCDDisplay: TLCDDisplay);
var
dlg: TOpenDialog;
begin
dlg := TOpenDialog.Create(nil);
try
dlg.FileName := '';
dlg.Filter := 'XML files (*.xml)|*.xml';
if dlg.Execute then
begin
ALCDDisplay.CharDefs.LoadFromFile(dlg.FileName);
ALCDDisplay.Invalidate;
end;
finally
dlg.Free;
end;
end;
{ Saves the chardefs of the specified LCDDisplay to an xml file. }
procedure SaveCharDefsToFile(ALCDDisplay: TLCDDisplay);
var
dlg: TOpenDialog;
begin
dlg := TSaveDialog.Create(nil);
try
dlg.FileName := '';
dlg.Filter := 'XML files (*.xml)|*.xml';
if dlg.Execute then
ALCDDisplay.CharDefs.SaveToFile(dlg.FileName);
finally
dlg.Free;
end;
end;
{ TLCDDisplayCharDefsPropertyEditor }
{ Opens the chardefs editor. }
procedure TLCDDisplayCharDefsPropertyEditor.Edit;
begin
EditCharDefs(LCDDisplay);
end;
{ Executes the routines assigned to the CharDefs context menu }
procedure TLCDDisplayCharDefsPropertyEditor.ExecuteVerb(Index: Integer);
begin
case Index of
0: Edit;
1: LoadCharDefsFromFile(LCDDisplay);
2: SaveCharDefsToFile(LCDDisplay);
end;
end;
{ The property editor should open the CharDefs editor. }
function TLCDDisplayCharDefsPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := inherited GetAttributes + [paDialog];
end;
{ Determines how many items will be added to the CharDefs context menu. }
function TLCDDisplayCharDefsPropertyEditor.GetVerbCount: Integer;
begin
Result := 3;
end;
{ Determines the menu item text for CharDefs context menu. }
function TLCDDisplayCharDefsPropertyEditor.GetVerb(Index: Integer): string;
begin
case Index of
0: Result := 'Edit...';
1: Result := 'Load from file...';
2: Result := 'Save to file...';
end;
end;
function TLCDDisplayCharDefsPropertyEditor.LCDDisplay: TLCDDisplay;
begin
Result := TLCDDisplay(GetComponent(0));
end;
{ TLCDDisplayComponentEditor }
procedure TLCDDisplayComponentEditor.Edit;
begin
ExecuteVerb(0);
end;
procedure TLCDDisplayComponentEditor.EditLines;
var
F: TForm;
Memo: TMemo;
begin
F := TForm.CreateNew(nil);
try
F.Caption := 'Edit LCDDisplay text';
F.Position := poScreenCenter;
F.Width := 300;
F.Height := 200;
Memo := TMemo.Create(F);
with Memo do
begin
Align := alClient;
BorderSpacing.Around := 8;
Parent := F;
Lines.Assign(LCDDisplay.Lines);
end;
with TButtonPanel.Create(F) do
begin
ShowButtons := [pbOK, pbCancel];
Parent := F;
end;
if F.ShowModal = mrOK then
begin
LCDDisplay.Lines.Assign(Memo.Lines);
LCDDisplay.Invalidate;
end;
finally
F.Free;
end;
end;
procedure TLCDDisplayComponentEditor.ExecuteVerb(Index: Integer);
begin
case Index of
0: EditLines;
1: EditCharDefs(LCDDisplay);
2: LoadCharDefsFromFile(LCDDisplay);
3: SaveCharDefsToFile(LCDDisplay);
end;
end;
{ Determines how many items will be added to the LCDDisplay context menu. }
function TLCDDisplayComponentEditor.GetVerbCount: Integer;
begin
Result := 4;
end;
{ Determines the menu item text for LCDDisplay context menu. }
function TLCDDisplayComponentEditor.GetVerb(Index: Integer): string;
begin
case Index of
0: Result := 'Lines text...';
1: Result := 'Edit character defs...';
2: Result := 'Load character defs from file...';
3: Result := 'Save character defs to file...';
end;
end;
function TLCDDisplayComponentEditor.LCDDisplay: TLCDDisplay;
begin
Result := TLCDDisplay(GetComponent);
end;
end.