<?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="TexMathsHelp" script:language="StarBasic">&apos;
&apos;    TexMathsHelp
&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 Help dialog


&apos; Force variable declaration
Option Explicit

Private oDlgBaseHelp as Variant
Private closed as Boolean

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

&apos; Flag used for gtk3 workaround
Private isLinux as Boolean


&apos; Main subroutine
Sub HelpDialog()

	DefaultSysConfig()

	&apos; Parent window
	Dim oParent as Variant
	oParent = ThisComponent.getCurrentController().getFrame().getContainerWindow()
      
	&apos; Create base outer window as non modal dialog
	oDlgBaseHelp = CreateBaseDialog(oParent)
 
	&apos; Set window title
	oDlgBaseHelp.setTitle( _(&quot;TexMaths Help&quot;) )
     
	&apos; Embed inner dialog created with the dialog editor
	Dim URL as String, oDlgHelp as Variant
	URL = &quot;vnd.sun.star.script:TexMaths.TexMathsHelp_GUI?location=application&quot;
	oDlgHelp = CreateInnerDialog(oDlgBaseHelp, URL)

	&apos; Load help text, depending on the locale 
	Dim helpPath as String, filePath as String, msg as String
	helpPath = &quot;help/help.&quot; &amp; GetLocale()
	filePath = ConvertToURL( glb_PkgPath &amp; helpPath )
	If Not FileExists( filePath ) Then	helpPath =  &quot;help/help.en&quot;
	msg = ReadTextFileUtf8(helpPath, glb_PkgPath)
	msg = chr(10) &amp; &quot; TexMaths &quot; &amp; GetTexMathversion() &amp; chr(10) &amp; msg
	oDlgHelp.getControl(&quot;HelpText&quot;).setText(msg)
    
 	&apos; Read registry
	Dim oSystemInfo as Variant
	oSystemInfo = GetConfigAccess(&quot;/ooo.ext.texmaths.Registry/SystemInfo&quot;, TRUE)

	Dim aRect as New com.sun.star.awt.Rectangle
	aRect = CreateUnoStruct(&quot;com.sun.star.awt.Rectangle&quot;)

	&apos; Compute conversion factors between map appFonts and pixels
	aRect = oDlgHelp.getPosSize()
	convX = aRect.Width / oDlgHelp.Model.Width 
	convY = aRect.Height / oDlgHelp.Model.Height

	Dim minW as Integer, minH as Integer
	minW = DLG_MIN_WIDTH * convX
	minH = DLG_MIN_HEIGHT * convY

	&apos; Set outer window initial position and size
	If oSystemInfo.HelpPositionX &lt;&gt; &quot;&quot; Then	aRect.X = oSystemInfo.HelpPositionX Else aRect.X = -1
		
	If oSystemInfo.HelpPositionY &lt;&gt; &quot;&quot; Then	aRect.Y = oSystemInfo.HelpPositionY	Else aRect.Y = -1

	If oSystemInfo.HelpWidth = &quot;&quot; Or Val(oSystemInfo.HelpWidth) &lt; minW Then	aRect.Width = minW Else	aRect.Width = oSystemInfo.HelpWidth

	If oSystemInfo.HelpHeight = &quot;&quot; Or Val(oSystemInfo.HelpHeight) &lt; minH Then aRect.Height = minH Else aRect.Height = oSystemInfo.HelpHeight

	If aRect.X &gt;= 0 And aRect.Y &gt;= 0 Then oDlgBaseHelp.setPosSize(aRect.X, aRect.Y, 0, 0, com.sun.star.awt.PosSize.POS)
	
	If aRect.Width &gt; 0 And aRect.Height &gt; 0 Then oDlgBaseHelp.setPosSize(0, 0, aRect.Width, aRect.Height, com.sun.star.awt.PosSize.SIZE)

	&apos; Find if Linux system
	If getGUIType() &lt;&gt; 1 Then

		If getUname() &lt;&gt; &quot;Darwin&quot; Then isLinux = TRUE

	Else

		isLinux = FALSE

	End If 

	&apos; Workaround for a bug with gtk3 backend =&gt; dialog is set to modal in Linux
	If isLinux Then &apos; Modal dialog
		
		oDlgBaseHelp.execute()
	
 	Else &apos;Non modal dialog
		
		&apos; Show dialog
		oDlgBaseHelp.setVisible(TRUE)
	
		&apos; Loop for non modal dialog
		closed = FALSE
		While(closed = FALSE)

			Wait 1000

		Wend

	End If
	
	&apos; Get outer window position and size
	aRect = oDlgBaseHelp.GetPosSize()

	&apos; Delete dialog
	oDlgBaseHelp.dispose()

	&apos; Save outer window position and size to the registry
	oSystemInfo.HelpPositionX = aRect.X
	oSystemInfo.HelpPositionY = aRect.Y

	&apos; Dialog is too narrow
	If aRect.Width &lt;  minW Then	aRect.Width = minW
		
	&apos; Dialog is too short
	If aRect.Height &lt;  minH Then aRect.Height = minH

	oSystemInfo.HelpWidth = aRect.Width
	oSystemInfo.HelpHeight = aRect.Height
	
	&apos; Apply changes
	oSystemInfo.commitChanges()

End Sub


&apos; Create new outer base dialog
&apos; oParent: parent window peer
&apos; Return dialog window
Function CreateBaseDialog(oParent as Variant) as Variant
     
	Dim oToolkit as Variant
	oToolkit = oParent.getToolkit()
	
	Dim WA as Variant, aRect as Variant, oDesc as Variant
	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)

	&apos; Create listeners
	oDialog.addTopWindowListener(CreateUnoListener(&quot;HelpWindowListener_&quot;, &quot;com.sun.star.awt.XTopWindowListener&quot;))
	oDialog.addWindowListener(CreateUnoListener(&quot;HelpWindowListener_&quot;, &quot;com.sun.star.awt.XWindowListener&quot;))

	CreateBaseDialog = oDialog

End Function


&apos; Create inner (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) as Variant

	Dim PS as Variant
	PS = com.sun.star.awt.PosSize
	
	Dim oDP as Variant, oChildDialog as Variant
	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; Close button label and tooltip
	oChildDialog.getControl(&quot;CloseButton&quot;).Model.Label = _(&quot;Close&quot;)
	oChildDialog.getControl(&quot;CloseButton&quot;).Model.HelpText = _(&quot;Close window&quot;)
	
	Dim aSize as Variant
	aSize = oChildDialog.getPosSize()
	oParent.setPosSize(0, 0, aSize.Width, aSize.Height, PS.SIZE)
	oChildDialog.addWindowListener(CreateUnoListener(&quot;HelpChildWindowListener_&quot;, &quot;com.sun.star.awt.XWindowListener&quot;))
	
	CreateInnerDialog = oChildDialog

End Function


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

	&apos; Inner dialog
	Dim oDialog as Variant
	oDialog = ev.Source
	
	&apos; Dialog size
	Dim aRect as Variant
	aRect = oDialog.getPosSize()
	
	&apos; Dialog min width and height 
	Const HLP_MIN_WIDTH  = 200
	Const HLP_MIN_HEIGHT = 140

	&apos; Convert min width and height to pixels
	Dim minW as Integer, minH as Integer
	minW = HLP_MIN_WIDTH * convX
	minH = HLP_MIN_HEIGHT * convY

	&apos; Resize inner dialog if too narrow
	If aRect.Width &lt; minW Then

		aRect.Width = minW
		oDialog.setPosSize(0, 0, aRect.Width, aRect.Height, com.sun.star.awt.PosSize.SIZE)

	End If

	&apos; Resize inner dialog if too short
	If aRect.Height &lt; minH Then

		aRect.Height = minH
		oDialog.setPosSize(0, 0, aRect.Width, aRect.Height, com.sun.star.awt.PosSize.SIZE)

	End If

	Dim oWidget as Variant
	Dim X as Integer, Y as Integer, W as Integer, H as Integer

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

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

End Sub


&apos; Close non modal dialog
Sub CloseHelp()

	&apos; Workaround for a bug with gtk3 backend
	If isLinux Then &apos; Modal dialog

		oDlgBaseHelp.endExecute()
		
	Else &apos; Non modal dialog

		oDlgBaseHelp.setVisible(FALSE)
		closed = TRUE
		
	End If

End Sub


Sub HelpChildWindowListener_windowMoved(ev)
End Sub

Sub HelpChildWindowListener_windowShown(ev)
End Sub

Sub HelpChildWindowListener_windowHidden(ev)
End Sub


&apos;Invoked when a window is resized
Sub HelpWindowListener_windowResized(ev)
      
	&apos; Inner dialog 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
	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 HelpWindowListener_windowClosing(ev)
    
	&apos; Workaround for a bug with gtk3 backend
	If isLinux Then &apos; Modal dialog

		ev.Source.endExecute()
		
	Else &apos; Non modal dialog
	
		&apos; Close window using the window bar close button
		ev.Source.setVisible(FALSE)
		closed = TRUE
	
	End If

End Sub


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

&apos; Invoked when a window has been closed
Sub HelpWindowListener_windowClosed(ev)
End Sub

&apos; Invoked when a window is iconified
Sub HelpWindowListener_windowMinimized(ev)
End Sub

&apos; Invoked when a window is de-iconified
Sub HelpWindowListener_windowNormalized(ev)
End Sub

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

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

Sub HelpWindowListener_disposing(ev)
End Sub

Sub HelpWindowListener_windowMoved(ev)
End Sub

Sub HelpWindowListener_windowShown(ev)
End Sub

Sub HelpWindowListener_windowHidden(ev)
End Sub


</script:module>