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
|
unit selection_history_manager;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, DBCtrls, Controls, selection_history_dialog, types_for_app,
md5, db;
type
{Менеджер буфера обмена}
{ TSelectionHistoryManager }
TSelectionHistoryManager = class
{Буфер обмена}
SelectionHistory : sdict;
{Конструктор}
constructor Create();
{Деструктор}
destructor Destroy();
{Вырезает выделение}
procedure Cut(var t : TControl);
{Вставляет выделение}
procedure Paste(var t : TControl);
end;
implementation
{ TSelectionManager }
constructor TSelectionHistoryManager.Create();
begin
SelectionHistory := sdict.Create()
end;
destructor TSelectionHistoryManager.Destroy();
begin
SelectionHistory.Free();
end;
procedure TSelectionHistoryManager.Cut(var t: TControl);
var keyBuf : String;
begin
// Set editing state in control
if (t as tdbMemo).DataSource.State <> dsEdit then
(t as tdbMemo).DataSource.Edit;
// Get the selected text from the TDBMemo
if Length((t as TDbMemo).SelText) > 0 then
begin
// Add the selected text to the TStringList
keyBuf:=MD5Print(MD5String((t as TDbMemo).SelText)); // compute key as md5
SelectionHistory.AddOrSetData(keyBuf, (t as TDbMemo).SelText); // upsert selected
// Cutting
(t as TDbMemo).SelText := '';
end;
end;
procedure TSelectionHistoryManager.Paste(var t: TControl);
var
dialog : TfrmSelectionHistory; // dialog from selection_history_dialog
key : String; // key for selection history map
cnt : Integer; // count of items in history
index : Integer; // current index for traverse Keys array
rec : TBufferHistoryRecord; // store key and value
prec : PBufferHistoryRecord; // pointer to object
Line, CharIndex : Integer; // current line and char in tdbmemo
begin
// set editing mode
if (t as tdbMemo).DataSource.State <> dsEdit then
(t as tdbMemo).DataSource.Edit;
dialog := TfrmSelectionHistory.Create(nil); // creates dialog
cnt := SelectionHistory.Count; // get count of items
for index:=0 to cnt - 1 do begin
key := SelectionHistory.Keys[index]; // get key
rec := TBufferHistoryRecord.Create(); // create object for storing info
rec.key_buf:=key; // save key
rec.value:= SelectionHistory.KeyData[key]; // save value
prec := @rec; // give address of object
dialog.lboSelectionHistory.AddItem(rec.value, TObject(prec)); // add item
end;
dialog.ShowModal; // show dialog
if dialog.ModalResult = mrOK then
begin
// Get the line number from the caret position (0-based)
Line := (t as TDBMemo).CaretPos.Y;
// Get the character index of the start of the line
CharIndex := (t as TDBMemo).CaretPos.X;
// Insert text
(t as TDBMemo).Lines[Line] :=
Copy((t as TDBMemo).Lines[Line], 1, CharIndex) +
dialog.InsertedText +
Copy((t as TDBMemo).Lines[Line], CharIndex + 1,
Length((t as TDBMemo).Lines[Line]) - CharIndex);
{ #todo : Add moving caret }
end;
dialog.Free;
end;
end.
|