Thread: make a loop for scrolling lines in menu box

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    make a loop for scrolling lines in menu box

    Hello, I want to make menu with 10 line size and scroll text line by line in menu from a list with text.
    For this I wrote same command as:

    Code:
    menu_set_text /* set text a line position in menu */
    get_line_text /* return text from list at line position specified */
    list_size /* return number of list lines */
    To display the top 10 lines from list:

    Code:
    for( i = 0; i < 10; i++ ) {
        char text[ 50 ];
        snprintf( text, sizeof (text), "%s", get_line_text( i ) );
        menu_set_text( i, text );
    }
    I would like to create a loop to scroll up/down lines in menu all items from list



    Code:
                           A
                           B
                           C   
                           D
                           E
                    -----------------
    Line 01         |      F        |   /* <-- pozition of cursor stay in Line 1 if move up and exist item of list to be scrolled */
    Line 02         |      G        |
    Line 03         |      H        |
    Line 04         |      I        |
    Line 05         |      J        |     
    Line 06         |      K        |   /* <-- pozition of cursor move up (from line 1 to 10) or down (from line 10 to 1) in menu */
    Line 07         |      L        |
    Line 08         |      M        |
    Line 09         |      N        |
    Line 10         |      O        |   /* <-- pozition of cursor stay in line 10  if move down and exist item of list to be scrolled */
                    -----------------
                           P
                           Q
                           R
                           S
                           T
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems a familiar problem...
    how to optimize "for" statement

    You even share the same ISP.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by Salem View Post
    Seems a familiar problem...
    how to optimize "for" statement

    You even share the same ISP.
    I have nothing to do with the "how to optimize "for" statement" post. If so considered, can close my post.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to post more code and an actual question if you want help.

    Because all I see is a handful of lines of code, and a list of things you would like to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Code:
    typedef struct list_info_s list_info_t;
    struct list_info_s
    {
        int position;
        char name[ 50 ];
    
        list_info_t *next;
        list_info_t *prev;
    };
    
    struct list_mgr_s
    {
        int num_lists;
    };
    
    int list_get_num_lists( list_mgr_t *mgr )
    {
        return mgr->num_lists;
    }
    
    const char *list_get_name( list_mgr_t *mgr, int position )
    {
        const list_info_t *list = list_get_list(mgr, position);
        if( (list) && (*list->name) ) {
            return list->name;
        } else {
            return 0;
        }
    }
    
    #define MENU_LIST_MAX_LINES 10
    
    
    menu_t *menu_new( const char *name )
    {
        menu_t *menu = malloc( sizeof( menu_t ) );
        if( !menu ) {
            return 0;
        }
    
        menu->numlines = 0;
        menu->name = strdup( name );
        if( !menu->name ) {
            free( menu );
            return 0;
        }
        return menu;
    }
    
    struct menu_list_s
    {
        int numlines;
        menu_string_t *lines[ MENU_LIST_MAX_LINES ];
    };
    
    void menu_list_set_text( menu_list_t *menul, int line, const char *text )
    {
        if( line < MENU_LIST_MAX_LINES ) {
            menu_string_show_text( menul->lines[ line ], text, 100 );
        }
    }
    
    void menu_list_set_lines( menu_list_t *menul, int numlines )
    {
        if( numlines > MENU_LIST_MAX_LINES ) numlines = MENU_LIST_MAX_LINES;
        menu->numlines = numlines;
    }
    
    void program_menu_list_set_lines( program_menu_t *menu, int numlines )
    {
        menu_list_set_lines( menu->list, numlines );
    }
    
    void program_menu_list_set_text( program_menu_t *menu, int line, const char *text )
    {
        menu_list_set_text( menu->list, line, text );
    }
    
    static void view_menu( commands_t *cmd )
    {
        int i;
    
        const int max_num_lines = 10;
        const int num_lists = list_get_num_lists( cmd->listmgr );
        char text[ 50 ];
        program_menu_list_set_lines( cmd->menu, num_lists + 1 );
        for( i = 0; i < num_lists; i++ ) {
            snprintf( text, sizeof (text), "%s", list_get_name( cmd->listmgr, i ) );
            program_menu_list_set_text( cmd->menu, i, text );
        }
        cmd->menusize = num_lists;
    }
    
    struct commands_s {
        program_menu_t *menu;
        list_mgr_t *listmgr;
    
        int menuactive;
        int menuposition;
        int menusize;
    };
    
    void commands_handle( commands_t *cmd, int program_cmd )
    {
        if( cmd->menuactive && program_is_menu_command( program_cmd ) ) {
            switch( program_cmd ) {
            case MENU_EXIT:
                menu_off( cmd );
                break;
            case MENU_UP:
                cmd->menuposition = (cmd->menuposition + cmd->menusize - 1) % (cmd->menusize);
                view_menu( cmd );
                break;
            case MENU_DOWN:
                cmd->menuposition = (cmd->menuposition + 1) % (cmd->menusize);
                view_menu( cmd );
                break;
            case SHOW_MENU:
                if( cmd->menu ) {
                    commands_clear_menu_positions( cmd );
                    view_menu( cmd );
                }
                break;
            }
            return;
        }
    }
    This create entry in list (for example in xml file:

    Code:
        <name="GREEN" />
        <name="RED" />
        <name="BLUE" />
        <name="BLACK" />
        <name="BROWN" /
        ...............................
    I want to create an menu ( view_menu( commands_t *cmd ) ) with maxim 10 line and display all items from xml file by scrolling up/down line by line to view all content of file.

    In my API menu is capable to set: number of lines in menu (maxim 10), display text in line, move up/down cursor in menu,

    Menu is set to 10 lines max because xml may contain a large number of items.

    I my actual code, menu is able to display only first 10 items of xml.
    Last edited by cecylia; 01-04-2013 at 02:01 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not going to read through all your code yet, because you say you can only display the first 10 menu items.

    You need to take that diagram you made in post #1, draw it out on paper, and start trying things out by hand. If you can't do it yourself, there's no way you can program a computer to do it. Some tips to get you started:

    You need to keep track of which menu items are on the screen and which line in the menu your cursor is on. I would have the following variables:
    num_items - the total number of menu items to display (20 in your example in post #1)
    menu_size - the number of items you can fit in the menu (10 in the example)
    start_item - the first item currently displayed on the menu ("F" in your example)
    end_item - the last item currently displayed on the menu ("O" in your example)
    row_selected - the row the cursor is on, i.e. what the user would select (not in your example, assume "J" for starters)

    Next, you need to think about how those variables change each time you press the up/down key. Note, num_items and menu_size will never change. Under what circumstances does pressing up/down change the values of start_item, end_item and row_selected? Think about the general case (up arrow makes row_selected smaller by 1, down arrow makes it bigger by 1). Also think about the edge cases, e.g. when you are at the top of the screen and go up. What is the value of row_selected at the top of the screen? What is it after you press the up key (does it change)? What happens to start_item and end_item in that case? What happens if row_selected is 1, and start_item is also at the beginning of the list (1 or 0 depending on how you index it)? Do you change anything?

    Go through all the possible scenarios for moving up and down, and make sure you try menus that are small and don't require scrolling (e.g. 6 elements), menu with exactly the same number of items as menu_size (e.g. 10) and menus with more than menu_size items.

    Write down all your logic as you do that, and turn it into code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make this 10 lines more elegant?
    By Ignazio Calò in forum C Programming
    Replies: 2
    Last Post: 06-08-2011, 04:19 PM
  2. Need help With Scrolling Text - Wanna make Dialog Credits...
    By jekko in forum Windows Programming
    Replies: 10
    Last Post: 02-28-2004, 02:39 AM
  3. "scrolling" menu bars
    By Lurker in forum Windows Programming
    Replies: 0
    Last Post: 05-25-2003, 06:08 PM
  4. Scrolling Menu Bar menus
    By FillYourBrain in forum Windows Programming
    Replies: 3
    Last Post: 02-15-2003, 11:02 AM
  5. Blank Lines in a menu
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2002, 03:51 PM