2018-10-19 06:30:16 -07:00
|
|
|
/* 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 | |
|
|
|
|
| | | |
|
|
|
|
| | | |
|
|
|
|
| +----------------------------------------+ |
|
|
|
|
+--------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2018-02-22 09:44:55 -08:00
|
|
|
using System;
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
public class TextEditor
|
2018-02-22 09:44:55 -08:00
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
private efl.ui.IWin win; // The main window
|
|
|
|
private efl.ui.IText editorTextBox; // The main text entry
|
|
|
|
private efl.ui.IButton toolbarButtonNew; // The "New" button in the toolbar
|
|
|
|
private efl.ui.IButton toolbarButtonSave; // The "Save" button in the toolbar
|
|
|
|
private efl.ui.IButton toolbarButtonLoad; // The "Load" button in the toolbar
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
private bool edited = false; // Document was edited since last save
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// 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");
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Quits the application
|
2018-02-22 09:44:55 -08:00
|
|
|
private void GUIQuitCb(object sender, EventArgs ea)
|
|
|
|
{
|
|
|
|
efl.ui.Config.Exit();
|
|
|
|
}
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// 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
|
2018-02-22 09:44:55 -08:00
|
|
|
private void EditorChangedCb(object sender, EventArgs ea)
|
|
|
|
{
|
|
|
|
edited = true;
|
|
|
|
GUIToolbarRefresh();
|
|
|
|
}
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Shows a modal message popup with an "OK" button
|
|
|
|
private void ShowMessage(string message)
|
2018-02-22 09:44:55 -08:00
|
|
|
{
|
2018-11-16 02:50:50 -08:00
|
|
|
new efl.ui.Text_Alert_Popup (win, (efl.ui.IText_Alert_Popup epopup) => {
|
2018-10-19 06:30:16 -07:00
|
|
|
epopup.SetText(message);
|
|
|
|
epopup.SetExpandable(new eina.Size2D(200,200));
|
2018-11-16 02:50:50 -08:00
|
|
|
epopup.SetButton(efl.ui.Alert_Popup_Button.Positive, "OK", null);
|
2018-10-19 06:30:16 -07:00
|
|
|
epopup.ButtonClickedEvt +=
|
2018-11-16 02:50:50 -08:00
|
|
|
(object sender, efl.ui.Alert_Popup.ButtonClickedEvt_Args ea) => {
|
2018-10-19 06:30:16 -07:00
|
|
|
// Dismiss popup when the button is clicked
|
2018-11-16 02:50:50 -08:00
|
|
|
((efl.ui.IText_Alert_Popup)sender).SetParent(null);
|
2018-10-19 06:30:16 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Adds a button to the toolbar, with the given text, icon and click event handler
|
|
|
|
private efl.ui.IButton GUIToolbarButtonAdd(efl.ui.IBox toolbar, string name,
|
|
|
|
string iconName, EventHandler func)
|
|
|
|
{
|
|
|
|
return new efl.ui.Button(toolbar, (efl.ui.IButton ebutton) => {
|
|
|
|
ebutton.SetText(name);
|
|
|
|
ebutton.ClickedEvt += func;
|
|
|
|
ebutton.SetHintWeight(0, 1);
|
|
|
|
toolbar.DoPack(ebutton);
|
|
|
|
|
|
|
|
// Set the content of the button
|
|
|
|
efl.Content.static_cast(ebutton.GetPart("efl.content")).SetContent(
|
|
|
|
// Which is an image
|
|
|
|
new efl.ui.Image(toolbar, (efl.ui.IImage eimage) => {
|
|
|
|
eimage.SetIcon(iconName);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2018-02-22 09:44:55 -08:00
|
|
|
}
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Creates a new toolbar, with all its buttons
|
2018-07-05 03:50:32 -07:00
|
|
|
private void GUIToolbarSetup(efl.ui.IBox parent)
|
2018-02-22 09:44:55 -08:00
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
// Create a horizontal box container for the buttons
|
|
|
|
efl.ui.IBox bar = new efl.ui.Box(parent, (efl.ui.IBox ebox) => {
|
|
|
|
// 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.
|
|
|
|
ebox.SetHintWeight(1, 0);
|
|
|
|
ebox.SetDirection(efl.ui.Dir.Horizontal);
|
|
|
|
parent.DoPack(ebox);
|
|
|
|
});
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// "New" button
|
|
|
|
toolbarButtonNew = GUIToolbarButtonAdd(bar, "New", "document-new",
|
|
|
|
(object sender, EventArgs ea) => {
|
|
|
|
// When this button is clicked, remove content and refresh toolbar
|
|
|
|
editorTextBox.SetText("");
|
|
|
|
GUIToolbarRefresh();
|
|
|
|
});
|
|
|
|
|
|
|
|
// "Save" button
|
|
|
|
toolbarButtonSave = GUIToolbarButtonAdd(bar, "Save", "document-save",
|
|
|
|
(object sender, EventArgs 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, EventArgs 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.
|
2018-07-05 03:50:32 -07:00
|
|
|
efl.ui.IBox box = new efl.ui.Box(parent);
|
|
|
|
bar.DoPack(box);
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// "Quit" button
|
2018-02-22 09:44:55 -08:00
|
|
|
GUIToolbarButtonAdd(bar, "Quit", "application-exit", GUIQuitCb);
|
|
|
|
}
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Builds the user interface for the text editor
|
2018-02-22 09:44:55 -08:00
|
|
|
public TextEditor()
|
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
// Create a window and initialize it
|
|
|
|
win = new efl.ui.Win(efl.App.GetLoopMain(), (efl.ui.IWin ewin) => {
|
2018-02-22 09:44:55 -08:00
|
|
|
ewin.SetText("Text Editor");
|
|
|
|
ewin.SetAutohide(true);
|
2018-10-19 06:30:16 -07:00
|
|
|
ewin.HideEvt += GUIQuitCb;
|
2018-02-22 09:44:55 -08:00
|
|
|
});
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Create a vertical box container
|
2018-07-05 03:50:32 -07:00
|
|
|
efl.ui.IBox box = new efl.ui.Box(win);
|
2018-02-22 09:44:55 -08:00
|
|
|
win.SetContent(box);
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Create the toolbar and add it to the box
|
2018-02-22 09:44:55 -08:00
|
|
|
GUIToolbarSetup(box);
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Create the main text entry
|
|
|
|
editorTextBox = new efl.ui.Text(box, (efl.ui.IText etext) => {
|
|
|
|
etext.SetFont("Mono", 14);
|
|
|
|
etext.SetMultiline(true);
|
|
|
|
etext.SetEditable(true);
|
|
|
|
etext.SetScrollable(true);
|
|
|
|
etext.SetHintMin(new eina.Size2D(360, 240));
|
|
|
|
etext.ChangedEvt += EditorChangedCb;
|
|
|
|
etext.ChangedUserEvt += EditorChangedCb;
|
|
|
|
box.DoPack(etext);
|
|
|
|
});
|
2018-02-22 09:44:55 -08:00
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Initial refresh of the toolbar buttons
|
|
|
|
GUIToolbarRefresh();
|
2018-02-22 09:44:55 -08:00
|
|
|
}
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// This method won't return until the application quits
|
2018-02-22 09:44:55 -08:00
|
|
|
public void Run()
|
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
// Start the EFL main loop
|
2018-02-22 09:44:55 -08:00
|
|
|
efl.ui.Config.Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Example
|
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
#if WIN32
|
|
|
|
[STAThreadAttribute()]
|
|
|
|
#endif
|
2018-02-22 09:44:55 -08:00
|
|
|
public static void Main()
|
|
|
|
{
|
2018-10-19 06:30:16 -07:00
|
|
|
// Initialize EFL and all UI components
|
2018-02-22 09:44:55 -08:00
|
|
|
efl.All.Init(efl.Components.Ui);
|
|
|
|
|
|
|
|
var textEditor = new TextEditor();
|
|
|
|
textEditor.Run();
|
|
|
|
|
2018-10-19 06:30:16 -07:00
|
|
|
// Shutdown EFL
|
2018-02-22 09:44:55 -08:00
|
|
|
efl.All.Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|