libreoffice-extensions-by-e.../source/my_first_extension/main.py

50 lines
1.3 KiB
Python

import uno
import unohelper
from com.sun.star.task import XJobExecutor
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
OK = MSG_BUTTONS.BUTTONS_OK
ID_EXTENSION = 'org.examples.zaz.000'
SERVICE = ('com.sun.star.task.Job',)
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
def create_instance(name, with_context=False):
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
def msgbox(message, title='ZAZ Python', buttons=OK, type_msg='infobox'):
""" Create message box
type_msg: infobox, warningbox, errorbox, querybox, messbox
http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XMessageBoxFactory.html
"""
toolkit = create_instance('com.sun.star.awt.Toolkit')
parent = toolkit.getDesktopWindow()
box = toolkit.createMessageBox(parent, type_msg, buttons, title, str(message))
return box.execute()
class MyFirstExtension(unohelper.Base, XJobExecutor):
def __init__(self, ctx):
self.ctx = ctx
# ~ XJobExecutor
def trigger(self, event):
print(event)
msgbox('Dammed World!')
return
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(MyFirstExtension, ID_EXTENSION, SERVICE)