#include <stdio.h> // sprintf()
#include <unistd.h> // getopt()

#include "list/list.h"

#include "vecscrn.h"
#include "figure.h"
#include "line.h"
#include "circle.h"
#include "input.h"
#include "menu.h"
#include "notice.h"

char optBlowup[200];


VecScreen::VecScreen( void )
{
  *FileName = (char) 0;
  *FileNameAni = (char) 0;

  movePoint = 0;
  moveFigure = 0;
  overLines = NULL;
  overPoint = new Point( this, -1, -1 );
  isClipBoardEmpty = 1;
  //clearFlags();
#ifdef __TOOLLIB__
  startx = 200; // Startposition des Grids
  starty = 0;   // Startposition des Grids
  dx = 11;       // x-Abstand der Linien
  dy = 12;       // y-Abstand der Linien
#endif
#ifdef __SVGALIB__
  startx = 100; // Startposition des Grids
  starty = 0;   // Startposition des Grids
  dx = 5;       // x-Abstand der Linien
  dy = 4;       // y-Abstand der Linien
#endif
  size = 40;    // Anzahl der Linien

}


int VecScreen::mouseCallback( void )
{
  static int x1, y1; // Startpunkt, wenn linke Maustaste gedrueckt wurde
  static int x2, y2; // Aktuelle Mausposition

  x2 = mouseX();  // aktuelle Mausposition
  y2 = mouseY();

  drawMousePos();

  if( mouseButton() == MMOUSE_RIGHTBUTTON )
  {

    if( line != NULL  && !movePoint )
    { // line noch in keiner Figur
      line->restoreBackground();
      delete line;
      line = NULL;
    }

    if( moveFigure )
    {
      mouseOn();
      moveFigure = 0;
    }

    myFigure->restoreBackground();
    *myFigure = *backupFigure;
    clearFlags();
    myFigure->setColor( getFgc() );
    myFigure->draw();
  }

  if( moveLine && !oLine )
  {
    Line* oLine;
    oLine  = myFigure->isOverLine( x2, y2 );
    if( oldOLine )
    {
      if( oLine != oldOLine )
      {
        oldOLine->setColor( getFgc() );
        oldOLine->restoreBackground();
        oldOLine->draw();
        oldOLine = NULL;
      }
    }
    if( oLine != oldOLine )
    {
      oLine->highlight();
      oldOLine = oLine;
    }
    else
    {
    }
  }

  if( movePoint && bIdle )
  {
    int x3 = ((x2+dx/2)/dx)*dx+startx%dx;
    int y3 = ((y2+dy/2)/dy)*dy+starty%dy;


    if(( overPoint->getX() != -1 ) && ( overPoint->getY() != -1 ) )
    {
      if(( overPoint->getX() != x3 ) || ( overPoint->getY() != y3 ) )
      {
        overPoint->restoreBackground();
        overPoint->set( -1, -1 );
      }
    }

    if( myFigure->isOverPoint( x3, y3 ) )
    {
      if(( overPoint->getX() != x3 ) || ( overPoint->getY() != y3 ) )
      {
        overPoint->set( x3, y3 );
        overPoint->saveBackground();
        overPoint->highlight( grid->getDy()/2);
      }
    }
  }

  if( movePoint && !bIdle )
  {
    if(( overPoint->getX() != -1 ) && ( overPoint->getY() != -1 ) )
    {
      overPoint->restoreBackground();
      overPoint->set( -1, -1 );
    }
  }


  // Linke Maustaste
  if( mouseButton() == MMOUSE_LEFTBUTTON ) // Button gedrueckt
  {
    if( !grid->isOver( x2, y2 ) )
      return 0;
    *backupFigure = *myFigure;
    if( !mousePressed )  // Button gerade gedrueckt
    {
      if( bIdle ) // mouse pressed and idle
      {
        if( movePoint )
        {
          // Koordinaten an Grid anpassen
          x2 = ((x2+dx/2)/dx)*dx+startx%dx;
          y2 = ((y2+dy/2)/dy)*dy+starty%dy;
          if( myFigure->isOverPoint( x2, y2 ) )
          {
            overLines = myFigure->getLineFromPosition( x2, y2 );
            //if( !overLines->isEmpty() )
            {
              line = overLines->retrieve();

              // Koordinaten an line anpassen
              x2 = line->getX();
              x1 = line->getX1();
              y2 = line->getY();
              y1 = line->getY1();
              line->highlight();
              draw = 1;
            }
          }
          else
          {
            clearFlags();
            movePoint = 1;
            drawMousePos();
            return 0;
          }
        }
        else if( moveFigure )
        {
          // mouse an figure anpassen
          //mouseOff();
          x1 = x2 = myFigure->getX();
          y1 = y2 = myFigure->getY();
          myFigure->highlight();
        }
        else if( moveLine )
        {
          oLine  = myFigure->isOverLine( x2, y2 );
          if( oLine )
          {
            // mouse an oLine anpassen
            x1 = x2 = oLine->getX();
            y1 = y2 = oLine->getY();
          }
          else
          {
            clearFlags();
            moveLine = 1;
            return 0;
          }
        }
        else
        { // einfach anfangen zu zeichnen
          // mouse an grid anpassen
          x1 = x2 = (( mouseX() + dx/2 ) / dx) * dx +startx%dx;
          y1 = y2 = ((mouseY() + dy/2) / dy) * dy + starty%dy;
          line = new MLine( this );
          draw = 1;
        }
        mouseX(x2);
        mouseY(y2);
        bIdle = 0;
      }
      else // mouse pressed an not idle
      {
        // mouse an Grid anpassen
        x2 = ((x2+dx/2)/dx)*dx+startx%dx;
        y2 = ((y2+dy/2)/dy)*dy+starty%dy;
        mouseX(x2);
        mouseY(y2);
        if( moveFigure )
        {
          myFigure->setColor( getFgc() );
          myFigure->moveTo( x2, y2 );
          clearFlags();
          moveFigure = 1;
          //mouseOn();
        }
        else if( moveLine )
        {
          if( oLine )
            oLine->moveTo( x2, y2 );
          myFigure->changedLine( oLine );
          clearFlags();
          moveLine = 1;
        }
        else if( movePoint && !overLines->isEmpty() )
        {

          line->restoreBackground();
          line->setColor( getFgc() );
          line->set( x2, y2, line->getX(), line->getY() );
          line->draw();
          myFigure->changedLine( line );

          int pos = overLines->find( line );
          pos = (pos+1) % overLines->getSize() ;
          overLines->setPos( pos );
          line = overLines->retrieve();
          if( ( line->getX() == x2 ) && ( line->getY() == y2 ) && ( overLines->getSize() > 1 ) )
          {
            // Koordinaten an line anpassen
            x2 = line->getX();
            x1 = line->getX1();
            y2 = line->getY();
            y1 = line->getY1();
            line->highlight();
            draw = 1;
          }
          else
          {
            clearFlags();
            movePoint = 1;
          }
        }
        else if( draw ) // bin am malen
        {
          if( line != NULL )
          { // Linie kuerzen, wenn noetig
            if( (x2-x1)/dx >= 5)
            {
              x2=x1+dx*5;
              mouseX(x2);
            }
            if( (x2-x1)/dx <= -5)
            {
              x2=x1-dx*5;
              mouseX(x2);
            }
            if( (y2-y1)/dy >= 5)
            {
              y2=y1+dy*5;
              mouseY(y2);
            }
            if( (y2-y1)/dy <= -5)
            {
              y2=y1-dy*5;
              mouseY(y2);
            }
            line->restoreBackground();
            line->set(line->getX(), line->getY(), x2, y2 );
            line->setColor( getFgc() );
            line->draw();
            //if( overLines && movePoint )
            //{
              //clearFlags();
              //movePoint = 1;
            //}
            //else
              // es wurde keine vorhanden Linie veraendert
              myFigure->append( line );
          }
          line = new MLine( this );
          x1 = x2;
          y1 = y2;
        }
      } // endif mousepressed and not idle
    }
    else // Button wird immernoch gedrueckt
    {
    }
    mousePressed = 1;
    doSmallFigure();
  }
  else // Button gar nicht gedrueckt
  {
    if( !bIdle )
    {
      if( draw )
      {
        if( grid->checkAndSetBounds( x2, y2 ) )
        {
          mouseX( x2 );
          mouseY( y2 );
        }

        if( (x2-x1)/dx >= 5)
        {
          x2=x1+dx*5;
          mouseX(x2);
        }
        if( (x2-x1)/dx <= -5)
        {
          x2=x1-dx*5;
          mouseX(x2);
        }
        if( (y2-y1)/dy >= 5)
        {
          y2=y1+dy*5;
          mouseY(y2);
        }
        if( (y2-y1)/dy <= -5)
        {
          y2=y1-dy*5;
          mouseY(y2);
        }
        line->restoreBackground();
        line->set( x1, y1, x2, y2 );
        line->draw();
      }
      //else
      if( moveFigure )
      {
        if( grid->checkAndSetBounds( x2, y2, myFigure ) )
        {
          mouseX( x2 );
          mouseY( y2 );
        }
        myFigure->moveTo( x2, y2 );
      }

      if( moveLine )
      {
        if( oLine )
        {
          if( grid->checkAndSetBounds( x2, y2, oLine ) )
          {
            mouseX( x2 );
            mouseY( y2 );
          }
          oLine->moveTo( x2, y2 );
        }
      }
    }
    mousePressed = 0;
  } // endif not idle
  //doSmallFigure();
  return 0;
}

int VecScreen::keyCallback( int key )
{
  switch (key)
  {
    case 'a':
    {
      Menu menu( this, mouseX(), mouseY() );
      menu.addEntry( "~right" );
      menu.addEntry( "~left" );
      menu.addEntry( "~upper" );
      menu.addEntry( "l~ower" );
      switch( menu.start() )
      {
        case 'u':
          myFigure->setStartPos( UPPER );
          break;
        case 'o':
          myFigure->setStartPos( LOWER );
          break;
        case 'r':
          myFigure->setStartPos( RIGHT );
          break;
        case 'l':
          myFigure->setStartPos( LEFT );
          break;
        default:
          myFigure->setStartPos( LEFT );
          break;
      }
      break;
    }
    case 'b':
    {
      if( myFigure->getSize() > 0 )
         myFigure->showStartPoint( grid->getDy()/2 );
      break;
    }
    case 'c':
    {
      myFigure->restoreBackground();
      myFigure->clearLines();
      smallFigure->restoreBackground();
      smallFigure->clearLines();
      clearFlags();
      break;
    }
    case 'C': // Copy to Clipboard :-)
    {
      *copyFigure =  *myFigure;
      isClipBoardEmpty = 0;
      break;
    }
    case 'd':
      if( bIdle )
        break;
      if( movePoint && !overLines->isEmpty() )
      {
        if( overLines->getSize() == 2 )
        {
          Line* l1 = overLines->first();
          Line* l2 = overLines->next();
          l1->restoreBackground();
          l2->restoreBackground();

          l1->set( l2->getX(), l2->getY(), l1->getX1(), l1->getY1() );
          l1->draw();
          line = l2;
        }
        else
          line->restoreBackground();
        myFigure->remove( line );
        overLines->remove( line );
        delete line;
        line = NULL;
        clearFlags();
        movePoint = 1;
      }
      else if( moveLine && oLine )
      {
        oLine->restoreBackground();
        myFigure->remove( oLine );
        delete oLine;
        oLine = NULL;
        oldOLine = NULL;
        bIdle = 1;
        //delete line;
        //line = NULL;
//        clearFlags();
//        moveLine = 1;
      }
      doSmallFigure();
      break;
    case 'f':
      if( bIdle )
      {
        clearFlags();
        moveFigure = 1;
        drawMousePos();
      }
      break;
    case 'h':
    {
      ifstream in( "vec.doc" );
      if( !in ) // Dateifehler
      {
        Notice n( this, "couldn't find vec.doc!" );
        n.start();
        break;
      }
      int cx = 0;
      int cy = 0;
      char s[MAXLINE];
      mouseOff();
      clearScreen();
      while( in.getline( s, MAXLINE ) )
      {
        print( cx, cy, s );
        cx = 0;
        cy += getFontHeight()+1;
      }
      getChar();
      redraw();
      mouseOn();
      break;
    }
    case 'i':
      if( bIdle )
      {
        clearFlags();
        moveLine = 1;
        drawMousePos();
      }
      break;
    case 'l':
    {
      char* name = getFilename( FileName );
      if( name != NULL )
      {
        strcpy( FileName, name );
        ifstream* in;
        in = new ifstream( FileName );
        int ret;
        if( (ret=myFigure->load( *in )) == LOAD_OK )
        {
          Notice n( this, "Loading ok!" );
          n.start();
        }
        else
        {
          Notice n( this, "Loading failed!" );
          switch( ret )
          {
            case DATEIFEHLER:
              n.setText( "Dateifehler!" );
              break;
            case KEINESPRITEKENNUNG:
              n.setText( "Keine Spritekennung!" );
              break;
            case KEINEDBKENNUNG:
              n.setText( "Keine DB-Kennung!" );
              break;
            case KEINELINIENANZAHL:
              n.setText( "Keine Linienanzahl!" );
              break;
            case KEINENSPRITENAMEN:
              n.setText( "Kein Spritename!" );
              break;
            default:
              break;
          }
          n.start();
        }
        delete in;
      }
      else
      {
        Notice n( this, "Kein Dateiname" );
        n.start();
      }
      redraw();
      *backupFigure = *myFigure;
      doSmallFigure();
      break;
    }
    case 'L': // Animation laden
    {
      char* name = getFilename( FileNameAni );
      if( name != NULL )
      {
        strcpy( FileNameAni, name );
        myFigure->restoreBackground();
        figureList.emptyWithElementDelete();
        ifstream* in;
        in = new ifstream( FileName );
        Figure* figure = new Figure(this);
        figure->setScale( dx, dy );
        figure->setBlowup( optBlowup );
        int ret;
        while( (ret = figure->load( *in ) ) == LOAD_OK )
        {
          figureList.append(figure);
          figure = new Figure(this);
          figure->setScale( dx, dy );
          figure->setBlowup( optBlowup );
        }
        if( figureList.isEmpty() )
        {
          Notice n( this, "Loading failed!" );
          switch( ret )
          {
            case DATEIFEHLER:
              n.setText( "Dateifehler!" );
              break;
            case KEINESPRITEKENNUNG:
              n.setText( "Keine Spritekennung!" );
              break;
            case KEINEDBKENNUNG:
              n.setText( "Keine DB-Kennung!" );
              break;
            case KEINELINIENANZAHL:
              n.setText( "Keine Linienanzahl!" );
              break;
            case KEINENSPRITENAMEN:
              n.setText( "Kein Spritename!" );
              break;
            default:
              break;
          }
          n.start();
          figureList.append(figure); // leere Figure anfuegen
        }
        else
        {
          Notice n( this, "Loading ok!" );
          n.start();
        }
        delete in;
        figure = figureList.first();
        myFigure = NULL;
        setFigure( figure );
        *backupFigure = *myFigure;
        doSmallFigure();
      }
      else
      {
        Notice n( this, "Kein Dateiname" );
        n.start();
      }
      break;
    }
    case 'm':
    {
      if( movePoint && (overPoint->getX() != -1) )
      {
        if( myFigure->setStartPos( *overPoint ) != 0 ) // kann nicht zusammenhaengend abgespeichert werden
        {
          Notice n( this, "Startpunkt nicht moeglich!" );
          n.start();
        }
        else
        {
          Notice n( this, "Startpunkt gesetzt!" );
          n.start();
        }
      }
      break;
    }
    case 'n':
    {
      if( bIdle )
      {
        Figure* figure = new Figure( this );
        figure->setScale( dx, dy );
        figure->setBlowup( optBlowup );
        figureList.append( figure );
        setFigure( figure );
        doSmallFigure();
        clearFlags();
        drawMousePos();
      }
      break;
    }
    case 'N':
    {
      int i;
      int len = strlen( myFigure->getName() );
      char* s = new char[ len + 1 ];
      for( i = 0; i < len; i++ )
        s[i] = ' ';
      s[i] = 0;
      print(0, 4*fontHeight, s );
      delete [] s;
      s = getFilename( myFigure->getName() );
      if( s != NULL )
      {
        myFigure->setName( s );
      }
      print(0, 4*fontHeight, myFigure->getName() );
      doSmallFigure();
      break;
    }
    case 'o':
      {
       Notice n(this);
       switch( myFigure->isEuler() )
       {
         case EULERWEG:
           n.setText( "Eulerweg!" );
           n.start();
           break;
         case EULERKREIS:
           n.setText( "Eulerkreis!" );
           n.start();
           break;
         default:
           n.setText( "nichts!" );
           n.start();
           break;
       }
       break;
    }
    case 'p':
      if( bIdle )
      {
        clearFlags();
        movePoint = 1;
        drawMousePos();
      }
      break;
    case 'P':
      if( isClipBoardEmpty )
        break;
      myFigure->restoreBackground();
      *myFigure = *copyFigure;
      myFigure->draw();
      break;
    case 'r':
      clearScreen();
      redraw();
      break;
    case 's':
    {
      if( myFigure->isEuler() == NICHTZUSAMMENHAENGEND )
      {
        Notice n( this, "Figur ist nicht speicherbar" );
        n.start();
        break;        
      }
      char* name = getFilename( myFigure->getName() );
      if( name != NULL )
      {
        strcpy( FileName, name );
        ofstream* out;
        out = new ofstream( name );
        *out << ';' << name << "\n";
        if( myFigure->getSize() > 0 )
          myFigure->save( *out );
        delete out;
      }
      else
      {
        Notice n( this, "Kein Dateiname" );
        n.start();
      }
      break;
    }
    case 'S':
    {
      //for( figureList.first(); !figureList.isLast(); figureList.next() )
      //{
        
      //}
      char* name = getFilename( FileNameAni );
      if( name != NULL )
      {
        strcpy( FileNameAni, name );
        ofstream* out;
        out = new ofstream( FileName );
        *out << ';' << FileName << "\n";
        for( figureList.first(); !figureList.isLast(); figureList.next() )
        {
          Figure* figure = figureList.retrieve();
          if( figure->getSize() > 0 )
          {
            figure->save( *out );
          }
        }
        delete out;
      }
      else
      {
        Notice n( this, "Kein Dateiname" );
        n.start();
      }
      break;
    }
    case 'x':
      myFigure->mirror_x();
      doSmallFigure();
      break;
    case 'X':  // myFigure loeschen
    {
      if( figureList.getSize() > 1 )
      {
        Figure* figure;
        myFigure->restoreBackground();
        figureList.remove( myFigure );
        if( figureList.isFirst() )
          figure = figureList.retrieve();
        else
          figure = figureList.previous();
        setFigure( figure ) ;
      }
      doSmallFigure();
      break;
    }
    case 'y':
      myFigure->mirror_y();
      doSmallFigure();
      break;
    case 'z':
      clearFlags();
      drawMousePos();
      break;
    /*case 'z':
      if( oLine && moveLine )
      {
        ZeigerListe<Line>* list = oLine->split();
        Line* line = list->first();
        myFigure->restoreBackground();
        myFigure->remove( line );
        myFigure->draw();
        while( (line=list->next()) != NULL )
        {
          myFigure->append( line );
        }
        myFigure->draw();
        myFigure->sort();
        clearFlags();
      }
      break;*/
    case '+':
    {
      if( figureList.getSize() > 1 )
      {
        myFigure->restoreBackground();
        int p = figureList.getPos();
        p=(p+1)%figureList.getSize();
        figureList.setPos( p );
        setFigure( figureList.retrieve() );
        doSmallFigure();
      }
      break;
    }
    case '-':
    {
      if( figureList.getSize() > 1 )
      {
        myFigure->restoreBackground();
        int p = figureList.getPos();
        p=(p-1+figureList.getSize())%figureList.getSize();
        figureList.setPos( p );
        setFigure( figureList.retrieve() );
        doSmallFigure();
      }
      break;
    }
  }
  return 0;
}


int VecScreen::main( int argc, char* argv[] )
{
#ifdef __SVGALIB__
  dx=5;dy=4;
#endif
#ifdef __TOOLLIB__
  dx=11;dy=12;
#endif

 optBlowup[0] = '\0';
 if (argc>1)
 {
  int arg_counter = 1;
  int i;
  while (argv[arg_counter])
  {
   char *pointer = argv[arg_counter];
   if ((pointer[0] == '-') || (pointer[0] == '/') || (pointer[0] == ':') || ( pointer[0] == '\\'))
   {
    if ((pointer[1] == 'h') || (pointer[1] == 'H') || (pointer[1] == '?'))
    {
     cout << "VECCY Vector line editor...\n";
     cout << "-bBLOWUP_FACTOR (blowup string added to saved coordinates)\n";
     cout << "-sSIZE (size of grid)\n";
     cout << "-xSTART_X (start coordinate of grid)\n";
     cout << "-ySTART_Y (start coordinate of grid)\n";
     cout << "-XWIDTH_X (width of grid)\n";
     cout << "-YHIGHT_Y (hight of grid)\n";
     return 0;
    }
    if (pointer[1] == 'b')
    {
     strcpy(optBlowup, pointer + 2);
    }
    if (pointer[1] == 's')
    {
     sscanf( pointer + 2, "%i", &i );
     size = i;
    }
    if (pointer[1] == 'x')
    {
     sscanf( pointer + 2, "%i", &i );
     startx = i;
     cout << i << " " << pointer + 2 << "\n";
    }
    if (pointer[1] == 'X')
    {
     sscanf( pointer + 2, "%i", &i );
     dx = i;
    }
    if (pointer[1] == 'y')
    {
     sscanf( pointer + 2, "%i", &i );
     starty = i;
    }
    if (pointer[1] == 'Y')
    {
     sscanf( pointer + 2, "%i", &i );
     dy = i;
    }
   }
   arg_counter++;
  }
 }

  if( open() != 0 )
  {
    cerr << "Couldn't open screen\n";
  }
  else
  {
    clearFlags();
    figureList.append( new Figure( this ) );
    copyFigure = new Figure( this );
    backupFigure = new Figure( this );
    smallFigure = new Figure( this );
    myFigure = figureList.first();
    for( figureList.first(); !figureList.isLast(); figureList.next() )
    {
       Figure* figure = figureList.retrieve();
       figure->setScale( dx, dy );
       figure->setBlowup( optBlowup );
    }
    strcpy( FileName, myFigure->getName());

    grid = new Grid(this, startx, starty);
    grid->setColor( Grey() );
    grid->setScale( dx, dy, size );
    grid->set( startx, starty );

    redraw();
    start();
  }
  return 0;
}

