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 from sys
import modules
13 from sys
import path
as syspath
15 # pyUNO program itself
19 from com
.sun
.star
.awt
.WindowClass
import TOP
, SIMPLE
20 from com
.sun
.star
.awt
.PushButtonType
import STANDARD
as standard
21 from com
.sun
.star
.awt
.PushButtonType
import OK
as ok
22 from com
.sun
.star
.awt
.PushButtonType
import CANCEL
as cancel
23 from com
.sun
.star
.awt
.PushButtonType
import HELP
as help
24 from com
.sun
.star
.awt
.TextAlign
import CENTER
as center
25 from com
.sun
.star
.awt
.TextAlign
import LEFT
as left
26 from com
.sun
.star
.awt
.TextAlign
import RIGHT
as right
29 from com
.sun
.star
.awt
import XActionListener
31 class MsgBox(unohelper
.Base
):
32 """Inspect UNO object, link to sdk and recursive calls"""
34 def __init__(self
, aContext
):
35 """acontext : a Valid UNO context
40 self
.smgr
= aContext
.ServiceManager
43 # List of opened Listeners
49 self
.fromBroxSize
= False
50 self
.numberOfLines
= -1
57 #####################################################
59 #####################################################
63 # computes parameters of the message dialog
64 if self
.numberOfLines
== -1:
66 numberOfLines
= len(self
.message
.split(chr(10)))
68 numberOfLines
= self
.numberOfLines
70 numberOfButtons
= len(self
.Buttons
)
71 self
.ButtonSpace
= self
.ButtonSize
/2
73 # button size is calculated from boxsize
74 size
= (2 * self
.boxSize
) / (3 * numberOfButtons
+ 1)
75 self
.ButtonSize
= size
76 self
.ButtonSpace
= self
.ButtonSize
/2
78 # boxsize is calculated from buttonsize
79 self
.boxSize
= numberOfButtons
* (self
.ButtonSize
+
80 self
.ButtonSpace
) + self
.ButtonSpace
82 # create the dialog model and set the properties
83 dialog_model
= self
.smgr
.createInstanceWithContext(
84 'com.sun.star.awt.UnoControlDialogModel',
86 dialog_model
.PositionX
= 50
88 dialog_model
.TabIndex
= 7
89 dialog_model
.Width
= self
.boxSize
#numberOfButtons * (self.ButtonSize +
90 # self.ButtonSpace) + 25
91 dialog_model
.Height
= 10 + self
.lineHeight
* numberOfLines
+ 10 + 12 + 10
92 dialog_model
.PositionY
= 63
93 dialog_model
.Sizeable
= True
94 dialog_model
.Closeable
= False
96 dialog
= self
.smgr
.createInstanceWithContext(
97 'com.sun.star.awt.UnoControlDialog', self
.ctx
)
100 label
= dialog_model
.createInstance(
101 'com.sun.star.awt.UnoControlFixedTextModel')
104 label
.Width
= dialog_model
.Width
- label
.PositionX
105 label
.Height
= self
.lineHeight
* numberOfLines
108 label
.MultiLine
= True
109 label
.Label
= self
.message
110 dialog_model
.insertByName('Label0', label
)
113 for buttonName
in self
.Buttons
:
115 button
= dialog_model
.createInstance(
116 'com.sun.star.awt.UnoControlButtonModel')
117 button
.PositionX
= nb
* self
.ButtonSpace
+ (nb
-1)* self
.ButtonSize
120 button
.Width
= self
.ButtonSize
121 button
.PositionY
= 10 + label
.Height
+ 10
122 button
.PushButtonType
= standard
124 button
.DefaultButton
= True
126 button
.DefaultButton
= False
127 button
.Label
= buttonName
128 dialog_model
.insertByName('Btn' + str(nb
), button
)
130 if not dialog
.getModel():
131 dialog
.setModel(dialog_model
)
133 # UNO toolkit definition
134 toolkit
= self
.smgr
.createInstanceWithContext('com.sun.star.awt.Toolkit', self
.ctx
)
135 a_rect
= uno
.createUnoStruct( 'com.sun.star.awt.Rectangle' )
137 dialog
.setTitle ( self
.title
)
141 win_descriptor
= uno
.createUnoStruct('com.sun.star.awt.WindowDescriptor')
142 win_descriptor
.Type
= TOP
143 win_descriptor
.ParentIndex
= -1
144 win_descriptor
.Bounds
= a_rect
145 peer
= toolkit
.createWindow( win_descriptor
)
146 dialog
.createPeer( toolkit
, peer
)
150 def _addListeners(self
):
151 """Add listeners to dialog"""
153 for buttonName
in self
.Buttons
:
155 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
156 the_listener
= ButtonListener(self
)
157 a_control
.addActionListener(the_listener
)
158 self
.lst_listeners
['Btn'+str(nb
)] = the_listener
161 def _removeListeners(self
):
162 """ remove listeners on exiting"""
164 for buttonName
in self
.Buttons
:
166 a_control
= self
.dialog
.getControl('Btn'+str(nb
))
167 a_control
.removeActionListener(self
.lst_listeners
['Btn'+str(nb
)])
170 def show(self
, message
, decoration
, title
):
171 self
.message
= message
172 self
.decoration
= decoration
175 self
.dialog
= self
._createBox
()
177 #execute the dialog --> blocking call
178 self
.dialog
.execute()
179 #end --> release listeners and dispose dialog
180 self
._removeListeners
()
181 self
.dialog
.dispose()
184 def addButton(self
, caption
):
185 self
.Buttons
.append(caption
)
188 def renderFromBoxSize(self
, size
= 150):
190 self
.fromBroxSize
= True
193 def renderFromButtonSize(self
, size
= 50):
194 self
.ButtonSize
= size
195 self
.fromBroxSize
= False
198 class ButtonListener(unohelper
.Base
, XActionListener
):
199 """Stops the MessageBox, sets the button label as returned value"""
200 def __init__(self
, caller
):
203 def disposing(self
, eventObject
):
206 def actionPerformed(self
, actionEvent
):
207 button
= actionEvent
.Source
208 self
.caller
.Response
= button
.Model
.Label
209 self
.caller
.dialog
.endExecute()
213 if __name__
== '__main__':
214 # get the uno component context from the PyUNO runtime
215 localContext
= uno
.getComponentContext()
217 # create the UnoUrlResolver
218 resolver
= localContext
.ServiceManager
.createInstanceWithContext(
219 "com.sun.star.bridge.UnoUrlResolver", localContext
)
221 # connect to the running office
222 # LibO has to be launched in listen mode as
223 # ./soffice "--accept=socket,host=localhost,port=2002;urp;"
224 ctx
= resolver
.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
226 myBox
.addButton("Yes")
227 myBox
.addButton("No")
228 myBox
.addButton("May be")
229 myBox
.renderFromBoxSize(150)
230 myBox
.numberOflines
= 2
232 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"))
235 myBox
.addButton("oK")
236 myBox
.renderFromButtonSize()
237 myBox
.numberOflines
= 2
239 print(myBox
.show("A small message",0,"Dialog title"))
241 # vim: set shiftwidth=4 softtabstop=4 expandtab: