#!./ginsbasic001ok
include "wxbasicscript/basefunctions.inc"

//gtkdocdir = "/mnt/sda7/gtk/gtk/share/gtk-doc/html/"




allowedargs = {"gint" , "guint" , "gboolean" , "gdouble" , "gunichar" ,  "const gchar" , "gchar", 
"gfloat" , "gfloat*", "gpointer","gconstpointer",
"TextIter1" , "TextIter2" , "TextIter3" , 
"li1->data",
"li2->data",
"li3->data",
"li4->data",
"li5->data"
}

allowedwidgets = {"GtkBox",
"GtkLabel",
"GtkTextBuffer",
"GtkTextView",
"GtkButton",
"GtkWindow",
"GtkEntry",
"GtkWidget",
"GtkContainer",
"GtkDialog",
"GtkCheckButton",
"GtkFileFilter"
}
 ' "GtkFileChooser",
 ' "GtkFileChooserButton"


//-- some functions do not work at all, skip them

ignoredfunctions = {
"gtk_accelerator_set_default_mod_mask" ,
"gtk_drag_dest_set_track_motion" ,
"gtk_drag_dest_get_track_motion",
"gtk_file_chooser_button_get_focus_on_click",
"gtk_file_chooser_button_set_focus_on_click",
"gtk_window_set_default_icon_list",
"gtk_window_set_deletable",
"gtk_window_get_deletable",
"gtk_text_buffer_get_has_selection",
"gtk_clipboard_wait_is_rich_text_available",
"Completion*",
"gtk_widget_is_composited",
"gtk_init_add",
"gtk_accel_groups_from_object"
}

ignoredtypes = {
"GTK_ICON_INFO" ,
"GDK_EVENT" ,
"GDK_MODIFIER_TYPE",
"GTK_WINDOW_TYPE",
"GDK_COLOR",
"GTK_TEXT_DIRECTION",
"GTK_REQUISITION"
}

ignoredenums = {
"GtkWindowPosition" 
}

arg = {}
droppedfunctions = {}
droppedfunctionscounter = 0
usedfunctions = {}
usedfunctionscounter = 0

flinecounter = 0
ginsfunctions = {}


thefile = "gtkfunctions.txt"

html = readfiletolist(thefile)



c=0
done = 0
for each theline in html
theline = replace(theline , "const " , "")


theline = replace(theline , "gfloat *" , "gfloat* ")

    //print theline

    checkmore = 0


//-- glib values

    if left(theline , 4) = "void" then
      ret = "void"
      checkmore = 1
    end if
    if left(theline , 5) = "guint" then
      ret = "guint"
      checkmore = 1
    end if
    if left(theline , 4) = "gint" then
      ret = "gint"
      checkmore = 1
    end if
    if left(theline , 8) = "gboolean" then
      ret = "gboolean"
      checkmore = 1
    end if
    if left(theline , 12) = "const gchar*" then
      ret = "const gchar*"
      checkmore = 1
    end if
    if left(theline , 7) = "gdouble" then
      ret = "gdouble"
      checkmore = 1
    end if
    if left(theline , 6) = "gchar*" then
      ret = "gchar*"
      checkmore = 1
    end if


newobject = ""
      for each wid in allowedwidgets
        if left(theline , len(wid)) = wid then
          ret = wid
          newobject = "NEWOBJECT:"
          checkmore = 1
        end if
      next


    if checkmore = 1 then

      firstarg = "(obj)"

      f = cutleft(theline , ret)
      f = trim(f)
      if left(f , 1) = "*" then
        f = cutleft(f , "*")
        f = trim(f)
      end if
      functionname = cutrightfromleft(f , " ")

//-- some functions do not work at all, skip them
stopit = 0
for each ifunc in ignoredfunctions
  if functionname = ifunc then
    stopit = 1
  end if
next
if stopit = 1 then
  continue
end if

'  print  functionname

      arg[0] = ""
      arg[1] = ""
      arg[2] = ""
      arg[3] = ""
      arg[4] = ""
      arg[5] = ""

      args = cutleft(theline , "(")
      args = cutright(args, ")")
      args = trim (args)
      noargs = 0
      if (! instr(args , ",")) then
        noargs = 1
        arg[0] = args
      end if

      if noargs = 0 then

        arglist = explode(args , ",")

        for n = 0 to 5
          if  arglist[n] != Nothing then
            arg[n] = arglist[n]
	    'arg[n] = replace(arg[n] , "*" , "")
            arg[n] = trim(arg[n]) 
          end if
        next
      end if

    print ret, functionname, widget, arg[0], arg[1]
//-- enums do not work at all yet, remove those functions

skiprest = 0

if len(functionname) < 10 then
  skiprest = 1
  dobuild =0
end if

if skiprest = 0 then
  for n = 0 to 5  

    if arg[n] != "" then
      for each itype in ignoredenums
        if instr(arg[n] , itype) then
          dobuild = 0
	  skiprest = 1
        end if
      next
    end if
  next
end if
'    print ret, functionname, widget, arg[0], arg[1]

if skiprest = 0 then

itercounter = 1
for n = 0 to 5

      arg[n] = trim(arg[n])
      if instr(arg[n] , " ") then
        arg[n] = cutrightfromleft(arg[n] , " ")
      end if

if left(arg[n] , 11) = "GtkTextIter" then
  arg[n] = "TextIter" & itercounter
  itercounter += 1
end if

    if n = 0 then
      for each wid in allowedwidgets
        if left(arg[n] , len(wid)) = wid then
          arg[n] = upconvert(arg[n])
          'arg[n] &= "(obj)"
        end if
      next
    else
      for each wid in allowedwidgets
        if left(arg[n] , len(wid)) = wid then
          arg[n] = "li" & n & "->data"
        end if
      next
    end if

      if left (arg[n] , 3) = "Gtk" or left (arg[n] , 3) = "Gdk" then
        arg[n] = upconvert(arg[n])
      end if


	// glib stuff :-/
	//arg[n] = replace(arg[n] , "G_S_LIST" , "GS_LIST")
//arg[n] = replace(arg[n] , "G_S_LIST" , "(obj)")
      arg[n] = trim(arg[n])
next

function upconvert(thestring)

  newwidget = ""
  for x = 1 to len(thestring)

    a = mid(thestring , x , 1) 
    b = ucase (a)
    if a = b then
      if x>1 then
        newwidget = newwidget & "_" & a
      else
        newwidget = newwidget & a
      end if
    else
      newwidget = newwidget &  b 
    end if
  next

  return ( newwidget )

end function





'      print ret, functionname, widget, arg[0], arg[1]


     //-- some argumenttypes are incompatible, so skip those functions
      dobuild = 0

if arg[0] = "" then
if arg[1] = "" then
if arg[2] = "" then
if arg[3] = "" then
if arg[4] = "" then
if arg[5] = "" then
      dobuild = 1
end if
end if
end if
end if
end if
end if



if dobuild = 0 then
  dobuild = 1
  for n = 1 to 5  // ignore first arg, as it is detected by gins
    if arg[n] != "" then
      dobuild = 0
      for each argtype in allowedargs
      
        if left(arg[n] , len(argtype)) = argtype then
          dobuild = 1
        end if
      next
      if instr(arg[n] , "->data") then
        dobuild = 1


      end if
    end if
'print "----  " & functionname & " --" & arg[n]& "-- " & dobuild
  next
end if
'print "--  " & functionname & " -- " & dobuild

//-- rough method, removes all functions with GTK/GDK objects after the first widget
//-- this works with many unknown functions
//-- however it would be more accurate to add disturbing functions manually to the excludelist

if dobuild = 1 then

  for n = 1 to 5  // ignore first arg, as it is detected by gins

    if arg[n] != "" then
        if left(arg[n] , 4) = "GTK_" or left(arg[n] , 4) = "GDK_" then
          dobuild = 0

        end if
    end if
  next
end if


//-- some types do not work at all, remove those functions

if dobuild = 1 then

  for n = 0 to 5  

    if arg[n] != "" then
      for each itype in ignoredtypes
        if arg[n] = itype then
          dobuild = 0
        end if
      next
    end if
  next
end if

end if // skiprest

     //-- build the final C-Code
      if dobuild = 1 then

        buildfunction()
formatoutput()
        usedfunctions[usedfunctionscounter] = ret & " " & functionname & " ( " & arg[0] & arg[1] & arg[2] & arg[3] & arg[4] & arg[5] & " ) " 
        usedfunctionscounter += 1
      else
formatoutput()
        droppedfunctions[droppedfunctionscounter] =  ret & " " & functionname & " ( " & arg[0] & arg[1] & arg[2] & arg[3] & arg[4] & arg[5] & " ) " 

        droppedfunctionscounter += 1
        //print "  DROPPING: " & functionname
        //print
      end if
    end if


  c+=1
next

writelisttofile("ginsfunctions.txt" , usedfunctions)
for each used in usedfunctions
  print "ok: " & used
next

for each dropped in droppedfunctions
  print "dropped: " &  dropped
next

ginshead = readfiletolist("gins.c-header")
writelisttofile("gins.c" , ginshead)
ginshead = {}
appendlisttofile("gins.c" , ginsfunctions)
writelisttofile("gins.c-body" , ginsfunctions)
ginsfunctions = {}
ginsfoot = readfiletolist("gins.c-footer")
appendlisttofile("gins.c" , ginsfoot)
ginsfoot = {}

