#include "srgp.h"
#include <stdio.h>

/* Based on testpaint.c as shipped with SRGP */

/* When the left mouse button is depressed, we paint in Replace mode.
   When the middle mouse button is depressed, we paint in buggy XOR
   mode as suggested by Problem 2.5.
   When the right mouse button is depressed, we paint in (allegedly)
   better XOR mode, my solution to Problem 2.5.
*/

#define LM   locator_measure


main()
{
   LM locmeasure;

   const int brushWidth = 10;
   const int brushHeight = 10;
   const int halfBrushWidth = 5;
   const int halfBrushHeight = 5;
   int col;
   rectangle brush, wholeScreen, wholeBrush;
   canvasID backup, brushCanvas;

   SRGP_begin ("Exercise 2.5 demonstration", 800,800,3,FALSE);

   backup = SRGP_createCanvas (800, 800);
   brushCanvas = SRGP_createCanvas (brushWidth, brushHeight);

   wholeScreen = SRGP_defRectangle (0,0,799,799);
   wholeBrush = SRGP_defRectangle (0,0,brushWidth-1,brushHeight-1);

/*
   SRGP_loadCommonColor (0, "black");
   SRGP_loadCommonColor (1, "blue");
   SRGP_loadCommonColor (2, "green");
   SRGP_loadCommonColor (3, "cyan");
   SRGP_loadCommonColor (4, "red");
   SRGP_loadCommonColor (5, "magenta");
   SRGP_loadCommonColor (6, "yellow");
   SRGP_loadCommonColor (7, "white");
*/


   SRGP_useCanvas (SCREEN_CANVAS);
   SRGP_setLocatorEchoType (CURSOR);
   SRGP_setFillStyle(SOLID); 
   SRGP_setLocatorButtonMask
      (LEFT_BUTTON_MASK | MIDDLE_BUTTON_MASK | RIGHT_BUTTON_MASK);
   SRGP_setInputMode(LOCATOR, SAMPLE);

   while (1) {
      SRGP_sampleLocator (&locmeasure);
      if (locmeasure.button_chord[LEFT_BUTTON]==DOWN &&
	  locmeasure.button_chord[RIGHT_BUTTON]==DOWN) break;
      if (locmeasure.button_chord[LEFT_BUTTON]==DOWN) {
	SRGP_setWriteMode (WRITE_REPLACE);
	do {
	   brush = SRGP_defRectangle (locmeasure.position.x-halfBrushWidth,
				     locmeasure.position.y-halfBrushHeight,
				     locmeasure.position.x+halfBrushWidth,
				     locmeasure.position.y+halfBrushHeight);
	   SRGP_fillRectangle (brush);
	   SRGP_sampleLocator (&locmeasure);
	   } while (locmeasure.button_chord[LEFT_BUTTON] == DOWN);
	}
      else if (locmeasure.button_chord[MIDDLE_BUTTON]==DOWN) {
	SRGP_setWriteMode (WRITE_XOR);
	do {
	   brush = SRGP_defRectangle (locmeasure.position.x-halfBrushWidth,
				     locmeasure.position.y-halfBrushHeight,
				     locmeasure.position.x+halfBrushWidth,
				     locmeasure.position.y+halfBrushHeight);
	   SRGP_fillRectangle (brush);
	   SRGP_sampleLocator (&locmeasure);
	   } while (locmeasure.button_chord[MIDDLE_BUTTON] == DOWN);
	}
      else if (locmeasure.button_chord[RIGHT_BUTTON]==DOWN) {
	SRGP_useCanvas (backup);
	SRGP_setWriteMode (WRITE_REPLACE);
	SRGP_copyPixel (SCREEN_CANVAS,wholeScreen,wholeScreen.bottom_left);
	do {
	   brush = SRGP_defRectangle (locmeasure.position.x-5,
				     locmeasure.position.y-5,
				     locmeasure.position.x+5,
				     locmeasure.position.y+5);
	   SRGP_useCanvas (brushCanvas);
	   SRGP_setWriteMode (WRITE_REPLACE);
	   SRGP_copyPixel (backup,brush,wholeBrush.bottom_left);
	   SRGP_setWriteMode (WRITE_XOR);
	   SRGP_fillRectangle (wholeBrush);
	   SRGP_useCanvas (SCREEN_CANVAS);
	   SRGP_setWriteMode (WRITE_REPLACE);
	   SRGP_copyPixel (brushCanvas,wholeBrush,brush.bottom_left);
	   SRGP_sampleLocator (&locmeasure);
	   } while (locmeasure.button_chord[RIGHT_BUTTON] == DOWN);
	}
      }
   }
