;+
; NAME:
;	SMEI_SEQUENCE::INIT
;
;
; PURPOSE:
;	Constructor for a SMEI sequence
;
;
; CATEGORY:
;	SMEI_SEQUENCE
;
;
; CALLING SEQUENCE:
;	seqref = obj_new("SMEI_SEQUENCE", flist, name=name)
;
;
; OPTIONAL INPUTS:
;	flist	string	Optional list of FITS files to be added as
;			attached SMEI_IMAGEs (findfile() wildcards are
;			allowed).
;
;
; KEYWORD PARAMETERS:
;	name	string	A name for the sequence
;	/compact	If set, then compact the images as they are
;			added (N.B. This only applies to images added
;			during initialization).
;	/menu		If set, then generate a menu to select files
;			and options
;	group	long	A possible group leader for a menu.
;	/noexpand	If set, then do not try to expand wildcards in
;			a file list.
;	/no_progress	If set, then disable the progress bars (useful
;			if slow network means the progress bars are
;			slowing operation).
;	/silent		If set, then supress any messges from readfits.
;	/show_display	If set, then start by showing the display tab
;			rather than the sequence setting tab.
;	/noverify	If set, then trust the image header lists.		
;
;
; OUTPUTS:
;	ok	int	Status flag returned to obj_new
;
;
; MODIFICATION HISTORY:
;	Original: 10/12/02; SJT
;	Add compact option: 13/12/02; SJT
;	Make sure we destroy attached images after a quit & destroy
;	from the menu: 10/1/03; SJT
;	Add no-data index: 11/7/03; SJT
;	Add off sky colours: 18/7/03; SJT
;	Add print options: 22/7/03; SJT
;	Add noexpand keyword and change findfile to file_search:
;	29/7/03; SJT
;	Add print spooling commands: 6/6/05; SJT
;	Add no_progress keyword: 25/8/05; SJT
;	Change default grid to Elong-PA: 14/1/10; SJT
;	Add silent keyword: 8/7/10l SJT
;	Add show_display keyword: 31/8/10; SJT
;	Add noverify keyword: 11/12/12; SJT
;-

function smei_sequence::init, flist, name = name, compact = compact, $
                              menu = menu, group = group, noexpand = $
                              noexpand, no_progress = no_progress, silent = $
                              silent, show_display = show_display, $
                              noverify = noverify

  self.pixmap.id = -1           ; Can't use the destroyer here
  
  self -> set_annotations, date = 1, grid = 2, file = 0, range = 0
  self -> set_delay, 0.25
  self -> set_colour_map, table = 0
  self -> set_null_index, [[128b, 0b], [128b, 255b]]
  self -> set_print_opts, file = 'idl.ps', xsize = 24., ysize = 18., $ $
                          order = 0, type = 0, columns = 1, rows = 1, $
                          print = 'lpr', preview = $
                          'gv --swap'

  if keyword_set(name) then self -> set_name, name

  if n_elements(flist) ne 0 then begin
     if keyword_set(noexpand) then begin
        files = strarr(n_elements(flist))
        for j = 0, n_elements(flist)-1 do files[j] = $
           get_comp_name(flist[j])

        nf = n_elements(files)
     endif else begin
        files = file_search(get_comp_name(flist[0]), count = nf)
        for j = 1, n_elements(flist)-1 do begin
           files = [files, file_search(get_comp_name(flist[j]), count $
                                       = nf1)]
           nf = nf+nf1
        endfor
     endelse
  endif else nf = 0

  if nf ne 0 then begin
     locs = where(files ne '')  ;Remove any non-matching wc's
     files = files[locs]
     if widget_info(/active) then pm = cw_progress(/column, $
                                                   title = 'Reading ' + $
                                                   'images', $
                                                   xsize = 200, $
                                                   ysize = 20, $
                                                   value = 0., $
                                                   max = nf) $
     else pm = 0l

     for j = 0l, nf-1 do begin
        imref = self -> add_image(files[j], compact = compact, silent $
                                  = widget_info(/valid, pm) || $
                                  keyword_set(silent))
        if widget_info(/valid, pm) then begin
           widget_control, pm, set_value = j+1
           if not obj_valid(imref) then smei_msg, $
              /inform, 'Failed to read: ', files[j]
        endif else $
           smei_msg, /inform, string(j+1, nf, (['Fail', $ $
                                                'OK'])[obj_valid(imref)], $
                                     $
                                     format = "('Read ',I0,' of ',I0,' " + $
                                     "Status:',A)")
     endfor
     if widget_info(/valid, pm) then widget_control, pm, /destroy
  endif

  if keyword_set(menu) then self -> menu, flag, group = group, $
                                          no_progress = no_progress, $
                                          show_display = show_display, $
                                          noverify = noverify $
  else flag = 1

  if flag eq 0 then self -> cleanup ; We can't destroy ourselves from
                                ; inside the init, but we must destroy
                                ; any attached images otherwise we'll
                                ; leak memory like a sieve.

  return, flag

end
