File: FXComposeContext.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (571 lines) | stat: -rw-r--r-- 17,303 bytes parent folder | download | duplicates (2)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/********************************************************************************
*                                                                               *
*                         C o m p o s e - C o n t e x t                         *
*                                                                               *
*********************************************************************************
* Copyright (C) 2005,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library 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.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXFont.h"
#include "FXComposeContext.h"
#include "FXException.h"


/*
  Notes:
  - In Asian languages, a text entry widget that's being edited may have
    an input method editor.  During the composition process, the system
    needs to keep track of the state of the composition until it is
    committed as an input to the widget.  This class represents that
    state.
  - Each text entry widget may have a compositon context, while it
    has the focus.
  - The composition context is deleted when the focus is moved to another
    widget away from the entry widget.
*/

#define DISPLAY(app)     ((Display*)((app)->display))


using namespace FX;

/*******************************************************************************/

namespace FX {


// Object implementation
FXIMPLEMENT(FXComposeContext,FXId,nullptr,0)


#ifdef WIN32   //////////////////////////  MS-Windows ///////////////////////////


// Deserialization
FXComposeContext::FXComposeContext():window(nullptr),message(0){
  FXTRACE((100,"FXComposeContext::FXComposeContext %p\n",this));
  }


// Create input context
FXComposeContext::FXComposeContext(FXApp* a,FXWindow* win,FXSelector sel):FXId(a),window(win),message(sel){
  FXTRACE((100,"FXComposeContext::FXComposeContext %p\n",this));
  }


// Realize the input context
void FXComposeContext::create(){
  if(!xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::create %p\n",getClassName(),this));
      xid=window->id();
      }
    }
  }


// Unrealize the input context
void FXComposeContext::destroy(){
  if(xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::destroy %p\n",getClassName(),this));
      ///////
      }
    }
  }


// Set focus to it
void FXComposeContext::focusIn(){
  if(xid){
    ///////
    }
  }


// Kill focus to it
void FXComposeContext::focusOut(){
  if(xid){
    ///////
    }
  }


// Set the font
void FXComposeContext::setFont(FXFont* font){
  if(xid && font && font->id()){
    HIMC himc=ImmGetContext((HWND)xid);
    LOGFONT lf;
    GetObject((HFONT)font->id(),sizeof(LOGFONT),&lf);
    ImmSetCompositionFont(himc,&lf);
    ImmReleaseContext((HWND)xid,himc);
    }
  }


// Set the spot
void FXComposeContext::setSpot(FXint x,FXint y){
  if(xid){
    HIMC himc=ImmGetContext((HWND)xid);
    COMPOSITIONFORM cf;
    cf.dwStyle=CFS_POINT;
    FXint tox,toy;
    window->translateCoordinatesTo(tox,toy,window->getShell(),x,y);
    cf.ptCurrentPos.x=tox;
    cf.ptCurrentPos.y=toy;
    ImmSetCompositionWindow(himc,&cf);
    ImmReleaseContext((HWND)xid,himc);
    }
  }


// Set the area
void FXComposeContext::setArea(FXint x,FXint y,FXint w,FXint h){
  if(xid){
    ///////
    }
  }


// Translate key event
FXString FXComposeContext::translateEvent(FXRawEvent& event){
  FXString result;
  if(xid){
    HIMC himc=ImmGetContext(event.hwnd);
    LONG mlen=0;
    FXnchar* mstr;
    if(event.lParam&GCS_RESULTSTR){
      mlen=ImmGetCompositionString(himc,GCS_RESULTSTR,nullptr,0);
      mstr=new FXnchar [mlen];
      ImmGetCompositionString(himc,GCS_RESULTSTR,mstr,mlen);
      }
    else{
      mlen=ImmGetCompositionString(himc,GCS_COMPSTR,nullptr,0);
      mstr=new FXnchar [mlen+1];
      ImmGetCompositionString(himc,GCS_COMPSTR,mstr,mlen);
      }
    mstr[mlen/sizeof(FXnchar)]=0;
    ImmReleaseContext(event.hwnd,himc);
    int utf8len=WideCharToMultiByte(CP_UTF8,0,mstr,-1,nullptr,0,nullptr,nullptr);
    FXchar* utf8str=new FXchar [utf8len];
    WideCharToMultiByte(CP_UTF8,0,mstr,-1,utf8str,utf8len,nullptr,nullptr);
    result.assign(utf8str,utf8len);

    delete [] mstr;
    delete [] utf8str;
    }
  return result;
  }


// Delete input context
FXComposeContext::~FXComposeContext(){
  FXTRACE((100,"FXComposeContext::~FXComposeContext %p\n",this));
  }


#else   //////////////////////////////  X-Windows ///////////////////////////////


// Deserialization
FXComposeContext::FXComposeContext():window(nullptr),message(0),fontset(0){
  FXTRACE((100,"FXComposeContext::FXComposeContext %p\n",this));
  }


// Create input context
FXComposeContext::FXComposeContext(FXApp* a,FXWindow* win,FXSelector sel):FXId(a),window(win),message(sel),fontset(0){
  FXTRACE((100,"FXComposeContext::FXComposeContext %p\n",this));
  }


/*

    // Determine list of input styles
    XIMStyles *ximstyles=nullptr;
    XGetIMValues((XIM)xim,XNQueryInputStyle,&ximstyles,nullptr);
    if(ximstyles){
      FXuint s;

      // Try preferred input style
      for(s=0; s<ximstyles->count_styles; s++){
        if(ximstyles->supported_styles[s]==inputstyle) goto match;
        }

      // Try root input style
      inputstyle=XIMPreeditNothing|XIMStatusNothing;
      for(s=0; s<ximstyles->count_styles; s++){
        if(ximstyles->supported_styles[s]==inputstyle) goto match;
        }

      // Try none style
      inputstyle=XIMPreeditNone|XIMStatusNone;
      for(s=0; s<ximstyles->count_styles; s++){
        if(ximstyles->supported_styles[s]==inputstyle) goto match;
        }

      // No style at all
      inputstyle=0;

      // Free list
match:XFree(ximstyles);
*/


/*
FXbool isIMRunning(Display *display){
  const FXchar *p=XSetLocaleModifiers(nullptr);
  if(p){
    FXTRACE((100,"XSetLocaleModifiers=%s\n",p));
    FXString server("@server=");
    server.append(p+4);         // skip "@im="
    FXint pos=server.find('@',1);
    if(0<pos) server.trunc(pos);
    Atom atom=XInternAtom(display,server.text(),False);
    Window win=XGetSelectionOwner(display,atom);
    return win!=None;
    }
  return false;
  }
*/


