// not UNIX
// StrawberryView.cpp : implementation of the CStrawberryView class
//

#include "stdafx.h"
#include "Strawberry.h"

#include "StrawberryDoc.h"
#include "CntrItem.h"
#include "StrawberryView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CStrawberryView

IMPLEMENT_DYNCREATE(CStrawberryView, CRichEditView)

BEGIN_MESSAGE_MAP(CStrawberryView, CRichEditView)
	ON_WM_DESTROY()

	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_CREATE()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
  //  ON_NOTIFY_RANGE(EN_SELCHANGE, 0, M1, MyLine)//  This do not work
  
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
  ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  ON_COMMAND(ID_EDIT_MY_UNDO, OnEditMyUndo)
  ON_UPDATE_COMMAND_UI(ID_EDIT_MY_UNDO, OnUpdateEditMyUndo)
  ON_COMMAND(ID_EDIT_MY_REDO, OnEditMyRedo)
  ON_UPDATE_COMMAND_UI(ID_EDIT_MY_REDO, OnUpdateEditMyRedo)
  ON_WM_CHAR()
END_MESSAGE_MAP()

// CStrawberryView construction/destruction

CStrawberryView::CStrawberryView()
{
  m_nWordWrap=WrapNone;
  //WrapChanged();
}

CStrawberryView::~CStrawberryView()
{
}

BOOL CStrawberryView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRichEditView::PreCreateWindow(cs);
}

void CStrawberryView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();

	// Set the printing margins (720 twips = 1/2 inch)
	SetMargins(CRect(720, 720, 720, 720)); // 720
}


// CStrawberryView printing

BOOL CStrawberryView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}


void CStrawberryView::OnDestroy()
{
  CStrawberryDoc *pDoc=GetDocument();
  if (pActivePj==pDoc) // On Destroy find new project window
  {
    pActiveFile=pActivePj;
    POSITION pos=pDocTemplate1->GetFirstDocPosition();
    while (pos && pActivePj==(pActiveFile=(CStrawberryDoc*)pDocTemplate1->GetNextDoc(pos)));
    if (pActivePj!=pActiveFile)
      pActivePj=pActiveFile;
    else
    {
      pActivePj=NULL;
      goto n1;
    }
  }
  else if (pActiveFile==pDoc)
  {
n1: POSITION pos=pDocTemplate2->GetFirstDocPosition();
    while (pos)
      if (pDoc!=(pActiveFile=(CStrawberryDoc*)pDocTemplate2->GetNextDoc(pos))
           && pActiveFile->m_FileType!=5) //Output
        goto n2;
    pActiveFile=pActivePj;
  }
n2:
  if (pDoc->m_FileType==1)
    delete pDoc->m_pInfo;

	// Deactivate the item on destruction; this is important
	// when a splitter view is being used
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
   CRichEditView::OnDestroy();
}



// OLE Server support

// The following command handler provides the standard keyboard
//  user interface to cancel an in-place editing session.  Here,
//  the server (not the container) causes the deactivation
void CStrawberryView::OnCancelEditSrvr()
{
	GetDocument()->OnDeactivateUI(FALSE);
}


// CStrawberryView diagnostics

#ifdef _DEBUG
void CStrawberryView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CStrawberryView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CStrawberryDoc* CStrawberryView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStrawberryDoc)));
	return (CStrawberryDoc*)m_pDocument;
}
#endif //_DEBUG


// CStrawberryView message handlers
void CStrawberryView::OnChange() 
{
  CStrawberryDoc *pDocument=GetDocument();
  if (pDocument->m_Star==0)
    return;
#if PROF
  if ((Environment&0x40000)==0 && pDocument->m_Star<5) // on Every Text Change
    pDocument->m_CharSet|=0x100;
#endif
  if (pDocument->m_Star==1)
    return;
  if (pDocument->m_Star<4)
  {
    pDocument->SetTitle(pDocument->GetTitle() + _T("*"));
    pDocument->m_Star=1;
  }
  else if (pDocument->m_Star==4) // Cap
  {
    CString temp=pDocument->GetTitle();
    temp.SetAt(temp.GetLength()-1, '*');
    pDocument->SetTitle(temp);
    pDocument->m_Star=1;
  }
  else if (pDocument->m_Star==5) // Put a cap
  {
    pDocument->SetTitle(pDocument->GetTitle() + _T("^"));
    pDocument->m_Star=4;
  }
  else if (pDocument->m_Star==6) // New
    pDocument->m_Star=2*(pOutput != &GetRichEditCtrl() );
}

void CStrawberryView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	if (bActivate)
    {
      CStrawberryDoc* pDoc=GetDocument();
#if TR
      if (pDoc==NULL)
        MessageBox(NULL, _T("Error"));
#endif
      if (pDoc->m_FileType==1)
        pActivePj=pActiveFile=pDoc;
      else if (pDoc->m_FileType!=5) //Output
        pActiveFile=pDoc;
      pMainFrame->PostMessage(WM_USER+3, 0, 0);
    }
	CRichEditView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

int CStrawberryView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
  if (CRichEditView::OnCreate(lpCreateStruct) == -1)
		return -1;
//  This do not work
// 	GetRichEditCtrl().SendMessage(EM_SETEVENTMASK, 0, (LPARAM) (DWORD)ENM_SELCHANGE);
  if (Environment&256) // Automatic Word Selection
		GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_AUTOWORDSELECTION);
	else
		GetRichEditCtrl().SetOptions(ECOOP_AND, ~(DWORD)ECO_AUTOWORDSELECTION);
//?	GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_SELECTIONBAR);
  CHARFORMAT cf;
  if (FileType==5) // Output
    cf.crTextColor=c_output;
  else if (FileType<5) // Normal
    cf.crTextColor=DefaultProject.m_Colors;
  else    // ADD File
    cf.crTextColor=c_green;
  my_strcpy(cf.szFaceName, DefaultProject.m_lf.lfFaceName);
  cf.yHeight=-DefaultProject.m_lf.lfHeight*15;
  if (cf.yHeight <190) cf.yHeight=190; //plamen test
  cf.dwMask=CFM_COLOR | CFM_FACE | CFM_SIZE  | CFM_BOLD | CFM_ITALIC| CFM_STRIKEOUT | CFM_UNDERLINE;
  cf.dwEffects=0;
  if (DefaultProject.m_lf.lfWeight>500)
    cf.dwEffects|=CFE_BOLD;
  if (DefaultProject.m_lf.lfItalic)
    cf.dwEffects|=CFE_ITALIC;
  if (DefaultProject.m_lf.lfUnderline)
    cf.dwEffects|=CFE_STRIKEOUT;
  if (DefaultProject.m_lf.lfStrikeOut)
    cf.dwEffects|=CFE_UNDERLINE;
  GetRichEditCtrl().SetDefaultCharFormat(cf);
	return 0;
}

void CStrawberryView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    pMainFrame->PostMessage(WM_USER+3, 0, 0);
	CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CStrawberryView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    pMainFrame->PostMessage(WM_USER+3, 0, 0);
	CRichEditView::OnLButtonDown(nFlags, point);
}

void CStrawberryView::OnEditPaste()
{
	ASSERT_VALID(this);
	m_nPasteType = 0;
	GetRichEditCtrl().Paste(); // PasteSpecial(CF_TEXT)
}

void CStrawberryView::OnEditMyUndo()
{
  CRichEditCtrl *pEdit=&GetRichEditCtrl();
  if (GetDocument()->m_Star==2 || GetDocument()->m_Star==3)
    GetDocument()->SetTitle(GetDocument()->GetTitle() + _T("*"));
  GetDocument()->m_Star=0;
  if (pEdit->CanUndo() && pEdit->GetUndoName()==UID_UNKNOWN)
  {
    CHARRANGE Selection;
    pEdit->GetSel(Selection);
    int EventMask=pEdit->SetEventMask(ENM_NONE);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOVSCROLL);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOHSCROLL);
    pEdit->HideSelection(TRUE, FALSE);
    do
      pEdit->Undo();
    while(pEdit->CanUndo() && pEdit->GetUndoName()==UID_UNKNOWN);
    pEdit->SetSel(Selection);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOVSCROLL);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOHSCROLL);
    if (pEdit->CanUndo())
      pEdit->Undo();
    pEdit->HideSelection(FALSE, FALSE);
    pEdit->SetEventMask(EventMask);
  }
  else if (pEdit->CanUndo())
    pEdit->Undo();
  GetDocument()->m_Star=1;
}
void CStrawberryView::OnUpdateEditMyUndo(CCmdUI *pCmdUI)
{
  if (!GetRichEditCtrl().CanUndo())
	  pCmdUI->Enable(FALSE);
}
void CStrawberryView::OnEditMyRedo()
{
  CRichEditCtrl *pEdit=&GetRichEditCtrl();
  if (GetDocument()->m_Star==2 || GetDocument()->m_Star==3)
    GetDocument()->SetTitle(GetDocument()->GetTitle() + _T("*"));
  GetDocument()->m_Star=0;
  pEdit->Redo();
  if (pEdit->CanRedo() && pEdit->GetRedoName()==UID_UNKNOWN)
  {
    CHARRANGE Selection;
    pEdit->GetSel(Selection);
    int EventMask=pEdit->SetEventMask(ENM_NONE);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOVSCROLL);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOHSCROLL);
    pEdit->HideSelection(TRUE, FALSE);
    do
      pEdit->Redo();
    while(pEdit->CanRedo() && pEdit->GetRedoName()==UID_UNKNOWN);
    pEdit->HideSelection(FALSE, FALSE);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOVSCROLL);
    pEdit->SetOptions(ECOOP_XOR, ECO_AUTOHSCROLL);
    pEdit->SetSel(Selection);
    pEdit->SetEventMask(EventMask);
  }
  GetDocument()->m_Star=1;
}
void CStrawberryView::OnUpdateEditMyRedo(CCmdUI *pCmdUI)
{
  if (!GetRichEditCtrl().CanRedo())
	  pCmdUI->Enable(FALSE);
}
void CStrawberryView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
#if PROF
  if (nChar==13 && (Environment&0x20000)!=0) // on Enter pressed
#else
  if (nChar==13 && (Environment&0x40000)==0) // on Enter pressed
#endif
  {
    CStrawberryDoc *pDocument=GetDocument();
    if ((pDocument->m_CharSet&0x80) && pDocument->m_Star!=0 && pDocument->m_Star<5)
      pDocument->m_CharSet|=0x100;
  }
  CRichEditView::OnChar(nChar, nRepCnt, nFlags);
}