void VecScreen::redraw()
{
  clearScreen();
  grid->draw();
  drawMousePos();
  print(0, 4*fontHeight, myFigure->getName() );
  doSmallFigure();
  myFigure->draw();
}

char* VecScreen::getFilename( char* defaultName )
{
  char* newName;
  Input input( this, 0, 0, 100, getFontHeight()+4 );
  input.setText( defaultName );
  newName = input.start();
  input.restoreBackground();
  if( *newName == (char) 0 )
    newName = NULL; 
  return newName;
}

void VecScreen::setFigure( Figure* figure )
{
  if( myFigure != figure )
  {
    if( myFigure != NULL )
    {
      int i;
      int len = strlen( myFigure->getName() );
      char* s = new char[ len + 1 ];
      for( i = 0; i < len; i++ )
        s[i] = ' ';
      s[i] = 0;
      print(0, 4*fontHeight, s );
      delete [] s;
      myFigure->restoreBackground();
    }
    myFigure = figure;
    myFigure->setScale( dx, dy );
    print(0, 4*fontHeight, myFigure->getName() );
  }
  drawMousePos();
  myFigure->draw();
}

void VecScreen::drawMousePos()
{
  char string[200];
  int x = (( mouseX()-startx+dx/2) / dx) ;
  int y = (( mouseY()-starty+dy/2) / dy) ;

  if( x < 0 )
    x = 0;
  if( x >= size )
    x = size;
  if( y < 0 )
    y = 0;
  if( y >= size )
    y = size;
  sprintf( string, "%c%c%c%c%c", bIdle ? 'I' : ' ', draw ? 'D' : ' ', movePoint ? 'P' : ' ', moveLine ? 'L' : ' ', moveFigure ? 'F' : ' ' );
  print(0, 0*fontHeight, string );
  sprintf( string, "%3i %3i", x, y );
  print(0, 1*fontHeight, string );
  sprintf( string, "lines %3i", myFigure->getSize() );
  print(0, 2*fontHeight, string );
  figureList.find( myFigure );
  sprintf( string, "figure %i/%i", figureList.getPos()+1, figureList.getSize() );
  print(0, 3*fontHeight, string );
}

