###########################################################################
#
# A collection of helper functions by Danny Brewer
# as extracted from the original OpenERP report designer plugin
#
#
#   Copyright (c) 2003-2004 Danny Brewer 
#   d29583@groovegarden.com 
# 
#   This library is free software; you can redistribute it and/or 
#   modify it under the terms of the GNU Lesser General Public 
#   License as published by the Free Software Foundation; either 
#   version 2.1 of the License, or (at your option) any later version. 
# 
#   This library is distributed in the hope that it will be useful, 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
#   Lesser General Public License for more details. 
# 
#   You should have received a copy of the GNU Lesser General Public 
#   License along with this library; if not, write to the Free Software 
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
# 
#   See:  http://www.gnu.org/licenses/lgpl.html 
############################################################################
import uno
import unohelper
import pythonloader

from com.sun.star.awt import XActionListener
class ActionListenerProcAdapter( unohelper.Base, XActionListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oActionEvent is a com.sun.star.awt.ActionEvent struct.
    def actionPerformed( self, oActionEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oActionEvent,) + self.tParams )

#--------------------------------------------------
# An ItemListener adapter.
# This object implements com.sun.star.awt.XItemListener.
# When itemStateChanged is called, this will call an arbitrary
#  python procedure, passing it...
#   1. the oItemEvent
#   2. any other parameters you specified to this object's constructor (as a tuple).
from com.sun.star.awt import XItemListener
class ItemListenerProcAdapter( unohelper.Base, XItemListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oItemEvent is a com.sun.star.awt.ItemEvent struct.
    def itemStateChanged( self, oItemEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oItemEvent,) + self.tParams )

#--------------------------------------------------
# An TextListener adapter.
# This object implements com.sun.star.awt.XTextistener.
# When textChanged is called, this will call an arbitrary
#  python procedure, passing it...
#   1. the oTextEvent
#   2. any other parameters you specified to this object's constructor (as a tuple).
from com.sun.star.awt import XTextListener
class TextListenerProcAdapter( unohelper.Base, XTextListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oTextEvent is a com.sun.star.awt.TextEvent struct.
    def textChanged( self, oTextEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oTextEvent,) + self.tParams )

class ErrorDialog:
    def __init__(self,sErrorMsg, sErrorHelpMsg="",sTitle="Error Message"):
        self.win = DBModalDialog(50, 50, 150, 120, sTitle)
        self.win.addFixedText("lblErrMsg", 5, 5, 120, 10, sErrorMsg)
        self.win.addEdit(
            "lblErrHelpMsg", 5, 15, 140, 75, sErrorHelpMsg,
            cAutoVScroll=True, cReadOnly=True, cMultiline=True)
        self.win.addButton('btnOK', 55,-5,40,15,'Ok'
                     ,actionListenerProc = self.btnOkOrCancel_clicked )
        self.win.doModalDialog("",None)
    def btnOkOrCancel_clicked( self, oActionEvent ):
        self.win.endExecute()

# The global Awt Toolkit.
# This is initialized the first time it is needed.
#goAwtToolkit = createUnoService( "com.sun.star.awt.Toolkit" )
goAwtToolkit = None

def getAwtToolkit():
    global goAwtToolkit
    if goAwtToolkit == None:
        goAwtToolkit = createUnoService( "com.sun.star.awt.Toolkit" )
    return goAwtToolkit

# This class builds dialog boxes.
# This can be used in two different ways...
# 1. by subclassing it (elegant)
# 2. without subclassing it (less elegant)
class DBModalDialog:
    """Class to build a dialog box from the com.sun.star.awt.* services.
    This doesn't do anything you couldn't already do using OOo's UNO API,
     this just makes it much easier.
    You can change the dialog box size, position, title, etc.
    You can add controls, and listeners for those controls to the dialog box.
    This class can be used by subclassing it, or without subclassing it.
    """
    def __init__( self, nPositionX=None, nPositionY=None, nWidth=None, nHeight=None, cTitle=None ):
        self.oDialogModel = createUnoService( "com.sun.star.awt.UnoControlDialogModel" )
        if nPositionX != None:  self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:  self.oDialogModel.PositionY = nPositionY
        if nWidth     != None:  self.oDialogModel.Width     = nWidth
        if nHeight    != None:  self.oDialogModel.Height    = nHeight
        if cTitle     != None:  self.oDialogModel.Title     = cTitle
        self.oDialogControl = createUnoService( "com.sun.star.awt.UnoControlDialog" )
        self.oDialogControl.setModel( self.oDialogModel )

    def release( self ):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    #--------------------------------------------------
    #   Dialog box adjustments
    #--------------------------------------------------

    def setDialogPosition( self, nX, nY ):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY

    def setDialogSize( self, nWidth, nHeight ):
        self.oDialogModel.Width = nWidth
        self.oDialogModel.Height = nHeight

    def setDialogTitle( self, cCaption ):
        self.oDialogModel.Title = cCaption

    def setVisible( self, bVisible ):
        self.oDialogControl.setVisible( bVisible )


    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlButton
    #--------------------------------------------------

    # After you add a Button control, you can call self.setControlModelProperty()
    #  passing any of the properties for a...
    #       com.sun.star.awt.UnoControlButtonModel
    #       com.sun.star.awt.UnoControlDialogElement
    #       com.sun.star.awt.UnoControlModel
    def addButton( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                       cLabel=None,
                       actionListenerProc=None,
                       nTabIndex=None ):
        self.addControl( "com.sun.star.awt.UnoControlButtonModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
                         cLabel=cLabel,
                         nTabIndex=nTabIndex )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )

    def setButtonLabel( self, cCtrlName, cLabel ):
        """Set the label of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setLabel( cLabel )

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlEditModel
    #--------------------------------------------------
    def addEdit( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        cText=None,
                        textListenerProc=None,
                        cReadOnly=None,
                        cMultiline=None,
                        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl( "com.sun.star.awt.UnoControlEditModel",
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None, cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=cAutoVScroll)

        if cText != None:
            self.setEditText( cCtrlName, cText )
        if textListenerProc != None:
            self.addTextListenerProc( cCtrlName, textListenerProc )

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlCheckBox
    #--------------------------------------------------

    # After you add a CheckBox control, you can call self.setControlModelProperty()
    #  passing any of the properties for a...
    #       com.sun.star.awt.UnoControlCheckBoxModel
    #       com.sun.star.awt.UnoControlDialogElement
    #       com.sun.star.awt.UnoControlModel
    def addCheckBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                       cLabel=None,
                       itemListenerProc=None,
                       nTabIndex=None ):
        self.addControl( "com.sun.star.awt.UnoControlCheckBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,  bMultiSelection=None,
                         cLabel=cLabel,
                         nTabIndex=nTabIndex )
        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )

    def setEditText( self, cCtrlName, cText ):
        """Set the text of the edit box."""
        oControl = self.getControl( cCtrlName )
        oControl.setText( cText )

    def getEditText( self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl( cCtrlName )
        return oControl.getText()

    def setCheckBoxLabel( self, cCtrlName, cLabel ):
        """Set the label of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setLabel( cLabel )

    def getCheckBoxState( self, cCtrlName ):
        """Get the state of the control."""
        oControl = self.getControl( cCtrlName )
        return oControl.getState();

    def setCheckBoxState( self, cCtrlName, nState ):
        """Set the state of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setState( nState )

    def enableCheckBoxTriState( self, cCtrlName, bTriStateEnable ):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.enableTriState( bTriStateEnable )


    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlFixedText
    #--------------------------------------------------

    def addFixedText( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        cLabel=None ):
        self.addControl( "com.sun.star.awt.UnoControlFixedTextModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                         bDropdown=None, bMultiSelection=None,
                         cLabel=cLabel, cMultiline=True)
        return self.getControl( cCtrlName )

    #--------------------------------------------------
    #   Add Controls to dialog
    #--------------------------------------------------

    def addControl( self, cCtrlServiceName,
                        cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=None,
                        bMultiSelection=None,
                        cLabel=None,
                        nTabIndex=None,
                        sImagePath=None,
                        cMultiline=None,
                        cReadOnly=None,
                        cAutoVScroll=None
                         ):
        oControlModel = self.oDialogModel.createInstance( cCtrlServiceName )
        self.oDialogModel.insertByName( cCtrlName, oControlModel )
        # if negative coordinates are given for X or Y position,
        #  then make that coordinate be relative to the right/bottom
        #  edge of the dialog box instead of to the left/top.
        if nPositionX < 0: nPositionX = self.oDialogModel.Width  + nPositionX - nWidth
        if nPositionY < 0: nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName

        if bDropdown != None:
            oControlModel.Dropdown = bDropdown

        if bMultiSelection!=None:
            oControlModel.MultiSelection=bMultiSelection

        if cLabel != None:
            oControlModel.Label = cLabel

        if cMultiline != None:
            oControlModel.MultiLine = True

        if cReadOnly != None:
            oControlModel.ReadOnly = True

        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True

        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex

        if sImagePath != None:
            oControlModel.ImageURL = sImagePath
    #--------------------------------------------------
    #   Access controls and control models
    #--------------------------------------------------

    #--------------------------------------------------
    #   com.sun.star.awt.UnoContorlListBoxModel
    #--------------------------------------------------


    def addComboListBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=True,
                        bMultiSelection=False,
                        itemListenerProc=None,
                        actionListenerProc=None,
                        ):

        mod = self.addControl( "com.sun.star.awt.UnoControlListBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,bDropdown,bMultiSelection )

        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )

    def addListBoxItems( self, cCtrlName, tcItemTexts, nPosition=0 ):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl( cCtrlName )
        oControl.addItems( tcItemTexts, nPosition )

    def selectListBoxItem( self, cCtrlName, cItemText, bSelect=True ):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl( cCtrlName )
        return oControl.selectItem( cItemText, bSelect )

    def selectListBoxItemPos( self, cCtrlName, nItemPos, bSelect=True ):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl( cCtrlName )
        return oControl.selectItemPos( nItemPos, bSelect )

    def removeListBoxItems( self, cCtrlName, nPosition, nCount=1 ):
        """Remove items from a ListBox."""
        oControl = self.getControl( cCtrlName )
        oControl.removeItems( nPosition, nCount )

    def getListBoxItemCount( self, cCtrlName ):
        """Get the number of items in a ListBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getItemCount()

    def getListBoxSelectedItem( self, cCtrlName ):
        """Returns the currently selected item."""
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItem()

    def getListBoxItem( self, cCtrlName, nPosition ):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getItem( nPosition )

    def getListBoxSelectedItemPos(self,cCtrlName):

        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self,cCtrlName):
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self,cCtrlName):

        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItemsPos()

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlComboBoxModel
    #--------------------------------------------------
    def addComboBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=True,
                        itemListenerProc=None,
                        actionListenerProc=None ):

        mod = self.addControl( "com.sun.star.awt.UnoControlComboBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )


    def setComboBoxText( self, cCtrlName, cText ):
        """Set the text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        oControl.setText( cText )

    def getComboBoxText( self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getText()

    def getComboBoxSelectedText( self, cCtrlName ):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedText();

    def getControl( self, cCtrlName ):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl( cCtrlName )
        return oControl

    def getControlModel( self, cCtrlName ):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl( cCtrlName )
        oControlModel = oControl.getModel()
        return oControlModel
    #---------------------------------------------------
    #    com.sun.star.awt.UnoControlImageControlModel
    #---------------------------------------------------
    def addImageControl( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        sImagePath="",
                        itemListenerProc=None,
                        actionListenerProc=None ):

        mod = self.addControl( "com.sun.star.awt.UnoControlImageControlModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=sImagePath)

        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )


    #--------------------------------------------------
    #   Adjust properties of control models
    #--------------------------------------------------

    def setControlModelProperty( self, cCtrlName, cPropertyName, uValue ):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel( cCtrlName )
        oControlModel.setPropertyValue( cPropertyName, uValue )

    def getControlModelProperty( self, cCtrlName, cPropertyName ):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel( cCtrlName )
        return oControlModel.getPropertyValue( cPropertyName )

    #--------------------------------------------------
    #   Sugar coated property adjustments to control models.
    #--------------------------------------------------

    def setEnabled( self, cCtrlName, bEnabled=True ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "Enabled", bEnabled )

    def getEnabled( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """

        return self.getControlModelProperty( cCtrlName, "Enabled" )

    def setState( self, cCtrlName, nState ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "State", nState )

    def getState( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "State" )

    def setLabel( self, cCtrlName, cLabel ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "Label", cLabel )

    def getLabel( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "Label" )

    def setHelpText( self, cCtrlName, cHelpText ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "HelpText", cHelpText )

    def getHelpText( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "HelpText" )


    #--------------------------------------------------
    #   Adjust controls (not models)
    #--------------------------------------------------

    # The following apply to all controls which are a
    #   com.sun.star.awt.UnoControl

    def setDesignMode( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        oControl.setDesignMode( bDesignMode )

    def isDesignMode( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        return oControl.isDesignMode()

    def isTransparent( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        return oControl.isTransparent()


    # The following apply to all controls which are a
    #   com.sun.star.awt.UnoControlDialogElement

    def setPosition( self, cCtrlName, nPositionX, nPositionY ):
        self.setControlModelProperty( cCtrlName, "PositionX", nPositionX )
        self.setControlModelProperty( cCtrlName, "PositionY", nPositionY )
    def setPositionX( self, cCtrlName, nPositionX ):
        self.setControlModelProperty( cCtrlName, "PositionX", nPositionX )
    def setPositionY( self, cCtrlName, nPositionY ):
        self.setControlModelProperty( cCtrlName, "PositionY", nPositionY )
    def getPositionX( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "PositionX" )
    def getPositionY( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "PositionY" )

    def setSize( self, cCtrlName, nWidth, nHeight ):
        self.setControlModelProperty( cCtrlName, "Width", nWidth )
        self.setControlModelProperty( cCtrlName, "Height", nHeight )
    def setWidth( self, cCtrlName, nWidth ):
        self.setControlModelProperty( cCtrlName, "Width", nWidth )
    def setHeight( self, cCtrlName, nHeight ):
        self.setControlModelProperty( cCtrlName, "Height", nHeight )
    def getWidth( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Width" )
    def getHeight( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Height" )

    def setTabIndex( self, cCtrlName, nWidth, nTabIndex ):
        self.setControlModelProperty( cCtrlName, "TabIndex", nTabIndex )
    def getTabIndex( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "TabIndex" )

    def setStep( self, cCtrlName, nWidth, nStep ):
        self.setControlModelProperty( cCtrlName, "Step", nStep )
    def getStep( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Step" )

    def setTag( self, cCtrlName, nWidth, cTag ):
        self.setControlModelProperty( cCtrlName, "Tag", cTag )
    def getTag( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Tag" )

    def setEchoChar(self, cCtrlName , cVal):
        self.setControlModelProperty(cCtrlName, "EchoChar", cVal)
    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, "EchoChar")

    #--------------------------------------------------
    #   Add listeners to controls.
    #--------------------------------------------------

    # This applies to...
    #   UnoControlButton
    def addActionListenerProc( self, cCtrlName, actionListenerProc ):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl( cCtrlName )
        oActionListener = ActionListenerProcAdapter( actionListenerProc )
        oControl.addActionListener( oActionListener )

    # This applies to...
    #   UnoControlCheckBox
    def addItemListenerProc( self, cCtrlName, itemListenerProc ):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl( cCtrlName )
        oActionListener = ItemListenerProcAdapter( itemListenerProc )
        oControl.addItemListener( oActionListener )

    #--------------------------------------------------
    #   Display the modal dialog.
    #--------------------------------------------------

    def doModalDialog( self, sObjName,sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible( True )
        if not sValue==None:
            self.selectListBoxItem( sObjName, sValue, True )
        self.oDialogControl.execute()

    def endExecute( self ):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()

#------------------------------------------------------------
#   Uno ServiceManager access
#   A different version of this routine and global variable
#    is needed for code running inside a component.
#------------------------------------------------------------


# The ServiceManager of the running OOo.
# It is cached in a global variable.
goServiceManager = False
pythonloader.DEBUG = 0
def getServiceManager( cHost="localhost", cPort="2002" ):
    """Get the ServiceManager from the running OpenOffice.org.
        Then retain it in the global variable goServiceManager for future use.
        This is similar to the GetProcessServiceManager() in OOo Basic.
    """
    global goServiceManager
    global pythonloader
    if not goServiceManager:
        # Get the uno component context from the PyUNO runtime
        oLocalContext = uno.getComponentContext()
        # Create the UnoUrlResolver on the Python side.

        goServiceManager=oLocalContext.ServiceManager

    return goServiceManager


#------------------------------------------------------------
#   Uno convenience functions
#   The stuff in this section is just to make
#    python progrmaming of OOo more like using OOo Basic.
#------------------------------------------------------------


# This is the same as ServiceManager.createInstance( ... )
def createUnoService( cClass ):
    """A handy way to create a global objects within the running OOo.
    Similar to the function of the same name in OOo Basic.
    """
    oServiceManager = getServiceManager()
    oObj = oServiceManager.createInstance( cClass )
    return oObj


# The StarDesktop object.  (global like in OOo Basic)
# It is cached in a global variable.
StarDesktop = None
def getDesktop():
    """An easy way to obtain the Desktop object from a running OOo.
    """
    global StarDesktop
    if StarDesktop == None:
        StarDesktop = createUnoService( "com.sun.star.frame.Desktop" )
    return StarDesktop
# preload the StarDesktop variable.
#getDesktop()


# The CoreReflection object.
# It is cached in a global variable.
goCoreReflection = False
def getCoreReflection():
    global goCoreReflection
    if not goCoreReflection:
        goCoreReflection = createUnoService( "com.sun.star.reflection.CoreReflection" )
    return goCoreReflection


def createUnoStruct( cTypeName ):
    """Create a UNO struct and return it.
    Similar to the function of the same name in OOo Basic.
    """
    oCoreReflection = getCoreReflection()
    # Get the IDL class for the type name
    oXIdlClass = oCoreReflection.forName( cTypeName )
    # Create the struct.
    oReturnValue, oStruct = oXIdlClass.createObject( None )
    return oStruct

#------------------------------------------------------------
#   API helpers
#------------------------------------------------------------

def hasUnoInterface( oObject, cInterfaceName ):
    """Similar to Basic's HasUnoInterfaces() function, but singular not plural."""

    # Get the Introspection service.
    oIntrospection = createUnoService( "com.sun.star.beans.Introspection" )

    # Now inspect the object to learn about it.
    oObjInfo = oIntrospection.inspect( oObject )

    # Obtain an array describing all methods of the object.
    oMethods = oObjInfo.getMethods( uno.getConstantByName( "com.sun.star.beans.MethodConcept.ALL" ) )
    # Now look at every method.
    for oMethod in oMethods:
        # Check the method's interface to see if
        #  these aren't the droids you're looking for.
        cMethodInterfaceName = oMethod.getDeclaringClass().getName()
        if cMethodInterfaceName == cInterfaceName:
            return True
    return False

def hasUnoInterfaces( oObject, *cInterfaces ):
    """Similar to the function of the same name in OOo Basic."""
    for cInterface in cInterfaces:
        if not hasUnoInterface( oObject, cInterface ):
            return False
    return True

#------------------------------------------------------------
#   High level general purpose functions
#------------------------------------------------------------


def makePropertyValue( cName=None, uValue=None, nHandle=None, nState=None ):
    """Create a com.sun.star.beans.PropertyValue struct and return it.
    """
    oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )

    if cName != None:
        oPropertyValue.Name = cName
    if uValue != None:
        oPropertyValue.Value = uValue
    if nHandle != None:
        oPropertyValue.Handle = nHandle
    if nState != None:
        oPropertyValue.State = nState

    return oPropertyValue


def makePoint( nX, nY ):
    """Create a com.sun.star.awt.Point struct."""
    oPoint = createUnoStruct( "com.sun.star.awt.Point" )
    oPoint.X = nX
    oPoint.Y = nY
    return oPoint


def makeSize( nWidth, nHeight ):
    """Create a com.sun.star.awt.Size struct."""
    oSize = createUnoStruct( "com.sun.star.awt.Size" )
    oSize.Width = nWidth
    oSize.Height = nHeight
    return oSize


def makeRectangle( nX, nY, nWidth, nHeight ):
    """Create a com.sun.star.awt.Rectangle struct."""
    oRect = createUnoStruct( "com.sun.star.awt.Rectangle" )
    oRect.X = nX
    oRect.Y = nY
    oRect.Width = nWidth
    oRect.Height = nHeight
    return oRect


def Array( *args ):
    """This is just sugar coating so that code from OOoBasic which
    contains the Array() function can work perfectly in python."""
    tArray = ()
    for arg in args:
        tArray += (arg,)
    return tArray


def loadComponentFromURL( cUrl, tProperties=() ):
    """Open or Create a document from it's URL.
    New documents are created from URL's such as:
        private:factory/sdraw
        private:factory/swriter
        private:factory/scalc
        private:factory/simpress
    """
    StarDesktop = getDesktop()
    oDocument = StarDesktop.loadComponentFromURL( cUrl, "_blank", 0, tProperties )
    return oDocument


#------------------------------------------------------------
#   Styles
#------------------------------------------------------------


def defineStyle( oDrawDoc, cStyleFamily, cStyleName, cParentStyleName=None ):
    """Add a new style to the style catalog if it is not already present.
    This returns the style object so that you can alter its properties.
    """

    oStyleFamily = oDrawDoc.getStyleFamilies().getByName( cStyleFamily )

    # Does the style already exist?
    if oStyleFamily.hasByName( cStyleName ):
        # then get it so we can return it.
        oStyle = oStyleFamily.getByName( cStyleName )
    else:
        # Create new style object.
        oStyle = oDrawDoc.createInstance( "com.sun.star.style.Style" )

        # Set its parent style
        if cParentStyleName != None:
            oStyle.setParentStyle( cParentStyleName )

        # Add the new style to the style family.
        oStyleFamily.insertByName( cStyleName, oStyle )

    return oStyle


def getStyle( oDrawDoc, cStyleFamily, cStyleName ):
    """Lookup and return a style from the document.
    """
    return oDrawDoc.getStyleFamilies().getByName( cStyleFamily ).getByName( cStyleName )

#------------------------------------------------------------
#   General Utility functions
#------------------------------------------------------------


def convertToURL( cPathname ):
    """Convert a Windows or Linux pathname into an OOo URL."""
    if len( cPathname ) > 1:
        if cPathname[1:2] == ":":
            cPathname = "/" + cPathname[0] + "|" + cPathname[2:]
    cPathname = cPathname.replace( "\\", "/" )
    cPathname = "file://" + cPathname
    return cPathname


8cb902a26524ee0fe0e7ac61056276b63c96bf82
<mask token>


class DBModalDialog:
    <mask token>

    def __init__(self, nPositionX=None, nPositionY=None, nWidth=None,
        nHeight=None, cTitle=None):
        self.oDialogModel = createUnoService(
            'com.sun.star.awt.UnoControlDialogModel')
        if nPositionX != None:
            self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:
            self.oDialogModel.PositionY = nPositionY
        if nWidth != None:
            self.oDialogModel.Width = nWidth
        if nHeight != None:
            self.oDialogModel.Height = nHeight
        if cTitle != None:
            self.oDialogModel.Title = cTitle
        self.oDialogControl = createUnoService(
            'com.sun.star.awt.UnoControlDialog')
        self.oDialogControl.setModel(self.oDialogModel)

    def release(self):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    def setDialogPosition(self, nX, nY):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY
    <mask token>

    def setDialogTitle(self, cCaption):
        self.oDialogModel.Title = cCaption

    def setVisible(self, bVisible):
        self.oDialogControl.setVisible(bVisible)

    def addButton(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cLabel=None, actionListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlButtonModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setButtonLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def addEdit(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cText=None, textListenerProc=None, cReadOnly=None, cMultiline=None,
        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl('com.sun.star.awt.UnoControlEditModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=
            cAutoVScroll)
        if cText != None:
            self.setEditText(cCtrlName, cText)
        if textListenerProc != None:
            self.addTextListenerProc(cCtrlName, textListenerProc)

    def addCheckBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None, itemListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlCheckBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def setEditText(self, cCtrlName, cText):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getEditText(self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def setCheckBoxLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def getCheckBoxState(self, cCtrlName):
        """Get the state of the control."""
        oControl = self.getControl(cCtrlName)
        return oControl.getState()

    def setCheckBoxState(self, cCtrlName, nState):
        """Set the state of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setState(nState)

    def enableCheckBoxTriState(self, cCtrlName, bTriStateEnable):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.enableTriState(bTriStateEnable)
    <mask token>

    def addControl(self, cCtrlServiceName, cCtrlName, nPositionX,
        nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
        cLabel=None, nTabIndex=None, sImagePath=None, cMultiline=None,
        cReadOnly=None, cAutoVScroll=None):
        oControlModel = self.oDialogModel.createInstance(cCtrlServiceName)
        self.oDialogModel.insertByName(cCtrlName, oControlModel)
        if nPositionX < 0:
            nPositionX = self.oDialogModel.Width + nPositionX - nWidth
        if nPositionY < 0:
            nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName
        if bDropdown != None:
            oControlModel.Dropdown = bDropdown
        if bMultiSelection != None:
            oControlModel.MultiSelection = bMultiSelection
        if cLabel != None:
            oControlModel.Label = cLabel
        if cMultiline != None:
            oControlModel.MultiLine = True
        if cReadOnly != None:
            oControlModel.ReadOnly = True
        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True
        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex
        if sImagePath != None:
            oControlModel.ImageURL = sImagePath

    def addComboListBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, bMultiSelection=False, itemListenerProc=
        None, actionListenerProc=None):
        mod = self.addControl('com.sun.star.awt.UnoControlListBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown,
            bMultiSelection)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def addListBoxItems(self, cCtrlName, tcItemTexts, nPosition=0):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl(cCtrlName)
        oControl.addItems(tcItemTexts, nPosition)

    def selectListBoxItem(self, cCtrlName, cItemText, bSelect=True):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItem(cItemText, bSelect)

    def selectListBoxItemPos(self, cCtrlName, nItemPos, bSelect=True):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItemPos(nItemPos, bSelect)

    def removeListBoxItems(self, cCtrlName, nPosition, nCount=1):
        """Remove items from a ListBox."""
        oControl = self.getControl(cCtrlName)
        oControl.removeItems(nPosition, nCount)

    def getListBoxItemCount(self, cCtrlName):
        """Get the number of items in a ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItemCount()

    def getListBoxSelectedItem(self, cCtrlName):
        """Returns the currently selected item."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItem()

    def getListBoxItem(self, cCtrlName, nPosition):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItem(nPosition)

    def getListBoxSelectedItemPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemsPos()

    def addComboBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlComboBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setComboBoxText(self, cCtrlName, cText):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getComboBoxText(self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def getComboBoxSelectedText(self, cCtrlName):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedText()

    def getControl(self, cCtrlName):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl(cCtrlName)
        return oControl

    def getControlModel(self, cCtrlName):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl(cCtrlName)
        oControlModel = oControl.getModel()
        return oControlModel

    def addImageControl(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, sImagePath='', itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlImageControlModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=
            sImagePath)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setControlModelProperty(self, cCtrlName, cPropertyName, uValue):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        oControlModel.setPropertyValue(cPropertyName, uValue)

    def getControlModelProperty(self, cCtrlName, cPropertyName):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        return oControlModel.getPropertyValue(cPropertyName)

    def setEnabled(self, cCtrlName, bEnabled=True):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Enabled', bEnabled)

    def getEnabled(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Enabled')

    def setState(self, cCtrlName, nState):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'State', nState)

    def getState(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'State')

    def setLabel(self, cCtrlName, cLabel):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Label', cLabel)

    def getLabel(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Label')

    def setHelpText(self, cCtrlName, cHelpText):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'HelpText', cHelpText)

    def getHelpText(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'HelpText')

    def setDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        oControl.setDesignMode(bDesignMode)

    def isDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isDesignMode()

    def isTransparent(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isTransparent()

    def setPosition(self, cCtrlName, nPositionX, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def setPositionX(self, cCtrlName, nPositionX):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)

    def setPositionY(self, cCtrlName, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def getPositionX(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionX')

    def getPositionY(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionY')
    <mask token>

    def setWidth(self, cCtrlName, nWidth):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)

    def setHeight(self, cCtrlName, nHeight):
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def getWidth(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Width')

    def getHeight(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Height')

    def setTabIndex(self, cCtrlName, nWidth, nTabIndex):
        self.setControlModelProperty(cCtrlName, 'TabIndex', nTabIndex)

    def getTabIndex(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'TabIndex')
    <mask token>

    def getStep(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Step')

    def setTag(self, cCtrlName, nWidth, cTag):
        self.setControlModelProperty(cCtrlName, 'Tag', cTag)

    def getTag(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Tag')

    def setEchoChar(self, cCtrlName, cVal):
        self.setControlModelProperty(cCtrlName, 'EchoChar', cVal)

    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, 'EchoChar')

    def addActionListenerProc(self, cCtrlName, actionListenerProc):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ActionListenerProcAdapter(actionListenerProc)
        oControl.addActionListener(oActionListener)

    def addItemListenerProc(self, cCtrlName, itemListenerProc):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ItemListenerProcAdapter(itemListenerProc)
        oControl.addItemListener(oActionListener)

    def doModalDialog(self, sObjName, sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible(True)
        if not sValue == None:
            self.selectListBoxItem(sObjName, sValue, True)
        self.oDialogControl.execute()

    def endExecute(self):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()


<mask token>

<mask token>


class ErrorDialog:

    def __init__(self, sErrorMsg, sErrorHelpMsg='', sTitle='Error Message'):
        self.win = DBModalDialog(50, 50, 150, 120, sTitle)
        self.win.addFixedText('lblErrMsg', 5, 5, 120, 10, sErrorMsg)
        self.win.addEdit('lblErrHelpMsg', 5, 15, 140, 75, sErrorHelpMsg,
            cAutoVScroll=True, cReadOnly=True, cMultiline=True)
        self.win.addButton('btnOK', 55, -5, 40, 15, 'Ok',
            actionListenerProc=self.btnOkOrCancel_clicked)
        self.win.doModalDialog('', None)
    <mask token>


<mask token>


class DBModalDialog:
    """Class to build a dialog box from the com.sun.star.awt.* services.
    This doesn't do anything you couldn't already do using OOo's UNO API,
     this just makes it much easier.
    You can change the dialog box size, position, title, etc.
    You can add controls, and listeners for those controls to the dialog box.
    This class can be used by subclassing it, or without subclassing it.
    """

    def __init__(self, nPositionX=None, nPositionY=None, nWidth=None,
        nHeight=None, cTitle=None):
        self.oDialogModel = createUnoService(
            'com.sun.star.awt.UnoControlDialogModel')
        if nPositionX != None:
            self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:
            self.oDialogModel.PositionY = nPositionY
        if nWidth != None:
            self.oDialogModel.Width = nWidth
        if nHeight != None:
            self.oDialogModel.Height = nHeight
        if cTitle != None:
            self.oDialogModel.Title = cTitle
        self.oDialogControl = createUnoService(
            'com.sun.star.awt.UnoControlDialog')
        self.oDialogControl.setModel(self.oDialogModel)

    def release(self):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    def setDialogPosition(self, nX, nY):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY

    def setDialogSize(self, nWidth, nHeight):
        self.oDialogModel.Width = nWidth
        self.oDialogModel.Height = nHeight

    def setDialogTitle(self, cCaption):
        self.oDialogModel.Title = cCaption

    def setVisible(self, bVisible):
        self.oDialogControl.setVisible(bVisible)

    def addButton(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cLabel=None, actionListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlButtonModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setButtonLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def addEdit(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cText=None, textListenerProc=None, cReadOnly=None, cMultiline=None,
        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl('com.sun.star.awt.UnoControlEditModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=
            cAutoVScroll)
        if cText != None:
            self.setEditText(cCtrlName, cText)
        if textListenerProc != None:
            self.addTextListenerProc(cCtrlName, textListenerProc)

    def addCheckBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None, itemListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlCheckBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def setEditText(self, cCtrlName, cText):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getEditText(self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def setCheckBoxLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def getCheckBoxState(self, cCtrlName):
        """Get the state of the control."""
        oControl = self.getControl(cCtrlName)
        return oControl.getState()

    def setCheckBoxState(self, cCtrlName, nState):
        """Set the state of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setState(nState)

    def enableCheckBoxTriState(self, cCtrlName, bTriStateEnable):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.enableTriState(bTriStateEnable)

    def addFixedText(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None):
        self.addControl('com.sun.star.awt.UnoControlFixedTextModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, cMultiline=True)
        return self.getControl(cCtrlName)

    def addControl(self, cCtrlServiceName, cCtrlName, nPositionX,
        nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
        cLabel=None, nTabIndex=None, sImagePath=None, cMultiline=None,
        cReadOnly=None, cAutoVScroll=None):
        oControlModel = self.oDialogModel.createInstance(cCtrlServiceName)
        self.oDialogModel.insertByName(cCtrlName, oControlModel)
        if nPositionX < 0:
            nPositionX = self.oDialogModel.Width + nPositionX - nWidth
        if nPositionY < 0:
            nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName
        if bDropdown != None:
            oControlModel.Dropdown = bDropdown
        if bMultiSelection != None:
            oControlModel.MultiSelection = bMultiSelection
        if cLabel != None:
            oControlModel.Label = cLabel
        if cMultiline != None:
            oControlModel.MultiLine = True
        if cReadOnly != None:
            oControlModel.ReadOnly = True
        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True
        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex
        if sImagePath != None:
            oControlModel.ImageURL = sImagePath

    def addComboListBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, bMultiSelection=False, itemListenerProc=
        None, actionListenerProc=None):
        mod = self.addControl('com.sun.star.awt.UnoControlListBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown,
            bMultiSelection)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def addListBoxItems(self, cCtrlName, tcItemTexts, nPosition=0):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl(cCtrlName)
        oControl.addItems(tcItemTexts, nPosition)

    def selectListBoxItem(self, cCtrlName, cItemText, bSelect=True):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItem(cItemText, bSelect)

    def selectListBoxItemPos(self, cCtrlName, nItemPos, bSelect=True):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItemPos(nItemPos, bSelect)

    def removeListBoxItems(self, cCtrlName, nPosition, nCount=1):
        """Remove items from a ListBox."""
        oControl = self.getControl(cCtrlName)
        oControl.removeItems(nPosition, nCount)

    def getListBoxItemCount(self, cCtrlName):
        """Get the number of items in a ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItemCount()

    def getListBoxSelectedItem(self, cCtrlName):
        """Returns the currently selected item."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItem()

    def getListBoxItem(self, cCtrlName, nPosition):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItem(nPosition)

    def getListBoxSelectedItemPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemsPos()

    def addComboBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlComboBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setComboBoxText(self, cCtrlName, cText):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getComboBoxText(self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def getComboBoxSelectedText(self, cCtrlName):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedText()

    def getControl(self, cCtrlName):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl(cCtrlName)
        return oControl

    def getControlModel(self, cCtrlName):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl(cCtrlName)
        oControlModel = oControl.getModel()
        return oControlModel

    def addImageControl(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, sImagePath='', itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlImageControlModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=
            sImagePath)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setControlModelProperty(self, cCtrlName, cPropertyName, uValue):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        oControlModel.setPropertyValue(cPropertyName, uValue)

    def getControlModelProperty(self, cCtrlName, cPropertyName):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        return oControlModel.getPropertyValue(cPropertyName)

    def setEnabled(self, cCtrlName, bEnabled=True):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Enabled', bEnabled)

    def getEnabled(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Enabled')

    def setState(self, cCtrlName, nState):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'State', nState)

    def getState(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'State')

    def setLabel(self, cCtrlName, cLabel):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Label', cLabel)

    def getLabel(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Label')

    def setHelpText(self, cCtrlName, cHelpText):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'HelpText', cHelpText)

    def getHelpText(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'HelpText')

    def setDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        oControl.setDesignMode(bDesignMode)

    def isDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isDesignMode()

    def isTransparent(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isTransparent()

    def setPosition(self, cCtrlName, nPositionX, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def setPositionX(self, cCtrlName, nPositionX):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)

    def setPositionY(self, cCtrlName, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def getPositionX(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionX')

    def getPositionY(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionY')

    def setSize(self, cCtrlName, nWidth, nHeight):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def setWidth(self, cCtrlName, nWidth):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)

    def setHeight(self, cCtrlName, nHeight):
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def getWidth(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Width')

    def getHeight(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Height')

    def setTabIndex(self, cCtrlName, nWidth, nTabIndex):
        self.setControlModelProperty(cCtrlName, 'TabIndex', nTabIndex)

    def getTabIndex(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'TabIndex')

    def setStep(self, cCtrlName, nWidth, nStep):
        self.setControlModelProperty(cCtrlName, 'Step', nStep)

    def getStep(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Step')

    def setTag(self, cCtrlName, nWidth, cTag):
        self.setControlModelProperty(cCtrlName, 'Tag', cTag)

    def getTag(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Tag')

    def setEchoChar(self, cCtrlName, cVal):
        self.setControlModelProperty(cCtrlName, 'EchoChar', cVal)

    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, 'EchoChar')

    def addActionListenerProc(self, cCtrlName, actionListenerProc):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ActionListenerProcAdapter(actionListenerProc)
        oControl.addActionListener(oActionListener)

    def addItemListenerProc(self, cCtrlName, itemListenerProc):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ItemListenerProcAdapter(itemListenerProc)
        oControl.addItemListener(oActionListener)

    def doModalDialog(self, sObjName, sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible(True)
        if not sValue == None:
            self.selectListBoxItem(sObjName, sValue, True)
        self.oDialogControl.execute()

    def endExecute(self):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()


<mask token>

<mask token>


class TextListenerProcAdapter(unohelper.Base, XTextListener):
    <mask token>

    def textChanged(self, oTextEvent):
        if callable(self.oProcToCall):
            apply(self.oProcToCall, (oTextEvent,) + self.tParams)


class ErrorDialog:

    def __init__(self, sErrorMsg, sErrorHelpMsg='', sTitle='Error Message'):
        self.win = DBModalDialog(50, 50, 150, 120, sTitle)
        self.win.addFixedText('lblErrMsg', 5, 5, 120, 10, sErrorMsg)
        self.win.addEdit('lblErrHelpMsg', 5, 15, 140, 75, sErrorHelpMsg,
            cAutoVScroll=True, cReadOnly=True, cMultiline=True)
        self.win.addButton('btnOK', 55, -5, 40, 15, 'Ok',
            actionListenerProc=self.btnOkOrCancel_clicked)
        self.win.doModalDialog('', None)

    def btnOkOrCancel_clicked(self, oActionEvent):
        self.win.endExecute()


<mask token>


class DBModalDialog:
    """Class to build a dialog box from the com.sun.star.awt.* services.
    This doesn't do anything you couldn't already do using OOo's UNO API,
     this just makes it much easier.
    You can change the dialog box size, position, title, etc.
    You can add controls, and listeners for those controls to the dialog box.
    This class can be used by subclassing it, or without subclassing it.
    """

    def __init__(self, nPositionX=None, nPositionY=None, nWidth=None,
        nHeight=None, cTitle=None):
        self.oDialogModel = createUnoService(
            'com.sun.star.awt.UnoControlDialogModel')
        if nPositionX != None:
            self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:
            self.oDialogModel.PositionY = nPositionY
        if nWidth != None:
            self.oDialogModel.Width = nWidth
        if nHeight != None:
            self.oDialogModel.Height = nHeight
        if cTitle != None:
            self.oDialogModel.Title = cTitle
        self.oDialogControl = createUnoService(
            'com.sun.star.awt.UnoControlDialog')
        self.oDialogControl.setModel(self.oDialogModel)

    def release(self):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    def setDialogPosition(self, nX, nY):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY

    def setDialogSize(self, nWidth, nHeight):
        self.oDialogModel.Width = nWidth
        self.oDialogModel.Height = nHeight

    def setDialogTitle(self, cCaption):
        self.oDialogModel.Title = cCaption

    def setVisible(self, bVisible):
        self.oDialogControl.setVisible(bVisible)

    def addButton(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cLabel=None, actionListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlButtonModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setButtonLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def addEdit(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cText=None, textListenerProc=None, cReadOnly=None, cMultiline=None,
        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl('com.sun.star.awt.UnoControlEditModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=
            cAutoVScroll)
        if cText != None:
            self.setEditText(cCtrlName, cText)
        if textListenerProc != None:
            self.addTextListenerProc(cCtrlName, textListenerProc)

    def addCheckBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None, itemListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlCheckBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def setEditText(self, cCtrlName, cText):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getEditText(self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def setCheckBoxLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def getCheckBoxState(self, cCtrlName):
        """Get the state of the control."""
        oControl = self.getControl(cCtrlName)
        return oControl.getState()

    def setCheckBoxState(self, cCtrlName, nState):
        """Set the state of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setState(nState)

    def enableCheckBoxTriState(self, cCtrlName, bTriStateEnable):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.enableTriState(bTriStateEnable)

    def addFixedText(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None):
        self.addControl('com.sun.star.awt.UnoControlFixedTextModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, cMultiline=True)
        return self.getControl(cCtrlName)

    def addControl(self, cCtrlServiceName, cCtrlName, nPositionX,
        nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
        cLabel=None, nTabIndex=None, sImagePath=None, cMultiline=None,
        cReadOnly=None, cAutoVScroll=None):
        oControlModel = self.oDialogModel.createInstance(cCtrlServiceName)
        self.oDialogModel.insertByName(cCtrlName, oControlModel)
        if nPositionX < 0:
            nPositionX = self.oDialogModel.Width + nPositionX - nWidth
        if nPositionY < 0:
            nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName
        if bDropdown != None:
            oControlModel.Dropdown = bDropdown
        if bMultiSelection != None:
            oControlModel.MultiSelection = bMultiSelection
        if cLabel != None:
            oControlModel.Label = cLabel
        if cMultiline != None:
            oControlModel.MultiLine = True
        if cReadOnly != None:
            oControlModel.ReadOnly = True
        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True
        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex
        if sImagePath != None:
            oControlModel.ImageURL = sImagePath

    def addComboListBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, bMultiSelection=False, itemListenerProc=
        None, actionListenerProc=None):
        mod = self.addControl('com.sun.star.awt.UnoControlListBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown,
            bMultiSelection)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def addListBoxItems(self, cCtrlName, tcItemTexts, nPosition=0):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl(cCtrlName)
        oControl.addItems(tcItemTexts, nPosition)

    def selectListBoxItem(self, cCtrlName, cItemText, bSelect=True):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItem(cItemText, bSelect)

    def selectListBoxItemPos(self, cCtrlName, nItemPos, bSelect=True):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItemPos(nItemPos, bSelect)

    def removeListBoxItems(self, cCtrlName, nPosition, nCount=1):
        """Remove items from a ListBox."""
        oControl = self.getControl(cCtrlName)
        oControl.removeItems(nPosition, nCount)

    def getListBoxItemCount(self, cCtrlName):
        """Get the number of items in a ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItemCount()

    def getListBoxSelectedItem(self, cCtrlName):
        """Returns the currently selected item."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItem()

    def getListBoxItem(self, cCtrlName, nPosition):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItem(nPosition)

    def getListBoxSelectedItemPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemsPos()

    def addComboBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlComboBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setComboBoxText(self, cCtrlName, cText):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getComboBoxText(self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def getComboBoxSelectedText(self, cCtrlName):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedText()

    def getControl(self, cCtrlName):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl(cCtrlName)
        return oControl

    def getControlModel(self, cCtrlName):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl(cCtrlName)
        oControlModel = oControl.getModel()
        return oControlModel

    def addImageControl(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, sImagePath='', itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlImageControlModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=
            sImagePath)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setControlModelProperty(self, cCtrlName, cPropertyName, uValue):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        oControlModel.setPropertyValue(cPropertyName, uValue)

    def getControlModelProperty(self, cCtrlName, cPropertyName):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        return oControlModel.getPropertyValue(cPropertyName)

    def setEnabled(self, cCtrlName, bEnabled=True):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Enabled', bEnabled)

    def getEnabled(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Enabled')

    def setState(self, cCtrlName, nState):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'State', nState)

    def getState(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'State')

    def setLabel(self, cCtrlName, cLabel):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Label', cLabel)

    def getLabel(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Label')

    def setHelpText(self, cCtrlName, cHelpText):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'HelpText', cHelpText)

    def getHelpText(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'HelpText')

    def setDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        oControl.setDesignMode(bDesignMode)

    def isDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isDesignMode()

    def isTransparent(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isTransparent()

    def setPosition(self, cCtrlName, nPositionX, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def setPositionX(self, cCtrlName, nPositionX):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)

    def setPositionY(self, cCtrlName, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def getPositionX(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionX')

    def getPositionY(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionY')

    def setSize(self, cCtrlName, nWidth, nHeight):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def setWidth(self, cCtrlName, nWidth):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)

    def setHeight(self, cCtrlName, nHeight):
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def getWidth(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Width')

    def getHeight(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Height')

    def setTabIndex(self, cCtrlName, nWidth, nTabIndex):
        self.setControlModelProperty(cCtrlName, 'TabIndex', nTabIndex)

    def getTabIndex(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'TabIndex')

    def setStep(self, cCtrlName, nWidth, nStep):
        self.setControlModelProperty(cCtrlName, 'Step', nStep)

    def getStep(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Step')

    def setTag(self, cCtrlName, nWidth, cTag):
        self.setControlModelProperty(cCtrlName, 'Tag', cTag)

    def getTag(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Tag')

    def setEchoChar(self, cCtrlName, cVal):
        self.setControlModelProperty(cCtrlName, 'EchoChar', cVal)

    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, 'EchoChar')

    def addActionListenerProc(self, cCtrlName, actionListenerProc):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ActionListenerProcAdapter(actionListenerProc)
        oControl.addActionListener(oActionListener)

    def addItemListenerProc(self, cCtrlName, itemListenerProc):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ItemListenerProcAdapter(itemListenerProc)
        oControl.addItemListener(oActionListener)

    def doModalDialog(self, sObjName, sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible(True)
        if not sValue == None:
            self.selectListBoxItem(sObjName, sValue, True)
        self.oDialogControl.execute()

    def endExecute(self):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()


<mask token>

<mask token>


class ActionListenerProcAdapter(unohelper.Base, XActionListener):

    def __init__(self, oProcToCall, tParams=()):
        self.oProcToCall = oProcToCall
        self.tParams = tParams

    def actionPerformed(self, oActionEvent):
        if callable(self.oProcToCall):
            apply(self.oProcToCall, (oActionEvent,) + self.tParams)


<mask token>


class ItemListenerProcAdapter(unohelper.Base, XItemListener):

    def __init__(self, oProcToCall, tParams=()):
        self.oProcToCall = oProcToCall
        self.tParams = tParams

    def itemStateChanged(self, oItemEvent):
        if callable(self.oProcToCall):
            apply(self.oProcToCall, (oItemEvent,) + self.tParams)


<mask token>


class TextListenerProcAdapter(unohelper.Base, XTextListener):

    def __init__(self, oProcToCall, tParams=()):
        self.oProcToCall = oProcToCall
        self.tParams = tParams

    def textChanged(self, oTextEvent):
        if callable(self.oProcToCall):
            apply(self.oProcToCall, (oTextEvent,) + self.tParams)


class ErrorDialog:

    def __init__(self, sErrorMsg, sErrorHelpMsg='', sTitle='Error Message'):
        self.win = DBModalDialog(50, 50, 150, 120, sTitle)
        self.win.addFixedText('lblErrMsg', 5, 5, 120, 10, sErrorMsg)
        self.win.addEdit('lblErrHelpMsg', 5, 15, 140, 75, sErrorHelpMsg,
            cAutoVScroll=True, cReadOnly=True, cMultiline=True)
        self.win.addButton('btnOK', 55, -5, 40, 15, 'Ok',
            actionListenerProc=self.btnOkOrCancel_clicked)
        self.win.doModalDialog('', None)

    def btnOkOrCancel_clicked(self, oActionEvent):
        self.win.endExecute()


<mask token>


class DBModalDialog:
    """Class to build a dialog box from the com.sun.star.awt.* services.
    This doesn't do anything you couldn't already do using OOo's UNO API,
     this just makes it much easier.
    You can change the dialog box size, position, title, etc.
    You can add controls, and listeners for those controls to the dialog box.
    This class can be used by subclassing it, or without subclassing it.
    """

    def __init__(self, nPositionX=None, nPositionY=None, nWidth=None,
        nHeight=None, cTitle=None):
        self.oDialogModel = createUnoService(
            'com.sun.star.awt.UnoControlDialogModel')
        if nPositionX != None:
            self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:
            self.oDialogModel.PositionY = nPositionY
        if nWidth != None:
            self.oDialogModel.Width = nWidth
        if nHeight != None:
            self.oDialogModel.Height = nHeight
        if cTitle != None:
            self.oDialogModel.Title = cTitle
        self.oDialogControl = createUnoService(
            'com.sun.star.awt.UnoControlDialog')
        self.oDialogControl.setModel(self.oDialogModel)

    def release(self):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    def setDialogPosition(self, nX, nY):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY

    def setDialogSize(self, nWidth, nHeight):
        self.oDialogModel.Width = nWidth
        self.oDialogModel.Height = nHeight

    def setDialogTitle(self, cCaption):
        self.oDialogModel.Title = cCaption

    def setVisible(self, bVisible):
        self.oDialogControl.setVisible(bVisible)

    def addButton(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cLabel=None, actionListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlButtonModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setButtonLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def addEdit(self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
        cText=None, textListenerProc=None, cReadOnly=None, cMultiline=None,
        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl('com.sun.star.awt.UnoControlEditModel', cCtrlName,
            nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,
            cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=
            cAutoVScroll)
        if cText != None:
            self.setEditText(cCtrlName, cText)
        if textListenerProc != None:
            self.addTextListenerProc(cCtrlName, textListenerProc)

    def addCheckBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None, itemListenerProc=None, nTabIndex=None):
        self.addControl('com.sun.star.awt.UnoControlCheckBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, nTabIndex=nTabIndex)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def setEditText(self, cCtrlName, cText):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getEditText(self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def setCheckBoxLabel(self, cCtrlName, cLabel):
        """Set the label of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setLabel(cLabel)

    def getCheckBoxState(self, cCtrlName):
        """Get the state of the control."""
        oControl = self.getControl(cCtrlName)
        return oControl.getState()

    def setCheckBoxState(self, cCtrlName, nState):
        """Set the state of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.setState(nState)

    def enableCheckBoxTriState(self, cCtrlName, bTriStateEnable):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl(cCtrlName)
        oControl.enableTriState(bTriStateEnable)

    def addFixedText(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, cLabel=None):
        self.addControl('com.sun.star.awt.UnoControlFixedTextModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=
            None, bMultiSelection=None, cLabel=cLabel, cMultiline=True)
        return self.getControl(cCtrlName)

    def addControl(self, cCtrlServiceName, cCtrlName, nPositionX,
        nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
        cLabel=None, nTabIndex=None, sImagePath=None, cMultiline=None,
        cReadOnly=None, cAutoVScroll=None):
        oControlModel = self.oDialogModel.createInstance(cCtrlServiceName)
        self.oDialogModel.insertByName(cCtrlName, oControlModel)
        if nPositionX < 0:
            nPositionX = self.oDialogModel.Width + nPositionX - nWidth
        if nPositionY < 0:
            nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName
        if bDropdown != None:
            oControlModel.Dropdown = bDropdown
        if bMultiSelection != None:
            oControlModel.MultiSelection = bMultiSelection
        if cLabel != None:
            oControlModel.Label = cLabel
        if cMultiline != None:
            oControlModel.MultiLine = True
        if cReadOnly != None:
            oControlModel.ReadOnly = True
        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True
        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex
        if sImagePath != None:
            oControlModel.ImageURL = sImagePath

    def addComboListBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, bMultiSelection=False, itemListenerProc=
        None, actionListenerProc=None):
        mod = self.addControl('com.sun.star.awt.UnoControlListBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown,
            bMultiSelection)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)

    def addListBoxItems(self, cCtrlName, tcItemTexts, nPosition=0):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl(cCtrlName)
        oControl.addItems(tcItemTexts, nPosition)

    def selectListBoxItem(self, cCtrlName, cItemText, bSelect=True):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItem(cItemText, bSelect)

    def selectListBoxItemPos(self, cCtrlName, nItemPos, bSelect=True):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl(cCtrlName)
        return oControl.selectItemPos(nItemPos, bSelect)

    def removeListBoxItems(self, cCtrlName, nPosition, nCount=1):
        """Remove items from a ListBox."""
        oControl = self.getControl(cCtrlName)
        oControl.removeItems(nPosition, nCount)

    def getListBoxItemCount(self, cCtrlName):
        """Get the number of items in a ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItemCount()

    def getListBoxSelectedItem(self, cCtrlName):
        """Returns the currently selected item."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItem()

    def getListBoxItem(self, cCtrlName, nPosition):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getItem(nPosition)

    def getListBoxSelectedItemPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self, cCtrlName):
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedItemsPos()

    def addComboBox(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, bDropdown=True, itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlComboBoxModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setComboBoxText(self, cCtrlName, cText):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        oControl.setText(cText)

    def getComboBoxText(self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getText()

    def getComboBoxSelectedText(self, cCtrlName):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl(cCtrlName)
        return oControl.getSelectedText()

    def getControl(self, cCtrlName):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl(cCtrlName)
        return oControl

    def getControlModel(self, cCtrlName):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl(cCtrlName)
        oControlModel = oControl.getModel()
        return oControlModel

    def addImageControl(self, cCtrlName, nPositionX, nPositionY, nWidth,
        nHeight, sImagePath='', itemListenerProc=None, actionListenerProc=None
        ):
        mod = self.addControl('com.sun.star.awt.UnoControlImageControlModel',
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=
            sImagePath)
        if itemListenerProc != None:
            self.addItemListenerProc(cCtrlName, itemListenerProc)
        if actionListenerProc != None:
            self.addActionListenerProc(cCtrlName, actionListenerProc)

    def setControlModelProperty(self, cCtrlName, cPropertyName, uValue):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        oControlModel.setPropertyValue(cPropertyName, uValue)

    def getControlModelProperty(self, cCtrlName, cPropertyName):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel(cCtrlName)
        return oControlModel.getPropertyValue(cPropertyName)

    def setEnabled(self, cCtrlName, bEnabled=True):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Enabled', bEnabled)

    def getEnabled(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Enabled')

    def setState(self, cCtrlName, nState):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'State', nState)

    def getState(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'State')

    def setLabel(self, cCtrlName, cLabel):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'Label', cLabel)

    def getLabel(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'Label')

    def setHelpText(self, cCtrlName, cHelpText):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty(cCtrlName, 'HelpText', cHelpText)

    def getHelpText(self, cCtrlName):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty(cCtrlName, 'HelpText')

    def setDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        oControl.setDesignMode(bDesignMode)

    def isDesignMode(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isDesignMode()

    def isTransparent(self, cCtrlName, bDesignMode=True):
        oControl = self.getControl(cCtrlName)
        return oControl.isTransparent()

    def setPosition(self, cCtrlName, nPositionX, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def setPositionX(self, cCtrlName, nPositionX):
        self.setControlModelProperty(cCtrlName, 'PositionX', nPositionX)

    def setPositionY(self, cCtrlName, nPositionY):
        self.setControlModelProperty(cCtrlName, 'PositionY', nPositionY)

    def getPositionX(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionX')

    def getPositionY(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'PositionY')

    def setSize(self, cCtrlName, nWidth, nHeight):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def setWidth(self, cCtrlName, nWidth):
        self.setControlModelProperty(cCtrlName, 'Width', nWidth)

    def setHeight(self, cCtrlName, nHeight):
        self.setControlModelProperty(cCtrlName, 'Height', nHeight)

    def getWidth(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Width')

    def getHeight(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Height')

    def setTabIndex(self, cCtrlName, nWidth, nTabIndex):
        self.setControlModelProperty(cCtrlName, 'TabIndex', nTabIndex)

    def getTabIndex(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'TabIndex')

    def setStep(self, cCtrlName, nWidth, nStep):
        self.setControlModelProperty(cCtrlName, 'Step', nStep)

    def getStep(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Step')

    def setTag(self, cCtrlName, nWidth, cTag):
        self.setControlModelProperty(cCtrlName, 'Tag', cTag)

    def getTag(self, cCtrlName):
        return self.getControlModelProperty(cCtrlName, 'Tag')

    def setEchoChar(self, cCtrlName, cVal):
        self.setControlModelProperty(cCtrlName, 'EchoChar', cVal)

    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, 'EchoChar')

    def addActionListenerProc(self, cCtrlName, actionListenerProc):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ActionListenerProcAdapter(actionListenerProc)
        oControl.addActionListener(oActionListener)

    def addItemListenerProc(self, cCtrlName, itemListenerProc):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl(cCtrlName)
        oActionListener = ItemListenerProcAdapter(itemListenerProc)
        oControl.addItemListener(oActionListener)

    def doModalDialog(self, sObjName, sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible(True)
        if not sValue == None:
            self.selectListBoxItem(sObjName, sValue, True)
        self.oDialogControl.execute()

    def endExecute(self):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()


<mask token>


def getServiceManager(cHost='localhost', cPort='2002'):
    """Get the ServiceManager from the running OpenOffice.org.
        Then retain it in the global variable goServiceManager for future use.
        This is similar to the GetProcessServiceManager() in OOo Basic.
    """
    global goServiceManager
    global pythonloader
    if not goServiceManager:
        oLocalContext = uno.getComponentContext()
        goServiceManager = oLocalContext.ServiceManager
    return goServiceManager


<mask token>


def getCoreReflection():
    global goCoreReflection
    if not goCoreReflection:
        goCoreReflection = createUnoService(
            'com.sun.star.reflection.CoreReflection')
    return goCoreReflection


def createUnoStruct(cTypeName):
    """Create a UNO struct and return it.
    Similar to the function of the same name in OOo Basic.
    """
    oCoreReflection = getCoreReflection()
    oXIdlClass = oCoreReflection.forName(cTypeName)
    oReturnValue, oStruct = oXIdlClass.createObject(None)
    return oStruct


<mask token>


def hasUnoInterfaces(oObject, *cInterfaces):
    """Similar to the function of the same name in OOo Basic."""
    for cInterface in cInterfaces:
        if not hasUnoInterface(oObject, cInterface):
            return False
    return True


<mask token>


def makeSize(nWidth, nHeight):
    """Create a com.sun.star.awt.Size struct."""
    oSize = createUnoStruct('com.sun.star.awt.Size')
    oSize.Width = nWidth
    oSize.Height = nHeight
    return oSize


def makeRectangle(nX, nY, nWidth, nHeight):
    """Create a com.sun.star.awt.Rectangle struct."""
    oRect = createUnoStruct('com.sun.star.awt.Rectangle')
    oRect.X = nX
    oRect.Y = nY
    oRect.Width = nWidth
    oRect.Height = nHeight
    return oRect


def Array(*args):
    """This is just sugar coating so that code from OOoBasic which
    contains the Array() function can work perfectly in python."""
    tArray = ()
    for arg in args:
        tArray += arg,
    return tArray


<mask token>


def convertToURL(cPathname):
    """Convert a Windows or Linux pathname into an OOo URL."""
    if len(cPathname) > 1:
        if cPathname[1:2] == ':':
            cPathname = '/' + cPathname[0] + '|' + cPathname[2:]
    cPathname = cPathname.replace('\\', '/')
    cPathname = 'file://' + cPathname
    return cPathname

###########################################################################
#
# A collection of helper functions by Danny Brewer
# as extracted from the original OpenERP report designer plugin
#
#
#   Copyright (c) 2003-2004 Danny Brewer 
#   d29583@groovegarden.com 
# 
#   This library is free software; you can redistribute it and/or 
#   modify it under the terms of the GNU Lesser General Public 
#   License as published by the Free Software Foundation; either 
#   version 2.1 of the License, or (at your option) any later version. 
# 
#   This library is distributed in the hope that it will be useful, 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
#   Lesser General Public License for more details. 
# 
#   You should have received a copy of the GNU Lesser General Public 
#   License along with this library; if not, write to the Free Software 
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
# 
#   See:  http://www.gnu.org/licenses/lgpl.html 
############################################################################
import uno
import unohelper
import pythonloader

from com.sun.star.awt import XActionListener
class ActionListenerProcAdapter( unohelper.Base, XActionListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oActionEvent is a com.sun.star.awt.ActionEvent struct.
    def actionPerformed( self, oActionEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oActionEvent,) + self.tParams )

#--------------------------------------------------
# An ItemListener adapter.
# This object implements com.sun.star.awt.XItemListener.
# When itemStateChanged is called, this will call an arbitrary
#  python procedure, passing it...
#   1. the oItemEvent
#   2. any other parameters you specified to this object's constructor (as a tuple).
from com.sun.star.awt import XItemListener
class ItemListenerProcAdapter( unohelper.Base, XItemListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oItemEvent is a com.sun.star.awt.ItemEvent struct.
    def itemStateChanged( self, oItemEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oItemEvent,) + self.tParams )

#--------------------------------------------------
# An TextListener adapter.
# This object implements com.sun.star.awt.XTextistener.
# When textChanged is called, this will call an arbitrary
#  python procedure, passing it...
#   1. the oTextEvent
#   2. any other parameters you specified to this object's constructor (as a tuple).
from com.sun.star.awt import XTextListener
class TextListenerProcAdapter( unohelper.Base, XTextListener ):
    def __init__( self, oProcToCall, tParams=() ):
        self.oProcToCall = oProcToCall # a python procedure
        self.tParams = tParams # a tuple

    # oTextEvent is a com.sun.star.awt.TextEvent struct.
    def textChanged( self, oTextEvent ):
        if callable( self.oProcToCall ):
            apply( self.oProcToCall, (oTextEvent,) + self.tParams )

class ErrorDialog:
    def __init__(self,sErrorMsg, sErrorHelpMsg="",sTitle="Error Message"):
        self.win = DBModalDialog(50, 50, 150, 120, sTitle)
        self.win.addFixedText("lblErrMsg", 5, 5, 120, 10, sErrorMsg)
        self.win.addEdit(
            "lblErrHelpMsg", 5, 15, 140, 75, sErrorHelpMsg,
            cAutoVScroll=True, cReadOnly=True, cMultiline=True)
        self.win.addButton('btnOK', 55,-5,40,15,'Ok'
                     ,actionListenerProc = self.btnOkOrCancel_clicked )
        self.win.doModalDialog("",None)
    def btnOkOrCancel_clicked( self, oActionEvent ):
        self.win.endExecute()

# The global Awt Toolkit.
# This is initialized the first time it is needed.
#goAwtToolkit = createUnoService( "com.sun.star.awt.Toolkit" )
goAwtToolkit = None

def getAwtToolkit():
    global goAwtToolkit
    if goAwtToolkit == None:
        goAwtToolkit = createUnoService( "com.sun.star.awt.Toolkit" )
    return goAwtToolkit

# This class builds dialog boxes.
# This can be used in two different ways...
# 1. by subclassing it (elegant)
# 2. without subclassing it (less elegant)
class DBModalDialog:
    """Class to build a dialog box from the com.sun.star.awt.* services.
    This doesn't do anything you couldn't already do using OOo's UNO API,
     this just makes it much easier.
    You can change the dialog box size, position, title, etc.
    You can add controls, and listeners for those controls to the dialog box.
    This class can be used by subclassing it, or without subclassing it.
    """
    def __init__( self, nPositionX=None, nPositionY=None, nWidth=None, nHeight=None, cTitle=None ):
        self.oDialogModel = createUnoService( "com.sun.star.awt.UnoControlDialogModel" )
        if nPositionX != None:  self.oDialogModel.PositionX = nPositionX
        if nPositionY != None:  self.oDialogModel.PositionY = nPositionY
        if nWidth     != None:  self.oDialogModel.Width     = nWidth
        if nHeight    != None:  self.oDialogModel.Height    = nHeight
        if cTitle     != None:  self.oDialogModel.Title     = cTitle
        self.oDialogControl = createUnoService( "com.sun.star.awt.UnoControlDialog" )
        self.oDialogControl.setModel( self.oDialogModel )

    def release( self ):
        """Release resources.
        After calling this, you can no longer use this object.
        """
        self.oDialogControl.dispose()

    #--------------------------------------------------
    #   Dialog box adjustments
    #--------------------------------------------------

    def setDialogPosition( self, nX, nY ):
        self.oDialogModel.PositionX = nX
        self.oDialogModel.PositionY = nY

    def setDialogSize( self, nWidth, nHeight ):
        self.oDialogModel.Width = nWidth
        self.oDialogModel.Height = nHeight

    def setDialogTitle( self, cCaption ):
        self.oDialogModel.Title = cCaption

    def setVisible( self, bVisible ):
        self.oDialogControl.setVisible( bVisible )


    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlButton
    #--------------------------------------------------

    # After you add a Button control, you can call self.setControlModelProperty()
    #  passing any of the properties for a...
    #       com.sun.star.awt.UnoControlButtonModel
    #       com.sun.star.awt.UnoControlDialogElement
    #       com.sun.star.awt.UnoControlModel
    def addButton( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                       cLabel=None,
                       actionListenerProc=None,
                       nTabIndex=None ):
        self.addControl( "com.sun.star.awt.UnoControlButtonModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None, bMultiSelection=None,
                         cLabel=cLabel,
                         nTabIndex=nTabIndex )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )

    def setButtonLabel( self, cCtrlName, cLabel ):
        """Set the label of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setLabel( cLabel )

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlEditModel
    #--------------------------------------------------
    def addEdit( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        cText=None,
                        textListenerProc=None,
                        cReadOnly=None,
                        cMultiline=None,
                        cAutoVScroll=None):
        """Add a Edit control to the window."""
        self.addControl( "com.sun.star.awt.UnoControlEditModel",
            cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None, cMultiline=cMultiline, cReadOnly=cReadOnly, cAutoVScroll=cAutoVScroll)

        if cText != None:
            self.setEditText( cCtrlName, cText )
        if textListenerProc != None:
            self.addTextListenerProc( cCtrlName, textListenerProc )

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlCheckBox
    #--------------------------------------------------

    # After you add a CheckBox control, you can call self.setControlModelProperty()
    #  passing any of the properties for a...
    #       com.sun.star.awt.UnoControlCheckBoxModel
    #       com.sun.star.awt.UnoControlDialogElement
    #       com.sun.star.awt.UnoControlModel
    def addCheckBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                       cLabel=None,
                       itemListenerProc=None,
                       nTabIndex=None ):
        self.addControl( "com.sun.star.awt.UnoControlCheckBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, bDropdown=None,  bMultiSelection=None,
                         cLabel=cLabel,
                         nTabIndex=nTabIndex )
        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )

    def setEditText( self, cCtrlName, cText ):
        """Set the text of the edit box."""
        oControl = self.getControl( cCtrlName )
        oControl.setText( cText )

    def getEditText( self, cCtrlName):
        """Set the text of the edit box."""
        oControl = self.getControl( cCtrlName )
        return oControl.getText()

    def setCheckBoxLabel( self, cCtrlName, cLabel ):
        """Set the label of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setLabel( cLabel )

    def getCheckBoxState( self, cCtrlName ):
        """Get the state of the control."""
        oControl = self.getControl( cCtrlName )
        return oControl.getState();

    def setCheckBoxState( self, cCtrlName, nState ):
        """Set the state of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.setState( nState )

    def enableCheckBoxTriState( self, cCtrlName, bTriStateEnable ):
        """Enable or disable the tri state mode of the control."""
        oControl = self.getControl( cCtrlName )
        oControl.enableTriState( bTriStateEnable )


    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlFixedText
    #--------------------------------------------------

    def addFixedText( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        cLabel=None ):
        self.addControl( "com.sun.star.awt.UnoControlFixedTextModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                         bDropdown=None, bMultiSelection=None,
                         cLabel=cLabel, cMultiline=True)
        return self.getControl( cCtrlName )

    #--------------------------------------------------
    #   Add Controls to dialog
    #--------------------------------------------------

    def addControl( self, cCtrlServiceName,
                        cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=None,
                        bMultiSelection=None,
                        cLabel=None,
                        nTabIndex=None,
                        sImagePath=None,
                        cMultiline=None,
                        cReadOnly=None,
                        cAutoVScroll=None
                         ):
        oControlModel = self.oDialogModel.createInstance( cCtrlServiceName )
        self.oDialogModel.insertByName( cCtrlName, oControlModel )
        # if negative coordinates are given for X or Y position,
        #  then make that coordinate be relative to the right/bottom
        #  edge of the dialog box instead of to the left/top.
        if nPositionX < 0: nPositionX = self.oDialogModel.Width  + nPositionX - nWidth
        if nPositionY < 0: nPositionY = self.oDialogModel.Height + nPositionY - nHeight
        oControlModel.PositionX = nPositionX
        oControlModel.PositionY = nPositionY
        oControlModel.Width = nWidth
        oControlModel.Height = nHeight
        oControlModel.Name = cCtrlName

        if bDropdown != None:
            oControlModel.Dropdown = bDropdown

        if bMultiSelection!=None:
            oControlModel.MultiSelection=bMultiSelection

        if cLabel != None:
            oControlModel.Label = cLabel

        if cMultiline != None:
            oControlModel.MultiLine = True

        if cReadOnly != None:
            oControlModel.ReadOnly = True

        if cAutoVScroll != None:
            oControlModel.AutoVScroll = True

        if nTabIndex != None:
            oControlModel.TabIndex = nTabIndex

        if sImagePath != None:
            oControlModel.ImageURL = sImagePath
    #--------------------------------------------------
    #   Access controls and control models
    #--------------------------------------------------

    #--------------------------------------------------
    #   com.sun.star.awt.UnoContorlListBoxModel
    #--------------------------------------------------


    def addComboListBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=True,
                        bMultiSelection=False,
                        itemListenerProc=None,
                        actionListenerProc=None,
                        ):

        mod = self.addControl( "com.sun.star.awt.UnoControlListBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,bDropdown,bMultiSelection )

        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )

    def addListBoxItems( self, cCtrlName, tcItemTexts, nPosition=0 ):
        """Add a tupple of items to the ListBox at specified position."""
        oControl = self.getControl( cCtrlName )
        oControl.addItems( tcItemTexts, nPosition )

    def selectListBoxItem( self, cCtrlName, cItemText, bSelect=True ):
        """Selects/Deselects the ispecified item."""
        oControl = self.getControl( cCtrlName )
        return oControl.selectItem( cItemText, bSelect )

    def selectListBoxItemPos( self, cCtrlName, nItemPos, bSelect=True ):
        """Select/Deselect the item at the specified position."""
        oControl = self.getControl( cCtrlName )
        return oControl.selectItemPos( nItemPos, bSelect )

    def removeListBoxItems( self, cCtrlName, nPosition, nCount=1 ):
        """Remove items from a ListBox."""
        oControl = self.getControl( cCtrlName )
        oControl.removeItems( nPosition, nCount )

    def getListBoxItemCount( self, cCtrlName ):
        """Get the number of items in a ListBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getItemCount()

    def getListBoxSelectedItem( self, cCtrlName ):
        """Returns the currently selected item."""
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItem()

    def getListBoxItem( self, cCtrlName, nPosition ):
        """Return the item at specified position within the ListBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getItem( nPosition )

    def getListBoxSelectedItemPos(self,cCtrlName):

        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItemPos()

    def getListBoxSelectedItems(self,cCtrlName):
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItems()

    def getListBoxSelectedItemsPos(self,cCtrlName):

        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedItemsPos()

    #--------------------------------------------------
    #   com.sun.star.awt.UnoControlComboBoxModel
    #--------------------------------------------------
    def addComboBox( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        bDropdown=True,
                        itemListenerProc=None,
                        actionListenerProc=None ):

        mod = self.addControl( "com.sun.star.awt.UnoControlComboBoxModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight,bDropdown)
        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )


    def setComboBoxText( self, cCtrlName, cText ):
        """Set the text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        oControl.setText( cText )

    def getComboBoxText( self, cCtrlName):
        """Set the text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getText()

    def getComboBoxSelectedText( self, cCtrlName ):
        """Get the selected text of the ComboBox."""
        oControl = self.getControl( cCtrlName )
        return oControl.getSelectedText();

    def getControl( self, cCtrlName ):
        """Get the control (not its model) for a particular control name.
        The control returned includes the service com.sun.star.awt.UnoControl,
         and another control-specific service which inherits from it.
        """
        oControl = self.oDialogControl.getControl( cCtrlName )
        return oControl

    def getControlModel( self, cCtrlName ):
        """Get the control model (not the control) for a particular control name.
        The model returned includes the service UnoControlModel,
         and another control-specific service which inherits from it.
        """
        oControl = self.getControl( cCtrlName )
        oControlModel = oControl.getModel()
        return oControlModel
    #---------------------------------------------------
    #    com.sun.star.awt.UnoControlImageControlModel
    #---------------------------------------------------
    def addImageControl( self, cCtrlName, nPositionX, nPositionY, nWidth, nHeight,
                        sImagePath="",
                        itemListenerProc=None,
                        actionListenerProc=None ):

        mod = self.addControl( "com.sun.star.awt.UnoControlImageControlModel",
                         cCtrlName, nPositionX, nPositionY, nWidth, nHeight, sImagePath=sImagePath)

        if itemListenerProc != None:
            self.addItemListenerProc( cCtrlName, itemListenerProc )
        if actionListenerProc != None:
            self.addActionListenerProc( cCtrlName, actionListenerProc )


    #--------------------------------------------------
    #   Adjust properties of control models
    #--------------------------------------------------

    def setControlModelProperty( self, cCtrlName, cPropertyName, uValue ):
        """Set the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel( cCtrlName )
        oControlModel.setPropertyValue( cPropertyName, uValue )

    def getControlModelProperty( self, cCtrlName, cPropertyName ):
        """Get the value of a property of a control's model.
        This affects the control model, not the control.
        """
        oControlModel = self.getControlModel( cCtrlName )
        return oControlModel.getPropertyValue( cPropertyName )

    #--------------------------------------------------
    #   Sugar coated property adjustments to control models.
    #--------------------------------------------------

    def setEnabled( self, cCtrlName, bEnabled=True ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "Enabled", bEnabled )

    def getEnabled( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """

        return self.getControlModelProperty( cCtrlName, "Enabled" )

    def setState( self, cCtrlName, nState ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "State", nState )

    def getState( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "State" )

    def setLabel( self, cCtrlName, cLabel ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "Label", cLabel )

    def getLabel( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "Label" )

    def setHelpText( self, cCtrlName, cHelpText ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        self.setControlModelProperty( cCtrlName, "HelpText", cHelpText )

    def getHelpText( self, cCtrlName ):
        """Supported controls...
            UnoControlButtonModel
            UnoControlCheckBoxModel
        """
        return self.getControlModelProperty( cCtrlName, "HelpText" )


    #--------------------------------------------------
    #   Adjust controls (not models)
    #--------------------------------------------------

    # The following apply to all controls which are a
    #   com.sun.star.awt.UnoControl

    def setDesignMode( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        oControl.setDesignMode( bDesignMode )

    def isDesignMode( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        return oControl.isDesignMode()

    def isTransparent( self, cCtrlName, bDesignMode=True ):
        oControl = self.getControl( cCtrlName )
        return oControl.isTransparent()


    # The following apply to all controls which are a
    #   com.sun.star.awt.UnoControlDialogElement

    def setPosition( self, cCtrlName, nPositionX, nPositionY ):
        self.setControlModelProperty( cCtrlName, "PositionX", nPositionX )
        self.setControlModelProperty( cCtrlName, "PositionY", nPositionY )
    def setPositionX( self, cCtrlName, nPositionX ):
        self.setControlModelProperty( cCtrlName, "PositionX", nPositionX )
    def setPositionY( self, cCtrlName, nPositionY ):
        self.setControlModelProperty( cCtrlName, "PositionY", nPositionY )
    def getPositionX( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "PositionX" )
    def getPositionY( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "PositionY" )

    def setSize( self, cCtrlName, nWidth, nHeight ):
        self.setControlModelProperty( cCtrlName, "Width", nWidth )
        self.setControlModelProperty( cCtrlName, "Height", nHeight )
    def setWidth( self, cCtrlName, nWidth ):
        self.setControlModelProperty( cCtrlName, "Width", nWidth )
    def setHeight( self, cCtrlName, nHeight ):
        self.setControlModelProperty( cCtrlName, "Height", nHeight )
    def getWidth( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Width" )
    def getHeight( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Height" )

    def setTabIndex( self, cCtrlName, nWidth, nTabIndex ):
        self.setControlModelProperty( cCtrlName, "TabIndex", nTabIndex )
    def getTabIndex( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "TabIndex" )

    def setStep( self, cCtrlName, nWidth, nStep ):
        self.setControlModelProperty( cCtrlName, "Step", nStep )
    def getStep( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Step" )

    def setTag( self, cCtrlName, nWidth, cTag ):
        self.setControlModelProperty( cCtrlName, "Tag", cTag )
    def getTag( self, cCtrlName ):
        return self.getControlModelProperty( cCtrlName, "Tag" )

    def setEchoChar(self, cCtrlName , cVal):
        self.setControlModelProperty(cCtrlName, "EchoChar", cVal)
    def getEchoChar(self, cCtrlName):
        return self.setControlModelProperty(cCtrlName, "EchoChar")

    #--------------------------------------------------
    #   Add listeners to controls.
    #--------------------------------------------------

    # This applies to...
    #   UnoControlButton
    def addActionListenerProc( self, cCtrlName, actionListenerProc ):
        """Create an com.sun.star.awt.XActionListener object and add it to a control.
        A listener object is created which will call the python procedure actionListenerProc.
        The actionListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlButton
        """
        oControl = self.getControl( cCtrlName )
        oActionListener = ActionListenerProcAdapter( actionListenerProc )
        oControl.addActionListener( oActionListener )

    # This applies to...
    #   UnoControlCheckBox
    def addItemListenerProc( self, cCtrlName, itemListenerProc ):
        """Create an com.sun.star.awt.XItemListener object and add it to a control.
        A listener object is created which will call the python procedure itemListenerProc.
        The itemListenerProc can be either a method or a global procedure.
        The following controls support XActionListener:
            UnoControlCheckBox
        """
        oControl = self.getControl( cCtrlName )
        oActionListener = ItemListenerProcAdapter( itemListenerProc )
        oControl.addItemListener( oActionListener )

    #--------------------------------------------------
    #   Display the modal dialog.
    #--------------------------------------------------

    def doModalDialog( self, sObjName,sValue):
        """Display the dialog as a modal dialog."""
        self.oDialogControl.setVisible( True )
        if not sValue==None:
            self.selectListBoxItem( sObjName, sValue, True )
        self.oDialogControl.execute()

    def endExecute( self ):
        """Call this from within one of the listeners to end the modal dialog.
        For instance, the listener on your OK or Cancel button would call this to end the dialog.
        """
        self.oDialogControl.endExecute()

#------------------------------------------------------------
#   Uno ServiceManager access
#   A different version of this routine and global variable
#    is needed for code running inside a component.
#------------------------------------------------------------


# The ServiceManager of the running OOo.
# It is cached in a global variable.
goServiceManager = False
pythonloader.DEBUG = 0
def getServiceManager( cHost="localhost", cPort="2002" ):
    """Get the ServiceManager from the running OpenOffice.org.
        Then retain it in the global variable goServiceManager for future use.
        This is similar to the GetProcessServiceManager() in OOo Basic.
    """
    global goServiceManager
    global pythonloader
    if not goServiceManager:
        # Get the uno component context from the PyUNO runtime
        oLocalContext = uno.getComponentContext()
        # Create the UnoUrlResolver on the Python side.

        goServiceManager=oLocalContext.ServiceManager

    return goServiceManager


#------------------------------------------------------------
#   Uno convenience functions
#   The stuff in this section is just to make
#    python progrmaming of OOo more like using OOo Basic.
#------------------------------------------------------------


# This is the same as ServiceManager.createInstance( ... )
def createUnoService( cClass ):
    """A handy way to create a global objects within the running OOo.
    Similar to the function of the same name in OOo Basic.
    """
    oServiceManager = getServiceManager()
    oObj = oServiceManager.createInstance( cClass )
    return oObj


# The StarDesktop object.  (global like in OOo Basic)
# It is cached in a global variable.
StarDesktop = None
def getDesktop():
    """An easy way to obtain the Desktop object from a running OOo.
    """
    global StarDesktop
    if StarDesktop == None:
        StarDesktop = createUnoService( "com.sun.star.frame.Desktop" )
    return StarDesktop
# preload the StarDesktop variable.
#getDesktop()


# The CoreReflection object.
# It is cached in a global variable.
goCoreReflection = False
def getCoreReflection():
    global goCoreReflection
    if not goCoreReflection:
        goCoreReflection = createUnoService( "com.sun.star.reflection.CoreReflection" )
    return goCoreReflection


def createUnoStruct( cTypeName ):
    """Create a UNO struct and return it.
    Similar to the function of the same name in OOo Basic.
    """
    oCoreReflection = getCoreReflection()
    # Get the IDL class for the type name
    oXIdlClass = oCoreReflection.forName( cTypeName )
    # Create the struct.
    oReturnValue, oStruct = oXIdlClass.createObject( None )
    return oStruct

#------------------------------------------------------------
#   API helpers
#------------------------------------------------------------

def hasUnoInterface( oObject, cInterfaceName ):
    """Similar to Basic's HasUnoInterfaces() function, but singular not plural."""

    # Get the Introspection service.
    oIntrospection = createUnoService( "com.sun.star.beans.Introspection" )

    # Now inspect the object to learn about it.
    oObjInfo = oIntrospection.inspect( oObject )

    # Obtain an array describing all methods of the object.
    oMethods = oObjInfo.getMethods( uno.getConstantByName( "com.sun.star.beans.MethodConcept.ALL" ) )
    # Now look at every method.
    for oMethod in oMethods:
        # Check the method's interface to see if
        #  these aren't the droids you're looking for.
        cMethodInterfaceName = oMethod.getDeclaringClass().getName()
        if cMethodInterfaceName == cInterfaceName:
            return True
    return False

def hasUnoInterfaces( oObject, *cInterfaces ):
    """Similar to the function of the same name in OOo Basic."""
    for cInterface in cInterfaces:
        if not hasUnoInterface( oObject, cInterface ):
            return False
    return True

#------------------------------------------------------------
#   High level general purpose functions
#------------------------------------------------------------


def makePropertyValue( cName=None, uValue=None, nHandle=None, nState=None ):
    """Create a com.sun.star.beans.PropertyValue struct and return it.
    """
    oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )

    if cName != None:
        oPropertyValue.Name = cName
    if uValue != None:
        oPropertyValue.Value = uValue
    if nHandle != None:
        oPropertyValue.Handle = nHandle
    if nState != None:
        oPropertyValue.State = nState

    return oPropertyValue


def makePoint( nX, nY ):
    """Create a com.sun.star.awt.Point struct."""
    oPoint = createUnoStruct( "com.sun.star.awt.Point" )
    oPoint.X = nX
    oPoint.Y = nY
    return oPoint


def makeSize( nWidth, nHeight ):
    """Create a com.sun.star.awt.Size struct."""
    oSize = createUnoStruct( "com.sun.star.awt.Size" )
    oSize.Width = nWidth
    oSize.Height = nHeight
    return oSize


def makeRectangle( nX, nY, nWidth, nHeight ):
    """Create a com.sun.star.awt.Rectangle struct."""
    oRect = createUnoStruct( "com.sun.star.awt.Rectangle" )
    oRect.X = nX
    oRect.Y = nY
    oRect.Width = nWidth
    oRect.Height = nHeight
    return oRect


def Array( *args ):
    """This is just sugar coating so that code from OOoBasic which
    contains the Array() function can work perfectly in python."""
    tArray = ()
    for arg in args:
        tArray += (arg,)
    return tArray


def loadComponentFromURL( cUrl, tProperties=() ):
    """Open or Create a document from it's URL.
    New documents are created from URL's such as:
        private:factory/sdraw
        private:factory/swriter
        private:factory/scalc
        private:factory/simpress
    """
    StarDesktop = getDesktop()
    oDocument = StarDesktop.loadComponentFromURL( cUrl, "_blank", 0, tProperties )
    return oDocument


#------------------------------------------------------------
#   Styles
#------------------------------------------------------------


def defineStyle( oDrawDoc, cStyleFamily, cStyleName, cParentStyleName=None ):
    """Add a new style to the style catalog if it is not already present.
    This returns the style object so that you can alter its properties.
    """

    oStyleFamily = oDrawDoc.getStyleFamilies().getByName( cStyleFamily )

    # Does the style already exist?
    if oStyleFamily.hasByName( cStyleName ):
        # then get it so we can return it.
        oStyle = oStyleFamily.getByName( cStyleName )
    else:
        # Create new style object.
        oStyle = oDrawDoc.createInstance( "com.sun.star.style.Style" )

        # Set its parent style
        if cParentStyleName != None:
            oStyle.setParentStyle( cParentStyleName )

        # Add the new style to the style family.
        oStyleFamily.insertByName( cStyleName, oStyle )

    return oStyle


def getStyle( oDrawDoc, cStyleFamily, cStyleName ):
    """Lookup and return a style from the document.
    """
    return oDrawDoc.getStyleFamilies().getByName( cStyleFamily ).getByName( cStyleName )

#------------------------------------------------------------
#   General Utility functions
#------------------------------------------------------------


def convertToURL( cPathname ):
    """Convert a Windows or Linux pathname into an OOo URL."""
    if len( cPathname ) > 1:
        if cPathname[1:2] == ":":
            cPathname = "/" + cPathname[0] + "|" + cPathname[2:]
    cPathname = cPathname.replace( "\\", "/" )
    cPathname = "file://" + cPathname
    return cPathname

