aboutsummaryrefslogtreecommitdiffstats
path: root/pages/develop/tutorials/csharp/hello-world-gui-cs.md.txt
blob: 65214df4700b4fe5ded4c53c3f59d72668ccc7c3 (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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.