File: lazsyngtk2imm.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 (85 lines) | stat: -rw-r--r-- 2,205 bytes parent folder | download | duplicates (4)
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
unit LazSynGtk2IMM;

{$mode objfpc}{$H+}

interface

uses
  {$ifdef LCLGtk2}
  gtk2, Gtk2Globals,
  {$endif}
  Classes, SysUtils, Messages, LMessages, LazSynIMMBase, SynEditKeyCmds;


type

  { LazSynImeGtk2 }

  LazSynImeGtk2 = class(LazSynIme)
  private
    FIMESelText: string;
  public
    procedure WMImeComposition(var Message: TMessage); override;
  end;


implementation

uses
  SynEdit;

{ LazSynImeGtk2 }

procedure LazSynImeGtk2.WMImeComposition(var Message: TMessage);
{$IFDEF WITH_GTK2_IM}
var
  IMStr:string;
  i:Integer;
{$ENDIF}
begin
{$IFDEF WITH_GTK2_IM}
  if (not FriendEdit.ReadOnly) then
  begin
    // set candidate position
    if (Message.WParam and (GTK_IM_FLAG_START or GTK_IM_FLAG_PREEDIT))<>0 then
      IM_Context_Set_Cursor_Pos(FriendEdit.CaretXPix,FriendEdit.CaretYPix+FriendEdit.LineHeight);
    // valid string at composition & commit
    if (Message.WParam and (GTK_IM_FLAG_COMMIT or GTK_IM_FLAG_PREEDIT)<>0) then
    begin
      // save selected text
      if Message.WParam and GTK_IM_FLAG_REPLACE=0 then
        FIMESelText:=FriendEdit.SelText;
      // insert preedit or commit string
      IMStr:=pchar(Message.LParam);
      // for IBUS IM
      if (Length(IMStr)=0) and (Message.WParam and GTK_IM_FLAG_REPLACE<>0) then
        TSynEdit(FriendEdit).CommandProcessor(ecDeleteChar,#0,nil)
      else
      for i:=1 to Length(IMStr) do
        TSynEdit(FriendEdit).CommandProcessor(ecChar,IMStr[i],nil);
      // select last preedit
      if (Message.WParam and GTK_IM_FLAG_COMMIT=0) then
      begin
        if Length(IMStr)>0 then
          for i:=1 to Length(UTF8Decode(IMStr)) do
            TSynEdit(FriendEdit).CommandProcessor(ecSelLeft,#0,nil);
      end
      else
        FIMESelText:='';
    end;
    // end composition and complete composition
    // To Do : skip insert saved selection after commit with ibus.
    if (Message.WParam and GTK_IM_FLAG_END<>0) and (FIMESelText<>'') then
    begin
      TSynEdit(FriendEdit).ClearSelection;
      // restore selection before preedit.
      for i:=1 to Length(FIMESelText) do
        TSynEdit(FriendEdit).CommandProcessor(ecChar,FIMESelText[i],nil);
      FIMESelText:='';
    end;
  end;
{$ENDIF}
end;

end.