Viewing contents of file '../idllib/contrib/tappin/graffer/graff_text.pro'
;+
; GRAFF_TEXT
;	Add text to a graffer plot.
;
; Usage:
;	ichange = graff_text(pdefs)
;
; Return value
;	ichange	int	1 if changed, 0 if not
;
; Argument:
;	pdefs	struct	in/out	Structure containing all the info
;				about the plot.
;
; History:
;	Original: 16/8/95; SJT
;	Add timer event to push to front if obscured: 23/8/95; SJT
;	Add a tracker help window like that in the main panel: 9/8/96;
;	SJT
;	Add a cancel button: 13/9/96; SJT
;	Modify to display the text in a window on the pop-up (for
;	speed of response with complex plots): 6/11/96; SJT
;	Made to function returning "cancel" state: 18/12/96; SJT
;	Add CAPTURE key to text inputs: 6/2/97; SJT
;-

pro Gr_text_disp, text

erase

opf = !P.font
if (text.font eq -1) then begin
    !P.font = 0
    tf = text.text
endif else begin
    !P.font = -1
    tf = '!'+string(text.font, format = "(I0)")+ $
      text.text+'!3'
endelse

xyouts, /norm, .05+.9*text.align, .1, tf, color = text.colour, $
  charsize = text.size, charthick = text.thick, align = text.align

!P.font = opf

usersym, [-.5, 0., 0., 0., .5], [-.866, 0., -1.77, 0., -.866]
plots, /norm, psym = 8, .05+.9*text.align, .1

end

function Grt_event, event

widget_control, event.id, get_uvalue = but
widget_control, event.handler, get_uvalue = pdefs, /no_copy

iexit = 0
idraw = 1

track_flag = strpos(tag_names(event, /struct), 'TRACK') ne -1
if (track_flag) then begin
    idraw = 0
    if (event.enter eq 0) then begin
        graff_msg, pdefs.ids.popmsg, ''
        goto, miss_case
    endif
endif

    
handle_value, pdefs.text, text, /no_copy

itxt = pdefs.transient.imove

case but of
    'DONE': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Text operation finished' $
    else begin
        iexit = 1
        idraw = 0b
    end
    
    'CANCEL': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Cancel text operation' $
    else begin
        iexit = -1
        idraw = 0b
    end
    
    'UPDATE': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Update screen' $
    else begin
        wc = !D.window
        wset, pdefs.ids.windex
        handle_value, pdefs.text, text, /set
        gr_plot_object, pdefs
        wset, wc
    endelse
    
    'TEXT': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Enter the text string' $
    else text(itxt).text = event.value
    
    'CHS': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Set the character size' $
    else text(itxt).size = event.value
    'COL': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Select the text colour' $
    else text(itxt).colour = event.value
    
    'JUST': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Select text justification' $
    else if (event.value le 2) then $
      text(itxt).align = float(event.value)/2. $
    else text(itxt).align = graff_just(text(itxt).align, $
                                       pdefs.popflag, group = event.top)
    
    'ORI': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Set orientation (Degrees anti clockwise)' $
    else text(itxt).orient = event.value
    'THI': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Select line width to draw text' $
    else text(itxt).thick = event.value
    
    'TX': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Adjust X position of anchor' $
    else text(itxt).x = event.value
    'TY': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Adjust Y position of anchor' $
    else text(itxt).y = event.value
    
    'TNORM': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Select data or viewport (normal) ' + $
      'coordinates' $
    else begin
        jb = widget_info(event.id, /parent)
        widget_control, jb, get_uvalue = poss
        cn = text(itxt).norm
        
        text(itxt).norm = event.value
        
        case event.value of
            0: case cn of
                0: dmy = [text(itxt).x, text(itxt).y]
                1: dmy = convert_coord(text(itxt).x, text(itxt).y, $
                                       /to_data, /norm)
                2: begin
                    gr_fra2norm, text(itxt).x, text(itxt).y, xn, yn
                    dmy = convert_coord(xn, yn, /to_data, /norm)
                end
            endcase
            1: case cn of
                0: dmy = convert_coord(text(itxt).x, text(itxt).y, $
                                        /data, /to_norm)
                1: dmy = [text(itxt).x, text(itxt).y]
                2: begin
                    gr_fra2norm, text(itxt).x, text(itxt).y, xn, yn
                    dmy = [xn, yn]
                end
            endcase
            2: case cn of
                0: begin
                    dmy = convert_coord(text(itxt).x, text(itxt).y, $
                                        /data, /to_norm)
                    gr_fra2norm, dmy(0), dmy(1), xf, yf, /invert
                    dmy = [xf, yf]
                end
                1: begin
                    gr_fra2norm, text(itxt).x, text(itxt).y, xf, yf, /invert
                    dmy = [xf, yf]
                end
                2: dmy = [text(itxt).x, text(itxt).y]
            endcase
        endcase
        
        text(itxt).x = dmy(0)
        text(itxt).y = dmy(1)
        widget_control, poss(0), set_value = dmy(0)
        widget_control, poss(1), set_value = dmy(1)
    end
    
    'FONT': if (track_flag) then  $
      graff_msg, pdefs.ids.popmsg, 'Select text font' $
    else text(itxt).font = event.value

    'POP': begin
        widget_control, event.top, /show
        widget_control, event.id, timer = 2.
        idraw = 0b
    end
