Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#include "VerbatimFormatter.h"
using namespace std;
VerbatimFormatter :: VerbatimFormatter(EditorDataController *edc)
: edc(edc),
ra(0), ca(0),
x(0), y(0),
rows(0), cols(0)
{
}
VerbatimFormatter :: ~VerbatimFormatter()
{
}
// Settings
void VerbatimFormatter :: SetMaxWidth(int maxWidth)
{
cols = maxWidth;
}
int VerbatimFormatter :: GetMaxWidth()
{
return cols;
}
void VerbatimFormatter :: SetMaxHeight(int maxHeight)
{
rows = maxHeight;
}
int VerbatimFormatter :: GetMaxHeight()
{
return rows;
}
// Render Methods
void VerbatimFormatter :: Build()
{
AdjustCoordinates();
}
int VerbatimFormatter :: GetRenderableLineCount()
{
return edc->GetNumLines() - ra;
}
shared_ptr<string> VerbatimFormatter :: GetLine(int y)
{
int endOfWindow = ca+cols;
return shared_ptr<string>(new string(edc->GetLineSegment(y+ra,ca,endOfWindow)));
}
void VerbatimFormatter :: GetCursorPosition(int &row, int &col)
{
row = y;
col = x;
}
void VerbatimFormatter :: SetCursorPosition(int row, int col)
{
y = row;
x = col;
}
// Cursor Conversion
void VerbatimFormatter :: GridToCursor(int &row, int &col)
{
row-=ra;
col-=ca;
}
void VerbatimFormatter :: CursorToGrid(int &row, int &col)
{
row+=ra;
col+=ca;
}
// Actions
void VerbatimFormatter :: HandleMoveLeft()
{
--x;
}
void VerbatimFormatter :: HandleMoveRight()
{
++x;
}
void VerbatimFormatter :: HandleMoveUp()
{
--y;
}
void VerbatimFormatter :: HandleMoveDown()
{
if (y == rows-2) {
++ra;
} else {
++y;
}
}
// Special Actions
void VerbatimFormatter :: HandlePageUp()
{
y-=GetMaxHeight();
}
void VerbatimFormatter :: HandlePageDown()
{
y+=GetMaxHeight();
}
void VerbatimFormatter :: HandleHome()
{
x = ca = 0;
}
void VerbatimFormatter :: HandleEnd()
{
x = edc->GetLineLength(y+ra);
}
// Private Methods
void VerbatimFormatter :: AdjustCoordinates()
{
// rows = 6. What will y be if y == 6 on this call.
// if (y == 6) { throw string("Y is 6 and rows is " + to_string(rows)); }
// First we fix y
if (y < 0 && !ra) {
y = 0; // We've gone off screen to a point that doesn't exist
} else if (y < 0) {
// Goal is for y to equal 0. Then we have to move ra.
ra += y;
y = 0;
}
int numLines = edc->GetNumLines();
if (y+ra >= numLines || y > (rows-2)) {
y = 0;
ra = 0;
while (y != rows-2 && y != numLines-1) {
++y;
}
while (y+ra != numLines-1) {
++ra;
}
}
// Second we fix x
if (x < 0 && !ca) {
x = 0; // Gone off screen to a point we shouldn't be.
} else if (x < 0) {
ca += x;
x = 0;
} else if (x >= cols) {
ca += (x-(cols-1));
x = cols-1;
}
int lineLen = edc->GetLineLength(y+ra);
while ((x+ca) > lineLen && x > 0) {
--x;
}
while ((x+ca) > lineLen && ca > 0) {
--ca;
}
}