pro list_codecs_event, event

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

case mnu of
    'CODEC': begin
        (*state).isel = event.index
        if event.clicks eq 2 then widget_control, event.top, /destroy $
        else widget_control, (*state).dobut, sensitive = 1
    end
    'DO': widget_control, event.top, /destroy
    'DONT': begin
        (*state).isel = -1
        widget_control, event.top, /destroy
    end
endcase

end

function list_codecs, ffmpeg = ffmpeg, quicktime = quicktime, group = group

;+
; LIST_CODECS
;	Make a list of the available codecs for FFMPEG AVI or
;	QUICKTIME output from transcode, and select one.
;
; Usage:
;	codec=list_codecs()
;
; Return Value:
;	The chosen codec (string)
;
; Keywords:
;	/ffmpeg		If set, then list the codecs for the ffmpeg
;			export module.
;	/quicktime	If set, then list the codecs for the quicktime
;			export module.
;	group	long	A parent widget.
;
; Notes:
;	Exactly one module keyword must be given.
;	The extraction relies on a fixed format for the codec list
;	emitted from transcode.
;
; History:
;	Original: 1/3/11; SJT
;-

if n_elements(group) eq 0 then group = 0l

if keyword_set(ffmpeg) && keyword_set(quicktime) then begin
    if widget_info(/valid, group) then junk = $
      dialog_message(['Must specify exactly', 'one module keyword'], $
                     dialog_parent = group) $
    else print, 'Must specify exactly one module keyword'
    return, ''
endif

case 1 of
    keyword_set(ffmpeg): module = 'ffmpeg'
    keyword_set(quicktime): module = 'mov'
    else: begin
        if widget_info(/valid, group) then junk = $
          dialog_message(['Must specify exactly', 'one module keyword'], $
                         dialog_parent = group) $
        else print, 'Must specify exactly one module keyword'
        return, ''
    end
endcase

modlib = '[export_'+module+'.so]'
spawn, 'transcode --log_no_color -y '+module+' -F list -i /dev/null', out, err
locs = where(strpos(err, modlib) ne -1, nm)

if nm le 4 then return, ''

nm = nm-4
clist = strtrim(strmid(err[locs[4:*]], strlen(modlib)+1), 2)
nl = strpos(clist, ' ')
cnames =strarr(nm)
for j = 0, nm-5 do cnames[j] = strmid(clist[j], 0, nl[j])
    
if ~widget_info(/valid, group) then begin
    for j = 0, nm-1 do print, j, clist[j], format = "(I0,') ',A)"
    isel = 0
    read, "Enter codec number to choose or -1 for none :_", isel
    if isel lt 0 then return, ''
    return, cnames[isel]
endif

base = widget_base(group = group, $
                   /modal, $
                   /column)

lab = widget_label(base, $
                   value = "Select codec for transcode")

junk = widget_list(base, $
                   value = clist, $
                   uvalue = 'CODEC', $
                   ysize = nm < 12)

jb = widget_base(base, $
                 /row)

dobut = widget_button(jb, $
                      value = 'Apply', $
                      uvalue = 'DO', $
                      sensitive = 0)

junk = widget_button(jb, $
                     value = 'Cancel', $
                     uvalue = 'DONT')

state = ptr_new({isel: 0l, $
                 dobut: dobut})

widget_control, lab, set_uvalue = state

widget_control, base, /real

xmanager, 'list_codecs', base

if (*state).isel lt 0 then rv = '' $
else rv = cnames[(*state).isel]

ptr_free, state

return, rv

end
