#!/usr/bin/gtkbasic002
include "/usr/lib/wxbasicscript/basefunctions.inc"

HERE = xwin_getdir(command(2))
HERE &= "/resource" 
xwin_cd(HERE)

//-- load  file and show the window
gins_glade("project.glade")
gins_main()


//-- manipulate a widget, e.g. move the window
gins_set("window1" , "10" , "10" , "<uposition>")

//-- fill label with text
msg = "please drag some entries around in the lists.\n"
msg &= "Also drop a file from rox-filer."
gtk("gtk_label_set_text", "label1" , msg ,0,0,0,0)


//-- load 3 different pixmaps
gtk_createobject("PixmapwidFromFile" , "pixmap1", "cone.xpm" , 0)
gtk_createobject("PixmapwidFromFile" , "pixmap2", "cube.xpm" , 0)
gtk_createobject("PixmapwidFromFile" , "pixmap3", "sphere.xpm" , 0)

//-- clists need gdk-representations of a pixmap, so create them
gtk_get_pixmap_and_mask("pixmap1", "gdkpixmap1" , "gdkmask1" )
gtk_get_pixmap_and_mask("pixmap2", "gdkpixmap2" , "gdkmask2" )
gtk_get_pixmap_and_mask("pixmap3", "gdkpixmap3" , "gdkmask3" )

//-- now we do not need the pixmaps any more, destroy them to free memory
gtk_destroyobject("pixmap1")
gtk_destroyobject("pixmap2")
gtk_destroyobject("pixmap3")

//-- add list 1

gtk_createobject("GtkBasicList" , "mylist1", "window1" , "scrolledwindow1")
gtk("gtk_widget_show" , "mylist1" , 0 ,0,0,0,0)
gtk("gtk_clist_set_column_title", "mylist1" , "0" , "this is list 1", 0,0,0)

//-- add 2 empty lines
gtk("gtk_clist_append", "mylist1" , "" ,0,0,0,0)
gtk("gtk_clist_append", "mylist1" , "" ,0,0,0,0)

//-- fill 2 lines with entries
gtk_clist_set_pixtext(  "mylist1" , "0" ,"0","list 1 line 0","2","gdkpixmap1","gdkmask1") 
gtk_clist_set_pixtext(  "mylist1" , "1" ,"0","list 1 line 1","2","gdkpixmap1","gdkmask1")


//-- add list 2

gtk_createobject("GtkBasicList" , "mylist2", "window1" , "scrolledwindow2")
gtk("gtk_widget_show" , "mylist2" , 0 ,0,0,0,0)
gtk("gtk_clist_set_column_title", "mylist2" , "0" , "this is list 2", 0,0,0)

//-- add 2 empty lines
gtk("gtk_clist_append", "mylist2" , "" ,0,0,0,0)
gtk("gtk_clist_append", "mylist2" , "" ,0,0,0,0)

//-- fill 2 lines with entries
gtk_clist_set_pixtext(  "mylist2" , "0" ,"0","list 2 line 0","2","gdkpixmap2","gdkmask2") 
gtk_clist_set_pixtext(  "mylist2" , "1" ,"0","list 2 line 1","2","gdkpixmap2","gdkmask2")

gdk_flush()

//-- mainloop with eventhandling
while 1

  checkclose()
  widget , event , pdata = gins_event()

  if widget != Nothing then
    'print widget, event, pdata

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

    if event = "GDK_BUTTON_RELEASE" then

     if widget = "button1" then
         gtk("gtk_clist_unselect_all" , "mylist2" , 0 , 0, 0,0,0  ) 
     end if
    end if

    if event = "DND_RECEIVED" then

	print widget, event, pdata

	if left(widget ,6)  = "mylist" then

		target = widget

		//-- this splits the received values in 3 strings
		analyze_dnd(pdata)

		if newrow >= 0 then

			//newrow += 1

			//-- use a sphere by default as icon for unknown sourcewidgets,
			//-- e.g. if you drop files from Rox-Filer
			pixmap = "gdkpixmap3"
			mask = "gdkmask3"
			if sourcewidget = "mylist1" then
				pixmap = "gdkpixmap1"
				mask = "gdkmask1"
			end if
			if sourcewidget = "mylist2" then
				pixmap = "gdkpixmap2"
				mask = "gdkmask2"
			end if

			//-- if sourcelist is the targetlist, delete the entry (so move it instead of copy)

			//-- if sourceline is targetline in same widget, stop
			stop = 0
			if target = sourcewidget then

				a = gtk_get_list_selection(target)

				a = val(a)
				if a = newrow then
					stop = 1
				else
gtk("gtk_clist_unselect_all" , target , 0 , 0, 0,0,0  )
					gtk("gtk_clist_remove", target , a ,0 ,0,0,0)

					if newrow < 0 then
						newrow = 0
					end if
				end if
			else
				// add new rows from other widgets always after the one the entry was dropped on
				newrow += 1
			end if

			//-- if the content has more than one lines, detect this
			if stop = 0 then
				if instr(content , "\n") then
					thelist = explode(content , "\n")
					for i = 0 to count(thelist) -1

						gtk("gtk_clist_insert", target , newrow+i ,"" ,0,0,0)
						gtk_clist_set_pixtext(  target , newrow+i , "0", trim(thelist[i]) ,"2",pixmap,mask)

					next

					gtk("gtk_clist_select_row" , target , newrow , "0", 0,0,0  )
				else
					//-- just 1 line
					gtk("gtk_clist_insert", target , newrow ,"" ,0,0,0)
					gtk_clist_set_pixtext(  target , newrow , "0", content ,"2",pixmap,mask)


					gtk("gtk_clist_select_row" , target , newrow , "0", 0,0,0  )
				end if
 			end if

			gdk_flush()
		end if
	end if
    end if
 

  end if

  xwin_usleep(2000)

wend


function analyze_dnd(pdata)
		sourcewidget = cutrightfromleft(pdata , ":")
		newrow = cutleft(pdata , ":")
		newrow = cutrightfromleft(newrow , ":")
		newrow = val(newrow)
//newrow = newrow + 1
		content = cutleft(pdata , ":")
		content = cutleft(content , ":")
		content = trim(content)
end function

 //------------------------------------------------------------
// this function exits Basic, if the grafical interface crashed
sub checkclose()
	a=gins_get("window1" , "name" , "<str>")
	if a = Nothing then
		print "window was closed, exiting..."
		gins_exit()
		end
	end if
end sub