endcase

if (idraw) then gr_text_disp, text(itxt)
handle_value, pdefs.text, text, /no_copy, /set

Miss_case:

widget_control, event.handler, set_uvalue = pdefs, /no_copy

return, {id:event.id, top:event.top, handler:event.handler, exited:iexit}

end

function Graff_text, pdefs, edit=edit, position=position

handle_value, pdefs.text, text, /no_copy

old_text = text                 ; Save current settings
old_ntext = pdefs.ntext         ; in case of a cancel

if (n_elements(edit) ne 0) then itxt = edit $
else begin
    if (pdefs.ntext ne 0) then text = [text, pdefs.text_options]
    itxt = pdefs.ntext
    text(itxt).x = position(0)
    text(itxt).y = position(1)
    pdefs.ntext = pdefs.ntext+1
endelse
pdefs.transient.imove = itxt

widget_control, pdefs.ids.graffer, sensitive = 0

tlb = widget_base(title = 'Graffer text', group_leader = $
                  pdefs.ids.graffer, resource = 'Graffer')
base = widget_base(tlb, /column)

                                ; The actual text

showit = widget_draw(base, ysize = 25, xsize = 320, /frame)

junk = graff_enter(base, /all_ev, xsize = 40, value = text(itxt).text, $
                   uvalue = 'TEXT', label = 'Text:', /track, /capture, $
                   /graphics)

                                ; Size & colour

jb = widget_base(base, /row)

junk = graff_enter(jb, /float, /all_ev, xsize = 6, value = $
                   text(itxt).size, format = "(f6.1)", uvalue = $
                   'CHS', label = 'Charsize:', /track, /capture)


col_list = ['White (bg)', $
            'Black', $
            'Red', $
            'Green', $
            'Blue', $
            'Cyan', $
            'Magenta', $
            'Yellow', $
            'Orange', $
            '#7f ff 00', $
            '#00 ff 7f', $
            '#00 7f ff', $
            '#7f 00 ff', $
            'Mauve', $
            'Dark Grey', $
            'Light Grey']

junk = cw_bbselector(jb, col_list, uvalue = 'COL', $
                     set_value = text(itxt).colour, $
                     label_left = 'Colour:', /track)

junk = graff_enter(jb, /int, /all_ev, xsize = 2, value = $
                   text(itxt).thick, format = "(I2)", uvalue = $
                   'THI', label = 'Thickness:', /track, /capture)

                                ; Alignment and orientation

case (text(itxt).align) of
    0.: ial = 0
    0.5: ial = 1
    1.0: ial = 2
    Else: ial = 3
end

jb = widget_base(base, /row)
junk = cw_bbselector(jb, ['Left', 'Centre', 'Right', 'Other ...'], $
                     set_value = ial, uvalue = 'JUST', label_left = $
                     'Justification:', /track)

junk = graff_enter(jb, /float, /all_event, xsize = 6, uvalue = 'ORI', $
                   value = text(itxt).orient, format = "(f6.1)",  $
                   label = 'Orientation (°):', /track, /capture)

                                ; Position

pb = widget_base(base, /row, uvalue = 'POP')

xpos = graff_enter(pb, /float, /all_event, xsize = 11, uvalue = 'TX', $
                   value = text(itxt).x, format = "(g11.4)", label = $
                   'Position:  X:', /track, /capture) 
ypos = graff_enter(pb, /float, /all_event, xsize = 11, uvalue = 'TY', $
                   value = text(itxt).y, format = "(g11.4)", label = $
                   'Y:', /track, /capture)

jb = widget_base(base, /row)

jjb = widget_base(jb, uvalue = [xpos, ypos])
junk = cw_bbselector(jjb, ['Data', 'Normal', '"Frame"'], set_value = $
                     text(itxt).norm, uvalue = 'TNORM', label_left = $
                     'Coordinates:', /track)

                                ; Fonts

font_list = ['Hardware', $
             'Simplex Roman', $
             'Simplex Greek',  $
             'Duplex Roman',  $
             'Complex Roman',  $
             'Complex Greek',  $
             'Complex Italic',  $
             'Maths & Special',  $
             'Special',  $
             'Gothic English',  $
             'Simplex Script',  $
             'Complex Script',  $
             'Gothic Italian',  $
             'Gothic German',  $
             'Cyrillic',  $
             'Triplex Roman',  $
             'Triplex Italic',  $
             'Miscellaneous']

fnos = [-1, indgen(16)+3, 20]
junk = cw_bbselector(jb, font_list, uvalue = 'FONT', $
                     return_uvalue = fnos, label_left = 'Font:', $
                     set_value = 0 > (text(itxt).font-2) < 17, /track)


                                ; Messages (what does what)

pdefs.ids.popmsg = widget_text(base, value = '', xsize = 45)

                                ; Done

jb = widget_base(base, /row)
junk = widget_button(jb, value = '  Done  ', uvalue = 'DONE', /track)
junk = widget_button(jb, value = ' Cancel ', uvalue = 'CANCEL', /track)
junk = widget_button(jb, value = ' Update ', uvalue = 'UPDATE', /track)

txtemp = text(itxt)

handle_value, pdefs.text, text, /no_copy, /set

use_timer = pdefs.popflag

widget_control, base, set_uvalue = pdefs, /no_copy

widget_control, tlb, /real

widget_control, showit, get_value = shno
wset, shno
gr_text_disp, txtemp

;	RYO widget management to allow us to get the values back from
;	the event handler without using a common block, even after the
;	hierarchy has been destroyed.

widget_control, base, event_func = 'grt_event'

if (use_timer) then widget_control, pb, timer = 2.

repeat begin
    ev = widget_event(base)
endrep until (ev.exited ne 0)

widget_control, base, get_uvalue = pdefs, /no_copy

wset, pdefs.ids.windex

widget_control, tlb, /destroy
if (ev.exited eq -1) then begin
    handle_value, pdefs.text, old_text, /set, /no_copy
    pdefs.ntext = old_ntext
    ichange = 0
endif else begin
    handle_value, pdefs.text, text, /no_copy
    pdefs.text_options = text(itxt)
    pdefs.text_options.text = ''
    handle_value, pdefs.text, text, /no_copy, /set
    ichange = 1
endelse

gr_plot_object, pdefs

widget_control, pdefs.ids.graffer, sensitive = 1

return, ichange

end