/******************************************************************************
 GMessageView.cc

	<Description>

	BASE CLASS = public JXTEBase

	Copyright © 1997 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

 *****************************************************************************/

//Class Header
#include <GMessageView.h>
#include <GPrefsMgr.h>
#include <GMGlobals.h>
#include <GMTextLink.h>

#include <JXColormap.h>
#include <JXDisplay.h>
#include <JXFontManager.h>
#include <JXScrollbar.h>

#include <JFontStyle.h>
#include <JFontManager.h>

#include <jASCIIConstants.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <iostream.h>
#include <jAssert.h>


/******************************************************************************
 Constructor

 *****************************************************************************/

GMessageView::GMessageView
	(
	JXMenuBar* 			menuBar, 
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
	:
   JXSRTextBase(kSelectableText, kFalse, kFalse, scrollbarSet, enclosure, 
   				hSizing, vSizing, x, y, w, h)
{
	GMessageViewX(scrollbarSet, enclosure, hSizing, vSizing, x, y, w, h);
	AppendEditMenu(menuBar, JXMenu::kMacintoshStyle, kFalse, kFalse, kFalse, kFalse);
	AppendSearchMenu(GetSearchDialog(), menuBar, JXMenu::kMacintoshStyle);
	SetAsActiveTextEditor();
}

GMessageView::GMessageView
	(
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
	:
   JXSRTextBase(kSelectableText, kFalse, kFalse, scrollbarSet, enclosure, 
   				hSizing, vSizing, x, y, w, h)
{
	GMessageViewX(scrollbarSet, enclosure, hSizing, vSizing, x, y, w, h);
}

void GMessageView::GMessageViewX
	(
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
{
	SetPTPrinter(GMGetPTPrinter());
	SetBackColor(GetColormap()->GetWhiteColor());
	ShouldAllowDragAndDrop(kTrue);
	SetDefaultFont(GGetPrefsMgr()->GetDefaultMonoFont(), GGetPrefsMgr()->GetDefaultFontSize());
	JSize tabChars = GGetPrefsMgr()->GetTabCharWidth();
	SetCRMTabCharCount(tabChars);
	JCoordinate charWidth = 
		TEGetFontManager()->GetCharWidth(GGetPrefsMgr()->GetDefaultMonoFont(),
			GGetPrefsMgr()->GetDefaultFontSize(), JFontStyle(), ' ');
	SetDefaultTabWidth(charWidth * tabChars);
	
	itsTextLink = new GMTextLink(GetDisplay(), this);
	assert(itsTextLink != NULL);

	ListenTo(GGetPrefsMgr());
}

/******************************************************************************
 Destructor

 *****************************************************************************/

GMessageView::~GMessageView()
{
	delete itsTextLink;
}

/******************************************************************************
 AdjustCursor (protected)

 ******************************************************************************/

void
GMessageView::AdjustCursor
	(
	const JPoint& 			pt, 
	const JXKeyModifiers& 	modifiers
	)
{
	const JTextEditor::CaretLocation caretLoc = CalcCaretLocation(pt);
	JIndexRange selRange;
	GetSelection(&selRange);
	JCursorIndex cursor;
	if (itsTextLink->LTAdjustCursor(caretLoc.charIndex, modifiers, selRange, &cursor))
		{
		DisplayCursor(cursor);
		}
	else
		{
		JXSRTextBase::AdjustCursor(pt, modifiers);
		}
}

/******************************************************************************
 HandleMouseDown (protected)

 ******************************************************************************/

void
GMessageView::HandleMouseDown
	(
	const JPoint& 			pt, 
	const JXMouseButton 	button,
	const JSize 			clickCount,
	const JXButtonStates&	buttonStates,
	const JXKeyModifiers& 	modifiers
	)
{
	JXSRTextBase::HandleMouseDown(pt, button, clickCount, buttonStates, modifiers);

	JIndex charIndex;
	if (GetCaretLocation(&charIndex))
		{
		itsTextLink->LTHandleMouseDown(charIndex, button, clickCount, buttonStates, modifiers);
		}
}

/******************************************************************************
 HandleMouseUp (protected)

 ******************************************************************************/

void
GMessageView::HandleMouseUp
	(
	const JPoint& 			pt, 
	const JXMouseButton 	button,
	const JXButtonStates&	buttonStates,
	const JXKeyModifiers& 	modifiers
	)
{
	JXSRTextBase::HandleMouseUp(pt, button, buttonStates, modifiers);
	JIndex charIndex;
	if (GetCaretLocation(&charIndex))
		{
		itsTextLink->LTHandleMouseUp(charIndex, button, buttonStates, modifiers);
		}
}

/******************************************************************************
 Receive

 ******************************************************************************/

void
GMessageView::Receive
	(
	JBroadcaster* sender,
	const JBroadcaster::Message& message
	)
{
	if (sender == GGetPrefsMgr() && message.Is(GPrefsMgr::kFontChanged))
		{
		JString fontName 	= GGetPrefsMgr()->GetDefaultMonoFont();
		JSize fontSize		= GGetPrefsMgr()->GetDefaultFontSize();
		JBoolean changed = 
			JConvertToBoolean(
				(fontName != GetDefaultFontName()) ||
				(fontSize != GetDefaultFontSize()));
		if (changed)
			{
			const JFontManager* fontMgr = GetFontManager();
			const JFloat h1 = fontMgr->GetLineHeight(GetDefaultFontName(),
													 GetDefaultFontSize(), JFontStyle());
			const JFloat h2 = fontMgr->GetLineHeight(fontName, fontSize, JFontStyle());
			JFloat vScrollScale    = h2 / h1;

			JXScrollbar *hScrollbar, *vScrollbar;
			const JBoolean ok = GetScrollbars(&hScrollbar, &vScrollbar);
			assert( ok );
			vScrollbar->PrepareForScaledMaxValue(vScrollScale);

			JSize tabCharCount = GGetPrefsMgr()->GetTabCharWidth();

			SetAllFontNameAndSize(fontName, fontSize, tabCharCount, kFalse, kFalse);
			}
		}
	else
		{
		JXSRTextBase::Receive(sender, message);
		}
}
