/* 
 * © 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_scrollpanel.h"

/**
 * Implementation of a specialized mforms panel, which has scrollbars.
 * On Windows every container has scrollbars, so we don't need a separate implementation here.
 * Instead we reuse PanelImpl.
 */
using namespace MySQL;
using namespace MySQL::Forms;

bool ScrollFillLayout::Layout(Object^ container, LayoutEventArgs^ arguments)
{
  // This layout is actually very simple. Simply resize the first (and only) child control so that
  // it fills the entire client area of the container. If enabled resize the container to fit the
  // (preferred) size of the content.

  Control^ parent= (Control^) container;
  if (parent->Controls->Count > 0)
  {
    Size boxSize= parent->Size;
    System::Drawing::Rectangle displayArea= parent->ClientRectangle;

    Control^ content= parent->Controls[0];
    Size childSize;

    if (ViewImpl::use_min_size_for_layout(content))
      childSize= content->MinimumSize;
    else
      childSize= content->GetPreferredSize(displayArea.Size);

    // Adjust child size only if it is smaller than the container size.
    // Remove a few pixel, otherwise the scrollable control will display its scrollbars
    // permanently or temporarily.
    // TODO: investigate if the exact value is that of the scrollbars.
    if (displayArea.Width - 20 > childSize.Width)
      childSize.Width= displayArea.Width - 20;
    if (displayArea.Height - 20 > childSize.Height)
      childSize.Height= displayArea.Height - 20;

    // Now stretch the client control to fill the entire display area.
    ViewImpl::remove_auto_resize(content, mforms::ResizeBoth);
    content->Size= childSize;

    return false;
  }
  return false;
}

//----------------- ScrollFillPanel ----------------------------------------------------------------

Drawing::Size ScrollFillPanel::GetPreferredSize(Drawing::Size proposedSize)
{
  // A scrollbox can be as small as possible. It scrolls if necessary.
  return MinimumSize;
}

//----------------- ScrollPanelImpl ----------------------------------------------------------------

bool ScrollPanelImpl::create(::mforms::ScrollPanel* self, bool bordered)
{
  ScrollPanelImpl^ panel= gcnew ScrollPanelImpl(self);
  if (panel != nullptr)
  {
    if (bordered)
    {
      // Have to fake a bordered scrollbox by embedding a panel in a groupbox.
      Control^ control= ViewImpl::create<GroupBox>(self, panel);
      control->AutoSize= false;
      control->Padding= Padding(5);
      panel->_container= gcnew ScrollFillPanel();
      control->Controls->Add(panel->_container);
      panel->_container->Dock= DockStyle::Fill;
    }
    else
      panel->_container= ViewImpl::create<ScrollFillPanel>(self, panel);
    panel->_container->AutoSize= false;
    panel->_container->AutoScroll= true;
    return true;
  }
  return false;
}

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

void ScrollPanelImpl::add(::mforms::ScrollPanel *self, ::mforms::View *view)
{
  ScrollPanelImpl^ panel= (ScrollPanelImpl^)ObjectImpl::FromUnmanaged(self);
  if (panel != nullptr)
    panel->add(view);
}

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

void ScrollPanelImpl::remove(::mforms::ScrollPanel *self)
{
  ScrollPanelImpl^ panel= (ScrollPanelImpl^)ObjectImpl::FromUnmanaged(self);
  if (panel != nullptr)
    panel->remove();
}

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

ScrollPanelImpl::ScrollPanelImpl(::mforms::ScrollPanel *self)
  : ViewImpl(self)
{
}

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

void ScrollPanelImpl::add(::mforms::View *view)
{
  LayoutDirty= true;
  ViewImpl^ child= (ViewImpl^)ObjectImpl::FromUnmanaged(view);
  Control^ ctl= child->get_control<Control>();

  _container->Controls->Add(ctl);
  ctl->Location= Point(0, 0);
}

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

void ScrollPanelImpl::remove()
{
  LayoutDirty= true;
  _container->Controls->Clear();
}

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

