/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CMessageDialog.cpp  -  description
                             -------------------
    begin                : Thu Oct 28 2004
    copyright            : (C) 2000-2005 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

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

   This library 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 Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <FL/Fl.h>
#include <FL/Fl_Pixmap.h>
#include <sptk3/CMessageDialog.h>
#include <sptk3/string_ext.h>

#include <pixmaps/dialog_error.xpm>
#include <pixmaps/dialog_info.xpm>
#include <pixmaps/dialog_question.xpm>
#include <pixmaps/dialog_warning.xpm>

using namespace std;

static Fl_Pixmap dialog_error(error_xpm);
static Fl_Pixmap dialog_info(info_xpm);
static Fl_Pixmap dialog_question(question_xpm);
static Fl_Pixmap dialog_warning(warning_xpm);

CAskDialog::CAskDialog(const char *label,int w)
: CDialog(w,100,label) {

   int textSize = int(Fl::h() / 64 + 0.5);
   if (textSize < 14) textSize = 14;

   CGroup *grp = new CGroup("",65,SP_ALIGN_LEFT);
   m_imageBox = new Fl_Box(8, 8, 50, 50);
   m_imageBox->box(FL_NO_BOX);
   grp->end();

   m_textBox = new CHtmlBox("",30,SP_ALIGN_CLIENT);
   m_textBox->labelWidth(0);
   m_textBox->textSize(textSize);
   m_textBox->color(FL_GRAY);

   m_inputBox = new CInput("",10,SP_ALIGN_BOTTOM);
   m_inputBox->labelWidth(0);
   m_inputBox->textSize(textSize);
   m_inputBox->hide();

   end();
}

bool CAskDialog::execute(string msg) {
   string htmlMessage(msg);
   if (upperCase(htmlMessage).find("<HTML>") == string::npos) {
      htmlMessage = "<HTML><BODY>" + replaceAll(htmlMessage,"\n","<BR>") + "</BODY></HTML>";
   }
   m_textBox->data(htmlMessage);
   relayout();
   int ww = m_textBox->parent()->w();
   int hh = 0;
   int dh = h() - m_textBox->parent()->h(); 
   m_textBox->preferredSize(ww,hh);

   if (m_inputBox->visible()) hh += 20;

   if (hh < 60) hh = 60;

   int windowHeight = hh + dh + 20;
   if (windowHeight > 300) windowHeight = 300;

   resize(x(),y(),w(),windowHeight);

   return showModal();
}	

bool CInputDialog::execute(string msg,string& inputText) {
   m_inputBox->data(inputText.c_str());
   if (CAskDialog::execute(msg)) {
      inputText = m_inputBox->data().asString().c_str();
      return true;
   }
   return false;
}

int spAsk(string message) {
   CAskDialog ask("Please, confirm");
   ask.icon(&dialog_question);
   return ask.execute(message);
}

int spInformation(string message) {
   CMessageDialog ask("Information");
   ask.icon(&dialog_info);
   return ask.execute(message);
}

int spWarning(string message) {
   CMessageDialog ask("Warning");
   ask.icon(&dialog_warning);
   return ask.execute(message);
}

int spError(string message) {
   CMessageDialog ask("Error");
   ask.icon(&dialog_error);
   return ask.execute(message);
}

int spInput(string message,string& inputText) {
   CInputDialog ask("Please, input");
   ask.icon(&dialog_question);
   return ask.execute(message,inputText);
}
