You are not logged in.

#1 2012-06-28 18:32:37

Simple Boot
Member
Registered: 2012-06-19
Posts: 21

[ SOLVED ] C and ncurses - window not moving

Hello, everyone smile

Can anyone tell me why this ( see below ) code does not produce any visible output ( child window is not moving ) ?

#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(mainwin, 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;
}

-- source paulgriffiths.net

EDIT: It seems that the problem is that it is not picking up arrow keys - if I bind those movements to any other keys everything works. Why ?

Last edited by Simple Boot (2012-06-29 17:39:37)


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Offline

#2 2012-06-28 23:48:37

dickey
Member
Registered: 2009-06-01
Posts: 35

Re: [ SOLVED ] C and ncurses - window not moving

Someone asked about this particular example early this year in another forum.
I pointed out that mvwin changed (per bug report) in 2006.

However, no comment was made about the keys.

In a quick check (replacing the mvwin with

move(y,x); printw("%d,%d", y, x);

I can see that the problem is that because the mvwin does not actually move the window (as the old behavior was), there is no visible effect.

For reference:
http://invisible-island.net/ncurses/NEW … #t20120114

+ several improvements to test/movewindow.c (prompted by discussion on
      Linux Mint forum):
      + modify movement commands to make them continuous
      + rewrote the test for mvderwin
      + rewrote the test for recursive mvwin

http://invisible-island.net/ncurses/NEW … #t20060218

+ remove 970913 feature for copying subwindows as they are moved in
      mvwin() (discussion with Bryan Christ).

http://invisible-island.net/ncurses/NEWS.html#t970913

+ Modified lib_mvwin.c:  Disable move of a pad.  Implement (costly)
      move of subwindows.  Fixed update behavior of movements of regular
      windows.

Offline

#3 2012-06-29 01:08:57

marxav
Member
From: Gatineau, PQ, Canada
Registered: 2006-09-24
Posts: 386

Re: [ SOLVED ] C and ncurses - window not moving

Use wrefresh(childwin) just after mvwin(childwin, y, x);.  But the box dox lines do not clean up automatically.

Offline

#4 2012-06-29 02:49:04

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [ SOLVED ] C and ncurses - window not moving

marxav wrote:

Use wrefresh(childwin) just after mvwin(childwin, y, x);.  But the box dox lines do not clean up automatically.

A look here has a bit about cleaning up the box lines. In fact, that whole tutorial is a really awesome way to learn ncurses.

Last edited by /dev/zero (2012-06-29 02:49:56)

Offline

#5 2012-06-29 17:39:22

Simple Boot
Member
Registered: 2012-06-19
Posts: 21

Re: [ SOLVED ] C and ncurses - window not moving

/dev/zero wrote:
marxav wrote:

Use wrefresh(childwin) just after mvwin(childwin, y, x);.  But the box dox lines do not clean up automatically.

A look here has a bit about cleaning up the box lines. In fact, that whole tutorial is a really awesome way to learn ncurses.

Thank you both - everything's fine now smile


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Offline

Board footer

Powered by FluxBB