xref: /aoo42x/main/pyuno/demo/swritercomp.py (revision 3f875f38)
1# *************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20# *************************************************************
21
22# just a simple copy of the swriter.py demo, but implemented as a component. The advantage is,
23# that the component may run within the office process which may give a performance improvement.
24
25import unohelper
26import uno
27
28# a UNO struct later needed to create a document
29from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
30from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
31from com.sun.star.awt import Size
32
33from com.sun.star.lang import XMain
34
35def insertTextIntoCell( table, cellName, text, color ):
36    tableText = table.getCellByName( cellName )
37    cursor = tableText.createTextCursor()
38    cursor.setPropertyValue( "CharColor", color )
39    tableText.setString( text )
40
41# the UNO component
42# implementing the interface com.sun.star.lang.XMain
43# unohelper.Base implements the XTypeProvider interface
44class SWriterComp(XMain,unohelper.Base):
45    def __init__( self, ctx ):
46        self.ctx = ctx
47
48    # implementation for XMain.run( [in] sequence< any > )
49    def run( self,args ):
50
51        ctx = self.ctx
52        smgr = ctx.ServiceManager
53        desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
54
55        # open a writer document
56        doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
57
58        text = doc.Text
59        cursor = text.createTextCursor()
60        text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
61        text.insertString( cursor, "Now we are in the second line.\n" , 0 )
62
63        # create a text table
64        table = doc.createInstance( "com.sun.star.text.TextTable" )
65
66        # with 4 rows and 4 columns
67        table.initialize( 4,4)
68
69        text.insertTextContent( cursor, table, 0 )
70        rows = table.Rows
71
72        table.setPropertyValue( "BackTransparent", uno.Bool(0) )
73        table.setPropertyValue( "BackColor", 13421823 )
74        row = rows.getByIndex(0)
75        row.setPropertyValue( "BackTransparent", uno.Bool(0) )
76        row.setPropertyValue( "BackColor", 6710932 )
77
78        textColor = 16777215
79
80        insertTextIntoCell( table, "A1", "FirstColumn", textColor )
81        insertTextIntoCell( table, "B1", "SecondColumn", textColor )
82        insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
83        insertTextIntoCell( table, "D1", "SUM", textColor )
84
85        values = ( (22.5,21.5,121.5),
86                  (5615.3,615.3,-615.3),
87                  (-2315.7,315.7,415.7) )
88        table.getCellByName("A2").setValue(22.5)
89        table.getCellByName("B2").setValue(5615.3)
90        table.getCellByName("C2").setValue(-2315.7)
91        table.getCellByName("D2").setFormula("sum <A2:C2>")
92
93        table.getCellByName("A3").setValue(21.5)
94        table.getCellByName("B3").setValue(615.3)
95        table.getCellByName("C3").setValue(-315.7)
96        table.getCellByName("D3").setFormula("sum <A3:C3>")
97
98        table.getCellByName("A4").setValue(121.5)
99        table.getCellByName("B4").setValue(-615.3)
100        table.getCellByName("C4").setValue(415.7)
101        table.getCellByName("D4").setFormula("sum <A4:C4>")
102
103
104        cursor.setPropertyValue( "CharColor", 255 )
105        cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )
106
107        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
108        text.insertString( cursor, " This is a colored text - blue with shadow\n" , 0 )
109        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
110
111        textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
112        textFrame.setSize( Size(15000,400))
113        textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
114
115        text.insertTextContent( cursor, textFrame, 0 )
116
117        textInTextFrame = textFrame.getText()
118        cursorInTextFrame = textInTextFrame.createTextCursor()
119        textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
120        textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the frame raises.",0)
121        text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
122
123        cursor.setPropertyValue( "CharColor", 65536 )
124        cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )
125
126        text.insertString( cursor, " That's all for now!" , 0 )
127        return 0
128
129
130# pythonloader looks for a static g_ImplementationHelper variable
131g_ImplementationHelper = unohelper.ImplementationHelper()
132g_ImplementationHelper.addImplementation( \
133        SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),)
134