OpenMultiFileRequester

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Haaaa. I done it again . Just edited instead of replying. Sorry James, just post your code again..


Edited by - fred on 05 June 2002 01:17:52
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Hi Fred,

Could we have one of these -- a file requester that returns a list of files (rather than just one)? I made an attempt to do it myself, but there are a few bugs :)

Code: Select all


  ...

; WORK IN PROGRESS!!! Has a couple of bugs. See if you can tell why the alternative
; filter in the demo fails (at the bottom of this source), because I can't!

  ...

;filter$ = "Text files|*.txt|Zip files|*.zip|All files|*.*"   ; Weird -- Zips don't get shown this way round,
                                                              ; plus it opens in source folder instead of default... :/

filter$ = "Zip files|*.zip|Text files|*.txt|All files|*.*"    ; This one works

  ...

Ok i'll try to explain my findings to you, it seems that the default dir thingie works like this:

First it checks the default dir(where your application is located) for files that matches the default extension, if no such files is found it automaticaly reverts to the 'My Documents' directory.. This explains why the second filter open there, you can verify this by placing a 'zip' file in the 'PureBasic\compiler' directory and when compiled and run it should behave like the first filter, with the difference that '*.zip' files is displayed instead of '*.txt' files.

Hope this helps.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

This feature is planned for 3.30... .

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.
Ok i'll try to explain my findings to you, it seems that the default dir thingie works like this:
Ah, yes, you're right, thanks... so it's not bugged after all :)

Fred: thanks -- here's my code again in case it's any use to you :)

Code: Select all


; -----------------------------------------------------------------------------
; OpenMultiFileRequester ()
; -----------------------------------------------------------------------------

; A function to allow the calling of an OpenFileRequester that returns multiple
; files...

; WORK IN PROGRESS!!! Has a couple of bugs. See if you can tell why the alternative
; filter in the demo fails (at the bottom of this source), because I can't!

; [url]mailto:punkrockprogrammer@hi-toro.com">punkrockprogrammer@hi-toro.com

; -----------------------------------------------------------------------------
; NewLine ()
; -----------------------------------------------------------------------------

; Just returns a (Windows) newline/linefeed/whatever...

Procedure.s NewLine ()
  ProcedureReturn Chr (13) + Chr (10)
EndProcedure

; -----------------------------------------------------------------------------
; OpenMultiFileRequester ()
; -----------------------------------------------------------------------------

; Returns a string containing the path of the selected files, followed by the
; names of the selected files. NOTE that all are separated by a "|" character!

; Example of returned string:

; "C:\My Documents|Pixies - Debaser.mp3|Pixies - Mr Grieves.mp3"

; That means the selected files were in "C:\My Documents", and the filenames
; were "Pixies - Debaser.mp3" and "Pixies - Mr Grieves.mp3"

; PARAMETERS...

