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

'writestringtofile("/root/xtest.txt" , "x")
'end

'for n=32 to 254:print n & " "&chr(n):next
'end

print "ok"

ALLPROJECTSFOLDER = "Gtkbasic002-projects"

thelist = xwin_system("echo ~")
HOME = trim(thelist[0])

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

  PROJECTOPENED = 0
  OPENEDPROJECTBASE = ""
  OPENEDPROJECT = ""
  OPENEDPROJECTEXEC = ""
  OPENEDPROJECTGLADE = ""

'xwin_usleep(1000000)

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

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

ptype = gins_set("combobox1" , "active" , "0", "<int>")

addfunctions()

gins_main()


//gdk_flush()
// -- create some textbuffers
gtk("gtk_text_view_get_buffer" ,"textview1" ,  0 , 0, 0,0,0)
gtk_setobjectname("buf1")
gtk("gtk_text_view_get_buffer" ,"textview2" ,  0 , 0, 0,0,0)
gtk_setobjectname("buf2")
gtk("gtk_text_view_get_buffer" ,"textview3" ,  0 , 0, 0,0,0)
gtk_setobjectname("buf3")
gtk("gtk_text_view_get_buffer" ,"textview4" ,  0 , 0, 0,0,0)
gtk_setobjectname("buf4")
gtk("gtk_text_view_get_buffer" ,"textviewstatus" ,  0 , 0, 0,0,0)
gtk_setobjectname("bufstatus")



//--- display some helptext
loadfiletobuffer(HERE & "/info1.txt" , "buf1")
loadfiletobuffer(HERE & "/info2.txt" , "buf2")
loadfiletobuffer(HERE & "/info3.txt" , "buf3")
loadfiletobuffer(HERE & "/info4.txt" , "buf4")

'end

gdk_flush()

//-- mainloop with eventhandling
start=0
while 1

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

'print "test"

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

    if event = "GDK_DELETE" then

      if widget="window1" then
        print "....bye...."
        gins_exit()
        end
      end if
     //-- now handled by C-eventloop
     ' if widget="newdialogwindow" then
     '   gtk("gtk_widget_hide" , "newdialogwindow",0,0,0,0,0)
     '   print "newdialog was closed"
     ' end if
    end if


    if event = "GDK_BUTTON_RELEASE"  then
  //  if event = "GDK_BUTTON_PRESS"  then
   ' print widget
   ' print event
   ' print pdata

      if widget = "mylist1" then
        functionslist_clicked()
      end if

      if widget = "newdialogbuttoncancel" then
        newdialogbuttoncancel()
      end if
      if widget="buttonnew" then
        buttonnew()
      end if
      if widget="buttonopen" then
        buttonopen()
      end if
      if widget="buttonrun" then
        buttonrun()
      end if

      if widget="buttonsave" then
        buttonsave()
      end if
      if widget="buttonsaveas" then
        buttonsaveas()
      end if
      if widget="buttondotpup" then
        buttondotpup()
      end if
      if widget="buttonglade" then
        shell("glade " & OPENEDPROJECTGLADE & " &")
      end if
      if widget="buttoneditor" then
        shell("leafpad " & OPENEDPROJECTEXEC & " &")
      end if
      if widget="buttongtkdoc" then
        shell("defaultbrowser http://gtk.org/api &")
      end if

      if widget="newdialogbutton1" then
        newdialogbutton1()
      end if

'gdk_flush()
    end if


  'end if
  end if
  xwin_usleep(2000)

wend

function  newdialogbuttoncancel()

  gins_set("newdialogwindow" , "visible" , "FALSE" , "<boolean>")
  gins_set("errorlabel1" , "label" , "" , "<str>")

end function  


function newdialogbutton1()
'gdk_flush()
 textview_settext(":bufstatus" , "wait, creating new project!")
'xwin_usleep(200000)

  ptype = gins_get("combobox1" , "active" , "<int>")

  print "creating new project..."
  projecttype = gins_get("combobox1" , "active" , "<int>")
  projectname = gins_get("newdialogentry1" , "text" , "<str>")
  a = ucase(left(projectname , 1))
  projectname = a & right(projectname , len(projectname)-1)
  projectname2 = replace(projectname , " " , "_")
  projectexec = "start-" &  projectname2 
  print projecttype , projectname, projectname2

  thelist = xwin_system("test -d "  & HOME & "/" & ALLPROJECTSFOLDER & "/" & projectname2 & ";echo $?")
  pexist = trim(thelist[0])
  if pexist = "0" then
    gins_set("errorlabel1" , "label" , "\n\nERROR: This Project exists!\n\n" , "<str>")
    return
  end if
  newdialogbuttoncancel()
  gins_set("errorlabel1" , "label" , "" , "<str>")

