
#include "savescr.h"

void WaitForSpace(ExtendedCursesWindow& ECW)
{
  ECW.refresh();
  while (ECW.getch()!=' ')
    ;
  ECW.addch('');
}

#define WAIT WaitForSpace(uW)

main()
{
  ExtendedCursesWindow uW;
  int i;

  for (i=0; i<=15; ++i)
    {
      uW.printw("This is the %d iteration.\n",i);
      uW.refresh();
    }

  uW.printw("Before saving a screen, savedScreenAvailable returns %d.\n\n",
	    uW.savedScreenAvailable());
  WAIT;
  uW.saveScreen();
  WAIT;
  uW.printw("After saving a screen, savedScreenAvailable returns %d.\n",
	    uW.savedScreenAvailable());
  WAIT;
  uW.restoreScreen();
  WAIT;
  uW.addstr("This text printed after the first restore and should stay on\n");
  uW.addstr("the screen even through the next attempted restore.\n");
  WAIT;
  uW.printw("Now savedScreenAvailable returns %d.\n",uW.savedScreenAvailable());
  WAIT;
  uW.restoreScreen();
  WAIT;
  uW.addstr("This should be right below 'next attempted restore'.\n");
  WAIT;
  uW.printw("Now savedScreenAvailable returns %d.\n",uW.savedScreenAvailable());
  WAIT;
}

/* ------------------------------------------------------------------------ */

#ifdef TEST_SCROLLING

#include "savescr.h"

main()
{
  extern void sleep(unsigned int);

  ExtendedCursesWindow uW;
  int i;
  char buf[100];
/*
  int y,x;
  uW.getyx(y,x);
  sprintf(buf, "Home position is (y=%d,x=%d); maxy=%d, maxx=%d.\n",
	  y, x, uW.maxy(), uW.maxx());
  uW.addstr(buf);
*/
  for (i=0; i<=100; ++i)
    {
      if (i>18) sleep(1);

      sprintf(buf,"This is the %d iteration.",i);
      uW.addstr(buf);

      int x, y;
      uW.getyx(y, x);

      if (y>=(uW.maxy()-1))
	{
	  uW.scrollok(1);
	  uW.scroll();
	  uW.scrollok(0);
	}

      uW.addstr("\n");
      if (y>=(uW.maxy()-1)) uW.move(y,0);

      uW.refresh();

      if (i>18) sleep(1);
    }
}

#endif TEST_SCROLLING

/* ------------------------------------------------------------------------ */

#ifdef TESTING_UPDATE_MESSAGE

#include <stream.h>

/* Works!
char* update_message(char* filename, char* string_to_update)
{
  istream in_file(filename, "r");
  char temp_char = NULL;
  char* buffer = NULL;
  int i = 0;
  
  // This routine seems kind of inefficient (loading one character at a time)
  // but since it only gets run at initialization, not worth changing; also
  // istream.h doesn't seem to include a truly non-processed way to get more
  // than one character at a time.

  if (in_file.readable())
    {
      while (!in_file.eof() && in_file.get(temp_char))
	{
	  if (buffer == NULL)
	    buffer = malloc(1);
	  else
	    buffer = realloc(buffer, i+1);
	  buffer[i] = temp_char;
	  i++;
	}
    }
  
  if (buffer != NULL)
    {
      buffer = realloc(buffer, i + 1);
      buffer[i] = 0;
      return buffer;
    }
  else return string_to_update;
};
*/

int update_message(char* filename, char** string_to_update)
{
  istream in_file(filename, "r");
  char temp_char = NULL;
  char* buffer = NULL;
  int i = 0;
  
  // This routine seems kind of inefficient (loading one character at a time)
  // but since it only gets run at initialization, not worth changing; also
  // istream.h doesn't seem to include a truly non-processed way to get more
  // than one character at a time.

  if (in_file.readable())
    {
      while (!in_file.eof() && in_file.get(temp_char))
	{
	  if (buffer == NULL)
	    buffer = malloc(1);
	  else
	    buffer = realloc(buffer, i+1);
	  buffer[i] = temp_char;
	  i++;
	}
    }
  
  if (buffer != NULL)
    {
      buffer = realloc(buffer, i + 1);
      buffer[i] = 0;
      *string_to_update = buffer;
      puts("***\n");
      puts(*string_to_update);
      puts("***\n");
    }
};

main()
{
  char* test_buf = "This is the original text in the buffer.\n";

  puts(test_buf);
  update_message("/mit/mitlibs/info/welcome.barton",&test_buf);
  puts("----------\n");
  printf("%s",test_buf);
}

#endif // TESTING_UPDATE_MESSAGE

/* ------------------------------------------------------------------------ */

#ifdef TESTING_SAVESCR

#include "savescr.h"

void WaitForSpace(CursesWindow& CW)
{
  CW.refresh();
  while (CW.getch()!=' ')
    ;
}

void WaitForSpace(ExtendedCursesWindow& ECW)
{
  ECW.refresh();
  while (ECW.getch()!=' ')
    ;
}

main()
{

  ExtendedCursesWindow ECW;

#define WAIT WaitForSpace(ECW)

  ECW.printw("This is some text which should be saved.\n");
  WAIT;

  ECW.move(15,40);
  ECW.printw("This text is ~middle of screen.\n");
  ECW.saveScreen();
  WAIT;

  ECW.move(3,1);
  ECW.printw("This is some text which should not end up being saved.\n");
  WAIT;

  ECW.restoreScreen();
  WAIT;

  ECW.printw("Here is more text which should appear right below '~middle'.\n");
  ECW.printw("If so the cursor is being saved, too.\n");
  WAIT;

/*
  CursesWindow Parent(stdscr);
  CursesWindow Child(stdscr);

#define WAIT WaitForSpace(Parent)

  Parent.clear();
  Child.clear();

  Parent.printw("This is the parent window.\n");
  Parent.refresh();
  WAIT;

  Parent.overwrite(Child);
  Parent.printw("This is text *AFTER* Parent.overwrite(Child).\n");
  Parent.refresh();
  WAIT;

  Child.overwrite(Parent);
  Parent.touchwin();
  Parent.refresh();
  WAIT;

  Parent.printw("This is text *AFTER* Child.overwrite(Parent).\n");
  Parent.printw("Previous 'This is' line should have disappeared.\n");
  Parent.refresh();
  WAIT;
*/

/*
  CursesWindow uW1(stdscr);
  CursesWindow uW2(stdscr);

  uW1.printw("This is screen uW1:\n");
  uW1.printw("~~~~~~~~~~~~~~~~~~\n");
  uW1.printw("Lines on terminal = %d\n",uW1.lines());
  uW1.printw("Columns on terminal = %d\n",uW1.cols());
  uW1.printw("Height of window = %d, maxy = %d, begy = %d\n",uW1.height(), uW1.maxy(), uW1.begy());
  uW1.printw("Width of window = %d, maxx = %d, begx = %d\n",uW1.width(), uW1.maxx(), uW1.begx());
  uW1.refresh();
  WaitForSpace(uW1);

  uW1.overwrite(uW2);

  uW1.clear();
  uW1.printw("This is fresh text.\n");
  WaitForSpace(uW1);

  uW2.overwrite(uW1);
  uW1.printw("\n\n");
  uW1.printw("This is text after uW2.overwrite(uW1), the previous comment\n");
  uW1.printw("should not be visible.\n");
  uW1.refresh();
  WaitForSpace(uW1);
*/

}

#endif // TESTING_SAVESCR

/* ------------------------------------------------------------------------ */

