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


//-- lets begin to check what enums we have

enums = readfiletolist("gins-enums.txt")
enums2 = {}

n = 0
for each enum in enums

	enums2[n] = enum
	enums2[n+1] = convert_arg_to_upper(enum)

	n +=2
next

//-- now process the functions

writestringtofile("gins.c-body" , "")

thefile = "gtkfunctions.txt"

html = readfiletolist(thefile)

references = {}
refcounter = 0

for each theline in html

	referencelines = {}
	referencelinescounter = 0

	// objectwrapper for C textarrays
	theline = replace(theline , "gchar *text[]" , "gtkbasicgchararray")

	if left(theline , 6) = "const " then
		theline = cutleft(theline , " ")
	end if

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

	//-- split the Gtk-function into pieces ----------------------------
	optiontypes = {}
	optionnames = {}

	returnvalue = cutrightfromleft(theline , " ")
	functionname = cutleft(theline , " ")
	functionname = replace(functionname , "( " , "")
	functionname = cutrightfromleft(functionname , " ")
	optionline = cutleftfromright(theline , "(")
	optionline =  cutright(optionline , ")")
	print theline
	print returnvalue
	print functionname 
	print optionline
	if instr(optionline , ",") then
		options = explode(optionline , ",")
	else
		options = {}
		options[0] = trim(optionline)
	end if
	optioncount = count(options)-1
	n = 0
	for each theoption in options
		theoption = trim(theoption)
		print "   " & theoption
		if instr(theoption , " ") then
			optiontype = cutright(theoption , " ")
			optionname = cutleft(theoption , " ")
			print "      type: " & optiontype
			print "      name: " & optionname
		else
			optiontype = theoption // usually void
			optionname = ""
		end if
		optiontypes[n] = trim(optiontype)
		optionnames[n] = trim(optionname)
		n += 1 
	next


	//-- build the start of the GtkBasic-function ----------------------------

        GtkBasic_firstline = "if (!strcmp( thefunction , \"" & functionname & "\")) {"
print "---> " & GtkBasic_firstline


	GtkBasic_secondline = functionname & " ("
	GtkBasic_thirdline = "returnstring(\"Nothing\");"
	if is_string(returnvalue) then
		GtkBasic_secondline = "resultbuffer = " &  functionname & " ("
		GtkBasic_thirdline = "sprintf(result , \"%s\" , resultbuffer);returnstring(result);"
	end if
	if is_int(returnvalue) then
		GtkBasic_secondline = "resultgint = " &  functionname & " ("
		GtkBasic_thirdline = "sprintf(result , \"%d\" , resultgint);returnstring(result);"
	end if
	if is_object(returnvalue) then
		GtkBasic_secondline = "GO[GOcount].obj = " &  functionname & " ("
		//-- returnvalue is currently unused (widgettype)
		GtkBasic_thirdline = "createnewreturnobject(\"NEWOBJECT\" , \"" & returnvalue & "\");returnstring(\"Nothing\");"
	end if

	if is_enum(returnvalue) then
		GtkBasic_secondline = "resultgint = " &  functionname & " ("
		GtkBasic_thirdline = "sprintf(result , \"%d\" , resultgint);returnstring(result);"
	end if

