void PaintView::autoDraw(bool randomSize) 
{
	glClear(GL_COLOR_BUFFER_BIT);
	initSetup();
	glBlendFunc(GL_NONE, GL_NONE);
	glDisable(GL_BLEND);

	int canvasWidth = m_pDoc->m_nWidth;
	int canvasHeight = m_pDoc->m_nHeight;
	int spacing = m_pDoc->getSpacing();
	Point source(coord.x + m_nStartCol, m_nEndRow - coord.y);
	Point target(coord.x, m_nWindowHeight - coord.y);
	

	for (int i = 0; i < canvasWidth; i += spacing) {
		for (int j = 0; j < canvasHeight; j += spacing) {
			m_pDoc->m_pCurrentBrush->BrushBegin(Point(m_nStartCol, m_nEndRow), Point(0, m_nWindowHeight));
			m_pDoc->m_pCurrentBrush->BrushMove(Point(i + m_nStartCol, m_nEndRow - j), Point(i, m_nWindowHeight - j));
		}
	}

	m_pDoc->m_pCurrentBrush->BrushEnd(Point(canvasWidth - 1 + m_nStartCol, m_nEndRow - canvasHeight - 1),
									  Point(canvasWidth - 1, m_nWindowHeight - canvasHeight - 1));
	
	SaveCurrentContent();
	glFlush();

	#ifndef MESA
		// To avoid flicker on some machines.
		glDrawBuffer(GL_BACK);
	#endif // !MESA
}
void PaintView::draw()
{
	#ifndef MESA
	// To avoid flicker on some machines.
  //	glDrawBuffer(GL_FRONT_AND_BACK);
	#endif // !MESA

	if(!valid())
	{

		glClearColor(0.7f, 0.7f, 0.7f, 1.0);

		// We're only using 2-D, so turn off depth 
		glDisable( GL_DEPTH_TEST );

		ortho();

		glClear( GL_COLOR_BUFFER_BIT );
	}

	Point scrollpos;// = GetScrollPosition();
	scrollpos.x = 0;
	scrollpos.y	= 0;

	m_nWindowWidth	= w();
	m_nWindowHeight	= h();

	int drawWidth, drawHeight;
	drawWidth = min( m_nWindowWidth, m_pDoc->m_nPaintWidth );
	drawHeight = min( m_nWindowHeight, m_pDoc->m_nPaintHeight );

	int startrow = m_pDoc->m_nPaintHeight - (scrollpos.y + drawHeight);
	if ( startrow < 0 ) startrow = 0;

	m_pPaintBitstart = m_pDoc->m_ucPainting + 
		3 * ((m_pDoc->m_nPaintWidth * startrow) + scrollpos.x);

	m_nDrawWidth	= drawWidth;
	m_nDrawHeight	= drawHeight;

	m_nStartRow		= startrow;
	m_nEndRow		= startrow + drawHeight;
	m_nStartCol		= scrollpos.x;
	m_nEndCol		= m_nStartCol + drawWidth;

	if ( m_pDoc->m_ucPainting && !isAnEvent) 
	{
		RestoreContent();

	}

	if ( m_pDoc->m_ucPainting && isAnEvent) 
	{

		// Clear it after processing.
		isAnEvent	= 0;	

		Point source( coord.x + m_nStartCol, m_nEndRow - coord.y );
		Point target( coord.x, m_nWindowHeight - coord.y );
		
		// This is the event handler
		switch (eventToDo) 
		{
		case LEFT_MOUSE_DOWN:
			m_pDoc->m_pCurrentBrush->BrushBegin( source, target );
			break;
		case LEFT_MOUSE_DRAG:
			m_pDoc->m_pCurrentBrush->BrushMove( source, target );
			break;
		case LEFT_MOUSE_UP:
			m_pDoc->m_pCurrentBrush->BrushEnd( source, target );

			SaveCurrentContent();
			RestoreContent();
			break;
		case RIGHT_MOUSE_DOWN:
			m_pDoc->m_pCurrentBrush->RightBrushBegin( source, target );
			break;
		case RIGHT_MOUSE_DRAG:
			m_pDoc->m_pCurrentBrush->RightBrushMove( source, target );
			break;
		case RIGHT_MOUSE_UP:
			m_pDoc->m_pCurrentBrush->RightBrushEnd( source, target );
			break;

		default:
			printf("Unknown event!!\n");		
			break;
		}
	}

	glFlush();

	#ifndef MESA
	// To avoid flicker on some machines.
	glDrawBuffer(GL_BACK);
	#endif // !MESA
}
void PaintView::draw()
{
	initSetup();

	if ( m_pDoc->m_ucPainting && !isAnEvent) 
	{
		RestoreContent(); // restore content upon refresh
	}

	if (m_pDoc->m_ucPainting && isAnEvent)
	{
		// Clear it after processing.
		isAnEvent = 0;

		Point source(coord.x + m_nStartCol, m_nEndRow - coord.y);
		Point target(coord.x, m_nWindowHeight - coord.y);
		// This is the event handler
		switch (eventToDo)
		{
		case LEFT_MOUSE_DOWN:
			m_pDoc->m_pUI->m_SizeRandButton->value(0);
			m_pDoc->m_pUI->setSizeRand(FALSE);
			savePreviousStrokes(); // save previous strokes before creating new strokes
			RestoreContent(); // put the strokes to colour buffer (no background)

			m_pDoc->m_pCurrentBrush->BrushBegin(source, target); // place the strokes to the colour buffer

			SaveCurrentContent(); // save the strokes to the buffer
			break;
		case LEFT_MOUSE_DRAG:
			RestoreContent();

			m_pDoc->m_pCurrentBrush->BrushMove(source, target);

			SaveCurrentContent();
			break;
		case LEFT_MOUSE_UP:
			RestoreContent();

			m_pDoc->m_pCurrentBrush->BrushEnd(source, target);
			
			SaveCurrentContent(); // save current strokes
			break;
		case RIGHT_MOUSE_DOWN:
			firstCoord = target;

			// Implement the right mouse stroke direction here
			rightClickDirectionLine = new RightClickDirectionLine(m_pDoc, "Right Click Direction Line");
			rightClickDirectionLine->BrushBegin(source, target);
			break;
		case RIGHT_MOUSE_DRAG:

			// We need to release the current content because 
			// each mouse drag refers to a different angle
			RestoreContent();

			rightClickDirectionLine->BrushMove(source, target);
			break;
		case RIGHT_MOUSE_UP:
			RestoreContent();

			rightClickDirectionLine->BrushEnd(source, target);
			angle = LineBrush::DetermineAngle(firstCoord, target);
			m_pDoc->setLineAngle(angle);
			delete rightClickDirectionLine;
			rightClickDirectionLine = NULL;

			break;
		default:
			printf("Unknown event!!\n");
			break;
		}
	}

	// put the background on top of the stroke
	// it sounds stupid, but if we want to do the other way,
	// we need to make everything rbga rather than rbg
	if (m_pDoc->m_ucPainting) {
		displayBackground(); 
	}

	glFlush();

	#ifndef MESA
		// To avoid flicker on some machines.
		glDrawBuffer(GL_BACK);
	#endif // !MESA

}