home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / widfile.pro < prev    next >
Encoding:
Text File  |  1997-07-08  |  21.8 KB  |  870 lines

  1. ;
  2. ; $Id: widfile.pro,v 1.18 1997/01/15 03:11:50 ali Exp $
  3. ;
  4. ;  WidFile
  5. ;   Widget File class library
  6. ;
  7. ; Copyright (c) 1993-1997, Research Systems, Inc.  All rights reserved.
  8. ;   Unauthorized reproduction prohibited.
  9. ;
  10. ; MODIFICATION HISTORY
  11. ;       Written by:     Joshua Goldstein,       12/93
  12. ;
  13. ;
  14.  
  15.  
  16. ;
  17. ;  Currpath
  18. ;   Return the current path. If there is no current path, chose
  19. ;   a reasonable default path. (I use the current directory)
  20. ;
  21. FUNCTION CurrPath
  22.  
  23.   COMMON WidEd_Comm
  24.  
  25.     IF FileDir NE '' THEN RETURN, FileDir       ; FileDir non nil
  26.  
  27.     CASE !Version.OS OF                         ; Use default (OS dependent)
  28.     'vms':  RETURN,'[]'                         ; VAX VMS
  29.     'Win32':  RETURN,'.'                      ; PC
  30.     'MacOS':  RETURN,''                         ; Macintosh
  31.     ELSE:   RETURN,'.'                          ; UNIX
  32.     ENDCASE
  33. END
  34.  
  35.  
  36. ;
  37. ;  ExtractSimpleName
  38. ;   Given a full file name, extract the simple filename without extention
  39. ;   or directory path.  See examples below:
  40. ;
  41. ;   foo.pro                 -> foo
  42. ;   /ugh/bar/foo.pro        -> foo
  43. ;   c:foo.wid               -> foo
  44. ;   a:\b\c\d\e\foo.pro      -> foo
  45. ;   xx:[IDL.JOSH]FOO.PRO    -> FOO
  46. ;
  47. FUNCTION ExtractSimpleName, File, START=Start
  48.  
  49.     Idx = WHERE(!Version.OS EQ [ "vms", "Win32", "MacOS" ])
  50.     Idx = Idx[0] + 1
  51.  
  52.                 ;   Unix, VMS, WIN, MAC
  53.     First       = ([ '/', ']', '\', ':'])[Idx]
  54.     Second      = ([ '',  ':', ':', ''])[Idx]
  55.     ExtCh       = ([ '.', '.', '.', '.'])[Idx]
  56.     ExtCh2      = ([ '',  ';', '',  ''])[Idx]
  57.  
  58.     ;   Find end of path and/or disk/volume descriptor.
  59.  
  60.     Start       = RSTRPOS(File, First) + 1
  61.     IF Start EQ 0 AND Second NE '' THEN Start = RSTRPOS(File, Second) + 1
  62.  
  63.     ;   Get filename.
  64.     Simple      = STRMID(File,Start,1000)
  65.  
  66.     ;   Remove ending if there might be one.
  67.     ;   Most have a '.' ending.  VMS might have a version too.
  68.  
  69.     IF ExtCh NE '' THEN BEGIN
  70.         Ext = RSTRPOS(Simple, ExtCh)
  71.         IF Ext EQ -1 AND ExtCh2 NE '' THEN Ext = RSTRPOS(Simple, ExtCh2)
  72.         IF Ext NE -1 THEN Simple = STRMID(Simple,0,Ext)
  73.     ENDIF
  74.  
  75.     RETURN, Simple      ; Return what we found.
  76. END
  77.  
  78.  
  79. ;
  80. ;  InsureExt
  81. ;   Make sure a file name has the correct ending
  82. ;
  83. PRO InsureExt, File, Ending
  84.  
  85.     Simple  = ExtractSimpleName( File, START=Start )
  86.  
  87.     File    = STRMID(File,0,Start) + Simple + Ending
  88. END
  89.  
  90. ;
  91. ;  InternalXprintf
  92. ;
  93. ;   Create a string given a list of arguments and a format string
  94. ;
  95. ;   Used to write less than a line because PRINTF, FORMAT=(..., $)
  96. ;   doesn't work on VMS for the file types we use.
  97. ;
  98. FUNCTION InternalXPrintf, n, a1, a2, a3, a4, a5, a6, a7, FORMAT=Fmt
  99.  
  100.     COMMON  XPrintf_Comm, OutputBuffer, ArgList
  101.  
  102.     DoFormat    = KEYWORD_SET(Fmt)
  103.     Buffer      = ""
  104.  
  105.     IF n EQ 0 THEN BEGIN
  106.         IF DoFormat EQ 0 THEN RETURN, ""
  107.         MESSAGE, 'STRING() required a positional argument'
  108.     ENDIF ELSE BEGIN
  109.  
  110.         StrCmd      = "Buffer = STRING(" + ArgList[ n ]
  111.         IF KEYWORD_SET(Fmt) THEN BEGIN
  112.             StrCmd  = StrCmd + ', FORMAT=Fmt )'
  113.         ENDIF ELSE BEGIN
  114.             StrCmd  = StrCmd + ')'
  115.         ENDELSE
  116.  
  117.     ENDELSE
  118.  
  119.     Dummy   = EXECUTE( StrCmd )
  120.     RETURN, Buffer
  121. END
  122.  
  123. ;
  124. ;  XPRINTF
  125. ;   VMS can't use FORMAT=(..., $) to have multiple I/O operations
  126. ;   write the same line of text.  At least not using the file types
  127. ;   we have.  Each I/O operation is its own line
  128. ;
  129. PRO XPRINTF, Unit, a1, a2, a3, a4, a5, a6, a7, $
  130.                                 FORMAT=Fmt, NO_EOL=NoNewline
  131.  
  132.     COMMON  XPrintf_Comm, OutputBuffer, ArgList
  133.  
  134.     n   = N_PARAMS() - 1
  135.  
  136.     IF KEYWORD_SET(NoNewline) THEN BEGIN
  137.         OutputBuffer    = OutputBuffer + $
  138.                 InternalXprintf( n, a1, a2, a3, a4, a5, a6, a7, FORMAT=Fmt )
  139.  
  140.     ENDIF ELSE BEGIN
  141.  
  142.         PRINTF, Unit, OutputBuffer + $
  143.                 InternalXprintf( n, a1, a2, a3, a4, a5, a6, a7, FORMAT=Fmt )
  144.         OutputBuffer    = ""
  145.  
  146.     ENDELSE
  147.  
  148. END
  149.  
  150. ;
  151. ;  GenWrite
  152. ;   General purpose object save routine.
  153. ;   Most objects can be written in one of two ways:
  154. ;       1. Just the object.
  155. ;       2. The object followed by a string array
  156. ;
  157. PRO GenWrite, Unit, Ptr, DEFAULT=Default
  158.  
  159.   COMMON WidEd_Comm
  160.  
  161.     ON_IOERROR, BadWrite
  162.  
  163.     Ptr2Obj, Ptr, Obj
  164.     WRITEU, Unit, Obj               ; Save basic information
  165.  
  166.     IF N_ELEMENTS(Default) NE 0 THEN BEGIN
  167.         GetValue, Obj, Names, Default   ; Use default if object has no value
  168.         WRITEU, Unit, N_ELEMENTS(Names) ; Save n_elements of value
  169.         WRITEU, Unit, Names             ; Save value
  170.     ENDIF
  171.  
  172.     Obj2Ptr, Obj, Ptr
  173.     RETURN
  174.  
  175.   BadWrite:
  176.     Dirty   = 2
  177.  
  178. END
  179.  
  180. ;
  181. ;  MISC_Restore
  182. ;   A great many of the objects are stored in a file in the same
  183. ;   way.  In fact, except for base objects EVERYTHING is stored
  184. ;   in on of 2 ways:  Just the object or an object with a <STRARR>
  185. ;   following it.  We handle both (if you tell us which it is)
  186. ;
  187. PRO MISC_Restore, Unit, Parent, Ptr, Type, HasValue1
  188.  
  189.     ; Get an object of the right sort
  190.     CALL_Procedure, Type+'_Alloc', Parent, Ptr
  191.     Ptr2Obj, Ptr, Obj
  192.  
  193.     IF HasValue1 THEN BEGIN
  194.  
  195.         SaveV1  = Obj.Value1        ; Save value pointer (crushed by read)
  196.         READU, Unit, Obj            ; Read in object
  197.  
  198.         N_Str   = 0                 ; Read in STRARR size and then strings
  199.         READU, Unit, N_str
  200.         Names   = STRARR(N_Str)
  201.         READU, Unit, Names
  202.  
  203.         Obj.Value1  = SaveV1        ; Restore saved value pointer
  204.         Obj2Ptr, Names, Obj.Value1  ; Store names in value pointer
  205.  
  206.     ENDIF ELSE BEGIN
  207.  
  208.         READU, Unit, Obj    ; Just object needs to be read
  209.  
  210.     ENDELSE
  211.  
  212.     Obj.Dialog  = 0         ; Clear old (and invalid) value
  213.     Obj.Next    = 0         ; Clear old (and invalid) value
  214.     Obj2Ptr, Obj, Ptr       ; Save what we've read
  215. END
  216.  
  217.  
  218. ;
  219. ;  FileNew
  220. ;   Function activated by the 'New' option on the File menu.
  221. ;   Destroys current widget tree and reinitializes top level base
  222. ;   to its starting state.  All dialogs (except main and possibly
  223. ;   paste are destroyed)
  224. ;
  225. PRO FileNew
  226.  
  227.   COMMON WidEd_Comm
  228.  
  229.     ;   Remove old preview bases
  230.  
  231.     FOR I=0,N_ELEMENTS(Bases)-1 DO BEGIN
  232.         B   = Bases[I]
  233.         IF B NE 0 AND WIDGET_INFO(B,/VALID_ID) THEN WIDGET_CONTROL, B, /DESTROY
  234.         Bases[I]    = 0
  235.     ENDFOR
  236.  
  237.     ;   Destroy any active dialog box widget copies
  238.  
  239.     FOR I=1,N_ELEMENTS(NewDialogs)-1 DO BEGIN
  240.         Ptr = NewDialogs[I].OldPtr
  241.         IF Ptr NE 0 THEN Destroy, Ptr
  242.     ENDFOR
  243.  
  244.     NewDialogs  = { WE_NEWOBJ, 0L, 0L, 0L }     ; No active dialogs
  245.     Dirty       = 0                             ; No change since 'New'
  246.  
  247.     ;   Close cut/copy/paste/edit boxes
  248.  
  249.     IF CutId NE 0 THEN BEGIN
  250.         WIDGET_CONTROL, CutId, /DESTROY
  251.         CutId   = 0
  252.     ENDIF
  253.  
  254.     IF CopyId NE 0 THEN BEGIN
  255.         WIDGET_CONTROL, CopyId, /DESTROY
  256.         CopyId  = 0
  257.     ENDIF
  258.  
  259.     IF PasteId NE 0 THEN BEGIN
  260.         WIDGET_CONTROL, PasteId, /DESTROY
  261.         PasteId = 0
  262.     ENDIF
  263.  
  264.     IF EditId NE 0 THEN BEGIN
  265.         WIDGET_CONTROL, EditId, /DESTROY
  266.         EditId  = 0
  267.     ENDIF
  268.  
  269.     LastId      = 1             ; Reset Id counter
  270.  
  271.     ;   Destroy widget tree and reinitialize main base to starting
  272.     ;   state.  Show preview of single top base to user.
  273.     IF N_ELEMENTS(TopPtr) THEN Destroy, TopPtr
  274.     Generate
  275.     UpdateEdit
  276. END
  277.  
  278.  
  279. ;
  280. ;  InternalFileOpen
  281. ;       Open a file without GUI garbage in front
  282. ;
  283. PRO InternalFileOpen, NewFile
  284.  
  285.   COMMON WidEd_Comm
  286.  
  287.     TmpLastId   = LastId        ; Save true LastId
  288.     LastId      = 1             ; Reset LastId.
  289.  
  290.     ;   Try to open the file
  291.     OPENR, Unit, NewFile, /GET_LUN, /XDR, ERROR = OpenError
  292.     IF OpenError NE 0 THEN BEGIN
  293.         ErrorDialog, TopDlg, ['Failed to open file:', NewFile ]
  294.         RETURN
  295.     ENDIF
  296.  
  297.     ;   Try to recover gracefully from errors encountered
  298.     ;   reading the file
  299.  
  300.     ON_IOERROR, Bad
  301.  
  302.     ;   Does it have the right header?
  303.  
  304.     Header  = 'WidgetEditFile'
  305.     TestHeader  = BYTE(Header)
  306.  
  307.     READU, Unit, TestHeader
  308.     IF STRING(TestHeader) NE Header THEN BEGIN
  309.         ErrorDialog, TopDlg, [ NewFile + ' is not', 'a valid .WID file']
  310.         RETURN
  311.     ENDIF
  312.  
  313.     ;   Get LastId
  314.  
  315.     READU, Unit, TmpLastId
  316.  
  317.     ;   Get Version Information (which we ignore)
  318.     Version     = 0L
  319.     READU, Unit, Version        ; Get Version
  320.     READU, Unit, Version        ; 4 unused bytes
  321.  
  322.  
  323.     ;   Indicate to the user this might take a while
  324.  
  325.     WIDGET_CONTROL, /HourGlass
  326.  
  327.     FileNew                     ; Destroy old widget tree
  328.     FileName    = NewFile       ; Set new filename
  329.     MAIN_Restore, Unit, 0L, TopPtr  ; Read in widget tree in file
  330.  
  331.     ;   Done reading.
  332.  
  333.     FREE_LUN, Unit      ; Close file.
  334.     Dirty   = 0         ; Clear dirty flag.  Consider this starting state
  335.     Generate            ; Show user preview of what we just read in.
  336.     UpdateEdit
  337.     UpdateMainDlg       ; Show it in main dlg
  338.     LastId  = TmpLastId
  339.     RETURN
  340.  
  341. Bad:
  342.  
  343.     ;   Close file.  Show user whatever we read.
  344.     FREE_LUN, Unit
  345.     ErrorDialog, TopDlg, 'File is corrupted or not a Widget Edit file'
  346.     Generate
  347.     UpdateEdit
  348.     LastId  = TmpLastId
  349. END
  350.  
  351.  
  352. ;
  353. ;  FileOpen
  354. ;   Get a filename from the user, read object data from that file.
  355. ;
  356. PRO FileOpen
  357.  
  358.   COMMON WidEd_Comm
  359.  
  360.     ;   Get filename choice from user.
  361.  
  362.     NewFile = pickfile(GROUP=TopDlg, /READ, $
  363.                 PATH=CurrPath(), FILTER='*.wid', $
  364.                 /MUST_EXIST, /NOCONFIRM, $
  365.                 GET_PATH=FileDir)
  366.  
  367.     ;   If the user hit Cancel in pickfile then just quit
  368.     IF NewFile EQ '' THEN RETURN
  369.  
  370.     ;   Certain 'special' operating systems can't handle a
  371.     ;   separator at the end of the pathname.
  372.  
  373.     IF !Version.OS EQ 'Win32' THEN BEGIN
  374.         FileDir = StrMid(FileDir, 0, StrLen(FileDir) - 1)
  375.     ENDIF
  376.  
  377.     ;   Add a .WID ending regardless of current ending
  378.     InsureExt, NewFile, ".wid"
  379.     InternalFileOpen, NewFile
  380. END
  381.  
  382.  
  383.  
  384. ;
  385. ;  FileSave
  386. ;   Prompt the user for a file name. Save object data to that file
  387. ;   Next/Children pointers saved in a file are ignored. Prefixes and
  388. ;   ordering of data in the file is used to show parent/child/peer
  389. ;   relationships.
  390. ;
  391. PRO FileSave
  392.  
  393.   COMMON WidEd_Comm
  394.  
  395.     ; Figure out a default filename
  396.     File    = FileName
  397.     InsureExt,File,''
  398.  
  399.     ;   Get the user's choice for a filename to save to
  400.     NewFile = pickfile(GROUP=TopDlg, /WRITE, $
  401.                 PATH=CurrPath(), FILE=File, GET_PATH=GetPath, $
  402.                 FILTER='*.wid')
  403.  
  404.     ;   Quit if user hit 'Cancel' button
  405.     IF NewFile EQ '' THEN RETURN
  406.  
  407.     ;   Handle DOS can't handle backslash problem
  408.  
  409.     IF !Version.OS EQ 'Win32' THEN BEGIN
  410.         GetPath = StrMid(GetPath, 0, StrLen(GetPath) - 1)
  411.     ENDIF
  412.  
  413.     ;   Add a .WID ending regardless of current ending
  414.     InsureExt, NewFile, ".wid"
  415.  
  416.     ;   Open the file
  417.     OPENW, Unit, NewFile, /XDR, /GET_LUN, ERROR=OpenError
  418.     IF OpenError NE 0 THEN BEGIN
  419.         ErrorDialog, TopDlg, [ 'Unable to open file', NewFile, 'for output' ]
  420.         RETURN
  421.     ENDIF
  422.  
  423.     ;   If there is a problem writing the file, tell
  424.     ;   the user there was a problem.
  425.  
  426.     ON_IOERROR, MsgBad
  427.  
  428.     ;   Indicate that this can be slow
  429.     WIDGET_CONTROL, /HOURGLASS
  430.  
  431.     ;   Write the header
  432.     WRITEU, Unit, BYTE('WidgetEditFile')
  433.     WRITEU, Unit, LastId
  434.     WRITEU, Unit, 100L          ; Version Id = 1.00
  435.     WRITEU, Unit, 0L            ; Extra Bytes in case we need them
  436.  
  437.     Ptr = TopPtr
  438.     WHILE PTR NE 0L DO BEGIN
  439.         WRITEU, Unit, 1
  440.         DEP_Save, Unit, Ptr
  441.         Ptr = NextPtr(Ptr)
  442.     ENDWHILE
  443.     WRITEU, Unit, 0     ; Indicate end of base list
  444.     FREE_LUN, Unit
  445.  
  446.     ;   Subroutines will indicate I/O error by setting Dirty to 2
  447.     IF Dirty EQ 2 THEN BEGIN
  448.         Dirty   = 1
  449.         GOTO, MsgBad
  450.     ENDIF
  451.  
  452.     ;   Success. Save new file name as the new default
  453.  
  454.     FileName    = NewFile
  455.     FileDir     = GetPath
  456.     Dirty       = 0         ; Saved current version.  New clean state.
  457.  
  458.     RETURN
  459.  
  460. MsgBad:
  461.     ErrorDialog, TopDlg, 'I/O error. Could not write file.'
  462. Bad:
  463.     Close, Unit
  464.     OPENW, Unit, NewFile, /DELETE, ERROR=IgnoredError   ; Remove bad file
  465.     FREE_LUN, Unit
  466. END
  467.  
  468.  
  469. ;
  470. ;  SSaveCmd
  471. ;   Write a string keyword for a widget. Default is to only
  472. ;   write the keyword if the keyword is not a null string but
  473. ;   the FORCE keyword forces the keyword to always be written.
  474. ;
  475. ;   It is assumed that there is a previously existing portion
  476. ;   to the command which will require a comma and a continuation
  477. ;   character to be appended before writing the next keyword.
  478. ;
  479. PRO SSaveCmd, Unit, Value, Keyword, FORCE=Force
  480.  
  481.     IF Value NE '' OR KEYWORD_SET(Force) THEN BEGIN
  482.     XPRINTF, Unit, ", $"
  483.     XPRINTF, FORMAT='("      ",A,"=''",A,"''")', /NO_EOL, $
  484.         Unit, Keyword, Qstring(Value)
  485.     ENDIF
  486. END
  487.  
  488.  
  489. ;
  490. ;  ISaveCmd
  491. ;   Write an integer(or long integer) keyword for a widget.
  492. ;   Default is to only write the keyword if the keyword is not
  493. ;   0 but the FORCE keyword forces the keyword to always be written.
  494. ;
  495. ;   It is assumed that there is a previously existing portion
  496. ;   to the command which will require a comma and a continuation
  497. ;   character to be appended before writing the next line of code.
  498. ;
  499. PRO ISaveCmd, Unit, Value, Keyword, FORCE=Force
  500.  
  501.     IF Value NE 0 OR KEYWORD_SET(Force) THEN BEGIN
  502.     XPRINTF, Unit, ", $"
  503.     XPRINTF, FORMAT='("      ",A,"=",A)', /NO_EOL, $
  504.         Unit, Keyword, STRTRIM(Value,2)
  505.     ENDIF
  506. END
  507.  
  508.  
  509. ;
  510. ;  SaveStr
  511. ;   Given:
  512. ;       A file unit to write to.
  513. ;       A name for the variable
  514. ;       A default value if the object has no value
  515. ;       An object containing a value (*)
  516. ;
  517. ;   Create IDL code to create a variable whose value is that <STRARR>
  518. ;   or the default if none is provided.
  519. ;
  520. ; (*) actually a widget id whose UVALUE is a <STRARR>
  521. ;
  522. PRO SaveStr, Unit, Ptr, Obj, StrName, Default
  523.  
  524.     GetValue, Obj, Names, Default
  525.     XPRINTF, Unit, '  ', StrName, ' = [ $'
  526.  
  527.     N   = N_ELEMENTS(Names)
  528.     ;   Every element but the last one is a string followed by ', $'
  529.     FOR I=0,N-2 DO $
  530.         XPRINTF, Unit, "    '", QString(Names[I]), "', $"
  531.  
  532.     ;   Last item has closing bracket for array we are declaring
  533.     XPRINTF, Unit, "    '", QString(Names[N-1]), "' ]"
  534. END
  535.  
  536.  
  537. ;
  538. ;  BeginMagic
  539. ;       Write a magic comment
  540. ;
  541. PRO BeginMagic, Unit, Id
  542.     PRINTF, Unit
  543.     PRINTF, Unit, "; CODE MODIFICATIONS MADE ABOVE THIS COMMENT WILL BE LOST.
  544.     PRINTF, Unit, "; DO NOT REMOVE THIS COMMENT: BEGIN " + Id
  545.     PRINTF, Unit
  546.     PRINTF, Unit
  547. END
  548.  
  549.  
  550. ;
  551. ;  EndMagic
  552. ;       Write a magic comment
  553. ;
  554. PRO EndMagic, Unit, Id
  555.     PRINTF, Unit
  556.     PRINTF, Unit
  557.     PRINTF, Unit, "; DO NOT REMOVE THIS COMMENT: END " + Id
  558.     PRINTF, Unit, "; CODE MODIFICATIONS MADE BELOW THIS COMMENT WILL BE LOST.
  559.     PRINTF, Unit
  560. END
  561.  
  562.  
  563. ;
  564. ;  FindMagic
  565. ;       Look for magic comments.  Return TRUE if we found them.
  566. ;
  567. FUNCTION FindMagic, Id, Unit, OldUnit
  568.  
  569.     COMMON FMagic_Comm, NoCheck
  570.  
  571.   COMMON WidEd_Comm
  572.  
  573.     ;   Can't find old stuff if it doesn't exist or
  574.     ;   forced overwrite.
  575.  
  576.     IF OldUnit EQ 0 OR NoCheck THEN RETURN, 0
  577.  
  578.  
  579.     Point_Lun, -OldUnit, SavePos        ; Remember where we are
  580.  
  581.     StartMagic  = "; DO NOT REMOVE THIS COMMENT: BEGIN " + Id
  582.     EndMagic    = "; DO NOT REMOVE THIS COMMENT: END " + Id
  583.     Line        = ""
  584.  
  585.     WHILE NOT EOF(OldUnit) DO BEGIN
  586.         READF, OldUnit, Line
  587.         IF Line EQ StartMagic THEN BEGIN
  588.             PRINTF, Unit
  589.             PRINTF, Unit, "; CODE MODIFICATIONS MADE ABOVE THIS COMMENT WILL BE LOST.
  590.             PRINTF, Unit, Line
  591.  
  592.             WHILE NOT EOF(OldUnit) DO BEGIN
  593.                 READF, OldUnit, Line
  594.                 PRINTF, Unit, Line
  595.                 IF Line EQ EndMagic THEN BEGIN
  596.                     PRINTF, Unit, "; CODE MODIFICATIONS MADE BELOW THIS COMMENT WILL BE LOST.
  597.                     PRINTF, Unit
  598.                     RETURN, 1
  599.                 ENDIF
  600.             ENDWHILE
  601.  
  602.             ;   Its bad if we get here
  603.  
  604.             ErrorDialog, TopDlg, $
  605.                 [ "Could not find END magic comment", $
  606.                   "for " + Id + " section" ]
  607.             EndMagic, Unit, Id  ; Restore magic comment
  608.  
  609.         ENDIF
  610.     ENDWHILE
  611.  
  612.     Point_Lun, OldUnit, SavePos
  613.     RETURN, 0
  614. END
  615.  
  616.  
  617. ;
  618. ;  MakeBackup
  619. ;       Need to make a backup in a machine independent way (painful,
  620. ;       slower, more slower).  VMS does its own backup stuff so we
  621. ;       don't have to.
  622. ;
  623. PRO MakeBackup, File, Unit
  624.  
  625.     OPENR, Unit, File, /GET_LUN, ERROR=OpenError
  626.     IF OpenError NE 0 THEN BEGIN
  627.         IF N_ELEMENTS(Unit) NE 0 THEN FREE_LUN, Unit
  628.         Unit = 0
  629.         RETURN
  630.     ENDIF
  631.  
  632.     IF !VERSION.OS EQ 'vms' THEN RETURN
  633.  
  634.     ;   On any non VMS system we need to create a backup
  635.  
  636.     SrcUnit     = Unit
  637.     BackFile    = File
  638.     InsureExt, BackFile, ".bak"
  639.  
  640.     OPENW, Unit, BackFile, /GET_LUN, ERROR=OpenError
  641.     IF OpenError NE 0 THEN BEGIN
  642.         FREE_LUN, SrcUnit
  643.         FREE_LUN, Unit
  644.         Unit    = 0
  645.         RETURN
  646.     ENDIF
  647.  
  648.     ;   Read line by line from source file and write it to the backup
  649.  
  650.     Line    = ''
  651.     WHILE NOT EOF(SrcUnit) DO BEGIN
  652.         READF, SrcUnit, Line
  653.         PRINTF, Unit, Line
  654.     ENDWHILE
  655.  
  656.     ;   Close files
  657.  
  658.     FREE_LUN, SrcUnit
  659.     CLOSE, Unit
  660.  
  661.     ;   Reopen backup file, this time for reading
  662.  
  663.     OPENR, Unit, BackFile, ERROR=OpenError
  664.     IF OpenError NE 0 THEN BEGIN
  665.         FREE_LUN, Unit
  666.         Unit    = 0
  667.     ENDIF
  668. END
  669.  
  670.  
  671. ;  FileGenPro
  672. ;   Main entry point for writing IDL code to reproduce the current
  673. ;   widget tree.  The user may opt to include or not include the
  674. ;   standard header document
  675. ;
  676. PRO FileGenPro, StdHdr, CheckFlag
  677.  
  678.     COMMON FMagic_Comm, NoCheck
  679.   COMMON WidEd_Comm
  680.  
  681.     NoCheck     = CheckFlag
  682.  
  683.     ;   Get a default filename
  684.  
  685.     File    = FileName
  686.     InsureExt,File,''
  687.  
  688.     ;   Ask the user 'which file should I write to?'
  689.  
  690.     NewFile = pickfile(GROUP=TopDlg, /WRITE, $
  691.             PATH=CurrPath(), FILE=File, GET_PATH=GetPath, $
  692.             FILTER='*.pro')
  693.  
  694.     ;   If the user hit 'Cancel' quit
  695.     IF NewFile EQ '' THEN RETURN
  696.  
  697.     IF !Version.OS EQ 'Win32' THEN BEGIN
  698.         GetPath = StrMid(GetPath, 0, StrLen(GetPath) - 1)
  699.     ENDIF
  700.  
  701.     ;   Add a .PRO ending regardless of current ending
  702.     InsureExt, NewFile, ".pro"
  703.  
  704.     MakeBackup, NewFile, OldUnit
  705.  
  706.     ;   Open the file
  707.  
  708.     OPENW, Unit, NewFile, /GET_LUN, ERROR=OpenError
  709.     IF OpenError NE 0 THEN BEGIN
  710.         ErrorDialog, TopDlg, [ 'Unable to open file', NewFile, 'for output' ]
  711.         RETURN
  712.     ENDIF
  713.  
  714.     ;   Try to recover from errors
  715.     ;   I/O Error recovery is VERY poor.
  716.  
  717.     ON_IOERROR, Bad
  718.  
  719.     WIDGET_CONTROL,/HOURGLASS
  720.  
  721.     ; Print a header
  722.  
  723.     PRINTF, Unit, ';'
  724.     PRINTF, Unit, '; Auto Save File For ', NewFile
  725.     PRINTF, Unit, ';'
  726.  
  727.     IF  !Version.Os EQ 'sunos' OR !Version.Os EQ 'hp-ux' OR $
  728.         !Version.Os EQ 'IRIX' OR !Version.Os EQ 'AIX' OR $
  729.         !Version.Os EQ 'ultrix' OR !Version.Os EQ 'DG/UX' THEN BEGIN
  730.         SPAWN, 'date', time
  731.         PRINTF,Unit,'; ', time & PRINTF,Unit,';'
  732.     ENDIF
  733.  
  734.     PRINTF, Unit
  735.     PRINTF, Unit
  736.  
  737.     ; Include the standard header.  Under UNIX we could do
  738.     ; fstat, malloc, readu, writeu -- but under VMS we are hosed so
  739.     ; instead we do while(!eof(fd)) n=read,write(n)
  740.  
  741.     IF FindMagic("HEADER", Unit, OldUnit) EQ 0 THEN BEGIN
  742.         BeginMagic, Unit, "HEADER"
  743.  
  744.         IF StdHdr THEN BEGIN
  745.             HeaderFile = FilePath('template.pro', SUBDIR=['help', 'widget'])
  746.             CLOSE,1
  747.             OPENR,1,HeaderFile
  748.             Line    = ''
  749.             WHILE NOT EOF(1) DO BEGIN
  750.                 READF,1,Line
  751.                 PRINTF,Unit,Line
  752.             ENDWHILE
  753.             CLOSE, 1
  754.         ENDIF
  755.  
  756.         EndMagic, Unit, "HEADER"
  757.     ENDIF
  758.  
  759.     DoFList2, TopPtr+0, 'PDMENU_MenuEv', Unit, OldUnit
  760.     DoFList2, TopPtr+0, 'DEP_BaseEv', Unit, OldUnit
  761.  
  762.     ; Write the widget building/entry point procedure
  763.  
  764.     Name    = ExtractSimpleName(NewFile)
  765.  
  766.     PRINTF, Unit, FORMAT='(//"PRO ",A,", GROUP=Group")', Name
  767.     PRINTF, Unit, FORMAT='(//"  IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0"/)'
  768.  
  769.     ; If we have pull down menus, we need to predeclare
  770.     ; the CW_PDMENU_S structure.  We declare them even if
  771.     ; we don't use them
  772.  
  773.     Cmd = "  junk   = { CW_PDMENU_S, flags:0, name:'' }"
  774.     PRINTF, Unit, FORMAT='(A//)', Cmd
  775.  
  776.     ;   Generate code to build and realize all other dependent
  777.     ;   top level bases
  778.  
  779.     DEP_GenWid, Unit, TopPtr
  780.     Ptr = NextPtr(TopPtr)
  781.     DoFList, Ptr, 'DEP_GenWid', Unit
  782.  
  783.     ;   Generate code to manage each of the dependent
  784.     ;   top level bases. Note we just register these bases
  785.     ;   with the XManager.
  786.  
  787.     Ptr = NextPtr(TopPtr)
  788.     WHILE Ptr NE 0 DO BEGIN
  789.  
  790.         PRINTF, Unit
  791.  
  792.         Name    = VarId(Ptr)
  793.         Ptr2Obj, Ptr, Obj
  794.  
  795.         IF Obj.EventProc NE '' THEN BEGIN
  796.             PRINTF, Unit, "  XMANAGER, '",Name,"', ",Name,  $
  797.                 ", /JUST_REG, EVENT_HANDLER='", QString( Obj.EventProc ), "'"
  798.         ENDIF ELSE BEGIN
  799.             PRINTF, Unit, "  XMANAGER, '",Name,"', ",Name,", /JUST_REG"
  800.         ENDELSE
  801.         Next    = Obj.Next
  802.         Obj2Ptr, Obj, Ptr
  803.         Ptr     = Next
  804.     ENDWHILE
  805.  
  806.     ;   Finally, generate code to manage the top level base
  807.  
  808.     PRINTF, Unit
  809.  
  810.     Name    = VarId(TopPtr)
  811.     Ptr2Obj, TopPtr, Obj
  812.     IF Obj.EventProc NE '' THEN BEGIN
  813.         PRINTF, Unit, "  XMANAGER, '",Name,"', ",Name, $
  814.                 ", EVENT_HANDLER='", QString( Obj.EventProc ), "'"
  815.     ENDIF ELSE BEGIN
  816.         PRINTF, Unit, "  XMANAGER, '",Name,"', ",Name
  817.     ENDELSE
  818.     Obj2Ptr, Obj, TopPtr
  819.  
  820.     PRINTF, Unit, 'END'
  821.  
  822.     ;   Success
  823.  
  824.     FREE_LUN, Unit
  825.  
  826.     ;   Remember this file as the new default file
  827.  
  828.     FileName    = NewFile
  829.     FileDir     = GetPath
  830.  
  831.     RETURN
  832.  
  833. Bad:
  834.     ErrorDialog, TopDlg, [ 'Failed to write file:', NewFile ]
  835.     Close, Unit
  836.     OPENW, Unit, NewFile, /DELETE, ERROR=IgnoredError
  837.     FREE_LUN, Unit
  838. END
  839.  
  840.  
  841. ;
  842. ;  TestDraw
  843. ;   Recursivly decend through the widget hierarchy looking for draw
  844. ;   widgets.  Put a sample plot in every draw widget.
  845. ;
  846. PRO TestDraw, Ptr
  847.     Ptr2Obj, Ptr, Obj
  848.  
  849.     CASE TAG_NAMES(Obj, /STRUCTURE) OF
  850.  
  851.     'WE_BASE': BEGIN
  852.         Child       = Obj.Children
  853.         WHILE Child NE 0L DO BEGIN
  854.             TestDraw, Child
  855.             Child   = NextPtr(Child)
  856.         ENDWHILE
  857.         END
  858.     'WE_DRAW': BEGIN
  859.         WIDGET_CONTROL, Obj.DrawId, GET_VALUE=WinId
  860.         WSET, WinId
  861.         Plot,[0,1]
  862.         END
  863.     ELSE:
  864.     ENDCASE
  865.     Obj2Ptr, Obj, Ptr
  866. END
  867.  
  868. PRO WidFile
  869. END
  870.