This example shows how one can use the \hyperlink{group__Layout}{Layout} widget to create a customized distribution of widgets on the screen, controlled by an Edje theme.

The full source code for this example can be found at \hyperlink{layout_example_01_c}{layout\-\_\-example\-\_\-01.\-c}.

Our custom layout is defined by a file, \hyperlink{layout_example_edc}{An example of layout theme file}, which is an Edje theme file. Look for the Edje documentation to understand it. For now, it's enough to know that we describe some specific parts on this layout theme\-: \begin{DoxyItemize}
\item a title text field; \item a box container; \item a table container; \item and a content container.\end{DoxyItemize}
Going straight to the code, the following snippet instantiates the layout widget\-:

 
\begin{DoxyCodeInclude}
   layout = elm_layout_add(win);
   evas\_object\_size\_hint\_weight\_set(layout, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm_win_resize_object_add(win, layout);
   snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"%s/examples/layout\_example.edj"}, 
      elm_app_data_dir_get());
   elm_layout_file_set(layout, buf, \textcolor{stringliteral}{"example/mylayout"});
   evas\_object\_show(layout);

\end{DoxyCodeInclude}


As any other widget, we set some properties for the size calculation. But notice on this piece of code the call to the function \hyperlink{group__Layout_gae6ec96da2c6725a2ba4eba0ea782523a}{elm\-\_\-layout\-\_\-file\-\_\-set()}. Here is where the theme file is loaded, and particularly the specific group from this theme file. Also notice that the theme file here is referenced as an .edj, which is a .edc theme file compiled to its binary form. Again, look for the Edje documentation for more information about theme files.

Next, we fetch from our theme a data string referenced by the key \char`\"{}title\char`\"{}. This data was defined in the theme, and can be used as parameters which the program get from the specific theme that it is using. In this case, we store the title of this window and program in the theme, as a \char`\"{}data\char`\"{} entry, just for demonstration purposes\-:


\begin{DoxyCodeInclude}

   \textcolor{comment}{// Setting title}
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *title = elm_layout_data_get(layout, \textcolor{stringliteral}{"title"});
   \textcolor{keywordflow}{if} (title)
     \{
        elm_win_title_set(win, title);
        elm_object_part_text_set(layout, TITLE, title);
     \}

\end{DoxyCodeInclude}


This call \hyperlink{group__Layout_ga2f26c6b713040627dfd3eafe51c318a3}{elm\-\_\-layout\-\_\-data\-\_\-get()} is used to fetch the string based on the key, and \hyperlink{group__General_ga9c088563c442670796564d2e5158f154}{elm\-\_\-object\-\_\-part\-\_\-text\-\_\-set()} will set the part defined in the theme as \char`\"{}example/title\char`\"{} to contain this string. This key \char`\"{}example/title\char`\"{} has nothing special. It's just an arbitrary convention that we are using in this example. Every string in this example referencing a part of this theme will be of the form \char`\"{}example/$<$something$>$\char`\"{}.

Now let's start using our layout to distribute things on the window space. Since the layout was added as a resize object to the elementary window, it will always occupy the entire space available for this window.

The theme already has a title, and it also defines a table element which is positioned approximately between 50\% and 70\% of the height of this window, and has 100\% of the width. We create some widgets (two icons, a clock and a button) and pack them inside the table, in a distribution similar to a H\-T\-M\-L table\-:


\begin{DoxyCodeInclude}

   \textcolor{comment}{// Add icon, clock and button to the table}
   icon = elm_icon_add(win);
   elm_icon_standard_set(icon, \textcolor{stringliteral}{"home"});
   evas\_object\_size\_hint\_weight\_set(icon, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(icon, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_table_pack(layout, TABLE, icon, 0, 0, 1, 1);
   evas\_object\_show(icon);

   icon2 = elm_icon_add(win);
   elm_icon_standard_set(icon2, \textcolor{stringliteral}{"close"});
   evas\_object\_size\_hint\_weight\_set(icon2, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(icon2, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_table_pack(layout, TABLE, icon2, 1, 0, 1, 1);
   evas\_object\_show(icon2);

   clk = elm_clock_add(win);
   evas\_object\_size\_hint\_weight\_set(clk, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(clk, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_table_pack(layout, TABLE, clk, 2, 0, 1, 1);
   evas\_object\_show(clk);

   bt = elm_button_add(win);
   elm\_object\_text\_set(bt, \textcolor{stringliteral}{"Click me!"});
   evas\_object\_size\_hint\_weight\_set(bt, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(bt, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_table_pack(layout, TABLE, bt, 0, 1, 3, 1);
   evas\_object\_smart\_callback\_add(bt, \textcolor{stringliteral}{"clicked"}, \_tbl\_btn\_cb, layout);
   evas\_object\_show(bt);

\end{DoxyCodeInclude}


Notice that we just set size hints for every object, and call the function \hyperlink{group__Layout_gacfb2978dde6b835932c949934809bef8}{elm\-\_\-layout\-\_\-table\-\_\-pack()}, which does all the work. It will place the elements in the specified row/column, with row and column span if required, and then the object's size and position will be controlled by the layout widget. It will also respect size hints, alignments and weight properties set to these widgets. The resulting distribution on the screen depends on the table properties (described in the theme), the size hints set on each widget, and on the cells of the table that are being used.

For instance, we add the two icons and the clock on the first, second and third cells of the first row, and add the button the second row, making it span for 3 columns (thus having the size of the entire table width). This will result in a table that has 2 rows and 3 columns.

Now let's add some widgets to the box area of our layout. This box is around 20\% and 50\% of the vertical size of the layout, and 100\% of its width. The theme defines that it will use an \char`\"{}horizontal flow\char`\"{} distribution to its elements. Unlike the table, a box will distribute elements without knowing about rows and columns, and the distribution function selected will take care of putting them in row, column, both, or any other available layout. This is also described in the Edje documentation.

This box area is similar to the \hyperlink{group__Box}{Box} widget of elementary, with the difference that its position and properties are controlled by the theme of the layout. It also contains more than one A\-P\-I to add items to it, since the items position now is defined in terms of a list of items, not a matrix. There's the first position (can have items added to it with \hyperlink{group__Layout_ga558e5e6627b954153bf8acdae0948427}{elm\-\_\-layout\-\_\-box\-\_\-prepend()}), the last position (\hyperlink{group__Layout_gafcb30a9481eabc7f501b3efcea75f341}{elm\-\_\-layout\-\_\-box\-\_\-append()}), the nth position (\hyperlink{group__Layout_ga0f7045569f06f65f15f0dc2eb7ee5085}{elm\-\_\-layout\-\_\-box\-\_\-insert\-\_\-at()}) and the position right before an element (\hyperlink{group__Layout_ga4a282118f209b67339a2afdd2684fd3c}{elm\-\_\-layout\-\_\-box\-\_\-insert\-\_\-before()}). We use insert\-\_\-at and prepend functions to add the first two buttons to this box, and insert\-\_\-before on the callback of each button. The callback code will be shown later, but it basically adds a button just before the clicked button using the \hyperlink{group__Layout_ga4a282118f209b67339a2afdd2684fd3c}{elm\-\_\-layout\-\_\-box\-\_\-insert\-\_\-before()} function. Here's the code for adding the first 2 buttons\-:


\begin{DoxyCodeInclude}

   item = elm_button_add(win);
   elm\_object\_text\_set(item, \textcolor{stringliteral}{"Position 0"});
   evas\_object\_size\_hint\_weight\_set(item, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(item, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_box_insert_at(layout, BOX, item, 0);
   evas\_object\_smart\_callback\_add(item, \textcolor{stringliteral}{"clicked"}, \_box\_btn\_cb, layout);
   evas\_object\_show(item);

   item = elm_button_add(win);
   elm\_object\_text\_set(item, \textcolor{stringliteral}{"Prepended"});
   evas\_object\_size\_hint\_weight\_set(item, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(item, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_box_prepend(layout, BOX, item);
   evas\_object\_smart\_callback\_add(item, \textcolor{stringliteral}{"clicked"}, \_box\_btn\_cb, layout);
   evas\_object\_show(item);

\end{DoxyCodeInclude}


Finally, we have an area in this layout theme, in the bottom part of it, reserved for adding an specific widget. Differently from the 2 parts described until now, this one can only receive one widget with the call \hyperlink{group__General_ga0531ec8867c72def7c780601764ca642}{elm\-\_\-object\-\_\-part\-\_\-content\-\_\-set()} for the layout. If there was already an item on this specific part, it will be deleted (one can use \hyperlink{group__General_gadc5a1df25fe17e31739841133b53b5fc}{elm\-\_\-object\-\_\-part\-\_\-content\-\_\-unset()} in order to remove it without deleting). An example of removing it without deleting, but manually deleting this widget just after that, can be seen on the callback for this button. Actually, the callback defined for this button will clean the two other parts (deleting all of their elements) and then remove and delete this button.


\begin{DoxyCodeInclude}

   bt2 = elm_button_add(win);
   elm\_object\_text\_set(bt2, \textcolor{stringliteral}{"Delete All"});
   elm_object_part_content_set(layout, SWALLOW, bt2);
   evas\_object\_smart\_callback\_add(bt2, \textcolor{stringliteral}{"clicked"}, \_swallow\_btn\_cb, layout);

\end{DoxyCodeInclude}


Also notice that, for this last added button, we don't have to call evas\-\_\-object\-\_\-show() on it. This is a particularity of the theme for layouts, that will have total control over the properties like size, position, visibility and clipping of a widget added with \hyperlink{group__General_ga0531ec8867c72def7c780601764ca642}{elm\-\_\-object\-\_\-part\-\_\-content\-\_\-set()}. Again, read the Edje documentation to understand this better.

Now we just put the code for the different callbacks specified for each kind of button and make simple comments about them\-:

 
\begin{DoxyCodeInclude}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_tbl\_btn\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *layout = data;

   elm_layout_table_unpack(layout, TABLE, btn);
   evas\_object\_del(btn);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_box\_btn\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *layout = data;
   Evas\_Object *item;
   \textcolor{keywordtype}{char} buf[30];

   snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"Button %02d"}, \_box\_buttons++);

   item = elm_button_add(elm_object_parent_widget_get(layout));
   elm\_object\_text\_set(item, buf);
   evas\_object\_size\_hint\_weight\_set(item, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(item, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_layout_box_insert_before(layout, BOX, item, btn);
   evas\_object\_smart\_callback\_add(item, \textcolor{stringliteral}{"clicked"}, \_box\_btn\_cb, layout);
   evas\_object\_show(item);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_swallow\_btn\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *layout = data;
   Evas\_Object *item;

   elm_layout_table_clear(layout, TABLE, EINA\_TRUE);
   elm_layout_box_remove_all(layout, BOX, EINA\_TRUE);
   item = elm_object_part_content_unset(layout, SWALLOW);
   evas\_object\_del(item);
\}

\end{DoxyCodeInclude}


The first callback is used for the button in the table, and will just remove itself from the table with \hyperlink{group__Layout_ga674a80b6376502c0747c3017916c479f}{elm\-\_\-layout\-\_\-table\-\_\-unpack()}, which remove items without deleting them, and then calling evas\-\_\-object\-\_\-del() on itself.

The second callback is for buttons added to the box. When clicked, these buttons will create a new button, and add them to the same box, in the position just before the clicked button.

And the last callback is for the button added to the \char`\"{}content\char`\"{} area. It will clear both the table and the box, passing {\ttfamily E\-I\-N\-A\-\_\-\-T\-R\-U\-E} to their respective {\ttfamily clear} parameters, which will imply on the items of these containers being deleted.

A screenshot of this example can be seen on\-:

 
\begin{DoxyImage}
\includegraphics{screenshots/layout_example_01}
\caption{width=}
\end{DoxyImage}
 