<?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="TexMathsError_Dlg" script:language="StarBasic" script:moduleType="normal">&apos;
&apos;    TexMathsError
&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;	 Macros used to create and manage the Error dialog


&apos; Force variable declaration
Option Explicit

Private oDlgBaseError as Variant
Private oDlgError as Variant

&apos; Conversion factors map appFonts -&gt; pixels
Private convX as Double
Private convY as Double


&apos; Modal error dialog
Sub ErrorDialog(str as String)
      
	&apos; Parent window
    Dim oParent as Variant  
	oParent = ThisComponent.getCurrentController().getFrame().getContainerWindow()
      
	&apos; Create base window as modal dialog
	oDlgBaseError = CreateBaseDialog(oParent)
	oDlgBaseError.setTitle( _(&quot;Error&quot;) )

	&apos; Embed the dialog created by the dialog editor
	Dim URL as String
	URL = &quot;vnd.sun.star.script:TexMaths.TexMathsError_GUI?location=application&quot;
	oDlgError = CreateInnerDialog(oDlgBaseError, URL, str)
       
	&apos; Compute conversion factors between map appfonts and pixels
	Dim aRect as Variant
	aRect = oDlgError.getPosSize()
	convX = aRect.Width / oDlgError.Model.Width 
	convY = aRect.Height / oDlgError.Model.Height

	&apos; Show the modal dialog, has to be done by the outer dialog
	If oDlgBaseError.execute() &gt; 0 Then	MsgBox( _(&quot;Error: dialog box can&apos;t be created...&quot;), 48, &quot;TexMaths&quot; )

	oDlgBaseError.dispose()
    
End Sub


&apos; Create new base modal dialog
&apos; oParent: parent window peer
&apos; Return dialog window
Function CreateBaseDialog(oParent as Variant) as Variant
     
	Dim oToolkit as Variant, WA as Variant, aRect as Variant, oDesc as Variant
	oToolkit = oParent.getToolkit()
	WA = com.sun.star.awt.WindowAttribute
	aRect = CreateUnoStruct(&quot;com.sun.star.awt.Rectangle&quot;)
	oDesc = CreateUnoStruct(&quot;com.sun.star.awt.WindowDescriptor&quot;)

	&apos;With oDesc

    	oDesc.Type = com.sun.star.awt.WindowClass.SIMPLE
        oDesc.WindowServiceName = &quot;dialog&quot;
        oDesc.Parent = oParent
        oDesc.ParentIndex = -1
        oDesc.Bounds = aRect
        oDesc.WindowAttributes = WA.CLOSEABLE OR WA.MOVEABLE OR WA.SIZEABLE OR WA.BORDER

	&apos;End With

	Dim oDialog as Variant
	oDialog = oToolkit.createWindow(oDesc)
	oDialog.addTopWindowListener(CreateUnoListener(&quot;ErrorWindowListener_&quot;, &quot;com.sun.star.awt.XTopWindowListener&quot;))
	oDialog.addWindowListener(CreateUnoListener(&quot;ErrorWindowListener_&quot;, &quot;com.sun.star.awt.XWindowListener&quot;))
	CreateBaseDialog = oDialog

End Function


&apos; Create child dialog embedded
&apos; oParent: parent window
&apos; URL: dialog URL, without toolbar
&apos; Return dialog window
Function CreateInnerDialog(oParent as Variant, URL as String, str as String) as Variant

	Dim PS as Variant, oDP as Variant, oChildDialog as Variant
	PS = com.sun.star.awt.PosSize
	oDP = CreateUnoService(&quot;com.sun.star.awt.ContainerWindowProvider&quot;)
	oChildDialog = oDP.createContainerWindow(URL, &quot;&quot;, oParent, nothing)
	oChildDialog.setPosSize(0, 0, 0, 0, PS.POS)
	oChildDialog.setVisible(TRUE)

	&apos; Dialog Strings for translation
	oChildDialog.getControl(&quot;CloseButton&quot;).Model.Label = _(&quot;Close&quot;)
	oChildDialog.getControl(&quot;CloseButton&quot;).Model.HelpText = _(&quot;Close window&quot;)
	oChildDialog.getControl(&quot;LogButton&quot;).Model.Label = _(&quot;Log...&quot;)
	oChildDialog.getControl(&quot;LogButton&quot;).Model.HelpText = _(&quot;Display log&quot;)
	oChildDialog.getControl(&quot;CodeButton&quot;).Model.Label = _(&quot;LaTeX code...&quot;)
	oChildDialog.getControl(&quot;CodeButton&quot;).Model.HelpText = _(&quot;Display LaTeX code&quot;)

	Dim aSize as Variant
	aSize = oChildDialog.getPosSize()
	oParent.setPosSize(0, 0, aSize.Width, aSize.Height, PS.SIZE)
	oChildDialog.addWindowListener(CreateUnoListener(&quot;ErrorChildWindow_&quot;, &quot;com.sun.star.awt.XWindowListener&quot;))

	&apos; Set text and background color
	Dim oTextField as Variant
	oTextField = oChildDialog.Model.ErrorText
	oTextField.Text = str
	oTextField.BackgroundColor = oChildDialog.StyleSettings.DialogColor

	CreateInnerDialog = oChildDialog

End Function


&apos; Close base dialog
Sub CloseDialog()

	oDlgBaseError.endExecute()
	
End Sub


&apos; View LaTeX code
Sub ViewLatexCode()

	PrintFile( &quot;tmpfile.tex&quot;, ConvertFromURL(glb_TmpPath) &amp; &quot;tmpfile.tex&quot; )
	
End Sub



&apos; View Log
Sub ViewLog()

	PrintFile( &quot;tmpfile.log&quot;, ConvertFromURL(glb_TmpPath) &amp; &quot;tmpfile.log&quot; )
	
End Sub


&apos; Resize base dialog window
&apos; This is called indirectly when resizing the outer dialog      
Sub ErrorChildWindow_windowResized(ev)

	&apos; Inner dialog
	Dim oDialog as Variant
	oDialog = ev.Source
	
	&apos; Dialog size
	Dim aRect as Variant
	aRect = oDialog.getPosSize()
	
	Dim X as Integer, Y as Integer, W as Integer, H as Integer

	&apos; Resize text field
	Dim oTextField as Variant
	oTextField = oDialog.getControl(&quot;ErrorText&quot;)
	W = aRect.Width - (oDialog.Model.Width - oTextField.Model.Width ) * convX
	H = aRect.Height - (oDialog.Model.Height - oTextField.Model.Height ) * convY
	oTextField.setPosSize(0, 0, W, H, com.sun.star.awt.PosSize.SIZE)

	&apos; Position view LaTeX code button
	Dim oButton as Variant
	oButton = oDialog.getControl(&quot;ViewButton&quot;)
	X = oButton.Model.PositionX * convX
	Y = aRect.Height - (oDialog.Model.Height - oButton.Model.PositionY ) * convY
	oButton.setPosSize(X , Y, 0, 0, com.sun.star.awt.PosSize.POS)

	&apos; Position close button
	oButton = oDialog.getControl(&quot;CloseButton&quot;)
	X = aRect.Width - (oDialog.Model.Width - oButton.Model.PositionX) * convX
	Y = aRect.Height - (oDialog.Model.Height - oButton.Model.PositionY ) * convY
	oButton.setPosSize(X , Y, 0, 0, com.sun.star.awt.PosSize.POS)

End Sub


Sub ErrorChildWindow_windowMoved(ev)
End Sub

Sub ErrorChildWindow_windowShown(ev)
End Sub

Sub ErrorChildWindow_windowHidden(ev)
End Sub


&apos;Invoked when a window is resized
Sub ErrorWindowListener_windowResized(ev)
      
	&apos; Window size
	Dim aSize as Variant
	aSize = ev.Source.getSize()

	&apos; Child window index is 0 for OpenOffice and 2 for LibreOffice
	Dim i as Integer
	If ev.Source.Windows(0).isVisible Then i = 0 Else i = 2

	&apos; Resize inner dialog as child window with number i
	ev.Source.Windows(i).setPosSize(0, 0, aSize.Width, aSize.Height, com.sun.star.awt.PosSize.SIZE)

End Sub


&apos;Invoked when a window is in the process of being closed
Sub ErrorWindowListener_windowClosing(ev)
    
	&apos; Close window using the window bar close button
	ev.Source.endExecute()

End Sub


&apos;Invoked when a window has been opened
Sub ErrorWindowListener_windowOpened(ev)
End Sub

&apos; Is invoked when a window has been closed
Sub ErrorWindowListener_windowClosed(ev)
End Sub

&apos; Is invoked when a window is iconified
Sub ErrorWindowListener_windowMinimized(ev)
End Sub

&apos; Is invoked when a window is de-iconified
Sub ErrorWindowListener_windowNormalized(ev)
End Sub

&apos;Invoked when a window is activated
Sub ErrorWindowListener_windowActivated(ev)
End Sub

&apos;Invoked when a window is de-activated
Sub ErrorWindowListener_windowDeactivated(ev)
End Sub

Sub ErrorWindowListener_disposing(ev)
End Sub

Sub ErrorWindowListener_windowMoved(ev)
End Sub

Sub ErrorWindowListener_windowShown(ev)
End Sub

Sub ErrorWindowListener_windowHidden(ev)
End Sub

</script:module>