/* SAMPLE PAINT PROGRAM TO ILLUSTRATE XSUPPORT.

  Overview
  --------
  After studying this program you should know how to create
  canvases and various UI widgets (sliders, buttons, etc.) 
  using XSupport. 
  
  Organization
  ------------
  The top portion of this file defines global variables and call-back
  functions which XSupport references for program logic and UI state. 
  Once the UI is defined main() launches the program.

  A UI widget is generally defined by these three parts:

    1. Global call-back function(s)
	   Each widget must reference a function that is called if a user
	   activates the widget. This function is called when the user pushes 
	   a button.
  
    2. Global state variable(s)
	   Most widgets require a state variable. A check box for example
	   needs to know wheter it is on or off.

    3. A global widget specific data structure
	   Finally each widget has a particular data struct that
	   ties all of its helper functions and variable together.
	   These widget data structures contain additional qualifiers
	   to specify labels, min/max values, or step size.


  History
  -------
  Created 4/94 by Apostolos Lerios - tolis@cs. 
  Modified 1/95 by Chase Garfinkle - chase@cs. 
  Added Comments 9/02 by Georg Petschnigg - georgp@stanford.edu

*/


#include <stdlib.h>
#include <stdio.h>

#include "xsupport.h"

/*****************************************************************************/
/* GLOBAL VARIABLES                                                          */
/*****************************************************************************/


/* PUSHBUTTONS. */
static void QuitButton() 
{
	printf("Goodbye!\n");
	exit(0);
}

PushButton PushButtons[] =
{
	{ NULL, "Quit", &QuitButton },
	{ NULL, NULL, NULL }
};


/* SLIDERS. */
static void Slider1Changed(float NewValue);
static void Slider2Changed(float NewValue);
static void DependentSlider1Changed(float NewValue);
static void DependentSlider2Changed(float NewValue);

Slider Sliders[] =
{
	{ NULL, "Slider 1", 0, 100, 10, 2, &Slider1Changed },
	{ NULL, "Slider 2", 0, 100, 10, 2, &Slider2Changed },
	{ NULL, "Dependent Slider 1", 0, 100, 100, 0, &DependentSlider1Changed },
	{ NULL, "Dependent Slider 2", -100, 0, -100, 0, &DependentSlider2Changed },
	{ NULL, NULL, 0, 0, 0, 0, NULL }
};

static void Slider1Changed(float NewValue) 
{
	printf("Slider 1 value %f\n",NewValue);
}

static void Slider2Changed(float NewValue) 
{
	printf("Slider 2 value %f\n",NewValue);
}

static void DependentSlider1Changed(float NewValue) 
{
	printf("Dependent Slider 1 value %f\n",NewValue);
	SetSlider(&Sliders[3],(int) -NewValue);
}

static void DependentSlider2Changed(float NewValue) 
{
	printf("Dependent Slider 2 value %f\n",NewValue);
	SetSlider(&Sliders[2],(int) -NewValue);
}


/* CANVASES. */

static void MouseInCanvas1(int,int,unsigned int);
static void MouseInCanvas2(int,int,unsigned int);


/* If the fourth parameter (the PuffInterval) is non-negative, the */
/* canvas will register mouse events every PuffInterval milliseconds, */
/* even if the mouse does not move and no button is pressed or */
/* released. */
Canvas Canvases[] =
{
	{ NULL, 256, 256, -1, NULL, &MouseInCanvas1, 0 },
	{ NULL, 256, 256, -1, NULL, &MouseInCanvas2, 0 },
	{ NULL, 0, 0, 0, NULL, NULL, 0 }
};

static void MouseInCanvas1(int X, int Y, unsigned int ButtonDown) 
{
	/*
		printf("Mouse in canvas 1 at %d %d; buttons/modifiers %d\n",
						   X,Y,ButtonDown);
	*/
	if (ButtonDown &&
		X>=0 && X<Canvases[0].Width &&
		Y>=0 && Y<Canvases[0].Height) 
	{
		if (ButtonDown&ShiftMask)
			SET_RED(PIXEL(&Canvases[0],X,Y),255);
		else
			SET_GREEN(PIXEL(&Canvases[0],X,Y),255);
		UpdateCanvas(&Canvases[0],X,X,Y,Y);
	}
}

static void MouseInCanvas2(int X, int Y, unsigned int ButtonDown) 
{
/*
	printf("Mouse in canvas 2 at %d %d; buttons/modifiers %d\n",
             	   X,Y,ButtonDown);
	sleep(1);
*/
}


/* DIALOG BUTTONS. */

static void LoadPPM(char *Name) 
{
	
	Canvas NewCanvas;
	
	if (!LoadCanvas(Name,&NewCanvas)) 
	{
		printf("Load failed!\n");
		return;
	}
	ResizeCanvas(&Canvases[0], NewCanvas.Width, NewCanvas.Height);
	memcpy(Canvases[0].Pixels, NewCanvas.Pixels, 
		sizeof(long) * NewCanvas.Width * NewCanvas.Height);
	free(NewCanvas.Pixels);
	UpdateCanvas(&Canvases[0],0,Canvases[0].Width-1,0,Canvases[0].Height-1);
}

static void SavePPM(char *Name) 
{
	if (!SaveCanvas(Name,&Canvases[0]))
		printf("Save failed!\n");
}


DialogButton DialogButtons[] =
{
    { NULL, "Load PPM", "Load image from PPM file:", &LoadPPM },
    { NULL, "Save PPM", "Save image to PPM file:", &SavePPM },
    { NULL, NULL, NULL, NULL }
};


/* CHOICE BUTTONS. */
/* Checkboxes. */

static void CheckBox1Changed(int Set) 
{
	printf("CheckBox 1 status %d!\n",Set);
}

static void CheckBox2Changed(int Set) 
{
	printf("CheckBox 2 status %d!\n",Set);
}

ChoiceButton CheckBoxChoices[] =
{
	{ NULL, "Check Box 1", 0, &CheckBox1Changed },
	{ NULL, "Check Box 2", 1, &CheckBox2Changed },
	{ NULL, NULL, 0, NULL }
};

/* Radio buttons. */

static void RadioButton1Changed(int Set) 
{
	printf("RadioButton 1 status %d!\n",Set);
}

static void RadioButton2Changed(int Set) 
{
	printf("RadioButton 2 status %d!\n",Set);
}

ChoiceButton RadioButtonChoices[]=
{
	{ NULL, "Radio Button 1", 1, &RadioButton1Changed },
	{ NULL, "Radio Button 2", 0, &RadioButton2Changed },
	{ NULL, NULL, 0, NULL }
};

/* Mode-selecting radio buttons. */

static CanvasMode Mode=ALL_COLORS;
static int GammaCorrect=1;

static void Update() { SetCanvasMode(&Canvases[0],Mode,GammaCorrect); }

static void SetGammaCorrectionOn(int Set) 
{
	if (Set && !GammaCorrect) 
	{
		GammaCorrect=1;
		Update();
	}
}

static void SetGammaCorrectionOff(int Set) 
{
	if (Set && GammaCorrect) 
	{
		GammaCorrect=0;
		Update();
	}
}

ChoiceButton GammaCorrectionChoices[] =
{
	{ NULL, "On", 1, &SetGammaCorrectionOn },
	{ NULL, "Off", 0, &SetGammaCorrectionOff },
	{ NULL, NULL, 0, NULL }
};

static void SetAllColorsMode(int Set) 
{
	if (Set && Mode!=ALL_COLORS) 
	{
		Mode=ALL_COLORS;
		Update();
	}
}

static void SetOnlyRedMode(int Set) 
{
	if (Set && Mode!=ONLY_RED) 
	{
		Mode=ONLY_RED;
		Update();
	}
}

static void SetOnlyGreenMode(int Set) 
{
	if (Set && Mode!=ONLY_GREEN) 
	{
		Mode=ONLY_GREEN;
		Update();
	}
}

static void SetOnlyBlueMode(int Set) 
{
	if (Set && Mode!=ONLY_BLUE) 
	{
		Mode=ONLY_BLUE;
		Update();
	}
}

ChoiceButton CanvasModeChoices[] =
{
	{ NULL, "All Colors", 1, &SetAllColorsMode },
	{ NULL, "Red", 0, &SetOnlyRedMode },
	{ NULL, "Green", 0, &SetOnlyGreenMode },
	{ NULL, "Blue", 0, &SetOnlyBlueMode },
	{ NULL, NULL, 0, NULL }
};

/* Interdependent buttons. */

static void DependentCheckBox1Changed(int);
static void DependentCheckBox2Changed(int);
static void DependentCheckBox3Changed(int);

ChoiceButton DependentCheckBoxChoices[] = 
{
	{ NULL, "Dependent Check Box 1", 1, &DependentCheckBox1Changed },
	{ NULL, "Dependent Check Box 2", 0, &DependentCheckBox2Changed },
	{ NULL, "Dependent Check Box 3", 0, &DependentCheckBox3Changed },
	{ NULL, NULL, 0, NULL }
};

static void DependentCheckBox1Changed(int Set) 
{
	printf("Dependent 1 status %d!\n",Set);
	SetChoice(&DependentCheckBoxChoices[2],0);
}

static void DependentCheckBox2Changed(int Set) 
{
	printf("Dependent 2 status %d!\n",Set);
	SetChoice(&DependentCheckBoxChoices[2],1);
}

static void DependentCheckBox3Changed(int Set) 
{
	printf("Dependent 3 status %d!\n",Set);
}

/* Collect sets. */

ChoiceButtonSet ChoiceButtons[] = 
{
	{ NULL, "Check Boxes: ", CheckBoxChoices, 0 },
	{ NULL, "Radio Buttons: ", RadioButtonChoices, 1 },
	{ NULL, "Gamma correct (8-bit mode only)", GammaCorrectionChoices, 1 },
	{ NULL, "Canvas mode: ", CanvasModeChoices, 1 },
	{ NULL, "Dependent Check Boxes: ", DependentCheckBoxChoices, 0 },
	{ NULL, NULL, NULL, 0}
};

/*****************************************************************************/
/* MAIN PROGRAM START                                                        */
/*****************************************************************************/

/* Main initializes the canvases (defined globably) fills them with a
   red-green ramp and then calls LiftOff() to start the User Interface
   thread.
*/

int main(int argc, char *argv[]) 
{
	
    int i, buf_width, buf_height, num_canvases, r, g;
    int buf_size;
    unsigned long *buf;
	
	/* Assign red-green ramp to the canvas pixels */
    num_canvases = 2;
    for (i = 0; i < num_canvases; i++) 
	{
		buf_width = Canvases[i].Width;
		buf_height = Canvases[i].Height;
		buf_size = buf_width * buf_height;
		
		/* Be sure to allocate your canvas before lift_off() */
		
		Canvases[i].Pixels = (unsigned long *)malloc(buf_size *
			sizeof(unsigned long));
		
		buf = Canvases[i].Pixels;
		/* Fill buffers with red-green ramp */
		for (r = 0; r < buf_height; r++) 
		{
			for (g = 0; g < buf_width; g++, buf++) 
			{
				*buf = g*256 + r;
			}
		}
    }
    
	LiftOff(&argc,argv,PushButtons,DialogButtons,ChoiceButtons,Sliders,Canvases);
	
	return 0;
}