inline void VecScreen::clearFlags( void )
{
  if( movePoint && overPoint )
  {
    overPoint->restoreBackground();
    overPoint->set( -1, -1 );
  }

  if( moveFigure && myFigure )
  {
    myFigure->restoreBackground();
    myFigure->setColor( getFgc() );
    myFigure->draw();
  }

  if( overLines )
  {
    delete overLines;
    overLines = NULL;
  }

  bIdle = 1;        // System hat nichts zu tun
  mousePressed = 0; // linke Maustaste nicht gedrueckt
  draw = 0;         // es wird momentan nicht gezeichnet
  movePoint= 0;     // es wird auch kein Punkt verschoben
  moveLine = 0;     // es wird auch keine Zeile verschoben
  moveFigure=0;     // beim verschieben nicht die ganze Figur verschieben
  line=NULL;
  oLine=NULL;
  oldOLine = NULL;
}


void VecScreen::doSmallFigure( void )
{
  int sdx = 2;
  int sdy = 2;
  smallFigure->restoreBackground();
  *smallFigure  = *myFigure;
  smallFigure->scale( sdx, sdy );
  int nx = ((myFigure->West()-grid->getX()) / grid->getDx()*sdx)-smallFigure->West()+smallFigure->getX();
  int ny = 100 + (myFigure->North()-grid->getY())/grid->getDy()*sdy-smallFigure->North()+smallFigure->getY();
  smallFigure->set( nx , ny );
  smallFigure->draw1();
}
