/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: classfinder_svc.h,v 1.6 2001/04/19 10:12:08 georgetasker Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __CLASSFINDER_SVC_G__
#define __CLASSFINDER_SVC_G__

#include "indexing_svc.h"
#include "wx/txtstrm.h"

class SourceParserBase;
class spContext;
class spFile;
class spOperation;
class spClass;
class TextBuffer;

class TextBuffer : public wxObject
{
public:
	TextBuffer();
	~TextBuffer();

	void SetContent( const wxString& text );
	const char* GetContent();

	void WriteString( const wxString& s );
	void WriteShort( int s );
	void WriteByte( int byte );
	int ReadShort();
	int ReadByte();
	wxString ReadString();

	void CleanUp();
	void Clear();

protected:
	void Resize( int newLen );

protected:
	char* mpBuf;
	int mReadHead;
	int mLen;
	int mBufLen;
};

struct SourceEditingContextInfo
{
	wxString mEditedText;

	wxStringList mCtxTypes;
	wxStringList mCtxIdentifiers;
};

class wxPMClassFinderService : public wxPMSimpleService,
							   public I_IndexHandler
{
public:
	wxPMClassFinderService();
	~wxPMClassFinderService();

	// overriden methods of wxPMService

	virtual bool Start();
	virtual bool Stop();

	virtual wxPMMergableMenuChain* GetMergableMenuChain();

	// some higher-level algorithms

	spContext* DetectLastContext( SourceEditingContextInfo& ctx );

	spClass* FindClass( const wxString& name );

	spContext* FindMember( const spClass* cl, const wxString& name, 
						   bool searchSuperClasses = TRUE );

	void DumpAllItems( wxTextOutputStream& stm );
	void DumpAllItemsToFile( const wxString& fname );

	// implementation of I_IndexHandler interface

	virtual const char* GetIndexHandlerName();

	virtual bool OnClearIndexItems( int indexId );
	virtual bool OnBeginBuild( int indexId );
	virtual bool OnIndexFile( const wxPMFileInfo& file, int fileId, int indexId );
	virtual bool OnEndBuild( int indexId );

	/*** command handlers ***/

	enum
	{
		ID_DUMP_CLASS_INFO = 100,
		ID_TEST_EXPR_PARSING
	};

	void OnDumpClassInfo( wxCommandEvent& event );
	void OnTestExprParsing( wxCommandEvent& event );

	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS( wxPMClassFinderService )

protected:

	// methods for (de)serialization of collected 
	// class/operation info in and out of wxString object

	void WriteParsedFile( spFile* file );
	void WriteParsedOperation( spOperation* op );
	void WriteParsedClass( spClass* cl );

	spClass* ReadClass();
	spOperation* ReadOperation();
	spContext* ReadContext();

	void WriteOpProperties( TextBuffer& buf, spOperation* op );
	void ReadOpProperties( TextBuffer& buf, spOperation* op );

protected:
	wxPMIndexingService* mpIndexSvc;
	SourceParserBase* mpParser;
	char* mpTmpBuf;
	int mTmpBufLen;

	TextBuffer mTBuf;
};
							   

#endif // __CLASSFINDER_SVC_G__