;+
;show information
;+
PRO sesMainDraw::ShowInfo , eventX , eventY
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	className = Obj_Class(oSystem.CurrentPro)
	nameArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
	temp = Where(className EQ nameArr , count)
	IF count NE 0 THEN BEGIN
		currentPrj = oSystem.CurrentPro
		currentPrj.oView->GetProperty, Viewplane_Rect = viewPlane, Location = viewLoc

		viewPar = [viewPlane, viewLoc]

		nPos = (viewPar[0:1]+[eventX,eventY]-viewPar[4:5])/currentPrj.zoom

		IF (nPos[0] LT -1) OR (nPos[0] GT currentPrj.imageX+2) THEN Return
		IF (nPos[1] LT -1) OR (nPos[1] GT currentPrj.imageY+2) THEN Return

		pos = [currentPrj.imageX, currentPrj.imageY]<nPos>0

		IF Max(pos) LE 5 THEN Return

		sPos = Strtrim(String(Fix(pos)),2)

		oStatus = oSystem.oStatus

		Widget_Control, oStatus.statusID[1], Set_Value = sPos[0]
		Widget_Control, oStatus.statusID[3], Set_Value = sPos[1]
	ENDIF

END
;+
;pan event
;+
PRO sesMainDraw::Pan , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.CurrentPro) THEN BEGIN
		currentPrj = oSystem.CurrentPro
		currentPrj.oView->GetProperty,  ViewPlane_Rect=viewPlane  , $
		                                Dimensions = viewDim      , $
		                                Location = loc

		IF viewDim[0] LE (oSystem.mainDraw).drawSize[0] AND $
		       viewDim[1] LE (oSystem.mainDraw).drawSize[1] THEN Return

		CASE event.type OF
			0 : BEGIN
				self.panStatus[0:3] = [1,0,event.x,event.y]
			END
			1 : BEGIN
			  	self.panStatus[0] = 0
			END
			2 : BEGIN
			  	IF self.panStatus[0] THEN BEGIN
			  		Class = (oSystem.toolBar).MouseTool
					distance = [self.panStatus[2]-event.x, self.panStatus[3]-event.y]
					locXY = loc-distance
					locXY[0] = (locXY[0]<0)>((oSystem.mainDraw).drawSize[0]-viewDim[0])
					locXY[1] = (locXY[1]<0)>((oSystem.mainDraw).drawSize[1]-viewDim[1])
					currentPrj.oView->SetProperty, Location = locXY
					oSystem.oWindow->Draw, oSystem.oScene
					self.panStatus[2:3] = [event.x,event.y]
					Widget_Control, event.id, Set_Draw_View = distance-loc
			  	ENDIF
			END
		ELSE:
		ENDCASE
	ENDIF
END
;+
;Zoom event
;+
PRO sesMainDraw::Zoom , event , type
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.CurrentPro) THEN BEGIN
		currentPrj = oSystem.CurrentPro
		CASE event.press OF
			1 : BEGIN
				CASE type OF
					1:BEGIN
						n = 1.1
						IF currentPrj.zoom*n GT 16. THEN Return
						currentPrj.zoom = currentPrj.zoom*n
						currentXY = [event.x,event.y]
						Class = (oSystem.toolBar).MouseTool
						Class->CreateView, currentXY = currentXY , zoom = currentPrj.zoom
					END
					2:BEGIN
						n = 1.1
						IF currentPrj.zoom/n LT .0002 THEN Return
						currentPrj.zoom = currentPrj.zoom/n
						currentXY = [event.x,event.y]
						Class = (oSystem.toolBar).MouseTool
						Class->CreateView, currentXY = currentXY , zoom = currentPrj.zoom
					END
					3:BEGIN

					END
				ELSE:
				ENDCASE
			END
			4 : BEGIN

			END
		ELSE :
		ENDCASE
	ENDIF
END
;+
;LQU event
;+
PRO sesMainDraw::LQU , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = (oSystem.CurrentPro).oView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
					IF Obj_Valid(item[0]) THEN BEGIN
						(oSystem.menuBar).ShowLQU->ShowValue , event.x, event.y
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0

				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = (oSystem.CurrentPro).oView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
						IF Obj_Valid(item[0]) THEN BEGIN
							(oSystem.menuBar).ShowLQU->ShowValue , event.x, event.y
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF
END
;+
;Magnifier event
;+
PRO sesMainDraw::Magnifier , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = (oSystem.CurrentPro).oView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
					IF Obj_Valid(item[0]) THEN BEGIN
						(oSystem.toolBar).Magnifier->ShowImage , event.x, event.y
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0
					(oSystem.toolBar).Magnifier->HideWidget
				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = (oSystem.CurrentPro).oView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
						IF Obj_Valid(item[0]) THEN BEGIN
							(oSystem.toolBar).Magnifier->ShowImage , event.x, event.y
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF

END
;+
;label event
;+
PRO sesMainDraw::Label , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	;
	;
	;
	Catch, theError
   	IF theError NE 0 THEN BEGIN
      Catch, /Cancel
      ok = Error_Message('Warning! select current point!')
      RETURN
   	ENDIF
	;
	;
	;
	IF oSystem.opt_state EQ 1 THEN BEGIN
        ; Handle events (selection, etc.) ...
		IF event.type NE 0 THEN BEGIN
;			widget_control,(oSystem.mainDraw).drawID,draw_motion_events=0
		ENDIF
        CASE event.type OF
            0 : BEGIN ; Button press
                CASE event.press OF

                	1: BEGIN ; Left button press
                		self.mouseType = 1
                     	oSystem.lable->leftbuttdown, event
                    END
                    4: BEGIN ; Right button press
                    	self.mouseType = 1
                     	oSystem.lable->rightbuttdown, event
                    END

                ELSE:
                ENDCASE
            END
            1: BEGIN ; Button release.
            	self.mouseType = 0
            	oSystem.lable->brelease,event
            END
			2: BEGIN ; Button motion.
				;IF self.mouseType EQ 1 THEN BEGIN
            		oSystem.lable->modelmove, event
            ;	endif
            END
        ELSE:
        ENDCASE
        ;
        ;KeyBoard events
        IF event.type NE 0 THEN BEGIN
        	;oSystem.lable->keyevent, event
        ENDIF
	ENDIF
END
;+
;colorbar event
;+
PRO sesMainDraw::Colorbar , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = ((oSystem.menuBar).Colorbar).colorView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
					IF Obj_Valid(item[0]) THEN BEGIN
						(oSystem.menuBar).Colorbar->MoveColorbar , event.x, event.y , 0
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0
				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = ((oSystem.menuBar).Colorbar).colorView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
						IF Obj_Valid(item[0]) THEN BEGIN
							(oSystem.menuBar).Colorbar->MoveColorbar , event.x, event.y , 1
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF

END
;+
;add title event
;+
PRO sesMainDraw::Title , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = (oSystem.CurrentPro).oView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
					IF Obj_Valid(item[0]) THEN BEGIN
						nameArr = StrArr(N_Elements(item))
						FOR i = 0 , N_Elements(item)-1 DO BEGIN
							nameArr = Obj_Class(item[i])
						ENDFOR
			   			IF nameArr EQ 'IDLGRTEXT' THEN BEGIN
							IF Obj_Valid((oSystem.menuBar).Title) THEN BEGIN
								(oSystem.menuBar).Title->ChangeXY , event.x , event.y , 0
							ENDIF
						ENDIF
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0
				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = (oSystem.CurrentPro).oView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
						IF Obj_Valid(item[0]) THEN BEGIN
							nameArr = StrArr(N_Elements(item))
							FOR i = 0 , N_Elements(item)-1 DO BEGIN
								nameArr = Obj_Class(item[i])
							ENDFOR
				   			IF nameArr EQ 'IDLGRTEXT' THEN BEGIN
								IF Obj_Valid((oSystem.menuBar).Title) THEN BEGIN
									(oSystem.menuBar).Title->ChangeXY , event.x , event.y , 1
								ENDIF
							ENDIF
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF

END
;+
;cut image event
;+
PRO sesMainDraw::CutImage , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = (oSystem.CurrentPro).oView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
					IF Obj_Valid(item[0]) THEN BEGIN
						nameArr = StrArr(N_Elements(item))
						FOR i = 0 , N_Elements(item)-1 DO BEGIN
							nameArr = Obj_Class(item[i])
						ENDFOR
;			   			IF nameArr EQ 'IDLGRIMAGE' OR nameArr EQ 'IDLGRPOLYGON'  THEN BEGIN
							IF Obj_Valid((oSystem.menuBar).CutImage) THEN BEGIN
								(oSystem.menuBar).CutImage->PlotFrame , event.x , event.y , 0
							ENDIF
;						ENDIF
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0
				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = (oSystem.CurrentPro).oView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
						IF Obj_Valid(item[0]) THEN BEGIN
							nameArr = StrArr(N_Elements(item))
							FOR i = 0 , N_Elements(item)-1 DO BEGIN
								nameArr = Obj_Class(item[i])
							ENDFOR
;				   			IF nameArr EQ 'IDLGRIMAGE' OR nameArr EQ 'IDLGRPOLYGON'  THEN BEGIN
								IF Obj_Valid((oSystem.menuBar).CutImage) THEN BEGIN
									(oSystem.menuBar).CutImage->PlotFrame , event.x , event.y , 1
								ENDIF
;							ENDIF
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF
END
;+
;plot x y value event
;+
PRO sesMainDraw::PlotXY , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	IF Obj_Valid(oSystem.currentPro) THEN BEGIN
		proArr = ['SESSINGLEIMAGE','SESCHANGEIMAGE']
		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
		IF count NE 0 THEN BEGIN
			CASE event.type OF
				0:BEGIN
					self.mouseType = 1
					view = (oSystem.CurrentPro).oView
					item = oSystem.oWindow->Select(view, [event.x, event.y])
			   		IF Obj_Valid(item[0]) THEN BEGIN
						nameArr = StrArr(N_Elements(item))
						FOR i = 0 , N_Elements(item)-1 DO BEGIN
							nameArr = Obj_Class(item[i])
						ENDFOR
;			   			IF nameArr EQ 'IDLGRIMAGE' OR nameArr EQ 'IDLGRPOLYLINE' THEN BEGIN
							IF Obj_Valid((oSystem.menuBar).PlotSection) THEN BEGIN
								(oSystem.menuBar).PlotSection->PlotCross , event.x , event.y
							ENDIF
;						ENDIF
					ENDIF
				END
				1:BEGIN
					self.mouseType = 0
				END
				2:BEGIN
					IF self.mouseType EQ 1 THEN BEGIN
						view = (oSystem.CurrentPro).oView
						item = oSystem.oWindow->Select(view, [event.x, event.y])
				   		IF Obj_Valid(item[0]) THEN BEGIN
							nameArr = StrArr(N_Elements(item))
							FOR i = 0 , N_Elements(item)-1 DO BEGIN
								nameArr = Obj_Class(item[i])
							ENDFOR
;				   			IF nameArr EQ 'IDLGRIMAGE' OR nameArr EQ 'IDLGRPOLYLINE' THEN BEGIN
								IF Obj_Valid((oSystem.menuBar).PlotSection) THEN BEGIN
									(oSystem.menuBar).PlotSection->PlotCross , event.x , event.y
								ENDIF
;							ENDIF
						ENDIF
					ENDIF
				END
			ELSE:
			ENDCASE
		ENDIF
	ENDIF
END
;+
;change draw size
;+
PRO sesMainDraw::Resize, newVisW, newVisH

	COMPILE_OPT IDL2

    oldGeom = Widget_Info(self.drawID, /Geometry)

    (self.oSystem).oWindow->GetProperty, $
    				Minimum_Virtual_Dimensions = minVirtualDims, $
    				Current_Zoom = currentZoom;, $
;    				Auto_Resize = autoResize

    newDims = [newVisW, newVisH]

    IF !Version.OS_Family EQ 'Windows' THEN BEGIN
        ; Here we check the Y dimension and adjust the X size.
        IF (newDims[1] LT minVirtualDims[1]) THEN BEGIN
            ; If adding a scrollbar take its space away from the draw.
            ; Here we have to guess at the scrollbar size.
            IF (oldGeom.Scr_XSize EQ oldGeom.XSize) THEN $
                newDims[0] -= 21
        ENDIF ELSE BEGIN
            ; If removing a scrollbar, add its space back to the draw.
            ; Here we can use geom to get the scrollbar size.
            newDims[0] += (oldGeom.Scr_XSize - oldGeom.XSize)
        ENDELSE

        ; Here we check the X dimension and adjust the Y size.
        IF (newDims[0] LT minVirtualDims[0]) THEN BEGIN
            ; If adding a scrollbar take its space away from the draw.
            IF (oldGeom.Scr_YSize EQ oldGeom.YSize) THEN $
                newDims[1] -= 21
        ENDIF ELSE BEGIN
            ; If removing a scrollbar, add its space back to the draw.
            newDims[1] += (oldGeom.Scr_YSize - oldGeom.YSize)
        ENDELSE
    ENDIF


	 virtualDims = (newDims > minVirtualDims)

    ; Resize the window and the widget.
    Widget_Control, self.drawID, $
        Draw_XSize = virtualDims[0], Draw_YSize = virtualDims[1], $
        XSize = newDims[0], YSize = newDims[1]

	self.drawSize = virtualDims
END
;+
;handle event
;+
PRO sesMainDraw::HandleEvent , event
	COMPILE_OPT IDL2
	oSystem = self.oSystem

	Catch, theError
   	IF theError NE 0 THEN BEGIN
      Catch, /Cancel
      ok = Error_Message('Warning!')
      RETURN
   	ENDIF

	IF event.type EQ 2 THEN BEGIN
		self->ShowInfo, event.x, event.y
   	ENDIF

   	CASE event.type OF
   		0:BEGIN
	   		proArr = ['SESSINGLEIMAGE','SESMULTIIMAGE','SESCHANGEIMAGE']
	   		temp = Where(Obj_Class(oSystem.CurrentPro) EQ proArr,count)
	   		tempClass = oSystem.CurrentPro
	   		IF temp EQ 1 THEN BEGIN
		   		view = tempClass.oView
		   		item = oSystem.oWindow->Select(view, [event.x, event.y])
		   		type = Obj_Valid(item)
		   		IF type[0] NE 0 THEN BEGIN
		   			number = N_Elements(item)
		   			IF number EQ 1 THEN BEGIN
			   			name = Obj_Class(item)
			   			IF name EQ 'IDLGRIMAGE' THEN BEGIN
			   				tempClass->plotFrame , item
			   				IF event.press EQ 4 THEN BEGIN
						   		rightBase = Widget_Info(event.id, Find_By_UName = 'RightMenu')
						   		multi = Widget_Info(event.id, Find_By_UName = 'rightMulti')
						   		single = Widget_Info(event.id, Find_By_UName = 'rightSingle')
						   		Widget_Control,multi,Sensitive = 0
						   		Widget_Control,single,Sensitive = 1
								Widget_DisplayContextMenu, event.id, event.x, event.y, rightBase
						    	oSystem.oWindow->Draw, oSystem.oScene
						    	oSystem.rightMenu->associateImage , item
						    ENDIF
			   			ENDIF
			   		ENDIF
		   		ENDIF
		   	ENDIF

		   	IF temp EQ 0 THEN BEGIN

		   	ENDIF

		   	IF temp EQ 2 THEN BEGIN
		   		IF event.press EQ 4 THEN BEGIN
			   		rightBase = Widget_Info(event.id, Find_By_UName = 'RightMenu')
			   		multi = Widget_Info(event.id, Find_By_UName = 'rightMulti')
			   		single = Widget_Info(event.id, Find_By_UName = 'rightSingle')
			   		Widget_Control,multi,Sensitive = 1
			   		Widget_Control,single,Sensitive = 0
					Widget_DisplayContextMenu, event.id, event.x, event.y, rightBase
					oSystem.oWindow->Draw, oSystem.oScene
			    ENDIF
		   	ENDIF
		END
		1:BEGIN
		END
		2:BEGIN
		END
		3:BEGIN
		END
		4:BEGIN
		   	oSystem.oWindow->Draw, oSystem.oScene
		END
   	ELSE:
   	ENDCASE
   	IF (oSystem.menuBar).CurrentName EQ 'PlotXY' THEN BEGIN
   		self->PlotXY , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'CutImage' THEN BEGIN
   		self->CutImage , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'Colorbar' THEN BEGIN
   		self->Colorbar , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'Label' THEN BEGIN
   		self->Label , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'Magnifier' THEN BEGIN
   		self->Magnifier , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'LQU' THEN BEGIN
   		self->LQU , event
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'ZoomOut' THEN BEGIN
   		self->Zoom , event , 1
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'ZoomIn' THEN BEGIN
   		self->Zoom , event , 2
   	ENDIF
   	IF (oSystem.menuBar).CurrentName EQ 'Pan' THEN BEGIN
   		self->Pan , event
   	ENDIF
