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