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

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


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 = ""
  OPENEDPROJECTGLADE = ""

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

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

gins_main()

// -- 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("info1.txt" , ":buf1")
loadfiletobuffer("info2.txt" , ":buf2")
loadfiletobuffer("info3.txt" , ":buf3")
loadfiletobuffer("info4.txt" , ":buf4")

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



//-- mainloop with eventhandling
while 1

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


  if widget != Nothing 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
    print widget
   ' print event
   ' print pdata

      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 " & OPENEDPROJECT & " &")
      end if
      if widget="buttongtkdoc" then
        shell("defaultbrowser http://gtk.org/api &")
      end if

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



  end if
  xwin_usleep(10000)
wend

function  newdialogbuttoncancel()

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

end function  


function newdialogbutton1()

  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 , " " , "_")

  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 & "/\"*"
  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 & projectname2 & "\"")
  xwin_system("mv " & "\"" & pb & "project.GBprj\" \"" & pb & projectname2 & ".GBprj\"")
  OPENEDPROJECTBASE = pb
  OPENEDPROJECT = "\"" & pb & projectname2 & "\""
  OPENEDPROJECTGLADE = "\"" & OPENEDPROJECTBASE & "/resource/project.glade" & "\""

//    textview_settext(":bufstatus" , "  finished, created new project!   current project is: " & projectname)

  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( OPENEDPROJECT & " &")

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>")

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"
  result = xwin_system(fd & " 'Select a project' '" & HOME & "/" & ALLPROJECTSFOLDER  & "' '*.GBprj' '1' '0'" )

  textview_settext(":bufstatus" , "  loading project...")

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

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

    OPENEDPROJECTBASE = cutright(thefile , "/")
    OPENEDPROJECT = "\"" & replace(thefile , ".GBprj" , "") & "\""
    OPENEDPROJECTGLADE = "\"" & OPENEDPROJECTBASE & "/resource/project.glade" & "\""
    PROJECTOPENED = 1
    textview_settext(":bufstatus" , "  loaded --> " & thefile)
  else
    textview_settext(":bufstatus" , " ")
  end if
end function

function textview_settext(thebuffer , thetext)

  // BUG: ":" often crashes the textview, so replace it until solution was found
  thetext = replace(thetext , ":" , "-->")

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

return

// unused
	gtk("gtk_text_view_get_buffer" ,thetextview ,  0 , 0, 0,0,0)
	gtk_setobjectname("buf1")
        gtk("gtk_text_buffer_get_bounds",":buf1" , "1" , "2" , 0, 0,0 )
        gtk("gtk_text_buffer_delete", ":buf1" , "1" , "2" , "1", 0,0)
        gtk("gtk_text_buffer_place_cursor", ":buf1" , "1" , 0 , 0, 0,0)
        gtk("gtk_text_buffer_insert_at_cursor", ":buf1" , thetext , "-1" , 0, 0,0)

end function

 //--- load a textfile to a textbuffer
//
sub loadfiletobuffer(thefile , thebuffer)
  info = ""
  infolist = readfiletolist(thefile)
  for each theline in infolist
    info &= theline & "\n"
  next
  infolist = {}
  textview_settext(thebuffer , info)
  info = ""
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

