home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / Libraries / Menu / c / SetText < prev    next >
Encoding:
Text File  |  1993-04-30  |  2.3 KB  |  75 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Menu.SetText.c
  12.     Author:  Copyright © 1993 Shaun Blackmore and Jason Williams
  13.     Version: 1.00 (30 Apr 1993)
  14.     Purpose: Changes the text in a menu item
  15. */
  16.  
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. #include "Wimp.h"
  21.  
  22.  
  23. void Menu_SetText(menu_ptr menu, int entry, char *text)
  24. {
  25.   int       length, oldlength;
  26.   char      *dest;
  27.   menu_item *item = (menu_item*) (((int) menu) + sizeof(menu_block));
  28.  
  29.   item = &item[entry];
  30.  
  31.   /*  If the new text will not fit into the old buffer, then
  32.    *    if it was indirected
  33.    *      Deallocate the old indirected buffer
  34.    *    Allocate a new buffer
  35.    *  NOTE: we don't bother shrinking the buffer if the new text is shorter
  36.    */
  37.    
  38.   if (item->iconflags.data.indirected)
  39.     oldlength = item->icondata.indirecttext.bufflen; 
  40.   else
  41.     oldlength = wimp_MAXNAME;
  42.  
  43.   if ((length = strlen(text)) >= oldlength)
  44.   {
  45.     if (item->iconflags.data.indirected)
  46.       free(item->icondata.indirecttext.buffer);
  47.  
  48.     item->icondata.indirecttext.buffer = malloc(length + 1);
  49.     if (item->icondata.indirecttext.buffer != NULL)
  50.     {
  51.       item->iconflags.data.indirected         = TRUE;
  52.       item->icondata.indirecttext.bufflen     = length + 1;
  53.       item->icondata.indirecttext.validstring = (char *) -1;
  54.     }
  55.   }
  56.  
  57.   /*  Copy the new string into the buffer, truncating it if it is still
  58.    *  too long to fit into the buffer.
  59.    */
  60.    
  61.   if (item->iconflags.data.indirected)
  62.   {
  63.     length = item->icondata.indirecttext.bufflen - 1;
  64.     dest   = item->icondata.indirecttext.buffer;
  65.   }
  66.   else
  67.   {
  68.     length = wimp_MAXNAME - 1;
  69.     dest   = item->icondata.text;
  70.   }
  71.  
  72.   strncpy(dest, text, length);    /* Copy the string (truncate if neccesary) */
  73.   dest[length] = 0;               /* And ensure it is zero terminated        */
  74. }
  75.