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
This commit is contained in:
parent
eb2cf48195
commit
a040529352
|
@ -1,141 +1,213 @@
|
|||
/* 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 : IDisposable
|
||||
public class TextEditor
|
||||
{
|
||||
private efl.ui.IWin win;
|
||||
private efl.ui.IText editor;
|
||||
private efl.ui.IButton toolbarNew;
|
||||
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
|
||||
|
||||
private bool edited = false;
|
||||
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, EventArgs ea)
|
||||
{
|
||||
efl.ui.Config.Exit();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
private efl.ui.IButton GUIToolbarButtonAdd(efl.ui.IBox toolbar, string name, string iconName, EventHandler func)
|
||||
// Shows a modal message popup with an "OK" button
|
||||
private void ShowMessage(string message)
|
||||
{
|
||||
efl.ui.IButton button = new efl.ui.Button(toolbar);
|
||||
button.SetText(name);
|
||||
button.ClickedEvt += func;
|
||||
|
||||
toolbar.DoPack(button);
|
||||
|
||||
efl.ui.IImage img = new efl.ui.Image(toolbar);
|
||||
img.SetIcon(iconName);
|
||||
|
||||
efl.Content.static_cast(button.GetPart("efl.content")).SetContent(img);
|
||||
|
||||
return button;
|
||||
new efl.ui.Popup_Alert_Text (win, (efl.ui.IPopup_Alert_Text epopup) => {
|
||||
epopup.SetText(message);
|
||||
epopup.SetExpandable(new eina.Size2D(200,200));
|
||||
epopup.SetButton(efl.ui.Popup_Alert_Button.Positive, "OK", null);
|
||||
epopup.ButtonClickedEvt +=
|
||||
(object sender, efl.ui.Popup_Alert.ButtonClickedEvt_Args ea) => {
|
||||
// Dismiss popup when the button is clicked
|
||||
((efl.ui.IPopup_Alert_Text)sender).SetParent(null);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// 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);
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Creates a new toolbar, with all its buttons
|
||||
private void GUIToolbarSetup(efl.ui.IBox parent)
|
||||
{
|
||||
efl.ui.IBox bar = new efl.ui.Box(parent);
|
||||
bar.SetHintWeight(1, 0);
|
||||
bar.SetDirection(efl.ui.Dir.Horizontal);
|
||||
parent.DoPack(bar);
|
||||
// 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);
|
||||
});
|
||||
|
||||
toolbarNew = GUIToolbarButtonAdd(bar, "New", "document-new",
|
||||
(object sender, EventArgs ea) =>
|
||||
{
|
||||
editor.SetText("");
|
||||
// "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);
|
||||
}
|
||||
});
|
||||
|
||||
// spacer box
|
||||
// "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.
|
||||
efl.ui.IBox box = new efl.ui.Box(parent);
|
||||
box.SetHintWeight(10, 0);
|
||||
bar.DoPack(box);
|
||||
|
||||
// "Quit" button
|
||||
GUIToolbarButtonAdd(bar, "Quit", "application-exit", GUIQuitCb);
|
||||
}
|
||||
|
||||
// Builds the user interface for the text editor
|
||||
public TextEditor()
|
||||
{
|
||||
// Create a window and initialize it
|
||||
win = new efl.ui.Win(efl.App.GetLoopMain(), (efl.ui.IWin ewin) => {
|
||||
ewin.SetText("Text Editor");
|
||||
ewin.SetAutohide(true);
|
||||
ewin.HideEvt += GUIQuitCb;
|
||||
});
|
||||
|
||||
// Create a vertical box container
|
||||
efl.ui.IBox 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, (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);
|
||||
});
|
||||
|
||||
// Initial refresh of the toolbar buttons
|
||||
GUIToolbarRefresh();
|
||||
}
|
||||
|
||||
private void GUIToolbarRefresh()
|
||||
{
|
||||
toolbarNew.SetDisabled(!edited);
|
||||
}
|
||||
|
||||
public TextEditor()
|
||||
{
|
||||
win = new efl.ui.Win(null, (efl.ui.IWin ewin) => {
|
||||
ewin.SetWinType(efl.ui.Win_Type.Basic);
|
||||
ewin.SetText("Text Editor");
|
||||
ewin.SetAutohide(true);
|
||||
});
|
||||
|
||||
// when the user clicks "close" on a window there is a request to hide
|
||||
win.HideEvt += GUIQuitCb;
|
||||
|
||||
efl.ui.IBox box = new efl.ui.Box(win);
|
||||
eina.Size2D sz;
|
||||
sz.W = 360;
|
||||
sz.H = 240;
|
||||
box.SetHintMin(sz);
|
||||
|
||||
win.SetContent(box);
|
||||
|
||||
GUIToolbarSetup(box);
|
||||
|
||||
editor = new efl.ui.Text(box);
|
||||
editor.SetFont("Mono", 14);
|
||||
editor.SetMultiline(true);
|
||||
editor.SetEditable(true);
|
||||
editor.SetScrollable(true);
|
||||
editor.SetScrollable(true);
|
||||
editor.ChangedEvt += EditorChangedCb;
|
||||
editor.ChangedUserEvt += EditorChangedCb;
|
||||
|
||||
box.DoPack(editor);
|
||||
}
|
||||
|
||||
~TextEditor()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
editor.Dispose();
|
||||
toolbarNew.Dispose();
|
||||
win.Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
// This method won't return until the application quits
|
||||
public void Run()
|
||||
{
|
||||
// start main loop
|
||||
// Start the EFL main loop
|
||||
efl.ui.Config.Run();
|
||||
}
|
||||
}
|
||||
|
||||
public class Example
|
||||
{
|
||||
#if WIN32
|
||||
[STAThreadAttribute()]
|
||||
#endif
|
||||
public static void Main()
|
||||
{
|
||||
// Initialize EFL and all UI components
|
||||
efl.All.Init(efl.Components.Ui);
|
||||
|
||||
var textEditor = new TextEditor();
|
||||
textEditor.Run();
|
||||
|
||||
// dispose after quit
|
||||
textEditor.Dispose();
|
||||
|
||||
// Shutdown EFL
|
||||
efl.All.Shutdown();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue