File: FXAccelTable.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 (641 lines) | stat: -rw-r--r-- 17,634 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/********************************************************************************
*                                                                               *
*                   A c c e l e r a t o r   T a b l e   C l a s s               *
*                                                                               *
*********************************************************************************
* Copyright (C) 1998,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 "fxchar.h"
#include "fxkeys.h"
#include "fxmath.h"
#include "fxascii.h"
#include "fxunicode.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXElement.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXObject.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXAccelTable.h"
#include "fxpriv.h"

/*
  Notes:
  - Routines parseAccel(), unparseAccel(), and parseHotKey() now static members of
    FXAccelTable.

  - Need to call parseAccel() is much reduced since addition of convenience functions
    that take a string as input; these new routines overload addAccel(), removeAccel(),
    hasAccel() and targetOfAccel().

  - Thus, often no need to call parseAccel() directly in many cases, and much less
    cumbersome stuff to learn.

  - We need to deal with X11 unicode keysyms (with 0x01000000 flag) in some way.
*/

#define TOPIC_CONSTRUCT  1000
#define TOPIC_KEYBOARD   1009
#define TOPIC_ACCEL      1011

#define EMPTYSLOT       0xfffffffe   // Previously used, now empty slot
#define UNUSEDSLOT      0xffffffff   // Unsused slot marker


using namespace FX;

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

