/* 
 * © 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 label wrapper for the mforms library.
 */

#include "stdafx.h"
#include "wf_label.h"

using namespace System;
using namespace Drawing;
using namespace Windows::Forms;

using namespace MySQL::Forms;

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

/**
 * Converts an mform alignment value into a Windows::Forms alignment value.
 */
ContentAlignment get_alignment(::mforms::Alignment align)
{
  switch (align)
  {
  case ::mforms::BottomLeft: 
    return ContentAlignment::BottomLeft;
  case ::mforms::BottomCenter: 
    return ContentAlignment::BottomCenter;
  case ::mforms::BottomRight: 
    return ContentAlignment::BottomRight;
  case ::mforms::MiddleLeft: 
    return ContentAlignment::MiddleLeft;
  case ::mforms::MiddleCenter: 
    return ContentAlignment::MiddleCenter;
  case ::mforms::MiddleRight: 
    return ContentAlignment::MiddleRight;
  case ::mforms::TopLeft: 
    return ContentAlignment::TopLeft;
  case ::mforms::TopCenter: 
    return ContentAlignment::TopCenter;
  case ::mforms::TopRight: 
    return ContentAlignment::TopRight;
  }
  return ::System::Drawing::ContentAlignment::TopLeft;
}

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

WrapControlLabel::WrapControlLabel()
{
  _auto_wrapping= false;
}

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

/**
 * Returns the preferred size of the label depending on its auto-wrapping mode.
 */
Drawing::Size WrapControlLabel::GetPreferredSize(Drawing::Size proposedSize)
{
  Drawing::Size result;
  if (!_auto_wrapping)
  {
    // Default behavior, just let the control itself determine what it needs.
    result= Label::GetPreferredSize(Drawing::Size::Empty);
  }
  else
  {
    // If the horizontal proposed size is 0 then we are in a dilemma, since we need a
    // preset width to compute the proper height. In this case we use the current width of
    // the control. If this width is 0 then the computed size is the same as if wrapping
    // would not be enabled.
    if (proposedSize.Width == 0)
      proposedSize.Width= Width;
    result= Label::GetPreferredSize(proposedSize);
  };

  return result;
}

//----------------- Static functions ---------------------------------------------------------------

bool LabelImpl::create(::mforms::Label *self)
{
  LabelImpl ^label= gcnew LabelImpl(self);

  if (label != nullptr)
  {
    Label ^l= ViewImpl::create<WrapControlLabel>(self, label);
    l->TextAlign= ContentAlignment::MiddleLeft;
    l->BorderStyle= BorderStyle::None;
    return true;
  }
  return false;
}

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

void LabelImpl::set_style(::mforms::Label *self, ::mforms::LabelStyle style)
{
  LabelImpl^ label= (LabelImpl^)ObjectImpl::FromUnmanaged(self);

  if (label != nullptr)
    label->set_style(style);
}

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

void LabelImpl::set_text(::mforms::Label *self, const std::string &text)
{
  LabelImpl^ label= (LabelImpl^)ObjectImpl::FromUnmanaged(self);

  if (label != nullptr)
    label->set_text(text);
}

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

void LabelImpl::set_text_align(::mforms::Label *self, ::mforms::Alignment align)
{
  LabelImpl^ label= (LabelImpl^)ObjectImpl::FromUnmanaged(self);

  if (label != nullptr)
    label->set_text_align(align);
}

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

void LabelImpl::set_color(::mforms::Label *self, const std::string &color)
{
  LabelImpl^ label= (LabelImpl^)ObjectImpl::FromUnmanaged(self);

  if (label != nullptr)
    label->set_color(color);
}

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

void LabelImpl::set_wrap_text(::mforms::Label *self, bool flag)
{
  LabelImpl^ label= (LabelImpl^)ObjectImpl::FromUnmanaged(self);

  if (label != nullptr)
    label->set_wrap_text(flag);
}

//----------------- Actual implementation ----------------------------------------------------------

LabelImpl::LabelImpl(::mforms::Label *self)
  : ViewImpl(self)
{
}

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

void LabelImpl::set_style(::mforms::LabelStyle style)
{
  LayoutDirty= true;

  Label ^label= get_control<Label>();

  switch (style)
  {
  case ::mforms::NormalStyle:
    label->Font= gcnew Font(label->Font, FontStyle::Regular);
    break;
  case ::mforms::BoldStyle: 
    label->Font= gcnew Font(label->Font, FontStyle::Bold);
    break;

  case ::mforms::BigStyle:
    label->Font= gcnew Font(label->Font->Name, 11.0, FontStyle::Regular, GraphicsUnit::Pixel);
    break;
  case ::mforms::BigBoldStyle:
    label->Font= gcnew Font(label->Font->Name, 11.0, FontStyle::Bold, GraphicsUnit::Pixel);
    break;
  case ::mforms::SmallStyle:
    label->Font= gcnew Font(label->Font->Name, 9.0, FontStyle::Regular, GraphicsUnit::Pixel);
    break;
  case ::mforms::InfoCaptionStyle:
    label->Font= gcnew Font(label->Font->Name, label->Font->Size, FontStyle::Regular, GraphicsUnit::Pixel);
    break;
  case ::mforms::BoldInfoCaptionStyle:
    label->Font= gcnew Font(label->Font->Name, label->Font->Size, FontStyle::Bold, GraphicsUnit::Pixel);
    break;
  case ::mforms::WizardHeadingStyle:
    label->Font= gcnew Font(label->Font->Name, 12.0, FontStyle::Bold, GraphicsUnit::Pixel);
    label->ForeColor= ColorTranslator::FromHtml("#003392");
    break;
  case ::mforms::SmallHelpTextStyle:
    label->Font= gcnew Font(label->Font->Name, 9.0, FontStyle::Regular, GraphicsUnit::Pixel);
    break;
  }
}

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

void LabelImpl::set_text(const std::string &text)
{
  String^ new_text= CppStringToNative(text);
  WrapControlLabel^ label= get_control<WrapControlLabel>();
  if (label->Text != new_text)
  {
    LayoutDirty= true;
    label->Text= new_text;

    Size newSize= label->PreferredSize;
    if (label->AutoWrapping)
      newSize.Width= label->Size.Width;
    label->Size= newSize;
  };
}

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

void LabelImpl::set_text_align(::mforms::Alignment align)
{
  LayoutDirty= true;
  get_control<Label>()->TextAlign= get_alignment(align);
}

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

void LabelImpl::set_color(const std::string &color)
{
  get_control<Label>()->ForeColor= ColorTranslator::FromHtml(CppStringToNative(color));
}

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

void LabelImpl::set_wrap_text(bool flag)
{
  get_control<WrapControlLabel>()->AutoWrapping= flag;
}

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

