/******************************************************************************
Copyright (C) 2005 Matteo Lucarelli

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************/

#include "UIdisplay.h"

UIdisplay::UIdisplay(int w, int h, const char *l):Fl_Double_Window(w,h,l)
{
  fl_register_images(); 
  Fl::visual(FL_RGB);
  m_bMain = new Fl_Box(0,0,w,h);
  m_img=0;
  m_bMain->box(FL_FLAT_BOX);
  show();
}

UIdisplay::~UIdisplay() {
}

int UIdisplay::LoadFile(const char *n) {

  if (m_img) m_img->release();

  m_img = Fl_Shared_Image::get(n);
  
  if (!m_img) {
     // unsupported format
     return -1;
  }
  
  if ((m_img->w()>m_bMain->w())||(m_img->h()>m_bMain->h())){
    Fl_Image *temp;
    if (m_img->w()>m_img->h()) 
       temp=m_img->copy(m_bMain->w(),m_bMain->h()*m_img->h()/m_img->w());
    else 
         temp=m_img->copy(m_bMain->w()*m_img->w()/m_img->h(),m_bMain->h());

    m_img->release();
    m_img = (Fl_Shared_Image *)temp;
  }

  //b->label(name);
  m_bMain->image(m_img);
  m_bMain->redraw();
}