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
371
372
|
{
fpGUI - Free Pascal GUI Library
Edit class declarations
Copyright (C) 2000 - 2006 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
{%mainunit fpgui.pp}
{$IFDEF read_interface}
{ TCustomEdit }
TCustomEdit = class(TWidget)
private
FFontColor: TColor;
FSelStart: integer;
FSelOffset: integer;
FCursorPos: Integer;
FPasswordChar: Char;
FOnChange: TNotifyEvent;
procedure SetFontColor(const AValue: TColor);
procedure SetPasswordChar(APasswordChar: Char);
procedure SetCursorPos(ACursorPos: Integer);
procedure DoMousePressed(pEvent: TMousePressedEventObj);
protected
procedure Paint(Canvas: TFCanvas); override;
function ProcessEvent(Event: TEventObj): Boolean; override;
procedure CalcSizes; override;
procedure EvKeyPressed(Key: Word; Shift: TShiftState); override;
procedure EvKeyChar(KeyChar: Char); override;
procedure EvTextChanged; override;
property CanExpandWidth default True;
property Cursor default crIBeam;
property PasswordChar: Char read FPasswordChar write SetPasswordChar default #0;
property CursorPos: Integer read FCursorPos write SetCursorPos;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property FontColor: TColor read FFontColor write SetFontColor;
procedure SetText(const AText: String); override;
public
constructor Create(AOwner: TComponent); override;
constructor Create(const pText: string; pOwner: TComponent); overload;
end;
TEdit = class(TCustomEdit)
published
property CanExpandWidth;
property Enabled;
property PasswordChar;
property Text;
property OnChange;
property FontColor;
end;
{$ENDIF read_interface}
{$IFDEF read_implementation}
// ===================================================================
// TCustomEdit
// ===================================================================
constructor TCustomEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
FCanExpandWidth := True;
FCursor := crIBeam;
FFontColor := clWindowText;
FCursorPos := 0;
end;
constructor TCustomEdit.Create(const pText: string; pOwner: TComponent);
begin
Create(pOwner);
Text := pText;
end;
procedure TCustomEdit.Paint(Canvas: TFCanvas);
var
Borders: TRect;
s: String;
x: Integer;
ItemRect: TRect;
ItemFlags: TItemFlags;
begin
ItemFlags := [];
Borders := Style.GetEditBoxBorders;
Style.DrawEditBox(Canvas, Rect(0, 0, BoundsSize.cx, BoundsSize.cy));
if not Canvas.IntersectClipRect(Rect(Borders.Left + 1, Borders.Top + 1,
BoundsSize.cx - Borders.Right - 1, BoundsSize.cy - Borders.Bottom - 1)) then
exit;
if wsEnabled in WidgetState then
Canvas.SetColor(Style.GetUIColor(FFontColor))
else
Canvas.SetColor(Style.GetUIColor(clGrayText));
if PasswordChar = #0 then
s := PChar(Text)
else
begin
SetLength(s, Length(Text));
if Length(Text) > 0 then
FillChar(s[1], Length(Text), PasswordChar);
end;
// drawing selection
if (FSelOffset <> 0) then
begin
if (wsHasFocus in WidgetState) and FindForm.IsActive then
begin
Include(ItemFlags, ifFocused);
Include(ItemFlags, ifSelected);
end;
ItemRect.Left := Canvas.TextWidth(Copy(s, 1, CursorPos - FSelOffset));
ItemRect.Top := 0;
ItemRect.Right := Canvas.TextWidth(Copy(s, 1, CursorPos));
ItemRect.Bottom := Height;
{
ItemRect := Rect(0, 0, Width, Height);
ItemRect.TopLeft := ItemRect.TopLeft + 1;
ItemRect.BottomRight := ItemRect.BottomRight - 2;
}
// InflateRect(ItemRect, -1, -1);
// Style.SetUIColor(Canvas, clWindowText);
{$Note Start here: Refine the selection rect}
Style.DrawItemBefore(Canvas, ItemRect, ItemFlags);
Style.DrawText(Canvas, (Borders.TopLeft + Point(1, 1)), s, WidgetState);
Style.DrawItemAfter(Canvas, ItemRect, ItemFlags);
end
else
Canvas.TextOut(Borders.TopLeft + Point(1, 1), s);
if wsHasFocus in WidgetState then
begin
Canvas.SetColor(Style.GetUIColor(clWindowText));
x := Borders.Left + 1 + Canvas.TextWidth(Copy(s, 1, CursorPos));
Canvas.DrawLine(Point(x, Borders.Top), Point(x, BoundsSize.cy - Borders.Bottom));
end;
end;
function TCustomEdit.ProcessEvent(Event: TEventObj): Boolean;
begin
if Event.InheritsFrom(TMousePressedEventObj) then
begin
// Result := True;
DoMousePressed(TMousePressedEventObj(Event));
// Writeln('>>> TMousePressedEventObj in ' + Classname);
end;
Result := inherited ProcessEvent(Event);
end;
procedure TCustomEdit.EvKeyPressed(Key: Word; Shift: TShiftState);
begin
if Shift * [ssShift, ssAlt, ssCtrl, ssMeta, ssSuper, ssHyper, ssAltGr] = [] then
begin
// Writeln('1');
case Key of
keyLeft, keyUp:
if CursorPos > 0 then
CursorPos := CursorPos - 1;
keyRight, keyDown:
if CursorPos < Length(Text) then
CursorPos := CursorPos + 1;
keyHome:
CursorPos := 0;
keyEnd:
CursorPos := Length(Text);
else
inherited EvKeyPressed(Key, Shift);
end;
end
{ else if Shift * [ssShift, ssAlt, ssCtrl, ssMeta, ssSuper, ssHyper, ssAltGr] = [ssShift] then
begin
Writeln('2');
case Key of
keyHome:
begin
FSelOffset := CursorPos;
CursorPos := 0;
end;
keyEnd:
begin
FSelOffset := CursorPos;
CursorPos := Length(Text);
end;
else
EvKeyPressed(Key, Shift);
end;
end
}
else
inherited EvKeyPressed(Key, Shift);
end;
procedure TCustomEdit.EvKeyChar(KeyChar: Char);
begin
case KeyChar of
#8: { Backspace }
if CursorPos > 0 then
begin
Text := Copy(Text, 1, CursorPos - 1) + Copy(Text, CursorPos + 1, Length(Text));
CursorPos := CursorPos - 1;
end;
#127: { Del }
if CursorPos < Length(Text) then
begin
Text := Copy(Text, 1, CursorPos) + Copy(Text, CursorPos + 2, Length(Text));
Redraw;
end;
#32..#126, #128..#255:
begin
Text := Copy(Text, 1, CursorPos) + KeyChar + Copy(Text, CursorPos + 1, Length(Text));
CursorPos := CursorPos + 1;
end;
else
inherited EvKeyChar(KeyChar);
end;
end;
procedure TCustomEdit.CalcSizes;
var
Borders: TRect;
begin
Borders := Style.GetEditBoxBorders;
FMinSize := gfxbase.Size(50, Borders.Top + Borders.Bottom +
FindForm.Wnd.Canvas.FontCellHeight + 2);
end;
procedure TCustomEdit.EvTextChanged;
begin
Redraw;
if Assigned(OnChange) then
OnChange(Self);
end;
procedure TCustomEdit.SetText(const AText: String);
begin
FSelOffset := 0;
inherited SetText(AText);
end;
procedure TCustomEdit.SetPasswordChar(APasswordChar: Char);
begin
if APasswordChar <> PasswordChar then
begin
FPasswordChar := APasswordChar;
Redraw;
end;
end;
procedure TCustomEdit.SetFontColor(const AValue: TColor);
begin
if FFontColor = AValue then exit;
FFontColor := AValue;
end;
procedure TCustomEdit.SetCursorPos(ACursorPos: Integer);
begin
if ACursorPos <> CursorPos then
begin
FCursorPos := ACursorPos;
Redraw;
end;
end;
procedure TCustomEdit.DoMousePressed(pEvent: TMousePressedEventObj);
var
Borders: TRect;
cp: integer;
cpx: integer;
lSideMargin: integer;
n: integer;
cx: integer;
lText: string;
// replicates pStrValue a set of pRepCount times.
function lReplicate(const pStrValue : string; pRepCount : Word) : string;
var
pResult, pValue: PChar;
lenValue: cardinal;
begin
if (pRepCount = 0) or (Pointer(pStrValue) = nil) then
exit;
lenValue := Length(pStrValue);
SetLength(Result, lenValue * pRepCount);
pResult := Pointer(Result);
pValue := Pointer(pStrValue);
while pRepCount <> 0 do
begin
Move(pValue^, pResult^, lenValue);
Inc(pResult, lenValue);
Dec(pRepCount);
end;
end;
begin
if (pEvent.Button = mbLeft) then
begin
// searching for the appropriate character position
Borders := Style.GetEditBoxBorders;
lSideMargin := Borders.Left + 1;
cp := CursorPos;
// Make sure we work with the correct displayed text
if FPasswordChar = #0 then
lText := Text
else
lText := lReplicate(FPasswordChar, Length(Text));
cpx := FindForm.Wnd.Canvas.TextWidth(Copy(lText, 1, CursorPos)) + lSideMargin;
for n := 0 to Length(Text) do
begin
cx := FindForm.Wnd.Canvas.TextWidth(Copy(lText, 1, n)) + lSideMargin;
if abs(cx - pEvent.Position.x) < abs(cpx - pEvent.Position.x) then
begin
cpx := cx;
cp := n;
end;
end;
FCursorPos := cp;
if (ssShift in pEvent.Shift) then
begin
FSelOffset := FCursorPos - FSelStart;
end
else
begin
FSelStart := cp;
FSelOffset := 0;
end;
end;
end;
{$ENDIF read_implementation}
|