;+
;change color
;+
PRO sesColorbar::ChangeColor , r , g , b
	COMPILE_OPT IDL2
	IF self.removeType NE 1 THEN BEGIN
		oSystem = self.oSystem
		pal = BytArr(3, 256)
		pal[0,*] = r
		pal[1,*] = g
		pal[2,*] = b
		rgb = Reform(pal[*,Indgen(256*16) MOD 256],3,256,16)

	    self.colorImage->SetProperty,Data=rgb
	    oSystem.oWindow -> Draw,oSystem.oScene
	ENDIF
END
;+
;move colorbar
;+
PRO sesColorbar::MoveColorbar , eventX, eventY , type
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	self.colorView->GetProperty,Dimensions = dim
	self.colorView->GetProperty,Location = loc
	newLoc = [loc[0]+dim[0]/2,loc[1]+dim[1]/2]
	ox = eventX-newLoc[0]
	oy = eventY-newLoc[1]
	CASE type OF
		0:BEGIN
			self.colorView->SetProperty,Location = [loc[0]+ox, loc[1]+oy]
		END
		1:BEGIN
			nx = 0 >((loc[0]+ox)<(self.baseSize[0]-dim[0]))
			ny = 0 >((loc[1]+oy)<(self.baseSize[1]-dim[1]))
			self.colorView->SetProperty,Location = [nx, ny]
		END
	ELSE:
	ENDCASE
	oSystem.oWindow -> Draw,oSystem.oScene
END
;+
;handle event
;+
PRO sesColorbar::HandleEvent, event
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	UName = Widget_Info(event.id, /UName)
	dx = 255-0

	IF Tag_Names(event, /Structure_Name) EQ 'WIDGET_KILL_REQUEST' THEN BEGIN
		Widget_Control, event.top, /Destroy
		Obj_Destroy, self.oComp1
		Return
	ENDIF
	IF (event.type EQ 0) THEN BEGIN
		value = Widget_Info(event.ID, Component=event.component, $
		      	            Property_Value=event.identifier)
		; Set the component's property value.
		event.component->SetPropertyByIdentifier, event.identifier, value
	   	CASE event.identifier OF
			'COLOR':begin
				self.color=value
				self.colorAxis->SetProperty,Color = self.color
			END
			'TICKLEN':BEGIN
				self.ticklen=value
				self.colorAxis->SetProperty,Ticklen = self.ticklen
			END

			'SUBTICKLEN':BEGIN
				self.subticklen=value
				self.colorAxis->SetProperty,Subticklen = self.subticklen
			END

;			'ORIENTATION':BEGIN
;				self.orientation = value
;				IF value EQ 1 THEN BEGIN
;					self.colorView->SetProperty,Dimensions = [self.colorbarSize[1]+40,dx+20]
;					self.colorView->SetProperty,Location = [10,self.baseSize[1]/2-dx/2]
;					self.colorView->SetProperty,ViewPlane_Rect = [0,-10,self.colorbarSize[1]+40,dx+20]
;					self.colorImage->SetProperty,Location = [35,0]
;					self.colorAxis->SetProperty,Location = [35,0]
;					self.colorImage->SetProperty,Dimensions=[self.colorbarSize[1],dx]
;				ENDIF
;			END
			'AXIS_TITLE':BEGIN
				self.title = value
				self.colorTitle->SetProperty,Strings = self.title
			END
		ELSE:Return
		ENDCASE
		oSystem.oWindow -> Draw, oSystem.oScene
	ENDIF
END
;+
;edit
;+
PRO sesColorbar::Edit
	COMPILE_OPT IDL2

	cBase = Widget_Base(/column                             , $
						TITLE='edit colorbar'               , $
						UName      = 'EditColorbar' 	    , $
		                UValue	   = self					, $
						/modal                              , $
						/toolbar                            , $
						group_leader=self.mainBase          , $
						/tlb_kill_request_events			, $
						/TLB_MOVE_EVENTS					, $
						tlb_frame_attr=1					  )

	base1 = Widget_Base(cBase, /column)
	oComp1 = Obj_New('IDLitvisColorbar', $
						name='setColorbar', $
						description='set colorbar', $
						color=self.color, $
						ticklen=self.ticklen, $
						subticklen=self.subticklen, $
						orientation=self.orientation, $
						axis_title=self.title, $
						/REGISTER_PROPERTIES)
	self.oComp1 = oComp1
	oComp1->SetPropertyAttribute, 'TRANSPARENCY', NAME='transparency',/HIDE
	oComp1->SetPropertyAttribute, 'NAME', /HIDE
	oComp1->SetPropertyAttribute, 'DESCRIPTION', /HIDE
	oComp1->SetPropertyAttribute, 'HIDE', NAME='frame', /HIDE, $
		enumlist = ['show','hide']
	oComp1->SetPropertyAttribute, 'COLOR', NAME='coordinate color'
	oComp1->SetPropertyAttribute, 'TICKLEN', NAME='main scale length', valid_range=[1,40,1]
	ocomp1->SetPropertyAttribute, 'SUBTICKLEN', NAME='assistant scale length', valid_range=[0d,1d,0.2d]
	oComp1->SetPropertyAttribute, 'TEXT_COLOR', NAME='value color', sensitive=1,/HIDE
	oComp1->SetPropertyAttribute, 'FONT_SIZE', NAME='value size',/HIDE
	ocomp1->SetPropertyAttribute, 'ORIENTATION', NAME='bar direction', $
		enumlist = ['level','uprightness'] , /HIDE
	ocomp1->SetPropertyAttribute, 'AXIS_TITLE', HIDE=1, NAME='title'

	oComp1->SetPropertyAttribute, $
			['MAJOR', $
			'MINOR', $
			'BORDER_ON', $
			'GRIDSTYLE', $
			'THICK', $
			'NOTEXT', $
			'FONT_STYLE', $
			'TEXTPOS', $
			'TICKINTERVAL', $
			'TICKLAYOUT', $
			'TICKFORMAT', $
			'TICK_DEFINEDFORMAT', $
			'FONT_INDEX'], /HIDE
;
	prop1 = Widget_Propertysheet(base1, VALUE=oComp1, UNAME='PropSheet', $
	   		XSIZE=40, YSIZE=5, /SUNKEN_FRAME , $
	   		/MULTIPLE_PROPERTIES)

   	Widget_Control, cBase, /Realize
	self.colorContainer->Add,self.oComp1

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

END
;+
;remove
;+
PRO sesColorbar::Remove
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	Obj_Destroy , self.colorContainer
	oSystem.oWindow -> Draw, oSystem.oScene
	self.removeType = 1
END
;+
;create
;+
PRO sesColorbar::Create
	COMPILE_OPT IDL2
	oSystem = self.oSystem
	oModel = (oSystem.CurrentPro).oModel
	fMin = self.MinMaxValue[0]
    fMax = self.MinMaxValue[1]
    dx = 255-0

	self.colorContainer = Obj_New('IDL_Container')

	self.colorView = Obj_New('IDLgrView'		       , $
						  Dimensions = [dx+20,self.colorbarSize[1]+40]     , $
						  ViewPlane_Rect = [-10,0,dx+20,self.colorbarSize[1]+40] , $
						  Location = [self.baseSize[0]/2-dx/2,10]       , $
						  color = [200,200,200]                         , $
						  Transparent=1                                 )

	oSystem.oScene->Add , self.colorView
	self.colorModel = Obj_New('IDLgrModel')
	self.colorView->Add, self.colorModel

	rgb = BytArr(3,256,16)
    rgb[0,*,*] = Indgen(256*16) MOD 256
    rgb[1,*,*] = Indgen(256*16) MOD 256
    rgb[2,*,*] = Indgen(256*16) MOD 256
    self.colorImage = Obj_New('IDLgrImage'        , $
							   Location = [0,35] , $
                               rgb                 )
    self.colorTitle = Obj_New('IDLgrText'  , $
                              ' ')
    self.colorAxis = Obj_New('IDLgrAxis'   , $
                              /Exact       , $
                              Color=[255,255,255], $
                              Location = [0,35] , $
    						  Ticklen=15   , $
    						  Major=6      , $
    						  Title=self.colorTitle )

	self.colorModel->Add,self.colorImage
    self.colorModel->Add,self.colorAxis
    self.colorContainer->Add,self.colorView
    self.colorContainer->Add,self.colorModel
    self.colorContainer->Add,self.colorImage
    self.colorContainer->Add,self.colorTitle
    self.colorContainer->Add,self.colorAxis
