ide/Global Settings: Initial font selector (Windows support)
[sdk] / ide / src / dialogs / FontPicker.ec
1 import "ide"
2
3 SyntaxColorScheme colorScheme
4 {
5    keywordColors = [ skyBlue, skyBlue ];
6    commentColor = Color { 125, 125, 125 };
7    charLiteralColor = Color { 245, 50, 245 };
8    stringLiteralColor = Color { 245, 50, 245 };
9    preprocessorColor = { 120, 220, 140 };
10    numberColor = Color {   0, 192, 192 };
11 };
12
13 Array<float> sizes { [ 6, 7, 8.25f, 9, 10, 10.5f, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 26, 28, 30, 32, 36, 40, 44, 48, 54, 60, 66, 72, 80, 88, 96 ] };
14
15 Array<const String> fonts
16 { [
17    "Bitstream Vera Sans Mono",
18    "Consolas",
19    "Courier New",
20    "DejaVu Sans Mono",
21    "Lucida Console",
22    "Monaco",
23    "Monoid",
24    "OCR-A II",
25    "OCR B MT"
26
27    // "Terminal"
28 ] };
29
30 const String sampleText = //"The Quick Brown Fox Jumps Over The Lazy Dog";
31    "import \"ecere\"\n"
32    "\n"
33    "class HelloForm : Window\n"
34    "{\n"
35    "   caption = $\"My First eC Application\";\n"
36    "   borderStyle = sizable;\n"
37    "   size = { 280, 100 };\n"
38    "   hasClose = true;\n"
39    "   displayDriver = \"OpenGL\";\n"
40    "\n"
41    "   bool OnKeyDown(Key key, unichar ch)\n"
42    "   {\n"
43    "\n"
44    "      return true;\n"
45    "   }\n"
46    "\n"
47    "   Label label\n"
48    "   {\n"
49    "      this, position = { 10, 10 }, font = { \"Arial\", 30 },\n"
50    "      caption = $\"Hello, World!!\"\n"
51    "   };\n"
52    "};\n"
53    "\n"
54    "HelloForm hello { };\n";
55
56 Array<FontResource> fontResources { };
57
58 class DisplayedFace : FontResource
59 {
60    void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, Alignment alignment, DataDisplayFlags displayFlags)
61    {
62       surface.font = font;
63       faceName.OnDisplay(surface, x, y, width, fieldData, alignment, displayFlags);
64    }
65 };
66
67 class FontPicker : Window
68 {
69    opacity = 0;
70    /*
71    background = formColor;
72    caption = $"Font Picker";
73    borderStyle = sizable;
74    hasMaximize = true;
75    hasMinimize = true;
76    hasClose = true;
77    size = { 664, 426 };
78    anchor = { horz = 9, vert = -26 };
79    */
80    virtual bool Window::NotifyChanged();
81
82    property float fontSize
83    {
84       get { return curFont.size; }
85    }
86    property const String faceName
87    {
88       get { return curFont.faceName; }
89    }
90
91    FontResource curFont { "Consolas", 12};
92    DataField dfSize { class(float) };
93    DropBox dbSize
94    {
95       this, size = { 56, 24 }, anchor = { left = 4, bottom = 4 };
96
97       bool NotifySelect(DropBox dropBox, DataRow row, Modifiers mods)
98       {
99          curFont.size = row.GetData(dfSize);
100          for(f : fontResources)
101          {
102             RemoveResource(f);
103             f.size = Min(20, curFont.size);
104             AddResource(f);
105          }
106          listFonts.font = fontResources[0];
107          OnLoadGraphics();
108          listFonts.OnLoadGraphics();
109          listFonts.size = listFonts.size; // Fix for scrollbar?
110          listFonts.Update(null);
111          sample.font = curFont;
112
113          modifiedDocument = true;
114          NotifyChanged(master);
115          return true;
116       }
117    };
118    Button cbFixedPitch
119    {
120       this, isCheckbox = true, caption = $"Fixed pitch only", anchor = { left = 68, bottom = 8 };
121       checked = true;
122
123       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
124       {
125          ideSettings.showFixedPitchFontsOnly = button.checked;
126          OnDestroy();
127          OnCreate();
128          return true;
129       }
130    };
131    EditBox sample
132    {
133       this, anchor = { left = 0.439024, top = 16, right = 4, bottom = 8 }, font = curFont;
134       textVertScroll = true;
135       hasVertScroll = true;
136       hasHorzScroll = true;
137       freeCaret = true;
138
139       syntaxHighlighting = true;
140       syntaxColorScheme = colorScheme;
141
142       foreground = ivory;
143       selectionText = Color { 30, 40, 50 };
144       background = black;
145       selectionColor = lightYellow;
146       multiLine = true;
147       contents = sampleText;
148    };
149    ListBox listFonts
150    {
151       this, anchor = { left = 4, top = 16, right = 0.591463, bottom = 30 }, alwaysHighLight = true;
152       hasVertScroll = true;
153       dontHideScroll = true;
154
155       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
156       {
157          if(row)
158          {
159             FontResource fr = row.GetData(dfFace);
160             curFont.faceName = fr.faceName;
161
162             modifiedDocument = true;
163             NotifyChanged(master);
164          }
165          sample.font = curFont;
166          return true;
167       }
168    };
169    DataField dfFace { class(DisplayedFace) };
170
171    bool OnLoadGraphics()
172    {
173       int maxH = 12 + 5;
174       float size = dbSize.GetData(dfSize);
175       int maxSize = (int)(size * 96 / 72 * 1.5);
176       for(f : fontResources)
177       {
178          Font font = f.font;
179          int h;
180          // const String n = f.faceName;
181          displaySystem.FontExtent(font, "W", 1, null, &h);
182          h += 5;
183
184          h = font.ascent + font.descent + 5;
185          if(h > maxSize) h = maxSize;
186          maxH = Max(maxH, h);
187       }
188       listFonts.rowHeight = maxH;
189       return true;
190    }
191
192    FontPicker()
193    {
194       dbSize.AddField(dfSize);
195       for(s : sizes)
196       {
197          DataRow row = dbSize.AddRow();
198          row.SetData(dfSize, s);
199          if(s == curFont.size)
200             dbSize.currentRow = row;
201       }
202       listFonts.AddField(dfFace);
203    }
204
205    void OnDestroy()
206    {
207       for(f : fontResources)
208          RemoveResource(f);
209       listFonts.Clear();
210       fontResources.RemoveAll();
211    }
212
213    bool OnCreate()
214    {
215       Map<String, FontInfo> fonts = ListAvailableFonts();
216       DataRow sRow;
217       float fontSize;
218       bool fixedPitchOnly = ideSettings.showFixedPitchFontsOnly;
219
220       curFont.faceName = ideSettings.codeEditorFont;
221       curFont.size = fontSize = ideSettings.codeEditorFontSize;
222
223       cbFixedPitch.checked = fixedPitchOnly;
224       fontResources.minAllocSize = fonts.GetCount();
225
226       for(sRow = dbSize.firstRow; sRow; sRow = sRow.next)
227       {
228          float s = sRow.GetData(dfSize);
229          if(fontSize - s < 0.5 || s > fontSize)
230          {
231             dbSize.currentRow = sRow;
232             break;
233          }
234       }
235
236       for(f : fonts; f.defaultOrAnsiCharSet && (!fixedPitchOnly || f.fixedPitch))
237       {
238          FontResource fr { &f, curFont.size, window = this };
239          DataRow row = listFonts.AddRow();
240          incref fr;
241          row.SetData(dfFace, fr);
242          if(!strcmp(&f, curFont.faceName))
243             listFonts.currentRow = row;
244          fontResources.Add(fr);
245       }
246       delete fonts;
247       return true;
248    }
249 }
250
251 //FontPicker fontPicker { };
252
253 /*
254 Color selectionColor = lightYellow;
255 Color selectionText = Color { 30, 40, 50 };
256 Color viewsBackground = Color { 30, 40, 50 };
257 Color viewsText = lightGray;
258 Color outputBackground = black;
259 Color outputText = lime;
260 Color projectViewBackground = Color { 30, 40, 50 };
261 Color projectViewText = lightGray;
262 Color codeEditorBG = black;
263 Color codeEditorFG = ivory;
264 Color marginColor = Color {24, 24, 24};
265 Color selectedMarginColor = Color {64, 64, 64};
266 Color lineNumbersColor = Color {160, 160, 160};
267 */