aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavi Artigas <xavierartigas@yahoo.es>2018-08-28 07:18:46 -0700
committerapache <apache@e5-web1.enlightenment.org>2018-08-28 07:18:46 -0700
commit1d1e262a7cd067ddae6e30739437fde4a5ae3a2b (patch)
treec8f79cec9d5d6d17ee1ed04da155a3537c4ed884
parentWiki page e0.22.4_release changed with summary [created] by Simon (diff)
downloadwww-content-1d1e262a7cd067ddae6e30739437fde4a5ae3a2b.tar.gz
Wiki page hello-world-gui-cs.md changed with summary [created] by Xavi Artigas
-rw-r--r--pages/develop/tutorials/csharp/hello-world-gui-cs.md.txt258
1 files changed, 258 insertions, 0 deletions
diff --git a/pages/develop/tutorials/csharp/hello-world-gui-cs.md.txt b/pages/develop/tutorials/csharp/hello-world-gui-cs.md.txt
new file mode 100644
index 000000000..65214df47
--- /dev/null
+++ b/pages/develop/tutorials/csharp/hello-world-gui-cs.md.txt
@@ -0,0 +1,258 @@
+---
+~~Title: Graphical "Hello World" in C#~~
+~~NOCACHE~~
+---
+
+# Graphical "Hello World" in C# #
+
+In this guide you will learn how to build a complete graphical application, with a window and typical elements like a text box and a button.
+
+| | WARNING | |
+| --- | ------- | --- |
+| ![NOTE](/_media/note-important.png) | **The C# bindings are currently in BETA state**<br>They should only be used for experimenting and **NOT** for any product development.<br>The source code for the tutorials is subject to change in the future. | ![NOTE](/_media/note-important.png) |
+
+## Prerequisites ##
+
+* Read the [Setting Up a C# Development Environment](/develop/setup/csharp/) guide so you are able to build and run EFL C# applications.
+* This tutorial builds on top of the previous one, ["Hello World" in C#](hello-world-cs.md), so you should have read that one first.
+
+## Graphical Hello World ##
+
+Copy the code below into a file and build as instructed in the [Setting Up a C# Development Environment](/develop/setup/csharp/) guide.
+
+```csharp
+using System;
+
+public class Example
+{
+ // Callback to quit the application
+ public static void QuitCb(object sender, EventArgs e)
+ {
+ // Exit the EFL main loop
+ efl.ui.Config.Exit();
+ }
+
+#if WIN32
+ [STAThreadAttribute()]
+#endif
+ public static void Main()
+ {
+ // Initialize EFL and all UI components
+ efl.All.Init(efl.Components.Ui);
+
+ // Create a window and initialize it
+ efl.ui.IWin win = new efl.ui.Win(efl.App.GetLoopMain(), (efl.ui.IWin ewin) => {
+ // Set the window's title
+ ewin.SetText("Hello World");
+ // Request that the window is automatically hidden when the "close"
+ // button is pressed
+ ewin.SetAutohide(true);
+ // Hook to the Hide event
+ ewin.HideEvt += QuitCb;
+ });
+
+ // Create a box container
+ efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => {
+ // Set its minimum size
+ ebox.SetHintMin(new eina.Size2D(360, 240));
+ // Set the box as the content for the window
+ // The window size will adapt to the box size
+ win.SetContent(ebox);
+ });
+
+ // Create a text label widget
+ new efl.ui.Text(box, (efl.ui.IText etext) => {
+ // Set its content and customize it
+ etext.SetText("Hello World. This is an Efl.Ui application!");
+ etext.SetSelectionAllowed(false);
+ etext.SetHintWeight(1.0, 0.9);
+ etext.SetHintAlign(0.5, 0.5);
+ // Add the text to the box container
+ box.DoPack(etext);
+ });
+
+ // Create a button widget
+ new efl.ui.Button(box, (efl.ui.IButton ebutton) => {
+ // Customize it
+ ebutton.SetText("Quit");
+ ebutton.SetHintWeight(1.0, 0.1);
+ // Set the method to be called when the button is pressed
+ ebutton.ClickedEvt += QuitCb;
+ // Add the button to the box container
+ box.DoPack(ebutton);
+ });
+
+ // Start the EFL main loop
+ efl.ui.Config.Run();
+
+ // Shutdown EFL
+ efl.All.Shutdown();
+ }
+}
+```
+
+When you run the application, it should look something like this:
+
+![Full application with text box and button](/_media/playground/03-hello-world-gui/image01.png)
+
+## Walkthrough ##
+
+This tutorial builds on top of the previous one, ["Hello World" in C#](hello-world-cs.md), so the basic structure, including EFL initialization and shutdown, are the same. The following steps explain how to further customize the window and add the different widgets to it.
+
+### Registering for Window Events ###
+
+Take a look at the window instantiation:
+
+```csharp
+ // Create a window and initialize it
+ efl.ui.IWin win = new efl.ui.Win(efl.App.GetLoopMain(), (efl.ui.IWin ewin) => {
+ // Set the window's title
+ ewin.SetText("Hello World");
+ // Request that the window is automatically hidden when the "close"
+ // button is pressed
+ ewin.SetAutohide(true);
+ // Hook to the Hide event
+ ewin.HideEvt += QuitCb;
+ });
+```
+
+This creates a new window, making it a child of the application's Main Loop, and customizing it with an initialization method. In this case, the initialization method is a lambda function which:
+
+* Sets the window title to `Hello World`.
+* Sets the `autohide` flag to `true` (see the [previous tutorial](hello-world-cs.md)).
+* Connects an event handler to the window's `Hide Event`. This event will be emitted whenever the window is closed, because of the `autohide` flag above.
+
+All event handlers in EFL have the same signature:
+
+```csharp
+ public static void Handler(object sender, EventArgs e);
+```
+
+Where `sender` is the object that emitted the event, and `e` contains any additional information, which depends on the type of the event.
+
+> **NOTE:**
+> `e` must be cast to the correct type to be able to extract this information. For example, when connecting to the `efl.input.Interface.KeyDownEvt` to be informed of key presses, you must cast `e` to `efl.input.Interface.KeyDownEvt_Args` to retrieve the actual key.
+
+This tutorial simply quits the application whenever the window is closed:
+
+```csharp
+ // Callback to quit the application
+ public static void QuitCb(object sender, EventArgs e)
+ {
+ // Exit the EFL main loop
+ efl.ui.Config.Exit();
+ }
+```
+
+This is already the default behavior for EFL applications, so this event is connected here just for demonstration purposes.
+
+### Adding a Container Box ###
+
+Now you could start adding widgets directly to your window, but you would need to manually give them a position and a size. If later on you decide to make your window bigger, or if you want your window to be resizable by the user, you'll need to change the position and size of every widget.
+
+It is easier to put your widgets inside a *container* which takes care of positioning and sizing them, according to available space and other widgets. There's a variety of containers available, which allow you to stack your widgets vertically or horizontally, or align them on a square grid, for example.
+
+Although invisible themselves, containers are regular widgets too, so you can size and position them, and even nest them under other containers.
+
+This tutorial uses a *box* container:
+
+```csharp
+ // Create a box container
+ efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => {
+ // Set its minimum size
+ ebox.SetHintMin(new eina.Size2D(360, 240));
+ // Set the box as the content for the window
+ // The window size will adapt to the box size
+ win.SetContent(ebox);
+ });
+```
+
+A box container can be vertical or horizontal, and by default it is vertical. All widgets inside a vertical box container will have the same width as the container, and their heights will be automatically chosen so that they cover the whole surface of the container from top to bottom (Imagine a stack of pizza boxes neatly fitting inside your oven). By default, all widgets will have the same height, but that can be customized as you will see in a moment.
+
+In the initialization method of the box above you can see calls to:
+
+* `SetHintMin()`: This sets the **minimum** size for the box. You can resize the window and the box will grow or shrink as needed, but it will never go below this size.
+
+* `SetContent()`: This method specifies the **one** widget which will be the content of the window (Windows can only have one content set at any given time). If you want to have more than one widget in your window, you will need to wrap them in a container and set that as the content of the window. As shown above, the box container is set as the content of the window.
+
+### Adding a Text Label ###
+
+So far the tutorial has created a window and an invisible container, it is about time it adds something which can actually be seen:
+
+```csharp
+ // Create a text label widget
+ new efl.ui.Text(box, (efl.ui.IText etext) => {
+ // Set its content and customize it
+ etext.SetText("Hello World. This is an Efl.Ui application!");
+ etext.SetSelectionAllowed(false);
+ etext.SetHintWeight(1.0, 0.9);
+ etext.SetHintAlign(0.5, 0.5);
+ // Add the text to the box container
+ box.DoPack(etext);
+ });
+```
+
+This instantiates an `efl.ui.Text` object, as a child of the box container. Quite sensibly, this is the widget used to display text on screen. The initialization above is worth a careful look:
+
+* `SetText()`: Specifies the text string to display in the widget.
+* `SetSelectionAllowed()`: This disables selecting the text (by dragging over it with the mouse, for example). Just to showcase a bit the EFL's capabilities.
+* `SetHintWeight()`: This one controls the widget's size, but, instead of setting it explicitly as `SetHintMin()` did, it does so indirectly by giving the widget a **weight**. The container holding this widget decides the size for all its held widgets according to their relative weights (If all widgets have the same weight, they all have the same size. If you double the weight of one of the widgets, its size will be double that of the other widgets). This is a complex topic explained in its own tutorial (coming soon).
+
+ In the code above, the text widget is given a weight of `1.0` in the horizontal direction and `0.9` in the vertical direction (both directions are treated separately). The effect of this on the final widget size depends on the other widgets' weights, so it is explained in the next section.
+
+* `SetHintAlign()`: Once the container has allocated space for each widget according to its weight, if the actual widget is smaller, it has room to be aligned. This command controls horizontal and vertical alignment in this way: `0` means left (or top), `1` means right (or bottom), and intermediate values are possible, meaning intermediate positions. For example, `0.5` means centered, as used in the code.
+
+ > **NOTE**:
+ > There is currently an issue in EFL preventing correct vertical alignment of widgets inside vertical boxes (and horizontal alignment inside horizontal boxes). You can follow the resolution progress in ticket [T7352](https://phab.enlightenment.org/T7352).
+
+* `Efl.ui.Box.DoPack()`: This adds the text widget to the box container. The order in which widgets are added to a container dictates the order in which they are placed on the screen.
+
+ Besides being a child of the box object, the text widget also needs to be *packed* into the box with `DoPack()`.
+
+It is worth noting that, as hinted in the [previous tutorial](hello-world-cs.md), no variable is needed to store the widget: after its construction, it is already fully initialized (customized) and a reference has been handled to a parent which will take care of its destruction. This results in a very compact way to build user interfaces!
+
+### Adding a Button ###
+
+Let's now add a button below the text label:
+
+```csharp
+ // Create a button widget
+ new efl.ui.Button(box, (efl.ui.IButton ebutton) => {
+ // Customize it
+ ebutton.SetText("Quit");
+ ebutton.SetHintWeight(1.0, 0.1);
+ // Set the method to be called when the button is pressed
+ ebutton.ClickedEvt += QuitCb;
+ // Add the button to the box container
+ box.DoPack(ebutton);
+ });
+```
+
+This instantiates a button as a child of the box and initializes it so:
+
+* Its label is set to `Quit`.
+* Its vertical weight is set to `0.1`. The only other widget inside the box container is the text label created in the previous section, with a weight of `0.9`, so these two widgets will share the total height of the window at a 90% - 10% ratio.
+
+ > **NOTE**: You could also use `1` and `9` for the weights, or `10` and `90`, to achieve the same results. Remember that the weights are relative.
+
+* It connects the `QuitCb()` event handler to the `Clicked` event. In this way, when the button is pressed, the application will exit.
+* Adds the button to the box (besides being a child of the box object, the button also needs to be *packed* into the box with `DoPack()`).
+
+## Summary ##
+
+At the end of this tutorial you have learned:
+
+* How to react to **events** like window a being closed or a button being clicked.
+* How to use **container** widgets to take care of other widget's position and size.
+* How to further customize a widget's position and size using **weight** and **alignment** hints.
+
+## Further Reading ##
+
+[Setting up a C# Development Environment](/develop/setup/csharp/)
+: Read this before trying to develop with the EFL and C#
+
+["Hello World" in C#](hello-world-cs.md)
+: First tutorial in the series, explaining the basics.
+
+[Tutorial Code Examples](https://git.enlightenment.org/tools/examples.git/tree/tutorial/csharp)
+: C# Source code for the tutorials.