// Realize the input context
void FXComposeContext::create(){
  if(!xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::create %p\n",getClassName(),this));
#ifndef NO_XIM
      XIMCallback statusStartStruct;
      XIMCallback statusDoneStruct;
      XIMCallback statusDrawStruct;
      XIMCallback editStartStruct;
      XIMCallback editDoneStruct;
      XIMCallback editDrawStruct;
      XIMCallback editCaretStruct;
      XVaNestedList editAttr;
      XVaNestedList statusAttr;
      XIMStyles *ximstyles=nullptr;
      XRectangle rect;
      XPoint spot;
      FXuint style,s;

      // Check if input methods are available
      if(!getApp()->hasInputMethod()){ fxerror("FXComposeContext: no input methods\n"); }

      // We must have a window
      if(!window || !window->id()){ fxerror("FXComposeContext: illegal window parameter\n"); }

      // Get input style
      if(FXString::comparecase(getApp()->inputstyle,"onthespot")==0)
        style=XIMPreeditCallbacks|XIMStatusNothing;
      else if(FXString::comparecase(getApp()->inputstyle,"overthespot")==0)
        style=XIMPreeditPosition|XIMStatusNothing;
      else if(FXString::comparecase(getApp()->inputstyle,"offthespot")==0)
        style=XIMPreeditArea|XIMStatusArea;
      else if(FXString::comparecase(getApp()->inputstyle,"root")==0)
        style=XIMPreeditNothing|XIMStatusNothing;
      else
        style=XIMPreeditNone|XIMStatusNone;

      // Determine list of input styles
      XGetIMValues((XIM)getApp()->xim,XNQueryInputStyle,&ximstyles,nullptr);
      if(ximstyles){

        // Try preferred input style
        for(s=0; s<ximstyles->count_styles; s++){
          if(ximstyles->supported_styles[s]==style) goto m;
          }

        // Try root input style
        style=XIMPreeditNothing|XIMStatusNothing;
        for(s=0; s<ximstyles->count_styles; s++){
          if(ximstyles->supported_styles[s]==style) goto m;
          }

        // Try none style
        style=XIMPreeditNone|XIMStatusNone;
        for(s=0; s<ximstyles->count_styles; s++){
          if(ximstyles->supported_styles[s]==style) goto m;
          }

        // Pick first
        if(ximstyles->count_styles){
          style=ximstyles->supported_styles[0];
          }

        // Free list
m:      XFree(ximstyles);
        }

      // On the spot method
      if(style&XIMPreeditCallbacks){
        editStartStruct.client_data=(XPointer)this;
        editStartStruct.callback=(XIMProc)editStartCallback;
        editDoneStruct.client_data=(XPointer)this;
        editDoneStruct.callback=(XIMProc)editDoneCallback;
        editDrawStruct.client_data=(XPointer)this;
        editDrawStruct.callback=(XIMProc)editDrawCallback;
        editCaretStruct.client_data=(XPointer)this;
        editCaretStruct.callback=(XIMProc)editCaretCallback;
        editAttr=XVaCreateNestedList(0,XNPreeditStartCallback,&editStartStruct,XNPreeditDrawCallback,&editDrawStruct,XNPreeditDoneCallback,&editDoneStruct,XNPreeditCaretCallback,&editCaretStruct,nullptr);

        // Have status callbacks
        if(style&XIMStatusCallbacks){
          FXTRACE((100,"On the Spot/Status\n"));
          statusStartStruct.client_data=(XPointer)this;
          statusStartStruct.callback=(XIMProc)statusStartCallback;
          statusDoneStruct.client_data=(XPointer)this;
          statusDoneStruct.callback=(XIMProc)statusDoneCallback;
          statusDrawStruct.client_data=(XPointer)this;
          statusDrawStruct.callback=(XIMProc)statusDrawCallback;
          statusAttr=XVaCreateNestedList(0,XNStatusStartCallback,&statusStartStruct,XNStatusDoneCallback,&statusDoneStruct,XNStatusDrawCallback,&statusDrawStruct,nullptr);
          xid=(FXID)XCreateIC((XIM)getApp()->xim,XNInputStyle,XIMPreeditCallbacks|XIMStatusCallbacks,XNClientWindow,window->id(),XNPreeditAttributes,editAttr,XNStatusAttributes,statusAttr,nullptr);
          XFree(statusAttr);
          }

        // No status callbacks
        else{
          FXTRACE((100,"On the Spot\n"));
          xid=(FXID)XCreateIC((XIM)getApp()->xim,XNInputStyle,XIMPreeditCallbacks|XIMStatusNothing,XNClientWindow,window->id(),XNPreeditAttributes,editAttr,nullptr);
          }
        XFree(editAttr);
        }

      // Off the spot method
      else if(style&XIMPreeditArea){
        FXTRACE((100,"Off the Spot\n"));
        rect.x=0;
        rect.y=0;
        rect.width=window->getWidth();
        rect.height=window->getHeight();
        editAttr=XVaCreateNestedList(0,XNArea,&rect,nullptr);
        xid=(FXID)XCreateIC((XIM)getApp()->xim,XNInputStyle,XIMPreeditArea|XIMStatusArea,XNClientWindow,window->id(),XNPreeditAttributes,editAttr,nullptr);
        XFree(editAttr);
        }

      // Over the spot method
      else if(style&XIMPreeditPosition){
        FXTRACE((100,"Over the Spot\n"));
        spot.x=1;
        spot.y=1;
        int missing_charcount;
        char** missing_charsetlist;
        char* def_string;
        fontset=XCreateFontSet(DISPLAY(getApp()),"10x20,10x20",&missing_charsetlist,&missing_charcount,&def_string);
        editAttr=XVaCreateNestedList(0,XNSpotLocation,&spot,XNFontSet,fontset,nullptr);
        xid=(FXID)XCreateIC((XIM)getApp()->xim,XNInputStyle,XIMPreeditPosition|XIMStatusNothing,XNClientWindow,window->id(),XNPreeditAttributes,editAttr,nullptr);
        XFreeStringList(missing_charsetlist);
        XFree(editAttr);
        }

      // Root method
      else{
        FXTRACE((100,"Root\n"));
        xid=(FXID)XCreateIC((XIM)getApp()->xim,XNInputStyle,XIMPreeditNothing|XIMStatusNothing,XNClientWindow,window->id(),nullptr);
        }

      // Reset context
      if(xid){
        //long filterevents=0;
        //XGetICValues((XIC)xid,XNFilterEvents,&filterevents,nullptr);
        //XSelectInput((Display*)getApp()->getDisplay(),window->id(),BASIC_EVENT_MASK|ENABLED_EVENT_MASK|filterevents);
        XmbResetIC((XIC)xid);
        }
#endif
      }
    }
  }


// Unrealize the input context
void FXComposeContext::destroy(){
  if(xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::destroy %p\n",getClassName(),this));
#ifndef NO_XIM
      XDestroyIC((XIC)xid);
#endif
      }
    }
  }


// Set focus to it
void FXComposeContext::focusIn(){
#ifndef NO_XIM
  if(xid){
    XSetICFocus((XIC)xid);
    }
#endif
  }


