;+
;clean temp folder
;+
PRO sesFrame::CleanTempFolder
	COMPILE_OPT IDL2
	sep = self.sep
	number = (self.menuBar).numbers
	IF number NE 0 THEN BEGIN
		FOR i = 0 , number-1 DO BEGIN
			deleFolder = self.tempDir+sep+'Data'+String(Strtrim(i+1,2))
			deleImage = deleFolder+sep+'Image'+String(Strtrim(i+1,2))+'.dat'
			deleCopy = deleFolder+sep+'copyImage'+String(Strtrim(i+1,2))+'.dat'
			deletext = deleFolder+sep+'text'+String(Strtrim(i+1,2))+'.ini'
			File_Delete, deletext, /Quiet
			File_Delete, deleImage, /Quiet
			File_Delete, deleCopy, /Quiet
			File_Delete, deleFolder, /Quiet
			File_Delete, self.tempDir, /Quiet
		ENDFOR
	ENDIF
END
;+
;create init arr
;+
PRO sesFrame::CreateInitArr
	COMPILE_OPT IDL2
	self.lineRowArr = [[2,1,2],[3,2,2],[4,2,2],[5,2,3],[6,2,3]      , $
	                   [7,3,3],[8,3,3],[9,3,3],[10,3,4],[11,3,4]    , $
	                   [12,3,4],[13,3,5],[14,3,5],[15,3,5],[16,4,5] , $
	                   [17,4,5],[18,4,5],[19,4,5],[20,4,5],[21,5,5] , $
	                   [22,5,5],[23,5,5],[24,5,5],[25,5,5]];,[26,5,6] , $
	                   ;[27,5,6],[28,5,6],[29,5,6],[30,5,6]            ]
END
;+
;create temp folder
;+
PRO sesFrame::CreateTempFolder
	COMPILE_OPT IDL2

	tempDir = Filepath('', /TMP)
	self.tempDir = tempDir+'sestemp'
	IF File_Test(self.tempDir,  /Directory) EQ 0 THEN File_MKDir, self.tempDir

END
;+
;mouse style
;+
PRO sesFrame::CreateCursor

	COMPILE_OPT IDL2
	sep = self.sep
	bmpFile = ['hand.bmp','zoom.bmp']
	bmpFile = self.rootDir+sep+'resource'+sep+'bitmap'+sep+'cursor'+sep+bmpFile

	FOR n=0,1 DO BEGIN
		intArray = Read_Image(bmpFile[n])
		intArray = Reverse(intArray)
		strArray = StrArr(16)
		FOR i=0,15 DO BEGIN
		temp = ''
		FOR j=0,15 DO BEGIN
		  IF intArray[j,i] EQ 2 THEN str = ' '
		  IF intArray[j,i] EQ 0 THEN str = '#'
		  IF intArray[j,i] EQ 1 THEN str = '.'
		  temp = str+temp
		ENDFOR
		strArray[15-i] = temp
		ENDFOR

		pCursor = Create_Cursor(strArray, HotSpot = pHotspot, Mask = pMask)

		self.cursorData[n,*] = pCursor
		self.hotSpot[n,*] = pHotspot
		self.mask[n,*] = pMask
	ENDFOR

END
;+
;initialize figure object
;+
PRO sesFrame::CreateGraphic

	COMPILE_OPT IDL2

	oColor = [128,128,128]
;	oColor = [204,223,237]

	self.oScene = Obj_New('IDLgrScene', Color=oColor)

	self.oWindow->SetProperty, Graphics_Tree = self.oScene

	self.oWindow->SetCurrentCursor, 'ARROW'

	self.oWindow->Draw, self.oScene

END
;+
;splash
;+
PRO sesFrame::Splash, SplashFile = splashFile, SplashEnd = splashEnd

	COMPILE_OPT IDL2
	sep = self.sep
	IF ~Keyword_Set(splashEnd) THEN BEGIN

		IF ~Keyword_Set(splashFile) THEN $
				splashFile = self.rootDir+sep+'resource'+sep+'bitmap'+sep+'splash'+sep+'title.bmp'

		splashImage = Read_Image(splashFile)

		nDims = Size(splashImage, /N_Dimensions)
		dims = Size(splashImage, /Dimensions)
		dimX = dims[(nDims EQ 3)]
		dimY = dims[1+(nDims EQ 3)]

		Device, Get_Screen_Size = screenSize
		self.maxSize = screenSize

		xOff = (screenSize[0]-dimX)/2
		yOff = (screenSize[1]-dimY)/2

		splashBase = Widget_Base(Tlb_Frame_Attr = 5, Title = ' ', $
									XOffSet = xOff, YOffSet = yOff)
		splashDraw = Widget_Draw(splashBase, XSize = dimX, YSize = dimY)

