/* 
 * © 2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */
/**
 * Implementation of the textbox (multi line edit) wrapper for the mforms library.
 */

#include "stdafx.h"
#include "wf_textbox.h"

using namespace MySQL::Forms;

using namespace System::Windows::Forms;
using namespace System::Text;

//--------------------------------------------------------------------------------------------------

bool TextBoxImpl::create(::mforms::TextBox *self, ::mforms::ScrollBars scroll_bars)
{
  const ScrollBars mapper[]= 
    {ScrollBars::None, ScrollBars::Horizontal, ScrollBars::Vertical, ScrollBars::Both};

  TextBoxImpl ^cb= gcnew TextBoxImpl(self);

  if (cb != nullptr)
  {
    TextBox ^tb= ViewImpl::create<TextBox>(self, cb);
    tb->Multiline= true;
    tb->AcceptsReturn= true;
    tb->ScrollBars= mapper[scroll_bars];
    tb->TextChanged += gcnew EventHandler(change);
    tb->Size= Size(100, tb->PreferredSize.Height); // DefaultSize is not accessible here. 
    return true;
  }
  return false;
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::change(Object^ sender, EventArgs^ args)
{
  TextBox^ tb= (TextBox^) sender;

  if (tb->Tag != nullptr)
  {
    ::mforms::TextBox* box= ViewImpl::get_backend_control<::mforms::TextBox>(tb);
    if (box != NULL)
      box->callback();
  }
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::set_bordered(::mforms::TextBox *self, bool bordered)
{
  TextBoxImpl^ cb= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    //XXX
  }
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::set_text(::mforms::TextBox *self, const std::string &text)
{
  TextBoxImpl^ cb= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    // Convert LF only line breaks into Windows line breaks.
    StringBuilder^ builder= gcnew StringBuilder(CppStringToNative(text));
    builder->Replace("\n", Environment::NewLine);

    cb->get_control<Control>()->Text= builder->ToString();
  }
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::append_text(::mforms::TextBox *self, const std::string &text)
{
  TextBoxImpl^ cb= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    // Convert LF only line breaks into Windows line breaks.
    StringBuilder^ builder= gcnew StringBuilder(CppStringToNative(text));
    builder->Replace("\n", Environment::NewLine);

    cb->get_control<Control>()->Text+= builder->ToString();
  }
}

//--------------------------------------------------------------------------------------------------

std::string TextBoxImpl::get_text(::mforms::TextBox *self)
{
  TextBoxImpl^ cb= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    // Convert Windows line breaks to LF.
    StringBuilder^ builder= gcnew StringBuilder(cb->get_control<Control>()->Text);
    builder->Replace(Environment::NewLine, "\n");
    return NativeToCppString(builder->ToString());
  }
  return "";
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::set_read_only(::mforms::TextBox *self, bool flag)
{
   TextBoxImpl^ cb= (TextBoxImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    cb->get_control<TextBox>()->ReadOnly= flag;
  }
}

//--------------------------------------------------------------------------------------------------

void TextBoxImpl::set_padding(::mforms::TextBox *self, int pad)
{
}

//--------------------------------------------------------------------------------------------------

TextBoxImpl::TextBoxImpl(::mforms::TextBox *text)
  : ViewImpl(text)
{
}

//--------------------------------------------------------------------------------------------------

