-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbclealcddisplay_editorform.pas
370 lines (333 loc) · 10.4 KB
/
bclealcddisplay_editorform.pas
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
364
365
366
367
368
369
370
unit BCLeaLCDDisplay_EditorForm;
{$mode objfpc}{$H+}
interface
uses
LCLIntf, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
Grids, ExtCtrls, Buttons, BCLeaLCDDisplay;
type
{ TBCLeaLCDCharDefsEditor }
TBCLeaLCDCharDefsEditor = class(TForm)
btAdd: TBitBtn;
btReplace: TBitBtn;
btDelete: TBitBtn;
btOK: TBitBtn;
btCancel: TBitBtn;
cbCharSelector: TComboBox;
dgDotMatrix: TDrawGrid;
ImageList1: TImageList;
Label1: TLabel;
pnButtons: TPanel;
pnOKCancel: TPanel;
procedure btAddClick(Sender: TObject);
procedure btDeleteClick(Sender: TObject);
procedure btReplaceClick(Sender: TObject);
procedure cbCharSelectorChange(Sender: TObject);
procedure dgDotMatrixKeyDown(Sender: TObject; var Key: word;
{%H-}Shift: TShiftState);
procedure dgDotMatrixMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
{%H-}Shift: TShiftState; X, Y: integer);
procedure dgDotMatrixMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure dgDotMatrixPrepareCanvas({%H-}Sender: TObject; aCol, aRow: integer;
{%H-}aState: TGridDrawState);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormDestroy(Sender: TObject);
private
FBCLeaLCDDisplay: TBCLeaLCDDisplay;
FModified: boolean;
FSavedCharDefs: TBCLeaCharDefs;
FSelectedChar: string;
FTmpDotRows: TDotRows;
FOldRow, FOldCol: integer;
procedure SetBCLeaLCDDisplay(AValue: TBCLeaLCDDisplay);
procedure PopulateCharSelector;
procedure SaveCharDefs;
procedure ClearEditorGrid;
procedure SetupEditorGrid;
function GetDotMatrix: TDotRows;
function DotSet(ACol, ARow: integer): boolean;
procedure SetDot(ACol, ARow: integer; AValue: boolean);
procedure ToggleDot(ACol, ARow: integer);
public
property BCLeaLCDDisplay: TBCLeaLCDDisplay read FBCLeaLCDDisplay write SetBCLeaLCDDisplay;
end;
var
BCLeaLCDCharDefsEditor: TBCLeaLCDCharDefsEditor;
implementation
{$R *.lfm}
uses
Math, ButtonPanel;
function CharInputBox(const ACaption, APrompt, ADefault: string): string;
var
F: TForm;
ed: TEdit;
lbl: TLabel;
bp: TButtonPanel;
begin
F := TForm.CreateNew(nil);
try
F.Caption := ACaption;
F.BorderStyle := bsDialog;
lbl := TLabel.Create(F);
lbl.AnchorSideTop.Control := F;
lbl.AnchorSideLeft.Control := F;
lbl.BorderSpacing.Top := F.Scale96ToFont(12);
lbl.BorderSpacing.Bottom := F.Scale96ToFont(2);
lbl.BorderSpacing.Left := F.Scale96ToFont(12);
lbl.BorderSpacing.Right := F.Scale96ToFont(12);
lbl.WordWrap := True;
lbl.AutoSize := True;
lbl.Caption := APrompt;
lbl.Parent := F;
lbl.AdjustSize;
ed := TEdit.Create(F);
ed.AnchorSideTop.Control := lbl;
ed.AnchorSideTop.Side := asrBottom;
ed.AnchorSideLeft.Control := F;
ed.AnchorSideRight.Control := F;
ed.AnchorSideRight.Side := asrRight;
ed.Anchors := [akLeft, akTop, akRight];
ed.BorderSpacing.Left := F.Scale96ToFont(12);
ed.BorderSpacing.Right := F.Scale96ToFont(12);
ed.BorderSpacing.Bottom := F.Scale96ToFont(18);
ed.MaxLength := 1;
ed.Text := ADefault;
ed.Parent := F;
ed.AdjustSize;
bp := TButtonPanel.Create(F);
bp.ShowButtons := [pbOK, pbCancel];
bp.Parent := F;
bp.AdjustSize;
F.Constraints.MinHeight := ed.Top + ed.Height + ed.BorderSpacing.Bottom + bp.Height + 2 * bp.BorderSpacing.Around;
F.AutoSize := True;
F.Position := poScreenCenter;
if F.ShowModal = mrOk then
Result := ed.Text
else
Result := ADefault;
finally
F.Free;
end;
end;
procedure TBCLeaLCDCharDefsEditor.PopulateCharSelector;
var
i: integer;
begin
cbCharSelector.DropdownCount := 24;
cbCharSelector.Items.BeginUpdate;
try
cbCharSelector.Clear;
for i := 0 to FBCLeaLCDDisplay.CharDefs.Count - 1 do
cbCharSelector.Items.Add(FBCLeaLCDDisplay.CharDefs.CharByIndex[i]);
finally
cbCharSelector.Items.EndUpdate;
end;
end;
procedure TBCLeaLCDCharDefsEditor.btDeleteClick(Sender: TObject);
begin
if FSelectedChar <> '' then
begin
FBCLeaLCDDisplay.CharDefs.Delete(FSelectedChar);
FBCLeaLCDDisplay.Invalidate;
end;
PopulateCharSelector;
end;
procedure TBCLeaLCDCharDefsEditor.btAddClick(Sender: TObject);
var
newChar: string;
begin
newChar := CharInputBox('Dot matrix for...', 'Character', '');
if newChar = '' then
exit;
// Check whether the new character already has a dot matrix.
if FBCLeaLCDDisplay.CharDefs.Find(newChar) then
begin
MessageDlg(Format('Character "%s" already exists and cannot be added.', [newChar]),
mtError, [mbOK], 0);
exit;
end;
// Add new character and its dot matrix to the BCLeaLCDDisplay...
FSelectedChar := newChar;
FBCLeaLCDDisplay.CharDefs.Add(FSelectedChar, GetDotMatrix);
FBCLeaLCDDisplay.Invalidate;
// ... and update the editor form
PopulateCharSelector;
cbCharSelector.ItemIndex := cbCharSelector.Items.IndexOf(FSelectedChar);
FModified := False;
end;
{ Replaces the dotmatrix of the currently loaded character by the dotmatrix in
the editor. }
procedure TBCLeaLCDCharDefsEditor.btReplaceClick(Sender: TObject);
begin
if FSelectedChar <> '' then
begin
FBCLeaLCDDisplay.CharDefs.DotRows[FSelectedChar] := GetDotMatrix;
FBCLeaLCDDisplay.Invalidate;
FModified := False;
end;
end;
procedure TBCLeaLCDCharDefsEditor.cbCharSelectorChange(Sender: TObject);
begin
FSelectedChar := cbCharSelector.Text;
if FSelectedChar <> '' then
FTmpDotRows := FBCLeaLCDDisplay.CharDefs.DotRows[FSelectedChar]
else
ClearEditorGrid;
dgDotMatrix.Invalidate;
FModified := False;
end;
procedure TBCLeaLCDCharDefsEditor.dgDotMatrixKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
var
r, c: integer;
begin
r := dgDotMatrix.Row;
c := dgDotMatrix.Col;
if Key = 32 then
begin
ToggleDot(c, r);
dgDotMatrix.InvalidateCell(c, r);
FModified := True;
end;
end;
procedure TBCLeaLCDCharDefsEditor.dgDotMatrixMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
var
r, c: integer;
begin
dgDotMatrix.MouseToCell(X, Y, c, r);
ToggleDot(c, r);
dgDotMatrix.InvalidateCell(c, r);
FOldRow := r;
FOldCol := c;
FModified := True;
end;
procedure TBCLeaLCDCharDefsEditor.dgDotMatrixMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
var
r, c: integer;
begin
if Shift = [ssLeft] then
begin
dgDotMatrix.MouseToCell(X, Y, c, r);
if (c <> FOldCol) or (r <> FOldRow) then
begin
ToggleDot(c, r);
dgDotMatrix.InvalidateCell(c, r);
FOldRow := r;
FOldCol := c;
FModified := True;
end;
end;
end;
procedure TBCLeaLCDCharDefsEditor.dgDotMatrixPrepareCanvas(Sender: TObject; aCol, aRow: integer; aState: TGridDrawState);
begin
if DotSet(ACol, ARow) then
dgDotMatrix.Canvas.Brush.Color := clBlack
else
dgDotMatrix.Canvas.Brush.Color := clWhite;
end;
procedure TBCLeaLCDCharDefsEditor.FormActivate(Sender: TObject);
var
w: integer;
begin
w := Max(btOK.Width, btCancel.Width);
btOK.Constraints.MinWidth := w;
btCancel.Constraints.MinWidth := w;
end;
procedure TBCLeaLCDCharDefsEditor.FormCloseQuery(Sender: TObject; var CanClose: boolean);
const
mrReplace = -20;
mrAddAs = -21;
var
res: integer;
begin
if FModified and (ModalResult = mrOk) then
begin
res := QuestionDlg('Confirmation', 'Current dotmatrix of "' + FSelectedChar + '" has not yet been applied. What do you want to do?',
mtConfirmation, [mrReplace, 'Replace', mrAddAs, 'Add as...', 'isDefault', mrCancel, 'Cancel'], 0);
case res of
mrReplace:
btReplaceClick(nil);
mrAddAs:
btAddClick(nil);
mrCancel:
;
end;
CanClose := (res <> mrCancel) and (not FModified);
end;
if CanClose and (ModalResult <> mrOk) then
begin
FBCLeaLCDDisplay.CharDefs.Assign(FSavedCharDefs);
FBCLeaLCDDisplay.Invalidate;
end;
end;
procedure TBCLeaLCDCharDefsEditor.FormDestroy(Sender: TObject);
begin
FreeAndNil(FSavedCharDefs);
end;
function TBCLeaLCDCharDefsEditor.GetDotMatrix: TDotRows;
begin
Result := FTmpDotRows;
end;
{ Tests whether the dot corresponding to the specified grid column and row is
set. Note that the low-bit is at the right! }
function TBCLeaLCDCharDefsEditor.DotSet(ACol, ARow: integer): boolean;
var
c: integer;
begin
c := dgDotMatrix.ColCount - 1 - ACol;
Result := FTmpDotRows[ARow] and (1 shl c) <> 0; // avoid integer helper to keep usability with old fpc versions
end;
{ Sets the dot in the specified grid column and row if AValue is true,
or clears it if AValue is false.
Note that the low-bit is at the right of the grid! }
procedure TBCLeaLCDCharDefsEditor.SetDot(ACol, ARow: integer; AValue: boolean);
var
c: integer;
lDotRows: TDotRows;
begin
c := dgDotMatrix.ColCount - 1 - ACol;
lDotRows := CopyDotRows(FTmpDotRows);
if AValue then
lDotRows[ARow] := lDotRows[ARow] or (1 shl c)
else
lDotRows[ARow] := lDotRows[ARow] and not (1 shl c); // avoid integer helper to keep usability with old fpc version
FTmpDotRows := CopyDotRows(lDotRows);
end;
{ Toggles the dot in the specified grid column/row }
procedure TBCLeaLCDCharDefsEditor.ToggleDot(ACol, ARow: integer);
begin
SetDot(ACol, ARow, not DotSet(ACol, ARow));
end;
{ Save the char defs so that they can be restored if the form is not closed by OK. }
procedure TBCLeaLCDCharDefsEditor.SaveCharDefs;
begin
FSavedCharDefs.Free;
FSavedCharDefs := TBCLeaCharDefs.Create(nil);
FSavedCharDefs.Assign(FBCLeaLCDDisplay.CharDefs);
end;
procedure TBCLeaLCDCharDefsEditor.SetBCLeaLCDDisplay(AValue: TBCLeaLCDDisplay);
begin
FBCLeaLCDDisplay := AValue;
SetLength(FTmpDotRows, FBCLeaLCDDisplay.DotRowCount);
SaveCharDefs;
PopulateCharSelector;
SetupEditorGrid;
end;
procedure TBCLeaLCDCharDefsEditor.ClearEditorGrid;
var
i: integer;
begin
for i := 0 to High(FTmpDotRows) do
FTmpDotRows[i] := 0;
end;
{ Reads the size of the dot matrix from FBCLeaLCDDisplay and use it to define the
number of rows and columns in the editor grid. }
procedure TBCLeaLCDCharDefsEditor.SetupEditorGrid;
begin
dgDotMatrix.RowCount := FBCLeaLCDDisplay.DotRowCount;
ClearEditorGrid;
dgDotmatrix.ClientWidth := dgDotMatrix.ColCount * dgDotMatrix.DefaultColWidth;
dgDotMatrix.ClientHeight := dgDotMatrix.RowCount * dgDotMatrix.DefaultRowHeight;
dgDotMatrix.Constraints.MinWidth := dgDotMatrix.Width;
dgDotMatrix.Constraints.MinHeight := dgDotMatrix.Height;
end;
end.