; window        -- the parent window. Use 0 if no window exists.
; title$        -- the text that appears in the requester's title bar ("Eg. Select files...").
; dir$          -- folder to start requester in (use "" for system default, usually "My Documents").
; filter$       -- a string defining a filter which says which files will be displayed (see below).
; maxfiles      -- the routine has to pre-allocate memory for the filenames; it reserves the
;                  maximum filename length (#MAX_PATH) allowed by Windows, multiplied by this value.
;                  If you pass zero, it'll reserve enough for 1024 maximum-length filenames...
; flags         -- the requester's flags. Use 0 for defaults, or -1 for literally no flags. You can
;                  specify your own flags as per the Win32 API docs if you know what you're doing!

; Returns "" (empty string) if it fails or user cancels...

Procedure.s OpenMultiFileRequester (window, title$, dir$, filter$, maxfiles, flags)

  DefType.OPENFILENAME req

  req\lStructSize        = SizeOf (OPENFILENAME)                ; Size of structure
  req\hWndOwner          = window                               ; Parent window
  req\hInstance          = #NULL                                ; Not needed

  If maxfiles = 0
    maxfiles = 1024                                             ; Default to 1024 * max filename length
  EndIf

  If flags = 0
    req\Flags = #OFN_ALLOWMULTISELECT | #OFN_EXPLORER | #OFN_HIDEREADONLY ; Default 'quick-fix' flags
  Else
    If flags = -1
      req\Flags = 0                                                       ; If you really want no flags, use -1!
    EndIf
  EndIf
  
  filter$ = filter$ + "||"                                      ; Filter string has to end with 2 x #NULL

  ; Poke filter string into a memory block (have to do this to keep Chr (0) in the string!)...
  
  If AllocateMemory (0, Len (filter$), 0)
    memstart = MemoryID ()
    For a = 0 To Len (filter$) - 1
      a$ = Mid (filter$, a + 1, 1)                              ; String chars are numbered 1... x, so adding 1 to a here...
      If a$ = "|"
        a$ = Chr (0)                                            ; Replace "|" with Chr (0)
      EndIf
      PokeB (memstart + a, Asc (a$))
    Next
  Else
    ProcedureReturn ""
  EndIf

  req\lpstrFilter        = MemoryID ()                          ; Filter is in our allocated memory
  req\lpstrCustomFilter  = #NULL                                ; Not needed
  req\nMaxCustFilter     = #NULL                                ; Not needed
  req\nFilterIndex       = 0                                    ; Use default filter order

  If AllocateMemory (1, (maxfiles * #MAX_PATH), 0) = #False
    ProcedureReturn ""
  EndIf
  
  req\lpstrFile          = MemoryID ()                          ; Buffer for file string
  req\nMaxFile           = (maxfiles * #MAX_PATH)               ; Buffer length
  req\lpstrFileTitle     = #NULL                                ; Not needed
  req\nMaxFileTitle      = #NULL                                ; Not needed
  req\lpstrInitialDir    = @dir$                                ; Initial directory
  req\lpstrTitle         = @title$                              ; Title of requester
  
  If GetOpenFileName_ (req)                                     ; OK, call the requester

    For a = 0 To req\nFileOffset - 1                           ; req\nFileOffset is first *file* in list
      file$ = file$ + Chr (PeekB (req\lpstrFile + a))           ; Before that is the selected directory...
    Next
    
    file$ = file$ + "|"

    byte = req\nFileOffset                                      ; OK, get rest of files after directory

    Repeat
      b$ = Chr (PeekB (req\lpstrFile + byte))                   ; Read the character
      If b$ = ""                                                ; If it's Chr (0), we check the next character
        If Chr (PeekB (req\lpstrFile + byte + 1)) = ""          ; If the next character is also Chr (0), we're done
          gotall = #True
        Else
          file$ = file$ + "|"                                   ; Nope, just one Chr (0), so we have a complete filename
        EndIf
      Else
        file$ = file$ + b$                                      ; Not Chr (0), so we add it to the filename
      EndIf
      byte = byte + 1                                           ; Next byte in string...
    Until gotall = #True Or byte = (maxfiles * #MAX_PATH) - 1

    ; Free our memory, because this might get called over and over again...

    FreeMemory (0)
    FreeMemory (1)

    ProcedureReturn file$                                     ; We're done!
  Else
    ProcedureReturn ""                                        ; User cancelled requester
  EndIf
    
EndProcedure

; -----------------------------------------------------------------------------
; Demo
; -----------------------------------------------------------------------------

; NOTE that this demo just replaces the "|" characters in the returned string with
; newlines, so each filename gets a separate line in the requester :)

filter$ = "Text files|*.txt|Zip files|*.zip|All files|*.*"   ; Weird -- Zips don't get shown this way round,
                                                              ; plus it opens in source folder instead of default... :/

;filter$ = "Zip files|*.zip|Text files|*.txt|All files|*.*"    ; This one works

getfile$ = OpenMultiFileRequester (0, "Select files to open...", "", filter$, 0, 0)

result$ = ReplaceString (getfile$, "|", NewLine ())

If result$
  MessageRequester ("Path: " + path$, "Selected files..." + NewLine () + NewLine () + result$, 0)
EndIf

--
See ya,
James L Boyd.
<a href="http://www.hi-toro.com/[/url]
--
Post Reply