/* 
 * © 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.
 */
#include "stdafx.h"
#include "wf_textentry.h"

using namespace MySQL::Forms;

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

bool TextEntryImpl::create(::mforms::TextEntry *self, bool password)
{
  TextEntryImpl ^entry= gcnew TextEntryImpl(self);

  if (entry != nullptr)
  {
    TextBox ^tb= ViewImpl::create<TextBox>(self, entry);
    tb->TextChanged += gcnew EventHandler(change);
    tb->Multiline= false;
    if (password)
      tb->PasswordChar= '*';
    tb->Size= Size(100, tb->PreferredSize.Height); // DefaultSize is not accessible here. 
    return true;
  }
  return false;
}

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

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

  if (cb != nullptr)
  {
    TextBox^ box= cb->get_control<TextBox>();
    box->Text= CppStringToNative(text);
   // textentry shouldn't size to fit box->Size= box->PreferredSize;
  }
}

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

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

  if (cb != nullptr)
  {
    return NativeToCppString(cb->get_control<TextBox>()->Text);
  }
  return "";
}

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

void TextEntryImpl::set_password(::mforms::TextEntry *self, bool flag)
{
  TextEntryImpl^ cb= (TextEntryImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    if (flag)
      cb->get_control<TextBox>()->PasswordChar= '*';
    else
      cb->get_control<TextBox>()->PasswordChar= 0;
  }
}

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

void TextEntryImpl::set_max_length(::mforms::TextEntry *self, int length)
{
  TextEntryImpl^ cb= (TextEntryImpl^)ObjectImpl::FromUnmanaged(self);

  if (cb != nullptr)
  {
    cb->get_control<TextBox>()->MaxLength= length;
  }
}

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

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

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

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

TextEntryImpl::TextEntryImpl(::mforms::TextEntry *text)
  : ViewImpl(text)
{
}

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