/*
  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 bf::base_file_edit class.
 * \author Julien Jorge
 */

#include "bf/path_configuration.hpp"
#include "bf/workspace_environment.hpp"

#include <wx/filedlg.h>
#include <wx/sizer.h>
#include <wx/filename.h>

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param parent The window owning this one.
 * \param env The workspace environment to use.
 * \param v The initial value.
 */
template<typename T>
bf::base_file_edit<T>::base_file_edit
( wxWindow& parent, workspace_environment& env, const file_type& v )
  : super(v), wxPanel(&parent), m_filter(_("All files|*")), m_workspace(env)
{
  create_controls();
  Fit();
  value_updated();
} // base_file_edit::base_file_edit()

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param parent The window owning this one.
 * \param env The workspace environment to use.
 * \param filter The filter of the file selection dialog.
 * \param v The initial value.
 */
template<typename T>
bf::base_file_edit<T>::base_file_edit
( wxWindow& parent, workspace_environment& env, const wxString& filter, 
  const file_type& v )
  : super(v), wxPanel(&parent), m_filter(filter), m_workspace(env)
{
  create_controls();
  Fit();
  value_updated();
} // base_file_edit::base_file_edit()

/*----------------------------------------------------------------------------*/
/**
 * \brief Check if the displayed value is correct and, if it is, set the
 *        value according to the display.
 */
template<typename T>
bool bf::base_file_edit<T>::validate()
{
  this->value_from_string( m_path->GetValue() );
  return true;
} // base_file_edit::validate()

/*----------------------------------------------------------------------------*/
/**
 * \brief Changes the filter of the files displayed when browsing.
 * \param filter The filter of the file selection dialog.
 */
template<typename T>
void bf::base_file_edit<T>::set_filter( wxString filter )
{
  m_filter = filter;
} // base_file_edit::validate()

/*----------------------------------------------------------------------------*/
/**
 * \brief Update the controls with the new value.
 */
template<typename T>
void bf::base_file_edit<T>::value_updated()
{
  m_path->SetValue( std_to_wx_string(this->get_value().get_value()) );
} // base_file_edit::value_updated()

/*----------------------------------------------------------------------------*/
/**
 * \brief Create the controls of the edit.
 */
template<typename T>
void bf::base_file_edit<T>::create_controls()
{
  m_path = new wxTextCtrl(this, wxID_ANY);
  m_browse = new wxButton
    ( this, IDC_BROWSE, wxT("..."), wxDefaultPosition, wxSize(30, -1) );

  this->Connect( m_browse->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
                 wxCommandEventHandler(self_type::on_browse) );

  wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
  sizer->Add( m_path, 1, wxEXPAND );
  sizer->Add( m_browse, 0, wxEXPAND );

  this->SetSizer( sizer );
} // base_file_edit::create_controls()

/*----------------------------------------------------------------------------*/
/**
 * \brief The user clicked on the "browse" button.
 * \param event The event generated by the user's action.
 */
template<typename T>
void bf::base_file_edit<T>::on_browse( wxCommandEvent& WXUNUSED(event) )
{
  std::string p = wx_to_std_string(m_path->GetValue());
  path_configuration::get_instance().get_full_path
    ( p, m_workspace.get_name() );

  wxFileDialog dlg
    ( this, _("Choose a file"), wxEmptyString, std_to_wx_string(p), m_filter,
      wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );

  if (dlg.ShowModal() == wxID_OK)
    {
      std::string new_p = wx_to_std_string( dlg.GetPath() );
      path_configuration::get_instance().get_relative_path
        (new_p, m_workspace.get_name() );

      m_path->SetValue( std_to_wx_string(new_p) );
    }
} // base_file_edit::on_browse()
