<?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">&apos;
&apos;    TexMathsEquationsNumbered
&apos;
&apos;	 Copyright (C) 2012-2026 Roland Baudin (roland65@free.fr)
&apos;    Based on the work of Geoffroy Piroux (gpiroux@gmail.com)
&apos;
&apos;    This program is free software; you can redistribute it and/or modify
&apos;    it under the terms of the GNU General Public License as published by
&apos;    the Free Software Foundation; either version 2 of the License, or
&apos;    (at your option) any later version.
&apos;
&apos;    This program is distributed in the hope that it will be useful,
&apos;    but WITHOUT ANY WARRANTY; without even the implied warranty of
&apos;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
&apos;    GNU General Public License for more details.
&apos;
&apos;    You should have received a copy of the GNU General Public License
&apos;    along with this program; if not, write to the Free Software
&apos;    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
&apos;

&apos;	 Macro used to create a numbered equation 

&apos; Force variable declaration
Option Explicit

Sub Main

	&apos; Set TexMaths configuration
	SetConfig()

	&apos; Get break before num and break after options
	Dim oSystemInfo as Variant
	Dim breakBeforeNum as Boolean, breakAfterNum as Boolean
	Dim captionHidden as Boolean, captionLeftAlign as Boolean
	oSystemInfo = GetConfigAccess(&quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)
	If oSystemInfo.BreakBeforeNum = &quot;TRUE&quot; Then	breakBeforeNum = TRUE Else breakBeforeNum = FALSE
	If oSystemInfo.BreakAfterNum = &quot;TRUE&quot; Then	breakAfterNum = TRUE Else breakAfterNum = FALSE
	If oSystemInfo.CaptionHidden = &quot;TRUE&quot; Then captionHidden = TRUE Else captionHidden = FALSE
	If oSystemInfo.CaptionLeftAlign = &quot;TRUE&quot; Then captionLeftAlign = TRUE Else captionLeftAlign = FALSE

	&apos; Get page width and margin sizes
	Dim oDoc as Variant, oPageStyles as Variant, oStyle as Variant, oViewCursor as Variant, oPageStyleName as String
	oDoc = ThisComponent
	
	&apos; If we are in Writer preview mode, just silently exit
	If GetDocumentType(oDoc) = &quot;swriter&quot; Then

		If oDoc.getCurrentController.getFrame.LayoutManager.isElementVisible ( &quot;private:resource/toolbar/previewobjectbar&quot; ) Then Exit Sub

	End If 
	
	&apos; If the cursor is inside a text box
	If IsCursorInTextBox(oDoc) Then
	
  		MsgBox( _(&quot;Can&apos;t create numbered equations inside a text box!&quot;), 0, &quot;TexMaths&quot; )
  		Exit Sub
		
	End If

	&apos; Get view cursor and page style
	oViewCursor = oDoc.CurrentController.getViewCursor()
	oPageStyleName = oViewCursor.PageStyleName
	oPageStyles = oDoc.StyleFamilies.getByName(&quot;PageStyles&quot;)
	oStyle = oPageStyles.getByName(oPageStyleName)

	&apos; Declarations
	Dim nColdocType as Integer, nColsSec as Integer
	Dim oTextColdocType as Variant, oTextColsSec as Variant
	Dim tabpos1 as Integer, tabpos2 as Integer, colwidth as Integer
	Dim tabpos1_default as Integer, tabpos2_default as Integer

	&apos; Number of columns of the document
	oTextColdocType = oStyle.TextColumns
	nColdocType = oTextColdocType.getColumnCount()
	
	&apos; 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
	
	&apos; Single column document
	If nColdocType = 0 Then &apos; nColdocType = 0 means single column!

		&apos; View cursor is not in a section
		If isEmpty( oViewCursor.TextSection ) Then
					
			&apos; Use default tabs
			tabpos1 = tabpos1_default
			tabpos2 = tabpos2_default
		
		&apos; View cursor is in a section
		Else
			
			&apos; Number of columns of the section
			oTextColsSec = oViewCursor.TextSection.TextColumns
			nColsSec = oTextColsSec.getColumnCount()

			&apos; Single column section
			If nColsSec = 0 Then
			
				&apos; Use default tabs
				tabpos1 = tabpos1_default
				tabpos2 = tabpos2_default
			
			&apos; Section with two columns or more
			Else
			
		 		&apos; Automatic column widths (columns of same width)	
		 		If oTextColsSec.IsAutomatic() Then
					
					&apos; Column width
					colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nColsSec - 1) * oTextColsSec.AutomaticDistance ) / nColsSec
			
					&apos; Compute tab positions (middle of the column and border of the column right margin)
					tabpos1 = colwidth / 2
					tabpos2 = colwidth
		
				&apos; Non automatic column widths (columns of different widths)	
				Else
				
					&apos; Use default tabs
					tabpos1 = tabpos1_default
					tabpos2 = tabpos2_default
				
				End If
			
			End If
			
		End If
	
	&apos; Document with two or more columns
	Else
		
 		&apos; Automatic column widths (columns of same width)	
 		If oTextColdocType.IsAutomatic() Then
			
			&apos; View cursor is not in a section
			If isEmpty( oViewCursor.TextSection ) Then
						
				&apos; Column width
				colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nColdocType - 1) * oTextColdocType.AutomaticDistance ) / nColdocType
		
				&apos; Compute tab positions (middle of the column and border of the column right margin)
				tabpos1 = colwidth / 2
				tabpos2 = colwidth
	
			&apos; View cursor is in a section
			Else
				
				&apos; Number of columns of the section
				oTextColsSec = oViewCursor.TextSection.TextColumns
				nColsSec = oTextColsSec.getColumnCount()
	
				&apos; Single column section
				If nColsSec = 0 Then
				
					&apos; Column width
					colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nColdocType - 1) * oTextColdocType.AutomaticDistance ) / nColdocType
			
					&apos; Compute tab positions (middle of the column and border of the column right margin)
					tabpos1 = colwidth / 2
					tabpos2 = colwidth
				
				&apos; Section with two columns or more
				Else
				
			 		&apos; Automatic column widths (columns of same width)	
			 		If oTextColsSec.IsAutomatic() Then
						
						&apos; Column width
						colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nColdocType - 1) * oTextColdocType.AutomaticDistance ) / nColdocType
						colwidth = colwidth / nColsSec
				
						&apos; Compute tab positions (middle of the column and border of the column right margin)
						tabpos1 = colwidth / 2
						tabpos2 = colwidth
			
					&apos; Non automatic column widths (columns of different widths)	
					Else
					
						&apos; Use default tabs
						tabpos1 = tabpos1_default
						tabpos2 = tabpos2_default
					
					End If
				
				End If
				
			End If

		&apos; Non automatic column widths (columns of different widths)	
		Else
		
			&apos; Use default tabs
			tabpos1 = tabpos1_default
			tabpos2 = tabpos2_default
		
		End If

	End If

	&apos; Get access to the document
	Dim oFrame as Variant
	Dim oDispatcher as Variant
	oFrame = ThisComponent.CurrentController.Frame
	oDispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)

	&apos; Possibly insert an empty paragraph before the equation
	If breakBeforeNum = TRUE Then oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())

	Dim caption as String, numlevel as Integer, nleft as Integer

	&apos; Caption is hidden
	If captionHidden = TRUE Then
	
		&apos; Insert a centered tab stop at position tabpos1
		oDispatcher.executeDispatch(oFrame, &quot;.uno:Tabstops&quot;, &quot;&quot;, 0, _
		                            Array( MakePropertyValue(&quot;Tabstops.TabStops&quot;, _
		                                                     Array(Array(tabpos1, com.sun.star.style.TabAlign.CENTER, &quot;,&quot;, &quot; &quot;))) ) )
		&apos; Insert tab
		oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertText&quot;, &quot;&quot;, 0, _
		                            Array( MakePropertyValue(&quot;Text&quot;, CHR$(9)) ) )

		&apos; Possibly insert an empty paragraph after the equation
		If breakAfterNum = TRUE Then

			oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())
			
			&apos; Go one character left
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, _
                                        Array( MakePropertyValue(&quot;Count&quot;, 1), _
                                               MakePropertyValue(&quot;Select&quot;, FALSE) ) )

		End If

	&apos; Caption is visible	
	Else
	
		&apos; Left aligned caption
		If captionLeftAlign = TRUE Then
		
			&apos; Insert a centered tab stop at position tabpos1
			oDispatcher.executeDispatch(oFrame, &quot;.uno:Tabstops&quot;, &quot;&quot;, 0, _
			                            Array( MakePropertyValue(&quot;Tabstops.TabStops&quot;, _
			                                                     Array(Array(tabpos1, com.sun.star.style.TabAlign.CENTER, &quot;,&quot;, &quot; &quot;))) ) )
		
			&apos; Insert the equation caption text at the left
			If len(oSystemInfo.EquationCaption) = 0 Then caption = &quot;()&quot; _
			                                    	Else caption = &quot;(&quot; &amp; oSystemInfo.EquationCaption &amp; &quot; )&quot;
			
			oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertText&quot;, &quot;&quot;, 0, _
			                            Array( MakePropertyValue(&quot;Text&quot;, caption + CHR$(9)) ) )
	
			&apos; Go two characters left
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, _
			                            Array( MakePropertyValue(&quot;Count&quot;, 2), _
			                                   MakePropertyValue(&quot;Select&quot;, FALSE) ) )
	
			&apos; Insert a numbering variable named &quot;Equation&quot;
	
			&apos; Set the numbering level
			If oSystemInfo.NumLevel = &quot;0&quot; Then numlevel = 127 Else numlevel = Val(oSystemInfo.NumLevel) - 1
		
			oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, _
	                                    Array( MakePropertyValue(&quot;Type&quot;, 23), _
	                                           MakePropertyValue(&quot;SubType&quot;, numlevel), _
	                                           MakePropertyValue(&quot;Name&quot;, &quot;Equation&quot;), _
	                                           MakePropertyValue(&quot;Content&quot;, &quot;&quot;), _
	                                           MakePropertyValue(&quot;Format&quot;, 4), _
	                                           MakePropertyValue(&quot;Separator&quot;, &quot;.&quot;) ) )
	
			&apos; Go two characters right
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoRight&quot;, &quot;&quot;, 0, _
	                                    Array( MakePropertyValue(&quot;Count&quot;, 2), _
	                                           MakePropertyValue(&quot;Select&quot;, FALSE) ) )
	
			&apos; Possibly insert an empty paragraph after the equation
			If breakAfterNum = TRUE Then
	
				oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())
				
				&apos; Go one character left
				oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, _
	                                        Array( MakePropertyValue(&quot;Count&quot;, 1), _
	                                               MakePropertyValue(&quot;Select&quot;, FALSE) ) )
	
			End If
		
		&apos; Right aligned caption
		Else
		
			&apos; Insert two tab stops at positions tabpos1 and tabpos2
			&apos; The first one is centered and the second one is right justified
			oDispatcher.executeDispatch(oFrame, &quot;.uno:Tabstops&quot;, &quot;&quot;, 0, _
	                                    Array( MakePropertyValue(&quot;Tabstops.TabStops&quot;, _
	                                                              Array(Array(tabpos1, com.sun.star.style.TabAlign.CENTER, &quot;,&quot;, &quot; &quot;), _
	                                                                    Array(tabpos2, com.sun.star.style.TabAlign.RIGHT, &quot;,&quot;, &quot; &quot;) ) ) ) )                                    	
			&apos; Insert the equation caption text at the right
			If len(oSystemInfo.EquationCaption) = 0 Then caption = &quot;()&quot;	_
			                                        Else caption = &quot;(&quot; &amp; oSystemInfo.EquationCaption &amp; &quot; )&quot;
			
			oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertText&quot;, &quot;&quot;, 0, _
	                                    Array( MakePropertyValue(&quot;Text&quot;, CHR$(9) + CHR$(9) + caption) ) )
			
			&apos; Possibly insert an empty paragraph after the equation
			&apos; And set the number of characters to go left
			If breakAfterNum = TRUE Then
	
				oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())
				nleft = 2
	
			Else
	
				nleft = 1
	
			End If
		
			&apos; Go one or two characters left
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, _
			                            Array( MakePropertyValue(&quot;Count&quot;, nleft), _
			                                   MakePropertyValue(&quot;Select&quot;, FALSE) ) )
	
			&apos; Insert a numbering variable named &quot;Equation&quot;
	
			&apos; Set the numbering level
			&apos;Dim numlevel as Integer
			If oSystemInfo.NumLevel = &quot;0&quot; Then numlevel = 127 Else numlevel = Val(oSystemInfo.NumLevel) - 1
		
			oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, _
		                                Array( MakePropertyValue(&quot;Type&quot;, 23), _
			                                   MakePropertyValue(&quot;SubType&quot;, numlevel), _
			                                   MakePropertyValue(&quot;Name&quot;, &quot;Equation&quot;), _
			                                   MakePropertyValue(&quot;Content&quot;, &quot;&quot;), _
			                                   MakePropertyValue(&quot;Format&quot;, 4), _
			                                   MakePropertyValue(&quot;Separator&quot;, &quot;.&quot;), ) )
			
			&apos; Go len(caption) + 1 characters left
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, _
	                                    Array( MakePropertyValue(&quot;Count&quot;, len(caption) + 1), _
			                                   MakePropertyValue(&quot;Select&quot;, FALSE) ) )
			                                   
		End If	
	
	End If 

	&apos; Set vertical alignment to baseline
	oDispatcher.executeDispatch(oFrame, &quot;.uno:VerticalParagraphAlignment&quot;, &quot;&quot;, 0, _
                                Array( MakePropertyValue(&quot;VerticalParagraphAlignment&quot;, 1) ) )

	&apos; Launch the TexMaths Equation main macro
	TexMaths.TexMathsEquations.main()

End Sub

</script:module>