#!/usr/bin/ginsbasic001

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


//---   I have timing errors, if the filedialog is shown
//---   AFTER gins_main()
//---   So display it first, then hide it.

gins_glade("testfiledialog-mainwindow.glade")
//-- load  file and show the window
gins_glade("testfiledialog.glade")

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

gtk_createobject("GtkFileFilter" , "Filefilter" ,0,0)
gtk("gtk_file_filter_add_pattern"  , ":Filefilter" , "*.au" ,0,0,0,0)
gtk("gtk_file_chooser_set_filter"  , "filechooserdialog1" , ":Filefilter" ,0,0,0,0)

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

gtk("gtk_file_chooser_set_current_folder" , "filechooserdialog1" , "/usr/share/audio" ,0,0,0,0)
gins_set("filechooserdialog1" , "visible" , "1" , "<boolean>")

//-- If the dialog is destroyed, it cannot be reopened.
//-- So I have set the glade-file to generate a popup without close-button.
//-- For more comfort, we now will maximize it to fill the whole screen

screensize = xwin_screensize()
x=screensize[0]
y=screensize[1]
gtk("gtk_widget_set_usize" , "filechooserdialog1" ,x,y,0,0,0)
gtk("gtk_widget_hide" , "filechooserdialog1" ,0,0,0,0,0)

//-- test to avoid destroy of a normal window, no success yet :-(
gtk_grab_event( "filechooserdialog1" ,"destroy-event")

gins_main()

//- wait a moment to avoid async errors from xlib
gins_usleep(60000)
print "ok"


//-- 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
   ' print event
   ' print pdata

    if event = "GDK_DELETE" then
    if widget = "window1" then
        gins_exit()
        end
    end if
    end if

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

      if widget = "buttoncancelnew" then

	print "canceled"
	gtk("gtk_widget_hide" , "filechooserdialog1" ,0,0,0,0,0)

      end if

      if widget = "buttonoknew" then

	print "ok"
        mylist = gtk("gtk_file_chooser_get_filenames" , "filechooserdialog1" , 0,0,0,0,0)
	gtk("gtk_widget_hide" , "filechooserdialog1" ,0,0,0,0,0)

	for each theline in mylist
		print theline
	next 

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

      end if

      if widget = "button1" then
	gtk("gtk_widget_show" , "filechooserdialog1" ,0,0,0,0,0)
      end if

      if widget = "button2" then
	gtk("gtk_widget_hide" , "filechooserdialog1" ,0,0,0,0,0)
      end if

    end if
  end if
  
  gins_usleep(50000)

wend
