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}{}\doxysection{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}{0}
\DoxyCodeLine{collection}
\DoxyCodeLine{\{}
\DoxyCodeLine{   page}
\DoxyCodeLine{   \{}
\DoxyCodeLine{      name: "main";}
\DoxyCodeLine{      version: 1;}
\DoxyCodeLine{      title: "Preferences Widget";}
\DoxyCodeLine{      subtitle: "Example 01";}

\end{DoxyCodeInclude}


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


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{      widget: "elm/vertical\_box";}

\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}{0}
\DoxyCodeLine{      items \{}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "universe";}
\DoxyCodeLine{            type: INT;}
\DoxyCodeLine{            label: "Ultimate Answer of Life, the Universe and Everything";}
\DoxyCodeLine{            editable: 1;}
\DoxyCodeLine{}
\DoxyCodeLine{            int \{}
\DoxyCodeLine{               default: 42;}
\DoxyCodeLine{               min: 0;}
\DoxyCodeLine{               max: 150;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}

\end{DoxyCodeInclude}


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


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "another int";}
\DoxyCodeLine{            type: INT;}
\DoxyCodeLine{            widget: "elm/spinner";}
\DoxyCodeLine{}
\DoxyCodeLine{            int \{}
\DoxyCodeLine{               min: 0;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "label";}
\DoxyCodeLine{            type: LABEL;}
\DoxyCodeLine{            label: "Just a label...";}
\DoxyCodeLine{         \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "text";}
\DoxyCodeLine{            type: TEXT;}
\DoxyCodeLine{            editable: 1;}
\DoxyCodeLine{}
\DoxyCodeLine{            text \{}
\DoxyCodeLine{               placeholder: "This is a text field (:";}
\DoxyCodeLine{               default: "default str.";}
\DoxyCodeLine{               deny: "\string^[0-\/9]*\$";}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "date";}
\DoxyCodeLine{            type: DATE;}
\DoxyCodeLine{            label: "First EFL Developer Day";}
\DoxyCodeLine{}
\DoxyCodeLine{            date \{}
\DoxyCodeLine{               default: 2012 11 05;}
\DoxyCodeLine{               min: 1980 11 1;}
\DoxyCodeLine{               max: 2200 12 2;}
\DoxyCodeLine{            \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "sep";}
\DoxyCodeLine{            type: SEPARATOR;}
\DoxyCodeLine{         \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "save";}
\DoxyCodeLine{            type: SAVE;}
\DoxyCodeLine{            label: "Save";}
\DoxyCodeLine{         \}}

\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}{0}
\DoxyCodeLine{         item \{}
\DoxyCodeLine{            name: "reset";}
\DoxyCodeLine{            type: RESET;}
\DoxyCodeLine{            label: "Reset";}
\DoxyCodeLine{         \}}

\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}{}\doxysection{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}{0}
\DoxyCodeLine{   prefs = \mbox{\hyperlink{group__Elm__Prefs_ga6736d5f9363dd7acbcb39938668b3e45}{elm\_prefs\_add}}(win);}
\DoxyCodeLine{   evas\_object\_size\_hint\_weight\_set(prefs, \mbox{\hyperlink{Evas__Common_8h_aa47c85083d3dae0111c21ec49359306a}{EVAS\_HINT\_EXPAND}}, \mbox{\hyperlink{Evas__Common_8h_aa47c85083d3dae0111c21ec49359306a}{EVAS\_HINT\_EXPAND}});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Win_ga505d6bd217e5ee01e82b548ec35986eb}{elm\_win\_resize\_object\_add}}(win, prefs);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{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}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Smart__Object__Group_ga18a8b179f94d21b2b09e19db11741061}{evas\_object\_smart\_callback\_add}}(prefs, \textcolor{stringliteral}{"page,saved"}, \_page\_saved\_cb, NULL);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Smart__Object__Group_ga18a8b179f94d21b2b09e19db11741061}{evas\_object\_smart\_callback\_add}}(prefs, \textcolor{stringliteral}{"page,loaded"}, \_page\_loaded\_cb, NULL);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Smart__Object__Group_ga18a8b179f94d21b2b09e19db11741061}{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}{0}
\DoxyCodeLine{   elm\_prefs\_autosave\_set(prefs, \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{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}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Prefs_ga7e2cbea5a9696262669748533f578a53}{Elm\_Prefs\_Data}} *prefs\_data;}

\end{DoxyCodeInclude}



\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   prefs\_data = \mbox{\hyperlink{group__Elm__Prefs_ga58895cf0ae0fd70db018fbe8eb7ba5c2}{elm\_prefs\_data\_new}}(\textcolor{stringliteral}{"./prefs\_example\_01.cfg"}, NULL,}
\DoxyCodeLine{                                   \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a763d29c19960a4e0d4eb7bc7bb512481}{EET\_FILE\_MODE\_READ\_WRITE}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Prefs_gaec5a0c4ea7f8e54909c5cdbc05e9b064}{elm\_prefs\_file\_set}}(prefs, \textcolor{stringliteral}{"prefs\_example\_01.epb"}, NULL);}
\DoxyCodeLine{   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}{0}
\DoxyCodeLine{   label = \mbox{\hyperlink{group__Elm__Label_ga01e161a300e3ee6f7a13a44505724e03}{elm\_label\_add}}(win);}
\DoxyCodeLine{   elm\_object\_text\_set(label, \textcolor{stringliteral}{"Setting Values Programmatically"});}
\DoxyCodeLine{   evas\_object\_size\_hint\_align\_set(label, 0.5, 0.5);}
\DoxyCodeLine{}
\DoxyCodeLine{   notify = \mbox{\hyperlink{group__Elm__Notify_ga3b3eb10c622de79e55539aeb84e538a3}{elm\_notify\_add}}(win);}
\DoxyCodeLine{   elm\_notify\_align\_set(notify, 0.5, 1);}
\DoxyCodeLine{   elm\_notify\_timeout\_set(notify, 2);}
\DoxyCodeLine{   elm\_object\_content\_set(notify, label);}
\DoxyCodeLine{   evas\_object\_size\_hint\_weight\_set(notify, \mbox{\hyperlink{Evas__Common_8h_aa47c85083d3dae0111c21ec49359306a}{EVAS\_HINT\_EXPAND}}, \mbox{\hyperlink{Evas__Common_8h_aa47c85083d3dae0111c21ec49359306a}{EVAS\_HINT\_EXPAND}});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{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}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group__Extras_ga70134a7068c9bff4107a761abe0a2ad4}{evas\_object\_data\_set}}(notify, \textcolor{stringliteral}{"prefs\_data"}, prefs\_data);}

\end{DoxyCodeInclude}


 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_elm\_prefs\_data\_change(\textcolor{keywordtype}{void} *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *notify = data;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Prefs_ga7e2cbea5a9696262669748533f578a53}{Elm\_Prefs\_Data}} *prefs\_data;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Prefs_ga7525bee64184478acad1e807c3e570ae}{Elm\_Prefs\_Item\_Type}} type;}
\DoxyCodeLine{   \mbox{\hyperlink{struct__Eina__Value}{Eina\_Value}} value;}
\DoxyCodeLine{}
\DoxyCodeLine{   prefs\_data = \mbox{\hyperlink{group__Evas__Object__Group__Extras_gafbad43015793eda4ae50202c0ce0e652}{evas\_object\_data\_get}}(notify, \textcolor{stringliteral}{"prefs\_data"});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (\mbox{\hyperlink{group__Elm__Prefs_ga2b692083e7d307512858f1adc2563519}{elm\_prefs\_data\_value\_get}}(prefs\_data, \textcolor{stringliteral}{"main:universe"}, \&type, \&value))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \mbox{\hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}}(\&value, 42);}
\DoxyCodeLine{        \mbox{\hyperlink{group__Elm__Prefs_gaae2c2d085d8c0e9d6c24ff580807a54e}{elm\_prefs\_data\_value\_set}}(prefs\_data, \textcolor{stringliteral}{"main:universe"}, type, \&value);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (\mbox{\hyperlink{group__Elm__Prefs_ga2b692083e7d307512858f1adc2563519}{elm\_prefs\_data\_value\_get}}(prefs\_data, \textcolor{stringliteral}{"main:text"}, \&type, \&value))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \mbox{\hyperlink{group__Eina__Value__Value__Group_ga9a1407c953694c3d6f40ab01ebe9a294}{eina\_value\_set}}(\&value, \textcolor{stringliteral}{"This is a text field (:"});}
\DoxyCodeLine{        \mbox{\hyperlink{group__Elm__Prefs_gaae2c2d085d8c0e9d6c24ff580807a54e}{elm\_prefs\_data\_value\_set}}(prefs\_data, \textcolor{stringliteral}{"main:text"}, type, \&value);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}}(notify);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


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