Thread: ncurses scrollok scrolling contents of Pad up & down

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    43

    ncurses scrollok scrolling contents of Pad up & down

    I have created a pad using ncurses. newpad(rows, screen_width).

    I'm trying to get the contents to scroll. If I use the function scrollok(win_pointer, TRUE), then the cursor is set at the end of the pad and contents scroll up as new lines are generated and added to the content of win_pointer. However, I would like to be able to scroll back up (ie, the older lines) which have gone out of the win_pointer view area. I'm only able to scroll the number of lines generated in the current view.

    Code:
    WIN *p
    
    p = newpad(10, some_width);
    scrollok(p, TRUE);
    
    while(1)
    {
        waddstr(p, text_buffer);   /* text_buffer is continuously refreshing */
        prefresh(p, row, 0, 0, 0, 20, some_width);
        
        switch(wgetch(p))
        {
            case KEY_UP:
                    row -= 2;
                    if(row < 0) row = -1;
                    break;
                
            case KEY_DOWN:
                    row += 1;
                    if(row > 20) v->row = 20;
                    break;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You know, an actual compilable example would be a bonus, so we all don't have to go and independently read all about ncurses pads, and try to guess what "text_buffer is continuously refreshing" means (not to mention all the other stuff you've left out).
    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 2016
    Posts
    43
    After toying around with this for a few months I finally ended up with a desired solution.

    Code:
    void update_pad(void *V)
    {
        WIN *v = V;
        unsigned int i, ypos, xpos;
        int key;
        
        while((key = wgetch(v->p)) != 'q')
        {
            if(v->buffer[0] != '\0')
            {
                waddstr(v->p, v->buffer);
                v->buffer[0] = '\0';
                
                getyx(v->p, ypos, xpos);        /* Get the cursor position */
                
                if(ypos > v->starty + v->height - 1)
                    v->row = ypos - (v->starty + v->height - 1);
            }
    
            wrefresh(v->w);
            prefresh(v->p, 
                v->row, 0,                        /* pmin        */
                v->starty, v->startx,             /* smin        */
                                                /* smax        */
                v->starty + v->height - 1, v->startx + v->width - 1);
            
            if(key == KEY_UP)
            {
                v->row -= 2;
                if(v->row < 0) v->row = -1;
            }
            
            if(key == KEY_DOWN) 
                v->row += 1;
                
            if(key == KEY_RESIZE)
            {
                getmaxyx(stdscr, v->rows, v->cols);
                 v->height     = v->rows - 4 * LENGTH(Instr) - 1;
                 v->width     = v->cols;
                 delwin(v->w);
                 v->w        = create_newwin(4 * LENGTH(Instr) + 1, v->width, v->height, 0);
            }
        }
        
        quit = true;
    }

    Of-course in conjunction with:

    Code:
    void init_display(WIN *v)
    {
        initscr();
        raw();
        noecho();
        curs_set(0);            /* Hide the cursor */
         
        getmaxyx(stdscr, v->rows, v->cols);
        
        v->startx = v->starty = 0;
        v->height = v->rows - 4 * LENGTH(Instr) - 1;
        v->width = v->cols;
        v->row = 0;
            
        v->w = create_newwin(4 * LENGTH(Instr) + 1, v->width, v->height, 0);
        v->p = newpad(ARRAY_SIZE, v->width);
        
        /* leaveok(v->p, TRUE);     Show the hardware cursor moving */
        keypad(v->p, TRUE);
        nodelay(v->p, TRUE);
        scrollok(v->p, TRUE);
         
        v->buffer[0] = '\0';
    }
    Last edited by wiqxlzxsxj; 10-24-2016 at 05:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with ncurses
    By rakesh_01 in forum C Programming
    Replies: 2
    Last Post: 08-31-2009, 05:05 AM
  2. Ncurses Lib
    By lautarox in forum C Programming
    Replies: 6
    Last Post: 09-24-2008, 06:12 PM
  3. Ncurses or not?
    By Zarniwoop in forum C Programming
    Replies: 4
    Last Post: 05-04-2008, 11:19 AM
  4. nCurses
    By MethodMan in forum Linux Programming
    Replies: 6
    Last Post: 02-18-2003, 07:29 AM
  5. Ncurses and WINDOW scrolling
    By birkoss in forum Linux Programming
    Replies: 0
    Last Post: 05-17-2002, 11:47 AM

Tags for this Thread