
/*
 * socket.c --
 *
 * This file implements the socket widget
 *
 * Copyright (c) 2001 P.G. Baum
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

/*
   History:
   2001-11: Begin of developement
 */

#include "gnocl.h"
#include <gdk/gdkx.h>
#include <string.h>
#include <assert.h>

#if 0
typedef struct
{
   GNOCL_STD_OPTIONS_VARS;
   int dummy;
} SocketOptions;

static const GnoclWidgetOptions widgetOptions[] =
{
   { NULL, 0, 0 }
};
#endif

typedef struct
{
   char       *name;
   GtkSocket  *socket;
   Tcl_Interp *interp;
} SocketParams;

#if 0
static void freeOpts( SocketOptions *p )
{
}

int socketConfigure( Tcl_Interp *interp, SocketParams *para, 
      SocketOptions *popt )
{
   return TCL_OK;
}
#endif

static void socketDestroyFunc( GtkWidget *widget, gpointer data )
{
   SocketParams *p = (SocketParams *)data;

   gnoclForgetWidgetFromName( p->name );
   Tcl_DeleteCommand( p->interp, p->name );

   g_free( p->name );
   g_free( p );
}



static int socketFunc( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{

   static char *cmds[] = { GNOCL_STD_CMD_NAMES,
         "configure", "getXID", NULL };
   enum cmdIdx { GNOCL_STD_CMD_ENUMS, ConfigureIdx, GetXIDIdx };
   int idx;
   SocketParams *para = (SocketParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->socket );

   if( Tcl_GetIndexFromObj( interp, objv[1], cmds, "command", 
         TCL_EXACT, &idx ) != TCL_OK )
      return TCL_ERROR;

   switch( idx )
   {
      GNCOL_STD_CMD_CASE( interp, objc, objv, widget );
            break;

      case ConfigureIdx:
            {
               #if 0
               SocketOptions opt;
               int ret = TCL_ERROR;

               memset( &opt, 0, sizeof( opt ) );
               if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
                     widgetOptions, (char *)&opt ) == TCL_OK )
                  ret = socketConfigure( interp, para, &opt );
               freeOpts( &opt );
               if( ret != TCL_OK )
                  return TCL_ERROR;
               #endif
               Tcl_SetResult( interp, "no option valid", TCL_STATIC );
               return TCL_ERROR;
            }
            break;
      case GetXIDIdx:
            {
               long xid = GDK_WINDOW_XWINDOW( 
                     GTK_WIDGET( para->socket)->window );
               Tcl_Obj *val = Tcl_NewLongObj( xid );
               Tcl_SetObjResult( interp, val );
            }
            break;
   }

   return TCL_OK;
}

int gnoclSocketCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   SocketParams  *para;

#if 0
   SocketOptions opt;
   memset( &opt, 0, sizeof( opt ) );
   if( gnoclParseAllWidgetOpts( interp, objc, objv, 0,
         widgetOptions, (char *)&opt ) != TCL_OK )
      return TCL_ERROR;
#endif
   if( objc != 1 )
   {
      Tcl_SetResult( interp, "no option valid", TCL_STATIC );
      return TCL_ERROR;
   }

   para = g_new( SocketParams, 1 );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;
   para->socket = GTK_SOCKET( gtk_socket_new( ) );

#if 0
   if( socketConfigure( interp, para, &opt ) != TCL_OK )
   {
      /* FIXME: g_free pp->name ... */
      gtk_widget_destroy( GTK_WIDGET( para->socket ) );
      return TCL_ERROR;
   }
#endif

   gtk_signal_connect( GTK_OBJECT( para->socket ), "destroy", 
         GTK_SIGNAL_FUNC( socketDestroyFunc ), para );

   gnoclMemNameAndWidget( para->name, GTK_WIDGET( para->socket ) );
   gtk_widget_show( GTK_WIDGET( para->socket ) );

   Tcl_CreateObjCommand( interp, para->name, socketFunc, para, NULL );
   Tcl_SetObjResult( interp, Tcl_NewStringObj( para->name, -1 ) );

   return TCL_OK;
}

