This example creates a simple genlist with a small number of items and a callback that is called whenever an item is selected.
All the properties of this genlist are the default ones. The full code for this example can be seen at genlist_example_01.c.
For the simplest list that you plan to create, it's necessary to define some of the basic functions that are used for creating each list item, and associating them with the "item class" for that list. The item class is just an struct that contains pointers to the specific list item functions that are common to all the items of the list.
Let's show it by example. Our item class is declared globally and static as it will be the only item class that we need (we are just creating one list):
This item class will be used for every item that we create. The only functions that we are going to set are
icon_get. As the name suggests, they are used by the genlist to generate the label for the respective item, and to generate icon(s) to it too. Both the label and icon get functions can be called more than once for each item, with different
part parameters, which represent where in the theme of the item that label or icon is going to be set.
The default theme for the genlist contains only one area for label, and two areas for icon ("elm.swallow.icon" and "elm.swallow.end"). Since we just want to set the first icon (that will be at the left side of the label), we compare the part name given with "elm.swallow.icon". Notice that the
label_get function must return a strduped string, that will be freed later automatically by the list. Here's the code for
We will also provide a function that will be called whenever an item is selected in the genlist. However, this function is not part of the item class, it will be passed for each item being added to the genlist explicitly. Notice the similarity of the function signature with those used by
Now let's show the code used for really creating the list. Skipping boilerplate code used for creating a window and background, the first piece of code specific to our genlist example is setting the pointer functions of the item class to our above defined functions:
Notice that we also choose to use the "default" style for our genlist items. Another interesting point is that
del are set to
NULL, since we don't need these functions now.
del doesn't need to be used because we don't add any data that must be freed to our items, and
state_get is also not used since all of our items are the same and don't need to have different states to be used for each item. Finally we create our list:
Now we append several items to the list, and for all of them we need to give the list pointer, a pointer to the item class, the data that will be used with that item, a pointer to the parent of this item if it is in a group type list (this is not the case so we pass
NULL), possible flags for this item, the callback for when the item is selected, and the data pointer that will be given to the selected callback.
The rest of the code is also common to all the other examples, so it will be omitted here (look at the full source code link above if you need it).
You can try to play with this example, and see the selected callback being called whenever an item is clicked. It also already has some features enabled by default, like vertical bounce animation when reaching the end of the list, automatically visible/invisible scrollbar, etc. Look at the Genlist - list setup functions to see an example of setting these properties to the list.
The current example will look like this when running: