#!/usr/bin/gtkbasic002

//-- this library has a nice function writelisttofile()
'include "/usr/lib/wxbasicscript/basefunctions.inc"

//-- make shure we find the gladefile

a = xwin_getdir(command(2))
xwin_cd(a)


'gins_exit()
'end
//-- load  file and show the window
gins_glade("GBdialog-multifilechooser.glade")

'xmessage( "-----------------" & a )
'end

if command() != 7 then

  msg = "Filechooser:\n\nERROR:\n\nArgument count mismatch!\n\n"
  gins_set("errormessage1label1" , "label" , msg , "<str>")

  gins_set("errormessage1" , "visible" , "1" , "<boolean>")

else

title = command(3)
folder = command(4)
filemask = command(5)
mode = command(6)
mode = val(mode)
multi = command(7)

'a=xwin_system("")
'xmessage( "-----------------" & HERE )

chooser = {"filechooserdialog1" , "filechooserdialog2" , "filechooserdialog3" , "filechooserdialog4"}


 //-------------------------------------------------------------
//-- this filter allows to show only ".au" files in the dialog

gtk_createobject("GtkFileFilter" , "Filefilter" ,0,0)
gtk("gtk_file_filter_add_pattern"  , ":Filefilter" , filemask ,0,0,0,0)
gtk("gtk_file_chooser_set_filter"  , chooser[mode] , ":Filefilter" ,0,0,0,0)

 //-------------------------------------------------------------
//-- set current path for the dialog

gtk("gtk_file_chooser_set_current_folder" , chooser[mode] , folder ,0,0,0,0)
gins_set(chooser[mode] , "title" , title , "<str>")
if mode < 3 then
//gins_set(chooser[mode] , "select_multiple" , multi , "<boolean>")
gtk("gtk_file_chooser_set_select_multiple" , chooser[mode], multi ,0,0,0,0)
end if
gins_set(chooser[mode] , "visible" , "1" , "<boolean>")

end if

gins_main()



//-- mainloop with eventhandling
while 1

  widget , event , pdata = gins_event()

  if widget != Nothing then
   //--ncomment these to see all events in the console
   ' print widget, event
   ' print pdata

    if event = "GDK_DELETE" then
      if widget = "errormessage1" then
	print "MESSAGE:ERROR: filechooser argument mismatch"
        gins_exit()
        end
      else
        gins_exit()
        end
      end if
    end if

'   if event = "GDK_BUTTON_PRESS" then // does not work in JWM :-(((
    if event = "GDK_BUTTON_RELEASE" then

      if widget = "errormessage1button1" then

	print "MESSAGE:ERROR: filechooser argument mismatch"
        gins_exit()
        end
      end if

      if left(widget , 12 ) = "buttoncancel" then

	print "MESSAGE: canceled"
        gins_exit()
        end
      end if

      if left(widget , 8 ) = "buttonok" then


        if mode = 3 then
	        myresult = gtk("gtk_file_chooser_get_current_folder" , chooser[mode] , 0,0,0,0,0)
		print myresult
        else
		mylist = gtk("gtk_file_chooser_get_filenames" , chooser[mode] , 0,0,0,0,0)
		for each theline in mylist
			print theline
		next 
	end if


	// we can write the result to a tempfile, to use it in other programs
 //       writelisttofile("selected-files.tmp.txt" , mylist)
        gins_exit()
        end
      end if

    end if
  end if
  
  xwin_usleep(10000)

wend
