/*	LayoutViews.h
 *
 *		This contains the declaration of the various layout views
 *	which are used to edit the various layouts
 */

/*  YAAF Constructor - Rapid application development for YAAF
 *  Copyright (C) 1998 William Edward Woody
 *  
 *  This program 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.
 *  
 *  This program 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, 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_LAYOUTVIEWS
#define PandaWave_LAYOUTVIEWS

#include <XHandle.h>
#include <XView.h>

/************************************************************************/
/*																		*/
/*	Events																*/
/*																		*/
/************************************************************************/

#define	KSelectViewItem		'VItm'
#define	KUpdateViewItem		'UItm'


/************************************************************************/
/*																		*/
/*	Structures															*/
/*																		*/
/************************************************************************/

struct ViewTypeRecord;
struct ViewDeclaration;
struct ViewItemDeclaration;

class TResDocument;
class XGInlineEdit;
class XGPopMenuView;
class XGPopWindow;

/************************************************************************/
/*																		*/
/*	Views																*/
/*																		*/
/************************************************************************/

/*	TBackgroundView
 *
 *		This is a simple grey background
 */

class TBackgroundView : public XGView {
	public:
								TBackgroundView(XGOwner *owner, XGArgStream &stream);
		virtual					~TBackgroundView();
		enum {
			kClassID = 'pBAK'
		};
		
		void					DoDrawView(Rect);
};

/************************************************************************/
/*																		*/
/*	Edit Views															*/
/*																		*/
/************************************************************************/

/*	TRectEditView
 *
 *		Rectangle editor view
 */

class TRectEditView : public XGView {
	public:
								TRectEditView(XGOwner *, XGArgStream &);
		virtual					~TRectEditView();
		enum {
			kClassID = 'pRCT'
		};
		
		void					InitViewEdit(ViewDeclaration *v)
									{
										fView = v;
										fSelect = NULL;
										RecalcScroll();
										InvalView();
									}
		void					UpdateSelect(ViewItemDeclaration *item)
									{
										fSelect = item;
										RecalcScroll();
										InvalView();
									}
		void					UpdateContent(void)
									{
										RecalcScroll();
										InvalView();
									}
		
		/*
		 *	Events
		 */
		
		void					DoDrawView(Rect);
		void					DoSizeView();
		
		bool					DoMouseDown(Point,short);
		void					DoMouseMove(Point,short);
		void					DoMouseUp(Point,short);
		
		DECLAREDISPATCH
		long					DoKey(long,void *);
	
	private:
		ViewDeclaration			*fView;
		ViewItemDeclaration		*fSelect;
		Point					fHitStart;
		Point					fHitCur;
		short					fHitState;
		Rect					fHitLoc;
		Rect					fParLoc;
		
		short					SnapGrid(short);
		
		void					RecalcScroll(void);
		
		Rect					GetInitSize(void);
		void					DrawItem(XGDraw &, ViewItemDeclaration *, Rect);
		Rect					DrawViewItem(XGDraw &, ViewItemDeclaration *, Rect);

		Rect					CalcDrag(Point loc);
		Rect					CalcHilite(Rect, short);
		void					DrawHilite(XGDraw &, Rect);
		
		ViewItemDeclaration		*HitDetectInside(ViewItemDeclaration *, Point);
		ViewItemDeclaration		*HitDetectItem(Point);
		Rect					FindParentLocation(ViewItemDeclaration *);
		Rect					FindItemLocation(ViewItemDeclaration *);
		void					SetItemLocation(ViewItemDeclaration *, Rect);
		
		void					DrawView(XGDraw &, Rect);
		void					DrawScrollBar(XGDraw &, Rect);
		Rect					DrawScrollView(XGDraw &, ViewItemDeclaration *, Rect);
		void					DrawButton(XGDraw &, char *, Rect);
		void					DrawControlButton(XGDraw &, bool, char *, Rect);
		void					DrawText(XGDraw &, char *, Rect);
		void					DrawEdit(XGDraw &, char *, Rect);
		void					DrawGroup(XGDraw &, char *, Rect);
};

/*	TListView
 *
 *		Rectangle editor view
 */

class TListView : public XGView {
	public:
								TListView(XGOwner *, XGArgStream &);
		virtual					~TListView();
		
		enum {
			kClassID = 'pLST'
		};
		
		void					InitViewEdit(ViewDeclaration *v)
									{
										fViewDecl = v;
										RecalcScroll();
										InvalView();
									}
		void					UpdateSelect(ViewItemDeclaration *item);
		void					UpdateContent(void)
									{
										RecalcScroll();
										InvalView();
									}
		ViewItemDeclaration		*GetSelect()
									{
										return (fSelect != -1) ? 
													FindByIndex(fSelect) :
													NULL;
									}
		
		void					AddNewView();
		void					DeleteView();
									
		void					DoDrawView(Rect);
		void					DoSizeView();
		bool					DoMouseDown(Point, short);
		void					DoMouseMove(Point,short);
		void					DoMouseUp(Point,short);
	
	private:
		ViewDeclaration			*fViewDecl;
		long					fSelect;
		
		Rect					fLine;
		long					fWhere;
		ViewItemDeclaration		*fAfter;
		
		void					RecalcScroll();
		
		/*
		 *	Drawing support
		 */
		
		ViewItemDeclaration		*FindParent(ViewItemDeclaration *);
		long					FindByItem(ViewItemDeclaration *);
		ViewItemDeclaration		*FindByIndex(long count);
		
		long					CountItems(ViewItemDeclaration *);
		long					DrawItems(XGDraw &,
										  long  count,
										  short indent, 
										  short x, 
										  short y, 
										  ViewItemDeclaration *);
		void					DrawLine(XGDraw &,
										 long  count,
										 short indent, 
										 short x, 
										 short y, 
										 ViewItemDeclaration *);
};

/************************************************************************/
/*																		*/
/*	Views																*/
/*																		*/
/************************************************************************/

/*	TItemListView
 *
 *		Item list view editor
 */

class TItemListView : public XGView {
	public:
								TItemListView(XGOwner *, XGArgStream &);
		virtual					~TItemListView();
		
		enum {
			kClassID = 'pINF'
		};
		
		void					UpdateSelect(ViewItemDeclaration *item,
											 TResDocument *doc,
											 XGWindow *w);
		void					UpdateInspector(ViewItemDeclaration *item,
												TResDocument *doc,
												XGWindow *w)
									{
										if ((item == fItem) &&
											(doc == fDoc) &&
											(w == fWindow)) InvalView();
									}
		
		void					DoDrawView(Rect);
		void					DoSizeView(void);
		bool					DoMouseDown(Point,short);
		void					DoMouseMove(Point,short);
		void					DoMouseUp(Point,short);
		
		DECLAREDISPATCH
		long					DoInlineClose(long, void *);
		long					DoKeyEvent(long,void *);
	private:
		void					DrawLine(XGDraw &, short x, short y, short index);
		void					RecalcScroll();
		
		void					OpenPopupMenu();
		void					RunPopupMenu(Point,short);
		void					EndPopupMenu(Point,short);
		void					OpenInlineEdit();
		
		long					fSelect;
		XGWindow				*fWindow;
		TResDocument			*fDoc;
		ViewTypeRecord			*fType;
		ViewItemDeclaration		*fItem;

		XGPopWindow				*fPopWindow;
		XGPopMenuView			*fPopView;
		XGStringList			fPopList;
		XGInlineEdit			*fInline;
};

#endif // PandaWave_LAYOUTVIEWS
