title: scrolling menu ( c code )
body: <p>I want to create a menu with max 10 line window, and display in this window all items from a xml file ( containing more than 10 items), by scrolling up/down line in window to view all content of file.</p>
  
  <p>Questions: How make this possible in my API (like in ncurses scroll menu):</p>
  
  <p>"If the sub window given for a window is not big enough to show all the items, then the menu will be scrollable. When you are on the last item in the present list, if you send REQ_DOWN_ITEM, it gets translated into REQ_SCR_DLINE and the menu scrolls by one item. You can manually give REQ_SCR_ operations to do scrolling. Let's see how it can be done."</p>
  
  <p>Part of my code for this feature:</p>
  
  <pre><code>static void menu( commands_t *cmd )
  {
      /* this display only first 10 line */
      int i; 
      char string[ 128 ];
      for( i = 0; i &lt; 10; i++ ) {
          snprintf( string, sizeof (string), "%s", list_get_name( cmd-&gt;listmgr, i ) );
          menu_list_set_text( cmd-&gt;menu, i, string );
      }
  }
  </code></pre>
  
  <p>Currently, this function can display only the first 10 items from list.</p>
  
  <pre><code>                       A
                         B
                         C  
                         D
                         E
                  -----------------
  Line 01         |      F        |   
  Line 02         |      G        |
  Line 03         |      H        |
  Line 04         |      I        |
  Line 05         |      J        |    
  Line 06         |      K        |   
  Line 07         |      L        |
  Line 08         |      M        |
  Line 09         |      N        |
  Line 10         |      O        |   
                  -----------------
                         P
                         Q
                         R
                         S
                         T
                      .......
                         Z
  </code></pre>
  
  <p>For this I try to create API capable to set text in menu line from list, move up/down cursor in menu but I don't now how to make to move lines up or down.</p>
  
  <pre><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;
  };
  
  const list_info_t *list_get_list( list_mgr_t *mgr, int pos)
  {
      const list_info_t *tmp = mgr-&gt;first;
      int i;
      for (i = 0; tmp &amp;&amp; i &lt; pos; i++)
          tmp = tmp-&gt;next;
      return tmp;
  }
  
  const char *list_get_name( list_mgr_t *mgr, int position )
  {
      const list_info_t *list = list_get_list(mgr, position);
      if( (list) &amp;&amp; (*list-&gt;name) ) {
          return list-&gt;name;
      } else {
          return 0;
      }
  }
  
  TO MOVE UP/DOWN:
  
  void commands_handle( commands_t *cmd, int prog_cmd )
  {
      switch( prog_cmd ) {
      case MENU_UP:
          cmd-&gt;menu_position = (cmd-&gt;menu_position + cmd-&gt;menu_size - 1) % (cmd-&gt;menu_size);
          menu( cmd );
          break;
      case MENU_DOWN:
          cmd-&gt;menu_position = (cmd-&gt;menu_position + 1) % (cmd-&gt;menu_size);
          menu( cmd );
          break;
      return;
      }
  } 
  
  
  MENU:
  
  
  static void menu( commands_t *cmd )
  {
      /* this display only first 10 line */
      int i; 
      char string[ 128 ];
      for( i = 0; i &lt; 10; i++ ) {
          snprintf( string, sizeof (string), "%s", list_get_name( cmd-&gt;listmgr, i ) );
          menu_list_set_text( cmd-&gt;menu, i, string );
      }
  }
  </code></pre>
  
  <p>Windows of menu is for max 10 items lines because xml may contain a large number of items.
  What programming options have to display all lines from file to be visible on menu by presing UP/DOWN button?</p>
  
  <p>This is actual API for menu:</p>
  
  <pre><code>#define MENU_MAX 10
  
  struct menu_s
  {
      char *name;
      char text[ MENU_MAX ][ 128 ];
      char arguments[ MENU_MAX ][ 128 ];
      int commands[ MENU_MAX ];
      char back_argument[ 128 ];
      int back_command;
      int numlines;
      int cursor;
      int defaultcursor;
  };
  
  menu_t *menu_new( const char *name )
  {
      menu_t *menu = malloc( sizeof( menu_t ) );
      if( !menu ) {
      return 0;
      }
  
      menu-&gt;numlines = 0;
      menu-&gt;cursor = 0;
      menu-&gt;defaultcursor = 0;
      menu-&gt;name = strdup( name );
      if( !menu-&gt;name ) {
          free( menu );
          return 0;
      }
  
      return menu;
  }
  
  void menu_delete( menu_t *menu )
  {
      free( menu-&gt;name );
      free( menu );
  }
  
  void menu_reset_num_lines( menu_t *menu )
  {
      menu-&gt;numlines = 0;
  }
  
  void menu_set_text( menu_t *menu, int line, const char *text )
  {
      snprintf( menu-&gt;text[ line ], sizeof( menu-&gt;text[ 0 ] ), "%s", text );
      if( line &gt;= menu-&gt;numlines ) menu-&gt;numlines = line + 1;
  }
  
  void menu_set_enter_command( menu_t *menu, int line, int command,
                           const char *argument )
  {
      menu-&gt;commands[ line ] = command;
      snprintf( menu-&gt;argum#define MENU_MAX 10
  
  struct menu_s
  {
      char *name;
      char text[ MENU_MAX ][ 128 ];
      char arguments[ MENU_MAX ][ 128 ];
      int commands[ MENU_MAX ];
      char back_argument[ 128 ];
      int back_command;
      int numlines;
      int cursor;
      int defaultcursor;
  };
  
  menu_t *menu_new( const char *name )
  {
      menu_t *menu = malloc( sizeof( menu_t ) );
      if( !menu ) {
      return 0;
      }
  
      menu-&gt;numlines = 0;
      menu-&gt;cursor = 0;
      menu-&gt;defaultcursor = 0;
      menu-&gt;name = strdup( name );
      if( !menu-&gt;name ) {
          free( menu );
          return 0;
      }
  
      return menu;
  }
  
  void menu_delete( menu_t *menu )
  {
      free( menu-&gt;name );
      free( menu );
  }
  
  void menu_reset_num_lines( menu_t *menu )
  {
      menu-&gt;numlines = 0;
  }
  
  void menu_set_text( menu_t *menu, int line, const char *text )
  {
      snprintf( menu-&gt;text[ line ], sizeof( menu-&gt;text[ 0 ] ), "%s", text );
      if( line &gt;= menu-&gt;numlines ) menu-&gt;numlines = line + 1;
  }
  
  void menu_set_enter_command( menu_t *menu, int line, int command,
                           const char *argument )
  {
      menu-&gt;commands[ line ] = command;
      snprintf( menu-&gt;arguments[ line ], sizeof( menu-&gt;arguments[ 0 ] ),
            "%s", argument );
  }
  
  void menu_set_back_command( menu_t *menu, int command,
                          const char *argument )
  {
      menu-&gt;back_command = command;
      snprintf( menu-&gt;back_argument, sizeof( menu-&gt;back_argument ),
            "%s", argument );
  }
  
  void menu_set_cursor( menu_t *menu, int cursor )
  {
      menu-&gt;cursor = cursor;
  }
  
  const char *menu_get_name( menu_t *menu )
  {
      return menu-&gt;name;
  }
  
  int menu_get_num_lines( menu_t *menu )
  {
      return menu-&gt;numlines;
  }
  
  const char *menu_get_text( menu_t *menu, int line )
  {
      return menu-&gt;text[ line ];
  }
  
  int menu_get_enter_command( menu_t *menu, int line )
  {
      return menu-&gt;commands[ line ];
  }
  
  const char *menu_get_enter_argument( menu_t *menu, int line )
  {
      return menu-&gt;arguments[ line ];
  }
  
  int menu_get_back_command( menu_t *menu )
  {
      return menu-&gt;back_command;
  }
  
  const char *menu_get_back_argument( menu_t *menu )
  {
      return menu-&gt;back_argument;
  }
  
  int menu_get_cursor( menu_t *menu )
  {
      return menu-&gt;cursor;
  }
  
  int menu_get_default_cursor( menu_t *menu )
  {
      return menu-&gt;defaultcursor;
  }
  </code></pre>
  
  <p>I tried a  ugly solution:</p>
  
  <pre><code>static void menu( commands_t *cmd )
  {
      int i;
      for( i = 0; i &lt; 10; i++ ) {
          char string[ 128 ];
          snprintf( string, sizeof (string), "%s", list_get_name( cmd-&gt;listmgr, i )
          menu_list_set_text( cmd-&gt;menu, i, string );
          int scroll;
          for( scroll = 0; scroll &lt; list_size; scroll++ ) {
              if( cmd-&gt;curmenupos == 10 + scroll ) {
                  snprintf( string, sizeof (string), "%s", list_get_name( cmd-&gt;listmgr, i + scroll + 1 )
                  menu_list_set_text( cmd-&gt;menu, i, string );
              }
           }
        }
     }
  </code></pre>
  
  <p>where "cmd->curmenupos" is command to get menu line position and
        "list_size" is number of items from xml</p>