
///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                    User Interface                     //
//                                                       //
//                    Program: SAGA                      //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                 ACTIVE_Description.cpp                //
//                                                       //
//          Copyright (C) 2005 by Olaf Conrad            //
//                                                       //
//-------------------------------------------------------//
//                                                       //
// This file is part of 'SAGA - System for Automated     //
// Geoscientific Analyses'. SAGA is free software; you   //
// can redistribute it and/or modify it under the terms  //
// of the GNU General Public License as published by the //
// Free Software Foundation, either version 2 of the     //
// License, or (at your option) any later version.       //
//                                                       //
// SAGA 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 General Public        //
// License for more details.                             //
//                                                       //
// You should have received a copy of the GNU General    //
// Public License along with this program; if not, see   //
// <http://www.gnu.org/licenses/>.                       //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//    contact:    Olaf Conrad                            //
//                Institute of Geography                 //
//                University of Goettingen               //
//                Goldschmidtstr. 5                      //
//                37077 Goettingen                       //
//                Germany                                //
//                                                       //
//    e-mail:     oconrad@saga-gis.org                   //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#include <wx/clipbrd.h>
#include <wx/dataobj.h>

#include "res_controls.h"
#include "res_commands.h"

#include "helper.h"

#include "active_description.h"

#include "wksp_base_item.h"
#include "wksp_tool_manager.h"


///////////////////////////////////////////////////////////
//                                                       //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
IMPLEMENT_CLASS(CActive_Description, wxHtmlWindow)

//---------------------------------------------------------
BEGIN_EVENT_TABLE(CActive_Description, wxHtmlWindow)
	EVT_MENU(ID_CMD_DESCRIPTION_COPY     , CActive_Description::On_Command_Event)
	EVT_MENU(ID_CMD_DESCRIPTION_COPY_HTML, CActive_Description::On_Command_Event)

	EVT_RIGHT_UP(CActive_Description::On_Mouse_Event)
	EVT_KEY_DOWN(CActive_Description::On_Key_Down   )
END_EVENT_TABLE()


///////////////////////////////////////////////////////////
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
CActive_Description::CActive_Description(wxWindow *pParent)
	: wxHtmlWindow(pParent, ID_WND_ACTIVE_DESCRIPTION, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_AUTO|wxSUNKEN_BORDER)
{
	// nop
}


///////////////////////////////////////////////////////////
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CActive_Description::Set_Item(CWKSP_Base_Item *pItem)
{
	m_pItem = pItem;

	if( m_pItem == NULL )
	{
		SetPage(_TL("No description available"));
	}
	else
	{
		SG_UI_Process_Set_Busy(true);//, CSG_String::Format("%s: %s...", _TL("Updating"), Get_Name()));
		wxString Description(m_pItem->Get_Description()); if( Description.Length() <= 8192 ) { Description.Replace("\n", "<br>"); }
		SG_UI_Process_Set_Busy(false);

		SetPage(Description);
	}

	return( true );
}


///////////////////////////////////////////////////////////
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CActive_Description::Clipboard_Copy(bool bHTML)
{
	if( wxTheClipboard->Open() )
	{
		wxString Text;

		if( !bHTML )
		{
			Text = SelectionToText(); if( Text.IsEmpty() ) { Text = ToText(); }
		}
		else if( m_pItem )
		{
			SG_UI_Process_Set_Busy(true);//, CSG_String::Format("%s: %s...", _TL("Updating"), Get_Name()));
			Text = m_pItem->Get_Description(); if( Text.Length() <= 8192 ) { Text.Replace("\n", "<br>"); }
			SG_UI_Process_Set_Busy(false);
		}

		if( Text.IsEmpty() == false )
		{
			wxTheClipboard->SetData(new wxTextDataObject(Text));
		}

		wxTheClipboard->Close();

		return( Text.IsEmpty() == false );
	}

	return( false );
}


///////////////////////////////////////////////////////////
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CActive_Description::On_Command_Event(wxCommandEvent &event)
{
	switch( event.GetId() )
	{
	default: event.Skip(); break;

	case ID_CMD_DESCRIPTION_COPY     : Clipboard_Copy(false); break;
	case ID_CMD_DESCRIPTION_COPY_HTML: Clipboard_Copy( true); break;
	}
}

//---------------------------------------------------------
void CActive_Description::On_Key_Down(wxKeyEvent &event)
{
	if( event.ControlDown() )
	{
		switch( event.GetKeyCode() )
		{
		default: event.Skip(); break;

		case 'C': case 'c': Clipboard_Copy(false); break;
		}
	}
}

//---------------------------------------------------------
void CActive_Description::On_Mouse_Event(wxMouseEvent &event)
{
	if( event.ButtonUp() && event.RightUp() ) // right-mouse-button-up event
	{
		wxMenu Menu;

		if( m_pItem && (m_pItem->Get_Type() == WKSP_ITEM_Tool || m_pItem->Get_Type() == WKSP_ITEM_Tool_Library)
		&&  g_pTools->Get_Parameter("DOCS_EDIT")->asBool() && g_pTools->Get_Parameter("DOCS_FOLDER")->is_Enabled() && SG_Dir_Exists(g_pTools->Get_Parameter("DOCS_FOLDER")->asString()) )
		{
			CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_RELOAD);

			Menu.AppendSeparator();
			CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_EDIT);

			if( m_pItem->Get_Type() == WKSP_ITEM_Tool )
			{
				CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_PARM);
				CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_REFS);
				CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_AUTH);
			}
			else // if( m_pItem->Get_Type() == WKSP_ITEM_Tool_Library )
			{
				CMD_Menu_Add_Item(&Menu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_REFS);
			}

			Menu.AppendSeparator();
		}

		CMD_Menu_Add_Item(&Menu, false, ID_CMD_DESCRIPTION_COPY);

		if( m_pItem )
		{
			CMD_Menu_Add_Item(&Menu, false, ID_CMD_DESCRIPTION_COPY_HTML);
		}

		PopupMenu(&Menu);
	}
}

//---------------------------------------------------------
void CActive_Description::OnLinkClicked(const wxHtmlLinkInfo &Link)
{
	Open_WebBrowser(Link.GetHref());
}


///////////////////////////////////////////////////////////
//                                                       //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////

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