// Kill focus to it
void FXComposeContext::focusOut(){
#ifndef NO_XIM
  if(xid){
    XUnsetICFocus((XIC)xid);
    }
#endif
  }


// Set the font
void FXComposeContext::setFont(FXFont* font){
  if(xid && font && font->id()){
#ifndef NO_XIM
      ///
#endif
    }
  }


// Set the spot
void FXComposeContext::setSpot(FXint x,FXint y){
#ifndef NO_XIM
  if(xid){
    XVaNestedList editAttr;
    XPoint spot;
    spot.x=x;
    spot.y=y;
    editAttr=XVaCreateNestedList(0,XNSpotLocation,&spot,nullptr);
    XSetICValues((XIC)xid,XNPreeditAttributes,editAttr,nullptr);
    XFree(editAttr);
    }
#endif
  }


// Set the area
void FXComposeContext::setArea(FXint x,FXint y,FXint w,FXint h){
#ifndef NO_XIM
  if(xid){
    XVaNestedList editAttr;
    XRectangle rect;
    rect.x=x;
    rect.y=y;
    rect.width=w;
    rect.height=h;
    editAttr=XVaCreateNestedList(0,XNArea,&rect,nullptr);
    XSetICValues((XIC)xid,XNPreeditAttributes,editAttr,nullptr);
    XFree(editAttr);
    }
#endif
  }


// Translate key event
FXString FXComposeContext::translateEvent(FXRawEvent& event){
  FXString result;
#ifndef NO_XIM
  if(xid){
    char* buffer=new char [513];
    KeySym sym; Status s; int n;
    n=XmbLookupString((XIC)xid,&event.xkey,buffer,512,&sym,&s);
    if(s==XBufferOverflow){
      delete [] buffer;
      buffer=new char [n+1];
      n=XmbLookupString((XIC)xid,&event.xkey,buffer,n,&sym,&s);
      }
    if(s!=XLookupChars && s!=XLookupBoth) n=0;
    // FIXME decode buffer based on XLocaleOfIM(XIMOfIC((XIC)xid))
    buffer[n]=0;
    FXTRACE((100,"XLocaleOfIM=%s\n",XLocaleOfIM(XIMOfIC((XIC)xid))));
    result.assign(buffer,n);
    delete [] buffer;
    }
#endif
  return result;
  }


int FXComposeContext::editStartCallback(void*,FXComposeContext* cc,void*){
  FXTRACE((100,"editStartCallback\n"));
  return -1;			// No length limit
  }


void FXComposeContext::editDoneCallback(void*,FXComposeContext* cc,void*){
  FXTRACE((100,"editDoneCallback\n"));
  }


void FXComposeContext::editDrawCallback(void*,FXComposeContext* cc,void* ptr){
#ifndef NO_XIM
  XIMPreeditDrawCallbackStruct *drawstruct=(XIMPreeditDrawCallbackStruct*)ptr;
  XIMText *ximtext=drawstruct->text;
  FXTRACE((100,"editDrawCallback caret=%d first=%d len=%d\n",drawstruct->caret,drawstruct->chg_first,drawstruct->chg_length));
#endif
  }


void FXComposeContext::editCaretCallback(void*,FXComposeContext* cc,void* ptr){
#ifndef NO_XIM
  XIMPreeditCaretCallbackStruct *caretstruct=(XIMPreeditCaretCallbackStruct*)ptr;
  FXTRACE((100,"editCaretCallback position=%d direction=%d style=%d\n",caretstruct->position,caretstruct->direction,caretstruct->style));
#endif
  }


void FXComposeContext::statusStartCallback(void*,FXComposeContext* cc,void*){
  FXTRACE((100,"statusStartCallback\n"));
  }


void FXComposeContext::statusDoneCallback(void*,FXComposeContext* cc,void*){
  FXTRACE((100,"statusDoneCallback\n"));
  }


void FXComposeContext::statusDrawCallback(void*,FXComposeContext* cc,void* ptr){
#ifndef NO_XIM
  XIMStatusDrawCallbackStruct* drawstruct=(XIMStatusDrawCallbackStruct*)ptr;
  FXTRACE((100,"statusDrawCallback\n"));
#endif
  }


// Delete input context
FXComposeContext::~FXComposeContext(){
  FXTRACE((100,"FXComposeContext::~FXComposeContext %p\n",this));
  destroy();
  window=(FXWindow*)-1L;
  if(fontset) XFreeFontSet(DISPLAY(getApp()),(XFontSet)fontset);
  fontset=(XFontSet)-1L;
  }

#endif  /////////////////////////////////////////////////////////////////////////


}