;+
; NAME:
;	SMEI_SEQUENCE::MENU
;
;
; PURPOSE:
;	To interactively define a SMEI sequence
;
;
; CATEGORY:
;	SMEI_SEQUENCE
;
;
; CALLING SEQUENCE:
;	status = seqref -> menu([flag, group=group])
;
;
; INPUTS:
;
;
;
; OPTIONAL INPUTS:
;
;
;
; KEYWORD PARAMETERS:
;	group	long	A possible group leader for the menu
;	/no_progress	If set, then disable the progress bars (useful
;			if slow network means the progress bars are
;			slowing operation).
;	/show_display	If set, then start by showing the display tab
;			rather than the sequence setting tab
;
;
; OPTIONAL OUTPUTS:
;	flag	1 or 0 for success or failure (needed when called from
;		the constructor)
;
;
; RESTRICTIONS:
;	If any managed widgets are present then the GROUP keyword MUST
;	be present if the sequence doesn't already exist.
;
;
; MODIFICATION HISTORY:
;	Original: 16/12/02; SJT
;	Rearranged layout: 6/1/03; SJT
;	Added progress bar: 10/1/03; SJT
;	Added print option: 16/5/03; SJT
;	Redesigned: 11/6/03; SJT
;	Handle 3-state grid setting: 17/6/03; SJT
;	Renamed menu2 and changed to use tab widgets (for IDL >= 5.6):
;	26/11/03; SJT
;	Merge movie types into a single button: 31/3/04; SJT
;	Add support for background model subtraction: 7/5/04; SJT
;	Add 3-state date settings: 3/6/05; SJT
;	Add no_progress keyword: 25/8/05; SJT
;	Add support for masking flags: 23/10/07; SJT
;	Added support for shifted rectangular maps: 23/11/09; SJT
;	Added SHOW_DISPLAY keyword: 31/8/10; SJT
;	Add noverify keyword: 11/12/12; SJT
;-

pro ss_menu2_event, event

sbase = widget_info(event.top, /child)
widget_control, sbase, get_uvalue = state
widget_control, event.id, get_uvalue = mnu

case mnu of
    'NAME': (*state).original -> set_name, event.value

    'ADD':  begin
        widget_control, event.top, /hourglass, sensitive = 0
        (*state).original -> add_menu, group = event.top, no_progress $
          = (*state).no_progress, noverify = (*state).noverify
        widget_control, event.top, /sensitive, hourglass = 0
    end
    'DEL': (*state).original -> delete_menu, group = event.top
    'MODEL': (*state).original -> model_menu, group = event.top

    'SHOW': (*state).original -> show, group = event.top

    'GRID': (*state).original -> set_annotations, grid = event.index
    'DATE': (*state).original -> set_annotations, date = event.index
    'IRANGE': (*state).original -> set_annotations, range = event.select
    'FILE': (*state).original -> set_annotations, file = event.select
    'PLANE': (*state).original -> set_plane, event.index
    'CENT': (*state).original -> set_shift, event.index

    'COMP': if smei_query(dialog_parent = event.top, $
                          ['This will destroy the raw images', $
                           'are you sure you want to do this?']) then begin
        widget_control, event.top, hourglass = 1
        (*state).original -> compact, /mk_nan
        widget_control, event.top, hourglass = 0
    endif

    'RANGE': (*state).original -> range_menu, group = event.top
    'ZOOM': junk = (*state).original -> zoom_menu(group = event.top)
    'MASK': (*state).original -> flags_menu, group = event.top

    'COLOUR': (*state).original -> set_colour_map, /menu, group = $
      event.top

    'SAVE': (*state).original -> save, group = event.top, /menu
    'MOVIE': (*state).original -> movie_menu, group = event.top, $
      no_progress = (*state).no_progress
    'PNG': (*state).original -> png_menu, group = event.top, $
      no_progress = (*state).no_progress

    'PRINT': (*state).original -> print_menu, group = event.top

    'INFO': (*state).original -> show_info, group = event.top

    'PROFILE': (*state).original -> profile_menu, group = event.top

    'QUIT': if (*state).iam_init then begin
        widget_control, event.top, /destroy
        if event.value eq 'DESTROY' then (*state).state = -1 $
        else (*state).state = 1
        return
    endif else begin
        widget_control, event.top, /destroy
        if event.value eq 'DESTROY' then obj_destroy, $
          (*state).original
        ptr_free, state
        return
    endelse

    "MAP":                      ; Ignore events from the tab.

endcase

isi = (*state).original -> get_count() gt 0
widget_control, (*state).delid, sensitive = isi
widget_control, (*state).showid, sensitive = isi
widget_control, (*state).mvid, sensitive = isi
widget_control, (*state).pngid, sensitive = isi
widget_control, (*state).prid, sensitive = isi
widget_control, (*state).cmpid, sensitive = isi
widget_control, (*state).profid, sensitive = isi
if widget_info(/valid, (*state).mskid) then $
  widget_control, (*state).mskid, sensitive = isi
widget_control, (*state).mskbase, map = $
  (*state).original -> get_pipe_level() ge 2
widget_control, (*state).ctbase, map = (*state).original -> $
  get_projection() eq 2

npl = (*state).original -> get_n_planes()
if (*state).original -> get_plane() gt npl then $
  (*state).original -> set_plane, 0
if npl eq 0 then plist = 'Processed' $
else plist = ['Processed', string(indgen(npl)+1, $
                                  format = "('Raw #',I2)")]
widget_control, (*state).plid, set_value = plist, set_droplist_select $ $
                = (*state).original -> get_plane(), sensitive = npl gt $
                0
widget_control, (*state).bgid, sensitive = npl ge 2

end

pro smei_sequence::menu2, flag, iam_init, group = group, no_progress = $
                          no_progress, show_display = show_display, $
                          noverify = noverify

;	We need to check if this is called from the init method or
;	from elsewhere as we must run it blocking or modal if the call
;	is from init.


if widget_info(/active) and not keyword_set(group) and iam_init then begin
    smei_msg, /error, ["smei_sequence::menu2 must have a group", $
                       "leader if widgets are already present", $
                       "and the call is from the constructor"]
    return
endif

igrid = self -> get_annotations(/grid)
idate = self -> get_annotations(/date)
ifile = self -> get_annotations(/file)
irange = self -> get_annotations(/range)
nplane = self -> get_n_planes()

base = widget_base(/column, $
                   title = 'SMEI Sequence', $
                   group = group, $
                   modal = keyword_set(group) and iam_init)

label = widget_label(base, $
                     value = 'SMEI Sequence set up')

junk = cw_ffield(base, $
                 label = "Sequence Name:", $
                 value = self -> get_name(), $
                 /text, $
                 xsize = 30, $
                 uvalue = 'NAME')



cbase = widget_tab(base, $
                   uvalue = "MAP")


mainbase = widget_base(cbase, $
                       title = "Sequence Settings", $
                       row = 5, $
                       /grid)

junk = widget_button(mainbase, $
                     value = 'Add images', $
                     uvalue = 'ADD')

delid = widget_button(mainbase, $
                      value = "Delete/Sort", $
                      uvalue = 'DEL', $
                      sensitive = self -> get_count() gt 0)

bgid = widget_button(mainbase, $
                     value = "Subtract background", $
                     uvalue = 'MODEL', $
                     sensitive = nplane ge 2)

junk = widget_button(mainbase, $
                     value = 'Sequence info', $
                     uvalue = 'INFO')

cmpid = widget_button(mainbase, $
                      value = 'Compact all', $
                      uvalue = 'COMP', $
                      sensitive = self -> get_count() gt 0)

junk = widget_button(mainbase, $
                     value = 'Save Sequence', $
                     uvalue = 'SAVE')

prid = widget_button(mainbase, $
                     value = 'Print sequence', $
                     uvalue = 'PRINT', $
                     sensitive = self -> get_count() gt 0)


mvid = widget_button(mainbase, $
                     value = 'Save to Movie', $
                     uvalue = 'MOVIE')
widget_control, mvid, sensitive = self -> get_count() gt 0

pngid = widget_button(mainbase, $
                      value = 'Save to PNGs', $
                      uvalue = 'PNG')
widget_control, pngid, sensitive = self -> get_count() gt 0

profid = widget_button(mainbase, $
                       value = 'Create profile', $
                       uvalue = 'PROFILE')
widget_control, profid, sensitive = self -> get_count() gt 0

dispbase = widget_base(cbase, $
                       title = "Display Settings", $
                       /column)

