;***
;##############################################################################
;#Function  name   : ql_upd_frame                                             #
;#LEVEL            : 2                                                        #
;#                                                                            #
;#Author           : Nicolas MORISSET                                         #
;#Language         : IDL                                                      #
;#Purpose          : manage cut area                                          #
;#Creation date    : 03-03-94                                                 #
;#External modules :                                                          #
;#                                                                            #
;#Call sequence    :                                                          #
;#                                                                            #
;#Arguments        :                                                          #
;#                                                                            #
;#Description      :                                                          #
;#                                                                            #
;#                                                                            #
;##############################################################################
;***

PRO ql_upd_frame, type, xdev, ydev, width, ev

  ;============================================================================
  ;                       Common Block in use
  ;============================================================================
  COMMON const_com
  COMMON curr_data_p_com
  COMMON curr_data_i_com
  COMMON widget_id_com
  COMMON dispcal_com
  COMMON color_com

  ;============================================================================
  ;                       Initialization
  ;============================================================================
  err = 0
  ;============================================================================
  ;                       Processing
  ;============================================================================

  ql_refresh, type, err

  IF (type EQ PROFILE) THEN BEGIN
     xoffset  = curr_dctrl_p.x.offset
     yoffset  = curr_dctrl_p.y.offset
     xplot_sz = curr_dctrl_p.x.plot_sz
     yplot_sz = curr_dctrl_p.y.plot_sz
     xnb_dot  = curr_dctrl_p.x.nb_dot
     ynb_dot  = curr_dctrl_p.y.nb_dot
     xplotmin = curr_dctrl_p.x.plot_min
     xplotmax = curr_dctrl_p.x.plot_max
     color    = yellow

  ENDIF ELSE BEGIN
     xoffset  = curr_dctrl_i.x.offset
     yoffset  = curr_dctrl_i.y.offset
     xplot_sz = curr_dctrl_i.x.plot_sz
     yplot_sz = curr_dctrl_i.y.plot_sz
     xnb_dot  = curr_dctrl_i.x.nb_dot
     ynb_dot  = curr_dctrl_i.y.nb_dot
     xplotmin = curr_dctrl_i.x.plot_min
     xplotmax = curr_dctrl_i.x.plot_max
     color    = blue
  ENDELSE

  ; Clipping
  ; --------
  xleft  = xdev - width + 1
  xright = xdev + width

  IF (xleft LT xoffset) THEN BEGIN
    xleft  = xoffset
    xdev   = xoffset + width - 1
    xright = xoffset + 2*width - 1
  ENDIF

  IF (xright GT xoffset + xplot_sz - 1) THEN BEGIN
    xleft  = xoffset + xplot_sz - 2*width - 1
    xdev   = xoffset + xplot_sz - width - 1
    xright = xoffset + xplot_sz - 1
  ENDIF

  x_begin_cut = xleft  - xoffset
  x_end_cut   = xright - xoffset

 
  x_begin_data = xplotmin + ((x_begin_cut/(xplot_sz-1)) * (xplotmax-xplotmin))
  x_end_data   = xplotmin + ((x_end_cut  /(xplot_sz-1)) * (xplotmax-xplotmin))

  ; Compute indexes
  ; ---------------
  IF (type EQ PROFILE) THEN BEGIN
    i = 0
    WHILE (curr_x_p(i) LT x_begin_data) AND (i LT xnb_dot-1) DO i = i + 1
    IF ( i GE xnb_dot) THEN i = -1
    x_begin_cut = i

    i = 0
    WHILE (curr_x_p(i) LT x_end_data) AND (i LT xnb_dot-1)   DO i = i + 1
    IF (i LE 0) OR (i GE xnb_dot) THEN i = -1
    x_end_cut = i-1
  ENDIF ELSE BEGIN
    x_begin_cut = ROUND(x_begin_cut * ( (xnb_dot-1)/(xplot_sz-1) )  )
    x_end_cut   = ROUND(x_end_cut   * ( (xnb_dot-1)/(xplot_sz-1) )  )
  ENDELSE



  ; Drawing
  ; -------
  xx    = LONARR(2)
  yy    = LONARR(2)
  xx(0) = xleft
  xx(1) = xleft
  yy(0) = yoffset
  yy(1) = yoffset+yplot_sz
  PLOTS, xx, yy, /DEVICE, COLOR=color

  xx(0) = xright
  xx(1) = xright
  PLOTS, xx, yy, /DEVICE, COLOR=color

END

