VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} LinkToNewDocDlg 
   Caption         =   "Добавить ссылку на новый документ"
   ClientHeight    =   6285
   ClientLeft      =   120
   ClientTop       =   465
   ClientWidth     =   9210.001
   OleObjectBlob   =   "LinkToNewDocDlg.frx":0000
   StartUpPosition =   1  'CenterOwner
End
Attribute VB_Name = "LinkToNewDocDlg"
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)
  
  Call cbDocType.AddItem("01. Не определен")
  Call cbDocType.AddItem("02. Международное законодательство")
  Call cbDocType.AddItem("03. Конституция")
  Call cbDocType.AddItem("04. Федеральный конституционный закон")
  Call cbDocType.AddItem("05. Кодекс")
  Call cbDocType.AddItem("06. Президент")
  Call cbDocType.AddItem("07. Федеральный закон")
  Call cbDocType.AddItem("08. Правительство")
  Call cbDocType.AddItem("09. Верховный суд")
  Call cbDocType.AddItem("10. Генеральная прокуратура")
  Call cbDocType.AddItem("11. Министерство")
  Call cbDocType.AddItem("12. Агентство, служба, ведомство")
  
  Call cbDocStatus.AddItem("1. ВНУТРЕННИЙ: рассматривать все связи")
  Call cbDocStatus.AddItem("2. ГРАНИЧНЫЙ: только связи внутри Реестра")
  Call cbDocStatus.AddItem("3. СПРАВОЧНЫЙ: без исходящих связей")
  Call cbDocStatus.AddItem("4. ИГНОРИРУЕМЫЙ: игнорировать все связи")
  
  tbType.Value = 1
  tbStatus.Value = 1
End Sub

Public Function Init(iSource As InfoDocument, theDB As InfoDatabase, existingBMs As Word.Bookmarks, sTitle$)
  isCancelled_ = True
  
  Set database_ = theDB
  Set source_ = iSource
  Set bms_ = existingBMs
  
  tbTitle.Text = sTitle
  Call InitSourceBookmarks
  Call UpdateGID
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

Public Function GetDocument() As ItemLaw
  Set GetDocument = ParseLawTitle(SubstituteWhitespace(tbTitle.Text))
  With GetDocument
    .isDummy_ = True
    .gid_ = CLng(tbDocumentID.Text)
    .fileName_ = HT03_FILE_NOT_LOADED
    .type_ = cbDocType.ListIndex + 1
    .status_ = cbDocStatus.ListIndex + 1
    .title_ = SubstituteWhitespace(tbTitle.Text)
  End With
End Function

' ============
Private Sub btnGenerateID_Click()
  Call UpdateGID
End Sub

Private Sub tbStatus_Change()
  Dim sStatus$: sStatus = tbStatus.Text
  If Not IsNumeric(sStatus) Then _
    Exit Sub
  Dim val&: val = CLng(sStatus)
  If val < 1 Or val > cbDocStatus.ListCount Then _
    Exit Sub
  cbDocStatus.ListIndex = val - 1
End Sub

Private Sub tbType_Change()
  Dim sType$: sType = tbType.Text
  If Not IsNumeric(sType) Then _
    Exit Sub
    
  Dim val&: val = CLng(sType)
  If val < 1 Or val > cbDocType.ListCount Then _
    Exit Sub
    
  cbDocType.ListIndex = val - 1
End Sub

Private Sub cbDocStatus_Change()
  tbStatus.Value = cbDocStatus.ListIndex + 1
End Sub

Private Sub cbDocType_Change()
  tbType.Value = cbDocType.ListIndex + 1
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 UpdateGID()
  tbDocumentID.Text = database_.GenerateID(tbTitle.Text)
End Function

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

