'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "GlobalUtilities.mmbas"
'#Uses "GlobalParameterNames.mmbas"
'#Uses "ParameterNames.mmbas"
Const MaxImageHeight As Single = 100
Const MaxImageWidth As Single = 100

Dim ImageWidth As Single
Dim ImageHeight As Single

Dim bodyString As String

Dim iconString As String
Dim imageString As String
Dim hyperlinkString As String
Dim hyperlinkIcon As Icon

Dim MMCalloutType As Variant
Dim template As String
Dim IsValidCallut As Boolean
Dim IsValidCalloutCollection As Boolean
Dim DoProcessTemplate As Boolean
Dim count As Long
Dim i As Long
Dim getParameterResult As Variant
Dim LocalCounter As Variant
Dim topicString As String
Dim CalloutCount As Long
Dim oneCallout As Topic
Dim oneCalloutCollection As Topics
Dim oneHyperlink As Hyperlink
Dim oneImage As Image
Dim oneIconCollection As Icons
Dim oneIcon As Icon

Option Explicit

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport
    Dim aPage As Mm5WebExportLib.Page

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

	Dim oneTopic As Topic

    On Error GoTo ReportError
    webex.Trace("Entering BoxCoNotItem.htm",2)

    ' Store currently active topic
    Set oneTopic = webex.ActiveTopic

    Set oneCalloutCollection = oneTopic.CalloutTopics(False)
    IsValidCalloutCollection = oneCalloutCollection.IsValid

    For Each oneCallout In oneCalloutCollection

        If Not webex.IsSuccessful Then
            Exit For
        End If

        ' Store icons used in a variant list (to determine what items to include in the icon legend)
        Set oneIconCollection = oneCallout.Icons(True)
        If oneIconCollection.Count > 0 Then
            For Each oneIcon In oneIconCollection
                webex.VariantSets(weVariantSetIcons).Add(oneIcon.Name)
            Next
        End If

		ExportCallout oneCallout, webex, False
    Next

    If webex.IsSuccessful And oneTopic.HasBoundary Then
    	If oneTopic.Boundary.HasSummaryTopic Then

	        ' Store icons used in a variant list (to determine what items to include in the icon legend)
	        Set oneIconCollection = oneTopic.Boundary.SummaryTopic.Icons(True)
	        If oneIconCollection.Count > 0 Then
	            For Each oneIcon In oneIconCollection
	                webex.VariantSets(weVariantSetIcons).Add(oneIcon.Name)
	            Next
	        End If

			ExportCallout oneTopic.Boundary.SummaryTopic, webex, True
		End If
    End If

    ' Write out the result
    webex.PushString(bodyString)

    webex.Trace("BoxCoNotItem.htm", 2)

    Exit Sub
 
ReportError:
    ProcessError(webex)
    Exit Sub
 
End Sub

Sub ExportCallout (oneCallout As Topic, webex As WebExport, isSummary As Boolean)
	Dim oneTopic As Topic

   ' Determine if this callout is put inline or linked to
    MMCalloutType = webex.CalloutType(oneCallout)

    If MMCalloutType = weContentCalloutExternal Then

        ' the callout is exported as a separate page, link to it

        template = webex.ReadFile( "BoxCoNotLink.htm" )
        webex.SetParameter("CustomParameter.CalloutLink" , webex.PathToUrl(webex.Pages.FindPage(oneCallout).FileName))
        webex.SetParameter("CustomParameter.CalloutHyperlinkName" , VerifyTopicText(webex, oneCallout))

        topicString = template

        ' Process all commands and parameters
        ProcessString webex, topicString

        ' Store our processed review comments
        bodyString = bodyString + topicString

    ElseIf MMCalloutType = weLinkCallout Then
      ' Callout will be exported in related section

    Else

        ' Callout is put inline

        ' Process the icons for the callout (this function also remembers the hyperlink icon for later)
        Call ExportCalloutIcons (oneCallout, webex)
        ' Process the image for the calllout
        Call ExportCalloutImage (oneCallout, webex, isSummary)
        ' Process the hyperlink for the callout
        Call ExportHyperlink (oneCallout, webex)

        ' Get the callout topic text
        webex.SetParameter( "CustomParameter.TheCalloutText", VerifyTopicText(webex, oneCallout))
        template = webex.ReadFile( "BoxCoNotItem.htm" )
        topicString = template

        ' Process all commands and parameters
        ProcessString webex, topicString

        ' Store our processed review comments
        bodyString = bodyString + topicString

        ' Placeholder replacement: Text notes
        If oneCallout.Notes.IsEmpty Then
            bodyString = Replace(bodyString, "#Notes#", "")
        Else
            bodyString = Replace(bodyString, "#Notes#", GlobalExportNotes(webex, oneCallout))
        End If

        ' Placeholder replacement: Icons
        If oneCallout.Icons(True).Count > 0 Then
            bodyString = Replace(bodyString, "#Icons#", iconString)
        Else
            bodyString = Replace(bodyString, "#Icons#", "")
        End If

        ' Placeholder replacement: Image
        If oneCallout.HasImage = True Then
            bodyString = Replace(bodyString, "#Image#", imageString)
        Else
            bodyString = Replace(bodyString, "#Image#", "")
        End If

        'Placeholder replacement: Hyperlink
        If oneCallout.HasHyperlink = True Then
            bodyString = Replace(bodyString, "#Hyperlink#", hyperlinkString)
        Else
            bodyString = Replace(bodyString, "#Hyperlink#", "")
        End If

        imageString = ""
        iconString = ""

    End If

End Sub


Sub ExportCalloutIcons (oneCallout As Topic, webex As WebExport)

    Dim icontemplate As String
    Dim localtopicString As String
    Dim localbodyString As String
    Dim oneIconsCollection As Icons
    Dim oneIcon As Icon
    Dim aRelativeMapMarkerPath As String

    Set oneIconsCollection = oneCallout.Icons(True)

    ' Iterate over icons
    For Each oneIcon In oneIconsCollection
    
        If Not webex.IsSuccessful Then
            Exit For
        End If

        ' don't export the controlstrip icons
        Select Case oneIcon.Type

            Case  mmIconTypeAutomaticNotes
                ' do nothing

            Case mmIconTypeAutomaticHyperlink
                ' remember icon for hyperlink export
                Set hyperlinkIcon = oneIcon

            Case Else

                icontemplate = webex.ReadFile( "SnipIcons.htm" )

                ' Save the graphic to the export directory
                aRelativeMapMarkerPath = RelativeMapMarkerPath(webex, oneIcon.Name)
                oneIcon.Save(webex.FullExportFilename(aRelativeMapMarkerPath), mmGraphicTypeGif)

                ' Get parameters for HTML template processing
                webex.SetParameter("CustomParameter.CodeName", oneIcon.Name)
                webex.SetParameter("CustomParameter.CodeFileName", webex.PathToUrl(aRelativeMapMarkerPath))
                webex.SetParameter("CustomParameter.CodeLabel", IconLabel(oneIcon, webex.Document))

                ' Read our template
                localtopicString = icontemplate

                ' Process all commands and parameters
                ProcessString webex, localtopicString

                ' Our merged string to write back
                localbodyString = localbodyString + localtopicString

        End Select

    Next

    iconString = localbodyString

End Sub

