summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavi Artigas <xavierartigas@yahoo.es>2018-10-19 15:30:16 +0200
committerXavi Artigas <xavierartigas@yahoo.es>2018-10-19 15:30:42 +0200
commita040529352ca927329c557f9c1400c451752a87b (patch)
tree8d050f24ab4003c2a09fd16a6f51c827f7206be9
parenteb2cf481957fdace6bc6a30ea4af05e8e3c37b13 (diff)
csharp texteditor app: Revamping of this example
Summary: Added save and load buttons. Added information popups. Adapted to the style of the rest of the csharp examples. Reviewers: lauromoura, vitor.sousa, felipealmeida, ajwillia.ms Reviewed By: lauromoura Differential Revision: https://phab.enlightenment.org/D7173
-rw-r--r--apps/csharp/texteditor/src/texteditor_main.cs230
1 files changed, 151 insertions, 79 deletions
diff --git a/apps/csharp/texteditor/src/texteditor_main.cs b/apps/csharp/texteditor/src/texteditor_main.cs
index ac6d3f38..63e8e193 100644
--- a/apps/csharp/texteditor/src/texteditor_main.cs
+++ b/apps/csharp/texteditor/src/texteditor_main.cs
@@ -1,141 +1,213 @@
1/* Simple text editor with a main text box and a toolbar on top:
2 +vbox----------------------------------------+
3 | +hbox------------------------------------+ |
4 | | +btn-+ +btn-+ +btn-+ +box-----+ +btn-+ | |
5 | | |NEW | |SAVE| |LOAD| | spacer | |QUIT| | |
6 | | +----+ +----+ +----+ +--------+ +----+ | |
7 | +----------------------------------------+ |
8 | +text------------------------------------+ |
9 | | | |
10 | | | |
11 | | Main text box | |
12 | | | |
13 | | | |
14 | +----------------------------------------+ |
15 +--------------------------------------------+
16*/
17
1using System; 18using System;
2 19
3public class TextEditor : IDisposable 20public class TextEditor
4{ 21{
5 private efl.ui.IWin win; 22 private efl.ui.IWin win; // The main window
6 private efl.ui.IText editor; 23 private efl.ui.IText editorTextBox; // The main text entry
7 private efl.ui.IButton toolbarNew; 24 private efl.ui.IButton toolbarButtonNew; // The "New" button in the toolbar
25 private efl.ui.IButton toolbarButtonSave; // The "Save" button in the toolbar
26 private efl.ui.IButton toolbarButtonLoad; // The "Load" button in the toolbar
8 27
9 private bool edited = false; 28 private bool edited = false; // Document was edited since last save
10 29
30 // File to load and save is fixed since we do not use a file selection dialog
31 private readonly string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
32 "texteditor_example.txt");
11 33
34 // Quits the application
12 private void GUIQuitCb(object sender, EventArgs ea) 35 private void GUIQuitCb(object sender, EventArgs ea)
13 { 36 {
14 efl.ui.Config.Exit(); 37 efl.ui.Config.Exit();
15 } 38 }
16 39
40 // Enables or disables buttons on the toolbar as required
41 private void GUIToolbarRefresh()
42 {
43 // "New" is enabled if there is text in the text box
44 toolbarButtonNew.SetDisabled(string.IsNullOrEmpty(editorTextBox.GetText()));
45 // "Save" is enabled if the text has been modified since last save or load
46 toolbarButtonSave.SetDisabled(!edited);
47 // "Load" is enabled if there is a file to load
48 toolbarButtonLoad.SetDisabled(!System.IO.File.Exists(filename));
49 }
50
51 // Called when the text in the editor has changed
17 private void EditorChangedCb(object sender, EventArgs ea) 52 private void EditorChangedCb(object sender, EventArgs ea)
18 { 53 {
19 edited = true; 54 edited = true;
20 GUIToolbarRefresh(); 55 GUIToolbarRefresh();
21 } 56 }
22 57
23 private efl.ui.IButton GUIToolbarButtonAdd(efl.ui.IBox toolbar, string name, string iconName, EventHandler func) 58 // Shows a modal message popup with an "OK" button
59 private void ShowMessage(string message)
24 { 60 {
25 efl.ui.IButton button = new efl.ui.Button(toolbar); 61 new efl.ui.Popup_Alert_Text (win, (efl.ui.IPopup_Alert_Text epopup) => {
26 button.SetText(name); 62 epopup.SetText(message);
27 button.ClickedEvt += func; 63 epopup.SetExpandable(new eina.Size2D(200,200));
28 64 epopup.SetButton(efl.ui.Popup_Alert_Button.Positive, "OK", null);
29 toolbar.DoPack(button); 65 epopup.ButtonClickedEvt +=
30 66 (object sender, efl.ui.Popup_Alert.ButtonClickedEvt_Args ea) => {
31 efl.ui.IImage img = new efl.ui.Image(toolbar); 67 // Dismiss popup when the button is clicked
32 img.SetIcon(iconName); 68 ((efl.ui.IPopup_Alert_Text)sender).SetParent(null);
33 69 };
34 efl.Content.static_cast(button.GetPart("efl.content")).SetContent(img); 70 });
71 }
35 72
36 return button; 73 // Adds a button to the toolbar, with the given text, icon and click event handler
74 private efl.ui.IButton GUIToolbarButtonAdd(efl.ui.IBox toolbar, string name,
75 string iconName, EventHandler func)
76 {
77 return new efl.ui.Button(toolbar, (efl.ui.IButton ebutton) => {
78 ebutton.SetText(name);
79 ebutton.ClickedEvt += func;
80 ebutton.SetHintWeight(0, 1);
81 toolbar.DoPack(ebutton);
82
83 // Set the content of the button
84 efl.Content.static_cast(ebutton.GetPart("efl.content")).SetContent(
85 // Which is an image
86 new efl.ui.Image(toolbar, (efl.ui.IImage eimage) => {
87 eimage.SetIcon(iconName);
88 })
89 );
90 });
37 } 91 }
38 92
93 // Creates a new toolbar, with all its buttons
39 private void GUIToolbarSetup(efl.ui.IBox parent) 94 private void GUIToolbarSetup(efl.ui.IBox parent)
40 { 95 {
41 efl.ui.IBox bar = new efl.ui.Box(parent); 96 // Create a horizontal box container for the buttons
42 bar.SetHintWeight(1, 0); 97 efl.ui.IBox bar = new efl.ui.Box(parent, (efl.ui.IBox ebox) => {
43 bar.SetDirection(efl.ui.Dir.Horizontal); 98 // 0 vertical weight means that the toolbar will have the minimum height
44 parent.DoPack(bar); 99 // to accommodate all its buttons and not a pixel more. The rest of the
45 100 // space will be given to the other object in the parent container.
46 toolbarNew = GUIToolbarButtonAdd(bar, "New", "document-new", 101 ebox.SetHintWeight(1, 0);
47 (object sender, EventArgs ea) => 102 ebox.SetDirection(efl.ui.Dir.Horizontal);
48 { 103 parent.DoPack(ebox);
49 editor.SetText(""); 104 });
50 edited = false;
51 GUIToolbarRefresh();
52 });
53 105
54 // spacer box 106 // "New" button
107 toolbarButtonNew = GUIToolbarButtonAdd(bar, "New", "document-new",
108 (object sender, EventArgs ea) => {
109 // When this button is clicked, remove content and refresh toolbar
110 editorTextBox.SetText("");
111 GUIToolbarRefresh();
112 });
113
114 // "Save" button
115 toolbarButtonSave = GUIToolbarButtonAdd(bar, "Save", "document-save",
116 (object sender, EventArgs ea) => {
117 // When this button is clicked, try to save content and refresh toolbar
118 try {
119 System.IO.File.WriteAllText(filename, editorTextBox.GetText());
120 edited = false;
121 GUIToolbarRefresh();
122 ShowMessage("Saved!");
123 } catch (Exception e) {
124 // If something fails, show the error message
125 ShowMessage(e.Message);
126 }
127 });
128
129 // "Load" button
130 toolbarButtonLoad = GUIToolbarButtonAdd(bar, "Load", "document-open",
131 (object sender, EventArgs ea) => {
132 // When this button is clicked, try to load content and refresh toolbar
133 try {
134 editorTextBox.SetText(System.IO.File.ReadAllText(filename));
135 edited = false;
136 GUIToolbarRefresh();
137 ShowMessage("Loaded!");
138 } catch (Exception e) {
139 // If something fails, show the error message
140 ShowMessage(e.Message);
141 }
142 });
143
144 // Spacer box to use all available space not required by buttons
145 // (It has a default horizontal weight of 1, whereas all buttons have
146 // a horizontal weight of 0).
147 // As a result, it pushes the "Quit" button to the right margin and
148 // the rest to the left.
55 efl.ui.IBox box = new efl.ui.Box(parent); 149 efl.ui.IBox box = new efl.ui.Box(parent);
56 box.SetHintWeight(10, 0);
57 bar.DoPack(box); 150 bar.DoPack(box);
58 151
152 // "Quit" button
59 GUIToolbarButtonAdd(bar, "Quit", "application-exit", GUIQuitCb); 153 GUIToolbarButtonAdd(bar, "Quit", "application-exit", GUIQuitCb);
60
61 GUIToolbarRefresh();
62 }
63
64 private void GUIToolbarRefresh()
65 {
66 toolbarNew.SetDisabled(!edited);
67 } 154 }
68 155
156 // Builds the user interface for the text editor
69 public TextEditor() 157 public TextEditor()
70 { 158 {
71 win = new efl.ui.Win(null, (efl.ui.IWin ewin) => { 159 // Create a window and initialize it
72 ewin.SetWinType(efl.ui.Win_Type.Basic); 160 win = new efl.ui.Win(efl.App.GetLoopMain(), (efl.ui.IWin ewin) => {
73 ewin.SetText("Text Editor"); 161 ewin.SetText("Text Editor");
74 ewin.SetAutohide(true); 162 ewin.SetAutohide(true);
163 ewin.HideEvt += GUIQuitCb;
75 }); 164 });
76 165
77 // when the user clicks "close" on a window there is a request to hide 166 // Create a vertical box container
78 win.HideEvt += GUIQuitCb;
79
80 efl.ui.IBox box = new efl.ui.Box(win); 167 efl.ui.IBox box = new efl.ui.Box(win);
81 eina.Size2D sz;
82 sz.W = 360;
83 sz.H = 240;
84 box.SetHintMin(sz);
85
86 win.SetContent(box); 168 win.SetContent(box);
87 169
170 // Create the toolbar and add it to the box
88 GUIToolbarSetup(box); 171 GUIToolbarSetup(box);
89 172
90 editor = new efl.ui.Text(box); 173 // Create the main text entry
91 editor.SetFont("Mono", 14); 174 editorTextBox = new efl.ui.Text(box, (efl.ui.IText etext) => {
92 editor.SetMultiline(true); 175 etext.SetFont("Mono", 14);
93 editor.SetEditable(true); 176 etext.SetMultiline(true);
94 editor.SetScrollable(true); 177 etext.SetEditable(true);
95 editor.SetScrollable(true); 178 etext.SetScrollable(true);
96 editor.ChangedEvt += EditorChangedCb; 179 etext.SetHintMin(new eina.Size2D(360, 240));
97 editor.ChangedUserEvt += EditorChangedCb; 180 etext.ChangedEvt += EditorChangedCb;
98 181 etext.ChangedUserEvt += EditorChangedCb;
99 box.DoPack(editor); 182 box.DoPack(etext);
100 } 183 });
101
102 ~TextEditor()
103 {
104 Dispose(false);
105 }
106
107 protected void Dispose(bool disposing)
108 {
109 editor.Dispose();
110 toolbarNew.Dispose();
111 win.Dispose();
112 }
113 184
114 public void Dispose() 185 // Initial refresh of the toolbar buttons
115 { 186 GUIToolbarRefresh();
116 Dispose(true);
117 GC.SuppressFinalize(this);
118 } 187 }
119 188
189 // This method won't return until the application quits
120 public void Run() 190 public void Run()
121 { 191 {
122 // start main loop 192 // Start the EFL main loop
123 efl.ui.Config.Run(); 193 efl.ui.Config.Run();
124 } 194 }
125} 195}
126 196
127public class Example 197public class Example
128{ 198{
199#if WIN32
200 [STAThreadAttribute()]
201#endif
129 public static void Main() 202 public static void Main()
130 { 203 {
204 // Initialize EFL and all UI components
131 efl.All.Init(efl.Components.Ui); 205 efl.All.Init(efl.Components.Ui);
132 206
133 var textEditor = new TextEditor(); 207 var textEditor = new TextEditor();
134 textEditor.Run(); 208 textEditor.Run();
135 209
136 // dispose after quit 210 // Shutdown EFL
137 textEditor.Dispose();
138
139 efl.All.Shutdown(); 211 efl.All.Shutdown();
140 } 212 }
141} 213}