DocumentPreview.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import traceback
  19. from ..common.Properties import Properties
  20. from com.sun.star.awt import WindowDescriptor
  21. from com.sun.star.awt import Rectangle
  22. from com.sun.star.awt.WindowClass import SIMPLE
  23. from com.sun.star.awt.VclWindowPeerAttribute import CLIPCHILDREN
  24. from com.sun.star.awt.WindowAttribute import SHOW
  25. '''
  26. To change the template for this generated type comment go to
  27. Window>Preferences>Java>Code Generation>Code and Comments
  28. '''
  29. class DocumentPreview(object):
  30. PREVIEW_MODE = 1
  31. '''
  32. create new frame with window inside
  33. load a component as preview into this frame
  34. '''
  35. def __init__(self, xmsf, control):
  36. self.xControl = control
  37. self.createPreviewFrame(xmsf, self.xControl)
  38. def setDocument(self, url_, propNames, propValues=None):
  39. if propValues is None:
  40. if propNames == DocumentPreview.PREVIEW_MODE:
  41. self.setDocument(url_, ("Preview", "ReadOnly"), (True, True))
  42. else:
  43. self.loadArgs = propNames
  44. self.xFrame.activate()
  45. self.xComponent = self.xFrame.loadComponentFromURL(url_, "_self", 0, tuple(self.loadArgs))
  46. return self.xComponent
  47. else:
  48. self.url = url_
  49. ps = Properties()
  50. for index,item in enumerate(propNames):
  51. ps[item] = propValues[index]
  52. return self.setDocument(self.url, ps.getProperties1())
  53. def closeFrame(self):
  54. if self.xFrame is not None:
  55. self.xFrame.close(False)
  56. '''
  57. create a new frame with a new container window inside,
  58. which is not part of the global frame tree.
  59. Attention:
  60. a) This frame won't be destroyed by the office. It must be closed by you!
  61. Do so - please call XCloseable::close().
  62. b) The container window is part of the frame. Don't hold it alive - nor try to kill it.
  63. It will be destroyed inside close().
  64. '''
  65. def createPreviewFrame(self, xmsf, xControl):
  66. controlPeer = xControl.Peer
  67. r = xControl.PosSize
  68. toolkit = xmsf.createInstance("com.sun.star.awt.Toolkit")
  69. aDescriptor = WindowDescriptor()
  70. aDescriptor.Type = SIMPLE
  71. aDescriptor.WindowServiceName = "window"
  72. aDescriptor.ParentIndex = -1
  73. aDescriptor.Parent = controlPeer
  74. #xWindowPeer; #argument !
  75. aDescriptor.Bounds = Rectangle(0, 0, r.Width, r.Height)
  76. aDescriptor.WindowAttributes = CLIPCHILDREN | SHOW
  77. self.xWindow = toolkit.createWindow(aDescriptor)
  78. self.xFrame = xmsf.createInstance("com.sun.star.frame.Frame")
  79. self.xFrame.initialize(self.xWindow)
  80. self.xWindow.setVisible(True)
  81. def dispose(self):
  82. try:
  83. self.closeFrame()
  84. except Exception:
  85. traceback.print_exc()