;view widget maker, 12/22/99
;2/16/2008 - use for viewts and viewsw versions also, decide by
;window #, normals are 0-14, ts are 20-24, sw are 24-28
 ;;echo
 win = $win
 wtype = 0
 if win ge 20 wtype = 1
 if win ge 25 wtype = 2
 sw = string(win)		;a bit indirect
 viq = xmtoplevel_form(0,0,'X' )		;now a top level
 equate, eval('$viewer'+sw), viq

 s1 = 'File'	s2 = 'Display'	s3='Options'   s5='Help'
 bar = xmmenubar(viq, $f4, 'gray',s1, s2, s3, s5)
 xmattach, bar(0), 0, 1,1,1,0
 xmsetmnemonic, bar(1), 'F'
 xmsetmnemonic, bar(2), 'D'
 xmsetmnemonic, bar(3), 'O'
 xmsetmnemonic, bar(4), 'H'
 ;pull down menu for file
 s1 = 'load file'
 s2 = 'save'
 ;s3 = 'save full data'
 ;s4 = 'print'
 s5 = 'hide'
 s10 = 'destroy'
 ;different display options for ts and sw views
 ;;4/24/2008 - remove print option until we fix it, note that ts and sw types
 ;don't have a load file option
 ncase wtype
   { sq = 'view_file,'+sw	pd1=xmpulldownmenu(bar(0), bar(1), sq, $f4, 'gray', s1,s2,s5,s10 }
   { sq = 'view_tsfile,'+sw	pd1=xmpulldownmenu(bar(0), bar(1), sq, $f4, 'gray', s2,s5,s10 }
   { sq = 'view_swfile,'+sw	pd1=xmpulldownmenu(bar(0), bar(1), sq, $f4, 'gray', s2,s5,s10 }
 endcase
 ;;pd1=xmpulldownmenu(bar(0), bar(1), sq, $f4, 'gray', s1,s2,s4,s5,s10)

 if wtype eq 0 then {
   ;pull down menu for display
   s0 = 'despiker'
   s1 = 'image mapping'	s2 = 'connection set'
   s3 = 'view range'	s4 = 'lat/long'
   s5 = 'time box'	s6 = 'noise (EMI) removal'
   s7 = 'redraw'		s8 = '>draw grid'
   s9 = 'erase'		s10 = 'hide/show info box'
   pd1=xmpulldownmenu(bar(0),bar(2),'view_display,'+sw,$f4,'gray',s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10)
   ;the grid sub-menus
   s1 = '5'	s2 = '10'	s3 = '20'	s4 = '50'	s5 = '100'
   s6 = '>grid color'
   pr1=xmpulldownmenu(pd1(0),pd1(9),'grid_cb,'+sw, $f4, 'gray',s1,s2,s3,s4,s5,s6)
   ;the grid colors
   s1 = 'black'	s2 = 'white'	s3 = 'red'	s4 = 'yellow'	s5 = 'green'
   pr2=xmpulldownmenu(pr1(0),pr1(6),'grid_color_cb', $f4, 'gray',s1,s2,s3,s4,s5)

   ;pull down menu for options
   s0 = 'statistics'
   s1 = 'magnify'
   s2 = 'bin pixels'
   s3 = 'rotate'
   s4 = 'undo last'
   pd1=xmpulldownmenu(bar(0), bar(3), 'view_option,'+sw, $f4, 'gray',s0,s1,s2,s3,s4)
   ;pull down menu for help
   s1 = 'General Instructions'
   s2 = '>help by topic'
   pd1=xmpulldownmenu(bar(0), bar(4), 'view_help,'+sw, $f4, 'gray', s1, s2)
   s1 = 'general'
   s2 = 'file'
   s3 = 'display'
   s4 = 'options'
   pr1=xmpulldownmenu(pd1(0), pd1(2), 'menu_topic', $f4, 'gray',s1,s2,s3,s4)
 } else if wtype eq 1 then {
   ;pull down menu for display, these options differ from image viewer
   s1 = 'image mapping'
   s2 = 'view range'
   s3 = 'redraw'
   s4 = 'erase'
   s5 = 'hide/show info box'
   sq = 'viewts_display,'
   pd1=xmpulldownmenu(bar(0),bar(2),sq+sw,$f4,'gray',s1,s2,s3,s4,s5)

   ;pull down menu for options
   s0 = 'no options yet'
   pd1=xmpulldownmenu(bar(0), bar(3), 'viewts_option,'+sw, $f4, 'gray',s0)
   ;pull down menu for help
   s1 = 'General Instructions'
   s2 = 'time slices'
   pd1=xmpulldownmenu(bar(0), bar(4), 'viewts_help,'+sw, $f4, 'gray', s1, s2)
   ;also we manage the scaling and bypass the ftsc in view_part1
   $view_scaletype(win) = 5
 } else if wtype eq 2 then {
   ;these are the SP slice views
   ;mostly same options for ts and sw types at present
   ;pull down menu for display, these options differ from image viewer
   s1 = 'image mapping'
   s2 = 'view range'
   s3 = 'redraw'
   s4 = 'erase'
   s5 = 'hide/show info box'
   sq = 'viewts_display,'
   pd1=xmpulldownmenu(bar(0),bar(2),sq+sw,$f4,'gray',s1,s2,s3,s4,s5)

   ;pull down menu for options
   s0 = 'line plots'
   pd1=xmpulldownmenu(bar(0), bar(3), 'viewsp_option,'+sw, $f4, 'gray',s0)
   ;pull down menu for help
   s1 = 'General Instructions'
   pd1=xmpulldownmenu(bar(0), bar(4), 'viewsp_help,'+sw, $f4, 'gray', s1)
   ;also we manage the scaling and bypass the ftsc in view_part1
   $view_scaletype(win) = 5
 }
 ;get a board for the top, this holds the orientation, zoom, text box, ...
 bq = xmboard(viq, 0, 0, 0, 0)
 ;name of the orientation pixmaps depends on type
 sq = getenv('ANA_PIXMAPS')
 ncase wtype
   sq = sq + '/orientate.'
   sq = sq + '/orientatets.'
   sq = sq + '/orientatesw.'
 endcase
 for i=0,7 do {
   equate, eval('s'+string(i+1)), sq+string(i)
 }
 dm = 0
 $view_order(win) = 0		;10/24/97 make default 0
 op2=xmpixmapoptionmenu(bq,'orient_cb,'+sw,$f1,'','',s1,s2,s3,s4,s5,s6,s7,s8,dm)
 if wtype eq 2 then xmposition, op2(0), 0, 3 else xmposition, op2(0), 0, 5
 ;to support connected windows, we have to save the orientation widget numbers
 $view_order_widgets(0,win) = op2(0:8)	;better be just 9 elements

 s00 = smap(byte(188))	s0 = smap(byte(189))
 ;zoom varies with wtype
 ty,'wtype = ', wtype
 if wtype eq 0 then {
   ;radio buttons for zoom for wtype 0
   s1 = '1'  s2 = '2'  s3 = '3'  s4 = '4' s5 = '8'  s6 = '16' 
   zq = xmradiobox(bq,'viewzoom_cb,'+sw,$f4,'',s00,s0,s1,s2,s3,s4,s5,s6, 4)
   xmposition,zq(0), 90, 0
   for i=1,8 do xmselectcolor,zq(i),'red'
   ;also save the zoom widgets so programs can press the buttons
   $view_zoom_widgets(0,win) = zq(1:*)
   zm = $view_zoom_fac(win)  ;preset to one but for a copy we get copied value
   xmtogglesetstate, zq(3), 1, 0
 }
 if wtype eq 1 then {
   ;the x zoom is done as an option menu here
   s1 = '1'  s2 = '2'  s3 = '3'  s4 = '4' s5 = '8'  s6 = '16' s7 = 'entry'
   iq = xmoptionmenu(bq, 'ts_xzoom_cb,'+sw,$f4,'','s  zoom',s00,s0,s1,s2,s3,s4,s5,s6,s7,2)
   xmposition, iq(0), 90, 5
   $viewts_space_zoom_fac($win) = 1.0	;not the button number but the actual zoom value
   ;for the arbitrary entry we have a text box, normally unmanaged
   xq = xmtextfield(bq,'',4,'ts_xzoomentry_cb,'+sw,$f3,'white')
   xmposition, xq, 230, 5, 50, 33
   xtunmanage, xq
   $viewts_text_widgets(1, $win) = xq
   ;the time scale is specified via a text entry box, it will be blank here and
   ;loadable by the user or set to mean time interval
   xmposition, xmlabel(bq,'t scale',$f4), 92, 47
   xq = xmtextfield(bq,'',10,'tstscale_cb,'+sw,$f3,'white')
   xmposition, xq, 151, 42, 70, 35
   $viewts_text_widgets(0, $win) = xq
   xmposition, xmlabel(bq,'s/pixel',$f4), 230, 47
   $viewts_t_scale($win) = -1	;-1 for native pixels, 0 for mean time
   $view_zoom_widgets(0,$win) = iq(1:*)
   zm = $viewts_space_zoom_fac($win)  ;preset to one but for a copy we get copied value
 }
 if wtype eq 2 then {
   ;we share some arrays with the ts views (wtype=1)
   ;the s zoom is done as an option menu here, similar to x zoom in ts
   s1 = '1'  s2 = '2'  s3 = '3'  s4 = '4' s5 = '8'  s6 = '16' s7 = 'entry'
   ;we use the same callback as the ts views
   iq = xmoptionmenu(bq, 'sp_szoom_cb,'+sw,$f4,'','s  zoom',s00,s0,s1,s2,s3,s4,s5,s6,s7,2)
   xmposition, iq(0), 80, 3
   $viewts_space_zoom_fac($win) = 1.0	;not the button number but the actual zoom value
   ;for the arbitrary entry we have a text box, normally unmanaged
   xq = xmtextfield(bq,'',4,'sp_szoomentry_cb,'+sw,$f3,'white')
   xmposition, xq, 220, 6, 50, 33
   xtunmanage, xq
   $viewts_text_widgets(1, $win) = xq
   $viewts_t_scale($win) = -1	;-1 for native pixels, 0 for mean time
   $view_zoom_widgets(0,$win) = iq(1:*)
   zm = $viewts_space_zoom_fac($win)  ;preset to one but for a copy we get copied value
   ;;remove these 3/24/2008, not very useful
   ;selectors for IQUV
   ;;xmposition, xmlabel(bq,'show', $f4), 83, 44
   ;;s1 = 'I'  s2 = 'Q'  s3 = 'U'  s4 = 'V'
   ;;tq = xmcheckbox(bq,'spselectiquv_cb,'+sw, $f4, '', s1,s2,s3,s4,4)
   ;;for i=1,4 do { xmselectcolor,tq(i),'red'  xmtogglesetstate, tq(i), 1, 0 }
   ;;$spiquvmask = zero(intarr(4))+1
   ;;xmposition, tq(0), 135, 39

   ;text box for wavelength index
   xmposition, xmlabel(bq,'W', $f4), 380, 27
   xq = xmtextfield(bq,string($spxywaveindex),3,'spwindex_cb,'+sw,$f3,'white')
   xmposition, xq, 405, 21, 50, 33
   $viewts_text_widgets(0, $win) = xq
   b1 = xmbutton(bq, 's','spwscrollflip_cb,'+sw,$f10,'gray')
   equate, eval('$spwavescroll'+sw), b1
   xmposition, b1, 390, 2, 20, 19
   xmposition, xmarrow(bq, 0, 'spwindexdn_cb,'+sw), 415, 2, 19, 19
   xmposition, xmarrow(bq, 1, 'spwindexup_cb,'+sw),   435,  2, 19, 19

   ;text box for space index
   xmposition, xmlabel(bq,'S', $f4), 382, 63
   xq = xmtextfield(bq,'',4,'spsindex_cb,'+sw,$f3,'white')
   xmposition, xq, 405, 57, 50, 33
   $viewts_text_widgets(2, $win) = xq
   b1 = xmbutton(bq, 's','spsscrollflip_cb,'+sw,$f10,'gray')
   equate, eval('$spspacescroll'+sw), b1
   xmposition, b1, 457, 33, 19, 20
   xmposition, xmarrow(bq, 2, 'spsindexdn_cb,'+sw), 457, 55, 19, 19
   xmposition, xmarrow(bq, 3, 'spsindexup_cb,'+sw),   457,  74, 19, 19

   ;and a button to redraw the slice
   xmposition, xmbutton(bq,'redraw slice','spdrawslices_cb,'+sw,$f4,'gray'), 275, 3, 0, 33
   ;and some for drawing operations, omitted for now (11/7/2008)
   ;;xmposition, xmbutton(bq,'plot stack','spdrawstack_cb,'+sw,$f4,'gray'), 83, 57, 0, 33
   ;instead (11/7/2008) add a P type option
   s1 = 'QUV'  s2 = 'QU'   s3 = 'QUV+'  s4 = 'QU+'
   iq = xmoptionmenu(bq, 'spxyw_p_type_cb,'+sw,$f4,'','P style',s1,s2,s3,s4,1)
   $spxywphasv = 0
   $spxypmode = 0
   xmposition, iq(0), 5, 57
   
   ;and some more for Alan's w sum and mirror option
   lq = xmlabel(bq,'V sum\nwidth', $f11)
   xmposition, lq, 179, 58
   xmalignment, lq, 1
   xq = xmtextfield(bq,'1',4,'spwindex_cb,'+sw,$f3,'white')
   xmposition, xq, 227, 57, 45, 33
   equate, eval('$spwavesumrange'+sw), xq
   lq = xmlabel(bq,'V mirror\ncenter', $f11)
   xmposition, lq, 278, 58
   xmalignment, lq, 1
   xq = xmtextfield(bq,'0',4,'spwindex_cb,'+sw,$f3,'white')
   xmposition, xq, 330, 57, 45, 33
   equate, eval('$spwavelc'+sw), xq
   zq = xmradiobox(bq,'mirrorsign_cb,'+sw,$f4,'','+','-', 2)
   xmposition,zq(0), 290, 30
   for i=1,2 do xmselectcolor,zq(i),'red'
   xmtogglesetstate, zq(2), 1, 0
   $spxywavemirrorsign = 1
 }
 ;a label for image info, esp for TRACE
 $view_text_widget(win) = xmtextbox(bq, 5, 30, $f8,'')
 xmtextseteditable, $view_text_widget(win), 1
 xmposition, $view_text_widget(win), 282, 0

 sc = xmscrolledwindowapp(viq, 4, 4)
 formvstack, bar(0), bq, sc
 xmattach, sc, 0, 1,1,0,1

 ;limit initial size of this window to 512x512
 ix = $view_nx(win) < 512
 if wtype then iy = $view_ny(win) < 768 else iy = $view_ny(win) < 512
 $view_xsize(win) =ix
 $view_width(win) =ix
 $view_ysize(win) =iy
 $view_height(win) =iy
 ;a special input call back for SP slice views
 if wtype eq 2 then {
   $view_draw(win)=xmdrawingarea(sc,win,ix,iy,'viewsp_input_cb,'+sw,'view_resize_cb,'+sw,'view_expose_cb,'+sw)
 } else {
   $view_draw(win)=xmdrawingarea(sc,win,ix,iy,'view_input_cb,'+sw,'view_resize_cb,'+sw,'view_expose_cb,'+sw)
 }
 iq = ix/zm
 ;are we part of a connected set of views ?
 if $view_connect(win) then {
   nxs = $view_super_nx nys = $view_super_ny
 } else {  nxs = $view_nx(win)  nys = $view_ny(win) }
 $view_sch(win) = xmhscrollbar(sc, nxs, iq, .5*iq, 'view_scrollh_cb,'+sw, 1)
 iq = iy/zm
 $view_scv(win) = xmvscrollbar(sc, nys, iq, .5*iq, 'view_scrollv_cb,'+sw, 1)
 xmsize, $view_sch(win), ix, 20
 xmsize, $view_scv(win), 20, iy
 $view_ix(win) = 0
 $view_iy(win) = 0
 ;also set the center, we want to try to maintain the center when zooming
 ;and changing orientation
 $view_ixc(win) = ix/2
 $view_iyc(win) = iy/2

 scanpositions, viq, 'viewer'+sw, $bw_ix, $bw_iy
 xtpopup, viq
 xmposition, viq, $bw_ix, $bw_iy + $magic_mac_yoffset
 ;;ty,'mark 8'
 xtloop, 2
 ty,'done in view_widgettool ******************'