;+
;draw
;+
PRO sesCutout::DrawWidget
	COMPILE_OPT IDL2
	oSystem = self.oSystem

;	oView = (oSystem.CurrentPro).oView
;	(oSystem.CurrentPro).imageModel->SetProperty , Hide = 1
;	oView->SetProperty, Transparent=1
;	oSystem.oWindow -> Draw,oSystem.oScene,/Draw_Instance
;	(oSystem.CurrentPro).imageModel->SetProperty , Hide = 0
;	oView->SetProperty, Transparent=0

	oSystem.oWindow -> Draw,oSystem.oScene
END
;+
;set view
;+
PRO sesCutout::SetView , dimension
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	drawSize = [(oSystem.mainDraw).drawSize,dimension[1], dimension[2]]
	zoom = (oSystem.CurrentPro).zoom
	Type = oSystem.zoomState

	sesViewSize, drawSize, Type = type				 	       , $
						 	ZoomRate = zoom			 	       , $
						 	windowVD = windowVD		           , $
							viewPlane = viewPlane	           , $
							viewDim = viewDim		           , $
							currentXY=currentXY                , $
							oDraw = (oSystem.mainDraw).drawID  , $
							oView = (oSystem.CurrentPro).oView , $
							viewLoc = viewLoc

	IF KeyWord_Set(viewDim) THEN (oSystem.CurrentPro).oView->SetProperty, Dimensions = viewDim
	IF KeyWord_Set(viewLoc) THEN (oSystem.CurrentPro).oView->SetProperty, Location = viewLoc
	IF KeyWord_Set(windowVD) THEN Widget_Control, (oSystem.mainDraw).drawID	, $
           Draw_XSize = windowVD[0], Draw_YSize = windowVD[1]

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

	(oSystem.CurrentPro).oModel->Reset
	(oSystem.CurrentPro).oModel->Scale, zoom, zoom, 1

	(oSystem.CurrentPro).oView->SetProperty, ViewPlane_Rect = viewPlane

	oSystem.oWindow -> Draw, oSystem.oScene

END
;+
;plot frame
;+
PRO sesCutout::PlotFrame , eventX , eventY , type
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	oView = (oSystem.CurrentPro).oView
	zoom = (oSystem.CurrentPro).zoom
	oView->GetProperty, Location = viewLoc
	eventX = (eventX - viewLoc[0])/zoom
	eventY = (eventY - viewLoc[1])/zoom

	CASE type OF
		0:BEGIN
			self.eventXY = [eventX,eventY]
		END
		1:BEGIN
			data = [[self.eventXY[0],eventY] , $
			        [eventX,eventY]          , $
			        [eventX,self.eventXY[1]] , $
			        [self.eventXY[0],self.eventXY[1]]]

			ox = [min(data[0,*]),max(data[0,*])]
			oy = [min(data[1,*]),max(data[1,*])]
			oData = [[ox[0]-1,oy[0]-1],[ox[1]+1,oy[0]-1] , $
			         [ox[1]+1,oy[1]+1],[ox[0]-1,oy[1]+1]]
			self.cutFrame->SetProperty,data=data
   			self.outFrame->SetProperty,data=oData
			self->DrawWidget
			Widget_Control,self.cancelBu,Sensitive = 0
			Widget_Control,self.okBu,Sensitive = 1
			Widget_Control,self.sureBu,Sensitive = 1
			Widget_Control, self.l1, Set_Value = Strtrim(String(fix(ox[0])),2)
			Widget_Control, self.l2, Set_Value = Strtrim(String(fix(ox[1])),2)
			Widget_Control, self.w1, Set_Value = Strtrim(String(fix(oy[0])),2)
			Widget_Control, self.w2, Set_Value = Strtrim(String(fix(oy[1])),2)
		END
	ELSE:
	ENDCASE
END
;+
;according as text value chang frame
;+
PRO sesCutout::ChangeFrame
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	textValue = self.textValue
	zoom = (oSystem.CurrentPro).zoom
	IF textValue[0] GE textValue[1] OR textValue[2] GE textValue[3] THEN BEGIN
		status = Dialog_Message('please input right value', Title = 'warn')
		Return
	ENDIF ELSE BEGIN
		data = [[textValue[0],textValue[2]] , $
		        [textValue[1],textValue[2]] , $
		        [textValue[1],textValue[3]] , $
		        [textValue[0],textValue[3]]]/zoom
		ox = [min(data[0,*]),max(data[0,*])]
		oy = [min(data[1,*]),max(data[1,*])]
		oData = [[ox[0]-1,oy[0]-1],[ox[1]+1,oy[0]-1] , $
		         [ox[1]+1,oy[1]+1],[ox[0]-1,oy[1]+1]]
		self.cutFrame->SetProperty,data=data
		self.outFrame->SetProperty,data=oData
		self->DrawWidget
		Widget_Control,self.cancelBu,Sensitive = 0
		Widget_Control,self.okBu,Sensitive = 1
		Widget_Control,self.sureBu,Sensitive = 1
		Widget_Control, self.l1, Set_Value = Strtrim(String(fix(ox[0])),2)
		Widget_Control, self.l2, Set_Value = Strtrim(String(fix(ox[1])),2)
		Widget_Control, self.w1, Set_Value = Strtrim(String(fix(oy[0])),2)
		Widget_Control, self.w2, Set_Value = Strtrim(String(fix(oy[1])),2)
	ENDELSE

END
;+
;handle event
;+
PRO sesCutout::HandleEvent, event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	UName = Widget_Info(event.id, /UName)
	tagName = Tag_Names(event, /Structure_Name)
	zoom = (oSystem.CurrentPro).zoom
	IF tagName EQ 'WIDGET_TEXT_CH' OR tagName EQ 'WIDGET_TEXT_DEL' THEN Return
	oView = (oSystem.CurrentPro).oView
	oView->GetProperty, Dimensions = viewDim
	IF Tag_Names(event, /Structure_Name) EQ 'WIDGET_KILL_REQUEST' THEN BEGIN
		Widget_Control, event.top, /Destroy
		Obj_Destroy, self.CutContainer
        oSystem.oWindow -> Draw,oSystem.oScene
        menuBar = (oSystem.menuBar)
        menuBar.CurrentName = ''
        Obj_Destroy,(oSystem.menuBar).CutImage
		Return
	ENDIF

	CASE UName OF
		'length1':BEGIN
			IF event.enter EQ 0 THEN BEGIN
				Widget_Control, event.id, Get_Value = dim
				dim = 0 >(Fix(viewDim[0])<Fix(dim))
				Widget_Control, event.id, Set_Value = Strtrim(String(dim),2)
			ENDIF
		END
		'length2':BEGIN
			IF event.enter EQ 0 THEN BEGIN
				Widget_Control, event.id, Get_Value = dim
				dim = 0 >(Fix(viewDim[0])<Fix(dim))
				Widget_Control, event.id, Set_Value = Strtrim(String(dim),2)
			ENDIF
		END
		'width1':BEGIN
			IF event.enter EQ 0 THEN BEGIN
				Widget_Control, event.id, Get_Value = dim
				dim = 0 >(Fix(viewDim[1])<Fix(dim))
				Widget_Control, event.id, Set_Value = Strtrim(String(dim),2)
			ENDIF
		END
		'width2':BEGIN
			IF event.enter EQ 0 THEN BEGIN
				Widget_Control, event.id, Get_Value = dim
				dim = 0 >(Fix(viewDim[1])<Fix(dim))
				Widget_Control, event.id, Set_Value = Strtrim(String(dim),2)
			ENDIF
		END
		'plot':BEGIN
			Widget_Control, self.l1, Get_Value = length1
			Widget_Control, self.l2, Get_Value = length2
			Widget_Control, self.w1, Get_Value = width1
			Widget_Control, self.w2, Get_Value = width2
			self.textValue = Fix([length1,length2,width1,width2])
			self->ChangeFrame
		END
		'OK':BEGIN
			IF tagName EQ 'WIDGET_DRAW' THEN BEGIN
				IF event.press EQ 1 THEN BEGIN
					ImageClass = oSystem.CurrentPro
					WSet, self.drawOK
					TV, self.ok1, /True
					oView = ImageClass.oView
					oView->GetProperty, Dimensions = dim
					ImageClass.oImage->GetProperty , data = image
					Ptr_Free,self.backImage
					self.backImage = Ptr_New(temporary(image))
					self.outFrame->GetProperty,data=data
					ox = [min(data[0,*]),max(data[0,*])]
					oy = [min(data[1,*]),max(data[1,*])]
					ox = (0>ox)<(dim[0]-1)
					oy = (0>oy)<(dim[1]-1)
					image = ((*self.backImage))[ox[0]:ox[1],oy[0]:oy[1]]
					dimension = Size(image)
					ImageClass.imageX = dimension[1]
					ImageClass.imageY = dimension[2]
					(oSystem.CurrentPro).oImage->SetProperty , data = image
					self.cutFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
					self.outFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
					Widget_Control,self.labelX,Set_Value =Strtrim(String(dimension[1]),2)
					Widget_Control,self.labelY,Set_Value =Strtrim(String(dimension[2]),2)
					self->SetView , dimension
					WSet, self.drawOK
					TV, self.ok0, /True
					Widget_Control,self.cancelBu,Sensitive = 1
					Widget_Control,self.okBu,Sensitive = 0
					Widget_Control,self.sureBu,Sensitive = 0
					(oSystem.menuBar).Title->Create
					Widget_Control, self.l1, Set_Value = '0'
					Widget_Control, self.l2, Set_Value = '0'
					Widget_Control, self.w1, Set_Value = '0'
					Widget_Control, self.w2, Set_Value = '0'
				ENDIF
			ENDIF
		END
		'CANCEL':BEGIN
			IF tagName EQ 'WIDGET_DRAW' THEN BEGIN
				IF event.press EQ 1 THEN BEGIN
					ImageClass = oSystem.CurrentPro
					WSet, self.drawCan
					TV, self.can1, /True
					dimension = Size((*self.backImage))
					ImageClass.imageX = dimension[1]
					ImageClass.imageY = dimension[2]
					(oSystem.CurrentPro).oImage->SetProperty , data = (*self.backImage)
					self.cutFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
					self.outFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
					Widget_Control,self.labelX,Set_Value =Strtrim(String(dimension[1]),2)
					Widget_Control,self.labelY,Set_Value =Strtrim(String(dimension[2]),2)
					self->SetView , dimension
					WSet, self.drawCan
					TV, self.can0, /True
					(oSystem.menuBar).Title->Create
				ENDIF
			ENDIF
		END
		'Confirm':BEGIN
			ImageClass = oSystem.CurrentPro
			WSet, self.drawOK
			TV, self.ok1, /True
			oView = ImageClass.oView
			oView->GetProperty, Dimensions = dim
			ImageClass.oImage->GetProperty , data = image
			Ptr_Free,self.backImage
			self.backImage = Ptr_New(temporary(image))
			self.outFrame->GetProperty,data=data
			ox = [min(data[0,*]),max(data[0,*])]
			oy = [min(data[1,*]),max(data[1,*])]
			ox = (0>ox)<(dim[0]-1)
			oy = (0>oy)<(dim[1]-1)
			image = ((*self.backImage))[ox[0]:ox[1],oy[0]:oy[1]]
			dimension = Size(image)
			ImageClass.imageX = dimension[1]
			ImageClass.imageY = dimension[2]
			(oSystem.CurrentPro).oImage->SetProperty , data = image
			self.cutFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
			self.outFrame->SetProperty,data = [[0,0],[0,0],[0,0],[0,0]]
			Widget_Control,self.labelX,Set_Value =Strtrim(String(dimension[1]),2)
			Widget_Control,self.labelY,Set_Value =Strtrim(String(dimension[2]),2)
			self->SetView , dimension
			WSet, self.drawOK
			TV, self.ok0, /True

			Widget_Control, event.top, /Destroy
			Obj_Destroy, self.CutContainer
	        oSystem.oWindow -> Draw,oSystem.oScene
	        menuBar = (oSystem.menuBar)
	        menuBar.CurrentName = ''
	        Obj_Destroy,(oSystem.menuBar).CutImage
	        (oSystem.menuBar).Title->Create
		END
	ELSE:
	ENDCASE

END
;+
;create frame
;+
PRO sesCutout::CreateFrame
	COMPILE_OPT IDL2
	oSystem = self.oSystem

	self.cutFrame = Obj_New('IDLgrPolyGon'       , $
                           Alpha_Channel = 0.3   , $
                           Thick = 1             , $
                           Style = 2             , $
                           Linestyle = 0         , $
                           Color = [133,162,205]   $
							 )

	self.outFrame = Obj_New('IDLgrPolyGon'       , $
                           Alpha_Channel = 1   , $
                           Thick = 1             , $
                           Style = 1             , $
                           Linestyle = 0         , $
                           Color = [243,194,70]   $
							 )
	(oSystem.CurrentPro).oModel->Add,self.cutFrame
	(oSystem.CurrentPro).oModel->Add,self.outFrame
	self.CutContainer->Add,[self.cutFrame,self.outFrame]

END
;+
;create widget
;+
PRO sesCutout::Create
	COMPILE_OPT IDL2
	Device, Get_Screen_Size = screenSize
	oSystem = self.oSystem
	sep = oSystem.sep
	oView = (oSystem.CurrentPro).oView
	oView->GetProperty, Dimensions = viewDim
	xOff = 4
	yOff = screenSize[1]-(oSystem.baseSize)[1]-2
	self.CutContainer = Obj_New('IDL_Container')

	ok0 = Read_Image(self.RootDir + sep+'resource'+sep+'bitmap'+sep+'cut'+sep+'cut_0_1.bmp')
	ok1 = Read_Image(self.RootDir + sep+'resource'+sep+'bitmap'+sep+'cut'+sep+'cut_0_2.bmp')
	can0 = Read_Image(self.RootDir + sep+'resource'+sep+'bitmap'+sep+'cut'+sep+'cut_1_1.bmp')
	can1 = Read_Image(self.RootDir + sep+'resource'+sep+'bitmap'+sep+'cut'+sep+'cut_1_2.bmp')

	self.ok0 = ok0
	self.can0 = can0
	self.ok1 = ok1
	self.can1 = can1
;	oSystem.oWindow->Draw,oSystem.oScene, /Create_Instance
	cBase = Widget_Base(Group_Leader = self.mainBase		, $
		                UName      = 'CutImage' 	        , $
		                UValue	   = self					, $
		                Title      = 'cut out image'		, $
		                Space      = 2          			, $
		                XOffSet    = xOff					, $
		                YOffSet    = yOff					, $
		                Map        = 0                      , $
		                /ToolBar 							, $
		                /Column		               			, $
		                /Floating               			, $
		                /Tlb_Kill_Request_Events 			)

	self.cBase = cBase

	labelBaseX = Widget_Base(cBase                          , $
							/Row                            , $
                            space = 5                         $
	                       )

	label = Widget_Label(labelBaseX                          , $
						  Value = 'image length   :  '	 	   $
						 )

	self.labelX = Widget_Label(labelBaseX                    , $
							   /Dynamic_Resize               , $
						  Value = Strtrim(String(fix(viewDim[0])),2)    $
						 )

	labelBaseY = Widget_Base(cBase                           , $
							/Row                             , $
                            space = 5                          $
	                       )

	label = Widget_Label(labelBaseY                          , $
						  Value = 'image width    :  '	 	   $
						 )

	self.labelY = Widget_Label(labelBaseY                     , $
								/Dynamic_Resize               , $
						  Value = Strtrim(String(fix(viewDim[1])),2)    $
						 )

	textBase = Widget_Base(cBase                           , $
						   /Row                             , $
                           space = 5                          $
	                       )
	label = Widget_Label(textBase                          , $
						  Value = 'length :'    $
						 )

	self.l1 = Widget_Text(textBase      		, $
                        /EditAble           	, $
                        /No_NewLine         	, $
                        /Kbrd_Focus_Events		, $
                        UName  = 'length1'	    , $
                        Value = '0'             , $
                        Scr_XSize = 30       	)

	label = Widget_Label(textBase       , $
						  Value = ':'    $
						 )

	self.l2 = Widget_Text(textBase      		, $
                        /EditAble           	, $
                        /No_NewLine         	, $
                        /Kbrd_Focus_Events		, $
                        UName  = 'length2'	    , $
                        Value = '0'             , $
                        Scr_XSize = 30       	)

	textBase = Widget_Base(cBase                           , $
						   /Row                             , $
                           space = 5                          $
	                       )
	label = Widget_Label(textBase                          , $
						  Value = 'width  :'    $
						 )

	self.w1 = Widget_Text(textBase      		    , $
                        /EditAble           	, $
                        /No_NewLine         	, $
                        /Kbrd_Focus_Events		, $
                        UName  = 'width1'	    , $
                        Value = '0'             , $
                        Scr_XSize = 30       	)

	label = Widget_Label(textBase       , $
						  Value = ':'    $
						 )

	self.w2 = Widget_Text(textBase      		    , $
                        /EditAble           	, $
                        /No_NewLine         	, $
                        /Kbrd_Focus_Events		, $
                        UName  = 'width2'	    , $
                        Value = '0'             , $
                        Scr_XSize = 30       	)

	drawBase = Widget_Base(cBase                , $
						   space  = 5          , $
						   /Align_Right         , $
                           /Row                   $
                          )

	self.cancelBu = Widget_Draw(drawBase		    , $
							Retain = 2				, $
							UName = 'CANCEL'		, $
							/Button_Events      	, $
							/Tracking_Events    	, $
							Scr_XSize = 20			, $
							Scr_YSize = 20			)

	self.okBu = Widget_Draw(drawBase		    , $
						Retain = 2				, $
						UName = 'OK'		    , $
						/Button_Events      	, $
						/Tracking_Events    	, $
						Scr_XSize = 20			, $
						Scr_YSize = 20			)

	self.plotBu = Widget_Button(drawBase            , $
								UName = 'plot'      , $
	                            Value = 'Plot'        $
	                          )

	self.sureBu = Widget_Button(drawBase            , $
								UName = 'Confirm'   , $
	                            Value = 'OK'     $
	                            )

	Widget_Control, cBase, /realize
	Widget_Control,self.cancelBu,Sensitive = 0
	Widget_Control,self.okBu,Sensitive = 0
	Widget_Control,self.sureBu,Sensitive = 0
	Widget_Control, self.cancelBu, Get_Value = drawCan
	Widget_Control, self.okBu, Get_Value = drawOK

	WSet, drawOK
	TV, ok0 , /True

	WSet, drawCan
	TV, can0, /True

	Widget_Control,cBase,map = 1
	self->CreateFrame

	self.drawCan = drawCan
	self.drawOK = drawOK

	XManager, 'SunEle'                              , $
	           cBase                                , $
	           /No_Block                            , $
	           Event_Handler = 'sesFrame_Event'     , $
  			   Cleanup = 'sesFrame_Cleanup'

END
;+
;close flow
;+
PRO sesCutout::CloseFlow
	COMPILE_OPT IDL2
	Obj_Destroy,self.CutContainer
	Obj_Destroy,self.cutFrame
	Ptr_Free,self.backImage
END
;+
;clean up
;+
PRO sesCutout::CleanUp
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	Obj_Destroy,self.CutContainer
	Obj_Destroy,self.cutFrame
	Obj_Destroy, self.copyImage
	Ptr_Free,self.backImage
	Obj_Destroy,(oSystem.menuBar).CutImage
END
;+
;initialize
;+
FUNCTION sesCutout::Init  , oSystem = oSystem

	COMPILE_OPT IDL2
	self.oSystem = oSystem
	Widget_Control,(oSystem.mainDraw).drawID,draw_motion=1
	self.mainBase = oSystem.mBase
	self.baseSize =  oSystem.baseSize
	self.RootDir = oSystem.RootDir
	(oSystem.CurrentPro).oImage->GetProperty , Data = Image
	self.MinMaxValue = [Min(Image),Max(Image)]
	self.imageX = (oSystem.CurrentPro).imageX
	self.imageY = (oSystem.CurrentPro).imageY
	self->Create
	oSystem.zoomState = 'initSize'
	self->SetView , [0,self.imageX,self.imageY]
	Class = (oSystem.CurrentPro)
	Class.zoom = 1
	Return,1
END
;+
;SingImage class name
;+
PRO sesCutout__Define
	COMPILE_OPT IDL2
	void = { sesCutout                                , $
	         ;system parameter
	         mainBase           :    0L               , $
			 imageX             :    0                , $
			 imageY             :    0                , $
			 cBase              :    0L               , $
			 wdrawX             :    0L               , $
			 wdrawY             :    0L               , $
			 drawCan            :    0L               , $
	         drawOK             :    0L               , $
	         sureBu             :    0L               , $
	         labelX             :    0L               , $
	         labelY             :    0L               , $
	         cancelBu           :    0L               , $
	         okBu               :    0L               , $
	         plotBu             :    0L               , $
	         l1                 :    0L               , $
	         l2                 :    0L               , $
	         w1                 :    0L               , $
	         w2                 :    0L               , $
	         mouseType          :    0                , $
			 baseSize           :    [0,0]        	  , $
			 MinMaxValue        :    [0,0]            , $
			 ok0                :    BytArr(3,20,20)  , $
			 can0               :    BytArr(3,20,20)  , $
			 ok1                :    BytArr(3,20,20)  , $
			 can1               :    BytArr(3,20,20)  , $
			 RootDir            :    ''               , $
			 eventXY            :    [0,0]            , $
			 textValue          :    IntArr(4)        , $
			 ;system objct
	         CutContainer       :    Obj_New()        , $
	         cutFrame           :    Obj_New()        , $
	         outFrame           :    Obj_New()        , $
	         backImage          :    Ptr_New()        , $
	         copyModel          :    Obj_New()        , $
	         copyImage          :    Obj_New()        , $
	         obuffer            :    Obj_New()        , $
	         oSystem            :    Obj_New()          $

	        }
END