Re: Open a file with standard application from dialog

Fernand Vanrie <[email protected]> Thu, 02 Dec 2010 10:31:05 +0100
Newsgroups gmane.comp.openoffice.devel.api
Message-ID <[email protected]>
  Volker

please find the code here:

'-----FILEPICKER 
START--------------------------------------------------------------

sub runPicker

UseSystemFileDialogs(false)
'sPath = FileNameoutofPath(ThisComponent.Url , "/")



If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
       GlobalScope.BasicLibraries.LoadLibrary("Tools")
    End If

Openfile()

  if not isempty(oPreviewWindow) then
  oPreviewWindow.reMovePaintListener( oPaintListener )
  oPreviewWindow.Dispose
  end if
end sub

function OpenFile() as String

   OpenFile = ""
   boInitialized = false

   oPickerListener = CreateUnoListener("pickerListener_", 
"com.sun.star.ui.dialogs.XFilePickerListener")
   oFP = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
   With oFP


     .Initialize( 
Array(com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE 
) )

     '.appendFilter("BerichtFoto's" , "F" & right(year(now),2) & 
"*.jpg;" & "F" & right(year(now),2) & "*.jpeg")
     .appendFilter("Alle Files", "*.*" )
      if bOverschrijven = true then
     .setTitle( "welke file wil je OVERSCHRIJVEN ?" )
     end if
     .setTitle( "Berichten Foto's Opladen" )

     .setDisplayDirectory( ConvertToURL( sPath ) )
     
.enableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
False)
     
.enableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW, 
False)
     .setlabel(109,"FileInfo")
      dim aPickAray(3) as string
    aPickAray(0) = TargetResolution & " DPI"
    aPickAray(1) = "OpenOffice File"
    aPickAray(2) = "No Preview posible !!!"
    oFP.setValue(109, 2 , aPickAray())

     .addFilePickerListener( oPickerLIstener ) ' MOET LAATSE HANDELING 
ZIJN anders begint de listener te werken als er iets aan de dialoog 
wijzigt !!!!
     If .execute() Then OpenFile = .Files(0)

              sGraphicURL = Openfile
              sURLtoKIll = openfile

              if len(sGraphicURL) > 0 then
              if bBfoto <> true then
              sPIFotonaam = left(filenameoutofpath(sGraphicURL),15) & "A"
              else
              returncode = msgbox ("we laden nu een B-foto ?", 4 + 32)
           if returncode = 6 then
              sPIFotonaam = left(filenameoutofpath(sGraphicURL),15) & "B"
              else
              end
              end if
              end if
              else
              beep
              msgbox ("kies een bruikbare file !!!!",48)
              fot_ond.Cancelodialog
              end
           endif






           sPath = oFP.getDisplayDirectory

                .reMoveFilePickerListener(oPickerLIstener)
     .Dispose()
     End With
end function


sub PreviewWindowLoadPicFile(sUrl as String)
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
       GlobalScope.BasicLibraries.LoadLibrary("Tools")
    End If
dim x
     x =  
GetRegistryKeyContent("org.openoffice.Setup/Product").ooSetupVersionAboutBox
    if x < "3.0" then
         oUserDevinfo = thiscomponent.DocumentInfo()
         else oUserDevinfo = 
thiscomponent.DocumentProperties.UserdefinedProperties
         end if


   oPreviewGraph  = getGraphFromUrl(sUrl)


if isnull(oPreviewGraph) then
  beep
  oPreviewGraph  = 
getGraphFromUrl(converttoURL("T:\Template\Artikels\STYLES\verkeerdbeeld.gif"))
endif
if oPreviewgraph.gettype = 1 then
    sFileType = "Raster" else
    sFileType = "Vector"
end if



if sFileType = "Raster"  then

          if  bOpladen = true then
              OO_MMw = 
(oPreviewGraph.sizepixel.width/TargetResolution)*25.40*100
              OO_MMh = 
(oPreviewGraph.sizepixel.height/TargetResolution)*25.40*100
         end if
end if

if sFiletype = "Raster" then

    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
0, True)
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW, 
0, True)
    oFP.enableControl(109, False)
    oFP.setValue(109,4,0)
    oFP.setValue(109, 1 , "Max   " & int( OO_MMw/100/460) & " kol x " 
&int( OO_MMh/100) & " mm Hoog"
    oFP.setValue(109,5,0)
    oPreviewWindow = InstallNewPreviewWindow(oFP.Window, 
com.sun.star.awt.WindowAttribute.BORDER)
   oPreviewWindow.addPaintListener( oPaintListener )
   lPreviewStatus = PREVIEW_PICFILE

    else
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
0, True)
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW, 
0, True)
    oFP.enableControl(109, False)
    oFP.setValue(109,4,0)
    oFP.setValue(109, 1 , "VECTORFile --  NU " & int( 
oPreviewGraph.size100thMM.width/100/49) & " kol x " & int( 
oPreviewGraph.size100thMM.height/100) & " mm Hoog"
    oFP.setValue(109,5,0)
    oPreviewWindow = InstallNewPreviewWindow(oFP.Window, 
com.sun.star.awt.WindowAttribute.BORDER)
    oPreviewWindow.addPaintListener( oPaintListener )
    lPreviewStatus = PREVIEW_PICFILE
    end if


end sub


Sub PreviewWindowLoadOOFile(sUrl as String)
    oPreviewWindow = InstallNewPreviewWindow(oFP.Window, 0)
    
oFP.setLabel(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
""
    
oFP.setLabel(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW,  
""
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
0, False)
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW, 
0, False)
    oFP.setValue(109,4,0)
    oFP.setValue(109,1,"OpenOffice file")
    oFP.setValue(109,5,0)
   oFrame = createUnoService("com.sun.star.frame.Frame")
   oFrame.initialize(oPreviewWindow)
   Dim aProps(0) As New com.sun.star.beans.PropertyValue
   aProps(0).Name  = "Preview"
   aProps(0).Value = true
'  oFrame.loadComponentFromURL(sUrl, "", 0, aProps())
   oPreviewDoc = oFrame.loadComponentFromURL(sUrl, "", 0, aProps()) ' 
hier laden  we ook de inhoud in het frame

   lPreviewStatus = PREVIEW_OOFILE
   if IsNull(oPreviewDoc) then
     oPreviewWindow.dispose()
     lPreviewStatus = PREVIEW_NONE
     endif
end sub

sub PreviewWindowCleanUp()
if lPreviewStatus = PREVIEW_NONE then exit sub

    if lPreviewStatus = PREVIEW_PICFILE then
    oPreviewGraph = nothing
    oPreviewWindow = InstallNewPreviewWindow(oFP.Window, 0)
    ' oPreviewWindow.reMovePaintListener( oPaintListener )
   ' oPreviewWindow.dispose()
   endif
if lPreviewStatus = PREVIEW_OOFILE then
     oPreviewDoc.close(true)
   '   oPreviewWindow.dispose()
   endif
lPreviewStatus = PREVIEW_NONE
' oPreviewWindow.dispose()

   end sub

function InstallNewPreviewWindow(oWin as Object, lWinAttrs as Long) as 
Object
' Create a window descriptor and set up its properties
   Dim aDescriptor As New com.sun.star.awt.WindowDescriptor
   aDescriptor.Type = com.sun.star.awt.WindowClass.SIMPLE
   aDescriptor.Parent = oWin
   aDescriptor.Bounds = oWin.Windows(lLastWindowAtStart).PosSize
   aDescriptor.WindowAttributes = com.sun.star.awt.WindowAttribute.SHOW 
or lWinAttrs
   oToolkit = createUnoService("com.sun.star.awt.Toolkit")
   InstallNewPreviewWindow = oToolkit.createWindow(aDescriptor)
end function



Sub PaintList01_windowPaint(oEvt)
   if IsNull(oPreviewGraph) then Exit Sub

   If oEvt.count > 0 Then Exit Sub

   oWin = oEvt.Source

   oPS15  = oWin.PosSize
   oPreviewGraphFP = oWin.createGraphics

   lWhite = RGB(212,208,200)
   oPreviewGraphFP.setFillColor(lWhite)
   oPreviewGraphFP.setLineColor(lWhite)

   oPreviewGraphFP.drawRect(0,0,oPS15.Width,oPS15.Height)

' calculate the position of the pic


   if oPreviewGraph.sizePixel.width = 0 then
     dRatioPreviewGraph = 1
     else
     dRatioPreviewGraph = oPreviewGraph.sizePixel.height / 
oPreviewGraph.sizePixel.width
     endif
   dRatioWin   = oPS15.Height / oPS15.Width
'  Dim sRectan as new com.sun.star.awt.Rectangle
    sRectan = oPS15
   sRectan.X=0
   sRectan.Y=0
   if dRatioPreviewGraph>dRatioWin then
     sRectan.Width  = oPS15.Height/dRatioPreviewGraph
     sRectan.X = (oPS15.Width-sRectan.Width)/2
     else
     sRectan.Height = oPS15.Width*dRatioPreviewGraph
     sRectan.Y = (oPS15.Height-sRectan.Height)/2
     endif

   oRenderer = createUnoService("com.sun.star.graphic.GraphicRendererVCL")
   oRenderer.Device = oPreviewGraphFP.Device
   oRenderer.DestinationRect = sRectan
   oRenderer.render(oPreviewGraph)
end Sub

Sub PaintList01_disposing(oEvt As Object)
End Sub

sub WindList01_windowResized( oEvt as com.sun.star.awt.WindowEvent )
   oWin = oFP.Window
   if UBound(oWin.Windows)<= lLastWindowAtStart then exit sub
   aR = oWin.Windows(lLastWindowAtStart).PosSize
   oWin.Windows(lLastWindowAtStart+1).setPosSize(aR.X, aR.Y, aR.Width, 
aR.Height, com.sun.star.awt.PosSize.POSSIZE)
end sub

sub WindList01_windowMoved( e as com.sun.star.awt.WindowEvent )
end sub

sub WindList01_windowShown( e as com.sun.star.lang.EventObject )
end sub

sub WindList01_windowHidden( e as com.sun.star.lang.EventObject )
end sub

sub WindList01_disposing( e as com.sun.star.lang.EventObject )
end sub


sub pickerListener_fileSelectionChanged(oEvent as Object)
   call PreviewWindowCleanUp()
   oUCB = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
   sOriginalURL = oFP.Files(0)
   if not oUCB.Exists(sOriginalURL) then
   oFP.setImage(1,0)
    exit sub
   endif
   if oUCB.IsFolder(sOriginalURL) then
    oFP.setImage(1,0)
  exit sub
   endif

   lType = getFileType(sOriginalURL)
   if lType = PREVIEW_PICFILE then call 
PreviewWindowLoadPicFile(sOriginalURL)
   if lType = PREVIEW_OOFILE then call 
PreviewWindowLoadOOFile(sOriginalURL)
   if lType = PREVIEW_NONE then
     oPreviewWindow = InstallNewPreviewWindow(oFP.Window, 0)
    
oFP.setLabel(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
""
    
oFP.setLabel(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW,  
""
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_LINK, 
0, False)
    
oFP.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PREVIEW, 
0, False)
    oFP.setValue(109,4,0)
    oFP.setValue(109,1,"Geen Preview Mogelijk !!")
    oFP.setValue(109,5,0)
    end if
end Sub

sub pickerListener_disposing(oEvent)
end sub

sub pickerListener_directoryChanged(oEvent)
end Sub

sub pickerListener_helpRequested(oEvent)
end Sub

sub pickerListener_controlStateChanged(oEvent as Object)
end Sub

sub pickerListener_dialogSizeChanged(oEvent)
   if boInitialized then exit sub   ' this routine is only used for 
general initializations
   boInitialized = true
   lLastWindowAtStart = UBound(oFP.Window.Windows)
   lPreviewStatus = PREVIEW_NONE
   oWindowListener = CreateUnoListener("WindList01_", 
"com.sun.star.awt.XWindowListener")
   oFP.Window.addWindowListener( oWindowListener )
   oPaintListener  = CreateUnoListener("PaintList01_", 
"com.sun.star.awt.XPaintListener")
end Sub

function getGraphFromUrl(sFileURL as String) as Object
   oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
   Dim oPropsIN(0)as new com.sun.star.beans.PropertyValue
   oPropsIN(0).Name  = "URL"
   oPropsIN(0).Value = sFileURL
   getGraphFromUrl = oProvider.queryGraphic(oPropsIN())
end function

' some very simple file type recognition. You may add all file types, oo 
understands ...
function getFileType(sUrl as String) as long
   getFileType = PREVIEW_NONE
   asUrl = Split(sUrl,".")
   lUBound = UBound(asUrl)
   if lUBound <=0 then exit function
   sExt = LCase(asUrl(lUBound))
   select case sExt
     case "bmp", "gif", "jpg", "png" , "eps" , "tiff" , 
"tif"                 'not necessarily complete
       getFileType = PREVIEW_PICFILE
     case "ods", "odt", "odp", "ppt", "sxc", "sxw", "txt" , "htm" , 
"html", "pdf"   'not necessarily complete
       getFileType = PREVIEW_OOFILE
     end select
end function

'---------------------------------------------------------------------
Sub UseSystemFileDialogs(Hoe as boolean)
dim aArgs(0) as  Object
dim aPropValue as new  com.sun.star.beans.PropertyValue
xconfig = 
createunoservice("com.sun.star.configuration.ConfigurationProvider")
aPropValue.Name = "nodepath"
aPropValue.Value = "org.openoffice.Office.Common/Misc"
aArgs(0) = aPropValue
xAccess = 
xconfig.createinstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess",aArgs())
aIsSystemDialog = 
xAccess.getHierarchicalPropertyValue("UseSystemFileDialog")
xAccess.replaceByName("UseSystemFileDialog", Hoe)
xAccess.commitChanges
End Sub





'------FILEPICKER 
END------------------------------------------------------------------