tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / scripting / source / pyprov / msgbox.py
blob5e9436fff82bae0fea6eed61b76899d07b5dd2d2
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
13 import uno
14 import unohelper
16 # UNO GUI toolkit
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
21 # used UNO listeners
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
29 """
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
45 self.Buttons = []
46 self.Response = ''
48 return
50 #####################################################
51 # GUI definition #
52 #####################################################
53 def _createBox(self):
54 """Create the Box"""
56 # computes parameters of the message dialog
57 if self.numberOfLines == -1:
58 #calculate
59 numberOfLines = len(self.message.split(chr(10)))
60 else:
61 numberOfLines = self.numberOfLines
63 numberOfButtons = len(self.Buttons)
64 self.ButtonSpace = self.ButtonSize/2
65 if self.fromBroxSize:
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
70 else:
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',
78 self.ctx)
79 dialog_model.PositionX = 50
80 dialog_model.Step = 1
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)
92 # label Label0
93 label = dialog_model.createInstance(
94 'com.sun.star.awt.UnoControlFixedTextModel')
95 label.PositionX = 10
96 label.TabIndex = 9
97 label.Width = dialog_model.Width - label.PositionX
98 label.Height = self.lineHeight* numberOfLines
99 label.PositionY = 10
100 label.Align = left
101 label.MultiLine = True
102 label.Label = self.message
103 dialog_model.insertByName('Label0', label)
105 nb = 0
106 for buttonName in self.Buttons:
107 nb +=1
108 button = dialog_model.createInstance(
109 'com.sun.star.awt.UnoControlButtonModel')
110 button.PositionX = nb * self.ButtonSpace + (nb-1)* self.ButtonSize
111 button.TabIndex = 8
112 button.Height = 12
113 button.Width = self.ButtonSize
114 button.PositionY = 10 + label.Height + 10
115 button.PushButtonType = standard
116 if nb == 1:
117 button.DefaultButton = True
118 else:
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' )
129 a_rect.X = 50
130 dialog.setTitle ( self.title )
131 a_rect.Width = 270
132 a_rect.Height = 261
133 a_rect.Y = 63
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 )
141 return dialog
143 def _addListeners(self):
144 """Add listeners to dialog"""
145 nb = 0
146 for buttonName in self.Buttons:
147 nb +=1
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
152 return
154 def _removeListeners(self):
155 """ remove listeners on exiting"""
156 nb = 0
157 for buttonName in self.Buttons:
158 nb +=1
159 a_control = self.dialog.getControl('Btn'+str(nb))
160 a_control.removeActionListener(self.lst_listeners['Btn'+str(nb)])
161 return
163 def show(self, message, decoration, title):
164 self.message = message
165 self.decoration = decoration
166 self.title = title
167 # Create GUI
168 self.dialog = self._createBox()
169 self._addListeners()
170 #execute the dialog --> blocking call
171 self.dialog.execute()
172 #end --> release listeners and dispose dialog
173 self._removeListeners()
174 self.dialog.dispose()
175 return self.Response
177 def addButton(self, caption):
178 self.Buttons.append(caption)
179 return
181 def renderFromBoxSize(self, size = 150):
182 self.boxSize = size
183 self.fromBroxSize = True
184 return
186 def renderFromButtonSize(self, size = 50):
187 self.ButtonSize = size
188 self.fromBroxSize = False
189 return
191 class ButtonListener(unohelper.Base, XActionListener):
192 """Stops the MessageBox, sets the button label as returned value"""
193 def __init__(self, caller):
194 self.caller = caller
196 def disposing(self, eventObject):
197 pass
199 def actionPerformed(self, actionEvent):
200 button = actionEvent.Source
201 self.caller.Response = button.Model.Label
202 self.caller.dialog.endExecute()
203 return
205 ### TEST
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" )
218 myBox = MsgBox(ctx)
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"))
227 myBox = MsgBox(ctx)
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: