#!/usr/bin/gtkbasic002

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

ALLPROJECTSFOLDER = "Gtkbasic002-projects"

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

HERE = xwin_getdir(command(2)) 
xwin_cd(HERE)

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

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

	gtk("gtk_text_view_get_buffer" ,"textview4" ,  0 , 0, 0,0,0)
	gtk_setobjectname("buf1")

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

gins_main()



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

// grouping radiobuttons crashes glade and gins... so handle them further down with events
//gins_set("radiobutton2" , "group" , "radiobutton1" , "<str>")

//-- 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 hndled 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 left(widget , 3)="fc_" then
        chose_file(widget)
      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="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>")
  //gtk("gtk_widget_hide" , "newdialogwindow",0,0,0,0,0)

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

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

  textview_settext("textview4" , "finished, created new project! current project is --> "  & projectname)


  PROJECTOPENED = 1

end function  

function buttonrun ()

  if PROJECTOPENED = 0 then
    textview_settext("textview4" , "Error:: no project loaded or created from new!")
    return
  end if
  shell( OPENEDPROJECT & " &")

end function

function buttonnew ()

  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 buttonopen ()
  gdk_flush()
  file_chooser("open project" , HOME & "/" & ALLPROJECTSFOLDER , "*" , "3" , "0")

end function

function chose_file (widget)

  mylist = file_chooser_event(widget)
  gins_set(fc , "visible" , "FALSE" , "<boolean>")
  print mylist
end function


function textview_settext(thetextview , thetext)

  'thetext = replace(thetext , ":" , "U+0x3A")



  gtk("gtk_text_buffer_set_text" ,":buf1" , thetext , -1, 0,0,0)
return
	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

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

