/*
  Copyright (C) 2012 Stuffomatic Ltd. <contact@stuff-o-matic.com>

  All rights reserved.

  See the accompanying license file for details about usage, modification and
  distribution of this file.
*/
/**
 * \file
 * \brief Implementation of the accordion class.
 * \author Julien Jorge
 */
#include "bf/accordion.hpp"

#include <wx/sizer.h>
#include <claw/assert.hpp>

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param parent The parent control of the accordion.
 * \param orient The orientation of the accordion: wxVERTICAL or wxHORIZONTAL.
 */
bf::accordion::accordion( wxWindow* parent, int orient )
  : wxPanel(parent), m_current_button(NULL)
{
  m_content = new wxBoxSizer(orient);
  SetSizer(m_content);
} // accordion::accordion()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add something in the accordion.
 * \param c The control to add.
 * \param label The label associated to this control.
 */
void bf::accordion::add( wxWindow* c, const wxString& label )
{
  wxSizer* s = new wxBoxSizer( wxHORIZONTAL );
  s->Add( c, 0, wxEXPAND );
  add( s, label );
} // accordion::add()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add something in the accordion.
 * \param s The sizer to add.
 * \param label The label associated to this control.
 */
void bf::accordion::add( wxSizer* s, const wxString& label )
{
  wxToggleButton* btn = new wxToggleButton( this, wxID_ANY, label );
  Connect( btn->GetId(), wxEVT_COMMAND_TOGGLEBUTTON_CLICKED,
           wxCommandEventHandler(accordion::on_button) );
  m_sizers[btn] = s;

  m_content->Add( btn, 0, wxEXPAND );
  m_content->Add( s, 0, wxEXPAND );
  m_content->Show(s, false, true);
  m_content->Layout();
} // accordion::add()

/*----------------------------------------------------------------------------*/
/**
 * \brief The user clicked on a button of the accordion.
 * \param event The event generated by the click.
 */
void bf::accordion::on_button( wxCommandEvent& event )
{
  wxToggleButton* btn = dynamic_cast<wxToggleButton*>(event.GetEventObject());
  CLAW_PRECOND( btn != NULL );

  if ( btn == m_current_button )
    {
      m_content->Show(m_sizers[m_current_button], false, true);
      m_current_button = NULL;
    }
  else
    {
      if ( m_current_button != NULL )
        {
          m_content->Show(m_sizers[m_current_button], false, true);
          m_current_button->SetValue(false);
        }

      m_content->Show(m_sizers[btn], true, true);
      m_current_button = btn;
    }

  m_content->Layout();
} // accordion::on_button()
