Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE3150_Project/DocumentFormatter.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
172 lines (140 sloc)
3.08 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DocumentFormatter.h" | |
using namespace std; | |
DocumentFormatter :: DocumentFormatter(EditorDataController *edc) | |
: doc(edc), x(0), y(0) | |
{ | |
} | |
DocumentFormatter :: ~DocumentFormatter() | |
{ | |
} | |
// Settings | |
void DocumentFormatter :: SetMaxWidth(int maxWidth) | |
{ | |
doc.SetMaxWidth(maxWidth); | |
} | |
int DocumentFormatter :: GetMaxWidth() | |
{ | |
return doc.GetMaxWidth(); | |
} | |
void DocumentFormatter :: SetMaxHeight(int maxHeight) | |
{ | |
doc.SetMaxHeight(maxHeight); | |
} | |
int DocumentFormatter :: GetMaxHeight() | |
{ | |
return doc.GetMaxHeight(); | |
} | |
// Render Methods | |
void DocumentFormatter :: Build() | |
{ | |
doc.BuildDocument(); | |
AdjustCoordinates(); | |
} | |
int DocumentFormatter :: GetRenderableLineCount() | |
{ | |
return doc.GetActivePage()->GetNumLines(); | |
} | |
shared_ptr<string> DocumentFormatter :: GetLine(int x) | |
{ | |
string *str = new string(doc.GetActivePage()->GetLine(x)); | |
return shared_ptr<string>(str); | |
} | |
void DocumentFormatter :: GetCursorPosition(int &row, int &col) | |
{ | |
row = y; | |
col = x; | |
} | |
void DocumentFormatter :: SetCursorPosition(int row, int col) | |
{ | |
y = row; | |
x = col; | |
} | |
void DocumentFormatter :: GridToCursor(int &row, int &col) | |
{ | |
// Step one seach for paragraph equal to row | |
Build(); | |
int y = 0; | |
shared_ptr<DocumentParagraph> para = doc.GetPage(0)->GetParagraph(0); | |
while (para->GetParagraphNumber() != row) { | |
++y; | |
int numPages = (y-(y%GetMaxHeight()))/GetMaxHeight(); | |
int lineNum = y-(numPages*GetMaxHeight()); | |
para = doc.GetPage(doc.GetPageNumContainingLine(y))->GetParagraphContainingLine(lineNum); | |
} | |
y += para->GetRowFromGridCol(col); | |
row = y - (doc.GetActivePage()->GetPageNum()*GetMaxHeight()); | |
} | |
void DocumentFormatter :: CursorToGrid(int &row, int &col) | |
{ | |
doc.CursorToGrid(row,col); | |
} | |
// Actions | |
void DocumentFormatter :: HandleMoveLeft() | |
{ | |
if (!x && (y || doc.GetPageNum())) { | |
--y; | |
x = GetMaxWidth() + 1; | |
} else { | |
--x; | |
} | |
} | |
void DocumentFormatter :: HandleMoveRight() | |
{ | |
if (x == doc.GetActivePage()->GetLineLength(y) && (doc.GetActivePage()->GetNumLines()-1) != y) { | |
++y; | |
x = 0; | |
} else { | |
++x; | |
} | |
} | |
void DocumentFormatter :: HandleMoveUp() | |
{ | |
--y; | |
} | |
void DocumentFormatter :: HandleMoveDown() | |
{ | |
++y; | |
} | |
// Special Actions | |
void DocumentFormatter :: HandlePageUp() | |
{ | |
doc.PageUp(); | |
} | |
void DocumentFormatter :: HandlePageDown() | |
{ | |
doc.PageDown(); | |
} | |
void DocumentFormatter :: HandleHome() | |
{ | |
x = 0; | |
} | |
void DocumentFormatter :: HandleEnd() | |
{ | |
x = GetMaxWidth(); | |
} | |
// Private Methods | |
void DocumentFormatter :: AdjustCoordinates() | |
{ | |
int pageNum = doc.GetPageNum(); | |
while (y < 0 && pageNum > 0) { | |
y+=GetMaxHeight(); | |
doc.PageUp(); | |
--pageNum; | |
} | |
if (y < 0 && pageNum == 0) { | |
y = 0; // Can't go above the buffer | |
} | |
while (y >= GetMaxHeight() && pageNum < doc.GetNumPages()-1) { | |
y-=GetMaxHeight(); | |
doc.PageDown(); | |
++pageNum; | |
} | |
if (y >= doc.GetActivePage()->GetNumLines()) { | |
y = doc.GetActivePage()->GetNumLines()-1; | |
} | |
if (x < 0) { | |
x = 0; // That's easy. There's no left or right shifting in this formatter | |
} else if (x > doc.GetActivePage()->GetLineLength(y)) { | |
x = doc.GetActivePage()->GetLineLength(y); | |
} | |
} |