
/*
 * plug.c --
 *
 * This file implements the plug widget
 *
 * Copyright (c) 2001 - 2002 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>

#ifdef GNOCL_USE_BONOBO
#include <bonobo/bonobo-plug.h>
#endif

typedef struct
{
#if 0
   GNOCL_STD_OPTIONS_VARS;
#endif
   Gnocl_StringBool child;
} PlugOptions;

static const GnoclWidgetOptions widgetOptions[] =
{
   { "-child", GNOCL_STRING, GNOCL_OFFSET( PlugOptions, child ) },
   { NULL, 0, 0 }
};

typedef struct
{
   char       *name;
#ifdef GNOCL_USE_BONOBO
   BonoboPlug  *plug;
#else
   GtkPlug  *plug;
#endif
   Tcl_Interp *interp;
} PlugParams;

static void freeOpts( PlugOptions *p )
{
   GNOCL_FREE_MEM_OPT( p->child );
}

static void plugDestroyFunc( GtkWidget *widget, gpointer data )
{
   PlugParams *p = (PlugParams *)data;

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

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


static int plugConfigure( Tcl_Interp *interp, PlugParams *para, 
      PlugOptions *popt )
{
   if( popt->child.changed )
   {
      GtkWidget *childWidget = gnoclGetWidgetFromName( popt->child.val, 
            interp );
      if( childWidget == NULL )
         return TCL_ERROR;

      gtk_container_add( GTK_CONTAINER( para->plug ), childWidget );
   }


   return TCL_OK;
}

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

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

   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:
            {
               PlugOptions opt;
               int ret = TCL_ERROR;

               memset( &opt, 0, sizeof( opt ) );
               if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
                     widgetOptions, (char *)&opt ) == TCL_OK )
                  ret = plugConfigure( interp, para, &opt );
               freeOpts( &opt );
               if( ret != TCL_OK )
                  return TCL_ERROR;
            }
            break;
   }

   return TCL_OK;
}

int gnoclPlugCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   PlugOptions opt;
   PlugParams  *para;
   long        socketId;

   if( objc < 2 )
   {
      Tcl_WrongNumArgs( interp, 1, objv, "socketId ?option val ...?" );
      return TCL_ERROR;
   }
   if( Tcl_GetLongFromObj( interp, objv[1], &socketId ) != TCL_OK )
      return TCL_ERROR;

   memset( &opt, 0, sizeof( opt ) );
   if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
         widgetOptions, (char *)&opt ) != TCL_OK )
      return TCL_ERROR;

   para = g_new( PlugParams, 1 );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;
#ifdef GNOCL_USE_BONOBO
   para->plug = BONOBO_PLUG( bonobo_plug_new( socketId ) );
#else
   para->plug = GTK_PLUG( gtk_plug_new( socketId ) );
#endif

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

   gtk_signal_connect( GTK_OBJECT( para->plug ), "destroy", 
         GTK_SIGNAL_FUNC( plugDestroyFunc ), para );

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

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

   return TCL_OK;
}