function buildfunction()

  ginsfunctions[flinecounter] = "  if (!strcmp( thefunction , \"" &  functionname & "\")) {"
  flinecounter += 1

  if ret = "void" then

        ginsfunctions[flinecounter] = "    " & functionname & "( " 

    cleanarguments()

    ginsfunctions[flinecounter] &= " );"
    flinecounter += 1

    ginsfunctions[flinecounter] = "    returnstring(\"Nothing\");"
    flinecounter += 1

  else

    resulttype = "resultbuffer"
    returnstring = "    sprintf(result , \"%s\" , " & resulttype & ");"

    if ret = "gboolean" then
      resulttype = "resultgboolean"
      returnstring = "    sprintf(result , \"%d\" , " & resulttype & ");"
    end if
    if ret = "guint" then
      resulttype = "resultguint"
      returnstring = "    sprintf(result , \"%d\" , " & resulttype & ");"
    end if
    if ret = "gint" then
      resulttype = "resultgint"
      returnstring = "    sprintf(result , \"%d\" , " & resulttype & ");"
    end if
    if ret = "gdouble" then
      resulttype = "resultgdouble"
      returnstring = "    sprintf(result , \"%f\" , " & resulttype & ");"
    end if

    if newobject = "NEWOBJECT:" then
      
      returnstring = "  createnewreturnobject(\"NEWOBJECT\" , \"" & ret & "\");\n"
      returnstring &= "    sprintf( result , \"Nothing\");"
      resulttype = "GO[GOcount].obj"

    end if

      ginsfunctions[flinecounter] = "    " & resulttype & " = " & functionname & "( "


    cleanarguments()

    ginsfunctions[flinecounter] &= " );"
    flinecounter += 1

    ginsfunctions[flinecounter] = returnstring
    flinecounter += 1
    ginsfunctions[flinecounter] = "    returnstring(result);"
    flinecounter += 1

  end if

   ' ginsfunctions[flinecounter] = "    free(resultbuffer);"
   ' flinecounter += 1
    ginsfunctions[flinecounter] = "    return;"
    flinecounter += 1
    ginsfunctions[flinecounter] = "  }"
    flinecounter += 1


end function

function cleanarguments()

    //-- this function converts some argumenttypes and builds the string for gins.c

for n=0 to 5

    filler = " , "
    if n=0 then
      filler = " "
    end if

    if arg[n] != "" then


      convertedarg = 0

      if left(arg[n] , 7) = "gdouble" then
        ginsfunctions[flinecounter] &= filler & "atof(arg" & n & ") "
        convertedarg = 1
      end if 

      if left(arg[n] , 4) = "gint" then
        ginsfunctions[flinecounter] &= filler & "atoi(arg" & n & ") "
        convertedarg = 1
      end if 

        if left(arg[n] , 5) = "guint" then
          ginsfunctions[flinecounter] &= filler & "atoi(arg" & n & ") "
          convertedarg = 1
        end if 

      if left(arg[n] , 8) = "gboolean" then
        ginsfunctions[flinecounter] &= filler & "atoi(arg" & n & ") "
        convertedarg = 1
      end if 


      if left(arg[n] , 6) = "gfloat" then
        if left(arg[n] , 7) != "gfloat*" then   

          ginsfunctions[flinecounter] &= filler & "atof(arg" & n & ") "
          convertedarg = 1
        else

          ginsfunctions[flinecounter] &= filler & "arg" & n & " "
          convertedarg = 1
          end if 
      end if

      if left(arg[n] , 5) = "gchar" then
        ginsfunctions[flinecounter] &= filler & "arg" & n & " "
        convertedarg = 1
      end if 

      if left(arg[n] , 8) = "gpointer" then
        ginsfunctions[flinecounter] &= filler & " arg" & n & " "
        convertedarg = 1
      end if 

      if instr(arg[n] , "->data") then
        ginsfunctions[flinecounter] &= filler &  arg[n] & " "
        convertedarg = 1
      end if 

      if left(arg[n] , 8) = "TextIter" then
        ginsfunctions[flinecounter] &= filler & " &TextIters[ atoi(arg" & n & ") ].Iter "
        convertedarg = 1
      end if 

//      if left(arg[n] , 8) = "G_S_LIST" then
//        ginsfunctions[flinecounter] &= filler & " obj "
//        convertedarg = 1
//      end if 

      if convertedarg = 0 then

        if n=0 then
          if arg[0] = "void" then

          else
              ginsfunctions[flinecounter] &= filler & arg[n] & "(obj)"
          end if 
       else
            ginsfunctions[flinecounter] &= " , arg" & n & " "
        end if 
     end if



    end if
next
end function

function formatoutput()


if arg[0] = "" then
  arg[0] = "0"
end if

if arg[1] = "" then
  arg[1] = "0"
end if
arg[1] = " , " & arg[1]
if arg[2] = "" then
  arg[2] = "0"
end if
arg[2] = " , " & arg[2]
if arg[3] = "" then
  arg[3] = "0"
end if
arg[3] = " , " & arg[3]
if arg[4] = "" then
  arg[4] = "0"
end if
arg[4] = " , " & arg[4]
if arg[5] = "" then
  arg[5] = "0"
end if
arg[5] = " , " & arg[5]

if ret = "void" then
  ret = ""
else
  ret = " a = "
end if

end function