SKIP=0
	//-- now deal with the options ... ----------------------------
	if optiontypes[0] != "void" then
	for n = 0 to count(optiontypes)-1

		//-- first argument usually is a widget
		// toadd: is it a reference
		if n = 0 then

			arg = convert_arg_to_upper (optiontypes[n])

			arg = replace( arg , "GCHAR" , "" )
			arg = replace( arg , "GPOINTER" , "" )
			//arg = replace( arg , "GDK_EVENT" , "" )
			arg = replace( arg , "GTK_FUNCTION" , "" )
			arg = replace( arg , "G_LIST" , "" )
			arg = replace( arg , "GTK_ICON_INFO" , "" )
			arg = replace( arg , "GTK_REQUISITION" , "" )
			arg = replace( arg , "GTK_ICON_SET" , "" )
			arg = replace( arg , "G_S_LIST" , "" )
			arg = replace( arg , "GTK_NOTEBOOK_WINDOW_CREATION_FUNC" , "" )
			arg = replace( arg , "G_TYPE" , "" )
			'arg = replace( arg , "" , "" )

			// default: gchar
			firstoption =" arg" & n & " "

			if is_int(arg) then
				firstoption = " atoi(arg" & n & ") "
			end if
			if is_float(arg) then
				firstoption = " atof(arg" & n & ") "
			end if

			if is_object(arg) then
				'if is_enum_or_int(arg) then
				'	firstoption = " atoi(arg" & n & ") "
				'else
					firstoption = " " & arg & "(obj) "
				'end if
			end if

			// enums, must find a mechanism to access them later
			if is_enum(arg) then

				firstoption = " atoi(arg" & n & ") "
				'SKIP = 1
			end if
			if arg = "GTK_TEXT_ITER" then
				firstoption = " &TextIters[ atoi(arg" & n & ") ].Iter "
			end if

			GtkBasic_secondline &= firstoption
		else

			//-- the other arguments

			optionstuff = ", arg" & n & " "
			// check for integer
			if is_int(optiontypes[n]) then
				optionstuff = ", atoi(arg" & n & ") "
			end if

			if is_float(optiontypes[n]) then
				optionstuff = ", atof(arg" & n & ") "

				// pointer to floats
				if instr(optionnames[n] , "*") then
					optionstuff = ", &gf" & n & " "
					//must add a way to return these pointers, uuhh.. ok, done

					referencelines[referencelinescounter] = "sprintf( pointervalues[" & n & "].retpoint , \"%f\" , gf" & n & ");"
					referencelinescounter +=1

				end if
			end if
			if is_object(optiontypes[n]) then
				if is_enum_or_int(optionnames[n]) then
					optionstuff = ", atoi(arg" & n & ") "
				else
					optionstuff = ", li" & n & "->data "
				end if
			end if

			// custom objectwrappers
			if optiontypes[n] = "gtkbasicgchararray" then
				optionstuff = ", emptytextarray" & n & " "
			end if

			if optiontypes[n] = "GtkTextIter" then
				optionstuff = ", &TextIters[ atoi(arg" & n & ") ].Iter "
			end if

			// enums not supported yet
			if is_enum(optiontypes[n]) then
				optionstuff = ", atoi(arg" & n & ") "
		'		SKIP = 1
			end if


			if is_reference(optionnames[n]) then
				// this is e.g. gtk_clist_get_text
				if is_string(optiontypes[n]) then
					optionstuff = ", &returntext[" & n & "].text "
				end if

				//this creates new objects, as some functions return only pointers
				if is_object(optiontypes[n]) then

					references[refcounter] = optiontypes[n] & " *x" & optiontypes[n] & n & ";"
					refcounter += 1

					referencelines[referencelinescounter] = "GO[GOcount].obj = x" & optiontypes[n] & n & ";"
					referencelinescounter +=1
					referencelines[referencelinescounter] = "createnewreturnobjectnosignal(arg" & n & ");"
					referencelinescounter +=1

					optionstuff = ", &x" & optiontypes[n] & n & " "

				end if
			end if

			GtkBasic_secondline &= optionstuff
		end if


	next
	end if
	GtkBasic_secondline = replace( GtkBasic_secondline , "(," , "(")
	GtkBasic_secondline &= ");"

	//-- some types for first arg must be removed from type(obj) to only (obj)-----------------

	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PAPER_SIZE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PRINTER(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PRINTER_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PRINT_JOB(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PRINT_UNIX_DIALOG(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_PAGE_SETUP_UNIX_DIALOG(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TYPE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_RECENT_INFO(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "(" , "(" )


	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_COLOR_SELECTION_CHANGE_PALETTE_WITH_SCREEN_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_COLOR_SELECTION_CHANGE_PALETTE_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_ABOUT_DIALOG_ACTIVATE_LINK_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_SELECTION_DATA(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TARGET_LIST(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TARGET_ENTRY(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_BINDING_SET(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_ICON_SOURCE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_RGB_CMAP(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_POINT(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_RECTANGLE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_REGION(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_W_CHAR(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_NATIVE_WINDOW(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_CURSOR(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_FONT(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_BITMAP(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TREE_PATH(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TREE_ROW_REFERENCE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TREE_ITER(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_KEY_SNOOP_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_GEOMETRY(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_POINTER_HOOKS(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_EVENT_FUNC(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_TIME_COORD(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_TEXT_ATTRIBUTES(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_ATOM(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_DISPLAY(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_COLOR(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GDK_VISUAL_TYPE(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_BORDER(" , "(" )
	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_FILE_FILTER(" , "(" )

	//-- some typenames must be corrected ----------------------------

	GtkBasic_secondline = replace( GtkBasic_secondline , "GTK_C_LIST" , "GTK_CLIST" )


	//-- print final function to file ----------------------------

print "---> " & GtkBasic_secondline
for each refoutline in referencelines
	print "....  " & refoutline
next
print "---> " & GtkBasic_thirdline
print "---> " & "gdk_threads_leave();LOCK=0;g_static_mutex_unlock (&mutex);return;"
print "---> " & "}"

//-- here I can interrupt the script to see the result for a specific function
'if instr( GtkBasic_firstline , "gtk_clist_get_text") then
'if instr( GtkBasic_firstline , "gtk_window_new") then
'	end
'end if

if SKIP = 0 then
	newfunction = {}
	offset = 0
	newfunction[0] = GtkBasic_firstline
	newfunction[1] = "  " & GtkBasic_secondline
	for each refoutline in referencelines
		newfunction[2+offset] = refoutline
		offset += 1
	next
	newfunction[2+offset] = "  " & GtkBasic_thirdline
	newfunction[3+offset] = "  " & "gdk_threads_leave();LOCK=0;g_static_mutex_unlock (&mutex);return;"
	newfunction[4+offset] = "}"

	// GtkBasic currently only accepts 1 + 5 args 
	if count(optiontypes) < 7 then

			appendlisttofile("gins.c-body" , newfunction)

	end if
end if
next

natsort(references)
newrefs = reducelist(references)
print newrefs
writelisttofile( "gins-references.c", newrefs)

//-------------------------------------------------------- tests for returntypes
function is_enum(thetype)


	'enums2 = {"GdkVisualType" ,"GtkImageType", "GDK_MODIFIER_TYPE" , "GTK_TEXT_DIRECTION" ,"GDK_IMAGE_TYPE",  "GTK_WINDOW_TYPE" }
	for each theenum in enums2
		if thetype = theenum then
			return 1
		end if
	next
	return 0
end function

function is_string(thetype)

	strings = {"gchar","gchar*" }
	for each thestring in strings
		if lcase(thetype) = thestring then
			return 1
		end if
	next
	return 0
end function

function is_int(thetype)

	integers = {"int" , "gint" , "guint" ,"gulong", "gboolean" , "gdouble" }
	for each theinteger in integers
		if lcase(thetype) = theinteger then
			return 1
		end if
	next
	return 0
end function


function is_float(thetype)

	floats = {"gfloat" }
	for each thefloat in floats
		if lcase(thetype) = thefloat then
			return 1
		end if
	next
	return 0
end function

function is_object(thetype)

	if lcase(left (thetype , 3)) = "gtk" then
		return 1
	end if
	if lcase(left (thetype , 3)) = "gdk" then
		return 1
	end if

	return 0
end function

function is_enum_or_int(thetype)

	if instr (thetype , "*") then
		return 0
	end if

	return 1
end function

function is_reference(thetype)

	if instr (thetype , "**") then
		return 1
	end if

	return 0
end function

function convert_arg_to_upper(arg)

	if len(arg) = 0 then
		return ""
	end if
	newarg = ""
	for i = 1 to len(arg)
		thechar = mid(arg , i , 1)
		uchar = ucase(thechar)
		if thechar = uchar then
			if i > 1 then
				thechar = "_" & uchar
			else
				thechar = uchar
			end if
		else
			thechar = uchar
		end if
		newarg &= thechar
	next
	if newarg ="GDK_G_C" then
		newarg ="GDK_GC"
	end if

 	if newarg ="GTK_U_I_MANAGER" then
		newarg ="GTK_UI_MANAGER"
	end if
	if newarg ="GTK_I_M_CONTEXT_SIMPLE" then
		newarg ="GTK_IM_CONTEXT_SIMPLE"
	end if
	if newarg ="GTK_I_M_CONTEXT" then
		newarg ="GTK_IM_CONTEXT"
	end if
	if newarg ="GTK_I_M_MULTICONTEXT" then
		newarg ="GTK_IM_MULTICONTEXT"
	end if

	return newarg
end function
