VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} NewLinkDlg 
   Caption         =   "Добавить ссылку"
   ClientHeight    =   4260
   ClientLeft      =   120
   ClientTop       =   465
   ClientWidth     =   7260
   OleObjectBlob   =   "NewLinkDlg.frx":0000
   StartUpPosition =   1  'CenterOwner
End
Attribute VB_Name = "NewLinkDlg"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public isCancelled_ As Boolean

Private database_ As InfoDatabase
Private source_ As InfoDocument
Private bms_ As Word.Bookmarks

Private Sub UserForm_Initialize()
  isCancelled_ = True
  Call PositionInMiddle(Me)
End Sub

Public Function Init(iSource As InfoDocument, theDB As InfoDatabase, existingBMs As Word.Bookmarks)
  isCancelled_ = True
  
  Set database_ = theDB
  Set source_ = iSource
  Set bms_ = existingBMs
  
  Call InitSourceBookmarks
End Function

Public Function GetLink() As ItemLink
  Set GetLink = New ItemLink
  With GetLink
    .sourceID_ = source_.gid_
    .sourceSub_ = cbSourceBookmark.Text
    .destID_ = CLng(tbDocumentID.Text)
    .destSub_ = tbSubTo.Text
    .type_ = GetLinkType
  End With
End Function

' ============
Private Sub btnValidateGid_Click()
  If Not IsNumeric(tbDocumentID.Value) Then
    Call UserInteraction.ShowMessage(EM_ID_INVALID_FORMAT)
    Exit Sub
  End If
  Dim gid&: gid = tbDocumentID.Value
  
  If Not database_.Contains(gid) Then
    tbTitle.Text = "Документ отсутствует в Реестре"
    Exit Sub
  End If
  
  Dim docIter As IteratorLaw: Set docIter = database_.Data(gid)
  If docIter.Status = T_LAWS_IGNORE Then
    tbTitle.Text = "Документ игнорируется"
    Exit Sub
  End If
  
  tbTitle.Text = docIter.Title
End Sub

Private Sub btnCancel_Click()
  isCancelled_ = True
  Call Me.Hide
End Sub

Private Sub btnOK_Click()
  If Not ValidateBookmark Then
    Call UserInteraction.ShowMessage(EM_BOOKMARK_NOT_AVAILABLE)
    Exit Sub
  End If
  
  isCancelled_ = False
  Call Me.Hide
End Sub

' ============
Private Function GetLinkType() As TLinkType
  If optLevel0.Value = True Then
    GetLinkType = T_LINK_DEFAULT
    Exit Function
  End If
  If optLevel1.Value = True Then
    GetLinkType = T_LINK_ASPECT
    Exit Function
  End If
  If optLevel2.Value = True Then
    GetLinkType = T_LINK_FOUNDATION
    Exit Function
  End If
  If optLevel3.Value = True Then
    GetLinkType = T_LINK_DEFINITION
    Exit Function
  End If
End Function

Private Function InitSourceBookmarks()
  Call cbSourceBookmark.Clear
  Dim n&
  For n = 1 To bms_.Count
    Call cbSourceBookmark.AddItem(bms_.Item(n).Name)
  Next n
  If bms_.Count > 0 Then _
    cbSourceBookmark.Value = bms_.Item(1).Name
End Function

Private Function ValidateBookmark() As Boolean
  Dim sSub$: sSub = cbSourceBookmark.Text
  ValidateBookmark = Not source_.doc_.Bookmarks.Exists(sSub) Or bms_.Exists(sSub)
End Function

