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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
<?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="TexMathsPreamble" script:language="StarBasic">'
' TexMathsPreamble
'
' Copyright (C) 2012-2020 Roland Baudin (roland65@free.fr)
' Based on the work of Geoffroy Piroux (gpiroux@gmail.com)
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'
' Macros used to create and manage the Preamble dialog
' Force variable declaration
Option Explicit
' Dialog declaration
Private oDlgBasePreamble as Variant
Private oDlgPreamble as Variant
' Conversion factors map appFonts -> pixels
Private convX as Double
Private convY as Double
' Main subroutine
Sub PreambleDialog()
DefaultSysConfig()
' Parent window
Dim oParent as Variant
oParent = ThisComponent.getCurrentController().getFrame().getContainerWindow()
' Create base outer window as modal dialog
oDlgBasePreamble = CreateBaseDialog(oParent)
' Set window title
oDlgBasePreamble.setTitle( _("TexMaths Preamble") )
' Embed inner dialog created with the dialog editor
Dim sURL as String
sURL = "vnd.sun.star.script:TexMaths.TexMathsPreamble_GUI?location=application"
oDlgPreamble = CreateInnerDialog(oDlgBasePreamble, sURL)
' Set the preamble text
oDlgPreamble.getControl("Preamble").setText(glb_Preamble)
oDlgPreamble.getControl("Preamble").Model.FontName = "Monospace"
Dim aRect as New com.sun.star.awt.Rectangle
aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")
' Compute conversion factors between map appFonts and pixels
aRect = oDlgPreamble.getPosSize()
convX = aRect.Width / oDlgPreamble.Model.Width
convY = aRect.Height / oDlgPreamble.Model.Height
' Show the modal dialog, has to be done by the outer dialog
If oDlgBasePreamble.execute() > 0 Then
MsgBox( _("Error: dialog box can't be created..."), 0, "TexMaths")
End If
' Delete dialog
oDlgBasePreamble.dispose()
End Sub
' Create new outer base dialog
' oParent: parent window peer
' 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("com.sun.star.awt.Rectangle")
oDesc = CreateUnoStruct("com.sun.star.awt.WindowDescriptor")
With oDesc
.Type = com.sun.star.awt.WindowClass.SIMPLE
.WindowServiceName = "dialog"
.Parent = oParent
.ParentIndex = -1
.Bounds = aRect
.WindowAttributes = WA.CLOSEABLE OR WA.MOVEABLE OR WA.SIZEABLE OR WA.BORDER
End With
Dim oDialog as Variant
oDialog = oToolkit.createWindow(oDesc)
' Create listeners
oDialog.addTopWindowListener(CreateUnoListener("PreambleWindowListener_", "com.sun.star.awt.XTopWindowListener"))
oDialog.addWindowListener(CreateUnoListener("PreambleWindowListener_", "com.sun.star.awt.XWindowListener"))
CreateBaseDialog = oDialog
End Function
' Create inner (child) dialog embedded
' oParent: parent window
' sURL: dialog URL, without toolbar
' Return dialog window
Function CreateInnerDialog(oParent as Variant, sURL as String) as Variant
Dim PS as Variant
PS = com.sun.star.awt.PosSize
Dim oDP as Variant, oChildDialog as Variant
oDP = CreateUnoService("com.sun.star.awt.ContainerWindowProvider")
oChildDialog = oDP.createContainerWindow(sURL, "", oParent, nothing)
oChildDialog.setPosSize(0, 0, 0, 0, PS.POS)
oChildDialog.setVisible(True)
' Dialog strings for translation
oChildDialog.setTitle( _("TexMaths Preamble") )
oChildDialog.getControl("LoadDefaultButton").Model.Label = _("Load Default")
oChildDialog.getControl("LoadDefaultButton").Model.HelpText = _("Load default preamble")
oChildDialog.getControl("LoadLocalButton").Model.Label = _("Load Local")
oChildDialog.getControl("LoadLocalButton").Model.HelpText = _("Load local preamble (from the current document)")
oChildDialog.getControl("LoadGlobalButton").Model.Label = _("Load Global")
oChildDialog.getControl("LoadGlobalButton").Model.HelpText = _("Load global preamble")
oChildDialog.getControl("ApplyButton").Model.Label = _("Apply")
oChildDialog.getControl("ApplyButton").Model.HelpText = _("Apply current preamble")
oChildDialog.getControl("SaveGlobalButton").Model.Label = _("Save Global")
oChildDialog.getControl("SaveGlobalButton").Model.HelpText = _("Save current preamble as global and apply it")
oChildDialog.getControl("SaveLocalButton").Model.Label = _("Save Local")
oChildDialog.getControl("SaveLocalButton").Model.HelpText = _("Save current preamble as local (into the current document) and apply it")
oChildDialog.getControl("IgnorePreamble").Model.Label = _("Do not use preamble for LaTeX type equations")
oChildDialog.getControl("IgnorePreamble").Model.HelpText = _("Ignore preamble when generating LaTeX type equations. Full LaTeX code must then be entered in the TexMaths equation dialog.")
' Set the value
oChildDialog.getControl("Preamble").setText(glb_Preamble)
' Ignore preamble button state
If glb_IgnorePreamble Then
oChildDialog.getControl("IgnorePreamble").setState(1)
Else
oChildDialog.getControl("IgnorePreamble").setState(0)
End If
' The local load button is activated if a preamble is stored in the current document
If ReadLocalPreamble() = 1 Then
oChildDialog.getControl("LoadLocalButton").setEnable(TRUE)
Else
oChildDialog.getControl("LoadLocalButton").setEnable(FALSE)
End If
' The global load button is activated if a global preamble exists
If FileExists(ConvertFromURL( glb_UserPath ) & "GlobalPreamble.tex" ) Then
oChildDialog.getControl("LoadGlobalButton").setEnable(TRUE)
Else
oChildDialog.getControl("LoadGlobalButton").setEnable(FALSE)
End If
Dim aSize as Variant
aSize = oChildDialog.getPosSize()
oParent.setPosSize(0, 0, aSize.Width, aSize.Height, PS.SIZE)
oChildDialog.addWindowListener(CreateUnoListener("PreambleChildWindowListener_", "com.sun.star.awt.XWindowListener"))
CreateInnerDialog = oChildDialog
End Function
' Resize inner dialog window
' This is called indirectly when resizing the outer base dialog
Sub PreambleChildWindowListener_windowResized(ev)
' Inner dialog
Dim oDialog as Variant
oDialog = ev.Source
' Dialog size
Dim aRect as Variant
aRect = oDialog.getPosSize()
' Dialog min width and height
Const PRB_MIN_WIDTH = 275
Const PRB_MIN_HEIGHT = 230
' Convert min width and height to pixels
Dim minW as Integer, minH as Integer
minW = PRB_MIN_WIDTH * convX
minH = PRB_MIN_HEIGHT * convY
' Resize inner dialog if too narrow
If aRect.Width < minW Then
aRect.Width = minW
oDialog.setPosSize(0, 0, aRect.Width, aRect.Height, com.sun.star.awt.PosSize.SIZE)
End If
' Resize inner dialog if too short
If aRect.Height < 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
' Resize preamble text
oWidget = oDialog.getControl("Preamble")
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)
' Position Apply button
oWidget = oDialog.getControl("ApplyButton")
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)
' Position Load Default button
oWidget = oDialog.getControl("LoadDefaultButton")
X = aRect.Width - (oDialog.Model.Width - oWidget.Model.PositionX) * convX
'X = 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)
' Position Save Global button
oWidget = oDialog.getControl("SaveGlobalButton")
X = 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)
' Position Load Global button
oWidget = oDialog.getControl("LoadGlobalButton")
X = 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)
' Position Save Local button
oWidget = oDialog.getControl("SaveLocalButton")
X = 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)
' Position Load Local button
oWidget = oDialog.getControl("LoadLocalButton")
X = 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)
' Position IgnorePreamble
oWidget = oDialog.getControl("IgnorePreamble")
X = 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
' Close modal dialog
Sub ClosePreamble()
oDlgBasePreamble.endExecute()
End Sub
' Load the default preamble
Sub LoadDefaultPreambleButton_Clicked()
' Load the default preamble
SetDefaultPreamble()
oDlgPreamble.getControl("Preamble").setText(glb_Preamble)
oDlgPreamble.getControl("IgnorePreamble").setState(glb_IgnorePreamble)
End Sub
' Load the local preamble if any
Sub LoadLocalPreambleButton_Clicked()
' Try to read the local (stored in the current document) preamble
If ReadLocalPreamble() = 0 Then
' Try to read the global preamble
If ReadGlobalPreamble() = 0 Then
' Fallback to the default preamble
SetDefaultPreamble()
End If
End If
oDlgPreamble.getControl("Preamble").setText(glb_Preamble)
oDlgPreamble.getControl("IgnorePreamble").setState(glb_IgnorePreamble)
End Sub
' Save the current preamble as local (store it into the current document)
Sub SaveLocalPreambleButton_Clicked()
' Set the preamble variable
glb_Preamble = oDlgPreamble.getControl("Preamble").getText()
' Save the preamble to a UserDefinedProperties property
Dim oDoc as Variant, oProperties as Variant, sIgnorePreamble as String
oDoc = ThisComponent
oProperties = oDoc.DocumentProperties.UserDefinedProperties
' Set the ignore preamble state
If oDlgPreamble.getControl("IgnorePreamble").state() = 1 Then
sIgnorePreamble = "TRUE"
glb_IgnorePreamble = TRUE
Else
sIgnorePreamble = "FALSE"
glb_IgnorePreamble = FALSE
End If
' No LaTeX preamble already set => create it and set it
If Not oProperties.PropertySetInfo.hasPropertyByName("TexMathsPreamble") Then
' Encode the preamble string to avoid newline characters (workaround for an LO bug)
oProperties.addProperty("TexMathsPreamble", com.sun.star.beans.PropertyAttribute.REMOVEABLE, EncodeNewline(glb_Preamble))
' Latex preamble already set => modify it
Else
' Encode the preamble string to avoid newline characters (workaround for an LO bug)
oProperties.setPropertyValue("TexMathsPreamble", EncodeNewline(glb_Preamble))
End If
' IgnorePreamble value might be present or not
If Not oProperties.PropertySetInfo.hasPropertyByName("TexMathsIgnorePreamble") Then
oProperties.addProperty("TexMathsIgnorePreamble", com.sun.star.beans.PropertyAttribute.REMOVEABLE, sIgnorePreamble)
Else
oProperties.setPropertyValue("TexMathsIgnorePreamble", sIgnorePreamble)
End If
' Close the dialog
oDlgBasePreamble.endExecute()
DisplayStatus( _("Preamble applied and saved...") )
End Sub
' Apply the current preamble
Sub ApplyPreambleButton_Clicked()
' Set The preamble variable
glb_Preamble = oDlgPreamble.getControl("Preamble").getText()
If oDlgPreamble.getControl("IgnorePreamble").state() = 1 Then
glb_IgnorePreamble = TRUE
Else
glb_IgnorePreamble = FALSE
End If
oDlgBasePreamble.endExecute()
DisplayStatus( _("Preamble applied...") )
End Sub
' Load the global preamble if any
Sub LoadGlobalPreambleButton_Clicked()
' Try to read the global preamble
If ReadGlobalPreamble() = 0 Then
' Fallback to the default preamble
SetDefaultPreamble()
End If
oDlgPreamble.getControl("Preamble").setText(glb_Preamble)
oDlgPreamble.getControl("IgnorePreamble").setState(glb_IgnorePreamble)
End Sub
' Save the current preamble as global
Sub SaveGlobalPreambleButton_Clicked()
Dim cURL as String
' Set the preamble variable
glb_Preamble = oDlgPreamble.getControl("Preamble").getText()
cURL = ConvertFromURL( glb_UserPath ) & "GlobalPreamble.tex"
' Open service file and an output stream
Dim oFileAccess as Variant, oTextStream as Variant
oFileAccess = createUnoService("com.sun.star.ucb.SimpleFileAccess")
oTextStream = createUnoService("com.sun.star.io.TextOutputStream")
' Write file
If oFileAccess.exists( cURL ) Then oFileAccess.kill( cURL )
oTextStream.setOutputStream( oFileAccess.openFileWrite( cURL ) )
oTextStream.writeString(glb_Preamble)
' Close the file
oTextStream.closeOutput()
' Save IgnorePreamble state to the registry
Dim oSystemInfo as Variant
oSystemInfo = GetConfigAccess("/ooo.ext.texmaths.Registry/SystemInfo", TRUE)
If oDlgPreamble.getControl("IgnorePreamble").state() = 1 Then
glb_IgnorePreamble = TRUE
oSystemInfo.IgnorePreamble = "TRUE"
Else
glb_IgnorePreamble = FALSE
oSystemInfo.IgnorePreamble = "FALSE"
End If
oSystemInfo.commitChanges()
' Close dialog
oDlgBasePreamble.endExecute()
DisplayStatus( _("Global preamble applied and saved...") )
End Sub
Sub PreambleChildWindowListener_windowMoved(ev)
End Sub
Sub PreambleChildWindowListener_windowShown(ev)
End Sub
Sub PreambleChildWindowListener_windowHidden(ev)
End Sub
' Invoked when a window is resized
Sub PreambleWindowListener_windowResized(ev)
' Inner dialog size
Dim aSize as Variant
aSize = ev.Source.getSize()
' 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
' Invoked when a window is in the process of being closed
Sub PreambleWindowListener_windowClosing(ev)
' Close modal window
ev.Source.endExecute()
End Sub
' Invoked when a window has been opened
Sub PreambleWindowListener_windowOpened(ev)
End Sub
' Invoked when a window has been closed
Sub PreambleWindowListener_windowClosed(ev)
End Sub
' Invoked when a window is iconified
Sub PreambleWindowListener_windowMinimized(ev)
End Sub
' Invoked when a window is de-iconified
Sub PreambleWindowListener_windowNormalized(ev)
End Sub
' Invoked when a window is activated
Sub PreambleWindowListener_windowActivated(ev)
End Sub
' Invoked when a window is de-activated
Sub PreambleWindowListener_windowDeactivated(ev)
End Sub
Sub PreambleWindowListener_disposing(ev)
End Sub
Sub PreambleWindowListener_windowMoved(ev)
End Sub
Sub PreambleWindowListener_windowShown(ev)
End Sub
Sub PreambleWindowListener_windowHidden(ev)
End Sub
</script:module>
|