;		Widget_Control, splashBase, Map = 0
		Widget_Control, splashBase, /Realize

		Widget_Control, splashDraw, Get_Value = splashDrawID
		Device, Cursor_Standard = 32512
		WSet, splashDrawID
		Tv, splashImage,/True
		splashImage = 0

		Widget_Control, splashBase, /Map

		self.splashBase = splashBase

		Widget_Control, /HourGlass

	END ELSE BEGIN
		Wait, 1
		Widget_Control, self.splashBase, /Destroy
		Widget_Control, self.mBase, /Map
	ENDELSE

END
;+
;Resize window size
;+
PRO sesFrame::Resize, deltaW, deltaH

	COMPILE_OPT IDL2

    drawGeom = Widget_Info((self.mainDraw).drawID, /Geometry)

    newVisW = ((drawGeom.xSize+deltaW)>self.minSize[0])<self.maxSize[0]
    newVisH = ((drawGeom.ySize+deltaH)>self.minSize[1])<self.maxSize[1]

    isUpdate = Widget_Info(self.mBase, /UpDate)

    IF (!Version.OS_Family EQ 'Windows') THEN BEGIN
    	IF isUpdate THEN $
            Widget_Control, self.mBase, UpDate =0
    ENDIF ELSE BEGIN
    ;keep refurbish at all time in unix
        IF (~isUpdate) THEN $
            Widget_Control, self.mBase, /UpDate
    ENDELSE

    ; Update the statusbar to be the same width as the draw + panel.
    self.oStatus->Resize, newVisW

    ; Update the toolbar row to be the same width as the draw.
    self.toolBar->Resize, newVisW

    ;Update draw and scroll bar
;    IF (newVisW NE drawGeom.xSize OR newVisH NE drawGeom.ySize) THEN BEGIN
;        self.mainDraw->Resize, newVisW, newVisH
;    ENDIF

	;Update main base
    IF isUpdate AND ~Widget_Info(self.mBase, /UpDate) THEN $
        Widget_Control, self.mBase, /UpDate

    ;record window size if realize
    IF Widget_Info(self.mBase, /Realized) THEN BEGIN
        Widget_Control, self.mBase, Tlb_Get_Size = baseSize
        self.baseSize = baseSize
    ENDIF
    self.oWindow->Draw, self.oScene

END
;+
;manager for main pro
;+
PRO sesFrame::oXManager
	COMPILE_OPT IDL2
	XManager, 'SunEle'                              , $
	           self.mBase                           , $
	           /No_Block                            , $
	           Event_Handler = 'sesFrame_Event'   , $
  			   Cleanup = 'sesFrame_Cleanup'
END
;+
;create main widget
;+
PRO sesFrame::CreateWidget
	COMPILE_OPT IDL2

	self.mBase = Widget_Base( UName             = 'mBase'         , $
							  UValue            = self            , $
							  Title             = self.Title      , $
							  MBar           	= mBar	          , $
							  Space             = 1               , $
							  XPad              = 0               , $
							  YPad              = 0               , $
							  Map               = 0               , $
							  /Column                             , $
							  /Tlb_Kill_Request_Events			  , $
							  /Tlb_Size_Events                      $
	   						 )

	Widget_Control , self.mBase , /Realize

	self.foreColor = (Widget_Info(self.mBase, /System_Colors)).face_3D

	self.signColor = [212,208,200]

	;create menu bar
	self.menuBar = Obj_New('sesMenuBar'             , $
	                  		MBar     =   mbar       , $
	                  		oSystem  =   self         $
	                      )

	;create tools bar
    self.toolBar = Obj_New('sesToolBar'             , $
							oSystem  =   self         $
    					  )

	;define least size of the interface
	geom = Widget_Info(mBar, /Geometry)
	self.minSize = [geom.Scr_XSize + 10, 100]

	;create draw
	self.mainDraw = Obj_New('sesMainDraw'           , $
                            OSystem = self          , $
                            DispersionY = geom.Scr_YSize+32+74	 $
	                       )

	;create status bar
	self.oStatus = Obj_New('sesStatus'              , $
	                       OSystem = self             $
	                      )

	self.rightMenu = Obj_New('sesRightMenu'         , $
							 OSystem = self           $
							)

	self.oShmData = Obj_New('sesShmData'            , $
	                       OSystem = self             $
	                      )

	;create process bar
	self.oProcess = Obj_New('sesProcess', OSystem = self)

	;creat lable bar
    self.lable   = Obj_New ('sesLable'    ,            $
							oSystem  =   self 		   $
    					   )

	;record window size
	Widget_Control, self.mBase, Tlb_Get_Size = baseSize
    self.baseSize = baseSize

	;get window value
	Widget_Control, (self.mainDraw).drawID, Get_Value = oWindow
	self.oWindow = oWindow

END
;+
;event manage
;+
PRO sesFrame::HandleEvent , event
	COMPILE_OPT IDL2
	IF Tag_Names(event, /Structure_Name) EQ 'WIDGET_KILL_REQUEST' THEN BEGIN
		status = Dialog_Message('Exit SunEle?', Title = 'Warn', /Question)
		IF status EQ 'Yes'THEN Widget_Control, event.top, /Destroy
	    Return
	ENDIF
	UName = Widget_Info(event.id, /UName)
	CASE UName OF
		'mBase' : BEGIN
	        Widget_Control, event.top, Tlb_Get_Size = newSize
	        deltaW = newSize[0]-self.baseSize[0]
	        deltaH = newSize[1]-self.baseSize[1]
	        ;no change
	        IF deltaW EQ 0 AND deltaH EQ 0 THEN BREAK
			self->Resize, deltaW, deltaH
		END
	ELSE : BEGIN
		sPos = Strpos(UName, '_')
		sLen = StrLen(UName)
		iFaceClass = Strmid(UName, 0, sPos)
		iFaceOp = Strmid(UName, sPos+1, sLen-sPos-1)
		IF iFaceClass EQ 'sesMenu' THEN self.menuBar-> HandleEvent, iFaceOp , event = event
		IF iFaceClass EQ 'sesTool' THEN self.toolBar-> HandleEvent, iFaceOp, event
		IF UName EQ 'rightSingle' THEN self.rightMenu->HandleEvent, event,UName
		IF UName EQ 'rightMulti' THEN self.rightMenu->HandleEvent, event,UName
		IF UName EQ 'sesMainDraw' THEN self.mainDraw->HandleEvent, event
	ENDELSE
	ENDCASE
	IF UName EQ 'rightSingle' OR UName EQ 'rightMulti' THEN BEGIN
		self.oWindow -> Draw, self.oScene
	ENDIF
END
;+
;clean up
;+
PRO sesFrame::CleanUp
	COMPILE_OPT IDL2
	self->CleanTempFolder
	Obj_Destroy , self.menuBar
	Obj_Destroy , self.toolBar
	Obj_Destroy , self.MainDraw
	Obj_Destroy , self.oStatus
	Obj_Destroy , self.oScene
	Obj_Destroy , self.oShmData
	Obj_Destroy , self.oProcess
	Obj_Destroy , self.rightMenu
	Obj_Destroy , self.lable

END
;+
;initialize
;+
FUNCTION sesFrame::Init , RootDir = rootdir , $
						  sep = sep         , $
                          Title = Title
	COMPILE_OPT IDL2
	self.RootDir = rootdir
	self.Title = Title
	self.sep = sep
	self->Splash
	self->CreateWidget
	self->CreateGraphic
	self->CreateCursor
	self->Splash, /SplashEnd
	self->oXManager
	Widget_Control, self.mBase, /Map
	self.oWindow -> Draw, self.oScene
	self->CreateTempFolder
	self->CreateInitArr
	Return,1
END
;+
;main class name
;+
PRO sesFrame__Define
	COMPILE_OPT IDL2
	void = { sesFrame          				       , $
			 ;system objct
             sep                :   ''             , $
             mBase              :	0L             , $
             splashBase         :   0              , $
             menuBar            :   Obj_New()      , $
             toolBar            :   Obj_New()      , $
             mainDraw           :   Obj_New()      , $
             oStatus            :   Obj_New()      , $
             oShmData           :   Obj_New()      , $
             oWindow            :   Obj_New()      , $
             oProcess           :   Obj_New()      , $
             rightMenu          :   Obj_New()      , $
             CurrentPro         :   Obj_New()      , $
             lable              :   Obj_New()      , $
             oScene             :   Obj_New()      , $
             ;system parameter
	         zoomState          :   ''             , $
	         Title              :   ''             , $
             tempDir            :   ''             , $
	         foreColor          :   IntArr(3)      , $
	         signColor          :   IntArr(3)      , $
	         minSize            :   IntArr(2)      , $
	         maxSize            :   IntArr(2)      , $
	         baseSize           :   IntArr(2)      , $
	         cursorData		    :	LonArr(10,16)  , $
           	 hotSpot			:	LonArr(10,2)   , $
           	 mask			    :	LonArr(10,16)  , $
           	 lineRowArr         :   IntArr(3,29)   , $
           	 opt_state		    :   0b			   , $
	         RootDir            :   ''               $
			}


END