
;+ ***********************************************************************
; NAME:
;	CW_PLOTSERIE
;
; PURPOSE:
;	Cette fonction gere les parametres de trace en fonction du temps
;	des resultats de reduction de donnees du NRH	
;
; CATEGORY:
;	COMPOUND-WIDGET.
;
; CALLING SEQUENCE:
;	ID = CW_PLOTSERIE (Parent)
;
; INPUTS:
;	Parm1:	Describe the positional input parameters here. Note again
;		that positional parameters are shown with Initial Caps.
;	
; KEYWORD PARAMETERS:
;	Ces KEYWORDS servent a donner des valeurs initiales
;
;	YPLOT	nombre de traces en Y, 1 par defaut
;
;	XPLOT	nombre de traces en X, 1 par defaut
;
;	SCALE	Echelle logarithmique si = 0, lineaire si =1,
;		logarithmique par defaut
;
;	PRINT	Trace dans fichiers Poscript si = 1, sinon visualisation X
;		X par defaut
;
;	PSNAME	Nom du fichier POSCRIPT, file.ps par defaut
;
;	PNGNAME	Nom du fichier PNG, file.png par defaut
;
;	BACKGROUND visualiser le bouton BACKGROUND
;
;	COLOR	visualiser le bouton COLOR
;
; OUTPUTS:
;	Certain BUTTON ne retournent pas d'evenements:
;		XPLOT	nombre de traces en X (Plots on X)
;		YPLOT	nombre de traces en Y (Plots on Y)
;		PRINT	Ouverture d'un fichier Poscript si =1 (OPEN Print file)
;		CLOSE	Fermeture du fichier Poscript (CLOSE Print File)
;		PNG	Sauvegarde de la fenetre en PNG (SAVE on PNG)
;	Les autres BUTTON retournent un evenement et une stucture associee 
;	a l'evenement
;	STRUCTURE:
;		event = {id:parent, Top:event.top, Handler:0L,Value:val}
;	A chaque de ces evenements est associe une U_VALUE. Liste des 
;	U_VALUE et valeurs associee
;		PLOT	pas de valeur, PLOT IDL (PLOT)
;		OPLOT	pas de valeur, OPLOT IDL (OPLOT
;		SCALE	Ehelle logarithmique (0) ou lineaire (1) (LOG scale)
;		YRGC	Yrange defini par curseur, event.value = [Y1,Y2]
;			taleau des valeurs a passer a YRANGE (YRANGE Cursor)
;		YRGA	Yrange defini automatiquement, event.value=[0.0,0.0]
;				(YRANGE auto)
;		BCK	Controle du BACKGROUND
;		CLR	Controle de la couleur
;		
; PROCEDURE:
;	Cette procedure cree et gere les widgets necessaire a la visualisation
;	de traces en fonction du temps.
;	Elle gere les variables !P.MULTI et !D.NAME
;	Il est necessaire de gerer les evenements crees par cette fonction
;	dans la procedure appelante, c'est a dire de prevoir les traitements
;	correspondant a chacune des U_VALUE definies precedamment.
;	Le fichier POSCRIPT doit etre ferme avant de pouvoir etre 
;	visualisee
;	La table decouleur est modifiee par le widget COLOR YES et restauree
;	par COLOR NO 
;
; EXAMPLE:
;	Widget appelant
;       b= CW_PLOTSERIE(base3,PSNAME='flux.ps', PNGNAME='flux.png')
;
;	WIDGET_event, event
;		.....
;	WIDGET_CONTROL,event.id, Get_uvalue = ev
;	CASE EV OF
;		'PLOT': appeler le trace correspondant
;
;		'OPLOT': appeler le tracer correspondant a OPLOT
;
;		'SCALE': Echelle = event.value
;
;		'YRGA' : YRANGE = event.value
;
;		'YRGC':  YRANGE = event.value
;			............		
;
; MODIFICATION HISTORY:
;	Ecrit par: J Bonmartin le 04/02/98
;-*******************************************************************

Function CW_PLOTSERIE_event, event

COMMON STR_CW_PLOTSERIE, str_plot, Couleurs

ON_ERROR,2
parent= event.handler
Stash= WIDGET_INFO(Parent, /child)
VAL=0

WIDGET_CONTROL, event.id, GET_UVALUE = uv

	IF event.id EQ str_plot.xplot THEN BEGIN
		 !P.multi[1] = event.value
		 !P.multi[0] = 0
			RETURN,0
		End

	IF event.id EQ str_plot.yplot THEN  Begin
		!P.multi[2] = event.value
		 !P.multi[0] = 0
			RETURN,0
		End

	IF event.id EQ str_plot.scale THEN Val = event.index

	If event.id EQ str_plot.close THEN BEGIN
		If !D.name EQ 'PS' THEN BEGIN
			Device,/Close
			r=WIDGET_MESSAGE('FILE '+Str_plot.Ps+' Closed')
			!P.multi[0] = 0
			SET_PLOT,'X'
			WIDGET_CONTROL,Str_plot.Prnt,SET_VALUE = 0
			END
		RETURN,0
		END

	IF event.id EQ str_plot.prnt THEN BEGIN
		!P.multi(0)= !P.multi[1]*!P.multi[2] 
		IF event.select EQ 1 THEN BEGIN
			 Set_plot,'PS' 
			Ysize = 8 * !P.multi(2)
			Yoffset = 12 - 3 * (!P.multi(2)-1)
			IF Ysize GT 24 THEN Ysize = 24
			Device, Filename = Str_plot.ps , /COLOR,Bits=8,$
				YSIZE= ysize, YOFFSET = Yoffset
			END
		IF event.select EQ 0 Then BEGIN
			Device,/Close
			!P.multi[0] = 0
			r=WIDGET_MESSAGE('File '+Str_plot.Ps+' Closed')
			Set_plot,'X'
			END
	RETURN,0
	END
	
	IF event.id Eq Str_plot.Fgf THEN BEGIN
		r= WIDGET_MESSAGE ('SAVE on '+Str_plot.png)
		WRITE_PNG, Str_plot.png, TVRD()
	RETURN,0
	End

	IF event.id EQ Str_plot.yra THEN $
			VAL = [0,0]

	IF event.id EQ Str_plot.yrc THEN BEGIN
		R= WIDGET_MESSAGE('SELECT Y Range by 2 points')
		YT=FLTARR(2)
		CURSOR, X, Y, 4 & YT(0)=Y			
		CURSOR, X, Y, 4 & YT(1)=Y
		VAL = [Min(yt), MAX(yt)]
		R=WIDGET_MESSAGE('YRANGE= '+String(Val(0))+','+String(Val(1)))
	END

	IF event.id EQ Str_plot.bck THEN VAL = Event.index
;		VAL= 0 
;		IF Event.index EQ 1 THEN VAL = !D.Table_size-1
;		END
	IF event.id EQ Str_plot.clr THEN BEGIN
		VAL = Event.index
		IF Val EQ 1 THEN BEGIN
			NRH_COLOR_PLOT,rr,gg,bb,RED=R,GREEN=G,BLUE=B
			Couleurs[*,0]=R & Couleurs[*,1]=G & Couleurs[*,2]=B
		ENDIF ELSE TVLCT,Couleurs[*,0],Couleurs[*,1],Couleurs[*,2]
		END
WIDGET_CONTROL,parent, SET_UVALUE= uv

RET = {id:parent, Top:event.top, Handler:0L,Value:val}
return, ret

END

FUNCTION CW_PLOTSERIE, Parent, PSNAME=Psname, PNGNAME = Pngname, $
	 YPLOT=yplot, XPLOT=xplot, SCALE=scale, PRINT=print,$
	  BACKGROUND= Background, COLOR= Color

COMMON STR_CW_PLOTSERIE, str_plot, Couleurs

COULEURS = INTARR(!D.table_size,3)
STR_PLOT = {STR_PLOT,oplot:0L, plot:0L, xplot:0L, yplot:0L, scale:0L, $
		prnt:0L, Close:0L, fgf:0L, Yra:0L, Yrc:0L, Bck:0L, Clr:0L, $
		 PS: 'file.ps',PNG: 'file.png', vplot:0L}

XP=1
YP=1
PRT=0
SCL=0
IF KEYWORD_SET(xplot) THEN Xp=xplot
IF KEYWORD_SET(yplot) THEN Yp=yplot
IF KEYWORD_SET(Print) THEN Prt=print
IF KEYWORD_SET(Scale) THEN Scl=scale
IF KEYWORD_SET(Psname) THEN Str_plot.Ps=psname
IF KEYWORD_SET(Pngname) THEN Str_plot.Png=Pngname

!P.MULTI[1] = Xp
!P.MULTI[2] = Yp

BASE=WIDGET_BASE(Parent, Column= 2, /align_center,/Frame)

	str_plot.xplot= CW_FIELD(base, UVALUE= 'XPLOT', VALUE=Xp, $
		/integer, title='Plots on X',/Return_event,Xsize=3)
        str_plot.yra= WIDGET_BUTTON(base,Value= 'YRANGE auto',$
				UVALUE = 'YRGA')
	str_plot.prnt=CW_BGROUP(base,'OPEN Print File', UVALUE = 'PRINT', $
		/NONEXCLUSIVE, /Return_name, SET_VALUE=  string(prt))
        str_plot.close= WIDGET_BUTTON(base,Value= 'CLOSE Print File', $
				UVALUE = 'CLOSE')
        str_plot.plot= WIDGET_BUTTON(base,Value= 'PLOT', UVALUE= 'PLOT')
		List=['Black','White']
IF KEYWORD_SET (BACKGROUND) THEN $
	str_plot.Bck= WIDGET_DROPLIST(Base, VALUE=List, UVALUE='BCK', $
		TITLE='BACKGROUND')  


	str_plot.yplot= CW_FIELD(base, UVALUE= 'YPLOT', VALUE=Yp, $
		/integer, title='Plots on Y',/Return_event,Xsize=3)
        str_plot.yrc= WIDGET_BUTTON(base,Value= 'YRANGE Cursor', $
				UVALUE = 'YRGC')
	LIST=['LOG scale','LIN scale']
	str_plot.scale=WIDGET_DROPLIST(base,UVALUE= 'SCALE', VALUE=list)
		WIDGET_CONTROL,str_plot.scale,SET_DROPLIST_SELECT=scl
        str_plot.fgf= WIDGET_BUTTON(base,Value= 'SAVE on PNG', $
				UVALUE = 'PNG')
        str_plot.oplot= WIDGET_BUTTON(base,Value= 'OPLOT', UVALUE = 'OPLOT')
		List=['No','Yes']
IF KEYWORD_SET (COLOR) THEN $
	str_plot.Clr= WIDGET_DROPLIST(Base, VALUE=List, UVALUE='CLR', $
		TITLE='COLOR')
 
WIDGET_CONTROL, base, EVENT_FUNC='CW_PLOTSERIE_EVENT'
WIDGET_CONTROL,WIDGET_INFO(base,/Child)

RETURN , Base

END
