#include once "SimpleGadget.bas"
#include once "StringGadget.bas"
#include once "ScrollBar.bas"
#include once "ListBox.bas"
#include once "Label.bas"
#include once "MessageBox.bas"

'InitGFX m u s s aufgerufen worden sein

declare function FileRequester(posx as integer,posy as integer,bt as string,path as string, file as string="",fmode as integer=0) as string
declare sub FindContent(gad as Gadget ptr,t as string )

function FileRequester(posx as integer,posy as integer,bt as string,path as string, file as string="",fmode as integer=0) as string
  function=""

  'Sichern des vom Requester bedeckten Hintergrundes
  dim as integer ptr gfxbackup
  gfxbackup=imagecreate(400,300)
  get (posx,posy)-(posx+399,posy+299),gfxbackup
  
  FillA(posx,posy,400,300,GadgetColor,0)
  FrameB(posx+2,posy+21,396,277,1)
  ClearBox(posx+3,posy+22,394,275)
  draw string (posx+5, posy+3),bt & "...",TextColor
  
  dim as string sep,drive,originpath=curdir
  'Windows Variante
  #ifdef __fb_win32__
    sep="\"
  #endif
  'Tux Variante
  #ifdef __FB_LINUX__
    sep="/"
  #endif

  dim as integer rows
  if fontheight=8  then rows=22
  if fontheight=14 then rows=12
  if fontheight=16 then rows=11

  dim as string entry,GoToFolder,full
  GoToFolder=""
  entry=""
  chdir path
  path=curdir
  drive=left(path,1)


  dim as EventHandle ptr event
  event=CreateEventHandle

  dim as Gadget ptr labelp,labelf,doit,cancel,strpath,strfile,contlist,cd
  
  labelp=AddLabel(event,posx+5, posy+28,"Datei:",TextColor)
  strpath=AddStringGadget(event,posx+52,posy+25,38,path,,1)
  contlist=AddListBox(event,posx+5,posy+54,46,rows)
  labelf=AddLabel(event,posx+5, posy+245,"Datei:",TextColor)
  strfile=AddStringGadget(event,posx+62,posy+242,29,file,,1)
  doit=AddSimpleGadget(event,posx+309,posy+242,85,23,bt)
  cancel=AddSimpleGadget(event,posx+309,posy+271,85,23,"Abbruch")'"Cancel"
  cd=AddSimpleGadget(event,posx+370,posy+25,24,23,"..")




  TO_ClearText(contlist)
  FindContent(contlist,"D")
  FindContent(contlist,"F")
  UpdateGadget(contlist)

  GadgetOn(labelp,cd)
  do
    event->xSleep(1,0)
    if event->GADGETMESSAGE then
      select case event->GADGETMESSAGE

        'Pfadeingabe
        case strpath
          chdir UExport(GetString(strpath))           'Wechsel ins Directory
          path=UImport(curdir)                        'Setze aktuellen Pfad
          
          TO_ClearText(contlist)
          FindContent(contlist,"D")
          FindContent(contlist,"F")
          UpdateGadget(contlist)

          SetString(strpath,path)                     'Aktualisiere die Pfadanzeige

        case contlist
          entry=TO_GetLineContent(contlist,GetListBoxVal(contlist))

          if entry<>"" then
            if left(entry,6)="<DIR> " then                'wenn entry ein Directory
              GoToFolder=right(entry,len(entry)-6)
              chdir UExport(GoToFolder)                   'Wechsel ins Directory
              path=UImport(curdir)                        'Setze aktuellen Pfad

              TO_ClearText(contlist)
              FindContent(contlist,"D")
              FindContent(contlist,"F")
              UpdateGadget(contlist)

              SetString(strpath,path)                     'Aktualisiere die Pfadanzeige
            else                                          'ansonsten ist entry ein Dateiname
              file=entry
              SetString(strfile,file)
            end if
          end if

        case cd
          chdir ".."                                  'Wechsel ins übergeordnete Directory
          path=UImport(curdir)                        'Setze aktuellen Pfad
          
          TO_ClearText(contlist)
          FindContent(contlist,"D")
          FindContent(contlist,"F")
          UpdateGadget(contlist)

          SetString(strpath,path)                     'Aktualisiere die Pfadanzeige        

				'Beenden und Rückabe eines Strings
        case strfile,doit
          file=GetString(strfile)
          if file<>"" then
            if right(path,1)<> sep then
              full=UExport(path) & sep & UExport(file)
            else
              full=UExport(path) & file
            end if
            if fmode then
              if fileexists(full) then
                function=full
                event->EXITEVENT=1
              else
                GadgetSleep(labelp,cd)
                MessageBox(posx+20,posy+100,"Zugriff auf Datei nicht moeglich!","Fehler",MBType_OK)
                RestoreActivation(labelp,cd)
              end if
            else
              function=full
              event->EXITEVENT=1
            end if
          end if

        'Abbruch des FileRequesters
        case cancel
          event->EXITEVENT=1
          function=""

      end select
    end if
  loop until event->EXITEVENT
  DestroyEventHandle(event)
  chdir originpath
  put (posx,posy),gfxbackup,pset
  imagedestroy gfxbackup
end function


sub FindContent(gad as Gadget ptr,t as string )
  dim as integer attr
  dim as string entryname,pre
  if ucase(t)="D" then 'bei "D" suche nach Verzeichnissen
    attr=&H10 + &H01
    pre="<DIR> "       '"Vorsilbe" um Verzeichnisse von Dateien unterscheiden zu können
  end if
  if ucase(t)="F" then  'bei "F" suche nach Dateien
    attr=&H00
    pre=""
  end if

  entryname=dir("*",attr)
  do
    if (len(entryname)>0) and (entryname<>".") and (entryname<>"..") then
      if TO_GetLineContent(gad,1)="" then
        TO_SetLineContent(gad,1,pre & UImport(entryname))
      else
        TO_AppendLine(gad,pre & UImport(entryname))
      end if
    end if
    entryname=dir
  loop while len(entryname)
end sub
