/*	XTextControls.h
 *
 *		Standard text controls
 */

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  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_XTEXTCONTROLS
#define PandaWave_XTEXTCONTROLS

#include <XConfig.h>
#include <XView.h>
#include <XEvent.h>
#include <XString.h>

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

class XGFont;
class XGDraw;

/************************************************************************/
/*																		*/
/*	Internal structures													*/
/*																		*/
/************************************************************************/

/*	XGECharType
 *
 *		Enumeration of the character types I can type
 */

enum XGECharType {
	KXGEAnyChar = 0,
	KXGEAlphaNumeric = 1,					// letters and numbers only
	KXGEHexDigit = 2,						// hex digits only
	KXGENumeric = 3,						// numbers only
	KXGENumSign = 4,						// +/- and numbers
	KXGEFloating = 5						// +/-, numbers and '.'
};

/*	XGSTextInitRecord
 *
 *		This contains the common initialization parameters to initialize
 *	a button
 */

struct XGSTextInitRecord {
	XGSViewInitRecord			v;
	char						text[256];
	short						font;				// font resource ID
};

/*	XGSEditInitRecord
 *
 *		Edit initialization record
 */

struct XGSEditInitRecord {
	XGSTextInitRecord			v;
	
	bool						password;			// password field?
	short						maxchars;			// max characters
	XGECharType					chartype;			// what can i type?
};

/************************************************************************/
/*																		*/
/*	Standard Classes													*/
/*																		*/
/************************************************************************/

/*	@C"XGStaticText"
 *	@D"XGView"
 *	@H"XTextControls.h"
 *	@I
 *
 *		Standard static text control. I do this myself...
 */

class XGStaticText : public XGView {
	public:
								XGStaticText(XGOwner *parent, XGArgStream &init);
								XGStaticText(XGOwner *parent, XGSTextInitRecord &init);
		virtual					~XGStaticText();
		
		enum {
			kClassID = 'text'
		};
		
		/*
		 *	Standard behaviour routines
		 */
		
		void					DoDrawView(Rect);
		
		/*
		 *	Text hooks
		 */
		
		void					GetTitle(char *) const;
		void					SetTitle(const char *);

	private:
		void					Init(char *, short);
		
		short					SkipSpace(const char *, short);
		short					NextWord(const char *, short);
		short					CharWidth(XGDraw &, const char *, short);
		void					DrawLines(XGDraw &, const char *, short, short, short);

		XGFont					*fFontPtr;
		XGString				fString;
};


/*	@C"XGEditText"
 *	@D"XGView"
 *	@H"XTextControls.h"
 *	@I
 *
 *		And this is my standard edit control
 */

class XGEditText : public XGView {
	public:
								XGEditText(XGOwner *parent, XGArgStream &init);
								XGEditText(XGOwner *parent, XGSEditInitRecord &init);
		virtual					~XGEditText();
		
		enum {
			kClassID = 'edit'
		};
		
		/*
		 *	Standard behaviour routines
		 */
		
		void					DoDrawView(Rect);
		void					DoActivate(bool);
		bool					DoMouseDown(Point,short);
		void					DoMouseMove(Point,short);
		void					DoMouseUp(Point,short);
		
		void					GainFocus();
		void					LoseFocus();
		
		/*
		 *	Dispatched messages
		 */
		
		DECLAREDISPATCH
		long					DoIdle(long, void *);
		long					DoGetMenuStatus(long, void *);
		long					DoMenuCommand(long, void *);
		long					DoKey(long, void *);

		/*
		 *	Text selection
		 */
		
		void					SetSelect(short,short);
		void					GetSelect(short *, short *);
		
		/*
		 *	Editing control
		 */
		
		void					SetAttributes(XGECharType type, short max = 0)
									{
										fCharType = type;
										fMaxChars = max;
									}
		
		/*
		 *	Text Hooks
		 */
		
		void					GetTitle(char *) const;
		void					SetTitle(const char *);
		
		/*
		 *	Standard editor modifier hooks
		 */
		
		virtual bool			CanInsertKey(char);
		virtual bool			IsSpecialKey(char);
		virtual void			DrawTextFrame(XGDraw &, Rect);
		virtual bool			ValidateText(char *);
		
	private:
		void					Init(char *, short, short, XGECharType, bool);

		XGFont					*fFontPtr;
		bool					fPassword;

		char					fText[256];
		short					fStart;
		short					fEnd;
		short					fLength;
		short					fScroll;
		short					fMaxScroll;
		
		short					fSelectStart;
		short					fMaxChars;
		XGECharType				fCharType;
		long					fTime;					// cursor time

		void					InsertChar(char);
		void					DeleteChar();
		void					DrawContents(XGDraw &draw);
		short					HitDetect(XGDraw &, short x);
		
		void					RecalcScroll();
		void					ScrollInRange();
		
		void					DoCut();
		void					DoCopy();
		void					DoPaste();
		void					DoClear();
};

/*	@C"XGGroupBox"
 *	@D"XGView"
 *	@H"XTextControls.h"
 *	@I
 *
 *		Standard group box. This creates a group box which draws
 *	a rectangle around the view. On the Macintosh, I duplicate the
 *	functionality in the XGStaticText view; in Windows, I call
 *	up the correct view window
 */

class XGGroupBox : public XGView {
	public:
								XGGroupBox(XGOwner *parent, XGArgStream &init);
								XGGroupBox(XGOwner *parent, XGSTextInitRecord &init);
								~XGGroupBox();

		enum {
			kClassID = 'grou'
		};

		/*
		 *	Group box
		 */
		
		void					DoDrawView(Rect);
		
		/*
		 *	Text hooks
		 */
		
		void					GetTitle(char *) const;
		void					SetTitle(const char *);

	private:
		void					Init(char *, short);

		XGFont					*fFontPtr;
		XGString				fString;
};


#endif
