sdk: const correctness
[sdk] / ecere / src / gui / Interface.ec
1 namespace gui;
2
3 import "Window"
4
5 // Key to character mapping
6 static byte characters[2][128] =
7 {
8    {
9       128,128,'1','2','3','4','5','6','7','8','9','0','-','=',128,128,
10       'q','w','e','r','t','y','u','i','o','p','[',']',128,128,'a','s',
11       'd','f','g','h','j','k','l',';','\'','`',128,'\\','z','x','c','v',
12       'b','n','m',',','.','/',128,'*',128,' ',128,128,128,128,128,128,
13       128,128,128,128,128,128,128,'7','8','9','-','4','5','6','+','1',
14       '2','3','0',128,128,128,128,128,128,128,128,128,128,128,128,128,
15       128,128,'/',128,128,128,128,128,128,128,128,128,128,128,128,128,
16       128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
17    },
18    {
19       128,128,'!','@','#','$','%','^','&','*','(',')','_','+',128,128,
20       'Q','W','E','R','T','Y','U','I','O','P','{','}',128,128,'A','S',
21       'D','F','G','H','J','K','L',':','"','~',128,'|','Z','X','C','V',
22       'B','N','M','<','>','?',128,'*',128,' ',128,128,128,128,128,128,
23       128,128,128,128,128,128,128,'7','8','9','-','4','5','6','+','1',
24       '2','3','0',128,128,128,128,128,128,128,128,128,128,128,128,128,
25       128,128,'/',128,128,128,128,128,128,128,128,128,128,128,128,128,
26       128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
27    }
28 };
29
30 public struct Joystick
31 {
32    int x,y,z;
33    int rx,ry,rz;
34    uint buttons;
35 //   int slider[2];
36 //   uint pov[4];
37 };
38
39 public class Interface
40 {
41 public:
42    class_data const char * name;
43
44    class_property const char * name
45    {
46       set { class_data(name) = value; }
47       get { return class_data(name); }
48    }
49
50    // --- User Interface System ---
51    virtual bool ::Initialize();
52    virtual void ::Terminate();
53    virtual bool ::ProcessInput(bool processAll);
54    virtual void ::Wait();
55    virtual void ::Lock(Window window);
56    virtual void ::Unlock(Window window);
57    virtual void ::SetTimerResolution(uint hertz);
58
59    virtual const char ** ::GraphicsDrivers(int * numDrivers);
60    virtual void ::EnsureFullScreen(bool * fullScreen);
61    virtual void ::GetCurrentMode(bool * fullScreen, Resolution * resolution, PixelFormat * colorDepth, int * refreshRate);
62    virtual bool ::ScreenMode(bool fullScreen, Resolution resolution, PixelFormat colorDepth, int refreshRate, bool * textMode);
63
64    // --- Window Creation ---
65    virtual void * ::CreateRootWindow(Window window);
66    virtual void ::DestroyRootWindow(Window window);
67
68    // --- Window manipulation ---
69    virtual void ::SetRootWindowCaption(Window window, const char * name);
70    virtual void ::PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize);
71    virtual void ::OffsetWindow(Window window, int * x, int * y);
72    virtual void ::UpdateRootWindow(Window window);
73    virtual void ::SetRootWindowState(Window window, WindowState state, bool visible);
74    virtual void ::ActivateRootWindow(Window window);
75    virtual void ::OrderRootWindow(Window window, bool topMost);
76    virtual void ::SetRootWindowColor(Window window);
77    virtual void ::FlashRootWindow(Window window);
78
79    // --- Mouse-based window movement ---
80    virtual void ::StartMoving(Window window, int x, int y, bool fromKeyBoard);
81    virtual void ::StopMoving(Window window);
82
83    // --- Mouse manipulation ---
84    virtual void ::GetMousePosition(int *x, int *y);
85    virtual void ::SetMousePosition(int x, int y);
86    virtual void ::SetMouseRange(Window window, Box box);
87    virtual void ::SetMouseCapture(Window window);
88
89    // --- Mouse cursor ---
90    virtual void ::SetMouseCursor(Window window, SystemCursor cursor);
91
92    // --- Caret manipulation ---
93    virtual void ::SetCaret(int caretX, int caretY, int size);
94
95    // --- Clipboard manipulation ---
96    virtual void ::ClearClipboard();
97    virtual bool ::AllocateClipboard(ClipBoard clipBoard, uint size);
98    virtual bool ::SaveClipboard(ClipBoard clipBoard);
99    virtual bool ::LoadClipboard(ClipBoard clipBoard);
100    virtual void ::UnloadClipboard(ClipBoard clipBoard);
101
102    // --- State based input ---
103    virtual bool ::AcquireInput(Window window, bool state);
104    virtual bool ::GetMouseState(MouseButtons * buttons, int * x, int * y);
105    virtual bool ::GetJoystickState(int device, Joystick joystick);
106    virtual bool ::GetKeyState(Key key);
107
108    virtual bool ::SetIcon(Window window, BitmapResource icon);
109    virtual void ::GetScreenArea(Window window, Box box);
110
111    Key ::GetExtendedKey(Key key)
112    {
113       switch(key)
114       {
115          case keyPadHome:     return home;
116          case keyPadUp:       return up;
117          case keyPadPageUp:   return pageUp;
118          case keyPadLeft:     return left;
119          case keyPadRight:    return right;
120          case keyPadEnd:      return end;
121          case keyPadDown:     return down;
122          case keyPadPageDown: return pageDown;
123          case keyPadInsert:   return insert;
124          case keyPadDelete:   return del;
125          case enter:          return keyPadEnter;
126          case leftControl:    return rightControl;
127          case leftAlt:        return rightAlt;
128          case slash:          return keyPadSlash;
129          case numLock:        return numLock;
130          case scrollLock:     return pauseBreak;
131       }
132       return 0;
133    }
134
135    void ::WriteKeyedTextDisabled(Surface surface, int x, int y, const char *text, Key hotKey, bool disabled)
136    {
137       if(text)
138       {
139          char * tab = strstr(text, "\t");
140          bool opacity = surface.textOpacity;
141          if(guiApp.textMode)
142          {
143             disabled = false;
144             surface.SetForeground(guiApp.currentSkin.disabledFrontColor);
145          }
146          if(tab)
147          {
148             int len = strlen(tab+1),tw;
149             surface.TextExtent(tab+1, strlen(tab + 1), &tw, null);
150
151             if(disabled)
152             {
153                surface.SetForeground(guiApp.currentSkin.disabledBackColor);
154                surface.WriteText(x+1,y+1, text,tab-text);
155                surface.WriteText(surface.width - 3 - tw - 12 + 1, y + 1,tab+1,len);
156                surface.SetForeground(guiApp.currentSkin.disabledFrontColor );
157
158                surface.TextOpacity(false);
159             }
160             surface.TextOpacity(false);
161             surface.WriteText(x,y, text,tab-text);
162             surface.WriteText(surface.width - 3 - tw - 12, y,tab+1,len);
163             surface.TextOpacity(opacity);
164          }
165          else
166          {
167             if(disabled)
168             {
169                surface.SetForeground(guiApp.currentSkin.disabledBackColor);
170                surface.WriteText(x+1,y+1, text,strlen(text));
171                surface.SetForeground(guiApp.currentSkin.disabledFrontColor);
172                surface.TextOpacity(false);
173             }
174             surface.WriteText(x,y, text, strlen(text));
175             surface.TextOpacity(opacity);
176          }
177          if(hotKey)
178          {
179             char ch;
180             int c;
181             bool firstLetter;
182             char * tab = strchr(text, '\t');
183
184             // Look for an uppercase letter starting a word
185             firstLetter = true;
186             ch = isalpha(characters[1][hotKey.code]) ? characters[1][hotKey.code] : characters[0][hotKey.code];
187             for(c = 0; text[c] && text[c] != '\t'; c++)
188             {
189                if(toupper(text[c]) == ch && firstLetter)
190                   break;
191                else if(text[c] == ' ')
192                   firstLetter = true;
193                else
194                   firstLetter = false;
195             }
196
197             // Look for an uppercase letter anywhere
198             if(!text[c] || text[c] == '\t')
199             {
200                char * string = strchr(text, ch);
201                if(string && (!tab || string < tab)) c = string - text;
202             }
203
204             // Look for a lowercase letter starting a word
205             if(!text[c] || text[c] == '\t')
206             {
207                firstLetter = true;
208                ch = characters[0][hotKey.code];
209                for(c = 0; text[c] && text[c] != '\t'; c++)
210                {
211                   if(tolower(text[c]) == ch && firstLetter)
212                      break;
213                   else if(text[c] == ' ')
214                      firstLetter = true;
215                   else
216                      firstLetter = false;
217                }
218             }
219
220             // Look for a lowercase letter anywhere
221             if(!text[c] || text[c] == '\t')
222             {
223                char * string = strchr(text, characters[0][hotKey.code]);
224                if(string && (!tab || string < tab)) c = string - text;
225             }
226
227             if(text[c] && text[c] != '\t')
228             {
229                int tw, cw, ch;
230                surface.TextExtent(text, (int)c, &tw, null);
231                surface.TextExtent(text + c, 1, &cw, &ch);
232                if(!guiApp.textMode)
233                {
234                   if(disabled)
235                   {
236                      surface.SetForeground(guiApp.currentSkin.disabledBackColor);
237                      surface.HLine(x + tw + 1, x + tw + cw-1 + 1, y + ch-1 + 1);
238                      surface.SetForeground(guiApp.currentSkin.disabledFrontColor);
239                   }
240                   surface.HLine(x + tw, x + tw + cw-1, y + ch-1);
241                }
242                else
243                {
244                   surface.SetForeground(Color { 255,255,85 });
245                   surface.WriteText(x + c * textCellW, y, text + c, 1);
246                }
247             }
248          }
249       }
250    }
251
252    void ::WriteKeyedText(Surface surface, int x, int y, const char *text, Key hotKey)
253    {
254       WriteKeyedTextDisabled(surface, x, y, text, hotKey, false);
255    }
256
257    char ::TranslateKey(Key key, bool shift)
258    {
259       int code = (int)key.code;
260       return (code <= 127) ? characters[shift][code] : 0;
261    }
262 };