;   oModel->Add,self.colorModel


;    self.colorModel ->Scale, 5, 1,1
;    self.colorView->SetProperty,Dimensions = [(dx+20)*5,self.colorbarSize[1]+40] , $
;                                ViewPlane_Rect = [-10,0,(dx+20)*5,self.colorbarSize[1]+40]  , $
;                                Location = [0,10]

;    self.colorModel ->Translate,100-fmin*255./dx,20,0

	(oSystem.CurrentPro).oPalette->Getproperty, Red_Values = r,Green_Values = g, Blue_Values = b
	pal = BytArr(3, 256)
	pal[0,*] = r
	pal[1,*] = g
	pal[2,*] = b
	rgb = Reform(pal[*,Indgen(256*16) MOD 256],3,256,16)

    self.colorImage->SetProperty,Data=rgb,Dimensions=[dx,self.colorbarSize[1]]
;                                 ;Location=[0-self.colorbarSize[0],0-10]
    self.colorAxis->GetProperty,TickText=oText,TITLE=oTitle
    oText->SetProperty,Char_Dimensions=[0,0]
    self.colorTitle->SetProperty,Char_Dimensions=[0,0],String=sTitle
    self.colorAxis->SetProperty,Range=[0,255]

    self.ticklen = 15.000000
    self.subticklen = 0.500000
    self.title = 'Grayscale'
    self.color = [255,255,255]

	oSystem.oWindow -> Draw, oSystem.oScene
END
;+
;clean up
;+
PRO sesColorbar::CleanUp
	COMPILE_OPT IDL2
	Obj_Destroy , self.colorContainer

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

	COMPILE_OPT IDL2
	self.oSystem = oSystem
	self.mainBase = oSystem.mBase
	self.baseSize =  (oSystem.mainDraw).drawSize
	oView = (oSystem.CurrentPro).oView
	oView->GetProperty, Dimensions = viewDim
	oView->GetProperty, Location = viewLoc
	self.viewDim = viewDim
	self.viewLoc = viewLoc
	(oSystem.CurrentPro).oImage->GetProperty , Data = Image
	self.MinMaxValue = [Min(Image),Max(Image)]
	self.colorbarSize[0] = self.baseSize[0]/2+50
	self.colorbarSize[1] = 16
	self->Create

	Return,1
END
;+
;Colorbar class name
;+
PRO sesColorbar__Define
	COMPILE_OPT IDL2
	void = { sesColorbar                          , $
	         ;system parameter
	         mainBase           :    0L           , $
			 imageX             :    0            , $
			 imageY             :    0            , $
			 viewDim            :    [0,0]        , $
			 viewLoc            :    [0,0]        , $
			 baseSize           :    [0,0]        , $
			 MinMaxValue        :    [0,0]        , $
			 colorbarSize       :    [0,0]        , $
			 color              :    [0,0,0]      , $
			 ticklen            :    0.0          , $
			 subticklen         :    0.0          , $
			 orientation        :    0L           , $
			 title              :    ''           , $
			 removeType         :    0            , $
			 ;system objct

			 colorContainer     :    Obj_New()    , $
			 colorImage         :    Obj_New()    , $
			 colorTitle         :    Obj_New()    , $
			 colorAxis          :    Obj_New()    , $
			 colorModel         :    Obj_New()    , $
			 colorView          :    Obj_New()    , $
			 oComp1             :    Obj_New()    , $
	         oSystem            :    Obj_New()      $

	        }
END