summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/ui/DocumentPreview.py
blob: 565dd5712d6374bdcb769d10f97a8e312c835c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
import traceback
from wizards.common.Properties import Properties

from com.sun.star.awt import WindowDescriptor
from com.sun.star.awt import Rectangle
from com.sun.star.awt.WindowClass import SIMPLE
from com.sun.star.awt.VclWindowPeerAttribute import CLIPCHILDREN
from com.sun.star.awt.WindowAttribute import SHOW

'''
@author rpiterman
To change the template for this generated type comment go to
Window>Preferences>Java>Code Generation>Code and Comments
'''

class DocumentPreview(object):
    PREVIEW_MODE = 1

    '''
    create new frame with window inside
    load a component as preview into this frame
    '''

    def __init__(self, xmsf, control):
        self.xControl = control
        self.createPreviewFrame(xmsf, self.xControl)

    def setDocument(self, url_, propNames, propValues=None):
        if propValues is None:
            if propNames == DocumentPreview.PREVIEW_MODE:
                self.setDocument(url_, ("Preview", "ReadOnly"), (True, True))
            else:
                self.loadArgs = propNames
                self.xFrame.activate()
                self.xComponent = self.xFrame.loadComponentFromURL(url_, "_self", 0, tuple(self.loadArgs))
                return self.xComponent
        else:
            self.url = url_
            ps = Properties()
            for index,item in enumerate(propNames):
                ps[item] = propValues[index]
            return self.setDocument(self.url, ps.getProperties(ps))

    def reload(self, xmsf):
        self.closeFrame()
        self.createPreviewFrame(xmsf, self.xControl)
        self.setDocument(self.url, self.loadArgs)

    def closeFrame(self):
        if self.xFrame is not None:
            self.xFrame.close(False)

    '''
    create a new frame with a new container window inside,
    which isnt part of the global frame tree.

    Attention:
    a) This frame wont be destroyed by the office. It must be closed by you!
       Do so - please call XCloseable::close().
    b) The container window is part of the frame. Dont hold it alive - nor try to kill it.
       It will be destroyed inside close().
    '''

    def createPreviewFrame(self, xmsf, xControl):
        controlPeer = xControl.Peer
        r = xControl.PosSize
        toolkit = xmsf.createInstance("com.sun.star.awt.Toolkit")
        aDescriptor = WindowDescriptor()
        aDescriptor.Type = SIMPLE
        aDescriptor.WindowServiceName = "window"
        aDescriptor.ParentIndex = -1
        aDescriptor.Parent = controlPeer
        #xWindowPeer; #argument !
        aDescriptor.Bounds = Rectangle(0, 0, r.Width, r.Height)
        aDescriptor.WindowAttributes = CLIPCHILDREN | SHOW
        self.xWindow = toolkit.createWindow(aDescriptor)
        self.xFrame = xmsf.createInstance("com.sun.star.frame.Frame")
        self.xFrame.initialize(self.xWindow)
        self.xWindow.setVisible(True)

    def dispose(self):
        try:
            self.closeFrame()
        except Exception:
            traceback.print_exc()