Ooops
November 28, 2020, 01:28:28 AM

Author Topic: [bb] file requester - B3d by Dan [ 1+ years ago ]  (Read 562 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] file requester - B3d by Dan [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : file requester - B3d
Author : Dan
Posted : 1+ years ago

Description : Displays all files (or filters the files with the specified extension)
in a scrollable list and allows you to choose one from it.

see <a href="codearcs90a4.html?code=3215" target="_blank"> here [/url] for more commented example.


Code :
Code: BlitzBasic
  1. ;====================================================================
  2. ; Project: FileRequester
  3. ; Version: 1.0
  4. ; Author: ~Dan~
  5. ; Email:
  6. ; Copyright: PD
  7. ; Description: Simple File Chooser
  8. ;                               displays a list of files in a choosen folder
  9. ;                               with choosen extension.
  10. ;                               Use Arrow Up/Down to choose a file
  11. ;                               Return/Enter to set the filename into ToLoad$ variable
  12. ;                               Set FR_ext$ to ".*" to accept all files
  13. ;               Returns FR_Error=1 if directory does not exist         
  14. ;====================================================================
  15. ; The following decls are needed ONLY for the Screen function
  16. ; if you dont want to use it, comment the whole Screen Function
  17. ; and remove the calls to it
  18. ;
  19. ; User32.decls 
  20. ;;.lib "user32.dll"
  21. ;api_GetSystemMetrics% (nIndex%) : "GetSystemMetrics"
  22. ;api_GetActiveWindow%():"GetActiveWindow"
  23. ;api_GetDC% (hwnd%) : "GetDC"
  24. ;api_GetDesktopWindow% () : "GetDesktopWindow"
  25. ;
  26. ;
  27. ; GDI32.decls
  28. ;.lib "gdi32.dll"
  29. ;api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"
  30. ;====================================================================
  31.  
  32.  
  33. Screen 320,200
  34. Global TxtY=-2
  35.  
  36. FR_folder$="c:"
  37. FR_ext$=".*"
  38. ReqMaxShow=5
  39. ;ReqMaxShow=(GraphicsHeight()/FontHeight())-4  ;Use this to have resolution dependant height of the displayed items
  40.  
  41. For Z=1 To 3         ; to test the reuseability
  42.         Gosub FileRequester
  43.         If fr_Error
  44.                 Print FR_folder$
  45.                 Print " - No such directory !": Delay 5000 :Exit
  46.         EndIf
  47.         Cls
  48.         Locate 0,0
  49.         If ToLoad$<>""
  50.                 Print "("+Z+")"+"filename: "
  51.                 Print ToLoad$
  52.         Else
  53.                 Print "No filename, or Cancelled!"
  54.         EndIf
  55.         FlushKeys()
  56.         Print "Press any key to continue"
  57.         WaitKey()
  58. Next
  59.  
  60. End
  61.  
  62.  
  63. .FileRequester          ; Returns filename in ToLoad$, if its empty then it was cancelled or not available
  64. ;                                       ; Returns FR_Error - 1 Directory doesnt exists
  65. ; before call set :
  66. ; FR_folder$="c:data"   ;               to the folder which will be checked
  67. ; FR_ext$=".*"  ;                               as extension of the file displayed, ".*" for every file
  68. ; ReqMaxShow=5  ;                               set how many files will be displayed at once
  69. ;ReqMaxShow=(GraphicsHeight()/FontHeight())-4  ;Use this to have height resolution-dependant number of the displayed items
  70.  
  71.  
  72. FR_LoadedFile=0
  73.  
  74. fr_delay=80   ;Arrow Up/Down Delay time, in millisecs
  75. FR_Error=0
  76.  
  77. If FileType(FR_folder$)<2 Then ToLoad$="": FR_Error=1 : Goto theend
  78.  
  79. For fr_x=1 To 2                                                         ;Reads 2 time the Current folder !
  80.         FR_DIR=ReadDir(FR_folder$)
  81.         If fr_x=2                                                                       ;Second pass, make a dim statement
  82.                 Dim FR_Filename$(FR_LoadedFile)
  83.                 FR_LoadedFile=0
  84.         EndIf
  85.        
  86.         Repeat
  87.                 FR_File$=NextFile$(FR_DIR)
  88.                 If FR_File$="" Then Exit
  89.                 If FileType(FR_folder$+""+FR_File$) = 1 Then
  90.                         If Lower$(Right$(FR_File$,Len(FR_ext$)))=FR_ext$ Or FR_ext$=".*";Check for .dat files
  91.                                 FR_LoadedFile=FR_LoadedFile+1                                                   ;1st pass count .dat files
  92.                                 If fr_x=2 Then FR_Filename$(FR_LoadedFile)=FR_File$             ;2ns pass add to FR_Filename$
  93.                         End If
  94.                 End If
  95.         Forever
  96.        
  97.         CloseDir (FR_DIR)
  98.         If FR_LoadedFile=0 Then ToLoad$="" : Goto theend
  99. Next
  100.  
  101. FR_RequesterPos=1
  102. FR_ReqOffset=0
  103.  
  104. ;Check if Enter,Return or Esc keys are Held down
  105.  
  106.  
  107. While KeyDown(156) Or KeyDown(28) Or KeyDown(1) ; Delay The Program Execution until theese keys are released
  108. Delay 1
  109. Wend
  110. FlushKeys()
  111. Delay 10
  112.  
  113. FR_time=MilliSecs()
  114.  
  115. Repeat
  116.         Cls
  117.         Color $ff,$ff,$ff
  118.         TextY (-2,"Please Choose: ("+FR_LoadedFile+") files")
  119.         Color $FF,$FF,$0
  120.         TextY (0, String$("-",40))
  121.        
  122.        
  123.         If KeyDown(200) And MilliSecs()-FR_time>fr_delay ; Key Arrow Up
  124.                 FR_time=MilliSecs()
  125.                 FR_RequesterPos=FR_RequesterPos-1
  126.         EndIf
  127.        
  128.         If KeyDown(208) And MilliSecs()-FR_time>fr_delay; Key Arrow Down
  129.                 FR_time=MilliSecs()
  130.                 FR_RequesterPos=FR_RequesterPos+1
  131.         EndIf
  132.  
  133.         If KeyDown(156) Or KeyDown(28)                  ;Return or Enter
  134.                 Exit
  135.         EndIf
  136.        
  137.         If KeyDown(1)                                                   ;Esc
  138.                 ToLoad$=""
  139.                 Exit
  140.         EndIf
  141.        
  142.         If FR_RequesterPos<1
  143.                 FR_RequesterPos=1
  144.                 If FR_ReqOffset>0
  145.                         FR_ReqOffset=FR_ReqOffset-1
  146.                 EndIf
  147.         EndIf
  148.  
  149.        
  150.         If FR_RequesterPos>ReqMaxSHow
  151.                 FR_RequesterPos=ReqMaxShow
  152.                 If FR_ReqOffset<FR_LoadedFile-ReqMaxShow
  153.                         FR_ReqOffset=FR_ReqOffset+1
  154.                 EndIf
  155.         EndIf
  156.        
  157.         If FR_RequesterPos>FR_LoadedFile Then FR_RequesterPos=FR_LoadedFile
  158.         For fr_LX=1 To ReqMaxShow
  159.                 If fr_LX=FR_RequesterPos
  160.                         Color $0,$ff,$ff
  161.                         ToLoad$=FR_Filename$(fr_LX+FR_ReqOffset)
  162.                 Else
  163.                         Color $Af,$af,$af
  164.                 EndIf
  165.                 If fr_LX <= FR_LoadedFile
  166.                         TextY (0,FR_Filename$(fr_LX+FR_ReqOffset))
  167.                 Else
  168.                         ;TextY (0,"-empty slot-") ; Uncheck to display Empty Slot if there are not
  169.                         ;                                                       enough files to fit the ReqMaxShow display
  170.                 EndIf
  171.         Next
  172.        
  173.         Color $FF,$FF,$0
  174.         TextY (0,String$("-",40))
  175.         Color $ff,$ff,$ff
  176.         TextY (0,"Return to Accept, Esc to Cancel")
  177.         Delay 10
  178.         Flip
  179. Forever
  180. .theend
  181. FlushKeys()
  182. Return
  183.  
  184.  
  185. Function TextY (num,txt$)
  186. ;Num = How many columns to skip
  187. ;Replaces Print with Text
  188. ;Every Function call displays the text
  189. ;1 line under the old one, like calling
  190. ;multiple print statements after eachother.
  191. ;
  192. ; Set num to -2 to make the text go on top (like locate 0,0)
  193. ; Use the Global TxtY=-2 outside this function
  194. ;
  195. ;Global TxtY=-2       ;Make TxtY global variable for displaying help text
  196.    If num=-2
  197.       TxtY=0
  198.    Else
  199.           TxtY=TxtY+(FontHeight()*(num+1))
  200.    EndIf
  201.    Text 0,TxtY,txt$
  202. End Function   
  203.  
  204. Function Screen(x,y)
  205.    DeskX=api_GetSystemMetrics(0)
  206.         DeskY=api_GetSystemMetrics(1)
  207.         If x>DeskX Then x=DeskX
  208.     If x<64 Then x=64
  209.         If y>DeskY Then y=DeskY
  210.     If y<64 Then y=64
  211.     bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
  212.         Graphics x,y,bits,2
  213.         Graphics x,y,bits,3
  214.         api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)
  215. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal