/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Icon.FileIcon.c
    Author:  Copyright  1994 Tim Browse
    Version: 1.01 (26 Jun 1994)
    Purpose: Changes an icon in a window to display the correct filetype
      	     sprite for the given filetype number.
*/


#include <stdio.h>

#include "Desk.Wimp.h"
#include "Desk.WimpSWIS.h"
#include "Desk.Icon.h"

void Desk_Icon_FileIcon(Desk_window_handle window, Desk_icon_handle icon, int filetype)
{
  /* Icon should be an indirected text-only icon with enough room in text
     buffer to hold the sprite name.  
     This converts to an indirected sprite-only icon, and fills in the sprite
     name and area.
  */
  Desk_icon_createblock iconcreate;
  Desk_icon_handle handle;

  /* Get the data for this icon */
  Desk_Wimp_GetIconState(window, icon, &iconcreate.icondata);

  /* Delete this icon - we have to change the data fields */
  Desk_Wimp_DeleteIcon(window, icon);

  /* Put sprite name in name field */
  sprintf((char *) iconcreate.icondata.data.indirectsprite.name, 
          "file_%03x", filetype);

  /* Fill in sprite area */
  iconcreate.icondata.data.indirectsprite.spritearea = (unsigned int *) 1;

  /* Fill in sprite name length */
  iconcreate.icondata.data.indirectsprite.nameisname = 8; /* Desk_file_xxx */

  /* Change to sprite-only */
  iconcreate.icondata.flags.data.text   = Desk_bool_FALSE;
  iconcreate.icondata.flags.data.sprite = Desk_bool_TRUE;

  /* Re-create the icon */
  iconcreate.window = window;
  Desk_Wimp_CreateIcon(&iconcreate, &handle);
    
  /* Force the icon to be redrawn */
  Desk_Wimp_SetIconState(window, handle, 0, 0);
}
