Getting embedded objects in selection in OOo Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
lmari
Posts: 7
Joined: Mon Mar 03, 2008 1:00 pm

Getting embedded objects in selection in OOo Basic

Post by lmari »

Hello.

I am trying to write an OOo Basic macro enabling the automatic resize of the Math formulas embedded as objects in a Writer document. The behavior I would like to implement is:
1. if there is no selection, then operate on the whole document;
2. else operate only on the objects in the selection.

The logic to solve the problem 1 is:

Code: Select all

for i = 0 to ThisComponent.getEmbeddedObjects().count - 1
  obj = objs(i).getEmbeddedObject()
  if (not IsNull(obj)) and obj.supportsService("com.sun.star.formula.FormulaProperties") then
    obj.BaseFontHeight = newFontSize ' set somehow
  end if
next i
but I am not able to obtain the collection of the objects in the current selection, given the fact that, for example:

Code: Select all

objs_in_sel = doc.getCurrentSelection().getByIndex(0).getEmbeddedObjects()
does not work.

I would really appreciate an help.

Luca
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Getting embedded objects in selection in OOo Basic

Post by ms777 »

Hi,

try something along the following code ... note that only formulas anchored 'As Character' are detected

Good luck,

ms777

Code: Select all

Sub Main
oDoc = ThisComponent
oSel = oDoc.CurrentController.Selection

if oSel.supportsService("com.sun.star.text.TextEmbeddedObject") then
  if oSel.EmbeddedObject.supportsService("com.sun.star.formula.FormulaProperties") then
    msgbox "Formula"
    exit sub
    endif
  endif

if oSel.supportsService("com.sun.star.text.TextRanges") then
  oTR = oSel.getByIndex(0)
  oEnum = oTR.createEnumeration( ) 
  while oEnum.hasMoreElements() 
    oTR1 = oEnum.nextElement()
    oEnum1 = oTR1.createEnumeration( ) 
    while oEnum1.hasMoreElements() 
      oTR2 = oEnum1.nextElement()
      if oTR2.TextPortionType = "Frame" then
        oEnum2 = oTR2.createContentEnumeration("com.sun.star.text.TextEmbeddedObject") 
        while oEnum2.hasMoreElements() 
          oTR3 = oEnum2.nextElement()
          if oTR3.EmbeddedObject.supportsService("com.sun.star.formula.FormulaProperties") then
            msgbox "Formula"
            endif
          wend 
        endif
      wend 
    wend 
  endif
End Sub
lmari
Posts: 7
Joined: Mon Mar 03, 2008 1:00 pm

Re: Getting embedded objects in selection in OOo Basic

Post by lmari »

Thank you so much.

Your solution is nice and it does work.

Now a further problem, then.
After having changed the formula size, I would like to redraw the formula itself.
I found on this forum the following solution:

Code: Select all

temp = oFormula.Formula ' some tricks to force OO.o redraw formulas
oFormula.Formula = ""
oFormula.Formula = temp
which is not really elegant but if it works...
The point is that this code generates an odd behavior (at least on my OO 2.3.1 on Linux): only formulas that have been pre-selected (i.e., a double click on them before running the macro) automatically redraw. For the others, the formula size is actually changed, but the redraw actually happens only as the result of a double click on them.

Any idea?

Luca
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Getting embedded objects in selection in OOo Basic

Post by ms777 »

lmari wrote:...but the redraw actually happens only as the result of a double click on them.

Any idea?

Luca
Hi,

try the following. It only works, if the formula is not selected. Tested on OO 2.1 on XP

ms777

Code: Select all

Sub Main
oFormula = ThisComponent.EmbeddedObjects.getByIndex(0)
oFormula.EmbeddedObject.BaseFontHeight = oFormula.EmbeddedObject.BaseFontHeight *2
oClientSite = oFormula.ExtendedControlOverEmbeddedObject.ClientSite
oClientSite.activatingInplace()
oClientSite.deactivatedInplace()
End Sub
lmari
Posts: 7
Joined: Mon Mar 03, 2008 1:00 pm

Re: Getting embedded objects in selection in OOo Basic

Post by lmari »

Thank you once more, but I need to redraw all the formulas whose size has been changed, and I am not able to merge the last solution you have proposed inside the code you suggested in your first post.
May you be so kind to show how to do it?

Luca
72julien350
Posts: 4
Joined: Fri Nov 22, 2013 11:13 pm

Re: Getting embedded objects in selection in OOo Basic

Post by 72julien350 »

I search to do the same thing (modify the height of formulas in selections)
After several hours, i don't succeed.
Imari, can you put your macro in this forum please.
Thank you very very very much.
LibreOffice4 on Windows7
72julien350
Posts: 4
Joined: Fri Nov 22, 2013 11:13 pm

Re: Getting embedded objects in selection in OOo Basic

Post by 72julien350 »

Hello,
Finaly i succeed with help of C. Develland with his macro Cmath
http://cdeval.free.fr/

Code: Select all

Sub FormatFormuleNormal
oDoc = ThisComponent
oSel = oDoc.CurrentController.Selection
'taille = InputBox("taille ?","Taille",11)

'a=oSel.supportsService("com.sun.star.text.TextEmbeddedObject")
'Print(a)
if oSel.supportsService("com.sun.star.text.TextEmbeddedObject") then
'	embeddedObjects =ThisComponent.getEmbeddedObjects()
'	elementNames = embeddedObjects.getElementNames()
'	
'	for i=0 to UBOUND(elementNames)	' pour tous les objets du document
'	element = embeddedObjects.getByName(elementNames(i)).Model
'	
 ' 		if element.supportsService("com.sun.star.formula.FormulaProperties") then
    '	element.BaseFontHeight = taille				' Taille de base
'
 ' 		endif	' fin si formule
'	next i		' fin boucle pour tous les objets
'	ThisComponent.reformat()
	
Else ' jusque la ca marche

	oLaFormule=oSel.getbyIndex(0)
	sMonEnum=oLaFormule.getAvailableServiceNames
	oLesObjets=oLaFormule.createContentEnumeration(sMonEnum(0))
	
	while oLesObjets.HasMoreElements
		oLaFormule=oLesObjets.NextElement
	
		if oLaFormule.supportsService("com.sun.star.text.TextEmbeddedObject") then	
		'	oLaFormule.model.BaseFontHeight = taille
			oLaFormule.model.BaseFontHeight = 13					' Taille de base
			oLaFormule.model.FontNameVariables= "Linux Libertine G"		' Police des variables
			oLaFormule.model.FontNameFunctions = "Linux Libertine G"	' Police des fonctions
			oLaFormule.model.FontNameNumbers= "Linux Libertine G"			' Police des nombres
			oLaFormule.model.FontNameText= "Linux Libertine G"				' Police du texte
			oLaFormule.model.CustomFontNameSerif= "Linux Libertine G"	' Police serif
			oLaFormule.model.CustomFontNameSans= "Linux Libertine G"		' Police sans
			oLaFormule.model.CustomFontNameFixed= "Linux Libertine G"	' Police fixe
		endif
		oLaFormule.ExtendedControlOverEmbeddedObject.update()
	wend
endif
	ThisComponent.reformat()
End Sub
LibreOffice4 on Windows7
Post Reply