1 // GrammarUnit.h
2 
3 #ifndef __GrammarUnit_h
4 #define __GrammarUnit_h
5 
6 #include <string>
7 #include <vector>
8 
9 using namespace std;
10 
11 enum EventType {
12     T_IGNORE    = 0,        // the event is ignored
13     T_CLEAR     = 1,        // the unit becomes empty
14     T_UPDATE    = 2,        // update display
PhoneticConfigPhoneticConfig15     T_COMMIT    = 3,        // a unit is composed
16     T_CANDIDATE = 4,        // candidate must be fetched
17     T_NEXT      = 5,        // cursor focus moved out to next unit
18     T_NEXTSUBFWD= 6,        // move to the end of next unit's first sub-unit
19     T_PREVIOUS  = 7,        // cursor focus moved out to previous unit
20     T_SPLIT     = 8         // a sibling unit is created
21 };                          // no merge (we can add to sibling & clear self)
22 
23 typedef unsigned int KeyModifier;
24 enum {
25     K_CTRL              = 1,
26     K_ALT               = 2,
27     K_OPT               = 2,
28     K_COMMAND           = 4,
29     K_FUNCTIONKEYMASK   = 7,
30     K_SHIFT             = 8,
31     K_CAPSLOCK          = 16
32 };
33 
34 typedef int KeyCode;
35 enum {
36     K_ESC=27, K_SPACE=32, K_RETURN=13, K_TAB=9, K_DELETE=127, K_BACKSPACE=8,
37     K_UP=30, K_DOWN=31, K_LEFT=28, K_RIGHT=29,
38     K_HOME=1, K_END=4, K_PAGEUP=11, K_PAGEDOWN=12,
39 };
40 
41 typedef vector<string> CandidateList;
42 
43 class GrammarUnit;
44 
45 // if you pass data with TEvent, you have to manage ownership/casting yourself
46 class TEvent {
47 public:
48     TEvent(EventType t=T_IGNORE) { type=t; data=NULL; }
49     EventType type;
50     void *data;
51 };
52 
53 class GrammarUnit {
54 public:
55     virtual ~GrammarUnit() {}
56 
57     // data storage and representation ("Document" and "View")
58     virtual const string& code()=0;                 // code stiring
59     virtual const string& presentation()=0;         // displayed "presentation"
60     virtual bool vacant()=0;                        // if no code() available
61     virtual size_t width()=0;                       // measured in code points
62     virtual size_t cursor()     { return width(); } // cursor position
63     virtual size_t markFrom()   { return 0; }       // highlight startlo
64     virtual size_t markLength() { return 0; }       // highlight length
65 
66     // event dispatching ("Controller")
67     virtual const TEvent keyEvent(KeyCode k, KeyModifier m)=0;
68     virtual const CandidateList fetchCandidateList()=0;
69     virtual const TEvent cancelCandidate()=0;
70     virtual const TEvent chooseCandidate(size_t index, const string &item)=0;
71 };
72 
73 #endif
74