1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
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/.
10 # prepare Python environment - Add the path of this class
12 # pyUNO program itself
17 from com
.sun
.star
.awt
.WindowClass
import TOP
18 from com
.sun
.star
.awt
.PushButtonType
import STANDARD
as standard
19 from com
.sun
.star
.awt
.TextAlign
import LEFT
as left
22 from com
.sun
.star
.awt
import XActionListener
24 class MsgBox(unohelper
.Base
):
25 """Inspect UNO object, link to sdk and recursive calls"""
27 def __init__(self
, aContext
):
28 """acontext : a Valid UNO context
33 self
.smgr
= aContext
.ServiceManager
36 # List of opened Listeners
42 self
.fromBroxSize
= False
43 self
.numberOfLines
= -1
50 #####################################################
52 #####################################################
56 # computes parameters of the message dialog
57 if self
.numberOfLines
== -1:
59 numberOfLines
= len(self
.message
.split(chr(10)))
61 numberOfLines
= self
.numberOfLines
63 numberOfButtons
= len(self
.Buttons
)
64 self
.ButtonSpace
= self
.ButtonSize
/2
66 # button size is calculated from boxsize
67 size
= (2 * self
.boxSize
) / (3 * numberOfButtons
+ 1)
68 self
.ButtonSize
= size
69 self
.ButtonSpace
= self
.ButtonSize
/2
71 # boxsize is calculated from buttonsize
72 self
.boxSize
= numberOfButtons
* (self
.ButtonSize
+
73 self
.ButtonSpace
) + self
.ButtonSpace
75 # create the dialog model and set the properties
76 dialog_model
= self
.smgr
.createInstanceWithContext(
77 'com.sun.star.awt.UnoControlDialogModel',
79 dialog_model
.PositionX
= 50
81 dialog_model
.TabIndex
= 7
82 dialog_model
.Width
= self
.boxSize
#numberOfButtons * (self.ButtonSize +
83 # self.ButtonSpace) + 25
84 dialog_model
.Height
= 10 + self
.lineHeight
* numberOfLines
+ 10 + 12 + 10
85 dialog_model
.PositionY
= 63
86 dialog_model
.Sizeable
= True
87 dialog_model
.Closeable
= False
89 dialog
= self
.smgr
.createInstanceWithContext(
90 'com.sun.star.awt.UnoControlDialog', self
.ctx
)
93 label
= dialog_model
.createInstance(
94 'com.sun.star.awt.UnoControlFixedTextModel')
97 label
.Width
= dialog_model
.Width
- label
.PositionX
98 label
.Height
= self
.lineHeight
* numberOfLines
101 label
.MultiLine
= True
102 label
.Label
= self
.message
103 dialog_model
.insertByName('Label0', label
)
106 for buttonName
in self
.Buttons
:
108 button
= dialog_model
.createInstance(
109 'com.sun.star.awt.UnoControlButtonModel')
110 button
.PositionX
= nb
* self
.ButtonSpace
+ (nb
-1)* self
.ButtonSize
113 button
.Width
= self
.ButtonSize
114 button
.PositionY
= 10 + label
.Height
+ 10
115 button
.PushButtonType
= standard
117 button
.DefaultButton
= True
119 button
.DefaultButton
= False
120 button
.Label
= buttonName
121 dialog_model
.insertByName('Btn' + str(nb
), button
)
123 if not dialog
.getModel():
124 dialog
.setModel(dialog_model
)
126 # UNO toolkit definition
127 toolkit
= self
.smgr
.createInstanceWithContext('com.sun.star.awt.Toolkit', self
.ctx
)
128 a_rect
= uno
.createUnoStruct( 'com.sun.star.awt.Rectangle' )
130 dialog
.setTitle ( self
.title
)
134 win_descriptor
= uno
.createUnoStruct('com.sun.star.awt.WindowDescriptor')
135 win_descriptor
.Type
= TOP
136 win_descriptor
.ParentIndex
= -1
137 win_descriptor
.Bounds
= a_rect
138 peer
= toolkit
.createWindow( win_descriptor
)
139 dialog
.createPeer( toolkit
, peer
)
143 def _addListeners(self
):
144 """Add listeners to dialog"""
146 for buttonName
in self
.Buttons
:
148 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
149 the_listener
= ButtonListener(self
)
150 a_control
.addActionListener(the_listener
)
151 self
.lst_listeners
['Btn'+str(nb
)] = the_listener
154 def _removeListeners(self
):
155 """ remove listeners on exiting"""
157 for buttonName
in self
.Buttons
:
159 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
160 a_control
.removeActionListener(self
.lst_listeners
['Btn'+str(nb
)])
163 def show(self
, message
, decoration
, title
):
164 self
.message
= message
165 self
.decoration
= decoration
168 self
.dialog
= self
._createBox
()
170 #execute the dialog --> blocking call
171 self
.dialog
.execute()
172 #end --> release listeners and dispose dialog
173 self
._removeListeners
()
174 self
.dialog
.dispose()
177 def addButton(self
, caption
):
178 self
.Buttons
.append(caption
)
181 def renderFromBoxSize(self
, size
= 150):
183 self
.fromBroxSize
= True
186 def renderFromButtonSize(self
, size
= 50):
187 self
.ButtonSize
= size
188 self
.fromBroxSize
= False
191 class ButtonListener(unohelper
.Base
, XActionListener
):
192 """Stops the MessageBox, sets the button label as returned value"""
193 def __init__(self
, caller
):
196 def disposing(self
, eventObject
):
199 def actionPerformed(self
, actionEvent
):
200 button
= actionEvent
.Source
201 self
.caller
.Response
= button
.Model
.Label
202 self
.caller
.dialog
.endExecute()
206 if __name__
== '__main__':
207 # get the uno component context from the PyUNO runtime
208 localContext
= uno
.getComponentContext()
210 # create the UnoUrlResolver
211 resolver
= localContext
.ServiceManager
.createInstanceWithContext(
212 "com.sun.star.bridge.UnoUrlResolver", localContext
)
214 # connect to the running office
215 # LibO has to be launched in listen mode as
216 # ./soffice "--accept=socket,host=localhost,port=2002;urp;"
217 ctx
= resolver
.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
219 myBox
.addButton("Yes")
220 myBox
.addButton("No")
221 myBox
.addButton("May be")
222 myBox
.renderFromBoxSize(150)
223 myBox
.numberOflines
= 2
225 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"))
228 myBox
.addButton("oK")
229 myBox
.renderFromButtonSize()
230 myBox
.numberOflines
= 2
232 print(myBox
.show("A small message",0,"Dialog title"))
234 # vim: set shiftwidth=4 softtabstop=4 expandtab: