
///////////////////////////////////////////////////////////
//                                                       //
//                         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 "wksp_tool_manager.h"

#include "active.h"
#include "active_description.h"


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

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

//---------------------------------------------------------
BEGIN_EVENT_TABLE(CActive_Description, wxHtmlWindow)
	EVT_MENU(ID_CMD_DESCRIPTION_COPY, 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
}


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

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

	case ID_CMD_DESCRIPTION_COPY:
		if( wxTheClipboard->Open() )
		{
			wxString Text(SelectionToText()); if( Text.IsEmpty() ) { Text = ToText(); }

			wxTheClipboard->SetData(new wxTextDataObject(Text));
			wxTheClipboard->Close();
		}
		break;
	}
}

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

		if( (g_pActive->Get_Active()->Get_Type() == WKSP_ITEM_Tool || g_pActive->Get_Active()->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(pMenu, false, ID_CMD_TOOL_DESCRIPTION_RELOAD   );
			pMenu->AppendSeparator();
			CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_DESCRIPTION_EDIT     );
			if( g_pActive->Get_Active()->Get_Type() == WKSP_ITEM_Tool )
			{
				CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_PARM);
				CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_REFS);
				CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_AUTH);
			}
			else // if( g_pActive->Get_Active()->Get_Type() == WKSP_ITEM_Tool_Library )
			{
				CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_DESCRIPTION_EDIT_REFS);
			}
			pMenu->AppendSeparator();
		}

		CMD_Menu_Add_Item(pMenu, false, ID_CMD_DESCRIPTION_COPY          );

		PopupMenu(pMenu);
	}
}

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

		case 'C': case 'c':
			if( wxTheClipboard->Open() )
			{
				wxTheClipboard->SetData(new wxTextDataObject(SelectionToText()));
				wxTheClipboard->Close();
			}
			break;
		}
	}
}

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


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

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