File: lazsyncocoaimm.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (315 lines) | stat: -rw-r--r-- 9,875 bytes parent folder | download
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
{
  SynEdit MacOS IME Handler:
  1. various IME are fully supported, such as Chinese/Japanese/Korean and DeadKeys
  2. MultiCarets supported
  3. GroupUndo or not are both fully supported

  in order to be compatible with MultiCarets:
  1. TSynEditUndoList.BeginBlock() cannot be used directly,
     only TSynEditUndoList.GroupUndo and TSynEditUndoList.ForceGroupEnd()
     can be combined to Undo
  2. InsertTextAtCaret() cannot be used directly, only ecChar Command can be used
  3. mabye the support for MultiSelections in MultiCaretsPlugin is not perfect.
     for example, Shift+Arrow can only expand the Selection of the Main Caret,
     but not other Carets

  macOS Lookup Word supported:
  1. implement ICocoaLookupWord in LazSynImeCocoa
  2. ICocoaLookupWord and ICocoaIMEControl can be implemented in different classes.
     considering that LazSynImeCocoa is relatively simple, it is appropriate to
     implement both interfaces in LazSynImeCocoa.
}

unit LazSynCocoaIMM;

{$mode objfpc}{$H+}
{$interfaces corba}

interface

uses
  Classes, SysUtils,
  Graphics,
  CocoaFullControlEdit,
  SynEditMiscClasses, LazSynIMMBase, SynEditKeyCmds, SynEditTextBase;

type
  { LazSynImeCocoa }

  LazSynImeCocoa = class( LazSynIme, ICocoaIMEControl, ICocoaLookupWord )
  private
    _undoList: TSynEditUndoList;
    _IntermediateTextBeginPos: TPoint;
  public
    procedure IMESessionBegin;
    procedure IMESessionEnd;
    procedure IMEUpdateIntermediateText( var params: TCocoaIMEParameters );
    procedure IMEInsertFinalText( var params: TCocoaIMEParameters );
    function  IMEGetTextBound( var params: TCocoaIMEParameters ): TRect;
  public
    procedure LWRowColForScreenPoint( var params: TCocoaLWParameters;
      const screenPoint: TPoint );
    procedure LWLineForRow( var params: TCocoaLWParameters );
    function  LWGetTextBound( var params: TCocoaLWParameters ): TRect;
    function  LWGetFont( var params: TCocoaLWParameters ): TFont;
  private
    procedure InsertTextAtCaret_CompatibleWithMultiCarets( var params: TCocoaIMEParameters );
    procedure SelectText_CompatibleWithMultiCarets( var params: TCocoaIMEParameters );
    function calcBound( var params: TCocoaIMEParameters ): TRect;
    function PosToPixels( const pos: TPoint ): TPoint;
  public
    constructor Create(AOwner: TSynEditBase);
    destructor Destroy; override;
  end;

implementation

uses
  LCLType, LazUTF8, SynEdit, LazSynEditText;

procedure LazSynImeCocoa.IMESessionBegin;
begin
  if FriendEdit.ReadOnly then exit;
  DoIMEStarted;
end;

procedure LazSynImeCocoa.IMESessionEnd;
begin
  if FriendEdit.ReadOnly then exit;
  ViewedTextBuffer.RedoList.Clear;    // clear Intermediate Text redo items
  DoIMEEnded;
end;

{
  update IME Intermediate Text, Key function for IME:
  1. some IME do not have a popup window and rely on the Editor
     to display the Intermediate Text
  2. use selection to simulate Intermediate Text display in this implementation,
     may be better to use TSynEditMarkup (slightly complicated)
  3. it means completely cancel the IME session if Intermediate Text is empty
  4. it's First call of IMEUpdateIntermediateText or IMEInsertFinalText
     if isFirstCall=True
  5. eat some chars if eatAmount>0 (such as DeadKeys)
}
procedure LazSynImeCocoa.IMEUpdateIntermediateText( var params: TCocoaIMEParameters );
var
  groupUndoBefore: boolean;
