# Test CawtWord procedures related to property handling.
#
# Copyright: 2007-2020 Paul Obermeier (obermeier@poSoft.de)
# Distributed under BSD license.

if { [file exists "SetTestPathes.tcl"] } {
    source "SetTestPathes.tcl"
}
package require cawt

# Open Word, show the application window and create a workbook.
set appId  [Word OpenNew true]
set docId1 [Word AddDocument $appId]

# Delete Word file from previous test run.
file mkdir testOut
set docFile1 [file join [pwd] "testOut" "Word-10_Properties1"]
set docFile2 [file join [pwd] "testOut" "Word-10_Properties2"]
append docFile1 [Word GetExtString $appId]
append docFile2 [Word GetExtString $appId]
file delete -force $docFile1
file delete -force $docFile2

# Set some builtin and custom properties and check their values.
Office SetDocumentProperty $docId1 "Author"     "Paul Obermeier"
Office SetDocumentProperty $docId1 "Company"    "poSoft"
Office SetDocumentProperty $docId1 "Title"      $docFile1
Office SetDocumentProperty $docId1 "CustomProp" "CustomValue1"

Cawt CheckString "Paul Obermeier" [Office GetDocumentProperty $docId1 "Author"]     "Property Author"
Cawt CheckString "poSoft"         [Office GetDocumentProperty $docId1 "Company"]    "Property Company"
Cawt CheckString $docFile1        [Office GetDocumentProperty $docId1 "Title"]      "Property Title"
Cawt CheckString "CustomValue1"   [Office GetDocumentProperty $docId1 "CustomProp"] "Property CustomProp"

# Get all builtin and custom properties and insert them into the document.
Word AppendText $docId1 "Builtin Properties:"
set builtinProps [Office GetDocumentProperties $docId1 "Builtin"]
set builtinTable [Word AddTable [Word GetEndRange $docId1] [expr [llength $builtinProps] +1] 2]
Word SetTableBorderLineStyle $builtinTable
Word SetTableOptions $builtinTable -left 0.4c -right 0.3c -top 0.1c -bottom 0.2c -spacing 0.05c
Word SetHeaderRow $builtinTable [list "Name" "Value"]

set row 2
set col 1
foreach propertyName $builtinProps {
    Word SetCellValue $builtinTable $row [expr $col + 0] $propertyName
    Word SetCellValue $builtinTable $row [expr $col + 1] [Office GetDocumentProperty $docId1 $propertyName]
    incr row
}

Word AppendText $docId1 "\nCustom Properties:"
set customProps [Office GetDocumentProperties $docId1 "Custom"]
set customTable [Word AddTable [Word GetEndRange $docId1] [expr [llength $customProps] +1] 2]
Word SetTableBorderLineStyle $customTable
Word SetHeaderRow $customTable [list "Name" "Value"]
set row 2
set col 1

foreach propertyName [Office GetDocumentProperties $docId1 "Custom"] {
    Word SetCellValue $customTable $row [expr $col + 0] $propertyName
    Word SetCellValue $customTable $row [expr $col + 1] [Office GetDocumentProperty $docId1 $propertyName]
    incr row
}

puts "Saving as Word file: $docFile1"
Word SaveAs $docId1 $docFile1
Word Close  $docId1

puts "Changing custom property ..."
set docId2 [Word OpenDocument $appId $docFile1]
Office SetDocumentProperty $docId2 "CustomProp" "CustomValue2"
Cawt CheckString "CustomValue2" [Office GetDocumentProperty $docId2 "CustomProp"] "Property CustomProp"

Word AppendText $docId2 "\nChanged Custom Properties:"
set customProps [Office GetDocumentProperties $docId2 "Custom"]
set customTable [Word AddTable [Word GetEndRange $docId2] [expr [llength $customProps] +1] 2]
Word SetTableBorderLineStyle $customTable
Word SetHeaderRow $customTable [list "Name" "Value"]
set row 2
set col 1

foreach propertyName [Office GetDocumentProperties $docId2 "Custom"] {
    Word SetCellValue $customTable $row [expr $col + 0] $propertyName
    Word SetCellValue $customTable $row [expr $col + 1] [Office GetDocumentProperty $docId2 $propertyName]
    incr row
}

puts "Saving as Word file: $docFile2"
Word SaveAs $docId2 $docFile2

Cawt PrintNumComObjects

if { [lindex $argv 0] eq "auto" } {
    Word Quit $appId
    Cawt Destroy
    exit 0
}
Cawt Destroy