namespace FX {


// Map
FXDEFMAP(FXAccelTable) FXAccelTableMap[]={
  FXMAPFUNC(SEL_KEYPRESS,0,FXAccelTable::onKeyPress),
  FXMAPFUNC(SEL_KEYRELEASE,0,FXAccelTable::onKeyRelease),
  };


// Object implementation
FXIMPLEMENT(FXAccelTable,FXObject,FXAccelTableMap,ARRAYNUMBER(FXAccelTableMap))


// Make empty accelerator table
FXAccelTable::FXAccelTable(){
  FXTRACE((TOPIC_CONSTRUCT,"%p->FXAccelTable::FXAccelTable\n",this));
  allocElms(key,1);
  key[0].code=UNUSEDSLOT;
  key[0].target=nullptr;
  key[0].messagedn=0;
  key[0].messageup=0;
  max=0;
  num=0;
  }


// Resize hash table, and rehash old stuff into it
void FXAccelTable::resize(FXuint m){
  FXuint p,i,c;
  FXAccelKey *newkey;
  allocElms(newkey,m+1);
  for(i=0; i<=m; i++){
    newkey[i].code=UNUSEDSLOT;
    newkey[i].target=nullptr;
    newkey[i].messagedn=0;
    newkey[i].messageup=0;
    }
  for(i=0; i<=max; i++){
    if((c=key[i].code)>=EMPTYSLOT) continue;
    p=(c*13)&m;
    while(newkey[p].code!=UNUSEDSLOT) p=(p+1)&m;
    newkey[p]=key[i];
    }
  freeElms(key);
  key=newkey;
  max=m;
  }

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

// Add accelerator key combination into the accelerator table
void FXAccelTable::addAccel(FXHotKey hotkey,FXObject* target,FXSelector seldn,FXSelector selup){
  if(hotkey){
    FXTRACE((TOPIC_ACCEL,"%p->FXAccelTable::addAccel: code=%04x state=%04x\n",this,(FXushort)hotkey,(FXushort)(hotkey>>16)));
    FXuint p=(hotkey*13)&max;
    FXuint c;
    FXASSERT(hotkey!=UNUSEDSLOT);
    FXASSERT(hotkey!=EMPTYSLOT);
    while((c=key[p].code)!=UNUSEDSLOT){ // Check if in table already
      if(c==hotkey) goto x;
      p=(p+1)&max;
      }
    ++num;
    if(max<(num<<1)) resize((max<<1)+1);
    FXASSERT(num<=max);
    p=(hotkey*13)&max;                  // Locate first unused or empty slot
    while(key[p].code<EMPTYSLOT){
      p=(p+1)&max;
      }
x:  key[p].code=hotkey;                 // Add or replace accelerator info
    key[p].target=target;
    key[p].messagedn=seldn;
    key[p].messageup=selup;
    }
  }


// Parse key-combination description and add it into the accelerator table
void FXAccelTable::addAccel(const FXString& string,FXObject* target,FXSelector seldn,FXSelector selup){
  addAccel(parseAccel(string),target,seldn,selup);
  }


// Parse key-combination description and add it into the accelerator table
void FXAccelTable::addAccel(const FXchar* string,FXObject* target,FXSelector seldn,FXSelector selup){
  addAccel(parseAccel(string),target,seldn,selup);
  }

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

// Remove accelerator key combination from the accelerator table
void FXAccelTable::removeAccel(FXHotKey hotkey){
  if(hotkey){
    FXTRACE((TOPIC_ACCEL,"%p->FXAccelTable::removeAccel: code=%04x state=%04x\n",this,(FXushort)hotkey,(FXushort)(hotkey>>16)));
    FXuint p=(hotkey*13)&max;
    FXuint c;
    FXASSERT(hotkey!=UNUSEDSLOT);
    FXASSERT(hotkey!=EMPTYSLOT);
    while((c=key[p].code)!=hotkey){
      if(c==UNUSEDSLOT) return;
      p=(p+1)&max;
      }
    if(key[(p+1)&max].code==UNUSEDSLOT){// Last in chain
      key[p].code=UNUSEDSLOT;
      }
    else{                               // Middle of chain
      key[p].code=EMPTYSLOT;
      }
    key[p].target=nullptr;
    key[p].messagedn=0;
    key[p].messageup=0;
    if(max>=(num<<2)) resize(max>>1);
    --num;
    FXASSERT(num<=max);
    }
  }


// Parse key-combination description and remove it from the accelerator table
void FXAccelTable::removeAccel(const FXString& string){
  removeAccel(parseAccel(string));
  }


// Parse key-combination description and remove it from the accelerator table
void FXAccelTable::removeAccel(const FXchar* string){
  removeAccel(parseAccel(string));
  }

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

// Return true if accelerator accelerator key-combination is in accelerator table
FXbool FXAccelTable::hasAccel(FXHotKey hotkey) const {
  if(hotkey){
    FXuint p=(hotkey*13)&max;
    FXuint c;
    FXASSERT(hotkey!=UNUSEDSLOT);
    FXASSERT(hotkey!=EMPTYSLOT);
    while((c=key[p].code)!=hotkey){
      if(c==UNUSEDSLOT) return false;
      p=(p+1)&max;
      }
    return true;
    }
  return false;
  }


// Parse key-combination description and return true if it is in the accelerator table
FXbool FXAccelTable::hasAccel(const FXString& string) const {
  return hasAccel(parseAccel(string));
  }


// Parse key-combination description and return true if it is in the accelerator table
FXbool FXAccelTable::hasAccel(const FXchar* string) const {
  return hasAccel(parseAccel(string));
  }

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

// Return target object of the given accelerator key-combination
FXObject* FXAccelTable::targetOfAccel(FXHotKey hotkey) const {
  if(hotkey){
    FXuint p=(hotkey*13)&max;
    FXuint c;
    FXASSERT(hotkey!=UNUSEDSLOT);
    FXASSERT(hotkey!=EMPTYSLOT);
    while((c=key[p].code)!=hotkey){
      if(c==UNUSEDSLOT) return nullptr;
      p=(p+1)&max;
      }
    return key[p].target;
    }
  return nullptr;
  }


// Parse key-combination description and return its target
FXObject* FXAccelTable::targetOfAccel(const FXString& string) const {
  return targetOfAccel(parseAccel(string));
  }


// Parse key-combination description and return its target
FXObject* FXAccelTable::targetOfAccel(const FXchar* string) const {
  return targetOfAccel(parseAccel(string));
  }

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

// Keyboard press; forward to accelerator target
long FXAccelTable::onKeyPress(FXObject* sender,FXSelector,void* ptr){
  FXTRACE((TOPIC_KEYBOARD,"%p->FXAccelTable::onKeyPress keysym=0x%04x state=%04x\n",this,((FXEvent*)ptr)->code,((FXEvent*)ptr)->state));
  FXEvent* event=(FXEvent*)ptr;
  FXuint code=MKUINT(event->code,event->state&(SHIFTMASK|CONTROLMASK|ALTMASK));
  FXuint p=(code*13)&max;
  FXuint c;
  FXASSERT(code!=UNUSEDSLOT);
  FXASSERT(code!=EMPTYSLOT);
  while((c=key[p].code)!=code){
    if(c==UNUSEDSLOT) return 0;
    p=(p+1)&max;
    }
  if(key[p].target && key[p].messagedn){
    key[p].target->tryHandle(sender,key[p].messagedn,ptr);
    }
  return 1;
  }


// Keyboard release; forward to accelerator target
long FXAccelTable::onKeyRelease(FXObject* sender,FXSelector,void* ptr){
  FXTRACE((TOPIC_KEYBOARD,"%p->FXAccelTable::onKeyRelease keysym=0x%04x state=%04x\n",this,((FXEvent*)ptr)->code,((FXEvent*)ptr)->state));
  FXEvent* event=(FXEvent*)ptr;
  FXuint code=MKUINT(event->code,event->state&(SHIFTMASK|CONTROLMASK|ALTMASK));
  FXuint p=(code*13)&max;
  FXuint c;
  FXASSERT(code!=UNUSEDSLOT);
  FXASSERT(code!=EMPTYSLOT);
  while((c=key[p].code)!=code){
    if(c==UNUSEDSLOT) return 0;
    p=(p+1)&max;
    }
  if(key[p].target && key[p].messageup){
    key[p].target->tryHandle(sender,key[p].messageup,ptr);
    }
  return 1;
  }

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

// Parse accelerator from string, yielding modifier and key code
FXHotKey FXAccelTable::parseAccel(const FXString& string){
  return FXAccelTable::parseAccel(string.text());
  }


// Parse accelerator from string, yielding modifier and key code
FXHotKey FXAccelTable::parseAccel(const FXchar* string){
  const FXchar* ptr=string;
  FXuint code=0;
  FXuint mods=0;

  // Parse leading space
  while(*ptr && Ascii::isSpace(*ptr)) ptr++;

  // Parse modifiers
  while(*ptr){

    // Modifier
    if(FXString::comparecase(ptr,"ctl",3)==0){ mods|=CONTROLMASK; ptr+=3; }
    else if(FXString::comparecase(ptr,"ctrl",4)==0){ mods|=CONTROLMASK; ptr+=4; }
    else if(FXString::comparecase(ptr,"alt",3)==0){ mods|=ALTMASK; ptr+=3; }
    else if(FXString::comparecase(ptr,"meta",4)==0){ mods|=METAMASK; ptr+=4; }
    else if(FXString::comparecase(ptr,"shift",5)==0){ mods|=SHIFTMASK; ptr+=5; }
    else break;

    // Separator
    if(*ptr=='+' || *ptr=='-' || Ascii::isSpace(*ptr)) ptr++;
    }

  // Test for some special keys
  if(FXString::comparecase(ptr,"home",4)==0){
    code=KEY_Home;
    }
  else if(FXString::comparecase(ptr,"end",3)==0){
    code=KEY_End;
    }
  else if(FXString::comparecase(ptr,"pgup",4)==0){
    code=KEY_Page_Up;
    }
  else if(FXString::comparecase(ptr,"pgdn",4)==0){
    code=KEY_Page_Down;
    }
  else if(FXString::comparecase(ptr,"left",4)==0){
    code=KEY_Left;
    }
  else if(FXString::comparecase(ptr,"right",5)==0){
    code=KEY_Right;
    }
  else if(FXString::comparecase(ptr,"up",2)==0){
    code=KEY_Up;
    }
  else if(FXString::comparecase(ptr,"down",4)==0){
    code=KEY_Down;
    }
  else if(FXString::comparecase(ptr,"ins",3)==0){
    code=KEY_Insert;
    }
  else if(FXString::comparecase(ptr,"del",3)==0){
    code=KEY_Delete;
    }
  else if(FXString::comparecase(ptr,"delete",6)==0){
    code=KEY_Delete;
    }
  else if(FXString::comparecase(ptr,"esc",3)==0){
    code=KEY_Escape;
    }
  else if(FXString::comparecase(ptr,"tab",3)==0){
    code=KEY_Tab;
    }
  else if(FXString::comparecase(ptr,"return",6)==0){
    code=KEY_Return;
    }
  else if(FXString::comparecase(ptr,"enter",5)==0){
    code=KEY_Return;
    }
  else if(FXString::comparecase(ptr,"back",4)==0){
    code=KEY_BackSpace;
    }
  else if(FXString::comparecase(ptr,"backspace",9)==0){
    code=KEY_BackSpace;
    }
  else if(FXString::comparecase(ptr,"spc",3)==0){
    code=KEY_space;
    }
  else if(FXString::comparecase(ptr,"space",5)==0){
    code=KEY_space;
    }

  // Test for function keys
  else if(Ascii::toLower(*ptr)=='f' && Ascii::isDigit(*(ptr+1))){
    if(Ascii::isDigit(*(ptr+2))){
      code=KEY_F1+10*(*(ptr+1)-'0')+(*(ptr+2)-'0')-1;
      }
    else{
      code=KEY_F1+*(ptr+1)-'0'-1;
      }
    }

  // Test if hexadecimal code designator
  else if(*ptr=='#' && Ascii::isHexDigit(*(ptr+1))){
    code=Ascii::digitValue(*(ptr+1));
    ptr+=2;
    while(Ascii::isHexDigit(*ptr)){
      code=(code<<4)+Ascii::digitValue(*ptr);
      ptr++;
      }
    }

  // Test if its a single character accelerator
  else if(Ascii::isPrint(*ptr)){
    if(mods&SHIFTMASK)
      code=Ascii::toUpper(*ptr)+KEY_space-' ';
    else
      code=Ascii::toLower(*ptr)+KEY_space-' ';
    }

  FXTRACE((TOPIC_ACCEL,"parseAccel(%s) = code=%04x mods=%04x\n",string,code,mods));
  return MKUINT(code,mods);
  }

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

// Unparse hot key back into a string
FXString FXAccelTable::unparseAccel(FXHotKey key){
  FXuint mods=(key&0xffff0000)>>16;
  FXuint code=(key&0xffff);
  FXString s;

  // Handle modifier keys
  if(mods&CONTROLMASK) s+="Ctrl+";
  if(mods&ALTMASK) s+="Alt+";
  if(mods&SHIFTMASK) s+="Shift+";
  if(mods&METAMASK) s+="Meta+";

  // Handle some special keys
  switch(code){
    case KEY_Home:
      s+="Home";
      break;
    case KEY_End:
      s+="End";
      break;
    case KEY_Page_Up:
      s+="PgUp";
      break;
    case KEY_Page_Down:
      s+="PgDn";
      break;
    case KEY_Left:
      s+="Left";
      break;
    case KEY_Right:
      s+="Right";
      break;
    case KEY_Up:
      s+="Up";
      break;
    case KEY_Down:
      s+="Down";
      break;
    case KEY_Insert:
      s+="Ins";
      break;
    case KEY_Delete:
      s+="Del";
      break;
    case KEY_Escape:
      s+="Esc";
      break;
    case KEY_Tab:
      s+="Tab";
      break;
    case KEY_Return:
      s+="Return";
      break;
    case KEY_BackSpace:
      s+="Back";
      break;
    case KEY_space:
      s+="Space";
      break;
    case KEY_F1:
    case KEY_F2:
    case KEY_F3:
    case KEY_F4:
    case KEY_F5:
    case KEY_F6:
    case KEY_F7:
    case KEY_F8:
    case KEY_F9:
    case KEY_F10:
    case KEY_F11:
    case KEY_F12:
    case KEY_F13:
    case KEY_F14:
    case KEY_F15:
    case KEY_F16:
    case KEY_F17:
    case KEY_F18:
    case KEY_F19:
    case KEY_F20:
    case KEY_F21:
    case KEY_F22:
    case KEY_F23:
    case KEY_F24:
    case KEY_F25:
    case KEY_F26:
    case KEY_F27:
    case KEY_F28:
    case KEY_F29:
    case KEY_F30:
    case KEY_F31:
    case KEY_F32:
    case KEY_F33:
    case KEY_F34:
    case KEY_F35:
      s+=FXString::value("F%d",code-KEY_F1+1);
      break;
    default:
      if(Ascii::isPrint(code)){
        if(mods&SHIFTMASK)
          s+=Ascii::toUpper(code);
        else
          s+=Ascii::toLower(code);
        }
      else{
        s+=FXString::value("#%04X",code);
        }
      break;
    }
  return s;
  }

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

// Parse hot key from string
FXHotKey parseHotKey(const FXString& string){
  const FXchar* ptr=string.text();
  FXuint code=0;
  FXuint mods=0;
  while(*ptr){
    if(*ptr=='&'){
      if(*(ptr+1)!='&'){
        FXwchar w=wc(ptr+1);
        if(Unicode::isAlphaNumeric(w)){
          mods=ALTMASK;
          code=fxucs2keysym(Unicode::toLower(w));
          }
        break;
        }
      ptr++;
      }
    ptr++;
    }
  FXTRACE((TOPIC_ACCEL,"parseHotKey(%s) = code=%04x mods=%04x\n",string.text(),code,mods));
  return MKUINT(code,mods);
  }


// Obtain hot key offset in string
FXint findHotKey(const FXString& string){
  FXint pos=0;
  FXint n=0;
  while(pos<string.length()){
    if(string[pos]=='&'){
      if(string[pos+1]!='&'){
        return n;
        }
      pos++;
      }
    pos++;
    n++;
    }
  return -1;
  }


// Strip hot key from string
FXString stripHotKey(const FXString& string){
  FXString result=string;
  FXint len=result.length();
  FXint i,j;
  for(i=j=0; j<len; j++){
    if(result[j]=='&'){
      if(result[j+1]!='&') continue;
      j++;
      }
    result[i++]=result[j];
    }
  result.trunc(i);
  return result;
  }

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

// Save data
void FXAccelTable::save(FXStream& store) const {
  FXObject::save(store);
  store << max;
  store << num;
  for(FXuint i=0; i<=max; i++){
    store << key[i].code;
    store << key[i].target;
    store << key[i].messagedn;
    store << key[i].messageup;
    }
  }


// Load data
void FXAccelTable::load(FXStream& store){
  FXObject::load(store);
  store >> max;
  store >> num;
  resizeElms(key,max+1);
  for(FXuint i=0; i<=max; i++){
    store >> key[i].code;
    store >> key[i].target;
    store >> key[i].messagedn;
    store >> key[i].messageup;
    }
  }


// Destroy table
FXAccelTable::~FXAccelTable(){
  FXTRACE((TOPIC_CONSTRUCT,"%p->FXAccelTable::~FXAccelTable\n",this));
  freeElms(key);
  key=(FXAccelKey*)-1L;
  }


}