' ' OOoLilyPondMusic : main module ' Copyright (C) 2005 geoffroy piroux ' Copyright (C) 2006 Samuel Hartmann ' ' 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 ' ' ' Option Explicit Private sConfigFile, sTmpPath, sLilyPondExecutable, sImageMagicExecutable, sTemplatePath, sFormat, sOSType As String Private sWriterTemplate As String Private sImpressTemplate As String Private iWriterAnchor, iWriterWrap As Integer Private iGraphicDPI As Integer Private bInWriter As Boolean ' Selected shape's variables... Private oShapeSize, oShapePosition, oShapeCrop As Object Private bShapeIsSelected As Boolean '--------------------------------------------------------------------------------------- Sub main Dim oDoc, oDocCtrl, oSelection, oAttributes, oGraphic, oShape As Object Dim sCode, sTemplate As String Dim iAnchor, iWrap As Integer Initialisation() ' Get the current document and controller oDoc = ThisComponent oDocCtrl = oDoc.getCurrentController() ' Test if we are in OOoWriter or OOoImpress ' And set the default user configuration. If oDoc.getImplementationName() = "SwXTextDocument" Then bInWriter=true Elseif oDoc.getImplementationName() = "SdXImpressDocument" Then bInWriter=false Else Msgbox(oDoc.getImplementationName() & ": document type not supported by OOoLilyPond", 0, "Error") Exit Sub End If ' Set default values if bInWriter then ' We are in a Writer doc, set default configuration... sCode="" sTemplate=sWriterTemplate iAnchor=iWriterAnchor iWrap=iWriterWrap else ' We are in a Impress doc, set default configuration... sCode="" sTemplate=sImpressTemplate end if ' If an OOoLilyPond object is selected, read the LilyPond Code bShapeIsSelected = False If Not isEmpty(oDocCtrl.selection()) Then 'only False in Impress when nothing is selected oSelection = oDocCtrl.getSelection() If oSelection.getImplementationName() = "com.sun.star.drawing.SvxShapeCollection" then oShape = oSelection.getByIndex(0) If Not ReadAttributes(oShape, sCode, sTemplate, iAnchor, iWrap) Then Exit Sub oShapePosition = oShape.position() bShapeIsSelected = True ElseIf oSelection.getImplementationName() = "SwXTextRanges" Then 'Nothing or normal text is selected bShapeIsSelected = False Else Msgbox ("The selected object is not an OOoLilyPond object ...", 0, "Error") Exit Sub End If End If ' Open the dialogue box OOoLilyPondDialog(sCode, sTemplate, iAnchor, iWrap) End Sub ' ToDo: ' Load Configration if Config File Exists Sub Initialisation DefaultConfig() if FileExists( sConfigFile ) then ReadConfigFile End Sub ' This is the core macro! It is called by the dialogue box. ' It calls the subroutinges that generate the image and insert it into the document. sub MakeMusic(sCode, sTemplate As String, iAnchor, iWrap As Integer) ' Definitions of variables... Dim iNumber As Integer Dim sShellArg,sMsg,sLilyPondCode as String ' We test if there is LilyPond code... If sCode = "" then Msgbox ("Enter LilyPond code...", 0, "Error") exit sub end if If Not ApplyTemplate(sCode, sTemplate, sLilyPondCode) Then Exit Sub CleanUp() If Not WriteLyFile(sLilyPondCode) Then Exit Sub If Not CallLilyPond() Then Exit Sub 'Check the result ! 'If Not FileExists(sTmpPath & "OOoLilyPond.eps") Then ' LilyPond error. ' PrintFile("OOoLilyPond.out") ' Exit Sub 'End If 'If sFormat = "png" And (Not FileExists(sTmpPath & "OOoLilyPond.png")) Then ' MsgBox("No png output is found", 0, "Error") ' Exit Sub 'End If ' If we arrive here, the compilation succeed. We can close the dialog... oDlgMain.endExecute() InsertMusic(sCode, sTemplate, iAnchor, iWrap) end sub sub test Dim oDoc, oDocCtrl, oSelection, oAttributes, oGraphic, oShape As Object oDoc = ThisComponent oDocCtrl = oDoc.getCurrentController() MsgBox isEmpty(oDocCtrl.selection()) end sub sub test2 MsgBox("Test3") Dim sCommand As String sCommand = "cd /d " & Chr(34) & "c:\slask\" & Chr(34) & Chr(10) _ & Chr(34) & sLilyPondExecutable & Chr(34) '"C:\Program\MuseScore 0.9\bin\mscore.exe" sCommand = sCommand & " " 'testWrite.msc -o test.pdf MsgBox (sCommand) WindowsCommand(sCommand) end sub sub loadDefaultCode Dim sLyFile As String Dim iNumber As Integer Dim sLilyPondCode As String Dim sCode As String Dim sLine As String Dim sTemplate As String 'sLilyPondCode = "hejsan hoppsan" 'Open sLyFile For Output As #iNumber 'Print #iNumber, sLilyPondCode 'Close #iNumber 'Open sLyFile For Input As #iNumber 'Read #iNumber, sLilyPondCode 'Close #iNumber 'MsgBox ("Hej") 'sCode = oDlgMain.getControl("LilyPondCode").getText() 'Msgbox sMsg sTemplate = oDlgMain.getControl("Template").getText() sLyFile=ConvertFromURL(sTemplatePath & sTemplate & ".msc") MsgBox(sLyFile) iNumber = Freefile sCode = "" Open sLyFile For Input As #iNumber While not eof(#iNumber) Line Input #iNumber, sLine If sLine <>"" then sCode = sCode & sLine & chr(13) end if wend Close #iNumber 'Msgbox sMsg oDlgMain.getControl("LilyPondCode").setText(sCode) end sub sub saveCodeToTempFile Dim sLyFile As String Dim sCode As String Dim iNumber As Integer sLyFile=ConvertFromURL(sTmpPath & "temp.msc") 'MsgBox sLyFile sCode = oDlgMain.getControl("LilyPondCode").getText() 'Msgbox sCode iNumber = Freefile Open sLyFile For Output As #iNumber Print #iNumber, sCode Close #iNumber end sub sub runMScoreWithTempFile Dim sCommand As String 'MuseScore If sOSType = "Windows" Then 'MsgBox sTmpPath sCommand = "cd /d " & Chr(34) & ConvertFromURL(sTmpPath) & Chr(34) & Chr(10) ' "c:\slask\" sCommand = sCommand & Chr(34) & sLilyPondExecutable & Chr(34) sCommand = sCommand & " " & "temp.msc" WindowsCommand(sCommand) ElseIf sOSType = "Unix" Then sCommand = "cd " & Chr(34) & ConvertFromURL(sTmpPath) & Chr(34) & "; " & sLilyPondExecutable & " temp.msc" BashCommand(sCommand) EndIf end sub sub loadCodeFromTempFile Dim sLyFile As String Dim iNumber As Integer Dim sLilyPondCode As String Dim sCode As String Dim sLine As String sLyFile=ConvertFromURL(sTmpPath & "temp.msc") 'MsgBox(sLyFile) iNumber = Freefile sCode = "" Open sLyFile For Input As #iNumber While not eof(#iNumber) Line Input #iNumber, sLine If sLine <>"" then sCode = sCode & sLine & chr(13) end if wend Close #iNumber 'Msgbox sMsg oDlgMain.getControl("LilyPondCode").setText(sCode) end sub sub handleMScore Dim sCode As String Dim control As Object sCode = oDlgMain.getControl("LilyPondCode").getText() if sCode = "" Then loadDefaultCode() End If 'msgBox "Hej" saveCodeToTempFile() runMScoreWithTempFile() loadCodeFromTempFile() LilyPondButton_Clicked() end sub