File: TexMathsNumberedEquations.xba

package info (click to toggle)
libreoffice-texmaths 0.49-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,388 kB
  • sloc: sh: 67; xml: 32; makefile: 2
file content (350 lines) | stat: -rw-r--r-- 12,411 bytes parent folder | download
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">
&apos;
&apos;    TexMathsEquationsNumbered
&apos;
&apos;	 Copyright (C) 2012-2020 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; 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(&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.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; 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 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

	&apos; Number of columns of the document
	oTextColsDoc = oStyle.TextColumns
	nbColsDoc = oTextColsDoc.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 nbColsDoc = 0 Then &apos; nbColsDoc = 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
			nbColsSec = oTextColsSec.getColumnCount()

			&apos; Single column section
			If nbColsSec = 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 - (nbColsSec - 1) * oTextColsSec.AutomaticDistance ) / nbColsSec
			
					&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 oTextColsDoc.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 - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
	
			&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
			nbColsSec = oTextColsSec.getColumnCount()

			&apos; Single column section
			If nbColsSec = 0 Then
			
				&apos; Column width
				colwidth = (oStyle.Width - oStyle.LeftMargin - oStyle.RightMargin - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
		
				&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 - (nbColsDoc - 1) * oTextColsDoc.AutomaticDistance ) / nbColsDoc
					colwidth = colwidth / nbColsSec
			
					&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; Eventually insert an empty paragraph before the equation
	If BreakBeforeNum = TRUE Then oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertPara&quot;, &quot;&quot;, 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
	
	&apos; Left aligned caption
	If CaptionLeftAlign = TRUE Then
	
		&apos; Insert a centered tab stop at position tabpos1
		args1(0).Name = &quot;Tabstops.TabStops&quot;
		args1(0).Value = Array(Array(tabpos1,com.sun.star.style.TabAlign.CENTER,&quot;,&quot;,&quot; &quot;))
		oDispatcher.executeDispatch(oFrame, &quot;.uno:Tabstops&quot;, &quot;&quot;, 0, args1())
	
		&apos; Insert the equation caption text at the left
		If len(oSystemInfo.EquationCaption) = 0 Then
			sCaption = &quot;()&quot;
		Else
			sCaption = &quot;(&quot; &amp; oSystemInfo.EquationCaption &amp; &quot; )&quot;
		End If
		args1(0).Name = &quot;Text&quot;
		args1(0).Value = sCaption + CHR$(9)
		oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertText&quot;, &quot;&quot;, 0, args1())
	
		&apos; Go two characters left
		args2(0).Name = &quot;Count&quot;
		args2(0).Value = 2
		args2(1).Name = &quot;Select&quot;
		args2(1).Value = false
		oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, args2())
	
		&apos; Insert a numbering variable named Equation
		args3(0).Name = &quot;Type&quot;
		args3(0).Value = 23
		args3(1).Name = &quot;SubType&quot;
	
		&apos; Set the numbering level
		If oSystemInfo.NumLevel = &quot;0&quot; Then
			args3(1).Value = 127	
		Else
			args3(1).Value = Val(oSystemInfo.NumLevel) - 1
		End If
	
		args3(2).Name = &quot;Name&quot;
		args3(2).Value = &quot;Equation&quot;
		args3(3).Name = &quot;Content&quot;
		args3(3).Value = &quot;&quot;
		args3(4).Name = &quot;Format&quot;
		args3(4).Value = 4
		args3(5).Name = &quot;Separator&quot;
		args3(5).Value = &quot;.&quot;
		oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, args3())
	
		&apos; Go two characters right
		args2(0).Name = &quot;Count&quot;
		args2(0).Value = 2
		args2(1).Name = &quot;Select&quot;
		args2(1).Value = false
		oDispatcher.executeDispatch(oFrame, &quot;.uno:GoRight&quot;, &quot;&quot;, 0, args2())

		&apos; Eventually 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
			args2(0).Name = &quot;Count&quot;
			args2(0).Value = 1
			args2(1).Name = &quot;Select&quot;
			args2(1).Value = false
			oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, args2())
	
		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
		args1(0).Name = &quot;Tabstops.TabStops&quot;
		args1(0).Value = 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;))
		oDispatcher.executeDispatch(oFrame, &quot;.uno:Tabstops&quot;, &quot;&quot;, 0, args1())
	
		&apos; Insert the equation caption text at the right
		If len(oSystemInfo.EquationCaption) = 0 Then
			sCaption = &quot;()&quot;
		Else
			sCaption = &quot;(&quot; &amp; oSystemInfo.EquationCaption &amp; &quot; )&quot;
		End If
		args1(0).Name = &quot;Text&quot;
		args1(0).Value = CHR$(9) + CHR$(9) + sCaption
		oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertText&quot;, &quot;&quot;, 0, args1())
	
		&apos; Evetually insert an empty paragraph after the equation
		&apos; And set the number of characters to go left
		Dim nleft as Integer
		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
		args2(0).Name = &quot;Count&quot;
		args2(0).Value = nleft
		args2(1).Name = &quot;Select&quot;
		args2(1).Value = false
		oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, args2())
	
		&apos; Insert a numbering variable named Equation
		args3(0).Name = &quot;Type&quot;
		args3(0).Value = 23
		args3(1).Name = &quot;SubType&quot;
	
		&apos; Set the numbering level
		If oSystemInfo.NumLevel = &quot;0&quot; Then
			args3(1).Value = 127	
		Else
			args3(1).Value = Val(oSystemInfo.NumLevel) - 1
		End If
	
		args3(2).Name = &quot;Name&quot;
		args3(2).Value = &quot;Equation&quot;
		args3(3).Name = &quot;Content&quot;
		args3(3).Value = &quot;&quot;
		args3(4).Name = &quot;Format&quot;
		args3(4).Value = 4
		args3(5).Name = &quot;Separator&quot;
		args3(5).Value = &quot;.&quot;
		oDispatcher.executeDispatch(oFrame, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, args3())
	
		&apos; Go len(sCaption)+1 characters left
		args2(0).Name = &quot;Count&quot;
		args2(0).Value = len(sCaption)+1
		args2(1).Name = &quot;Select&quot;
		args2(1).Value = false
		oDispatcher.executeDispatch(oFrame, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, args2())

	End If

	&apos; Set vertical alignment to middle
	dim args4(0) as new com.sun.star.beans.PropertyValue
	args4(0).Name = &quot;VerticalParagraphAlignment&quot;
	args4(0).Value = 3
	oDispatcher.executeDispatch(oFrame, &quot;.uno:VerticalParagraphAlignment&quot;, &quot;&quot;, 0, args4())

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

End Sub
</script:module>