OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Changing images in dialog interactively

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
hillerd
General User
General User


Joined: 02 Jul 2005
Posts: 22

PostPosted: Wed Mar 07, 2007 7:39 am    Post subject: Changing images in dialog interactively Reply with quote

The scope is to have a dialog with an "ImageControl". Its content shall be changed depending on some user action. The images of the control are fixed. Everything is to be provided in an extension.

The extension has the following content:
    META-INF
      manifest.xml
    images
      Image1.png
      Image2.png
    TestLib
      dialog.xlb
      Dialog1.xdl
      Module1.xba
      script.xlb
    ImageLocations.xcs
    ImageLocations.xcu


mainifest.xml lists the content of the extension:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest>
 <manifest:file-entry manifest:full-path="TestLib/" manifest:media-type="application/vnd.sun.star.basic-library"/>
 <manifest:file-entry manifest:full-path="ImageLocations.xcs" manifest:media-type="application/vnd.sun.star.configuration-schema"/>
 <manifest:file-entry manifest:full-path="ImageLocations.xcu" manifest:media-type="application/vnd.sun.star.configuration-data"/>
</manifest:manifest>


ImageLocations.xcs contains the schema of the xcu file:
Code:
<?xml version='1.0' encoding='UTF-8'?>
<oor:component-schema
  oor:name="ImageLocations"
  oor:package="name.HillerD.comp.TestLib"
  xmlns:oor="http://openoffice.org/2001/registry"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <templates/>
  <component>
    <prop oor:name="Image1" oor:type="xs:string"/>
    <prop oor:name="Image2" oor:type="xs:string"/>
  </component>   
</oor:component-schema>


and ImageLocations.xcu finally contains the relative positions of the images:
Code:
<?xml version='1.0' encoding='UTF-8'?>
<oor:component-data
  oor:name="ImageLocations"
  oor:package="name.HillerD.comp.TestLib"
  xmlns:oor="http://openoffice.org/2001/registry">
   <prop oor:name="Image1">
      <value>%origin%/images/Image1.png</value>
   </prop>
   <prop oor:name="Image2">
      <value>%origin%/images/Image2.png</value>
   </prop>
</oor:component-data>


Image1.png and Image2.png are two images that shall be shown in the dialog

dialog.xlb and script.xlb are created automatically by OOo when the BASIC code is exported as extension the first time.

The dialog Dialog1.xdl contains one image control and 2 buttons. Pressing button1 shall show Image1 in the image control, pressing button2 the Image2.

Module1.xba contains the BASIC code. All there is to do here is to look in the registry (.xcu file) to where the image is located and set the URL of the image control to that value. This is executed upon a click on the button:
Code:

OPTION EXPLICIT

REM *** this BASIC example shows how to change images in dialogs interactively ***
REM *** the images are part of the extension ***
Dim oDialog as object, odlgImage as object
Dim sImage(1) as string
Const sPrefix = "vnd.sun.star.expand:"

Dim oConfigProvider as object, oRegKey as object


Sub Main
' *** first thing to do is to get the real path of the images after the installation of the extension
' ****** get access to the configuration provider
  oConfigProvider = _
    CreateUnoService("com.sun.star.configuration.ConfigurationProvider")

' ****** define the node path, where the Image name is stored in the configuration file ImageLocations.xcu
  Dim aNodePath(0) As New com.sun.star.beans.PropertyValue
       aNodePath(0).Name = "nodepath"
     aNodePath(0).Value = "name.HillerD.comp.TestLib.ImageLocations"

' ****** obtain read-only access to that node
  oRegKey = oConfigProvider.createInstanceWithArguments( _
         "com.sun.star.configuration.ConfigurationAccess", aNodePath() )

' ****** retrieving the values
' ****** the content was "%origin%/images/Image1.png"
' ****** during the installation of the extension, %origin% was replaced
' ******    with "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/"
  sImage(0) = oRegKey.getByName("Image1")
  sImage(1) = oRegKey.getByName("Image2")
         
' ****** cut the "vnd.sun.star.expand:" protocol from the macrofied Url retrieved from the registry
  sImage(0) = mid(sImage(0), len(sPrefix)+1)
  sImage(1) = mid(sImage(1), len(sPrefix)+1)

' ****** now the "$UNO_USER_PACKAGES_CACHE" has to be replaced with the real location of the files on your system
' ****** this will be done by the MacroExpander
' ********* get the default context
  Dim oCtx as object
  oCtx = getProcessServiceManager().DefaultContext
 
' ********** get the macro expander from the default context
  Dim oMacroExpander as object
  oMacroExpander = oCtx.getValueByName("/singletons/com.sun.star.util.theMacroExpander") 
   
' ********** expand the macrofield expression, which gives us the real location of the files
  sImage(0) = oMacroExpander.ExpandMacros(sImage(0))
  sImage(1) = oMacroExpander.ExpandMacros(sImage(1))
   
' ****** At this point you should de-escape the resulting URL, but OOBasic does not offer a function for that.
' ****** Anyway this is a small problem if you avoid accented chars or spaces in filenames you use inside your package

' *** load and execute the dialog
  DialogLibraries.LoadLibrary("TestLib")      
  oDialog = CreateUnoDialog(DialogLibraries.TestLib.Dialog1)
  odlgImage = oDialog.GetControl("dlgImage")
  oDialog.execute()

' *** close dialog
  oDialog.dispose
End Sub

' *** the following subs are assigned to the event of the buttons
' *** each sub assigns the url to the Image1 or Image2 respectively, which changes the image in the dialog interactively
                                 ' dlgBtn1 pressed, show image 1
Sub dlgBtn1_pressed (oEvent as object)
  odlgImage.Model.ImageURL=sImage(0)
End Sub
                                 ' dlgBtn2 pressed, show image 2
Sub dlgBtn2_pressed (oEvent as object)
  odlgImage.Model.ImageURL=sImage(1)
End Sub


Many many thanks to
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group