#include <string.h> // strlen(); 
#include "line.h"
#include "notice.h"


Notice::Notice( Screen* newScreen, char* text )
  : Bitmap( newScreen, 0, 0, 0, 0 )
{
  set( 0, 100 ); 
  if( text != NULL )
  {
    if( strlen(text) < 7 )
      setWidth( 7*screen->getFontWidth() + 4 ); // minimal wegen <Taste>
    else
      setWidth( strlen(text) * screen->getFontWidth() + 4 );
  }
  else
    setWidth( 7*screen->getFontWidth() ); // minimal wegen <Taste>
  setHeight( 2 * screen->getFontHeight() + 4 + 1 );
  upper = new Line( newScreen, getX(), getY(), getX()+getWidth()-1, getY() );
  left  = new Line( newScreen, getX(), getY(), getX(), getY()+getHeight()-1 );
  right = new Line( newScreen, getX()+getWidth()-1, getY(), getX()+getWidth()-1, getY()+getHeight()-1 );
  lower = new Line( newScreen, getX(), getY()+getHeight()-1, getX()+getWidth()-1, getY()+getHeight()-1 );
  sBuffer = text;
}

Notice::~Notice()
{
  //restoreBackground();
  if( upper )
    delete upper;
  if( lower )
    delete lower;
  if( left )
    delete left;
  if( right )
    delete right;
}
void Notice::start()
{
  saveBackground();
  draw();
  screen->getChar();
  restoreBackground();
}

void Notice::setText( char* text )
{
  if( text == NULL )
    return;
  sBuffer = text;
  if( strlen( text ) < 7 )
    setWidth( 7*screen->getFontWidth() + 4 );
  else
    setWidth( strlen(text) * screen->getFontWidth() + 4 );
  setHeight( 2 * screen->getFontHeight() + 4 + 1 );
  upper->set( getX(), getY(), getX()+getWidth()-1, getY() );
  left->set( getX(), getY(), getX(), getY()+getHeight()-1 );
  right->set( getX()+getWidth()-1, getY(), getX()+getWidth()-1, getY()+getHeight()-1 );
  lower->set( getX(), getY()+getHeight()-1, getX()+getWidth()-1, getY()+getHeight()-1 );
}

void Notice::draw()
{
  screen->mouseOff();
  saveBackground();
  screen->setArea( getX(), getY(), getWidth(), getHeight(), screen->getBgc() );
  upper->draw();
  right->draw();
  left->draw();
  lower->draw();
  screen->print( getX()+2, getY()+2, sBuffer );
  int w = (strlen( "<Taste>" )*screen->getFontWidth()) / 2;
  screen->print( getX()+getWidth()/2-w, getY()+2+screen->getFontHeight()+1, "<Taste>" );
  screen->mouseOn();
}

