Rockbox Development > Starting Development and Compiling

Dynamic text in menu items (was: something related to rb->snprintf and menus)

<< < (2/2)

torne:
The menu macros are not function calls that happen at runtime, they are datastructure creation macros which happen at compile time. You can only pass constant, known-at-compile-time values into them. To have dynamic menus you need to do it differently, by providing a function which can be called at runtime to return the appropriate string. See MENUITEM_RETURNVALUE_DYNTEXT and similar as bluebrother suggested.

gbl08ma:
So, if I my conclusions I took from looking at the MENUITEM_RETURNVALUE_DYNTEXT definition on menu.h, and from observing the only practical example of using this macro existent in SVN (I did a recursive grep), located at root_menu.c (it's the resume playback/go to WPS item menu), I'm under the impression that to use this macro I need to do like this:

(kind of C-pseudo-code, "coded" directly on the reply textbox)

--- Code: ---bool show_test_menu(void)
{
    char[14] item1text = "Item 1 example";
    char[14] item2text = "Item 2 example";
    MENUITEM_RETURNVALUE_DYNTEXT(dyn_item1, CALL1, NULL, item1text,
            NULL, NULL, ICON_NOICON);
    MENUITEM_RETURNVALUE_DYNTEXT(dyn_item2, CALL2, NULL, item2text,
            NULL, NULL, ICON_NOICON);
    /* I don't know if ICON_NOICON exists... */

    /*and then tell to draw the menu... */

    MAKE_MENU(test_menu, "Test menu",
            item_callback, Icon_Rockbox,
            &dyn_item1, &dyn_item2);
}

/*have a function that handles item selection... */
static int item_callback(int action, const struct menu_item_ex *this_item)
{
    /* a switch would be in here... just not sure what to switch between...? between CALL1 and  CALL2? */
}


--- End code ---

So, am I correct and this is the way I should create a menu with dynamic items, or what is wrong or missing? Also please answer my questions on the "code"... I plan to "wikify" this once I get it working, so it is easier for other people to show dynamic information on the menu items.

--Edit--

Well, from another, deeper looking it seems that the function that I should use is MENUITEM_FUNCTION_DYNTEXT, which is the "same" as MENUITEM_FUNCTION, but for dynamic text. Just what I want, except that I don't seem to need a so complex thing:


--- Code: ---/* As above, except the text is dynamic */
#define MENUITEM_FUNCTION_DYNTEXT(name, flags, func, param,                 \
                                  text_callback, voice_callback,            \
                                  text_cb_data, callback, icon)             \
    static const struct menu_get_name_and_icon name##_                      \
        = {callback,text_callback,voice_callback,text_cb_data,icon};        \
    static const struct menu_func name##__ = {{(void*)func}, param};           \
    static const struct menu_item_ex name   =                                  \
        { MT_FUNCTION_CALL|MENU_DYNAMIC_DESC|flags,                            \
         { .function = & name##__}, {.menu_get_name_and_icon = & name##_}};

--- End code ---
What exactly should I put on the parameters name, flags, func, param, text_callback, voice_callback, text_cb_data, callback and icon? I know some of them seem pretty obvious on what they are, but what kind of data (char, int, some specific struct, pointer to a function or macro, etc.) should I pass to them, and are they all the parameters required (can't I just put NULL)? For example, I don't have a voice_callback as plugins don't have lang files.

From looking at an "example" at /apps/audio_eq_menu.c (a pretty complex example, by the way, but it was one of the few available on the code), it seems I should setup a function and various structures for each menu item...

Isn't there a simpler way to do this? Using MENUITEM_FUNCTION seems to be waaay simpler, but it doesn't support dynamic content, does it?
How does the system uptime menu under the System menu show these dynamic menu items with a time counter (that is updated on real-time)? Or isn't that a menu (but something that looks like one)? I can't find the file where that menu is coded, I'll keep on searching the files...

Thanks for all the help (I know that, actually, it seems it'd be less work for you guys, that are trying to help me, if I didn't try code when I can't...)

Navigation

[0] Message Index

[*] Previous page

Go to full version