gdk_threads_leave();
//GiNsTaBlE1EnD

gdk_flush();LOCK=0;

g_static_mutex_unlock (&mutex);
	returnstring("Nothing");
	free(resultbuffer);
	return;
}


void wBuiltin_gins_glade(){

gdk_threads_enter();

	char *thefile;
	wVariant *variant;
	variant = wStackPopString();
    	thefile = wVariantToChar( variant, W_FALSE, 0 );
	loadXMLFile(thefile);
	wVariantDeref( variant );
gdk_threads_leave();
}
void wBuiltin_gins_set(){

 //   LOCK = 1;
    static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
    g_static_mutex_lock (&mutex);
gdk_threads_enter ();
	gint intval;
	gboolean bvalue = TRUE;
	gdouble doubleValue;
	GtkWidget *obj;

	char *wstring;

	char objid[255];
	char name[255];
	char value[255];
	char type[255];

	wVariant *variant;

	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(type , "%s" , wstring);

	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(value , "%s" , wstring);

	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(name , "%s" , wstring);

	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(objid , "%s" , wstring);

	wVariantDeref( variant );
	free(wstring);

	obj = glade_xml_get_widget(xml, objid);


	if (strncmp(type,"<uposition>", 5)==0) {
		intval = atoi(value); 
		gint intval2 = atoi(name);
	       	gtk_widget_set_uposition(obj, intval2,intval );
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}

	if (strncmp(type,"<int>", 5)==0) {
		intval = atoi(value); 
		
		g_object_set (obj,name, intval, NULL);
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}
	if (strncmp(type,"<str>", 5)==0) {
		g_object_set (obj,name, &value, NULL);
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}
	if (strncmp(type,"<boolean>", 5)==0) {
		if (strncmp(value,"FALSE",5)==0) {
			bvalue = FALSE;
		}
		g_object_set (obj,name, bvalue, NULL);
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}
	if (strncmp(type,"<bool>", 5)==0) {
		if (strncmp(value,"FALSE",5)==0) {
			bvalue = FALSE;
		}
		g_object_set (obj,name, bvalue, NULL);
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}
	if (strncmp(type,"<double>", 5)==0) {
		doubleValue = strtod(value, NULL); 
		g_object_set (obj,name, doubleValue , NULL);
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
		return;
	}
gdk_threads_leave ();
LOCK=0;
g_static_mutex_unlock (&mutex);
}




void wBuiltin_gins_get(){
	//LOCK=1;

	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	g_static_mutex_lock (&mutex);

	gint intval;
	gboolean bvalue = TRUE;
	gdouble doubleValue;
	GtkWidget *obj;

	gdouble dval;
	gboolean bval;

	char type[255];
	char bvalstr[255] = "TRUE";
	char objid[255];
	char name[255];
	char buffer[300];

	gchar *value;
	char *wstring;
	wVariant *variant;

	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(type , "%s" , wstring);
	wVariantDeref( variant );
	wFree(wstring);



	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(name , "%s" , wstring);

	wVariantDeref( variant );
	wFree(wstring);

/*
LOCK=0;
g_static_mutex_unlock (&mutex);
return;
*/
	variant = wStackPopString();
    	wstring = wVariantToChar( variant, W_FALSE, 0 );
	sprintf(objid , "%s" , wstring);

	wVariantDeref( variant );
	wFree(wstring);

	obj = glade_xml_get_widget(xml, objid);

	if (obj == NULL) {
		printf("[gins] ERROR INVALID_OBJID\n");

		LOCK=0;
		g_static_mutex_unlock (&mutex);
		return;
	}


	if (strncmp(type,"<str>", 5)==0) {
		g_object_get (obj,name, &value, NULL);
		snprintf(buffer, 299, "%s", value);

		g_free(value);

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, buffer ,4,4 );

    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);



		LOCK=0;
		g_static_mutex_unlock (&mutex);
		return;
	}

	if (strncmp(type,"<double>", 5)==0) {
		g_object_get (obj,name, &dval, NULL);
		
		snprintf(buffer, 299, "%f", dval);

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, buffer ,4,4 );

    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);

		LOCK=0;
		g_static_mutex_unlock (&mutex);
		return;
	}
	
	if (strncmp(type,"<boolean>", 5)==0) {
		g_object_get (obj,name, &bval, NULL);
		if (! bval) { 
			bvalstr[0] = 'F';
			bvalstr[1] = 'A';
			bvalstr[2] = 'L';
			bvalstr[3] = 'S';
			bvalstr[4] = 'E';
			
		}
		snprintf(buffer, 299, "%s", &bvalstr);

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, buffer ,4,4 );

    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);

		LOCK=0;
		g_static_mutex_unlock (&mutex);
		return;
	}
	
	
	if (strncmp(type,"<int>", 5)==0) {
		g_object_get (obj,name, &intval, NULL);
		
		snprintf(buffer, 299, "%i", intval);

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, buffer ,4,4 );

    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);

		LOCK=0;
		g_static_mutex_unlock (&mutex);
		return;
	}

	LOCK=0;
	g_static_mutex_unlock (&mutex);
}
  

void wBuiltin_gins_event(){
	//LOCK=1;
	if(bufferfull==1){

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, ginsbuffer1 ,4,4 );
    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

           	//-- create a string
           	wVariantFromChar( myvariant, ginsbuffer2 ,4,4 );
    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

           	//-- create a string
           	wVariantFromChar( myvariant, ginsbuffer3 ,4,4 );
    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );
   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);

		bufferfull = 0;




	}

//int gxx = gtk_main_level();
//printf("-- %d\n" , gxx);

//LOCK=0;
}


void connections(GladeXML *xml){
	/*connect all widgets event*/
	GList* allwidgets=NULL;
	GtkWidget *widget;
	
	allwidgets = glade_xml_get_widget_prefix(xml, "");
	
	while (allwidgets != NULL) {
		g_signal_connect (G_OBJECT (allwidgets->data), "event", G_CALLBACK (signal_handler), NULL);
		allwidgets = allwidgets->next;
	}
	
	g_list_free(allwidgets);

}


int loadXMLFile(char *file){
	//printf("---%s\n" , file);
	removeNL(file);
	//gdk_threads_enter();
	/* load the interface */
	xml = glade_xml_new(file, NULL, NULL);
	//gdk_threads_leave();
	if (xml==NULL) return -1;
	connections(xml);
	return 0;
}


void wBuiltin_gins_exit(){
	//LOCK = 0;
	//bufferfull=0;
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	g_static_mutex_lock (&mutex);
	gdk_threads_enter ();
        gdk_flush();
exit(0);
	gtk_main_quit();
	gdk_threads_leave ();
	//LOCK=0;
	g_static_mutex_unlock (&mutex);

}





void wBuiltin_gins_usleep()
{
	int microseconds;
	microseconds = wStackPopNumber();
	//    printf("%d\n" , microseconds);

	#ifdef _WIN32
		sleep(microseconds/1000);
	#else
		usleep(microseconds);
	#endif
}


#ifdef _WIN32
void *mywinthread(){

printf("\n--starting new thread--\n");

	gtk_main();
}

#include <windows.h>
#include <winbase.h>
#include <process.h>
typedef unsigned (__stdcall *PTHREAD_START) (void *);

void wBuiltin_gins_main(){

unsigned long threadId;
//HANDLE hThread = CreateThread(NULL, 2000, (LPTHREAD_START_ROUTINE)mywinthread, 0, CREATE_SUSPENDED, &threadId);


unsigned threadID;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (PTHREAD_START) mywinthread, 0, 0, &threadID);

}
#else



void *mythread(void *ch){
     // avoid badwindow-errors
     int (*gmyold_handler) (Display *, XErrorEvent *);
     gmyold_handler= XSetErrorHandler (gignore_error);

gdk_threads_enter();
	gtk_main();
gdk_threads_leave();
printf("gtkmain has quit\n");
LOCK=0;
exit(0);
//wBuiltin_gins_exit();
}

void wBuiltin_gins_main(){
//gdk_threads_enter();
//  while (gtk_events_pending ()){

//	  gtk_main_iteration();

//}
//gdk_threads_leave();

//usleep(5000000);
//return;

     // avoid badwindow-errors
     int (*gmyold_handler) (Display *, XErrorEvent *);
     gmyold_handler= XSetErrorHandler (gignore_error);
//gtk_set_locale ();
//gtk_disable_setlocale ();
  //gdk_threads_enter ();
	char ch1='-';
	pthread_t p1;
	pthread_create (&p1, NULL, mythread, &ch1);

//gdk_threads_leave ();

}


#endif