This example shows how to create a simple prefs widget with Elementary, where the items values are \char`\"{}reset\char`\"{} on each timer tick.

We do that {\bfseries programmatically}, to demonstrate that by touching a given prefs widgets prefs data values, the changes reflect instantly on the UI.

We\textquotesingle{}ll create items on the .E\+PC file and after handle it on the .C file.\hypertarget{prefs_example_01_prefs-epc-01}{}\section{Creating items on E\+P\+C file}\label{prefs_example_01_prefs-epc-01}


First we\textquotesingle{}ll create prefs items on .E\+PC file that we\textquotesingle{}ll use later on the .C file. Note that the code is similar to .E\+DC (edje) files.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Here we define a page item. Pages are group of items grouped together, on a given prefs widget.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


In this part, we create a {\ttfamily I\+NT} type item, that by default will become a spinner widget in the UI, and default, min and max parameters are optional as well as in {\ttfamily F\+L\+O\+AT} type.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Other {\ttfamily I\+NT} type widget implementations may exist, as is exemplifyed on the item that follows.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Now we create a L\+A\+B\+EL type item and by default will become a read-\/only label in UI.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Now we create a T\+E\+XT type item and by default will become a single-\/line text entry in UI. Note that we use a Regular Expression to deny only entries with numbers.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


In this part we create a D\+A\+TE type item, by default will become a datetime in UI, and default, min and max parameters are optional.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Here we create a S\+E\+P\+A\+R\+A\+T\+OR type item, it has no value bound, serves only to divide and organize prefs items.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


In this part, we create a S\+A\+VE type item that will get all the values bounded to items and save it on C\+FG file. Next time you execute the application, all the values that you saved before will be loaded.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Here we create a R\+E\+S\+ET type item that will return all the values bounded to items as default declared on .E\+PC file.


\begin{DoxyCodeInclude}

\end{DoxyCodeInclude}


Pages and items have default implementation widgets, but, with the tag \textquotesingle{}widget\textquotesingle{}, you can use different widgets for prefs items. To a list of default widgets supported by each type, by default, refer to the Elementary Prefs Collection reference sheet. One can also register, at run time, custom item widget handlers too.\hypertarget{prefs_example_01_prefs-c-01}{}\section{Handling items on C File}\label{prefs_example_01_prefs-c-01}


Now we\textquotesingle{}re handling the .C file and first we\textquotesingle{}ll create a prefs widget.


\begin{DoxyCodeInclude}
   prefs = elm_prefs_add(win);
   evas\_object\_size\_hint\_weight\_set(prefs, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm\_win\_resize\_object\_add(win, prefs);
   evas\_object\_show(prefs);

\end{DoxyCodeInclude}


Here we add some specific callbacks, for example \char`\"{}item,changed\char`\"{} that will be called when any item that we created on E\+PC file changes.


\begin{DoxyCodeInclude}
   evas\_object\_smart\_callback\_add(prefs, \textcolor{stringliteral}{"page,saved"}, \_page\_saved\_cb, NULL);
   evas\_object\_smart\_callback\_add(prefs, \textcolor{stringliteral}{"page,loaded"}, \_page\_loaded\_cb, NULL);
   evas\_object\_smart\_callback\_add(prefs, \textcolor{stringliteral}{"item,changed"}, \_item\_changed\_cb, win);

\end{DoxyCodeInclude}


Here we set the prefs to save its values back (on the user data file) automatically on every UI element changes.


\begin{DoxyCodeInclude}
   elm_prefs_autosave_set(prefs, EINA\_TRUE);

\end{DoxyCodeInclude}


In this part we create the prefs data handle and set the .E\+PB file (.E\+PC compiled). This .E\+PB file contains all the default values from the items that we created, this file will be loaded when the program starts or when clicked on a R\+E\+S\+ET type item. There is another file created when the values from prefs items are saved, the .C\+FG file, that contains all the non-\/standard saved values from the prefs items, this file will be loaded when program starts as well.

 
\begin{DoxyCodeInclude}
   Elm_Prefs_Data *prefs\_data;

\end{DoxyCodeInclude}



\begin{DoxyCodeInclude}
   prefs\_data = elm_prefs_data_new(\textcolor{stringliteral}{"./prefs\_example\_01.cfg"}, NULL,
                                   EET\_FILE\_MODE\_READ\_WRITE);

   elm_prefs_file_set(prefs, \textcolor{stringliteral}{"prefs\_example\_01.epb"}, NULL);
   elm_prefs_data_set(prefs, prefs\_data);

\end{DoxyCodeInclude}


Here we just create a notify widget to appear when the values are reset.


\begin{DoxyCodeInclude}
   label = elm_label_add(win);
   elm\_object\_text\_set(label, \textcolor{stringliteral}{"Setting Values Programmatically"});
   evas\_object\_size\_hint\_align\_set(label, 0.5, 0.5);

   notify = elm_notify_add(win);
   elm\_notify\_align\_set(notify, 0.5, 1);
   elm\_notify\_timeout\_set(notify, 2);
   elm\_object\_content\_set(notify, label);
   evas\_object\_size\_hint\_weight\_set(notify, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_show(notify);

\end{DoxyCodeInclude}


Now we add a timer to reset the items values on each 5.\+0 seconds and show the notify.


\begin{DoxyCodeInclude}
   evas\_object\_data\_set(notify, \textcolor{stringliteral}{"prefs\_data"}, prefs\_data);

\end{DoxyCodeInclude}


 
\begin{DoxyCodeInclude}
\_elm\_prefs\_data\_change(\textcolor{keywordtype}{void} *data)
\{
   Evas\_Object *notify = data;
   Elm_Prefs_Data *prefs\_data;
   Elm_Prefs_Item_Type type;
   Eina\_Value value;

   prefs\_data = evas\_object\_data\_get(notify, \textcolor{stringliteral}{"prefs\_data"});

   \textcolor{keywordflow}{if} (elm_prefs_data_value_get(prefs\_data, \textcolor{stringliteral}{"main:universe"}, &type, &value))
     \{
        eina\_value\_set(&value, 42);
        elm_prefs_data_value_set(prefs\_data, \textcolor{stringliteral}{"main:universe"}, type, &value);
     \}

   \textcolor{keywordflow}{if} (elm_prefs_data_value_get(prefs\_data, \textcolor{stringliteral}{"main:text"}, &type, &value))
     \{
        eina\_value\_set(&value, \textcolor{stringliteral}{"This is a text field (:"});
        elm_prefs_data_value_set(prefs\_data, \textcolor{stringliteral}{"main:text"}, type, &value);
     \}

   evas\_object\_show(notify);

   \textcolor{keywordflow}{return} ECORE\_CALLBACK\_RENEW;
\}

\end{DoxyCodeInclude}


Here we finish the example. The full source code can be found on \hyperlink{prefs_example_01_c}{prefs\+\_\+example\+\_\+01.\+c} and \hyperlink{prefs_example_01_epc}{prefs\+\_\+example\+\_\+01.\+epc} 