VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "LayoutManager"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ========== Shape positioning on creation =============
Option Explicit

Private page_ As Visio.Page
Private layout_ As VsoLayoutType

Private width_ As Double
Private height_ As Double

Private headers_(1 To MAX_LEVEL) As Visio.Shape
Private counters_(1 To MAX_LEVEL) As Long ' Counters for last element on level
Private levelX_(1 To MAX_LEVEL) As Double
Private levelY_(1 To MAX_LEVEL) As Double
Private levelWidth_(1 To MAX_LEVEL) As Integer

Public Function Init(aPage As Visio.Page, params As ItemImportOptions)
  Set page_ = aPage
  layout_ = params.layout_
  Dim nLvl&
  For nLvl = 1 To MAX_LEVEL
    levelWidth_(nLvl) = params.levels_(nLvl)
  Next nLvl
  
  Call CreateHeaders
  width_ = headers_(1).CellsU("Width")
  height_ = headers_(1).CellsU("Height")
End Function

Public Function AddNewShape(aLevel&) As Visio.Shape
' Creating new shape for level and moving it last in level
  Dim result As Visio.Shape: Set result = page_.Drop(FindMaster(ThisDocument, MASTER_DOCUMENT), 0, 0)
  result.CellsU(CELLSU_DOCLEVEL) = CStr(aLevel)
  Call MoveLastInLevel(result, aLevel)
  Set AddNewShape = result
End Function

Public Function MoveLastInLevel(tShape As Shape, aLevel&)
  Dim itemRow%: itemRow = counters_(aLevel) Mod levelWidth_(aLevel)
  Dim itemColumn%: itemColumn = counters_(aLevel) \ levelWidth_(aLevel)
  Dim pinx As Double, piny As Double
  Select Case layout_
  Case VLT_HORIZONTAL:
    pinx = levelX_(aLevel) + (1 + itemColumn) * (width_ + HORIZONTAL_GAP / MM_TO_INCH)
    piny = levelY_(aLevel) - itemRow * (height_ + VERTICAL_GAP / MM_TO_INCH)
  Case VLT_VERTICAL:
    pinx = levelX_(aLevel) + itemRow * (width_ + HORIZONTAL_GAP / MM_TO_INCH)
    piny = levelY_(aLevel) - (1 + itemColumn) * (height_ + VERTICAL_GAP / MM_TO_INCH)
  End Select
  
  Call tShape.SetCenter(pinx, piny)
  counters_(aLevel) = counters_(aLevel) + 1
End Function

'=========
Private Function CreateHeaders()
' Creating head shapes to position
  Dim pinx As Double: pinx = 0
  Dim piny As Double: piny = 0
  Dim nLvl&
  Dim headerShape As Visio.Shape
  For nLvl = 1 To MAX_LEVEL
    If levelWidth_(nLvl) <= 0 Then _
      GoTo NEXT_LVL
    Set headerShape = page_.Drop(FindMaster(ThisDocument, MASTER_DOCUMENT), 0, 0)
    Set headers_(nLvl) = headerShape
    With headerShape
      .CellsU(CELLSU_DOCLEVEL) = CStr(nLvl)
      .Text = DocLevelText(nLvl)
      .Name = PREFIX_HEAD & CStr(nLvl)
      
      Call .SetCenter(pinx, piny)
      levelX_(nLvl) = pinx
      levelY_(nLvl) = piny
      
      Select Case layout_
      Case VLT_VERTICAL:
        pinx = pinx + levelWidth_(nLvl) * (.Cells("Width") + HORIZONTAL_GAP / MM_TO_INCH)
      Case VLT_HORIZONTAL:
        piny = piny - levelWidth_(nLvl) * (.Cells("Height") + VERTICAL_GAP / MM_TO_INCH)
      End Select
    End With
NEXT_LVL:
  Next nLvl
End Function
