*############################################################
* Color conversion utilities
* --------------------------
* Visual FoxPro and OpenOffice.org do not represent
* color values the same way.
* Conversion routines in both directions are provided
* to keep things easy.
* In addition, extremely convenient HSV routines are provided.
* See LibGraphics for discussion of HSV color model.
*############################################################
* Similar to VFP's built in RGB() function.
* Pass in R,G,B values and out comes an OpenOffice.org color value.
* Note that this is DIFFERENT from how VFP constructs color values.
FUNCTION OOoRGB( nRed, nGreen, nBlue )
RETURN BITOR( BITOR( BITLSHIFT( nRed, 16 ), BITLSHIFT( nGreen, 8 ) ), nBlue )
ENDFUNC
* Translate between a Visual FoxPro color and an OpenOffice.org color
* by using a simple formula.
* Pass in a VFP color, out comes an OOo color. And vice versa.
FUNCTION OOoColor( nColor )
LOCAL nTranslatedColor
nTranslatedColor = (INT( nColor / 65536 )) + (INT( INT( nColor / 256 ) % 256 ) * 256) + (INT( nColor % 256 ) * 65536)
RETURN nTranslatedColor
ENDFUNC
* Extract the Red component from an OpenOffice.org color.
* SImilar to the RGBRed() function in LibGraphics.
FUNCTION OOoRed( nOOoColor )
* RETURN INT( nOOoColor / 65536 )
RETURN BITRSHIFT( BITAND( nOOoColor, 0x00FF0000 ), 16 )
ENDFUNC
* Extract the Green component from an OpenOffice.org color.
* SImilar to the RGBGreen() function in LibGraphics.
FUNCTION OOoGreen( nOOoColor )
* RETURN INT( INT( nOOoColor / 256 ) % 256 )
RETURN BITRSHIFT( BITAND( nOOoColor, 0x0000FF00 ), 8 )
ENDFUNC
* Extract the Blue component from an OpenOffice.org color.
* SImilar to the RGBBlue() function in LibGraphics.
FUNCTION OOoBlue( nOOoColor )
* RETURN INT( nOOoColor % 256 )
RETURN BITAND( nOOoColor, 0x000000FF )
ENDFUNC
* Convenient way to construct an OOo color from HSV components.
* See LibGraphics for information about HSV.
* Note nHue is a number from 0.0 to 1.0.
FUNCTION OOoHSV( nHue, nSaturation, nBrightness )
LOCAL nColor
nColor = MakeHSVColor( nHue * 6.0, nSaturation, nBrightness )
nColor = OOoColor( nColor )
RETURN nColor
ENDFUNC
* Convenient way to extract the Hue component from an OOo color.
* See LibGraphics for information about HSV.
* Note nHue is a number from 0.0 to 1.0.
FUNCTION OOoHue( nOOoColor )
RETURN HSVHue( OOoColor( nOOoColor ) ) / 6.0
ENDFUNC
* Convenient way to extract the Saturation component from an OOo color.
* See LibGraphics for information about HSV.
FUNCTION OOoSaturation( nOOoColor )
RETURN HSVSaturation( OOoColor( nOOoColor ) )
ENDFUNC
* Convenient way to extract the Brightness component from an OOo color.
* See LibGraphics for information about HSV.
FUNCTION OOoBrightness( nOOoColor )
RETURN HSVValue( OOoColor( nOOoColor ) )
ENDFUNC
*############################################################
PROCEDURE DoNothing__ErrorHandler( pnError, pcErrMessage, pnLineNo, pcProgramFileSys16, pcProgram, pcErrorParamSys2018 )
ENDPROC
* Return the OpenOffice.org service manager object.
* Cache it in a global variable.
* Create it if not already cached.
FUNCTION OOoGetServiceManager()
IF (TYPE([goOOoServiceManager])!="O") OR ISNULL( goOOoServiceManager )
PUBLIC goOOoServiceManager
goOOoServiceManager = CREATEOBJECT( "com.sun.star.ServiceManager" )
ENDIF
RETURN goOOoServiceManager
ENDFUNC
* Sugar coated routine to ask the service manager to
* create you an instance of some other OpenOffice.org UNO object.
FUNCTION OOoServiceManager_CreateInstance( cServiceName )
LOCAL oServiceManager
oServiceManager = OOoGetServiceManager()
LOCAL oInstance
oInstance = .NULL.
LOCAL cOldErrHandler
cOldErrHandler = ON( "ERROR" )
ON ERROR = DoNothing__ErrorHandler( ERROR(), MESSAGE(), LINENO(), SYS(16), PROGRAM(), SYS(2018) )
oInstance = oServiceManager.createInstance( cServiceName )
ON ERROR &cOldErrHandler
IF ISNULL( oInstance )
=__OOoReleaseCachedVars()
oServiceManager = OOoGetServiceManager()
oInstance = oServiceManager.createInstance( cServiceName )
ENDIF
RETURN oInstance
ENDFUNC
* Sugar coated routine to create any UNO struct.
* Use the Bridge_GetStruct() feature of the OLE-UNO bridge.
FUNCTION OOoCreateStruct( cTypeName )
LOCAL oServiceManager
oServiceManager = OOoGetServiceManager()
LOCAL oStruct
oStruct = .NULL.
LOCAL cOldErrHandler
cOldErrHandler = ON( "ERROR" )
ON ERROR = DoNothing__ErrorHandler( ERROR(), MESSAGE(), LINENO(), SYS(16), PROGRAM(), SYS(2018) )
oStruct = oServiceManager.Bridge_GetStruct( cTypeName )
ON ERROR &cOldErrHandler
IF ISNULL( oStruct )
=__OOoReleaseCachedVars()
oServiceManager = OOoGetServiceManager()
oStruct = oServiceManager.Bridge_GetStruct( cTypeName )
ENDIF
RETURN oStruct
ENDFUNC
* Return the OpenOffice.org desktop object.
* Cache it in a global variable.
* Create it if not already cached.
FUNCTION OOoGetDesktop()
IF (TYPE([goOOoDesktop])!="O") OR ISNULL( goOOoDesktop )
PUBLIC goOOoDesktop
goOOoDesktop = OOoServiceManager_CreateInstance( "com.sun.star.frame.Desktop" )
COMARRAY( goOOoDesktop, 10 )
ENDIF
RETURN goOOoDesktop
ENDFUNC
*!* * Return an instance of com.sun.star.reflection.CoreReflection.
*!* * Cache it in a global variable.
*!* * Create it if not already cached.
*!* FUNCTION OOoGetCoreReflection()
*!* IF (TYPE([goOOoCoreReflection])!="O") OR ISNULL( goOOoCoreReflection )
*!* PUBLIC goOOoCoreReflection
*!* goOOoCoreReflection = OOoServiceManager_CreateInstance( "com.sun.star.reflection.CoreReflection" )
*!* COMARRAY( goOOoCoreReflection, 10 )
*!* ENDIF
*!* RETURN goOOoCoreReflection
*!* ENDFUNC
*!* * Create a UNO struct object and return it.
*!* * This routine is now obsolete.
*!* * See a superior implementation of this routine elsewhere
*!* * which uses the Bridge_GetStruct() feature of the OLE-UNO bridge.
*!* FUNCTION OOoCreateStruct( cTypeName )
*!* * Ask service manager for a CoreReflection object.
*!* LOCAL oCoreReflection
*!* oCoreReflection = OOoGetCoreReflection()
*!*
*!* * Get the IDL Class for the type name.
*!* LOCAL oXIdlClass
*!* oXIdlClass = .NULL.
*!* LOCAL cOldErrHandler
*!* cOldErrHandler = ON( "ERROR" )
*!* ON ERROR = DoNothing__ErrorHandler( ERROR(), MESSAGE(), LINENO(), SYS(16), PROGRAM(), SYS(2018) )
*!* oXIdlClass = oCoreReflection.forName( cTypeName )
*!* ON ERROR &cOldErrHandler
*!*
*!* IF ISNULL( oXIdlClass )
*!* =__OOoReleaseCachedVars()
*!* oCoreReflection = OOoGetCoreReflection()
*!* oXIdlClass = oCoreReflection.forName( cTypeName )
*!* ENDIF
*!*
*!* * Create a variable to hold the created Struct.
*!* * Assign it some initial value.
*!* LOCAL oStruct
*!* oStruct = CREATEOBJECT( "relation" ) && assign some kind of object initially
*!* * Ask the class definition to create an instance.
*!* oXIdlClass.CreateObject( [at] oStruct )
*!*
*!* RETURN oStruct
*!* ENDFUNC
* Create a com.sun.star.beans.PropertyValue struct and return it.
FUNCTION OOoPropertyValue( cName, uValue, nHandle, nState )
LOCAL oPropertyValue
oPropertyValue = OOoCreateStruct( "com.sun.star.beans.PropertyValue" )
oPropertyValue.Name = cName
oPropertyValue.Value = uValue
IF TYPE([nHandle])="N"
oPropertyValue.Handle = nHandle
ENDIF
IF TYPE([nState])="N"
oPropertyValue.State = nState
ENDIF
RETURN oPropertyValue
ENDFUNC
* Open or Create a document from it's URL.
* New documents are created by URL's such as:
* private:factory/sdraw
* private:factory/swriter
* private:factory/scalc
* private:factory/simpress
FUNCTION OOoOpenURL( cURL )
* LOCAL oPropertyValue
* oPropertyValue = OOoCreateStruct( "com.sun.star.beans.PropertyValue" )
* LOCAL ARRAY aNoArgs[1]
* aNoArgs[1] = oPropertyValue
* aNoArgs[1].Name = "ReadOnly"
* aNoArgs[1].Value = .F.
* These two lines replace the alternate version above,
* which are left commented for the insight they provide.
LOCAL ARRAY aNoArgs[1]
aNoArgs[1] = OOoPropertyValue( "Hidden", .F. )
LOCAL oDesktop
oDesktop = OOoGetDesktop()
LOCAL oDoc
oDoc = oDesktop.LoadComponentFromUrl( cURL, "_blank", 0, [at] aNoargs )
* Make sure that arrays passed to this document are passed zero based.
COMARRAY( oDoc, 10 )
RETURN oDoc
ENDFUNC
PROCEDURE __OOoReleaseCachedVars()
RELEASE goOOoServiceManager, goOOoDesktop, goOOoCoreReflection
ENDPROC