De StarDesktop

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 17:32, 28 January 2013 by DiGro (Talk | contribs)

Jump to: navigation, search
Book.png


Twee services worden het meest gebruikt bij het werken met documenten:

  • De service com.sun.star.frame.Desktop, die overeenkomt met de kernservice van Apache OpenOffice. Het verschaft de functies voor het frame-object van Apache OpenOffice, waaronder alle documentvensters worden geclassificeerd. Documenten kunnen ook worden gemaakt, geopend en geïmporteerd met deze service.
  • De basis-functionaliteit voor de individuele documentobjecten wordt verschaft door de service com.sun.star.document.OfficeDocument. Dit verschaft de methoden voor opslaan, exporteren en afdrukken van documenten.

De service com.sun.star.frame.Desktop wordt automatisch gemaakt als Apache OpenOffice wordt gestart. Deze service kan worden benaderd door Apache OpenOffice BASIC met globale naam StarDesktop.

De meest belangrijke interface van de StarDesktop is com.sun.star.frame.XComponentLoader. Deze regelt in principe de methode loadComponentFromURL, welke verantwoordelijk is voor het maken, importeren en openen van documenten.

Documentation note.png StarOffice 5 : De naam van het object StarDesktop dateert uit StarOffice 5, waarin alle documentvensters waren ingebed in één algemene toepassing, genaamd StarDesktop. In de huidige versie van Apache OpenOffice wordt niet langer een zichtbare StarDesktop gebruikt. De naam StarDesktop werd echter behouden voor het frame-object van Apache OpenOffice omdat het duidelijk aangeeft dat het een basaal object is voor de gehele toepassing.

Het object StarDesktop object vervangt het object Application van StarOffice 5 dat eerder werd toegepast als bronobject. Echter, anders dan het oude object Application, is StarDesktop primair verantwoordelijk voor het openen van nieuwe documenten. De residente functies in het oude object Application voor het beheren van de on-screen weergave van Apache OpenOffice (bijvoorbeeld: FullScreen,FunctionBarVisible, Height, Width, Top, Visible) worden niet langer gebruikt.


Documentation note.png VBA : Waar het actieve document in Word toegankelijk is via Application.ActiveDocument en in Excel via Application.ActiveWorkbook, is in Apache OpenOffice de StarDesktop verantwoordelijk voor die taak. Het object voor het actieve document is in Apache OpenOffice toegankelijk via de eigenschap StarDesktop.CurrentComponent, of via ThisComponent.


ThisComponent

De globale naam ThisComponent geeft in het algemeen hetzelfde object terug als StarDesktop.CurrentComponent, met één significant voordeel. Als u uitvoert vanuit de BASIC IDE, debuggen of verkennen, dan geeft StarDesktop de BASIC IDE zelf terug. Dat is waarschijnlijk niet wat u wilt. ThisComponent geeft het laatste eerder actieve document terug.

Basisinformatie over documenten in Apache OpenOffice

Bij het werken met documenten van Apache OpenOffice is het handig om enkele basiskwesties van het beheren van documenten in Apache OpenOffice te behandelen. Dit houdt onder meer in: de manier waarop bestandsnamen worden gestructureerd voor Apache OpenOffice documenten, als ook de opmaak waarin bestanden worden opgeslagen.

Bestandsnamen in URL-notatie

Omdat Apache OpenOffice een platform-onafhankelijke toepassing is wordt URL-notatie gebruikt (die onafhankelijk is van enig besturingssysteem), zoals die is gedefinieerd in de Internet Standard RFC 1738 voor bestandsnamen. Standaard bestandsnamen die dit systeem hanteren beginnen met het voorvoegsel (prefix) file:/// gevolgd door het lokale pad. Als de bestandsnaam sub-mappen bevat, dan worden deze gescheiden door één enkele voorwaartse streep, niet door een backslash zoals gebruikelijk is onder Windows. Het volgende pad refereert aan het bestand test.odt in de map doc op de C:-schijf.

file:///C:/doc/test.odt

Apache OpenOffice verschaft de functie ConvertToUrl om lokale bestandsnamen om te zetten naar een URL. Apache OpenOffice heeft de functie ConvertFromUrl om een URL om te zetten naar een lokale bestandsnaam:

MsgBox ConvertToUrl("C:\doc\test.odt") 
  ' levert op file:///C:/doc/test.odt
MsgBox ConvertFromUrl("file:///C:/doc/test.odt")    
  '  levert op (onder Windows) c:\doc\test.odt

Het voorbeeld zet een lokale bestandsnaam om naar een URL en geeft het weer in een berichtenvenster. Vervolgens zet het de URL om naar een lokale bestandsnaam en geeft ook die weer.

De Internet Standard RFC 1738, waarop dit is gebaseerd, staat gebruik toe van de tekens 0-9, a-z en A-Z. Alle andere tekens worden ingevoegd in de URLs als een escape-code (hulpcode). Om dit te doen worden zij omgezet in hun hexadecimale waarde in de UTF-8-tekenset en worden zij voorafgegaan door een percentage-teken. Een spatie in een lokale bestandsnaam, bijvoorbeeld, wordt daarom dan %20 in de URL.

XML-bestandsindeling

Documenten van Apache OpenOffice zijn gebaseerd op de bestandsindeling XML. Op XML gebaseerde bestanden kunnen worden geopend en bewerkt met andere programma's.

Compressie van bestanden

Omdat XML is gebaseerd op standaard tekstbestanden, zijn de resulterende bestanden gewoonlijk zeer groot. Apache OpenOffice comprimeert daarom de bestanden en slaat ze op als een ZIP-bestand. Door middel van een methode optie storeAsURL, kan de gebruiker de originele XML-bestanden direct opslaan. Bekijk Methode optie storeAsURL, hieronder.

Maken, openen en importeren van documenten

Documenten worden geopend, geïmporteerd en gemaakt met behulp van de methode

StarDesktop.loadComponentFromURL(URL, Frame, SearchFlags, FileProperties)

De eerste parameter van loadComponentFromURL specificeert de URL van het geassocieerde bestand.

De tweede parameter, loadComponentFromURL, verwacht een naam voor het frame-object van het venster dat Apache OpenOffice intern aanmaakt voor zijn beheer. Meestal wordt hier de vooraf gedefinieerde naam _blank gespecificeerd en dit maakt zeker dat Apache OpenOffice een nieuw venster aanmaakt.

De gebruiker kan, met behulp van deze parameters, document van Apache OpenOffice openen, omdat tijdelijke plaatsaanduidingen (vervangende waarden) aan de laatste twee parameters kunnen worden toegewezen:

Dim Doc As Object
Dim Url As String
Dim Dummy() 'Een (lege) array van PropertyValues
 
Url = "file:///C:/test.odt"
 
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)

De voorgaande aanroep opent het bestand test.odt en geeft het weer in een nieuw venster.

Elk aantal documenten kan op deze manier worden geopend in Apache OpenOffice BASIC en dan worden bewerkt door middel van de resulterende documentobjecten.

Documentation note.png StarOffice 5 : StarDesktop.loadComponentFromURL heeft voorrang boven de methoden Documents.Add en Documents.Open uit de oude API van Apache OpenOffice API.


Vervangen van de inhoud van het documentvenster

De genoemde waarde _blank voor de parameter Frame zorgt ervoor dat Apache OpenOffice een nieuw venster maakt voor elke aanroep vanuit loadComponentFromURL. In sommige situaties is het handig de inhoud van een bestaand venster te vervangen. In dat geval zou het frame-object van het venster een expliciete naam moeten bevatten. Let er op dat deze naam niet begint met een onderlijn (underscore). Verder moet de parameter SearchFlags zo worden ingesteld dat het corresponderende raamwerk wordt aangemaakt, als het al niet reeds bestaat. De corresponderende constante voor SearchFlags is:

SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
              com.sun.star.frame.FrameSearchFlag.ALL

Het volgende voorbeeld toont hoe de inhoud van een geopend venster kan worden vervangen met behulp van de parameter frame en SearchFlags:

Dim Doc As Object
Dim Dummy() 
Dim Url As String
Dim SearchFlags As Long
 
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
              com.sun.star.frame.FrameSearchFlag.ALL
Url = "file:///C:/test.odt"
Doc = StarDesktop.loadComponentFromURL(Url, "MijnFrame", SearchFlags, Dummy)
MsgBox "Druk op OK om het tweede document weer te geven."
 
Url = "file:///C:/test2.odt"
Doc = StarDesktop.loadComponentFromURL(Url, "MijnFrame", _
      SearchFlags, Dummy)

Het voorbeeld opent eerst het bestand test.odt in een nieuw venster met de frame-naam van MijnFrame. Als eenmaal het berichtenvenster is bevestigd, vervangt het de inhoud van het venster met het bestand test2.odt.

Methode opties loadComponentFromURL

De vierde parameter van de functie loadComponentFromURL is een gegevensveld PropertyValue dat Apache OpenOffice diverse opties voor het openen en maken van documenten verschaft. Het gegevensveld moet een structuur PropertyValue verschaffen voor elke optie waarin de naam van de optie is opgeslagen als een tekenreeks, net als de geassocieerde waarde.

loadComponentFromURL ondersteunt de volgende opties:

AsTemplate (Boolean)
indien true, laadt een nieuw, naamloos document vanuit de opgegeven URL. Indien false worden sjabloon-bestanden geladen voor bewerking.
CharacterSet (String)
definieert op welke tekenset een document is gebaseerd.
FilterName (String)
specificeert een speciaal filter voor de functie loadComponentFromURL. De beschikbare namen van de filters zijn gedefinieerd in het bestand \share\config\registry\instance\org\openoffice\office\TypeDetection.xml.
FilterData (String)
definieert aanvullende opties voor filters.
FilterOptions (String)
definieert aanvullende opties (gebruikt door oude filters).
Hidden (Boolean)
waarde true laadt het document in de modus onzichtbaar.
JumpMark (String)
springt, als een document eenmaal is geopend, naar de positie die is gedefinieerd in JumpMark.
MacroExecutionMode (Integer)
geeft aan of macro's in het document mogen worden uitgevoerd. Waarden : bekijk com.sun.star.document.MacroExecMode
Password (String)
draagt een wachtwoord over voor een beveiligd bestand.
ReadOnly (Boolean)
waarde true laadt een document in modus Alleen-lezen.
UpdateDocMode (Integer)
geeft aan hoe/of koppelingen zullen wordne bijgewerkt. Waarden : bekijk com.sun.star.document.UpdateDocMode

Het volgende voorbeeld toont hoe een door komma's gescheiden tekstbestand in Apache OpenOffice Calc kan worden geopend met behulp van de optie FilterName.

Dim Doc As Object
Dim FileProperties(1) As New com.sun.star.beans.PropertyValue
Dim Url As String
 
Url = "file:///C:/doc.csv"
FileProperties(0).Name = "FilterName"
FileProperties(0).Value ="Text - txt - csv (StarCalc)"
FileProperties(1).Name = "FilterOptions"
FileProperties(1).value  = "44,34,0,1"
 
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, FileProperties())

De array FileProperties heeft twee elementen, één voor elke gebruikte optie. De eigenschap Filtername definieert of Apache OpenOffice een tekstfilter van Apache OpenOffice Calc gebruikt om bestanden te openen. De eigenschap FilterOptions bevat de omschrijving van de syntaxis van het .csv-bestand.

Nieuwe documenten maken

Apache OpenOffice maakt automatisch een nieuw document aan als het document, gespecificeerd in de URL, een sjabloon is.

Als alternatief kan, als alleen een leeg document zonder enige toevoeging nodig is, een URL private:factory worden gespecificeerd:

Dim Dummy() 
Dim Url As String
Dim Doc As Object
 
Url = "private:factory/swriter"
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())

De aanroep maakt een leeg document voor Apache OpenOffice Writer.

Document Objects

The loadComponentFromURL function introduced in the previous section returns a document object. This supports the com.sun.star.document.OfficeDocument service, which in turn provides two central interfaces:

Saving and Exporting Documents

Apache OpenOffice documents are saved directly through the document object. The store method of the com.sun.star.frame.XStorable interface is available for this purpose:

Doc.store()

This call functions provided that the document has already been assigned a memory space. This is not the case for new documents. In this instance, the storeAsURL method is used. This method is also defined in com.sun.star.frame.XStorable and can be used to define the location of the document:

Dim URL As String
Dim Dummy()
 
Url = "file:///C:/test3.odt"
Doc.storeAsURL(URL, Dummy())

In addition to the preceding methods, com.sun.star.frame.XStorable also provides some help methods which are useful when saving documents. These are:

hasLocation()
specifies whether the document has already been assigned a URL.
isReadonly()
specifies whether a document has read-only protection.
isModified()
specifies whether a document has been modified since it was last saved.

The code for saving a document can be extended by these options so that the document is only saved if the object has actually been modified and the file name is only queried if it is actually needed:

If (Doc.isModified) Then
  If (Doc.hasLocation And (Not Doc.isReadOnly)) Then
    Doc.store()
  Else
    Doc.storeAsURL(URL, Dummy())
  End If
End If

The example first checks whether the relevant document has been modified since it was last saved. It only continues with the saving process if this is the case. If the document has already been assigned a URL and is not a read-only document, it is saved under the existing URL. If it does not have a URL or was opened in its read-only status, it is saved under a new URL.

storeAsURL Method Options

As with the loadComponentFromURL method, some options can also be specified in the form of a PropertyValue data field using the storeAsURL method. These determine the procedure Apache OpenOffice uses when saving a document. storeAsURL provides the following options:

CharacterSet (String)
defines which set of characters a document is based on.
FilterName (String)
specifies a special filter for the loadComponentFromURL function. The filter names available are defined in the \share\config\registry\instance\org\openoffice\office\TypeDetection.xml file.
FilterData (String)
defines additional options for filters.
FilterOptions (String)
defines additional options (used by old filters).
Overwrite (Boolean)
allows a file which already exists to be overwritten without a query.
Password (String)
transfers the password for a protected file.
Unpacked (Boolean)
saves the document (not compressed) in sub-directories.
The possibility to store documents in unpacked way is not currently supported, the "Unpacked" property is just ignored, see Issue 64364 .

The following example shows how the Overwrite option can be used in conjunction with storeAsURL:

Dim Doc As Object
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
Dim Url As String
' ... Initialize Doc 
 
Url = "file:///c:/test3.odt"
FileProperties(0).Name = "Overwrite"
FileProperties(0).Value = True
Doc.storeAsURL(Url, FileProperties())

The example then saves Doc under the specified file name if a file already exists under the name.

Printing Documents

Similar to saving, documents are printed out directly by means of the document object. The Print method of the com.sun.star.view.Xprintable interface is provided for this purpose. In its simplest form, the print call is:

Dim Dummy()
 
Doc.print(Dummy())

As in the case of the loadComponentFromURL method, the Dummy parameter is a PropertyValue data field through which Apache OpenOffice can specify several options for printing.

The options of the print method

The print method expects a PropertyValue data field as a parameter, which reflects the settings of the print dialog of Apache OpenOffice:

CopyCount (Integer)
specifies the number of copies to be printed.
FileName (String)
prints the document in the specified file.
Collate (Boolean)
advises the printer to collate the pages of the copies.
Sort (Boolean)
sorts the pages when printing out several copies (CopyCount > 1).
Pages (String)
contains the list of the pages to be printed (syntax as specified in print dialog).
Wait (Boolean)
if set to True the print method will return after the job is stored on the waiting list for the printer. Use this option if you want to close the document after print.

The following example shows how several pages of a document can be printed out using the Pages option:

Dim Doc As Object
Dim PrintProperties(1) As New com.sun.star.beans.PropertyValue
 
PrintProperties(0).Name="Pages"
PrintProperties(0).Value="1-3; 7; 9"
PrintProperties(1).Name="Wait"
PrintProperties(1).Value=True
Doc.print(PrintProperties())

Printer selection and settings

The com.sun.star.view.XPrintable interface provides the Printer property, which selects the printer. This property receives a PropertyValue data field with the following settings:

Name (String)
specifies the name of printer.
PaperOrientation (Enum)
specifies the paper orientation (com.sun.star.view.PaperOrientation.PORTRAIT value for portrait format, com.sun.star.view.PaperOrientation.LANDSCAPE for landscape format).
PaperFormat (Enum)
specifies the paper format (for example, com.sun.star.view.PaperFormat.A4 for DIN A4 or com.sun.star.view.PaperFormat.Letter for US letters).
PaperSize (Size)
specifies the paper size in hundredths of a millimeter.

The following example shows how a printer can be changed and the paper size set with the help of the Printer property.

Dim Doc As Object
Dim PrinterProperties(1) As New com.sun.star.beans.PropertyValue
Dim PaperSize As New com.sun.star.awt.Size
 
PaperSize.Width = 20000   ' corresponds to 20 cm
PaperSize.Height = 20000   ' corresponds to 20 cm
PrinterProperties (0).Name="Name"
PrinterProperties (0).Value="My HP Laserjet"
PrinterProperties (1).Name="PaperSize"
PrinterProperties (1).Value=PaperSize
Doc.Printer = PrinterProperties()

The example defines an object named PaperSize with the com.sun.star.awt.Size type. This is needed to specify the paper size. Furthermore, it creates a data field for two PropertyValue entries named PrinterProperties. This data field is then initialized with the values to be set and assigned the Printer property. From the standpoint of UNO, the printer is not a real property but an imitated one.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools