;******************************************************************************
;* File name: ql_pushp_trt                                                    *
;*                                                                            *
;* Content  : 	ql_pushp_trt                                                  *
;*                                                                            *
;*                                                                            *
;* Date		Author		Comment                                       *
;* -------------------------------------------------------------------------- *
;* 10-01-94	E.PETIT		Creation                                      *
;* 20-01-94	E.PETIT		Warning message, Add display ctrl struct      *
;* 16-02-94	E.PETIT		store banner struct                           *
;* 23-02-94	E.PETIT		Add color to plot                             *
;* 02-05-94	E.PETIT		Add DATATYPE init                             *
;*                                                                            *
;******************************************************************************


;***
;##############################################################################
;#Function  name   : ql_pushp_trt                                             #
;#LEVEL            : 1                                                        #
;#                                                                            #
;#Author           : Emmanuel PETIT                                           #
;#Language         : IDL                                                      #
;#Purpose          : manage push of profile into stack                        #
;#Creation date    : December 1993                                            #
;#Update date      : 20-01-94	E.PETIT	 Add warning mes, Add display ctrl    #
;#External modules :                                                          #
;#                                                                            #
;#Call sequence    : Called by Event Handler                                  #
;#                                                                            #
;#Arguments        :                                                          #
;#                                                                            #
;#Description      :                                                          #
;#                                                                            #
;#                                                                            #
;##############################################################################
;*** 

PRO ql_pushp_trt, ev

  ;============================================================================
  ;                       Common Block in use
  ;============================================================================
  COMMON stack_com
  COMMON const_com
  COMMON curr_data_p_com
  COMMON widget_id_com
  COMMON load_flg_com 

  ;============================================================================
  ;                       Initialization
  ;============================================================================
  free_index  = -1  
  push_header = curr_header_p

  ;============================================================================
  ;                       Processing 
  ;============================================================================
  WIDGET_CONTROL, /HOURGLASS 

  WIDGET_CONTROL, ev.id, GET_UVALUE=value
  selected_profile = FIX (STRMID (value, 6,1))

  ; -------------------
  ; Look for free space 
  ; -------------------
  FOR i=STACK_SZ-1,0,-1 DO BEGIN
    IF (stack_hd.p(i).free EQ 0) THEN BEGIN
      free_index = i
    ENDIF
  ENDFOR

  IF (free_index EQ -1) THEN BEGIN 
    ql_err_msg, 6
    RETURN 
  ENDIF

  ; --------------
  ; Update Extrema
  ; --------------
  yplotmax = MAX (curr_data_p(selected_profile,*), xmax, MIN=yplotmin) 
  IF (yplotmax EQ yplotmin) THEN yplotmax = yplotmin + 1
  curr_dctrl_p.y.plot_max = yplotmax
  curr_dctrl_p.y.plot_min = yplotmin
  curr_dctrl_p.y.min      = curr_dctrl_p.y.plot_min    
  curr_dctrl_p.y.max      = curr_dctrl_p.y.plot_max    
  IF(load_flag_p GT 1) THEN curr_dctrl_p.color      = MULTI_COLOR (selected_profile)

  ; -------------------------- 
  ; Update stack file & Header
  ; --------------------------   
  stack_hd.p(free_index).free = 1
  stack_hd.p(free_index).display_ctrl = curr_dctrl_p
  stack_hd.p(free_index).banner = curr_banner_p


  ; -------------------------------  
  ; Only one profile can be pushed
  ; -------------------------------  
  
  push_header.SSTYPIMG = -1
  IF (push_header.DATATYPE EQ 11 ) OR $
     (push_header.DATATYPE EQ 12 ) OR $
     (push_header.DATATYPE EQ 13 ) OR $
     (push_header.DATATYPE EQ 14 ) THEN push_header.DATATYPE = 11 


  POINT_LUN, stack_unit, 0
  WRITEU   , stack_unit, stack_hd 
  POINT_LUN, stack_unit, stack_hd.p(free_index).offset 
  WRITEU   , stack_unit, push_header
  WRITEU   , stack_unit, curr_data_p(selected_profile,*)


  ; --------------------   
  ; Update icons display
  ; --------------------
  WIDGET_CONTROL, /HOURGLASS 
  WIDGET_CONTROL, profile_stack_icon(free_index), GET_VALUE=win_num
  WSET, win_num

  tampon = !P.MULTI
  !P.MULTI = 0
  PLOT, curr_data_p(selected_profile,*), $
        XMARGIN = [0,0]                , $
        YMARGIN = [0,0]                , $
        COLOR   = curr_dctrl_p.color   , $
        XSTYLE  = 4                    , $
        YSTYLE  = 4                     

  !P.MULTI = tampon

  WIDGET_CONTROL, profile_draw, GET_VALUE=win_num
  WSET, win_num    
 
END