Sub ExportCalloutImage (oneCallout As Topic, webex As WebExport, isSummary As Boolean)

    Dim imagetemplate As String
    Dim localtopicString As String
    Dim localbodyString As String
    Dim oneImage As Image
    Dim imageFileName As String
    Dim fullImagePath As String

    ' Export callout image if one exists
    If oneCallout.HasImage Then

        Set oneImage = oneCallout.Image

        ' NEED TO CALL LARRY's MAGIC URL FOR FILE NAME HERE
        Dim parentTopic As Topic
        If isSummary Then
			Set parentTopic = oneCallout.ParentBoundary.Topic
        Else
			Set parentTopic = oneCallout.ParentTopic
        End If

        ' build file name from export page the topic is on plus a counter
        imageFileName =   webex.Pages.FindPage(parentTopic).BaseFilename _
                        + "_img" _
                        + CStr(webex.GetParameter("CustomParameter.ImageCounter")) _
                        + ".jpg"
        fullImagePath = webex.FullExportFilename(imageFileName)

        ' Save image
        oneImage.Save(fullImagePath, mmGraphicTypeJpeg)

        webex.SetParameter("CustomParameter.TopicImageText", "")
        webex.SetParameter("CustomParameter.TopicImage", imageFileName)

        ' Increment image counter
        webex.SetParameter("CustomParameter.ImageCounter", _
                            webex.GetParameter("CustomParameter.ImageCounter") + 1)

        ' Convert image dimensions to pixels
        ImageWidth = oneImage.Width
        Utilities.MillimetersToPixels (ImageWidth)
        ImageHeight = oneImage.Height
        Utilities.MillimetersToPixels (ImageHeight)

        ' If image width is too large, enforce max size
        If ImageWidth < MaxImageWidth Then
            webex.SetParameter("CustomParameter.TopicImageWidth", CStr(ImageWidth))
            Else
            webex.SetParameter("CustomParameter.TopicImageWidth", CStr(MaxImageWidth))
        End If

        ' If image height is too large, enforce max size
        If ImageHeight < MaxImageHeight Then
            webex.SetParameter("CustomParameter.TopicImageHeight", CStr(ImageHeight))
            Else
            webex.SetParameter("CustomParameter.TopicImageHeight", CStr(MaxImageHeight))
        End If

        imagetemplate  = webex.ReadFile( "SnipImg.htm" )

        ' Read our template
        localtopicString = imagetemplate

        ' Process all commands and parameters
        ProcessString webex, localtopicString

        ' Our merged string to write back
        localbodyString = localbodyString + localtopicString

    Else

        ' No image
        localbodyString = ""

    End If

    imageString = localbodyString

End Sub

Function ExportHyperlink(oneCallout As Topic, webex As WebExport)

    Dim localtopicString As String
    Dim localbodyString As String
    Dim template As String
    Dim theURL As String
    Dim theURLText As String
    Dim theURLTarget As String
    Dim aRelativeMapMarkerPath As String

    If oneCallout.HasHyperlink Then

        ' Read template file
         template = webex.ReadFile( "SnipHyp.htm" )

        ' "Magic" function to get the correct URL to any kind of linked item
         webex.UrlFromHyperlink(oneCallout.Hyperlink, theURL, theURLText, theURLTarget)

        ' If the hyperlink does not have a name, use the link itself as name
        If theURLText = "" Then
            theURLText = theURL
        End If

        webex.SetParameter ("CustomParameter.HyperlinkURL",theURL)
        webex.SetParameter ("CustomParameter.HyperlinkTarget",theURLTarget)
        webex.SetParameter ("CustomParameter.HyperlinkName",theURLText)

        ' Save the graphic to the export directory
        aRelativeMapMarkerPath = RelativeMapMarkerPath(webex, hyperlinkIcon.Name)
        hyperlinkIcon.Save( webex.FullExportFilename(aRelativeMapMarkerPath), mmGraphicTypeGif)
        webex.SetParameter("CustomParameter.CodeFileName", webex.PathToUrl(aRelativeMapMarkerPath))

        ' Process all commands and parameters
        localtopicString = template

        ProcessString webex, localtopicString

        localbodyString =  localtopicString

    Else

        ' No hyperlink
        localbodyString = ""

    End If

    hyperlinkString = localbodyString

End Function
