1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="TexMathsNumberedEquations" script:language="StarBasic">
'
' TexMathsEquationsNumbered
'
' Copyright (C) 2012-2020 Roland Baudin (roland65@free.fr)
' Based on the work of Geoffroy Piroux (gpiroux@gmail.com)
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'
' Macro used to create a numbered equation
' Force variable declaration
Option Explicit
Sub Main
' Get break before num and break after options
Dim oSystemInfo as Variant
Dim BreakBeforeNum as Boolean, BreakAfterNum as Boolean
Dim CaptionLeftAlign as Boolean
oSystemInfo = GetConfigAccess("/ooo.ext.texmaths.Registry/SystemInfo", TRUE)
If oSystemInfo.BreakBeforeNum = "TRUE" Then BreakBeforeNum = TRUE Else BreakBeforeNum = FALSE
If oSystemInfo.BreakAfterNum = "TRUE" Then BreakAfterNum = TRUE Else BreakAfterNum = FALSE
If oSystemInfo.CaptionLeftAlign = "TRUE" Then CaptionLeftAlign = TRUE Else CaptionLeftAlign = FALSE
' Get page width and margin sizes
Dim oDoc as Variant, oPageStyles as Variant, oStyle as Variant, oViewCursor as Variant, oPageStyleName as String
oDoc = ThisComponent
' If we are in Writer preview mode, just silently exit
If GetDocumentType(oDoc) = "swriter" Then
If oDoc.getCurrentController.getFrame.LayoutManager.isElementVisible ( "private:resource/toolbar/previewobjectbar" ) Then Exit Sub
End If
' Get view cursor and page style
oViewCursor = oDoc.CurrentController.getViewCursor()
oPageStyleName = oViewCursor.PageStyleName
oPageStyles = oDoc.StyleFamilies.getByName("PageStyles")
oStyle = oPageStyles.getByName(oPageStyleName)
' Declarations
Dim nbColsDoc as Integer, nbColsSec as Integer
Dim oTextColsDoc as Variant, oTextColsSec as Variant
Dim tabpos1 as Integer, tabpos2 as Integer, colwidth as Integer
Dim tabpos1_default as Integer, tabpos2_default as Integer
' Number of columns of the document
oTextColsDoc = oStyle.TextColumns
nbColsDoc = oTextColsDoc.getColumnCount()
' Default tab positions at middle of the page and border of the right margin
tabpos1_default = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin) / 2
tabpos2_default = oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin
' Single column document
If nbColsDoc = 0 Then ' nbColsDoc = 0 means single column!
' View cursor is not in a section
If isEmpty( oViewCursor.TextSection ) Then
' Use default tabs
tabpos1 = tabpos1_default
tabpos2 = tabpos2_default
' View cursor is in a section
Else
' Number of columns of the section
oTextColsSec = oViewCursor.TextSection.TextColumns
nbColsSec = oTextColsSec.getColumnCount()
' Single column section
If nbColsSec = 0 Then
' Use default tabs
tabpos1 = tabpos1_default
tabpos2 = tabpos2_default
' Section with two columns or more
Else
' Automatic column widths (columns of same width)
If oTextColsSec.IsAutomatic() Then
' Column width
colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nbColsSec - 1) * oTextColsSec.AutomaticDistance ) / nbColsSec
' Compute tab positions (middle of the column and border of the column right margin)
tabpos1 = colwidth / 2
tabpos2 = colwidth
' Non automatic column widths (columns of different widths)
Else
' Use default tabs
tabpos1 = tabpos1_default
tabpos2 = tabpos2_default
End If
End If
End If
' Document with two or more columns
Else
' Automatic column widths (columns of same width)
If oTextColsDoc.IsAutomatic() Then
' View cursor is not in a section
If isEmpty( oViewCursor.TextSection ) Then
' Column width
colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
' Compute tab positions (middle of the column and border of the column right margin)
tabpos1 = colwidth / 2
tabpos2 = colwidth
' View cursor is in a section
Else
' Number of columns of the section
oTextColsSec = oViewCursor.TextSection.TextColumns
nbColsSec = oTextColsSec.getColumnCount()
' Single column section
If nbColsSec = 0 Then
' Column width
colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
' Compute tab positions (middle of the column and border of the column right margin)
tabpos1 = colwidth / 2
tabpos2 = colwidth
' Section with two columns or more
Else
' Automatic column widths (columns of same width)
If oTextColsSec.IsAutomatic() Then
' Column width
colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
colwidth = colwidth / nbColsSec
' Compute tab positions (middle of the column and border of the column right margin)
tabpos1 = colwidth / 2
tabpos2 = colwidth
' Non automatic column widths (columns of different widths)
Else
' Use default tabs
tabpos1 = tabpos1_default
tabpos2 = tabpos2_default
End If
End If
End If
' Non automatic column widths (columns of different widths)
Else
' Use default tabs
tabpos1 = tabpos1_default
tabpos2 = tabpos2_default
End If
End If
' Get access to the document
Dim oFrame as Variant
Dim oDispatcher as Variant
oFrame = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
' Eventually insert an empty paragraph before the equation
If BreakBeforeNum = TRUE Then oDispatcher.executeDispatch(oFrame, ".uno:InsertPara", "", 0, Array())
Dim sCaption as String
Dim args1(0) as new com.sun.star.beans.PropertyValue
Dim args2(1) as new com.sun.star.beans.PropertyValue
Dim args3(5) as new com.sun.star.beans.PropertyValue
' Left aligned caption
If CaptionLeftAlign = TRUE Then
' Insert a centered tab stop at position tabpos1
args1(0).Name = "Tabstops.TabStops"
args1(0).Value = Array(Array(tabpos1,com.sun.star.style.TabAlign.CENTER,","," "))
oDispatcher.executeDispatch(oFrame, ".uno:Tabstops", "", 0, args1())
' Insert the equation caption text at the left
If len(oSystemInfo.EquationCaption) = 0 Then
sCaption = "()"
Else
sCaption = "(" & oSystemInfo.EquationCaption & " )"
End If
args1(0).Name = "Text"
args1(0).Value = sCaption + CHR$(9)
oDispatcher.executeDispatch(oFrame, ".uno:InsertText", "", 0, args1())
' Go two characters left
args2(0).Name = "Count"
args2(0).Value = 2
args2(1).Name = "Select"
args2(1).Value = false
oDispatcher.executeDispatch(oFrame, ".uno:GoLeft", "", 0, args2())
' Insert a numbering variable named Equation
args3(0).Name = "Type"
args3(0).Value = 23
args3(1).Name = "SubType"
' Set the numbering level
If oSystemInfo.NumLevel = "0" Then
args3(1).Value = 127
Else
args3(1).Value = Val(oSystemInfo.NumLevel) - 1
End If
args3(2).Name = "Name"
args3(2).Value = "Equation"
args3(3).Name = "Content"
args3(3).Value = ""
args3(4).Name = "Format"
args3(4).Value = 4
args3(5).Name = "Separator"
args3(5).Value = "."
oDispatcher.executeDispatch(oFrame, ".uno:InsertField", "", 0, args3())
' Go two characters right
args2(0).Name = "Count"
args2(0).Value = 2
args2(1).Name = "Select"
args2(1).Value = false
oDispatcher.executeDispatch(oFrame, ".uno:GoRight", "", 0, args2())
' Eventually insert an empty paragraph after the equation
If BreakAfterNum = TRUE Then
oDispatcher.executeDispatch(oFrame, ".uno:InsertPara", "", 0, Array())
' Go one character left
args2(0).Name = "Count"
args2(0).Value = 1
args2(1).Name = "Select"
args2(1).Value = false
oDispatcher.executeDispatch(oFrame, ".uno:GoLeft", "", 0, args2())
End If
' Right aligned caption
Else
' Insert two tab stops at positions tabpos1 and tabpos2
' The first one is centered and the second one is right justified
args1(0).Name = "Tabstops.TabStops"
args1(0).Value = Array(Array(tabpos1,com.sun.star.style.TabAlign.CENTER,","," "),Array(tabpos2,com.sun.star.style.TabAlign.RIGHT,","," "))
oDispatcher.executeDispatch(oFrame, ".uno:Tabstops", "", 0, args1())
' Insert the equation caption text at the right
If len(oSystemInfo.EquationCaption) = 0 Then
sCaption = "()"
Else
sCaption = "(" & oSystemInfo.EquationCaption & " )"
End If
args1(0).Name = "Text"
args1(0).Value = CHR$(9) + CHR$(9) + sCaption
oDispatcher.executeDispatch(oFrame, ".uno:InsertText", "", 0, args1())
' Evetually insert an empty paragraph after the equation
' And set the number of characters to go left
Dim nleft as Integer
If BreakAfterNum = TRUE Then
oDispatcher.executeDispatch(oFrame, ".uno:InsertPara", "", 0, Array())
nleft = 2
Else
nleft = 1
End If
' Go one or two characters left
args2(0).Name = "Count"
args2(0).Value = nleft
args2(1).Name = "Select"
args2(1).Value = false
oDispatcher.executeDispatch(oFrame, ".uno:GoLeft", "", 0, args2())
' Insert a numbering variable named Equation
args3(0).Name = "Type"
args3(0).Value = 23
args3(1).Name = "SubType"
' Set the numbering level
If oSystemInfo.NumLevel = "0" Then
args3(1).Value = 127
Else
args3(1).Value = Val(oSystemInfo.NumLevel) - 1
End If
args3(2).Name = "Name"
args3(2).Value = "Equation"
args3(3).Name = "Content"
args3(3).Value = ""
args3(4).Name = "Format"
args3(4).Value = 4
args3(5).Name = "Separator"
args3(5).Value = "."
oDispatcher.executeDispatch(oFrame, ".uno:InsertField", "", 0, args3())
' Go len(sCaption)+1 characters left
args2(0).Name = "Count"
args2(0).Value = len(sCaption)+1
args2(1).Name = "Select"
args2(1).Value = false
oDispatcher.executeDispatch(oFrame, ".uno:GoLeft", "", 0, args2())
End If
' Set vertical alignment to middle
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "VerticalParagraphAlignment"
args4(0).Value = 3
oDispatcher.executeDispatch(oFrame, ".uno:VerticalParagraphAlignment", "", 0, args4())
' Launch the TexMaths Equation main macro
TexMaths.TexMathsEquations.main()
End Sub
</script:module>
|