fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / scripting / source / pyprov / msgbox.py
blob50ac5e689ce76ec18b33975984506ae7483546a5
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
12 from os import path
13 from sys import modules
14 from sys import path as syspath
16 # pyUNO program itself
17 import uno, unohelper
19 # UNO GUI toolkit
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
29 # used UNO listeners
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
37 """
39 self.VERSION = '0.1'
40 self.ctx = aContext
41 self.smgr = aContext.ServiceManager
42 # UI Dialog object
43 self.dialog=None
44 # List of openned Listeners
45 self.lst_listeners={}
46 #UI parameters
47 self.ButtonSize = 50
48 self.boxSize = 200
49 self.lineHeight = 10
50 self.fromBroxSize = False
51 self.numberOfLines = -1
53 self.Buttons = []
54 self.Response = ''
56 return
58 #####################################################
59 # GUI definition #
60 #####################################################
61 def _createBox(self):
62 """Create the Box"""
64 # computes parameters of the message dialog
65 if self.numberOfLines == -1:
66 #calculate
67 numberOfLines = len(self.message.split(chr(10)))
68 else:
69 numberOfLines = self.numberOfLines
71 numberOfButtons = len(self.Buttons)
72 self.ButtonSpace = self.ButtonSize/2
73 if self.fromBroxSize:
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
78 else:
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',
86 self.ctx)
87 dialog_model.PositionX = 50
88 dialog_model.Step = 1
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)
100 # label Label0
101 label = dialog_model.createInstance(
102 'com.sun.star.awt.UnoControlFixedTextModel')
103 label.PositionX = 10
104 label.TabIndex = 9
105 label.Width = dialog_model.Width - label.PositionX
106 label.Height = self.lineHeight* numberOfLines
107 label.PositionY = 10
108 label.Align = left
109 label.MultiLine = True
110 label.Label = self.message
111 dialog_model.insertByName('Label0', label)
113 nb = 0
114 for buttonName in self.Buttons:
115 nb +=1
116 button = dialog_model.createInstance(
117 'com.sun.star.awt.UnoControlButtonModel')
118 button.PositionX = nb * self.ButtonSpace + (nb-1)* self.ButtonSize
119 button.TabIndex = 8
120 button.Height = 12
121 button.Width = self.ButtonSize
122 button.PositionY = 10 + label.Height + 10
123 button.PushButtonType = standard
124 if nb == 1:
125 button.DefaultButton = True
126 else:
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' )
137 a_rect.X = 50
138 dialog.setTitle ( self.title )
139 a_rect.Width = 270
140 a_rect.Height = 261
141 a_rect.Y = 63
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 )
149 return dialog
151 def _addListeners(self):
152 """Add listeners to dialog"""
153 nb = 0
154 for buttonName in self.Buttons:
155 nb +=1
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
160 return
162 def _removeListeners(self):
163 """ remove listeners on exiting"""
164 nb = 0
165 for buttonName in self.Buttons:
166 nb +=1
167 a_control = self.dialog.getControl('Btn'+str(nb))
168 a_control.removeActionListener(self.lst_listeners['Btn'+str(nb)])
169 return
171 def show(self, message, decoration, title):
172 self.message = message
173 self.decoration = decoration
174 self.title = title
175 # Create GUI
176 self.dialog = self._createBox()
177 self._addListeners()
178 #execute the dialog --> blocking call
179 self.dialog.execute()
180 #end --> release listeners and dispose dialog
181 self._removeListeners()
182 self.dialog.dispose()
183 return self.Response
185 def addButton(self, caption):
186 self.Buttons.append(caption)
187 return
189 def renderFromBoxSize(self, size = 150):
190 self.boxSize = size
191 self.fromBroxSize = True
192 return
194 def renderFromButtonSize(self, size = 50):
195 self.ButtonSize = size
196 self.fromBroxSize = False
197 return
199 class ButtonListener(unohelper.Base, XActionListener):
200 """Stops the MessageBox, sets the button label as returned value"""
201 def __init__(self, caller):
202 self.caller = caller
204 def disposing(self, eventObject):
205 pass
207 def actionPerformed(self, actionEvent):
208 button = actionEvent.Source
209 self.caller.Response = button.Model.Label
210 self.caller.dialog.endExecute()
211 return
213 ### TEST
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" )
226 myBox = MsgBox(ctx)
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"))
235 myBox = MsgBox(ctx)
236 myBox.addButton("oK")
237 myBox.renderFromButtonSize()
238 myBox.numberOflines = 2
240 print(myBox.show("A small message",0,"Dialog title"))