File: SYuditInput.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (317 lines) | stat: -rw-r--r-- 8,067 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
316
317
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "swidget/SYuditInput.h"

/**
 * SYuditInput is receiving events from STextEdit
 * It insert glyphs underlined into editorID and 
 * when composition finished it sends it to the editor, after that 
 * it may put half finished compsitions in the buffer.
 * @param name is the input name.
 * @param _eif is the editor interface to modify text.
 * @param ed is the editor.
 */
SYuditInput::SYuditInput (const SString& name, SEditorIF* _eif, SEditor* ed):
     encoder (name)
{
  eif = _eif;
  editor = ed;
  startDirection = SS_DR_L;
}

/**
 * Delete SYuditInput.
 * for normal operaztion use clear before deleting.
 */
SYuditInput::~SYuditInput ()
{
}

/**
 * If SYuditInput is present and working, STextEdit sends the
 * events here first for processing. 
 */
void
SYuditInput::keyPressed (SWindowListener::SKey key, const SString& _s,
            bool ctrl, bool shift, bool meta)
{ 

  SString s = _s;
  if (!eif->isEditable())
  {
    clear();
    editor->keyPressed (key, s, ctrl, shift, meta);
  }
  switch (key)
  {
  case SWindowListener::Key_BackSpace:
  case SWindowListener::Key_Delete:
   if (startIndex.getTextIndex()!=endIndex.getTextIndex())
   {
     SString pre = encoder.preEditBuffer();
     SV_UCS4 pb = encoder.postEditBuffer();
     almostClear();
     (void) encoder.decode ("", false); /* clear */
     SString post = encoder.encode (pb);
     SString a = encoder.encode (SV_UCS4());
     if (a.size()) post.append (a);
     if (pre.size()) post.append (pre);
     if (post.size() > 0) post.truncate(post.size()-1);
     s = post;
   }
   else
   {
     editor->keyPressed (key, s, ctrl, shift, meta);
     return;
   }
   break;
  /* Do not clear - we might need to press shift! */
  case SWindowListener::Key_Shift_R:
  case SWindowListener::Key_Shift_L:
   if (startIndex.getTextIndex()==endIndex.getTextIndex())
   {
     editor->keyPressed (key, s, ctrl, shift, meta);
   }
   break;
  case SWindowListener::Key_Control_R:
  case SWindowListener::Key_Control_L:
  case SWindowListener::Key_Meta_R:
  case SWindowListener::Key_Meta_L:
  case SWindowListener::Key_Alt_R:
  case SWindowListener::Key_Alt_L:
  case SWindowListener::Key_Escape:
  case SWindowListener::Key_Home:
  case SWindowListener::Key_End:
  case SWindowListener::Key_Prior:
  case SWindowListener::Key_Next:
  case SWindowListener::Key_Up:
  case SWindowListener::Key_Down:
  case SWindowListener::Key_Left:
  case SWindowListener::Key_Right:
  case SWindowListener::Key_Enter:
  case SWindowListener::Key_Return:
  case SWindowListener::Key_F1:
  case SWindowListener::Key_F2:
  case SWindowListener::Key_F3:
  case SWindowListener::Key_F4:
  case SWindowListener::Key_F5:
  case SWindowListener::Key_F6:
  case SWindowListener::Key_F7:
  case SWindowListener::Key_F8:
  case SWindowListener::Key_F9:
  case SWindowListener::Key_F10:
  case SWindowListener::Key_F11:
  case SWindowListener::Key_F12:
    if (startIndex.getTextIndex()!=endIndex.getTextIndex())
    {
      clear();
    }
    editor->keyPressed (key, s, ctrl, shift, meta);
    return;
  default:
    if (s.size()==0 && !(ctrl || meta)) return;
    if (ctrl || meta)
    {
      if (startIndex.getTextIndex()!=endIndex.getTextIndex()) clear();
      editor->keyPressed (key, s, ctrl, shift, meta); 
      return;
    }
  }
  if (s.size()==0) return;
  SString remaining = s;
  SV_UCS4 pbefore = encoder.postEditBuffer();
  SV_UCS4 u;
  // input is not ascii.
  if (s.size() > 1 && (s[0] &0x80) == 0x80) {
    SEncoder utf8;
    u = encoder.decode("", false);
    u.append (utf8.decode(s));
  } else {
    u = encoder.decode (s);
  }
  SV_UCS4 pafter = encoder.postEditBuffer();
  SV_UCS4 pedit;
  /* we just append ? */
  if (u.size() > 0 || pafter.size() != pbefore.size())
  {
    remaining = encoder.preEditBuffer();
    pedit = pafter;
    almostClear ();
    SEncoder utf8;
    editor->keyPressed (SWindowListener::Key_Send, utf8.encode(u), 
           false, false, false); 
  }
  /* process glyphs here */
  if (startIndex.getTextIndex()==endIndex.getTextIndex())
  {
    startIndex = eif->getCursorIndex();
    startDirection = eif->getDirection();
    endIndex = startIndex;
  }
  if (pedit.size()) 
  {
    SEncoder utf8;
    eif->insertPreEditText (utf8.encode (pedit), SPreEditor::Style_Default);
    endIndex = eif->getCursorIndex();
  }
  if (remaining.size())
  {
    eif->insertPreEditText (remaining, SPreEditor::Style_Default);
    endIndex = eif->getCursorIndex();
  }
  preEditSize = remaining.size() + pedit.size();
}

/**
 * clear the input text.
 * @param tosend is true if the remaining characters should
 * be sent out.
 * @return true if it had a text.
 */
bool
SYuditInput::clear(bool tosend)
{
  SString toSend;
  if (preEditSize)
  {
    /* flush */
    SV_UCS4 u = encoder.decode ("", false);
    if (u.size() > 0)
    {
      SEncoder utf8;
      toSend = utf8.encode(u);
    }
  }
  /* make it pre-emptive */
  encoder = SEncoder(encoder.getName());
  almostClear();
  if (toSend.size())
  {
     if (tosend)
     {
       editor->keyPressed (SWindowListener::Key_Send, toSend, 
           false, false, false); 
     }
     return true;
  }
  return false;
}

/**
 * clear it.
 */
void
SYuditInput::almostClear()
{
  if (startIndex.getTextIndex() == endIndex.getTextIndex())
  {
    startIndex = SCursorIndex (0, 0);
    endIndex = startIndex;
    preEditSize = 0;
    return;
  }
  /* Make it pre-emptive */
  SCursorIndex s = startIndex;
  SCursorIndex e = endIndex;

  startIndex = SCursorIndex (0, 0);
  endIndex = startIndex;
  preEditSize = 0;

  eif->endSelect ();
  eif->setCursorIndex (s);
  eif->eraseText (e.getTextIndex());
//fprintf (stderr, "setCursor\n");
  eif->setCursorIndex (s);
//fprintf (stderr, "setDirection\n");
  eif->setDirection (startDirection);
  return;
}

/**
 * return true if input method works fine.
 */
bool
SYuditInput::isOK()
{
  return encoder.isOK();
}

/**
 * Try to reverse encode the input with the current keymap
 */
SString
SYuditInput::encode (const SV_UCS4& v) const
{
  SString ret;
  if (v.size()==0) return SString(ret);
  SEncoder e = encoder;
  ret = e.encode (v);
  /* flush */
  SV_UCS4 empty;
  ret.append (e.encode (empty));

  /* check it against input */
  SV_UCS4 check = e.decode (ret, true);
  SString es;
  check.append (e.decode (es, false));
  bool tryagain = false;
  if (v.size() != check.size())
  {
    tryagain = true;
  }
  else for (unsigned int i=0; i<v.size(); i++)
  {
    if (v[i] != check[i])
    {
      tryagain = true;
      break;
    }
  }
  if (tryagain)
  {
    /* try with a space */
    check = e.decode (ret, true);
    check.append (e.decode (SString(" "), false));
    if (check.size() > v.size() && check.size() > 1 
      && check[check.size()-1] == (SS_UCS4)' ')
    {
      check.truncate (check.size()-1); 
    }
    if (v.size() != check.size())
    {
      ret.clear();
    }
    else for (unsigned int i=0; i<v.size(); i++)
    {
      if (v[i] != check[i])
      {
        ret.clear();
        break;
      }
    }
  }
  return SString (ret);
}