END
;+
;create main draw
;+
PRO sesMainDraw::Create
	COMPILE_OPT IDL2
	drawBase = Widget_Base(self.mainBase)

	;create draw to show cartoon
	self.cartoonBase = Widget_Base(drawBase, Map = 0)

	self.drawCar = Widget_Draw(self.cartoonBase						, $
			 					UName          = 'sesCarDraw'	    , $
								Retain         = 2         			, $
;								Renderer       = 1         			, $
								XSize          = self.drawSize[0] 	, $
								YSize          = self.drawSize[1]	, $
								Scr_XSize      = self.drawSize[0]	, $
								Scr_YSize      = self.drawSize[1] 	, $
								Graphics_Level = 2         			, $
								ClassName      = 'IDLitWindow'      )



	;create draw to show image
	self.frontBase = Widget_Base(drawBase, Map = 1 )

	self.drawID = Widget_Draw(self.frontBase						, $
			 					UName          = 'sesMainDraw'	    , $
								Retain         = 2         			, $
;								Renderer       = 1         			, $
								XSize          = self.drawSize[0] 	, $
								YSize          = self.drawSize[1]	, $
								Scr_XSize      = self.drawSize[0]	, $
								Scr_YSize      = self.drawSize[1] 	, $
								Graphics_Level = 2         			, $
								/App_Scroll                			, $
								/Button_Events             			, $
								/Keyboard_Events        			, $
		;						/Tracking_Events           			, $
								/Motion_Events                		  )
END
;+
;clean up
;+
PRO sesMainDraw::CleanUp
	COMPILE_OPT IDL2

END
;+
;initialize
;+
FUNCTION sesMainDraw::Init , oSystem = oSystem, DispersionY = dispersionY
	COMPILE_OPT IDL2
	self.oSystem = oSystem
	self.mainBase = oSystem.mBase
	Device, Get_Screen_Size = screenSize
	self.drawSize = [screenSize[0], screenSize[1]-dispersionY]

	self->Create

	Return,1

END
;+
;draw class name
;+
PRO sesMainDraw__Define

	COMPILE_OPT IDL2
	void = { sesMainDraw                           , $
	         ;system parameter
			 mainBase          :    0              , $
			 frontBase         :    0              , $
			 cartoonBase       :    0L             , $
			 drawID            :    0              , $
			 drawCar           :    0              , $
			 drawSize          :    [0,0]          , $
			 mouseType         :    0              , $
			 panStatus         :    IntArr(8)      , $
			 ;system objct
	         oSystem           :    Obj_New()        $

			}

END