;+
;disarrange arr
;+
FUNCTION sesMultiImage::sumArr , cx , cy
	COMPILE_OPT IDL2
	xx = N_Elements(cx)
	yy = N_Elements(cy)
	cc = IntArr(yy)
	tempX = 0
	FOR i = 0 , yy-1 DO BEGIN
		tempX = [tempX,cx]
	ENDFOR
	tempX = tempX[1:N_Elements(tempX)-1]
	;reversal arr : cy
	FOR i = 0 , yy-1 DO BEGIN
		cc[i] = cy[yy-1-i]
	ENDFOR
	tempY = 0
	FOR i = 0 , yy-1 DO BEGIN
		tempY = [tempY,replicate(cc[i],xx)]
	ENDFOR
	tempY = tempY[1:N_Elements(tempY)-1]
	newXY = IntArr(2,xx*yy)
	FOR j = 0 , xx*yy-1 DO BEGIN
		newXY[0:1,j] = [tempX[j],tempY[j]]
	ENDFOR
	Return,newXY
END
;+
;create select frame
;+
PRO sesMultiImage::CreateSeleFrame , rangeCoord = rangeCoord
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	frameXY = [[rangeCoord[0,3],rangeCoord[1,3]-self.singleGridXY[1],2] , $
	           [rangeCoord[0,3]+self.singleGridXY[0],rangeCoord[1,3]-self.singleGridXY[1],2] , $
	           [rangeCoord[0,3]+self.singleGridXY[0],rangeCoord[1,3],2] , $
	           [rangeCoord[0,3],rangeCoord[1,3],2]]

	self.seleFrame = Obj_New('IDLgrPolyGon'          , $
                               Data = frameXY        , $
                               Alpha_Channel = 0.7   , $
;							   Linestyle = 6         , $
                               Thick = 2             , $
                               Style = 1             , $
                               Color = [255,0,0]       $
							 )
	self.imageContainer->Add,self.seleFrame
	self.oModel->Add,self.seleFrame
	oSystem.oWindow -> Draw, oSystem.oScene
END
;+
;plot frame
;+
PRO sesMultiImage::plotFrame , imageClass

	COMPILE_OPT IDL2
	oSystem = self.oSystem
	imageClass->GetProperty,Name = name
	imageClass->GetProperty,UValue = posXY
;	self.singleGridXY
	tempName = strsplit(name, '_', /Extract)
	number = Fix(tempName[1])
	;plot frame X and Y coodinate
    X = [posXY[0,number-1],posXY[0,number-1]+self.singleGridXY[0]]
	Y = [posXY[1,number-1],posXY[1,number-1]+self.singleGridXY[1]]
	frameXY = [[X[0],Y[0],2],[X[1],Y[0],2],[X[1],Y[1],2],[X[0],Y[1],2]]
	self.seleFrame->SetProperty,Data=frameXY
	oSystem.oWindow -> Draw, oSystem.oScene
END
;+
;create widget
;+
PRO sesMultiImage::CreateWidget, rowLine ,rangeCoord = rangeCoord , $
                                 rangeX = rangeX , rangeY = rangeY
	COMPILE_OPT IDL2

	oSystem = self.oSystem

	self.oView = Obj_New('IDLgrView'				, $
						  Color = [128,128,128]	, $
						  Eye = 21				, $
						  ZClip = [20,-1]		)

	oSystem.oScene->Add , self.oView

	self.oModel = Obj_New('IDLgrModel')

	self.oView->Add, self.oModel

	self.imageModel = Obj_New('IDLgrModel')

	self.oModel->Add, self.imageModel

	;draw outer frame
	self.frameModel = Obj_New('IDLgrModel')

	self.outerFrame = Obj_New('IDLgrPolyGon'         , $
                               Data = rangeCoord     , $
                               Thick = 2             , $
                               Style = 1             , $
                               Color = [255,255,255]   $
							 )
	self.imageContainer->Add,self.outerFrame
	self.frameModel->Add,self.outerFrame

	FOR i = 1 , rowLine[1]-1 DO BEGIN
		lineFrame = Obj_New('IDLgrPolyLine'                            , $
                             Data = [rangeX[i,[0,2]],rangeX[i,[1,3]]]  , $
                             Thick = 2                                 , $
                             Color = [255,255,255]                       $
							)
		self.imageContainer->Add,lineFrame
		self.frameModel->Add,lineFrame
	ENDFOR
	IF rowLine[0] GE 2 THEN BEGIN
		FOR i = 1 , rowLine[0]-1 DO BEGIN
			lineFrame = Obj_New('IDLgrPolyLine'                            , $
	                             Data = [rangeY[i,[0,2]],rangeY[i,[1,3]]]  , $
	                             Thick = 2                                 , $
	                             Color = [255,255,255]                       $
								)
			self.imageContainer->Add,lineFrame
			self.frameModel->Add,lineFrame
		ENDFOR
	ENDIF
	self.oModel->Add,self.frameModel

	self.oView->SetProperty, Dimensions = [self.drawSize[0],self.drawSize[1]]

	self.oView->SetProperty, Location = [0,0]

	Widget_Control, (oSystem.mainDraw).drawID      , $
	                Draw_XSize = self.drawSize[0]  , $
	                Draw_YSize = self.drawSize[1]

 	Widget_Control, (oSystem.mainDraw).drawID      , $
 	                Scr_XSize = self.drawSize[0]   , $
 					Scr_YSize = self.drawSize[1]

	self.oView->SetProperty, ViewPlane_Rect = [0,0,self.drawSize]

	oSystem.oWindow -> Draw, oSystem.oScene

END
;+
;according to grid size count image size
;+
PRO sesMultiImage::CountImageSize , gridXY = gridXY  , $
									rangeX = rangeX  , $
		                       		rangeY = rangeY  , $
		                       		number = number  , $
		                       		rowLine = rowLine, $
			                        dimX = dimX      , $
			                        dimY = dimY      , $
			                        locaXY = locaXY  , $
			                        newXY = newXY    , $
			                        dimension = dimension
	COMPILE_OPT IDL2
	cx = rangeX[0:(N_Elements(rangeX[*,0])-2),0]
	IF self.Numbers EQ 2 THEN BEGIN
		cy = rangeX[0,1]
	ENDIF ELSE BEGIN
		cy = rangeY[0:(N_Elements(rangeY[*,1])-2),1]
	ENDELSE

	;newXY : record points coodinate
	newXY = self->sumArr(cx , cy)
	gridScale = Double(gridXY[0])/gridXY[1]
	imageScale = Double(dimX)/dimY
	IF imageScale LT gridScale THEN BEGIN
		dimension = Fix([imageScale*gridXY[1],gridXY[1]])
		locaX = (gridXY[0]-dimension[0])/2+newXY[0,number]+2
		locaY = newXY[1,number]
	ENDIF
	IF imageScale EQ gridScale THEN BEGIN
		dimension = Fix([gridXY[1],gridXY[1]])
		locaX = newXY[0,number]
		locaY = newXY[1,number]
	ENDIF
	IF imageScale GT gridScale THEN BEGIN
		dimension = Fix([gridXY[0],gridXY[0]*1/imageScale])
		locaX = newXY[0,number]
		locaY = (gridXY[1]-dimension[1])/2+newXY[1,number]
	ENDIF
	locaXY = [locaX,locaY]
END
;+
;count Image dimension
;+
PRO sesMultiImage::CountDimension , rangeX = rangeX , rangeY = rangeY , $
                                    rowLine = rowLine, titleName = titleName
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	sep = oSystem.sep
	gridXY = self.singleGridXY
	imageDir = ''
	copyDir = ''
	name = ''
	dimX = ''
	dimY = ''
	imageType = ''
	type = ''
	content = StrArr(6)
	newname = ''
	oSystem.oProcess->mapBase , 1
	FOR i = 0 , self.Numbers-1 DO BEGIN
		filename = self.tempDir+sep+'Data'+String(Strtrim(i+1,2))+ $
		           sep+'text'+String(Strtrim(i+1,2))+'.ini'
		openr,lun,filename,/get_lun
		Readf,lun,imageDir,copyDir,name,dimX,dimY,imageType,type,content,newname
		Free_lun,lun
		image = self.oShmData->ShmLink(imageDir = copyDir 	  , $
	                                  name = name         	  , $
	                                  dimX = dimX             , $
	                                  dimY = dimY             , $
	                                  imageType = imageType   , $
	                                  segName = segName       , $
	                                  type = type               $
	                                 )

		self->CountImageSize , gridXY = gridXY  , $
		                       rangeX = rangeX  , $
		                       rangeY = rangeY  , $
		                       number = i       , $
		                       rowLine = rowLine, $
		                       dimX = Fix(dimX) , $
		                       dimY = Fix(dimY) , $
		                       locaXY = locaXY  , $
		                       newXY = newXY    , $
		                       dimension = dimension

		oImage = Obj_New('IDLgrImage'         						, $
			               Data = Temporary(image)      			, $
			               Name = 'Class_'+String(Strtrim(i+1,2))   , $
			               UValue = newXY                           , $
			               Blend_Function = [3,4]					, $
			               Interleave = 0           				, $
				           dimensions = dimension       			, $
			               Location = [locaXY,10]	      			  )

		self.imageModel->Add,oImage

		font1 = Obj_New('IDLgrFont', 'Helvetica', Size=11)
		fileName = [newname,'#'+Strtrim(String(i+1),2)]
		Location = [[locaXY[0]+15,locaXY[1]+dimension[1]-20] , $
		            [locaXY[0]+15,locaXY[1]+15]]

		Label1  = Obj_New('IDLgrText'                            , $
		                   Alignment = 0                        , $
		                   Color           = [255,255,255]	    , $
		                   Location        = [Location[*,0],3] , $
						   Strings         = fileName[0]        , $
		                   font = font1           )
		font2 = Obj_New('IDLgrFont', 'Helvetica', Size=10,Thick = 6)
		Label2  = Obj_New('IDLgrText'                            , $
		                   Alignment = 0                        , $
		                   Color           = [255,255,0]	    , $
		                   Location        = [Location[*,1],3] , $
						   Strings         = fileName[1]        , $
		                   font = font2           )

		self.oModel->Add,Label1
		self.imageContainer->Add,Label1
		self.oModel->Add,Label2
		self.imageContainer->Add,Label2


		self.imageContainer->Add,oImage
		self.imageContainer->Add,font1
		self.imageContainer->Add,font2
		oSystem.oProcess->DoProcess , (36/self.Numbers)+i+1

		self.OI = i

	ENDFOR
	IF (36/self.Numbers)+self.OI+1 LT 36 THEN BEGIN
		FOR i = (36-((36/self.Numbers)+self.OI)) , 35 DO BEGIN
			oSystem.oProcess->DoProcess , i
		ENDFOR
	ENDIF
	oSystem.oProcess->mapBase , 0
	oSystem.oWindow -> Draw, oSystem.oScene


END
;+
;count frame coodinate
;+
PRO sesMultiImage::CountFrame , rowLine	, rangeCoord = rangeCoord , $
                                rangeX = rangeX , rangeY = rangeY
	COMPILE_OPT IDL2
	self.ImageRange = [865 , 540]
	;record range coodinate (x,y) start left corner
	;self,rangeCoord:[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
	self.rangeCoord = [ $
				  [(self.drawSize[0]-self.ImageRange[0])/2                    , $
				   (self.drawSize[1]-self.ImageRange[1])/2]                   , $
				  [(self.drawSize[0]-self.ImageRange[0])/2+self.ImageRange[0] , $
				   (self.drawSize[1]-self.ImageRange[1])/2]                   , $
				  [(self.drawSize[0]-self.ImageRange[0])/2+self.ImageRange[0] , $
				   (self.drawSize[1]-self.ImageRange[1])/2+self.ImageRange[1]], $
				  [(self.drawSize[0]-self.ImageRange[0])/2                    , $
				   (self.drawSize[1]-self.ImageRange[1])/2+self.ImageRange[1]]  $
				   ]

	rangeCoord = self.rangeCoord
	;averageX : record cut image coodinate : x
	;|---------------|--------------|--------------|  X
	;1               2              3	           4
	;record  coodinate of 1,2,3,4
	averageX = Round((rangeCoord[0,1]-rangeCoord[0,0])/rowLine[1])
	rowX = IntArr(rowLine[1]+1)
	rowY = IntArr(rowLine[1]+1)
	rowY[*] = rangeCoord[1,1]
	rowX[0] = rangeCoord[0,0]
	rowX[rowLine[1]] = rangeCoord[0,1]
	FOR i = 1 , rowLine[1]-1 DO BEGIN
		rowX[i] = rangeCoord[0,0] + i*averageX
	ENDFOR
	;averageY : record cut image coodinate : y
	;|---------------|--------------|--------------|  Y
	;1               2              3	           4
	;record  coodinate of 1,2,3,4
	IF rowLine[0] GE 2 THEN BEGIN
		averageY = Round((rangeCoord[1,2]-rangeCoord[1,1])/rowLine[0])
		lineY = IntArr(rowLine[0]+1)
		lineX = IntArr(rowLine[0]+1)
		lineX[*] = rangeCoord[0,0]
		lineY[0] = rangeCoord[1,1]
		lineY[rowLine[0]] = rangeCoord[1,2]
		FOR i = 1 , rowLine[0]-1 DO BEGIN
			lineY[i] = rangeCoord[1,1] + i*averageY
		ENDFOR
	ENDIF ELSE BEGIN
		lineY = IntArr(rowLine[0]+1)
		lineY[0] = rangeCoord[1,1]
		lineY[rowLine[0]] = rangeCoord[1,2]
	ENDELSE
	;count frame coodinate
	rangeX = [[rowX],[rowY],[rowX],[rowY+self.ImageRange[1]]]
	IF rowLine[0] GE 2 THEN BEGIN
		rangeY = [[lineX],[lineY],[lineX+self.ImageRange[0]],[lineY]]
	ENDIF
	;singleGridXY : single grid length and width
	self.singleGridXY = [(rowX[1]-rowX[0]),(lineY[1]-lineY[0])]

END
;+
;create multi draw widget
;+
PRO sesMultiImage::Create
	COMPILE_OPT IDL2
	self.imageContainer = Obj_New('IDL_Container')
	temp = Where(self.Numbers EQ self.lineRowArr[0,*])
	;lineRow : this arr record numbers of row and line
	rowLine = Float(self.lineRowArr[1:2,temp])
	self->CountFrame , rowLine , rangeCoord = rangeCoord , $
                                 rangeX = rangeX , rangeY = rangeY
    ;create widget
	self->CreateWidget , rowLine , rangeCoord = rangeCoord , $
	                              rangeX = rangeX , rangeY = rangeY

	self->CountDimension , rangeX = rangeX , rangeY = rangeY , rowLine = rowLine

	self->CreateSeleFrame , rangeCoord = rangeCoord

END
;+
;clean up
;+
PRO sesMultiImage::CleanUp
	COMPILE_OPT IDL2
	Obj_Destroy,self.imageContainer

END
;+
;initialize
;+
FUNCTION sesMultiImage::Init  , oSystem = oSystem  , $
                                Numbers = Numbers  , $
                                tempDir = tempDir


	COMPILE_OPT IDL2
	self.oSystem = oSystem
	self.Numbers = Numbers
	self.tempDir = tempDir
	self.lineRowArr = oSystem.lineRowArr
	self.oShmData = oSystem.oShmData
	self.drawSize = (oSystem.mainDraw).drawSize
	self->Create
	Return,1
END
;+
;SingImage class name
;+
PRO sesMultiImage__Define
	COMPILE_OPT IDL2
	void = { sesMultiImage                        , $
			 ;system parameter
			 Numbers            :    0            , $
			 lineRowArr         :   IntArr(3,29)  , $
			 ImageRange         :   IntArr(2)     , $ ; record view range to show image
			 drawSize           :   [0,0]         , $
			 rangeCoord         :   IntArr(2,4)   , $
			 singleGridXY       :   [0,0]         , $
			 tempDir            :   ''            , $
			 OI                 :   0             , $
			 ;system objct
			 oView              :    Obj_New()    , $
			 oModel             :    Obj_New()    , $
			 imageModel         :    Obj_New()    , $
			 frameModel         :    Obj_New()    , $
			 outerFrame         :    Obj_New()    , $
			 seleFrame          :    Obj_New()    , $
			 oShmData           :    Obj_New()    , $
			 imageContainer     :    Obj_New()    , $
	         oSystem            :    Obj_New()      $

	        }

END