//    textview_settext(":bufstatus" , "wait, creating new project!")

  xwin_system("mkdir \"" & HOME & "/" & ALLPROJECTSFOLDER & "\" 2>/dev/null")
  xwin_system("mkdir \"" & HOME & "/" & ALLPROJECTSFOLDER & "/" & projectname2 & "\"")

  pa = "\"" & HERE & "/ide-projects/" & projecttype & "/\"*"
print pa
  pb = HOME & "/" & ALLPROJECTSFOLDER & "/" & projectname2 & "/"
  xwin_system("cp -ax " & pa & " \"" & pb & "\"")
  xwin_system("cp -ax " & "\"" & HERE & "/ide-projects/" &  "/project.png\" " & "\"" & pb & projectname2 & ".png\"")
  xwin_system("cp -ax " & "\"" & HERE & "/ide-projects/" & "/icon.xpm\" " & pb)
  xwin_system("mv " & "\"" & pb & "project\" \"" & pb & projectexec & "\"")
  xwin_system("mv " & "\"" & pb & "project.GBprj\" \"" & pb & projectname2 & ".GBprj\"")
  OPENEDPROJECTBASE = pb
  OPENEDPROJECT = "\"" & pb & projectname2 & "\""
  OPENEDPROJECTEXEC = "\"" & pb & projectexec & "\""
  OPENEDPROJECTGLADE = "\"" & OPENEDPROJECTBASE & "/resource/project.glade" & "\""

//    textview_settext(":bufstatus" , "  finished, created new project!   current project is: " & projectname)
//gdk_flush()
  textview_settext("bufstatus" , "  finished, created new project! current project is --> "  & projectname & ".GBprj")

  PROJECTOPENED = 1

end function  

function buttonrun ()

  textview_settext("bufstatus" , " ")
  if PROJECTOPENED = 0 then
    textview_settext("bufstatus" , " Error --> no project loaded or created from new! Nothing to execute...")
    return
  end if
  shell( OPENEDPROJECTEXEC & " &")

end function

function buttonnew ()

'  textview_settext(":bufstatus" , " ")
  gins_set("newdialogwindow" , "visible" , "True" , "<boolean>")

  //-- this will bring it in front, if it is already open behind the mainwindow
  gins_set("newdialogwindow" , "visible" , "FALSE" , "<boolean>")
  gins_set("newdialogwindow" , "visible" , "True" , "<boolean>")
  textview_settext(":bufstatus" , " ")
end function

function buttonsave()
    textview_settext("bufstatus" , "  not needed yet, will save program-description...")
end function
function buttonsaveas()
    textview_settext("bufstatus" , "  not implemented yet, will save copy of project under new name...")
end function

function buttondotpup()
  textview_settext("bufstatus" , " ")
  if PROJECTOPENED = 0 then
    textview_settext("bufstatus" , " Error --> no project loaded or created from new! Nothing to package...")
    return
  end if
  shell("/usr/local/Dotpup-Wizard-mu06/dotpup-wizard \"" & OPENEDPROJECTBASE & "\" &")  
end function

function buttonopen ()

  fd = "/usr/local/Gtkbasic-002/dialog-multifilechooser/GBdialog-multifilechooser"

  textview_settext("bufstatus" , "  loading project...")
  result = xwin_system(fd & " 'Select a project' '" & HOME & "/" & ALLPROJECTSFOLDER  & "' '*.GBprj' '1' '0'" )
  //textview_settext(":bufstatus" , " ")


  result2 = xwin_system("test -f '" & trim(result[0]) & "' ; echo $?" )

  if trim(result2[0]) = "0" then
    thefile = trim(result[0])

    OPENEDPROJECTBASE = cutright(thefile , "/")
    OPENEDPROJECTEXEC = cutleftfromright(thefile , "/")
    OPENEDPROJECTEXEC = replace( OPENEDPROJECTEXEC  , ".GBprj" , "")
    OPENEDPROJECTEXEC = "\"" & OPENEDPROJECTBASE & "/start-" &  OPENEDPROJECTEXEC & "\""
    OPENEDPROJECT = "\"" & replace(thefile , ".GBprj" , "") & "\""

    OPENEDPROJECTGLADE = "\"" & OPENEDPROJECTBASE & "/resource/project.glade" & "\""
    PROJECTOPENED = 1
    textview_settext(":bufstatus" , "  loaded --> " & thefile)
  else

    //xwin_usleep(200000)
    textview_settext(":bufstatus" , " ")
    //textview_settext(":bufstatus" , "--------------")

  end if

//xwin_usleep(200000)
//gdk_flush()
end function

function textview_settext(thebuffer , thetext)
  //thetext = g_locale_to_utf8( thetext)


'  a = instr(thetext , ":")
//print a
'  if a > 0 then
'    thetext = left(thetext , a) & ":" & right(thetext , len(thetext)-a)
'  end if

  gtk("gtk_text_buffer_set_text" , thebuffer , thetext , -1, 0,0,0)

end function

function textview_appendtext(thebuffer , thetext)


 'thetext = replace(thetext , ":" , chr(58))

'  a = instr(thetext , ":")
'  if a > 0 then
'    thetext = left(thetext , a) & ":" & right(thetext , len(thetext)-a)
'  end if
 //thetext = g_locale_to_utf8( thetext)
// gtk("gtk_text_buffer_set_text" , thebuffer , thetext , len(thetext), 0,0,0)

        gtk("gtk_text_buffer_get_bounds", thebuffer , "1" , "2" , 0, 0,0 )
        gtk("gtk_text_buffer_place_cursor", thebuffer , "2" , 0 , 0, 0,0)
        'gtk("gtk_text_buffer_insert_at_cursor", thebuffer , thetext , "-1" , 0, 0,0)

       gtk("gtk_text_buffer_insert_interactive_at_cursor", thebuffer , thetext , "-1" , "1", 0,0)

//  gtk("gtk_text_buffer_insert_at_cursor", thebuffer , thetext , len(thetext) , 0, 0,0)
end function


 //--- load a textfile to a textbuffer
//
sub loadfiletobuffer(thefile , thebuffer)

  infolist = readfiletolist(thefile)


  for each theline in infolist

    textview_appendtext(thebuffer , " " & theline & "\n")
  next
  infolist = {}
end sub


sub checkclose()
	a=gins_get("window1" , "name" , "<str>")
	if a = Nothing then
	'	print "window was closed, exiting..."
	'	gins_exit()
	'	end
	end if

end sub

sub addfunctions()

	gtk_createobject("GtkBasicList" , "mylist1", "window1" , "scrolledwindow5")
	gtk("gtk_widget_show" , "mylist1" , 0 ,0,0,0,0)

	gtk("gtk_clist_set_column_title" ,"mylist1" , "0" , "available GTK-functions" , 0 , 0 , 0 ) 

	flist = readfiletolist(HERE & "/ginsfunctions.txt")

	n = 0
	for each theline in flist
		gtk("gtk_clist_append", "mylist1" , "" ,0,0,0,0)
		gtk("gtk_clist_set_text", "mylist1" , n , "0", theline,0,0)
		n += 1
	next

end sub

sub functionslist_clicked()
	'functionsentry1
	a = gtk_get_list_selection("mylist1")
	gtk("gtk_clist_get_text", "mylist1" , a , "0" , 0,0,0)
	b = gtk_returntext("3")
	d = ""
	if left(b , 4) = " a =" then
		d = "result = "
		b = replace(b , " a =" , "")
	end if
	b = "\"" & trim(b)
	b = replace(b , " (" , "\" , ")
	b = replace(b , " )" , "")

	b = replace(b , "li1->data" , "\"OBJECT\"")
	b = replace(b , "li2->data" , "\"OBJECT\"")
	b = replace(b , "li3->data" , "\"OBJECT\"")
	b = replace(b , "li4->data" , "\"OBJECT\"")
	b = replace(b , "li5->data" , "\"OBJECT\"")
	b = replace(b , "void" , "0")

	c = d & "gtk("  & b & ")"
	gins_set("functionsentry1" , "text" , c , "<str>")

	gtk("gtk_entry_select_region" , "functionsentry1"  , 0 , len(c) , 0 , 0 , 0)
end sub

