[comp.lang.c] Question about curses

jsv9504@ultb.isc.rit.edu (J.S. Veiss) (01/14/90)

I'm trying to print graphics characters from the alternate character set
and another character set I created in a program using the curses library.
I'm using standard ANSI escape codes to access the character sets.
I've gotten things down to be the only way the print the escape codes to 
change the character set is to print it to stdout instead of stdscr.  
However, when I print with stdout, I don't know where it's going to go on 
the screen.  It's really wierd, so bear with me here.

I'm passing a row and a column into a function.  I'm printing out numbers with
printw above each of the objects I'm printing with printf, followed by a
refresh.  Basically, it looks kinda like this:

PrintCard (row, col)

int row, col;

{
   move (row, col + 3);
   printw ("%d", col / 10);
   move (++row, col);
   printf ("%slqqqqqk%s", GRPH, ASCII);
   refresh ();
}

'col' comes in as multiples of 10.  GRPH is the escape code for the graphics
set.  ASCII is the escape code to switch back to the ascii character set.

what gets printed (in the correct row) is:

             1                 2           4     3   5
          +-----+           +-----+     +-----+-----+      <-graphics chars.

what should be printed is:
            1         2         3         4         5
         +-----+   +-----+   +-----+   +-----+   +-----+

I have to use printf because printw literally prints the escape codes as text
(i.e. the terminal doesn't get the escape command).  It seems as if the 'col'
variable I'm passing into move is getting screwed up, 'cuz if I print out the
column number at the top of the screen (to check) before I print each of the 
above entities out, the numbers print fine (in the right places.)

I've been working on this for many hours.  Think you can help?

I'd appreciate any help...

Jepher

daveh@marob.masa.com (Dave Hammond) (01/16/90)

In article <1911@ultb.isc.rit.edu> (J.S. Veiss) writes:
>
>I'm trying to print graphics characters from the alternate character set
>and another character set I created in a program using the curses library.
>I'm using standard ANSI escape codes to access the character sets.
>I've gotten things down to be the only way the print the escape codes to 
>change the character set is to print it to stdout instead of stdscr.
>[...]
>PrintCard (row, col)
>int row, col;
>{
>   move (row, col + 3);
>   printw ("%d", col / 10);
>   move (++row, col);
>   printf ("%slqqqqqk%s", GRPH, ASCII);
>   refresh ();
>}

Curses has no idea that "%slqqqqqk%s" is on screen, so it will
unwittingly overwrite portions of the box rule with spaces, whenever it
determines that the surrounding display area must be refreshed.  You
generally can not intermix direct display output with curses output,
because (1) curses has no idea what you are placing on screen, and (2)
curses buffers output until a refresh(), making it difficult to
coordinate direct output with curses output.

The only way to ensure that your direct output survives is to re-output
ALL direct output after EACH refresh().  The coordination effort can be
minimized by only refreshing immediately prior to reading user input
events.  If you only have a single event processing loop, then this
becomes fairly easy.  Of course, there is still the overhead imposed by
redrawing every box rule, before every input event.

As a point of information, newer curses implementations may support the
terminfo A_ALTCHARSET attribute, which will switch in and out of the
display's alternate character set.  Basically, you would code the above as:

PrintCard (row, col)
int row, col;
{
   move (row, col + 3);
   printw ("%d", col / 10);
   move (++row, col);
   attron(A_ALTCHARSET);
   addstr("lqqqqqk");
   attroff(A_ALTCHARSET);
   refresh ();
}

However, it should be noted that this option is highly unportable.

--
Dave Hammond
daveh@marob.masa.com
uunet!masa.com!marob!daveh