Difference between revisions of "Danny.OOo.DialogLib.py"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Added code from oooforum.)
 
(Adding categories)
 
Line 566: Line 566:
  
 
</source>
 
</source>
 +
 +
[[Category:Extensions]]
 +
[[Category:Python]]
 +
[[Category:Uno]]

Latest revision as of 19:59, 17 February 2015

A 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.

This module depends upon Danny.OOo.Listeners.ListenerProcAdapters.py found elsewhere in this thread.


#********************************************************************** 
# 
#   Danny.OOo.DialogLib.py 
# 
#   A module to easily work with OpenOffice.org. 
# 
#********************************************************************** 
#   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 
# 
#********************************************************************** 
#   If you make changes, please append to the change log below. 
# 
#   Change Log 
#   Danny Brewer         Revised 2004-06-05-01 
# 
#********************************************************************** 
 
 
 
# OOo's libraries 
import uno 
import unohelper 
 
# Danny's libraries 
from Danny.OOo.OOoLib import createUnoService, createUnoStruct 
from Danny.OOo.Listeners.ListenerProcAdapters import * 
#from Danny.OOo.Listeners.TopWindowListener import TopWindowListener 
 
 
 
# 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, 
                         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.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, 
                         cLabel=cLabel, 
                         nTabIndex=nTabIndex ) 
        if itemListenerProc != None: 
            self.addItemListenerProc( cCtrlName, itemListenerProc ) 
 
    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, 
                         cLabel=cLabel ) 
 
    #-------------------------------------------------- 
    #   Add Controls to dialog 
    #-------------------------------------------------- 
 
    def addControl( self, cCtrlServiceName, 
                        cCtrlName, nPositionX, nPositionY, nWidth, nHeight, 
                        cLabel=None, 
                        nTabIndex=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 cLabel != None: 
            oControlModel.Label = cLabel 
 
        if nTabIndex != None: 
            oControlModel.TabIndex = nTabIndex 
 
    #-------------------------------------------------- 
    #   Access controls and control models 
    #-------------------------------------------------- 
 
    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 
 
 
    #-------------------------------------------------- 
    #   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" ) 
 
 
    #-------------------------------------------------- 
    #   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 ): 
        """Display the dialog as a modal dialog.""" 
        self.oDialogControl.setVisible( 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() 
 
#-------------------------------------------------- 
# Example of Dialog box built by subclassing the DBModalDialog class. 
 
class Test1Dialog( DBModalDialog ): 
    def __init__( self ): 
        DBModalDialog.__init__( self ) 
        self.setDialogPosition( 60, 50 ) 
        self.setDialogSize( 150, 80 ) 
        self.setDialogTitle( "My Test1 Dialog" ) 
        self.addButton( "btnOK", -10, -10, 30, 14, "OK", 
                        actionListenerProc = self.btnOK_clicked ) 
        self.addButton( "btnCancel", -50, -10, 30, 14, "Cancel", 
                        actionListenerProc = self.btnCancel_clicked ) 
        self.nOkClicks = 0 
        self.nCancelClicks = 0 
 
    # Called when the OK button is clicked. 
    def btnOK_clicked( self, oActionEvent ): 
        self.nOkClicks += 1 
 
    # Called when the Cancel button is clicked. 
    def btnCancel_clicked( self, oActionEvent ): 
        self.nCancelClicks += 1 
 
def Test1(): 
    oTestDialog =  Test1Dialog() 
 
    # Display dialog box. 
    # This does not return until the dialog box is dismissed. 
    oTestDialog.doModalDialog() 
 
    # Print out the global button click counters. 
    print oTestDialog.nCancelClicks, oTestDialog.nOkClicks 
 
#-------------------------------------------------- 
# Example of modal creating a dialog box without subclassing DBModalDialog. 
 
# Global vars to keep track of button clicks. 
nOkClicks = 0 
nCancelClicks = 0 
# Global procs, called as event listeners on button clicks. 
def OKClicked( oActionEvent ): 
    global nOkClicks 
    nOkClicks += 1 
def CancelClicked( oActionEvent ): 
    global nCancelClicks 
    nCancelClicks += 1 
 
# Create a modal dialog box without subclassing DBModalDialog. 
def Test2(): 
    oTestDialog = DBModalDialog( 60, 50, 150, 80, "My Test2 Dialog" ) 
    oTestDialog.addButton( "btnOK", -10, -10, 30, 14, "OK" ) 
    oTestDialog.addButton( "btnCancel", -50, -10, 30, 14, "Cancel" ) 
    oTestDialog.addFixedText( "lbl01", 10, 10, 30, 14, "Test1" ) 
 
    # Install global event listener procs. 
    # Use the addActionListenerProc() routine instead of supplying the event 
    #  listener to the addButton() as in first example. 
    oTestDialog.addActionListenerProc( "btnOK", OKClicked ) 
    oTestDialog.addActionListenerProc( "btnCancel", CancelClicked ) 
 
    # Display dialog box. 
    # This does not return until the dialog box is dismissed. 
    oTestDialog.doModalDialog() 
 
    # Print out the global button click counters. 
    print nCancelClicks, nOkClicks 
 
#-------------------------------------------------- 
# Example of Dialog box built by subclassing the DBModalDialog class. 
 
class ModalDoggieKittyMonkeyDialog( DBModalDialog ): 
    def __init__( self ):        
        self.nNumDoggies = 0 
        self.nNumKitties = 0 
        self.nNumMonkeys = 0 
        self.bTails = False 
        self.bOkay = False 
 
        DBModalDialog.__init__( self, 60, 50, 200, 90, "Doggie/Kitty/Monkey Dialog" ) 
 
        # Add an OK and a Cancel button. 
        # Both buttons share an action listener named "self.btnOkOrCancel_clicked". 
        self.addButton( "btnOK", -10, -10, 50, 14, "OK", 
                        actionListenerProc = self.btnOkOrCancel_clicked ) 
        self.addButton( "btnCancel", -10 - 50 - 10, -10, 50, 14, "Cancel", 
                        actionListenerProc = self.btnOkOrCancel_clicked ) 
 
        # Add three buttons. 
        self.addButton( "btnDoggie", 5, 10, 50, 14, "Add Doggie", 
                        actionListenerProc = self.btnDoggie_Clicked ) 
        self.addButton( "btnKitty", 5, 30, 50, 14, "Add Kitty", 
                        actionListenerProc = self.btnKitty_Clicked ) 
        self.addButton( "btnMonkey", 5, 50, 50, 14, "Add Monkey", 
                        actionListenerProc = self.btnMonkey_Clicked ) 
 
        # Add a label to the dialog. 
        self.addFixedText( "lbl1", 60, 10, 100, 14, "Add some doggies, kitties, and monkeys." ) 
 
        # Add a checkbox to the dialog. 
        self.addCheckBox( "chkTails", 60, 30, 50, 14, "With tails", 
                          itemListenerProc = self.chkTails_changed ) 
 
    def btnOkOrCancel_clicked( self, oActionEvent ): 
        """Called when the OK or Cancel button is clicked.""" 
        if oActionEvent.Source.getModel().Name == "btnOK": 
            self.bOkay = True 
        self.endExecute() 
 
    def btnDoggie_Clicked( self, oActionEvent ): 
        """Called with the Doggie button is clicked.""" 
        self.nNumDoggies += 1 
 
    def btnKitty_Clicked( self, oActionEvent ): 
        """Called with the Kitty button is clicked.""" 
        self.nNumKitties += 1 
 
    def btnMonkey_Clicked( self, oActionEvent ): 
        """Called with the Monkey button is clicked.""" 
        self.nNumMonkeys += 1 
 
    def chkTails_changed( self, oItemEvent ): 
        """Called when the Tails checkbox is clicked.""" 
        self.bTails = self.getState( "chkTails" ) 
 
 
def Test3(): 
    oTestDialog = ModalDoggieKittyMonkeyDialog() 
    oTestDialog.doModalDialog() 
 
    # Print out the global button click counters. 
    print oTestDialog.nNumDoggies, oTestDialog.nNumKitties, oTestDialog.nNumMonkeys 
    print "okay:",oTestDialog.bOkay," tails:",oTestDialog.bTails, oTestDialog.getState( "chkTails" ) 
 
#>>> import Danny.OOo.DialogLib 
#>>> reload( Danny.OOo.DialogLib ); from Danny.OOo.DialogLib import *
Personal tools