#include Once "window9.bi"

#ifndef disable_webgadget
	
	Extern "C"
		
		Declare Function webkit_web_view_new() As gtkwidget Ptr 
		
		Declare Sub webkit_web_view_load_uri(web_view As  Any Ptr,  uri As gchar Ptr)	
		
		Declare Sub webkit_web_view_load_html(web_view As  Any Ptr , text As gchar Ptr , base_uri As gchar Ptr = 0)
		
		Declare Sub webkit_web_view_load_html_string(web_view As  Any Ptr , text As gchar Ptr , base_uri As gchar Ptr = 0)
		
		Declare Sub webkit_web_view_reload_bypass_cache(web_view As  Any Ptr) 
		
		Declare Function webkit_web_view_get_uri(web_view As  Any Ptr) As gchar Ptr 
		
		Declare Function webkit_web_view_can_go_back(web_view As  Any Ptr) As gboolean
		
		Declare Sub webkit_web_view_go_back(web_view As  Any Ptr)
		
		Declare Function webkit_web_view_can_go_forward(web_view As  Any Ptr) As gboolean 
		
		Declare Sub webkit_web_view_go_forward(web_view As  Any Ptr) 
		
		Declare Function webkit_web_view_is_loading(web_view As  Any Ptr) As gboolean
		
		Declare Function webkit_web_view_get_load_status (web_view As  Any Ptr) As Long
		
		Declare Function webkit_web_resource_get_data(resource As Any Ptr) As String Ptr
		
		Declare Function webkit_web_view_get_main_resource(web_view As  Any Ptr) As Any Ptr	
		
	End Extern	
	
	
	Sub Webgadgetnavigate(pBrowser As Hwnd , sUrl As String) Export
		
		webkit_web_view_load_uri(Cast(Any Ptr,pBrowser) , Strptr(sUrl))
		
	End Sub
	
	Sub Webgadgetsetbody(pBrowser As Hwnd , sHtml As String) Export
		
		#ifdef __USE_GTK3__
			
			webkit_web_view_load_html(Cast(Any Ptr,pBrowser) , Strptr(sHtml) )
			
		#else
			
			webkit_web_view_load_html_string(Cast(Any Ptr,pBrowser) , Strptr(sHtml) )
			
		#endif	
		
	End Sub
	
	Sub Webgadgetrefresh(pBrowser As Hwnd) Export
		
		webkit_web_view_reload_bypass_cache(pBrowser)
		
	End Sub
	
	Function Webgadgetgeturl(pBrowser As Hwnd) As String Export
		
		Return *webkit_web_view_get_uri(pBrowser)
		
	End Function
	
	Function Webgadgetstate(pBrowser As Hwnd) As gboolean Export
		
		#ifdef __USE_GTK3__
			
			Return webkit_web_view_is_loading(pBrowser)
			
		#else
			
			If webkit_web_view_get_load_status(pBrowser) = 2 Then
				
				Return False
				
			Else
				
				Return True
				
			Endif
			
		#endif
		
	End Function
	
	Sub Webgadgetgoback(pBrowser As Hwnd) Export
		
		If webkit_web_view_can_go_back(pBrowser) Then
			
			webkit_web_view_go_back(pBrowser)
			
		Endif
		
	End Sub
	
	Sub Webgadgetgoforward(pBrowser As Hwnd) Export
		
		If webkit_web_view_can_go_forward(pBrowser) Then
			
			webkit_web_view_go_forward(pBrowser)
			
		Endif
		
	End Sub
	
	Function Webgadgetgetbody(pBrowser As Hwnd ,iFlag As Long = 0) As String Export
		
		#ifndef __USE_GTK3__
			
			Dim As String Ptr bBuf = webkit_web_resource_get_data(webkit_web_view_get_main_resource(pBrowser))
			
			If bBuf = 0 Then
				
				Return ""
				
			Else
				
				Return *bBuf 
				
			Endif 
			
		#else
			
			Print !"function WebGadgetGetBody does not work in GTK3 . However, you can use the code that can return data HTML:\n\n" _
			!"enum\n"_
			!"\tWEBKIT_LOAD_STARTED\n"_
			!"\tWEBKIT_LOAD_REDIRECTED\n"_
			!"\tWEBKIT_LOAD_COMMITTED\n"_
			!"\tWEBKIT_LOAD_FINISHED\n"_	
			!"End Enum\n\n"_
			!"sub  getdatafunc cdecl(source_object as Gobject ptr, res as GAsyncResult ptr, user_data as gpointer )\n"_	
			!"\tdim as GError ptr gerr\n"_
			!"\tdim as gsize G_size\n"_ 
			!"\tdim as guchar PTR pBuff = webkit_web_resource_get_data_finish(source_object , res , @G_size,   @gerr)\n"_		
			!"\t? *cast(zstring ptr , pBuff)\n"_
			!"End Sub\n\n"_
			!"sub load_changes cdecl (web_view as any ptr, load_event as long, user_data as gpointer)\n"_	
			!"\tif load_event = WEBKIT_LOAD_FINISHED then\n"_   
			!"\t\tdim as any ptr resource = webkit_web_view_get_main_resource(web_view)\n"_
			!"\t\twebkit_web_resource_get_data(resource , 0 , @getdatafunc , 0)\n"_
			!"\tendif\n"_
			!"End Sub\n\n"_
			!"g_signal_connect(G_OBJECT(web_view), ""load-changed"", G_CALLBACK (@load_changes), 0)\n"
			
			Return ""
			
		#endif	
		
	End Function
	
	Function Webgadget( iGadget As Long , x As Long , y As Long , width_ As Long , height_ As Long , sTextUrl As String = " " , style As Long = 0 , tag As Long = 0) As HWND Export
		
		Dim As HWND scroll , web_view , vbox , mainBox
		
		Dim As ListT Ptr pListTemp
		
		Freegadget(iGadget)
		
		pGlobalTypeWindow9->OptGroup = 0 ' reset Group OptionGadget
		
		vbox = gtk_fixed_new()
		
		pListTemp = Cast(ListT Ptr,pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cint(pGlobalTypeWindow9->CurentHwnd)))
		
		mainBox = Cast(hwnd , pListTemp->anyTwoData)
		
		gtk_container_add (GTK_CONTAINER(mainBox), vbox)
		
		web_view = webkit_web_view_new ()
		
		tree_add_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)
		
		Dim As Node Ptr fNode = tree_find_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)
		
		gtk_widget_show(vbox)
		
		gtk_widget_show(web_view)
		
		#ifdef __USE_GTK3__
			
			gtk_fixed_put(GTK_FIXED(vbox), web_view, x , y)
			
			gtk_widget_set_size_request(web_view, width_, height_)	
			
			'g_signal_connect(G_OBJECT(web_view), "load-changed", G_CALLBACK (@load_changes), @(fNode->x))
			
			g_signal_connect(G_OBJECT(web_view), "size-allocate", G_CALLBACK (@size_allocate_signals), @(fNode->x))
			
		#else
			
			scroll = gtk_scrolled_window_new (NULL, NULL)
			
			gtk_scrolled_window_set_shadow_type(Cast(Any Ptr,scroll) , style)
			
			gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC)
			
			gtk_fixed_put(GTK_FIXED(vbox), scroll, x , y)
			
			gtk_widget_set_size_request(scroll, width_, height_)	
			
			gtk_container_add (GTK_CONTAINER (scroll), web_view)
			
			'g_signal_connect(G_OBJECT(web_view), "load-progress-changed", G_CALLBACK (@load_progress_changes), @(fNode->x))
			
			g_signal_connect(G_OBJECT(scroll), "size-allocate", G_CALLBACK (@size_allocate_signals), @(fNode->x))
			
			gtk_widget_show(scroll)	
			
		#endif
		
		g_object_set_data (Cast(Any Ptr, web_view), "X_SIZE" , Cast(Any Ptr,Cint(x)))
		
		g_object_set_data (Cast(Any Ptr, web_view), "Y_SIZE" , Cast(Any Ptr,Cint(y)))
		
		g_object_set_data (Cast(Any Ptr, web_view), "W_SIZE" , Cast(Any Ptr,Cint(width_)))
		
		g_object_set_data (Cast(Any Ptr, web_view), "H_SIZE" , Cast(Any Ptr,Cint(height_)))	
		
		pGlobalTypeWindow9->ListGadgets->addNodeSub(iGadget , Cint(web_view) , 17)
		
		If sTextUrl = " " Then		 
			
			webkit_web_view_load_uri(web_view, @"about:blank")
			
		Else
			
			webkit_web_view_load_uri(web_view, sTextUrl)
			
		Endif
		
		Return web_view
		
	End Function
	
#endif