/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: pm_sample.cpp,v 1.14 2001/04/21 14:20:41 georgetasker Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
    #pragma implementation "pm_sample.cpp"
    #pragma interface "pm_sample.cpp"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "pm_sample.h"

#include "propedit.h"
#include "eventeditor.h"

//#include "debuggerpl.h"
#include "markersvc.h"

#include "wx/html/htmlwin.h"

#define USE_PM_GUI 1

#if USE_PM_GUI
#include "wx/pmf/pm_gui.h"
#endif

#include "resbrowser.h"

/***** Implementation for class wxPlainTextDocument *****/

IMPLEMENT_DYNAMIC_CLASS( wxPlainTextDocument, wxPMDocument )

wxPlainTextDocument::wxPlainTextDocument()

	: wxPMDocument()
{}

wxPlainTextDocument::~wxPlainTextDocument()
{
	int o = 0;
	++o;
}

bool wxPlainTextDocument::SavePMDocument( wxOutputStream& s )
{
	s.Write( (const char*)m_FullTextStr, m_FullTextStr.Length() );

	return TRUE;
}

bool wxPlainTextDocument::LoadPMDocument( wxInputStream& s )
{
	bool ok = wxPMReadStreamToString( s, m_FullTextStr );

	wxASSERT( ok ); // DBG::

	UpdateAllViews( NULL );

	return ok;
}

wxString& wxPlainTextDocument::GetFullText()
{
	return m_FullTextStr;
}

void wxPlainTextDocument::SetFullText( const wxString& txt )
{
	m_FullTextStr = txt;
}

/***** Implementation for class wxPlainTextView *****/

static wxCommandInfo _gPlainTextViewCommands[] = {

	wxCommandInfo( -1, "Nothing to do with plain-text yet..", "", "" ), 

	wxCommandInfo( 0, "" ) // terminator
};

static wxPMMergableMenuChain _gPlainTextViewChains[] = {

	wxPMMergableMenuChain( "/&Edit/PlainText/", _gPlainTextViewCommands ),

	wxPMMergableMenuChain( "", NULL ) // terminator
};

BEGIN_EVENT_TABLE( wxPlainTextView, wxPMView )

	EVT_TEXT( 100, wxPlainTextView::OnText )

END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS( wxPlainTextView, wxPMView )

wxPlainTextView::wxPlainTextView()

	: wxPMView(),
	  m_pTextCtrl( NULL )
{
}

wxPlainTextView::~wxPlainTextView()
{}

wxPMMergableMenuChain* wxPlainTextView::GetMergableMenuChain()
{
	return _gPlainTextViewChains;
}

void wxPlainTextView::OnUpdate(wxView* sender, wxObject* hint)
{
	if ( sender != this )

		m_pTextCtrl->SetValue( ((wxPlainTextDocument*)GetDocument())->GetFullText() );
}

bool wxPlainTextView::OnCreate(wxDocument* doc, long flags)
{
	if ( !wxPMView::OnCreate( doc, flags ) ) return FALSE;

	wxASSERT( doc->IsKindOf( CLASSINFO(wxPlainTextDocument) ) );

	wxPlainTextDocument* pPTDoc = (wxPlainTextDocument*)doc;

	m_pTextCtrl = new wxTextCtrl( GetViewFrame(), 100, pPTDoc->GetFullText(),
								 wxDefaultPosition, wxDefaultSize,
                                 wxTE_MULTILINE | wxSUNKEN_BORDER );

	// FIXME::
	// "X seems to require a forced resize" --Julian

	int x = 0, y = 0;
	GetViewFrame()->GetClientSize(&x, &y);
	GetViewFrame()->SetSize(-1,-1, x+1, y);
	GetViewFrame()->SetSize(-1,-1, x, y);

	m_pTextCtrl->SetFocus();

	return TRUE;
}

void wxPlainTextView::OnText( wxCommandEvent& event )
{
	((wxPlainTextDocument*)GetDocument())->SetFullText( m_pTextCtrl->GetValue() );

	GetDocument()->Modify( TRUE );
}

/***** Implementation for class wxPlainTextInfoPane *****/

IMPLEMENT_DYNAMIC_CLASS( wxPlainTextInfoPane, wxPMPane )

wxPlainTextInfoPane::wxPlainTextInfoPane()
{
}

wxPlainTextInfoPane::~wxPlainTextInfoPane()
{
}

bool wxPlainTextInfoPane::Start()
{
	return TRUE;
}

bool wxPlainTextInfoPane::Stop()
{
	return TRUE;
}

void wxPlainTextInfoPane::Serialize( wxConfigBase& cfg, const wxString& key, bool isLoading )
{
}

