1 # -*- tab-width: 4; indent-tabs-mode: nil -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # prepare pythoin environnement - Add the path of this class
13 from sys
import modules
14 from sys
import path
as syspath
16 # pyUNO program itself
20 from com
.sun
.star
.awt
.WindowClass
import TOP
, SIMPLE
21 from com
.sun
.star
.awt
.PushButtonType
import STANDARD
as standard
22 from com
.sun
.star
.awt
.PushButtonType
import OK
as ok
23 from com
.sun
.star
.awt
.PushButtonType
import CANCEL
as cancel
24 from com
.sun
.star
.awt
.PushButtonType
import HELP
as help
25 from com
.sun
.star
.awt
.TextAlign
import CENTER
as center
26 from com
.sun
.star
.awt
.TextAlign
import LEFT
as left
27 from com
.sun
.star
.awt
.TextAlign
import RIGHT
as right
30 from com
.sun
.star
.awt
import XActionListener
32 class MsgBox(unohelper
.Base
):
33 """Inspect UNO object, link to sdk and recusrsive calls"""
35 def __init__(self
, aContext
):
36 """acontext : a Valid UNO context
41 self
.smgr
= aContext
.ServiceManager
44 # List of openned Listeners
50 self
.fromBroxSize
= False
51 self
.numberOfLines
= -1
58 #####################################################
60 #####################################################
64 # computes parameters of the message dialog
65 if self
.numberOfLines
== -1:
67 numberOfLines
= len(self
.message
.split(chr(10)))
69 numberOfLines
= self
.numberOfLines
71 numberOfButtons
= len(self
.Buttons
)
72 self
.ButtonSpace
= self
.ButtonSize
/2
74 # button size is calculated from boxsize
75 size
= (2 * self
.boxSize
) / (3 * numberOfButtons
+ 1)
76 self
.ButtonSize
= size
77 self
.ButtonSpace
= self
.ButtonSize
/2
79 # boxsize is calculated from buttonsize
80 self
.boxSize
= numberOfButtons
* (self
.ButtonSize
+
81 self
.ButtonSpace
) + self
.ButtonSpace
83 # create the dialog model and set the properties
84 dialog_model
= self
.smgr
.createInstanceWithContext(
85 'com.sun.star.awt.UnoControlDialogModel',
87 dialog_model
.PositionX
= 50
89 dialog_model
.TabIndex
= 7
90 dialog_model
.Width
= self
.boxSize
#numberOfButtons * (self.ButtonSize +
91 # self.ButtonSpace) + 25
92 dialog_model
.Height
= 10 + self
.lineHeight
* numberOfLines
+ 10 + 12 + 10
93 dialog_model
.PositionY
= 63
94 dialog_model
.Sizeable
= True
95 dialog_model
.Closeable
= False
97 dialog
= self
.smgr
.createInstanceWithContext(
98 'com.sun.star.awt.UnoControlDialog', self
.ctx
)
101 label
= dialog_model
.createInstance(
102 'com.sun.star.awt.UnoControlFixedTextModel')
105 label
.Width
= dialog_model
.Width
- label
.PositionX
106 label
.Height
= self
.lineHeight
* numberOfLines
109 label
.MultiLine
= True
110 label
.Label
= self
.message
111 dialog_model
.insertByName('Label0', label
)
114 for buttonName
in self
.Buttons
:
116 button
= dialog_model
.createInstance(
117 'com.sun.star.awt.UnoControlButtonModel')
118 button
.PositionX
= nb
* self
.ButtonSpace
+ (nb
-1)* self
.ButtonSize
121 button
.Width
= self
.ButtonSize
122 button
.PositionY
= 10 + label
.Height
+ 10
123 button
.PushButtonType
= standard
125 button
.DefaultButton
= True
127 button
.DefaultButton
= False
128 button
.Label
= buttonName
129 dialog_model
.insertByName('Btn' + str(nb
), button
)
131 if not dialog
.getModel():
132 dialog
.setModel(dialog_model
)
134 # UNO toolkit definition
135 toolkit
= self
.smgr
.createInstanceWithContext('com.sun.star.awt.Toolkit', self
.ctx
)
136 a_rect
= uno
.createUnoStruct( 'com.sun.star.awt.Rectangle' )
138 dialog
.setTitle ( self
.title
)
142 win_descriptor
= uno
.createUnoStruct('com.sun.star.awt.WindowDescriptor')
143 win_descriptor
.Type
= TOP
144 win_descriptor
.ParentIndex
= -1
145 win_descriptor
.Bounds
= a_rect
146 peer
= toolkit
.createWindow( win_descriptor
)
147 dialog
.createPeer( toolkit
, peer
)
151 def _addListeners(self
):
152 """Add listeners to dialog"""
154 for buttonName
in self
.Buttons
:
156 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
157 the_listener
= ButtonListener(self
)
158 a_control
.addActionListener(the_listener
)
159 self
.lst_listeners
['Btn'+str(nb
)] = the_listener
162 def _removeListeners(self
):
163 """ remove listeners on exiting"""
165 for buttonName
in self
.Buttons
:
167 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
168 a_control
.removeActionListener(self
.lst_listeners
['Btn'+str(nb
)])
171 def show(self
, message
, decoration
, title
):
172 self
.message
= message
173 self
.decoration
= decoration
176 self
.dialog
= self
._createBox
()
178 #execute the dialog --> blocking call
179 self
.dialog
.execute()
180 #end --> release listeners and dispose dialog
181 self
._removeListeners
()
182 self
.dialog
.dispose()
185 def addButton(self
, caption
):
186 self
.Buttons
.append(caption
)
189 def renderFromBoxSize(self
, size
= 150):
191 self
.fromBroxSize
= True
194 def renderFromButtonSize(self
, size
= 50):
195 self
.ButtonSize
= size
196 self
.fromBroxSize
= False
199 class ButtonListener(unohelper
.Base
, XActionListener
):
200 """Stops the MessageBox, sets the button label as returned value"""
201 def __init__(self
, caller
):
204 def disposing(self
, eventObject
):
207 def actionPerformed(self
, actionEvent
):
208 button
= actionEvent
.Source
209 self
.caller
.Response
= button
.Model
.Label
210 self
.caller
.dialog
.endExecute()
214 if __name__
== '__main__':
215 # get the uno component context from the PyUNO runtime
216 localContext
= uno
.getComponentContext()
218 # create the UnoUrlResolver
219 resolver
= localContext
.ServiceManager
.createInstanceWithContext(
220 "com.sun.star.bridge.UnoUrlResolver", localContext
)
222 # connect to the running office
223 # LibO has to be launched in listen mode as
224 # ./soffice "--accept=socket,host=localhost,port=2002;urp;"
225 ctx
= resolver
.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
227 myBox
.addButton("Yes")
228 myBox
.addButton("No")
229 myBox
.addButton("May be")
230 myBox
.renderFromBoxSize(150)
231 myBox
.numberOflines
= 2
233 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"))
236 myBox
.addButton("oK")
237 myBox
.renderFromButtonSize()
238 myBox
.numberOflines
= 2
240 print(myBox
.show("A small message",0,"Dialog title"))