msgbox.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
  2. #
  3. # This file is part of the LibreOffice project.
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public
  6. # License, v. 2.0. If a copy of the MPL was not distributed with this
  7. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. #
  9. # prepare Python environment - Add the path of this class
  10. from os import path
  11. from sys import modules
  12. from sys import path as syspath
  13. # pyUNO program itself
  14. import uno, unohelper
  15. # UNO GUI toolkit
  16. from com.sun.star.awt.WindowClass import TOP, SIMPLE
  17. from com.sun.star.awt.PushButtonType import STANDARD as standard
  18. from com.sun.star.awt.PushButtonType import OK as ok
  19. from com.sun.star.awt.PushButtonType import CANCEL as cancel
  20. from com.sun.star.awt.PushButtonType import HELP as help
  21. from com.sun.star.awt.TextAlign import CENTER as center
  22. from com.sun.star.awt.TextAlign import LEFT as left
  23. from com.sun.star.awt.TextAlign import RIGHT as right
  24. # used UNO listeners
  25. from com.sun.star.awt import XActionListener
  26. class MsgBox(unohelper.Base):
  27. """Inspect UNO object, link to sdk and recursive calls"""
  28. def __init__(self, aContext):
  29. """acontext : a Valid UNO context
  30. """
  31. self.VERSION = '0.1'
  32. self.ctx = aContext
  33. self.smgr = aContext.ServiceManager
  34. # UI Dialog object
  35. self.dialog=None
  36. # List of opened Listeners
  37. self.lst_listeners={}
  38. #UI parameters
  39. self.ButtonSize = 50
  40. self.boxSize = 200
  41. self.lineHeight = 10
  42. self.fromBroxSize = False
  43. self.numberOfLines = -1
  44. self.Buttons = []
  45. self.Response = ''
  46. return
  47. #####################################################
  48. # GUI definition #
  49. #####################################################
  50. def _createBox(self):
  51. """Create the Box"""
  52. # computes parameters of the message dialog
  53. if self.numberOfLines == -1:
  54. #calculate
  55. numberOfLines = len(self.message.split(chr(10)))
  56. else:
  57. numberOfLines = self.numberOfLines
  58. numberOfButtons = len(self.Buttons)
  59. self.ButtonSpace = self.ButtonSize/2
  60. if self.fromBroxSize:
  61. # button size is calculated from boxsize
  62. size = (2 * self.boxSize) / (3 * numberOfButtons + 1)
  63. self.ButtonSize = size
  64. self.ButtonSpace = self.ButtonSize/2
  65. else:
  66. # boxsize is calculated from buttonsize
  67. self.boxSize = numberOfButtons * (self.ButtonSize +
  68. self.ButtonSpace) + self.ButtonSpace
  69. # create the dialog model and set the properties
  70. dialog_model = self.smgr.createInstanceWithContext(
  71. 'com.sun.star.awt.UnoControlDialogModel',
  72. self.ctx)
  73. dialog_model.PositionX = 50
  74. dialog_model.Step = 1
  75. dialog_model.TabIndex = 7
  76. dialog_model.Width = self.boxSize#numberOfButtons * (self.ButtonSize +
  77. # self.ButtonSpace) + 25
  78. dialog_model.Height = 10 + self.lineHeight * numberOfLines + 10 + 12 + 10
  79. dialog_model.PositionY = 63
  80. dialog_model.Sizeable = True
  81. dialog_model.Closeable = False
  82. dialog = self.smgr.createInstanceWithContext(
  83. 'com.sun.star.awt.UnoControlDialog', self.ctx)
  84. # label Label0
  85. label = dialog_model.createInstance(
  86. 'com.sun.star.awt.UnoControlFixedTextModel')
  87. label.PositionX = 10
  88. label.TabIndex = 9
  89. label.Width = dialog_model.Width - label.PositionX
  90. label.Height = self.lineHeight* numberOfLines
  91. label.PositionY = 10
  92. label.Align = left
  93. label.MultiLine = True
  94. label.Label = self.message
  95. dialog_model.insertByName('Label0', label)
  96. nb = 0
  97. for buttonName in self.Buttons:
  98. nb +=1
  99. button = dialog_model.createInstance(
  100. 'com.sun.star.awt.UnoControlButtonModel')
  101. button.PositionX = nb * self.ButtonSpace + (nb-1)* self.ButtonSize
  102. button.TabIndex = 8
  103. button.Height = 12
  104. button.Width = self.ButtonSize
  105. button.PositionY = 10 + label.Height + 10
  106. button.PushButtonType = standard
  107. if nb == 1:
  108. button.DefaultButton = True
  109. else:
  110. button.DefaultButton = False
  111. button.Label = buttonName
  112. dialog_model.insertByName('Btn' + str(nb), button )
  113. if not dialog.getModel():
  114. dialog.setModel(dialog_model)
  115. # UNO toolkit definition
  116. toolkit = self.smgr.createInstanceWithContext('com.sun.star.awt.Toolkit', self.ctx)
  117. a_rect = uno.createUnoStruct( 'com.sun.star.awt.Rectangle' )
  118. a_rect.X = 50
  119. dialog.setTitle ( self.title )
  120. a_rect.Width = 270
  121. a_rect.Height = 261
  122. a_rect.Y = 63
  123. win_descriptor = uno.createUnoStruct('com.sun.star.awt.WindowDescriptor')
  124. win_descriptor.Type = TOP
  125. win_descriptor.ParentIndex = -1
  126. win_descriptor.Bounds = a_rect
  127. peer = toolkit.createWindow( win_descriptor )
  128. dialog.createPeer( toolkit, peer )
  129. return dialog
  130. def _addListeners(self):
  131. """Add listeners to dialog"""
  132. nb = 0
  133. for buttonName in self.Buttons:
  134. nb +=1
  135. a_control = self.dialog.getControl('Btn'+str(nb))
  136. the_listener = ButtonListener(self)
  137. a_control.addActionListener(the_listener)
  138. self.lst_listeners['Btn'+str(nb)] = the_listener
  139. return
  140. def _removeListeners(self):
  141. """ remove listeners on exiting"""
  142. nb = 0
  143. for buttonName in self.Buttons:
  144. nb +=1
  145. a_control = self.dialog.getControl('Btn'+str(nb))
  146. a_control.removeActionListener(self.lst_listeners['Btn'+str(nb)])
  147. return
  148. def show(self, message, decoration, title):
  149. self.message = message
  150. self.decoration = decoration
  151. self.title = title
  152. # Create GUI
  153. self.dialog = self._createBox()
  154. self._addListeners()
  155. #execute the dialog --> blocking call
  156. self.dialog.execute()
  157. #end --> release listeners and dispose dialog
  158. self._removeListeners()
  159. self.dialog.dispose()
  160. return self.Response
  161. def addButton(self, caption):
  162. self.Buttons.append(caption)
  163. return
  164. def renderFromBoxSize(self, size = 150):
  165. self.boxSize = size
  166. self.fromBroxSize = True
  167. return
  168. def renderFromButtonSize(self, size = 50):
  169. self.ButtonSize = size
  170. self.fromBroxSize = False
  171. return
  172. class ButtonListener(unohelper.Base, XActionListener):
  173. """Stops the MessageBox, sets the button label as returned value"""
  174. def __init__(self, caller):
  175. self.caller = caller
  176. def disposing(self, eventObject):
  177. pass
  178. def actionPerformed(self, actionEvent):
  179. button = actionEvent.Source
  180. self.caller.Response = button.Model.Label
  181. self.caller.dialog.endExecute()
  182. return
  183. ### TEST
  184. if __name__ == '__main__':
  185. # get the uno component context from the PyUNO runtime
  186. localContext = uno.getComponentContext()
  187. # create the UnoUrlResolver
  188. resolver = localContext.ServiceManager.createInstanceWithContext(
  189. "com.sun.star.bridge.UnoUrlResolver", localContext )
  190. # connect to the running office
  191. # LibO has to be launched in listen mode as
  192. # ./soffice "--accept=socket,host=localhost,port=2002;urp;"
  193. ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
  194. myBox = MsgBox(ctx)
  195. myBox.addButton("Yes")
  196. myBox.addButton("No")
  197. myBox.addButton("May be")
  198. myBox.renderFromBoxSize(150)
  199. myBox.numberOflines = 2
  200. print(myBox.show("A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message A very long message " + chr(10)+chr(10)+"Do you agree ?",0,"Dialog title"))
  201. myBox = MsgBox(ctx)
  202. myBox.addButton("oK")
  203. myBox.renderFromButtonSize()
  204. myBox.numberOflines = 2
  205. print(myBox.show("A small message",0,"Dialog title"))
  206. # vim: set shiftwidth=4 softtabstop=4 expandtab: