
/*
 * pixmap.c --
 *
 * This file implements the pixmap 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-12: Begin of developement
 */

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


typedef struct
{
#if 0
   GNOCL_STD_OPTIONS_VARS;
#endif
   Gnocl_IntBool width;
   Gnocl_IntBool height;
} PixmapOptions;

static const GnoclWidgetOptions widgetOptions[] =
{
   { "-width", GNOCL_INT, GNOCL_OFFSET( PixmapOptions, width ) },
   { "-height", GNOCL_INT, GNOCL_OFFSET( PixmapOptions, height ) },
   { NULL, 0, 0 }
};

typedef struct
{
   char        *name;
   GnomePixmap *pixmap;
   Tcl_Interp  *interp;
} PixmapParams;

#if 0
static void freeOpts( PixmapOptions *p )
{
   GNOCL_FREE_MEM_OPT( p->qqq );
}
#endif

static void pixmapDestroyFunc( GtkWidget *widget, gpointer data )
{
   PixmapParams *p = (PixmapParams *)data;

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

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


#if 0
int pixmapConfigure( Tcl_Interp *interp, PixmapParams *para, 
      PixmapOptions *popt )
{
   if( popt->child.changed )
   {
   }


   return TCL_OK;
}
#endif

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

   static char *cmds[] = { GNOCL_STD_CMD_NAMES, NULL };
   enum cmdIdx { GNOCL_STD_CMD_ENUMS };
   int idx;
   PixmapParams *para = (PixmapParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->pixmap );

   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;

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

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

   return TCL_OK;
}

int gnoclPixmapCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   PixmapOptions opt;
   PixmapParams  *para;
   char          *file;

   if( objc < 2 )
   {
      Tcl_WrongNumArgs( interp, 1, objv, "file ?option val ...?" );
      return TCL_ERROR;
   }

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

   if( gnoclGetStringType( objv[1] ) != GNOCL_STR_FILE )
   {
      Tcl_SetResult( interp, 
            "file must have string type \"file\" (prefix \"%/\")",
            TCL_STATIC );
      return TCL_ERROR;

   }
   file = gnoclGetStringFromObj( objv[1], NULL );


   para = g_new( PixmapParams, 1 );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;
   if( opt.width.changed || opt.height.changed )
   {
      if( opt.width.changed == 0 || opt.width.val < 1
            || opt.height.changed == 0 || opt.height.val < 1 )
      {
         g_free( para );
         Tcl_SetResult( interp, 
               "width and heigth must be set and be greater 0.",
               TCL_STATIC );
         return TCL_ERROR;
      }
      para->pixmap = GNOME_PIXMAP( gnome_pixmap_new_from_file_at_size( 
            file, opt.width.val, opt.height.val ) );
   }
   else
      para->pixmap = GNOME_PIXMAP( gnome_pixmap_new_from_file( file ) );

   if( para->pixmap == NULL )
   {
      g_free( para );
      Tcl_AppendResult( interp, "could not load file \"", 
               file, "\".", (char *)NULL );

      return TCL_ERROR;
   }

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

   gtk_signal_connect( GTK_OBJECT( para->pixmap ), "destroy", 
         GTK_SIGNAL_FUNC( pixmapDestroyFunc ), para );

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

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

   return TCL_OK;
}