db0 = widget_base(dispbase, $
                  /row)

junk = widget_button(db0, $
                     value = 'Data range', $
                     uvalue = 'RANGE')

junk = widget_button(db0, $
                     value = 'Colours', $
                     uvalue = 'COLOUR')

junk = widget_button(db0, $
                     value = 'Zoom', $
                     uvalue = 'ZOOM')

mskbase = widget_base(db0)
if self->get_pipe_level() ne 1 then begin
    mskid = widget_button(mskbase, $
                          value = 'Mask', $
                          uvalue = 'MASK')
    widget_control, mskid, sensitive = self -> get_count() gt 0
endif else mskid = 0l

if nplane eq 0 then plist = 'Processed' $ $
else plist = ['Processed', string(indgen(nplane)+1, $
                                  format = "('Raw #',I2)")]

db0 = widget_base(dispbase, $
                  /row)
plid = widget_droplist(db0, $
                       value = plist, $
                       title = 'Plane?', $
                       uvalue = 'PLANE', $
                       /align_right)
widget_control, plid, set_droplist_select = self -> get_plane(), $
                sensitive = nplane gt 0

ctbase = widget_base(db0, $
                     map = self -> get_projection() eq 2)
junk = widget_droplist(ctbase, $
                       value = ["North", $
                                "East", $
                                "South", $
                                "West"], $
                       title = 'Centre', $
                       uvalue = 'CENT', $
                       /align_right)
widget_control, junk, set_droplist_select = self -> $
  get_shift(/raw)

db0 = widget_base(dispbase, $
                  /row)

junk = widget_droplist(db0, $
                       value = ['None', 'Lat & Long', 'Elong & PA', $
                               'L & L (Labelled)', 'E & P (Labelled)'], $
                       title = 'Grid?', $
                       uvalue = 'GRID')
widget_control, junk, set_droplist_select = igrid

db0 = widget_base(dispbase, $
                  /row)

junk = widget_droplist(db0, $
                       value = ['None', 'Year-Day', 'Y-M-D'], $
                       title = 'Date format', $
                       uvalue = 'DATE')
widget_control, junk, set_droplist_select = idate

db0 = widget_base(dispbase, $
                  /row)
db1 = widget_base(db0, $
                  /row, $
                  /nonexclusive)

; junk = widget_button(db1, $
;                      value = 'Date?', $
;                      uvalue = 'DATE')
; widget_control, junk, set_button = idate

junk = widget_button(db1, $
                     value = 'Range?', $
                     uvalue = 'IRANGE')
widget_control, junk, set_button = irange

junk = widget_button(db1, $
                     value = 'Name?', $
                     uvalue = 'FILE')
widget_control, junk, set_button = ifile

if keyword_set(show_display) then widget_control, cbase, $
  set_tab_current = 1

;--------------------------------------------------------------------

showid = widget_button(base, $
                       value = "Show images", $
                       uvalue = 'SHOW', $
                       sensitive = self -> get_count() gt 0)

;--------------------------------------------------------------------

jb = widget_base(base, $
                 /row, $
                 /base_align_center)

junk = cw_bgroup(jb, $
                 ['Quit', 'Quit & Destroy'], $
                 /row, $
                 button_uvalue = ['QUIT', 'DESTROY'], $
                 uvalue = 'QUIT')

state = ptr_new({ss_menu2_state, $
                 original: self, $
                 delid: delid, $
                 bgid: bgid, $
                 showid: showid, $
                 mvid: mvid,  $
                 pngid: pngid, $
                 prid: prid, $
                 plid: plid, $
                 cmpid: cmpid, $
                 profid: profid, $
                 mskbase: mskbase, $
                 ctbase: ctbase, $
                 mskid: mskid, $
                 state: 0, $
                 iam_init: iam_init, $
                 no_progress: keyword_set(no_progress), $
                 noverify: keyword_set(noverify)})

widget_control, label, set_uvalue = state
widget_control, base, /real

xmanager, "ss_menu2", base, no_block = iam_init eq 0

if iam_init then begin
    case (*state).state of
        -1: begin
            if arg_present(flag) then flag = 0 $
            else obj_destroy, self
        end
        1: if arg_present(flag) then flag = 1
    endcase

    ptr_free, state
endif

end

         
