summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavi Artigas <xavierartigas@yahoo.es>2020-01-17 12:15:37 +0100
committerXavi Artigas <xavierartigas@yahoo.es>2020-02-20 19:27:50 +0100
commit3ae154e4de943a88ed20c947126d4365e781034f (patch)
tree4f0d6dda934bbe7a67099752ff5b2eda6b1a19f9
parentf835d4562212c3eba9025a74845128d451fffffc (diff)
-rw-r--r--apps/csharp/richeditor/meson.build10
-rw-r--r--apps/csharp/richeditor/src/meson.build12
-rw-r--r--apps/csharp/richeditor/src/richeditor_main.cs247
3 files changed, 269 insertions, 0 deletions
diff --git a/apps/csharp/richeditor/meson.build b/apps/csharp/richeditor/meson.build
new file mode 100644
index 00000000..e68c46ec
--- /dev/null
+++ b/apps/csharp/richeditor/meson.build
@@ -0,0 +1,10 @@
1project(
2 'efl-example-richeditor', 'cs',
3 version : '0.0.1',
4 meson_version : '>= 0.38.0')
5
6efl_mono = dependency('efl-mono', version : '>=1.20.99')
7efl_mono_libs = efl_mono.get_pkgconfig_variable('mono_libs')
8
9subdir('src')
10
diff --git a/apps/csharp/richeditor/src/meson.build b/apps/csharp/richeditor/src/meson.build
new file mode 100644
index 00000000..936361f9
--- /dev/null
+++ b/apps/csharp/richeditor/src/meson.build
@@ -0,0 +1,12 @@
1src = files([
2 'richeditor_main.cs',
3])
4
5deps = [efl_mono]
6
7executable('efl_example_richeditor', src,
8 dependencies : deps,
9 cs_args : efl_mono_libs,
10 install : true
11)
12
diff --git a/apps/csharp/richeditor/src/richeditor_main.cs b/apps/csharp/richeditor/src/richeditor_main.cs
new file mode 100644
index 00000000..7a343c1d
--- /dev/null
+++ b/apps/csharp/richeditor/src/richeditor_main.cs
@@ -0,0 +1,247 @@
1/* Simple text editor with a main text box and a toolbar on top:
2 +vbox-----------------------------------------------------------+
3 | +hbox-------------------------------------------------------+ |
4 | | +btn-+ +btn-+ +btn-+ +sep+ +btn---------+ +box---+ +btn-+ | |
5 | | |NEW | |SAVE| |LOAD| | | |Rich Buttons| |spacer| |QUIT| | |
6 | | +----+ +----+ +----+ +---+ +------------+ +------+ +----+ | |
7 | +-----------------------------------------------------------+ |
8 | +text-------------------------------------------------------+ |
9 | | | |
10 | | | |
11 | | Main text box | |
12 | | | |
13 | | | |
14 | +-----------------------------------------------------------+ |
15 +---------------------------------------------------------------+
16*/
17
18using System;
19using System.Collections.Generic;
20
21public class RichEditor : Efl.Csharp.Application
22{
23 private Efl.Ui.Win win; // The main window
24 private Efl.Ui.Textbox editorTextBox; // The main text entry
25 private Efl.Ui.Button toolbarButtonNew; // The "New" button in the toolbar
26 private Efl.Ui.Button toolbarButtonSave; // The "Save" button in the toolbar
27 private Efl.Ui.Button toolbarButtonLoad; // The "Load" button in the toolbar
28 private List<Efl.Ui.Button> richToolbarButtons; // List of rich buttons in the toolbar
29
30 private bool edited = false; // Document was edited since last save
31
32 // File to load and save is fixed since we do not use a file selection dialog
33 private readonly string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
34 "richeditor_example.txt");
35
36 // Quits the application
37 private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea)
38 {
39 if (ea.Arg == false)
40 Efl.App.AppMain.Quit(0);
41 }
42
43 // Enables or disables buttons on the toolbar as required
44 private void GUIToolbarRefresh()
45 {
46 // "New" is enabled if there is text in the text box
47 toolbarButtonNew.Disabled = string.IsNullOrEmpty(editorTextBox.Text);
48 // "Save" is enabled if the text has been modified since last save or load
49 toolbarButtonSave.Disabled = !edited;
50 // "Load" is enabled if there is a file to load
51 toolbarButtonLoad.Disabled = !System.IO.File.Exists(filename);
52 }
53
54 // Called when the text in the editor has changed
55 private void EditorChangedCb(object sender, EventArgs ea)
56 {
57 edited = true;
58 GUIToolbarRefresh();
59 }
60
61 // Called when there is no longer text selected in the editor, or the opposite
62 private void SelectionChangedCb(object sender, Efl.TextInteractiveHaveSelectionChangedEventArgs ea)
63 {
64 // The rich buttons are enabled if there is selected text
65 foreach (var b in richToolbarButtons) {
66 b.Disabled = !ea.Arg;
67 }
68 }
69
70 // Shows a modal message popup with an "OK" button
71 private void ShowMessage(string message)
72 {
73 var popup = new Efl.Ui.AlertPopup (win);
74 popup.ScrollableText = message;
75 popup.HintSizeMax = new Eina.Size2D(200, 200);
76 popup.SetButton(Efl.Ui.AlertPopupButton.Positive, "OK", null);
77 popup.ButtonClickedEvent +=
78 (object sender, Efl.Ui.AlertPopupButtonClickedEventArgs ea) => {
79 // Dismiss popup when the button is clicked
80 ((Efl.Ui.AlertPopup)sender).Del();
81 };
82 }
83
84 // Adds a button to the toolbar, with the given text, icon and click event handler
85 private Efl.Ui.Button GUIToolbarButtonAdd(Efl.Ui.Box toolbar, string name,
86 string iconName, EventHandler<Efl.Input.ClickableClickedEventArgs> func)
87 {
88 var button = new Efl.Ui.Button(toolbar);
89 button.Text = name;
90 button.ClickedEvent += func;
91 button.HintWeight = (0, 1);
92
93 // Set the content of the button, which is an image
94 var image = new Efl.Ui.Image(toolbar);
95 image.Icon = iconName;
96 button.Content = image;
97
98 toolbar.Pack(button);
99 return button;
100 }
101
102 // Creates a new toolbar, with all its buttons
103 private void GUIToolbarSetup(Efl.Ui.Box parent)
104 {
105 // Create a horizontal box container for the buttons
106 var bar = new Efl.Ui.Box(parent);
107 // 0 vertical weight means that the toolbar will have the minimum height
108 // to accommodate all its buttons and not a pixel more. The rest of the
109 // space will be given to the other object in the parent container.
110 bar.HintWeight = (1, 0);
111 bar.Orientation = Efl.Ui.LayoutOrientation.Horizontal;
112 parent.Pack(bar);
113
114 // "New" button
115 toolbarButtonNew = GUIToolbarButtonAdd(bar, "New", "document-new",
116 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
117 // When this button is clicked, remove content and refresh toolbar
118 editorTextBox.Text = "";
119 GUIToolbarRefresh();
120 });
121
122 // "Save" button
123 toolbarButtonSave = GUIToolbarButtonAdd(bar, "Save", "document-save",
124 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
125 // When this button is clicked, try to save content and refresh toolbar
126 try {
127 System.IO.File.WriteAllText(filename, editorTextBox.Text);
128 edited = false;
129 GUIToolbarRefresh();
130 ShowMessage("Saved!");
131 } catch (Exception e) {
132 // If something fails, show the error message
133 ShowMessage(e.Message);
134 }
135 });
136
137 // "Load" button
138 toolbarButtonLoad = GUIToolbarButtonAdd(bar, "Load", "document-open",
139 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
140 // When this button is clicked, try to load content and refresh toolbar
141 try {
142 editorTextBox.Text = System.IO.File.ReadAllText(filename);
143 edited = false;
144 GUIToolbarRefresh();
145 ShowMessage("Loaded!");
146 } catch (Exception e) {
147 // If something fails, show the error message
148 ShowMessage(e.Message);
149 }
150 });
151
152 var sep = new Efl.Ui.Separator(bar,Efl.Ui.LayoutOrientation.Vertical);
153 sep.HintWeight = (0, 1);
154 bar.Pack(sep);
155
156 richToolbarButtons = new List<Efl.Ui.Button>();
157 // "Bold" button
158 richToolbarButtons.Add(GUIToolbarButtonAdd(bar, "", "format-text-bold",
159 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
160 Efl.Text.Cursor start, end;
161 (start, end) = editorTextBox.SelectionCursors;
162 Efl.Text.Formatter.InsertAttribute(start, end, "font_weight=bold");
163 }));
164
165 // "Italic" button
166 richToolbarButtons.Add(GUIToolbarButtonAdd(bar, "", "format-text-italic",
167 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
168 Efl.Text.Cursor start, end;
169 (start, end) = editorTextBox.SelectionCursors;
170 Efl.Text.Formatter.InsertAttribute(start, end, "font_style=italic");
171 }));
172
173 // "Underline" button
174 richToolbarButtons.Add(GUIToolbarButtonAdd(bar, "", "format-text-underline",
175 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
176 Efl.Text.Cursor start, end;
177 (start, end) = editorTextBox.SelectionCursors;
178 Efl.Text.Formatter.InsertAttribute(start, end, "underline=single underline_color=white");
179 }));
180
181 // "Strikethrough" button
182 richToolbarButtons.Add(GUIToolbarButtonAdd(bar, "", "format-text-strikethrough",
183 (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
184 Efl.Text.Cursor start, end;
185 (start, end) = editorTextBox.SelectionCursors;
186 Efl.Text.Formatter.InsertAttribute(start, end, "strikethrough=on strikethrough_color=white");
187 }));
188
189 // Spacer box to use all available space not required by buttons
190 // (It has a default horizontal weight of 1, whereas all buttons have
191 // a horizontal weight of 0).
192 // As a result, it pushes the "Quit" button to the right margin and
193 // the rest to the left.
194 Efl.Ui.Box box = new Efl.Ui.Box(parent);
195 bar.Pack(box);
196
197 // "Quit" button
198 GUIToolbarButtonAdd(bar, "Quit", "application-exit",
199 (object sender, Efl.Input.ClickableClickedEventArgs e) => Efl.App.AppMain.Quit(0) );
200 }
201
202 // Builds the user interface for the text editor
203 protected override void OnInitialize(string[] args)
204 {
205 // Create a window and initialize it
206 win = new Efl.Ui.Win(parent: Efl.App.AppMain);
207 win.Text = "Rich Text Editor";
208 win.Autohide = true;
209 win.VisibilityChangedEvent += GUIQuitCb;
210
211 // Create a vertical box container
212 Efl.Ui.Box box = new Efl.Ui.Box(win);
213 win.Content = box;
214
215 // Create the toolbar and add it to the box
216 GUIToolbarSetup(box);
217
218 // Create the main text entry
219 editorTextBox = new Efl.Ui.Textbox(box);
220 editorTextBox.FontFamily = "Mono";
221 editorTextBox.FontSize = 14;
222 editorTextBox.Multiline = true;
223 editorTextBox.Editable = true;
224 editorTextBox.Scrollable = true;
225 editorTextBox.HintSizeMin = new Eina.Size2D(480, 240);
226 editorTextBox.ChangedEvent += EditorChangedCb;
227 editorTextBox.ChangedUserEvent += EditorChangedCb;
228 editorTextBox.HaveSelectionChangedEvent += SelectionChangedCb;
229 box.Pack(editorTextBox);
230
231 // Initial refresh of the toolbar buttons
232 GUIToolbarRefresh();
233 }
234}
235
236public class Example
237{
238#if WIN32
239 [STAThreadAttribute()]
240#endif
241 public static void Main()
242 {
243 RichEditor editor = new RichEditor();
244 editor.Launch();
245 }
246}
247