/*	TEditView.h
 *
 *		This is the view object which knows how to handle both a static
 *	and a changing text edit area.
 */

/*  XT - An example text editor application built with the YAAF library
 *
 *	PUBLIC DOMAIN: The source kit of 'xt' has been placed into the
 *	public domain by the author, William Edward Woody, on the hope
 *	that the source kit will provide a useful example of using the
 *	YAAF Library.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */


#ifndef PandaWave_TEDITVIEW
#define PandaWave_TEDITVIEW

#include <XView.h>
#include <XDialog.h>
#include "TEditCore.h"

/************************************************************************/
/*																		*/
/*	Constants															*/
/*																		*/
/************************************************************************/

#define KMarkDirty				'Dirt'

/************************************************************************/
/*																		*/
/*	Forwards															*/
/*																		*/
/************************************************************************/

class XGHandle;

struct PrefRecord {
	short	tabstop;
	bool	autoindent;
};

/************************************************************************/
/*																		*/
/*	Class Declaration													*/
/*																		*/
/************************************************************************/

/*	TEditView
 *
 *		The edit view object; this should live inside a scroll view
 */

class TEditView : public XGView {
	public:
								TEditView(XGOwner *, XGArgStream &);
		virtual					~TEditView();
		
		enum {
			kClassID = 'txED'
		};
		
		/*
		 *	Initialization support
		 */
		
		void					InitEdit(XGHandle *);
		
		/*
		 *	View methods
		 */
		
		void					DoDrawView(Rect);
		void					DoActivate(bool);
		void					DoSizeView(void);
		bool					DoMouseDown(Point,short);
		void					DoMouseMove(Point,short);
		void					DoMouseUp(Point,short);
		
		void					LoseFocus();
		
		/*
		 *	Events
		 */
		
		DECLAREDISPATCH
		long					DoKey(long,void *);
		long					DoPeriodic(long,void *);
		long					DoGetMenuStatus(long,void *);
		long					DoMenuCommand(long, void *);
	
	private:
		TEditCore				fEditText;
		
		void					RecalcScroll();
		void					ScrollInRange();
		unsigned long			HitDetect(Point);
		void					InvertCaret(void);
		
		void					InsertChar(char);
		void					DeleteChar();
		void					DrawCurLine(unsigned long sel = 0xFFFFFFFF);
		void					AutoIndent();
		
		unsigned long			FindPrevNext(unsigned long,bool);
		void					DoCursorLR(bool,bool);
		void					DoCursorUD(bool,bool);
		
		void					DoCut();
		void					DoCopy();
		void					DoPaste();
		void					DoClear();
		
		void					MarkDirty();
		
		long					TextWidth(XGDraw &, char *, unsigned long);
		void					DrawText(XGDraw &, char *, unsigned long);
		
		static long				FindDialogProc(XGDialog *, void *, long, long, void *);
		void					FindDialog();
		void					FindNext();
		void					Replace();
		void					ReplaceNext()
									{
										Replace();
										FindNext();
									}

		static long				GotoDialogProc(XGDialog *, void *, long, long, void *);
		void					GotoDialog();
		static long				PrefsDialogProc(XGDialog *, void *, long, long, void *);
		void					PrefsDialog();
									
		bool					fCaretState;
		bool					fAutoIndent;
		short					fTabStop;
		
		unsigned long			fLineHeight;
		unsigned long			fStartSelect;
		unsigned long			fEndSelect;
		unsigned long			fPosPixel;
		
		char					fSearch[256];
		char					fReplace[256];
};

#endif // PandaWave_TEDITVIEW

