;+
;handle event
;+
PRO sesPrint::HandleEvent, event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	UName = Widget_Info(event.id, /UName)
	Class = (oSystem.CurrentPro)
	IF Tag_Names(event, /Structure_Name) EQ 'WIDGET_KILL_REQUEST' THEN BEGIN
		Widget_Control, event.top, /Destroy
		Obj_Destroy, self.printContainer
		Return
	ENDIF
	CASE UName OF
		'printSet':BEGIN
			result = Dialog_Printersetup(self.myPrinter)
		END
		'confirm':BEGIN
			self.pView->SetProperty,Dimensions=[0.,0.]
			self.pView->SetProperty,Location = [0.,0.]

			windowSize =[self.viewX,self.viewY]
			self.pWindow->GetProperty, Resolution = screenResolution
			windowSizeCM = windowSize*screenResolution

			self.myPrinter->GetProperty, DIMENSIONS = pageSize, $
			   RESOLUTION = pageResolution
			pageSizeCM = pageSize*pageResolution
			pageScale = windowSizeCM/pageSizeCM
			self.pModel->Scale,pageScale[0], pageScale[1], 1.
			centering = ((pageSizeCM - windowSizeCM)/2.) $
			   /pageResolution
			self.pView->SetProperty, LOCATION = centering

		   	self.myPrinter->Draw,self.pView
			self.myPrinter->NewDocument

			self.pModel->Scale,1/pageScale[0],1/pageScale[1], 1
			self.pView->SetProperty, LOCATION = [0,0]

			Widget_Control, event.top, /Destroy
		END
		'cancel':BEGIN
			Widget_Control, event.top, /Destroy
			Obj_Destroy, self.printContainer
		END
	ELSE:
	ENDCASE
END
;+
;create print widget
;+
PRO sesPrint::Create
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	ClassImage = oSystem.CurrentPro

	IF Obj_Valid(oSystem.CurrentPro) THEN BEGIN
		ClassImage.oView->GetProperty, Dimensions = viewDim , $
		                            Location = viewLoc   , $
		                            ViewPlane_Rect = viewPlane
		ClassName = Obj_Class(oSystem.CurrentPro)
		IF ClassName EQ 'SESMULTIIMAGE' THEN BEGIN
			(oSystem.CurrentPro).seleFrame->SetProperty,Hide = 1
			oSystem.oScene->Setproperty,Color = [255,255,255]
		ENDIF
		save_img = SnapScreen(oview=ClassImage.oView, xsize=viewDim[0], $
		                      ysize=viewDim[1], zoom_param= 1)

		IF ClassName EQ 'SESMULTIIMAGE' THEN BEGIN
			(oSystem.CurrentPro).seleFrame->SetProperty,Hide = 0
			(oSystem.CurrentPro).outerFrame->GetProperty,data = data
			range = [min(data[0,*]),max(data[0,*]) , $
			         min(data[1,*]),max(data[1,*])]
			oSystem.oScene->Setproperty,Color = [128,128,128]
			save_img = save_img[*,(range[0]-1):(range[1]+2) , $
			                      (range[2]-1):(range[3]+2)]
		ENDIF
	ENDIF

	xSize = self.baseSize[0]*0.3
	ySize = self.baseSize[1]*0.3
	xOff = (self.baseSize[0]-300)/2
	yOff = (self.baseSize[1]-200)/2
	drawX = xSize*0.8
	drawY = ySize*0.8
	self.printContainer = Obj_New('IDL_Container')

	pBase = Widget_Base(Group_Leader = self.mainBase		, $
		                UName      = 'PrintBase' 	    	, $
		                UValue	   = self					, $
		                Title      = 'Print'		    	, $
		                Space      = 2          			, $
		                XOffSet    = xOff					, $
		                YOffSet    = yOff					, $
		                /Modal								, $
		                /Toolbar							, $
		                /Column                 			, $
		                /Tlb_Kill_Request_Events 			, $
		                /Floating               			  )

	drawBase = Widget_Base(pBase,yPad=10)

	pDraw = Widget_Draw(drawBase		,$
						XOffset=0		,$
						YOffset=0		,$
						XSize=drawX		,$
						YSize=drawY		,$
						Retain=2		,$
						UName='pDraw'	,$
						Frame=2			,$
						Graphics_Level=2 )

	printSetButton = Widget_Button(pBase				 	,$
								   Value='PrintSetup...'	,$
								   XSize=60					,$
								   UName='printSet'	 		 )

	printSetButton = Widget_Button(pBase			,$
								   Value='confirm'	,$
								   XSize=60			,$
								   UName='confirm'	 )

	printSetButton = Widget_Button(pBase			,$
								   Value='cancel'   ,$
								   XSize=60			,$
								   UName='cancel'	 )


	Widget_Control,pBase,/REALIZE

	self.viewX = (Size(save_img , /Dimensions))[1]
	self.viewY = (Size(save_img , /Dimensions))[2]

	self.pScene = Obj_New('IDLgrScene', Color=[128,128,128])
	self.pView = Obj_New('IDLgrView'			,$
					Location=[0,0]	 	,$
					EYE=3			 	,$
					Viewplane_rec=[0.,0.,self.viewX,self.viewY],$
					Color=[255,255,255] )

	self.pScene->Add,self.pView
	IF ClassName EQ 'SESSINGLEIMAGE' THEN BEGIN
		IF self.viewX GT self.viewy THEN BEGIN
			ratio = Float(self.viewY)/self.viewX
			loc = [0.,(drawY-drawX*ratio)/2.]
			self.pView->SetProperty,Dimensions=[drawX,drawX*ratio]
			self.pView->SetProperty,Location = loc
		ENDIF
		IF self.viewX LE self.viewy THEN BEGIN
			ratio = Float(self.viewX)/self.viewY
			loc = [(drawX-drawY*ratio)/2.,0.]
			self.pView->SetProperty,Dimensions=[drawY*ratio,drawY]
			self.pView->SetProperty,Location = loc
		ENDIF
	ENDIF

	self.pModel = Obj_New('IDLgrModel',Name='pModel')

	c = obj_new('IDLgrImage', data=save_img)

	self.pModel->Add,c

	self.pView->Add,self.pModel
	Widget_Control,pDraw,Get_Value=pWindow
	self.pWindow = pWindow
	self.pWindow->SetProperty, Graphics_Tree = self.pScene
	self.pWindow->Draw,self.pScene

	self.myPrinter=Obj_New('IDLgrPrinter',Quality=2,Print_Quality=2)

	self.printContainer->Add,self.pView
	self.printContainer->Add,self.pModel
	self.printContainer->Add,self.myPrinter
	save_img = 0

	XManager, 'SunEle'                              , $
	           pBase                                , $
	           /No_Block                            , $
	           Event_Handler = 'sesFrame_Event'     , $
  			   Cleanup = 'sesFrame_Cleanup'
END
;+
;clean up
;+
PRO sesPrint::CleanUp
	COMPILE_OPT IDL2
	Obj_Destroy,self.printContainer

END
;+
;initialize
;+
FUNCTION sesPrint::Init  , oSystem = oSystem

	COMPILE_OPT IDL2
	self.oSystem = oSystem
	self.mainBase = oSystem.mBase
	self.baseSize =  oSystem.baseSize
	self->Create

	Return,1
END
;+
;SingImage class name
;+
PRO sesPrint__Define
	COMPILE_OPT IDL2
	void = { sesPrint                             , $
	         ;system parameter
	         mainBase           :    0L           , $
			 viewX				:	 0L			  , $
			 viewY				:	 0L			  , $
			 baseSize           :    [0,0]        , $

			 ;system objct

	         printContainer     :    Obj_New()    , $
	         pScene				:	 Obj_New()	  , $
	         pModel				:	 Obj_New()	  , $
	         pView				:	 Obj_New()	  , $
	         pWindow			:	 Obj_New()	  , $
	         myPrinter          :    Obj_New()    , $
	         oSystem            :    Obj_New()      $

	        }
END