1 module widgets.editors;
2 
3 import dlangui;
4 
5 immutable testCode =
6 `
7 #!/usr/bin/env rdmd
8 // Computes average line length for standard input.
9 import std.stdio;
10 
11 void main()
12 {
13 ulong lines = 0;
14 double sumLength = 0;
15 foreach (line; stdin.byLine())
16 {
17     ++lines;
18     sumLength += line.length;
19 }
20 writeln("Average line length: ",
21         lines ? sumLength / lines : 0);
22 }
23 `;
24 
25 Widget createEditorSettingsControl(EditWidgetBase editor) {
26     HorizontalLayout res = new HorizontalLayout("editor_options");
27     res.addChild((new CheckBox("wantTabs", "wantTabs"d)).checked(editor.wantTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.wantTabs = checked; return true;}));
28     res.addChild((new CheckBox("useSpacesForTabs", "useSpacesForTabs"d)).checked(editor.useSpacesForTabs).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.useSpacesForTabs = checked; return true;}));
29     res.addChild((new CheckBox("readOnly", "readOnly"d)).checked(editor.readOnly).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.readOnly = checked; return true;}));
30     res.addChild((new CheckBox("showLineNumbers", "showLineNumbers"d)).checked(editor.showLineNumbers).addOnCheckChangeListener(delegate(Widget, bool checked) { editor.showLineNumbers = checked; return true;}));
31     res.addChild((new CheckBox("fixedFont", "fixedFont"d)).checked(editor.fontFamily == FontFamily.MonoSpace).addOnCheckChangeListener(delegate(Widget, bool checked) {
32         if (checked)
33             editor.fontFamily(FontFamily.MonoSpace).fontFace("Courier New");
34         else
35             editor.fontFamily(FontFamily.SansSerif).fontFace("Arial");
36         return true;
37     }));
38     res.addChild((new CheckBox("tabSize", "Tab size 8"d)).checked(editor.tabSize == 8).addOnCheckChangeListener(delegate(Widget, bool checked) {
39         if (checked)
40             editor.tabSize(8);
41         else
42             editor.tabSize(4);
43         return true;
44     }));
45     return res;
46 }
47 
48 class EditorsExample : VerticalLayout
49 {
50     this(string ID)
51     {
52         super(ID);
53 
54     // EditLine sample
55     addChild(new TextWidget(null, "EditLine: Single line editor"d));
56     EditLine editLine = new EditLine("editline1", "Single line editor sample text");
57     addChild(createEditorSettingsControl(editLine));
58     addChild(editLine);
59 
60     // EditBox sample
61     addChild(new TextWidget(null, "SourceEdit: multiline editor, for source code editing"d));
62 
63     SourceEdit editBox = new SourceEdit("editbox1");
64     editBox.text = UIString.fromRaw(testCode);
65     addChild(createEditorSettingsControl(editBox));
66     addChild(editBox);
67 
68     addChild(new TextWidget(null, "EditBox: additional view for the same content (split view testing)"d));
69     SourceEdit editBox2 = new SourceEdit("editbox2");
70     editBox2.content = editBox.content; // view the same content as first editbox
71     addChild(editBox2);
72     layoutHeight(FILL_PARENT).layoutWidth(FILL_PARENT);
73     }
74 }