Thread: Ncurses: Unable to move windows with mvwin !

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Ncurses: Unable to move windows with mvwin !

    Hello friends,

    Please have a look at this code which I googled across internet:
    Code:
    /*
    
      CURWIN1.C
      =========
      (c) Copyright Paul Griffiths 1999
      Email: [email protected]
    
      Moving windows with ncurses.
    
    */
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <curses.h>
    
    
    int main(void) {
        
        WINDOW * mainwin, * childwin;
        int      ch;
    
    
        /*  Set the dimensions and initial
        position for our child window   */
    
        int      width = 23, height = 7;
        int      rows  = 25, cols   = 80;
        int      x = (cols - width)  / 2;
        int      y = (rows - height) / 2;
    
    
        /*  Initialize ncurses  */
    
        if ( (mainwin = initscr()) == NULL ) {
        fprintf(stderr, "Error initialising ncurses.\n");
        exit(EXIT_FAILURE);
        }
        
    
        /*  Switch of echoing and enable keypad (for arrow keys)  */
    
        noecho();
        keypad(stdscr, TRUE);
    
    
        /*  Make our child window, and add
        a border and some text to it.   */
    
        childwin = subwin(mainwin, height, width, y, x);
        box(childwin, 0, 0);
        mvwaddstr(childwin, 1, 4, "Move the window");
        mvwaddstr(childwin, 2, 2, "with the arrow keys");
        mvwaddstr(childwin, 3, 6, "or HOME/END");
        mvwaddstr(childwin, 5, 3, "Press 'q' to quit");
    
        refresh();
    
    
        /*  Loop until user hits 'q' to quit  */
    
        while ( (ch = getch()) != 'q' ) {
    
        switch ( ch ) {
    
        case KEY_UP:
            if ( y > 0 )
            --y;
            break;
    
        case KEY_DOWN:
            if ( y < (rows - height) )
            ++y;
            break;
    
        case KEY_LEFT:
            if ( x > 0 )
            --x;
            break;
    
        case KEY_RIGHT:
            if ( x < (cols - width) )
            ++x;
            break;
    
        case KEY_HOME:
            x = 0;
            y = 0;
            break;
    
        case KEY_END:
            x = (cols - width);
            y = (rows - height);
            break;
    
        }
    
        mvwin(childwin, y, x);
        }
    
    
        /*  Clean up after ourselves  */
    
        delwin(childwin);
        delwin(mainwin);
        endwin();
        refresh();
    
        return EXIT_SUCCESS;
    }
    I am unable to move the subwin from the arrow keys. Please tell me where I am wrong. Actually, Even with my personal code the sub windows are not moving. Here is my code:
    Code:
    void
    move_window_center(WINDOW *parent, WINDOW *child)
    {
        int p_y, p_width;
        int c_y, c_width;
        int indent;
        
        getmaxyx(parent, p_y, p_width);
        (void) p_y;
        
        getmaxyx(child, c_y, c_width);
        (void) c_y;
    
        indent = p_width - c_width;
        indent /= 2;
        
        mvwin(child, 0, indent);
        wrefresh(child);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This should be moved to the Windows forum.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by claudiu View Post
    This should be moved to the Windows forum.
    Hello,

    I think the post is in correct location because its question with ncurses for linux (ubuntu) and its completely a c programming problem.

    Thanks.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are right, my apologies, I misread the question entirely.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > I am unable to move the subwin from the arrow keys
    I don't see anything in your code that would accept arrow keys.

    At any rate, have you tried compiling/running the code you found? I don't know Ncurses, but if the example is incorrect, there's no point using it.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    You need to wrefresh after the mvwin. Also you might want to get rid of the box. It does not seem to be redrawn properly. And accodring to the documentation, moving a subwindow is not recommended (man mvwin). You might want to rethink your strategy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ncurses & windows
    By gavra in forum C Programming
    Replies: 2
    Last Post: 07-22-2008, 09:26 AM
  2. ncurses and windows
    By Swarvy in forum Windows Programming
    Replies: 3
    Last Post: 04-10-2008, 08:24 PM
  3. Replies: 5
    Last Post: 01-10-2006, 05:55 PM
  4. ncurses, windows, and colors
    By subflood in forum Linux Programming
    Replies: 2
    Last Post: 08-26-2004, 10:14 AM
  5. Ncurses Windows
    By Karl in forum C Programming
    Replies: 0
    Last Post: 02-20-2004, 03:29 PM

Tags for this Thread