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