/***** Implementation for class wxHtmlView *****/

// TBD:: should be made statics of wxSourceCodeView class

static wxCommandInfo _gHtmlViewCommands[] = {

	wxCommandInfo( -1, "Nothing to do with html yet..", "", "" ), 

	wxCommandInfo( 0, "" ) // terminator
};

static wxPMMergableMenuChain _gHtmlViewChains[] = {

	wxPMMergableMenuChain( "/&Edit/Html/", _gHtmlViewCommands ),

	wxPMMergableMenuChain( "", NULL ) // terminator
};

IMPLEMENT_DYNAMIC_CLASS( wxHtmlView, wxPMView )

wxHtmlView::wxHtmlView()
{}

wxHtmlView::~wxHtmlView()
{
}

bool wxHtmlView::OnCreate( wxDocument* doc, long flags )
{
	if ( !wxPMView::OnCreate( doc, flags ) ) return FALSE;

	wxASSERT( doc->IsKindOf( CLASSINFO(wxPlainTextDocument) ) ); // DBG::

	wxPlainTextDocument* pPTDoc = (wxPlainTextDocument*)doc;

	pPTDoc->SetFullText( "<html>"
						 "<body TEXT=\"#000000\" BGCOLOR=\"#8D99BC\">"
						 "<font color=\"#00FFFF\"><center>"
						 "<h2>My <b>empty</b> <i>"
						 "<font color=\"#FFFF00\">HTML</font>"
						 "</i> document!</h2></html>" 
						 "</center></font>"
						 );

	m_pHtmlWnd = new wxHtmlWindow( GetViewFrame(), -1 );

	m_pHtmlWnd->SetPage( pPTDoc->GetFullText() );

	int x = 0, y = 0;
	GetViewFrame()->GetClientSize(&x, &y);
	GetViewFrame()->SetSize(-1,-1, x+1, y);
	GetViewFrame()->SetSize(-1,-1, x, y);

	return TRUE;
}

wxPMMergableMenuChain* wxHtmlView::GetMergableMenuChain()
{
	return _gHtmlViewChains;
}

void wxHtmlView::OnUpdate(wxView* sender, wxObject* hint)
{
	if ( sender != this )
	{
		wxString text = ((wxPlainTextDocument*)GetDocument())->GetFullText();
		m_pHtmlWnd->SetPage( text );
	}
}

/***** Implementation for class MySimpleWorkplace *****/

IMPLEMENT_DYNAMIC_CLASS( wxPMSimpleWorkplace, wxPMWorkplace )

wxPMSimpleWorkplace::wxPMSimpleWorkplace()
	: wxPMWorkplace()
{
	// add some some services to be present when workplace is active

	//AssociateServiceClass( CLASSINFO( wxPMWorkplaceBrowserPane ) );

#if USE_PM_GUI
	AssociateService( CLASSINFO( wxPMFileBrowserPane ) );
#endif

	AssociateService( CLASSINFO( wxDMNodePropertyPanel ) );
//	AssociateService( CLASSINFO( ResourceBrowser ) );
	AssociateService( CLASSINFO( EventEditorService ) );
//	AssociateServiceClass( CLASSINFO( wxPMTestPane ) );

	//AssociateService( CLASSINFO( wxsDebuggerService ) );
//	AssociateService( CLASSINFO( MarkerService ) );

}

bool wxPMSimpleWorkplace::SerializePMDocument( wxPMSerializer& ser )
{
	return wxPMWorkplace::SerializePMDocument( ser );
}

/***** Implementation for class wxPMTestPane *****/

IMPLEMENT_DYNAMIC_CLASS( wxPMTestPane, wxPMPane )

wxPMTestPane::wxPMTestPane()

	: wxPMPane(),
	  m_SmallList( 32,32, FALSE, 1 ),
	  m_pWnd( NULL )
{}

void wxPMTestPane::CreatePaneWindow( wxWindow* pParent )
{
	m_pWnd = new wxListCtrl( pParent, -1, wxDefaultPosition,
		wxDefaultSize, 
		wxLC_ICON
		| wxLC_AUTOARRANGE 
		);

	int idx = m_SmallList.Add( wxPMBmpMgr().FindBitmap( "misc/32x32_service" ) );

    m_pWnd->SetImageList( &m_SmallList, wxIMAGE_LIST_NORMAL  );

	for( size_t i = 0; i != 30; ++i )

		m_pWnd->InsertItem( 0, "First", 0 );

}

wxWindow* wxPMTestPane::GetPaneWindow()
{
	return m_pWnd;
}

bool wxPMTestPane::Start()
{
	// automatically request showing upon stratup
	wxPMBootSvc().ShowPane( this );

	return TRUE;
}
