Viewing contents of file '../idllib/contrib/tappin/graffer/gr_disp_man.pro'
;+
; GR_DISP_MAN
;
; Usage:
;	gr_disp_man
;
; Keyword:
;	initialise	input	If set & non-zero, then initialize the
;				system otherwise display help.
; History:
;	Original: 19/12/96; SJT
;	Modify reading of help file to speed it up: 18/3/97; SJT
;-

pro Grf_man_event, event

common Graff_manual, helptext, nlines, toptext, toplocs, current_topic

widget_control, event.id, get_uvalue = but
widget_control, widget_info(event.top, /child), get_uvalue = ids

case but of
    'QUIT': widget_control, event.top, /destroy
    
    'TOPIC': begin
        current_topic = event.index
        widget_control, ids.help, set_text_top_line = $
          toplocs(current_topic)
    end
endcase

end

pro Gr_man_display, topid

common Graff_manual, helptext, nlines, toptext, toplocs, current_topic

if (current_topic eq -1) then begin
    message = ['The GRAFFER help system',  $
               'has not been found',  $
               'sorry about that']
    base = widget_base(/column, title = 'GRAFFER: No Help', resource = $
                       'Graffer')
    junk = widget_text(base, value = message, xsize = $
                       max(strlen(message)), ysize = n_elements(message))
    junk = widget_button(base, value = 'Dismiss', uvalue = 'QUIT')
    
    widget_control, base, /real
    
    xmanager, 'grf_man', base, /modal, group = topid
    
endif else begin
    
    if (xregistered('grf_man')) then return
    
    tlb = widget_base(title = 'Graffer Help system', resource = $
                      'Graffer')
    base = widget_base(tlb, /column, xpad = 1, ypad = 1, space = 1)
    
    base1 = widget_base(base, /row)
    jb = widget_base(base1, /column)
    junk = widget_label(jb, value = 'Select topic')
    listid = widget_list(jb, value = toptext, uvalue = 'TOPIC', $
                         ysize = 22)
    widget_control, listid, set_list_select = current_topic
    
    jb = widget_base(base1, /column)
    junk = widget_label(jb, value = 'Help text')
    hlpid = widget_text(jb, xsize = 80, ysize = 25, /scroll, value = $
                        helptext)
    widget_control, hlpid, set_text_top_line = toplocs(current_topic)
    
    junk = widget_button(base, value = 'Dismiss', uvalue = 'QUIT')
    
    widget_control, base, set_uvalue = {list:listid, help:hlpid}
    
    widget_control, tlb, /real
    xmanager, 'grf_man', tlb, group = topid
endelse

end
    
pro Gr_man_get, jnln, js, docpath, file, istart, istartt, igot

common Graff_manual, helptext, nlines, toptext, toplocs, current_topic

helpfile = docpath+file

on_ioerror, no_help
openr, ilu, helpfile, /get

inln = ''
lvlsp = ['', '', '  ', '    ']
lvlul = ['', '=', '-', '']

while not eof(ilu) do begin
    readf, ilu, inln
    
    if (strpos(inln, '#BEGIN') eq 0) then begin
        inln = strtrim(strmid(inln, 6, strlen(inln)), 1) ; N.B. trim
                                ; leading spaces
        lvl = fix(inln)
        inln = strtrim(strmid(inln, strpos(inln, ' '), $
                              strlen(inln)), 2) ; N.B. trim
        if (lvl eq 1) then begin
            inln = string(js, format = "(I0,') ')") + inln
            js = js+1
        end
                                ; leading & trailing spaces
        if (istartt) then begin
            toptext = lvlsp(lvl)+inln
            toplocs = jnln
            istartt = 0
        endif else begin
            toptext = [temporary(toptext), lvlsp(lvl)+inln]
            toplocs = [temporary(toplocs), jnln]
        endelse
    endif else if (strpos(inln, '#INC') eq 0) then begin
        hf = strtrim(strmid(inln, 4, strlen(inln)), 2)
        gr_man_get, jnln, js, docpath, hf, istart, istartt, igot
        goto, eol
    endif else lvl = 0
    
    helptext(jnln) = inln
    jnln = jnln+1
    if (jnln eq nlines) then begin
        helptext = [temporary(helptext), strarr(250)]
        nlines = nlines+250
    endif
    if (lvl ne 0 and lvlul(lvl) ne '') then begin
        helptext(jnln) = string(replicate((byte(lvlul(lvl)))(0), $
                                          strlen(inln)))
        jnln = jnln+1
        if (jnln eq nlines) then begin
            helptext = [temporary(helptext), strarr(250)]
            nlines = nlines+250
        endif
    endif
    
Eol:
    
endwhile

free_lun, ilu
igot = 1
return

No_help:
if (n_elements(ilu) ne 0) then free_lun, ilu
igot = 0
return

end

pro Gr_man_init

common Graff_manual, helptext, nlines, toptext, toplocs, current_topic

                                ; Determine the path for the help
                                ; files.

help, calls = cstack
ltpos = strpos(cstack(0), '<')+1
rtpos = strpos(cstack(0), 'gr_disp_man.pro')
len = rtpos - ltpos
case !version.os of
    'MacOS': docpath = strmid(cstack(0), ltpos, len) + 'Docs\' 
    'windows': docpath = strmid(cstack(0), ltpos, len) + 'Docs\' 
    'Win32': docpath = strmid(cstack(0), ltpos, len) + 'Docs\' 
    'vms': docpath = strmid(cstack(0), ltpos, len-1) + '.DOCS]'
    Else: docpath = strmid(cstack(0), ltpos, len) + 'Docs/'
endcase

istart = 1
istartt = 1

jnln = 0l
js = 1
helptext = strarr(1000)
nlines = 1000

gr_man_get, jnln, js, docpath, 'Help.txt', istart, istartt, igot

if (igot) then begin
    current_topic = 0
    helptext = helptext(0:jnln-1)
endif else current_topic = -1

end

pro Gr_disp_man, topid, initialize=initialize

if (keyword_set(initialize)) then gr_man_init $
else gr_man_display, topid

end