begin
  if FriendEdit.ReadOnly
    then exit;

  // clear last Intermediate Text
  if not params.isFirstCall then
    FriendEdit.Undo;

  // length=0 means to completely cancel the IME session
  if params.textCharLength=0 then
    exit;

  // save caret pos
  _IntermediateTextBeginPos := FriendEdit.LogicalCaretXY;

  // in order to be compatible with MultiCarets,
  // TSynEditUndoList.BeginBlock() cannot be used directly,
  // only TSynEditUndoList.GroupUndo and TSynEditUndoList.ForceGroupEnd()
  // can be combined to Undo
  groupUndoBefore:= _undoList.GroupUndo;
  _undoList.GroupUndo:= true;
  _undoList.ForceGroupEnd;

  // need to eat some chars, such as DeadKeys
  if params.eatAmount<>0 then
    TSynEdit(FriendEdit).CommandProcessor(ecDeleteLastChar, #0, nil);

  // in order to be compatible with MultiCarets,
  // InsertTextAtCaret() cannot be used directly,
  // only ecChar Command can be used indirectly
  InsertTextAtCaret_CompatibleWithMultiCarets( params );
  SelectText_CompatibleWithMultiCarets( params );

  _undoList.GroupUndo:= groupUndoBefore;
end;

{
  insert IME Final Text, Key function for IME:
  1. called only when inputting via IME, otherwise handled by UTF8KeyPress()
  2. when the IME input is finished, either IMEUpdateIntermediateText(with empty text)
     is called, or IMEInsertFinalText(with final text) is called,
     NOT the both
  3. it's First call of IMEUpdateIntermediateText or IMEInsertFinalText
     if isFirstCall=True
  4. eat some chars if eatAmount>0 (such as DeadKeys)
}
procedure LazSynImeCocoa.IMEInsertFinalText( var params: TCocoaIMEParameters );
begin
  if FriendEdit.ReadOnly then exit;

  // clear Intermediate Text
  if not params.isFirstCall then
    FriendEdit.Undo;

  // need to eat some chars, such as DeadKeys
  if params.eatAmount<>0 then
    TSynEdit(FriendEdit).CommandProcessor( ecDeleteLastChar, #0, nil );

  InsertTextAtCaret_CompatibleWithMultiCarets( params );
end;

{
  calc Intermediate Text bound:
  1. return Intermediate Text bound when in IME inut state. it's possible
     to only get the bound of the Intermediate Text in a subrange
     (selectedStart and selectedLength)
  2. return caret pos when not in IME input state
  3. in Screen Pixels
}
function LazSynImeCocoa.IMEGetTextBound( var params: TCocoaIMEParameters ) : TRect;
begin
  if not params.isFirstCall then
    Result:= calcBound( params )
  else
    Result:= TRect.Create( Point(FriendEdit.CaretXPix,FriendEdit.CaretYPix), 0, FriendEdit.LineHeight );

  Result:= FriendEdit.ClientToScreen( Result );
end;

procedure LazSynImeCocoa.LWRowColForScreenPoint(
  var params: TCocoaLWParameters; const screenPoint: TPoint);
var
  localPoint: TPoint;
  logicalPoint: TPoint;
  lineText: String;
begin
  localPoint:= FriendEdit.ScreenToClient( screenPoint );
  logicalPoint:= TSynEdit(FriendEdit).PixelsToLogicalPos( localPoint );
  params.row:= logicalPoint.Y;
  if params.row > 0 then
    params.row:= params.row - 1;
  lineText:= FriendEdit.Lines[params.row];
  if (lineText.length>0) and (logicalPoint.x<=lineText.length+1) then begin
    if logicalPoint.x > lineText.length then
      logicalPoint.x:= lineText.length;
    params.col:= UTF8CodepointCount( pchar(lineText), logicalPoint.x ) - 1;
  end else begin
    params.col:= -1;
  end;
end;

procedure LazSynImeCocoa.LWLineForRow( var params: TCocoaLWParameters );
begin
  params.text:= FriendEdit.Lines[params.row];
end;

function LazSynImeCocoa.LWGetTextBound( var params: TCocoaLWParameters
  ): TRect;
var
  p1: TPoint;
  p2: TPoint;
  lineText: String;
  col1Bytes: PtrInt;
  col2Bytes: PtrInt;
begin
  lineText:= FriendEdit.Lines[params.row];


  col1Bytes:= UTF8CodepointToByteIndex( pchar(lineText),
                lineText.Length, params.col );
  col2Bytes:= col1Bytes + UTF8CodepointToByteIndex( pchar(lineText)+col1Bytes,
                lineText.Length-col1Bytes, params.length );
  // two vertexs in bytes
  p1:= Point( col1Bytes + 1, params.row + 1 );
  p2:= Point( col2Bytes + 1, params.row + 1 );

  // two vertexs in pixels
  p1:= PosToPixels( p1 );
  p2:= PosToPixels( p2 );
  p2.Y:= p2.Y + FriendEdit.LineHeight - FriendEdit.ExtraLineSpacing;

  // client rect in pixels
  Result:= TRect.Create( p1 , p2 );
  Result:= FriendEdit.ClientToScreen( Result );
end;

function LazSynImeCocoa.LWGetFont( var params: TCocoaLWParameters ): TFont;
begin
  Result:= FriendEdit.Font;
end;

procedure LazSynImeCocoa.InsertTextAtCaret_CompatibleWithMultiCarets( var params: TCocoaIMEParameters );
var
  i: integer;
  c: integer;
  ch: TUTF8Char;
begin
  if params.textByteLength=0 then Exit;
  i:=1;
  while( i<=params.textByteLength ) do
  begin
    c:= Utf8CodePointLen( @params.text[i], params.textByteLength-i+1, false );
    ch:= Copy( params.text, i, c );
    TSynEdit(FriendEdit).CommandProcessor( ecChar, ch, nil );
    inc( i, c );
  end;
end;

procedure LazSynImeCocoa.SelectText_CompatibleWithMultiCarets( var params: TCocoaIMEParameters );
var
  i: Integer;
  start:  Integer;
  length: Integer;
begin
  if params.selectedLength=0 then begin
    start:= 0;
    length:= params.textCharLength;
  end else begin
    start:= params.selectedStart;
    length:= params.selectedLength;
  end;

  for i:=params.textCharLength-start downto 1 do TSynEdit(FriendEdit).CommandProcessor( ecLeft, #0, nil );
  for i:=length downto 1 do TSynEdit(FriendEdit).CommandProcessor( ecSelRight, #0, nil );
end;

function LazSynImeCocoa.calcBound( var params: TCocoaIMEParameters ) : TRect;
var
  p1: TPoint;
  p2: TPoint;
begin
  // two vertexs in bytes
  p1:= _IntermediateTextBeginPos;
  p2:= p1;
  p1.X:= p1.X + UTF8CodepointToByteIndex( pchar(@params.text[1]),
                  params.textByteLength, params.selectedStart );
  p2.X:= p1.X + UTF8CodepointToByteIndex( pchar(@params.text[1])+p1.X,
                  params.textByteLength-p1.X, params.selectedLength );

  // two vertexs in pixels
  p1:= PosToPixels( p1 );
  p2:= PosToPixels( p2 );
  p2.Y:= p2.Y + FriendEdit.LineHeight;

  // client rect in pixels
  Result:= TRect.Create( p1 , p2 );
end;

function LazSynImeCocoa.PosToPixels( const pos: TPoint ) : TPoint;
begin
  Result:= FriendEdit.LogicalToPhysicalPos( pos );
  Result:= FriendEdit.TextXYToScreenXY( Result );
  Result:= TSynEdit(FriendEdit).ScreenXYToPixels( Result );
end;

constructor LazSynImeCocoa.Create( AOwner: TSynEditBase );
begin
  Inherited;
  _undoList:= ViewedTextBuffer.UndoList;
end;

destructor LazSynImeCocoa.Destroy;
begin
  _undoList:= nil;
  Inherited;
end;

end.