1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* Simple text editor with a main text box and a toolbar on top:
+vbox----------------------------------------+
| +hbox------------------------------------+ |
| | +btn-+ +btn-+ +btn-+ +box-----+ +btn-+ | |
| | |NEW | |SAVE| |LOAD| | spacer | |QUIT| | |
| | +----+ +----+ +----+ +--------+ +----+ | |
| +----------------------------------------+ |
| +text------------------------------------+ |
| | | |
| | | |
| | Main text box | |
| | | |
| | | |
| +----------------------------------------+ |
+--------------------------------------------+
*/
using System;
public class TextEditor : Efl.Csharp.Application
{
private Efl.Ui.Win win; // The main window
private Efl.Ui.Text editorTextBox; // The main text entry
private Efl.Ui.Button toolbarButtonNew; // The "New" button in the toolbar
private Efl.Ui.Button toolbarButtonSave; // The "Save" button in the toolbar
private Efl.Ui.Button toolbarButtonLoad; // The "Load" button in the toolbar
private bool edited = false; // Document was edited since last save
// File to load and save is fixed since we do not use a file selection dialog
private readonly string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
"texteditor_example.txt");
// Quits the application
private void GUIQuitCb(object sender, Efl.Gfx.IEntityVisibilityChangedEvt_Args ea)
{
if (ea.arg == false)
Efl.App.AppMain.Quit(0);
}
// Enables or disables buttons on the toolbar as required
private void GUIToolbarRefresh()
{
// "New" is enabled if there is text in the text box
toolbarButtonNew.SetDisabled(string.IsNullOrEmpty(editorTextBox.GetText()));
// "Save" is enabled if the text has been modified since last save or load
toolbarButtonSave.SetDisabled(!edited);
// "Load" is enabled if there is a file to load
toolbarButtonLoad.SetDisabled(!System.IO.File.Exists(filename));
}
// Called when the text in the editor has changed
private void EditorChangedCb(object sender, EventArgs ea)
{
edited = true;
GUIToolbarRefresh();
}
// Shows a modal message popup with an "OK" button
private void ShowMessage(string message)
{
var popup = new Efl.Ui.AlertPopup (win);
popup.SetScrollableText(message);
popup.SetHintSizeMax(new Eina.Size2D(200, 200));
popup.SetButton(Efl.Ui.AlertPopupButton.Positive, "OK", null);
popup.ButtonClickedEvt +=
(object sender, Efl.Ui.AlertPopupButtonClickedEvt_Args ea) => {
// Dismiss popup when the button is clicked
((Efl.Ui.AlertPopup)sender).SetParent(null);
};
}
// Adds a button to the toolbar, with the given text, icon and click event handler
private Efl.Ui.Button GUIToolbarButtonAdd(Efl.Ui.Box toolbar, string name,
string iconName, EventHandler<Efl.Input.IClickableClickedEvt_Args> func)
{
var button = new Efl.Ui.Button(toolbar);
button.SetText(name);
button.ClickedEvt += func;
button.SetHintWeight(0, 1);
// Set the content of the button, which is an image
var image = new Efl.Ui.Image(toolbar);
image.SetIcon(iconName);
button.SetContent(image);
toolbar.Pack(button);
return button;
}
// Creates a new toolbar, with all its buttons
private void GUIToolbarSetup(Efl.Ui.Box parent)
{
// Create a horizontal box container for the buttons
var bar = new Efl.Ui.Box(parent);
// 0 vertical weight means that the toolbar will have the minimum height
// to accommodate all its buttons and not a pixel more. The rest of the
// space will be given to the other object in the parent container.
bar.SetHintWeight(1, 0);
bar.SetOrientation(Efl.Ui.LayoutOrientation.Horizontal);
parent.Pack(bar);
// "New" button
toolbarButtonNew = GUIToolbarButtonAdd(bar, "New", "document-new",
(object sender, Efl.Input.IClickableClickedEvt_Args ea) => {
// When this button is clicked, remove content and refresh toolbar
editorTextBox.SetText("");
GUIToolbarRefresh();
});
// "Save" button
toolbarButtonSave = GUIToolbarButtonAdd(bar, "Save", "document-save",
(object sender, Efl.Input.IClickableClickedEvt_Args ea) => {
// When this button is clicked, try to save content and refresh toolbar
try {
System.IO.File.WriteAllText(filename, editorTextBox.GetText());
edited = false;
GUIToolbarRefresh();
ShowMessage("Saved!");
} catch (Exception e) {
// If something fails, show the error message
ShowMessage(e.Message);
}
});
// "Load" button
toolbarButtonLoad = GUIToolbarButtonAdd(bar, "Load", "document-open",
(object sender, Efl.Input.IClickableClickedEvt_Args ea) => {
// When this button is clicked, try to load content and refresh toolbar
try {
editorTextBox.SetText(System.IO.File.ReadAllText(filename));
edited = false;
GUIToolbarRefresh();
ShowMessage("Loaded!");
} catch (Exception e) {
// If something fails, show the error message
ShowMessage(e.Message);
}
});
// Spacer box to use all available space not required by buttons
// (It has a default horizontal weight of 1, whereas all buttons have
// a horizontal weight of 0).
// As a result, it pushes the "Quit" button to the right margin and
// the rest to the left.
Efl.Ui.Box box = new Efl.Ui.Box(parent);
bar.Pack(box);
// "Quit" button
GUIToolbarButtonAdd(bar, "Quit", "application-exit", (object sender, Efl.Input.IClickableClickedEvt_Args e) => { Efl.Ui.Config.Exit(); } );
}
// Builds the user interface for the text editor
protected override void OnInitialize(string[] args)
{
// Create a window and initialize it
win = new Efl.Ui.Win(parent: Efl.App.AppMain);
win.SetText("Text Editor");
win.SetAutohide(true);
win.VisibilityChangedEvt += GUIQuitCb;
// Create a vertical box container
Efl.Ui.Box box = new Efl.Ui.Box(win);
win.SetContent(box);
// Create the toolbar and add it to the box
GUIToolbarSetup(box);
// Create the main text entry
editorTextBox = new Efl.Ui.Text(box);
editorTextBox.SetFont("Mono", 14);
editorTextBox.Multiline = true;
editorTextBox.Editable = true;
editorTextBox.Scrollable = true;
editorTextBox.SetHintSizeMin(new Eina.Size2D(360, 240));
editorTextBox.ChangedEvt += EditorChangedCb;
editorTextBox.ChangedUserEvt += EditorChangedCb;
box.Pack(editorTextBox);
// Initial refresh of the toolbar buttons
GUIToolbarRefresh();
}
}
public class Example
{
#if WIN32
[STAThreadAttribute()]
#endif
public static void Main()
{
TextEditor editor = new TextEditor();
editor.Launch();
}
}
|