home *** CD-ROM | disk | FTP | other *** search
/ PC World 1997 November / PCWorld_1997-11_cd.bin / software / programy / komix / DATA.Z / fmdocconge.tcl < prev    next >
Text File  |  1996-10-23  |  10KB  |  370 lines

  1. #---------------------------------------------------------------------------
  2. #
  3. #      (c)     Westmount Technology    1994
  4. #
  5. #      File:           @(#)fmdocconge.tcl    /main/hindenburg/1
  6. #      Author:         <generated>
  7. #      Description:
  8. #---------------------------------------------------------------------------
  9. # SccsId = @(#)fmdocconge.tcl    /main/hindenburg/1   23 Oct 1996 Copyright 1994 Westmount Technology
  10.  
  11. # Start user added include file section
  12. # End user added include file section
  13.  
  14. require "fmcongen.tcl"
  15.  
  16. Class FmDocConGen : {FmConGen} {
  17.     method destructor
  18.     constructor
  19.     method generate
  20.     method genComponent
  21.     method genPicture
  22.     method genText
  23.     method genTextLine
  24.     method genTitle
  25.     method section
  26.     attribute frameWidth
  27.     attribute frameHeight
  28.     attribute frames
  29.     attribute pgfs
  30.     attribute chapter
  31.     attribute _section
  32. }
  33.  
  34. global FmDocConGen::pictureNr
  35. set FmDocConGen::pictureNr ""
  36.  
  37.  
  38. method FmDocConGen::destructor {this} {
  39.     set ref [$this _section]
  40.     if {$ref != ""} {
  41.         $ref _contentsGenerator ""
  42.     }
  43.     # Start destructor user section
  44.     # End destructor user section
  45.     $this FmConGen::destructor
  46. }
  47.  
  48. constructor FmDocConGen {class this name section} {
  49.     set this [FmConGen::constructor $class $this $name $section]
  50.     $section _contentsGenerator $this
  51.  
  52.     global FmDocConGen::pictureNr
  53.     if {${FmDocConGen::pictureNr} == ""} {
  54.         # Start numbering pictures at 100 to prevent conflicts with
  55.         # default frames in the template
  56.         set FmDocConGen::pictureNr 100
  57.     }
  58.  
  59.     $this frameWidth "6.0625"
  60.     $this frameHeight "7.0625"
  61.  
  62.     return $this
  63. }
  64.  
  65. method FmDocConGen::generate {this} {
  66.  
  67.     # First the contents is generated in two sections: the frames and the
  68.     # paragraphs. These sections are put in a MIF-file by expanding the
  69.     # template MIF-file '<name><version>.mif'. This file contains
  70.     # expandable TCL-macro's to include the contents of the sections.
  71.     # Furthermore the template MIF-file contains a macro to include the
  72.     # title of the document using 'FmDocConGen::genTitle'.
  73.  
  74.     set mifFid [$this openMif]
  75.  
  76.     if {$mifFid == ""} {
  77.         return
  78.     }
  79.  
  80.     # get the parent (chapter) if the current section
  81.     $this chapter [[$this section] parentSection]
  82.     if { [$this chapter] == "" } {
  83.         return ""
  84.     }
  85.  
  86.     # Start the sections for the frames and the paragraphs
  87.     $this frames [TextSection new]
  88.     $this pgfs   [TextSection new]
  89.  
  90.     set inSubdoc 0
  91.     set subLevel 0
  92.  
  93.     foreach sect [[$this section] getTree] {
  94.  
  95.         set level [$sect indentation]
  96.         set name  [$sect uiName]
  97.         set type  [$sect type]
  98.         set class [$sect operationClass]
  99.  
  100.         if {$inSubdoc} {
  101.             # Check for end of subdoc
  102.             if {$level <= $subLevel} {
  103.                 set inSubdoc 0
  104.                 set subLevel 0
  105.             }
  106.         }
  107.  
  108.         if {!$inSubdoc} {
  109.             # Check if the section is manipulatable. If so then skip
  110.             # this section and all it's underlying sections.
  111.             if {$class == "manipulate"} {
  112.                 # In subdocument
  113.                 set inSubdoc 1
  114.                 set subLevel $level
  115.             } else {
  116.                 # Generate contents for this section
  117.                 $this genComponent $sect
  118.             }
  119.         }
  120.     }
  121.  
  122.     # Make the Framemaker file
  123.     # Make a section and expand the template into it
  124.     set contents [TextSection new]
  125.  
  126.     # need to do this, otherwise pgfs is removed ???????
  127.     set pgfs [$this pgfs]
  128.     expand_file $contents [$this templatePath]
  129.  
  130.     # Write the contents into the MIF-file
  131.     $contents write $mifFid
  132.     close $mifFid
  133.  
  134.     $this mif2Bin
  135. }
  136.  
  137. method FmDocConGen::genComponent {this section} {
  138.  
  139.     # Look at the doctype of the class and generate a picture 
  140.     # or a text for it in the MIF-file.
  141.     case [$section docType] {
  142.         {Epsi Eps Ximage}    {$this genPicture $section}
  143.         {default}        {$this genText $section}
  144.     }
  145. }
  146.  
  147. method FmDocConGen::genPicture {this section} {
  148.  
  149.     # Generate MIF for a picture. This consists of making a frame with a
  150.     # reference to the path of the picture in the frames-section, and making
  151.     # a paragraph with a reference to this frame in the paragraphs-section.
  152.     # Also a paragraph for the picture-title is generated in the paragraphs-
  153.     # section.
  154.  
  155.     set frames [$this frames]
  156.     set pgfs [$this pgfs]
  157.     set fWidth [$this frameWidth]
  158.     set fHeight [$this frameHeight]
  159.  
  160.     # Take next picture-nr
  161.     global FmDocConGen::pictureNr
  162.     incr FmDocConGen::pictureNr
  163.  
  164.     set objFile "[[$this section] escape  [$section docLocation]]"
  165.  
  166.     
  167.     # get the boundingbox size and make the shaperect
  168.     # also 'center' the picture 
  169.     set hasBoundingBox 0
  170.  
  171.     if {![catch {set ps [open [$section docLocation] r]} reason]} {
  172.         seek $ps -200 end
  173.         while {[gets $ps line] != -1} {
  174.             if [regexp "%%BoundingBox:" $line] {
  175.                 set boxList [split $line]
  176.                 lvarpop boxList
  177.                 set left [lvarpop boxList]
  178.                 set top [lvarpop boxList]
  179.                 set right [lvarpop boxList]
  180.                 set bottom [lvarpop boxList]
  181.                 set hasBoundingBox 1
  182.                 break
  183.             }
  184.         }
  185.     }
  186.  
  187.     # Generate the frame in the frames-section
  188.     $frames append "  <Frame\n"
  189.     $frames append "    <ID ${FmDocConGen::pictureNr}>\n"
  190.     if {$hasBoundingBox == 1} {
  191.         set width [expr {($right - $left)/72.0}]
  192.         set height [expr {($bottom - $top)/72.0}]
  193.         set leftOffset [expr {($fWidth-$width)/2.0}]
  194.         set topOffset [expr {($fHeight-$height)/2.0}]
  195.         $frames append "      <ShapeRect $leftOffset\" \
  196.             $topOffset\" $width\" $height\">\n"
  197.     } else {
  198.         $frames append "    <ShapeRect 0.0\" 0.0\" $fWidth\" \
  199.             $fHeight\">\n"
  200.     }
  201.     $frames append "    <FrameType Below>\n"
  202.     $frames append "    <Float No>\n"
  203.     $frames append "    <NSOffset 0.0\">\n"
  204.     $frames append "    <BLOffset 0.0\">\n"
  205.     $frames append "    <AnchorAlign Center>\n"
  206.     $frames append "    <ImportObject\n"
  207.     $frames append "      <ImportObFile `$objFile'>\n"
  208.     $frames append "    >\n"
  209.     $frames append "  >\n"
  210.  
  211.     # Generate a paragraph with a reference to the frame in the
  212.     # paragraphs-section
  213.     $pgfs append "  <Para\n"
  214.     $pgfs append "    <PgfTag `Normal'>\n"
  215.     $pgfs append "    <ParaLine\n"
  216.     $pgfs append "      <AFrame ${FmDocConGen::pictureNr}>\n"
  217.     $pgfs append "    >\n"
  218.     $pgfs append "  >\n"
  219.  
  220.     # Generate a paragraph for the title of the picture in the
  221.     # paragraphs-section
  222.     $pgfs append "  <Para\n"
  223.     $pgfs append "    <PgfTag `PictureTitle'>\n"
  224.     $pgfs append "    <ParaLine\n"
  225.     $pgfs append "      <String `[[$this section] escapeText  [$section docTitle]]'>\n"
  226.     $pgfs append "      <Char HardReturn>\n"
  227.     $pgfs append "    >\n"
  228.     $pgfs append "    <ParaLine\n"
  229.     $pgfs append "      <String `'>\n"
  230.     $pgfs append "      <Char HardReturn>\n"
  231.     $pgfs append "    >\n"
  232.     $pgfs append "  >\n"
  233. }
  234.  
  235. method FmDocConGen::genText {this section} {
  236.  
  237.     set pgfs [$this pgfs]
  238.  
  239.     # Generate a paragraph for the text-title
  240.     $pgfs append "  <Para\n"
  241.     $pgfs append "    <PgfTag `TextTitle'>\n"
  242.     $pgfs append "    <ParaLine\n"
  243.     $pgfs append "      <String `[[$this section] escapeText [$section docTitle]]'>\n"
  244.     $pgfs append "    >\n"
  245.     $pgfs append "  >\n"
  246.  
  247.     # Start the paragraph for the text
  248.     $pgfs append "  <Para\n"
  249.     $pgfs append "    <PgfTag `Normal'>\n"
  250.  
  251.     # Open the text-file
  252.     if {[catch {set text [open [$section docLocation] r]} reason]} {
  253.         # Open failed. Put the error on the screen and 
  254.         # in the contents of the
  255.         # paragraph. Error is not fatal so continue execution.
  256.         set err "Error on opening 'path' for reading: $reason"
  257.         wmtkerror $err
  258.         $this genTextLine $err
  259.     } else {
  260.         # Read all lines from the text-file and put them in 
  261.         # the paragraph
  262.         while {[gets $text line] != -1} {
  263.             $this genTextLine $line
  264.         }
  265.         # Close the text-file
  266.         close $text
  267.     }
  268.  
  269.     # Finish the paragraph
  270.     $pgfs append "  >\n"
  271. }
  272.  
  273. method FmDocConGen::genTextLine {this line} {
  274.  
  275.     set pgfs [$this pgfs]
  276.  
  277.     # Start the paragraph-line
  278.     $pgfs append "    <ParaLine\n"
  279.  
  280.     # Walk through the leading spaces and tabs code the hard in the MIF
  281.     set i 0
  282.     set l [string length $line]
  283.     while {$i < $l} {
  284.         case [string index $line $i] {
  285.             { " " }    {$pgfs append "      <Char HardSpace>\n"}
  286.             { "\t" }    {$pgfs append "      <Char Tab>\n"}
  287.             default    {break}
  288.         }
  289.         incr i
  290.     }
  291.  
  292.     # Strip the leading spaces and tabs
  293.     set pureLine [string trim $line " \t"]
  294.  
  295.     # Put the string without the leading spaces and tabs 
  296.     # in the paragraph-line and finish the paragraph-line
  297.     $pgfs append "      <String `[[$this section] escapeText $pureLine]'>\n"
  298.     $pgfs append "      <Char HardReturn>\n"
  299.     $pgfs append "    >\n"
  300. }
  301.  
  302. method FmDocConGen::genTitle {this textRectID} {
  303.  
  304.     # Get the level of the section. 
  305.     # This determines the kind of title
  306.     set level [[$this section] indentation]
  307.  
  308.     # Determine the kind of title, i.e. the paragraph-tag
  309.     case $level {
  310.         {0 1}    {set tag Chapter}
  311.         {2}    {set tag Section}
  312.         {3}    {set tag SubSection}
  313.         {4}    {set tag SubSubSection}
  314.         default    {return ""}
  315.     }
  316.  
  317.     # Check if the section is the first one of this level 
  318.     # within the upper level. If so then use the paragraph-tag 
  319.     # extended with '1', to make the number starting at the beginning.
  320.     set extension 1
  321.  
  322.     set parent [[$this section] parentSection]
  323.     if { $parent == "" } {
  324.         return ""
  325.     }
  326.  
  327.     set brothers [$parent childSectionSet]
  328.     
  329.     # only if it's the first the extension is 1
  330.     if {[$this section] == [$brothers index]} {
  331.         set extension 1
  332.     } else {
  333.         set extension ""
  334.     }
  335.  
  336.     # Make a section for the title
  337.     set title [TextSection new]
  338.  
  339.     # Fill the section with the paragraph that defined the complete title
  340.     $title append "  <Para\n"
  341.     $title append "    <PgfTag `$tag$extension'>\n"
  342.     $title append "    <ParaLine\n"
  343.     $title append "      <TextRectID $textRectID>\n"
  344.     $title append "      <String `[[$this section] escapeText [[$this section] docTitle]]'>\n"
  345.     $title append "    >\n"
  346.     $title append "  >\n"
  347.  
  348.     # Return the contents of the title-section to be filled in into 
  349.     # the template
  350.     return [$title contents]
  351. }
  352.  
  353. # Do not delete this line -- regeneration end marker
  354.  
  355. method FmDocConGen::section {this args} {
  356.     if {$args == ""} {
  357.         return [$this _section]
  358.     }
  359.     set ref [$this _section]
  360.     if {$ref != ""} {
  361.         $ref _contentsGenerator ""
  362.     }
  363.     set obj [lindex $args 0]
  364.     if {$obj != ""} {
  365.         $obj _contentsGenerator $this
  366.     }
  367.     $this _section $obj
  368. }
  369.  
  370.