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

193 lines
5.0 KiB
ObjectPascal
Raw Normal View History

unit indLCDDisplay_EditorForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ButtonPanel,
Grids, indLCDDisplay;
type
{ TLCDCharDefsEditor }
TLCDCharDefsEditor = class(TForm)
btnDelete: TButton;
ButtonPanel: TButtonPanel;
cbCharSelector: TComboBox;
dgEditor: TDrawGrid;
Label1: TLabel;
procedure btnDeleteClick(Sender: TObject);
procedure cbCharSelectorChange(Sender: TObject);
procedure cbCharSelectorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure cbCharSelectorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure dgEditorPrepareCanvas(sender: TObject; aCol, aRow: Integer;
aState: TGridDrawState);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
private
FLCDDisplay: TLCDDisplay;
FSavedCharDefs: TCharDefs;
procedure SetLCDDisplay(AValue: TLCDDisplay);
procedure PopulateCharSelector;
procedure SaveCharDefs;
function SelectedChar: String;
function SelectedDotMatrix: TDotRows;
procedure SetupEditorGrid;
public
property LCDDisplay: TLCDDisplay read FLCDDisplay write SetLCDDisplay;
end;
var
LCDCharDefsEditor: TLCDCharDefsEditor;
implementation
{$R *.lfm}
uses
Math;
procedure TLCDCharDefsEditor.PopulateCharSelector;
var
i: Integer;
begin
cbCharSelector.DropdownCount := 24;
cbCharSelector.Items.BeginUpdate;
try
cbCharSelector.Clear;
for i := 0 to FLCDDisplay.CharDefs.Count-1 do
cbCharSelector.Items.Add(FLCDDisplay.CharDefs.CharByIndex[i]);
finally
cbCharSelector.Items.EndUpdate;
end;
end;
{ This is just a sample allowing me to test the property editor... }
procedure TLCDCharDefsEditor.btnDeleteClick(Sender: TObject);
var
ch: String;
begin
ch := SelectedChar;
if ch <> '' then
begin
FLCDDisplay.CharDefs.Delete(ch);
FLCDDisplay.Invalidate;
end;
end;
procedure TLCDCharDefsEditor.cbCharSelectorChange(Sender: TObject);
begin
dgEditor.Invalidate;
end;
procedure TLCDCharDefsEditor.cbCharSelectorKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
// cbCharSelector.Text := '';
end;
procedure TLCDCharDefsEditor.cbCharSelectorKeyUp(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
cbCharSelector.SelectAll;
end;
procedure TLCDCharDefsEditor.dgEditorPrepareCanvas(sender: TObject;
aCol, aRow: Integer; aState: TGridDrawState);
var
dotrows: TDotRows;
r, c: Integer;
dotSet: Boolean;
begin
dotRows := SelectedDotMatrix;
if dotRows = nil then
exit;
r := ARow;
c := dgEditor.ColCount - 1 - ACol;
dotSet := dotRows[r] and (1 shl c) <> 0;
if dotSet then
dgEditor.Canvas.Brush.Color := clBlack;
end;
procedure TLCDCharDefsEditor.FormActivate(Sender: TObject);
begin
Constraints.MinHeight := dgEditor.Top + dgEditor.Height + ButtonPanel.Height + 2*ButtonPanel.BorderSpacing.Around;
Constraints.MinWidth := Max(
ButtonPanel.OKButton.Left + ButtonPanel.OKButton.Width + ButtonPanel.BorderSpacing.Around,
btnDelete.Left + btnDelete.Width + btnDelete.BorderSpacing.Right
);
end;
{ When the form is not closed by the OK button, we must restored the saved,
original char defs of the FLCDDisplay. }
procedure TLCDCharDefsEditor.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if CanClose and (ModalResult <> mrOK) then
begin
FLCDDisplay.CharDefs.Assign(FSavedCharDefs);
FLCDDisplay.Invalidate;
end;
end;
procedure TLCDCharDefsEditor.FormDestroy(Sender: TObject);
begin
FreeAndNil(FSavedCharDefs);
end;
{ Save the char defs so that they can be restored if the form is not closed by OK. }
procedure TLCDCharDefsEditor.SaveCharDefs;
begin
FSavedCharDefs.Free;
FSavedCharDefs := TCharDefs.Create(nil);
FSavedCharDefs.Assign(FLCDDisplay.CharDefs);
end;
{ Returns the currently selected character. Note that this is a string because
we have to deal with UTF8 where a code-point can consist of up to 4 bytes. }
function TLCDCharDefsEditor.SelectedChar: String;
begin
Result := cbCharSelector.Text;
end;
function TLCDCharDefsEditor.SelectedDotMatrix: TDotRows;
var
ch: String;
begin
ch := SelectedChar;
if ch <> '' then
Result := FLCDDisplay.CharDefs.DotRows[ch]
else
Result := nil;
end;
procedure TLCDCharDefsEditor.SetLCDDisplay(AValue: TLCDDisplay);
begin
FLCDDisplay := AValue;
SaveCharDefs;
PopulateCharSelector;
SetupEditorGrid;
end;
{ Reads the size of the dot matrix from FLCDDisplay and use it to define the
number of rows and columns in the editor grid. }
procedure TLCDCharDefsEditor.SetupEditorGrid;
begin
dgEditor.RowCount := FLCDDisplay.DotRowCount;
dgEditor.ColCount := FLCDDisplay.DotColCount;
dgEditor.ClientWidth := dgEditor.ColCount * dgEditor.DefaultColWidth;
dgEditor.ClientHeight := dgEditor.RowCount * dgEditor.DefaultRowHeight;
dgEditor.Constraints.MinWidth := dgEditor.Width;
dgEditor.Constraints.MinHeight := dgEditor.Height;
end;
end.