BBC BASIC
« File Selector in BASIC? »

Welcome Guest. Please Login or Register.
Jan 23rd, 2018, 7:12pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: File Selector in BASIC?  (Read 469 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 692
xx File Selector in BASIC?
« Thread started on: Dec 20th, 2015, 11:07pm »

Has anybody written a File Selector (Choose File dialogue) in BASIC - i.e. without any calls to the Windows API - that they would be prepared to share?

Richard.
User IP Logged

jgharston
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Re: File Selector in BASIC?
« Reply #1 on: Dec 23rd, 2015, 8:39pm »

For people who haven't seen the conversation elsewhere, this is what I came up with, an update of something I started some years ago:

BLib/Win/FPicker.bbc

(Commented demo explaining the code here.)
« Last Edit: Dec 23rd, 2015, 8:39pm by jgharston » User IP Logged

RNBW
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 23
xx Re: File Selector in BASIC?
« Reply #2 on: Dec 26th, 2015, 11:05am »

Quote:
For people who haven't seen the conversation elsewhere, this is what I came up with, an update of something I started some years ago:

BLib/Win/FPicker.bbc

(Commented demo explaining the code here.)


It would be useful to look at this, but searching BLib/Win/FPicker.bbc got me nowhere and you didn't include the commented demo.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 692
xx Re: File Selector in BASIC?
« Reply #3 on: Feb 8th, 2016, 10:05pm »

My thanks to those who submitted programs. Although they worked well with keyboard input (which admittedly is all I asked for originally) they weren't really suitable for use on Android (mobile) devices with small touch screens.

I have therefore written my own utility, which takes the form of a launcher (it lets you select a program which it then automatically CHAINs). This is touch or mouse driven only - there is no option for keyboard input at all. Clearly on many platforms that wouldn't be acceptable, but it's ideal for a device with no physical keyboard.

I wanted to experiment particularly with reproducing the UI experience that a touch screen usually provides, such as scrolling using a 'flick' gesture (which continues as if it has inertia) and selection using a tap gesture. Although the end result may not be quite as smooth as a native Android application it's not bad.

I have deliberately made the program compatible with both BBC BASIC for Windows and BBC BASIC for SDL 2.0 so if you only currently have BB4W you can still try it. It can be driven with a mouse or a touchscreen, but of course the 'flick' gesture is not something you would commonly do using a mouse.

Richard.

Code:
      REM Program launcher for touch-screen and mouse operation.
      REM By R.T.Russell http://www.rtrussell.co.uk/ 08-Feb-2016
      REM This program is compatible with both BB4W and BBCSDL2!

      REM Initialisation:
      ON ERROR OSCLI "REFRESH ON" : MODE 3 : PRINT REPORT$ : END
      INSTALL @lib$+"sortlib"
      INSTALL @lib$+"stringlib"
      sort% = FN_sortinit(0,0)
      GCOL 143:COLOUR 143:GCOL 0:COLOUR 0:VDU 26,12,5,23,16,64|
      DIM name$(1000), type&(1000)
      cd$ = @dir$

      REM Come here each time the directory is changed:
      REPEAT
        REM Spool *DIR output to a temporary file:
        WIDTH 20
        VDU 21
        OSCLI "spool """ + @tmp$ + "dir.tmp.txt"""
        OSCLI "dir """ + cd$ + "*.*"""
        *spool
        VDU 6
        WIDTH 0
  
        REM Parse the file to extract directories and .bbc filenames.
        REM Cope with long filenames if they have split between lines
        REM (note this won't work if a 'read only' marker is present
        REM but the SDL 2 version of BBC BASIC doesn't support that):
        N% = 0
        F% = OPENIN(@tmp$ + "dir.tmp.txt")
        WHILE NOT EOF#F% AND N% < DIM(name$(),1)
          INPUT #F%,A$
          IF ASCA$=10 A$ = MID$(A$,2)
          IF LEFT$(A$,2)="  " THEN
            D$ = FN_lower(name$(N%))
            IF INSTR(D$,".") = 0 type&(N%) = 0 : N% += 1 : REM directory
            IF INSTR(D$,".bbc")  type&(N%) = 1 : N% += 1 : REM BBC file
            IF N% = 0 N% += 1 : REM Zeroth index holds *DIR title string
            name$(N%) = MID$(A$,3)
          ELSE
            name$(N%) += A$
          ENDIF
        ENDWHILE
        CLOSE #F%
        D$ = FN_lower(name$(N%))
        IF INSTR(D$,".") = 0 type&(N%) = 0 : N% += 1 : REM directory
        IF INSTR(D$,".bbc")  type&(N%) = 1 : N% += 1 : REM BBC file
        IF N% = 0 N% += 1 : REM Zeroth index holds *DIR title string
        name$(N%) = ".."
        num% = N%
  
        REM Remove trailing spaces:
        FOR I% = 1 TO num%
          A$ = name$(I%)
          WHILE RIGHT$(A$) = " " : A$ = LEFT$(A$) : ENDWHILE
          name$(I%) = A$
        NEXT
  
        REM Sort the array so directories are listed before programs:
        C% = num%
        CALL sort%, type&(1), name$(1)
  
        REM Select font using BBCSDL2 syntax; although this isn't valid
        REM for BB4W it will fall back to using a suitable font anyway:
        OSCLI "font """ + @lib$ + "FreeSans"", 20"
  
        REM Get window metrics (beware multithread race hazard in BBCSDL2):
        maxx%  = 2 * (@vdu%!208 - 1)
        maxy%  = 2 * (@vdu%!212 - 1)
        chary% = 2 * @vdu%!220
        nrows% = @vdu%!212 DIV @vdu%!220 + 2
  
        REM Display a drag-scrollable list of filenames and directories.
        REM Clicking/tapping moves into the directory or runs the program:
        touch% = FALSE
        speed = 0
        oldy% = -1
        refy% = -1
        reft% = TIME
        refresh% = TRUE
        scrolly% = 0
        *REFRESH OFF
        REPEAT
          WAIT 1
          MOUSE X%,Y%,B%
          IF Y% >= 0 IF Y% <= maxy% THEN
            IF B% AND 4 THEN
              IF NOT touch% THEN
                refy% = Y%
                reft% = TIME
                touch% = TRUE
              ELSE
                scrolly% += Y% - oldy%
                refresh% = TRUE
              ENDIF
            ELSE
              IF touch% THEN
                IF ABS(Y% - refy%) < 32 IF (TIME - reft%) < 50 EXIT REPEAT
                IF TIME - reft% speed = 2 * (Y% - refy%) / (TIME - reft%)
                touch% = FALSE
              ENDIF
              scrolly% += speed
              speed *= 0.9
              IF ABS(speed) < 8 speed = 0 ELSE refresh% = TRUE
            ENDIF
          ENDIF
          IF scrolly% < 0 scrolly% = 0
          IF refresh% THEN
            CLS
            MOVE 0,maxy% + scrolly% MOD chary%
            N% = scrolly% DIV chary%
            FOR I% = N% + 1 TO N% + nrows%
              IF I% > DIM(name$(),1) EXIT FOR
              PRINT name$(I%)
            NEXT
            *REFRESH
            refresh% = FALSE
          ENDIF
          oldy% = Y%
        UNTIL FALSE
  
        REM A selection has been made, so find the selected item:
        I% = (scrolly% + maxy% - Y%) / chary% + 1
  
        REM Change the background colour briefly as confirmation:
        GCOL 2,11
        RECTANGLE FILL 0, maxy% + scrolly% - I% * chary%, maxx%, chary%
        GCOL 0
        *REFRESH
        WAIT 25
  
        REM Try to open the selected file; if successful CHAIN it:
        F% = OPENIN(cd$ + name$(I%))
        IF F% THEN
          CLOSE #F%
          *REFRESH ON
          OSCLI "KEY1 CHAIN """ + @dir$ + "touchmenu""|M"
          VDU 23,22,640;500;8,16,16,128
          CHAIN cd$ + name$(I%)
        ENDIF
  
        REM As the file could not be opened assume it is a directory.
        REM In BB4W this will not backup above the initial directory:
        IF name$(I%) = ".." THEN
          cd$ = LEFT$(cd$, FN_instrr(LEFT$(cd$), "/", 0))
        ELSE
          cd$ += name$(I%) + "/"
        ENDIF
  
        REM Clear the arrays for the next scan:
        name$() = ""
        type&() = 0
      UNTIL FALSE
      END 

« Last Edit: Feb 8th, 2016, 11:20pm by Richard Russell » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls