
/* createicon.c
 *
 * Wimp function library
 *  Joseph Heenan, 1998.
 *
 *
 *
 *
 * $Log: createicon,v $
 * Revision 1.5  1999/01/26 15:36:32  joseph
 * Changed flags so icon area would be cleared before being redrawn
 *
 * Revision 1.4  1998/11/01 18:31:26  joseph
 * Now reads sprite size from the wimp sprite pool and creates icon of
 * the appropriate size.
 *
 * Revision 1.3  1998/05/24 13:11:04  joseph
 * Now take copy of sprite name and stores corrent length
 *
 * Revision 1.2  1998/05/10 20:13:10  jogu
 * Added names to file headers
 * Added implicit cast for frontend_taskname to wimp_report_error
 *   (it defines 'name' without 'const')
 *
 * Revision 1.1.1.1  1998/05/10 19:56:03  jogu
 * WimpCLib V1.00 (created from newshound c.wimpc rev 1.2)
 *
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "swis.h"
#include "wimplib.h"

#include "wimpclib.h"



int wimpc_createiconbar( const char *spritename )
{
  WimpCreateIconBlock defn;
  int handle;
  int width, height, mode;
  int size = strlen( spritename ) + 1;
  char *buf = malloc( size );

  if ( !buf ) return -1;

  memcpy( buf, spritename, size );

  defn.window_handle  = -1;
  defn.icon.bbox.xmin = 0;
  defn.icon.bbox.ymin = 0;
  defn.icon.bbox.xmax = 68; /* sprite width     16*text len? */
  defn.icon.bbox.ymax = 68; /* sprite height    68 + text height(16) + gap (4) */
  defn.icon.flags     = ( WimpIcon_Sprite | WimpIcon_Indirected
  		       | WimpIcon_HCentred | WimpIcon_NeedsHelp )
   		       + WimpIcon_ButtonType * ButtonType_Click
   		       + WimpIcon_FGColour * 7 + WimpIcon_BGColour;
  defn.icon.data.is.sprite              = buf;
  defn.icon.data.is.sprite_area         = (void *) 1; /* wimp sprite area */
  defn.icon.data.is.sprite_name_length  = size;

  /* Read sprite size if possible */
  if ( _swix( Wimp_SpriteOp, _IN(0)|_IN(2) | _OUTR(3,4) | _OUT(6),
                             40, spritename, &width, &height, &mode ) == NULL )
  {
    int xeig, yeig;
    if ( _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), mode, 4, &xeig) == NULL &&
         _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), mode, 5, &yeig) == NULL )
    {
      defn.icon.bbox.xmax = width << xeig;
      defn.icon.bbox.ymax = height << yeig;
    }
  }

  E_CHECK_RETURN( -1, wimp_create_icon( 0, &defn, &handle ) );

  return handle;
}
