home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / languages / progs / shell / Sources / c / Label < prev    next >
Encoding:
Text File  |  1994-06-25  |  2.9 KB  |  152 lines

  1. #include <string.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4.  
  5. #include "DeskLib:WimpSWIs.h"
  6.  
  7. #include "Shell.Shell.h"
  8. #include "Shell.Extra.h"
  9. #include "Shell.Check.h"
  10. #include "Shell.SafeAlloc.h"
  11. #include "Shell.Label.h"
  12.  
  13.  
  14. static void    Shell_LabelDisplayer(
  15.     Shell_convertpoint    convert,
  16.     wimp_point        rectsize,
  17.     void            *reference,
  18.     const wimp_rect        *redrawrect
  19.     )
  20. {
  21.     char *text = (char *) reference;
  22. UNUSED( redrawrect);
  23.  
  24. Shell_PrintString( text, 0, rectsize.y, convert);
  25. return;
  26. }
  27.  
  28.  
  29.  
  30. static BOOL Shell_LabelSaver( char *filename, Shell_rectblock *r)
  31. {    FILE    *f;
  32.     char *text = (char *) r->reference;
  33.  
  34. f = fopen( filename, "w");
  35. if (!f)    return FALSE;
  36.  
  37. fprintf( f, text);
  38. return TRUE;
  39. }
  40.  
  41.  
  42.  
  43. Shell_rectblock    *Shell_Label(
  44.     Shell_windblock *w,
  45.     int x, int y,
  46.     int forecol, int backcol,
  47.     const char *text
  48.     )
  49. {
  50.     wimp_rect    rect;
  51.     int        len        = strlen( text);
  52.     char        *text2;
  53.     Shell_rectblock    *r;
  54.  
  55. rect.min.x = x; rect.max.x = x + Shell_TEXTXSIZE*len;
  56. rect.max.y = y; rect.min.y = y - Shell_TEXTYSIZE;
  57.  
  58. text2 = Shell_SafeMalloc( 1+len);
  59. strcpy( text2, text);
  60.  
  61. r = Shell_AddRectangle( w, &rect, Shell_LabelDisplayer, (void *) text2);
  62. Shell_MakeRectIcon( r, forecol, backcol, "R1");
  63. r->saver = Shell_LabelSaver;
  64. return r;
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. Shell_rectblock    *Shell_Labelf(
  72.     Shell_windblock *w,
  73.     int x, int y,
  74.     int forecol, int backcol,
  75.     const char *fmt, ...
  76.     )
  77. {
  78.     va_list args;
  79.  
  80. va_start( args, fmt);
  81. vsprintf( Shell_string, fmt, args);
  82. va_end( args);
  83. return Shell_Label( w, x, y, forecol, backcol, Shell_string);
  84. }
  85.  
  86.  
  87.  
  88. /*
  89. #define MAX( a, b)    (( (a) > (b) ) ? a : b )
  90. */
  91.  
  92. void    Shell_ReLabel( Shell_rectblock *r, const char *text)
  93. {
  94.     wimp_rect    oldiconrect;
  95.     int        newlen = strlen( text);
  96.     int        oldsize = r->rect.max.x - r->rect.min.x;
  97.     int        newsize;
  98.  
  99. oldiconrect = r->icon.workarearect;
  100.  
  101. r->reference = Shell_SafeRealloc( r->reference, 1 + newlen);
  102. strcpy( (char *) r->reference, text);
  103.  
  104. r->rect.max.x = r->rect.min.x + Shell_TEXTXSIZE * newlen;
  105.  
  106. newsize = r->rect.max.x - r->rect.min.x;
  107.  
  108. if ( newsize != oldsize)    {
  109.     /* Redraw whole icon if it has changed size.    */
  110.  
  111.     Shell_ResizeIconRect( r);
  112.     /* Recalculate the icon border. Also forces redraw if rect is visible    */
  113.  
  114.     if ( oldsize > newsize)    {
  115.         /* Need to erase extra space from the old (larger) icon.    */
  116.         window_redrawblock block;
  117.         block.window        = r->window;
  118.         block.rect        = oldiconrect;
  119.         block.rect.min.x    = r->icon.workarearect.max.x;
  120.         Wimp_ForceRedraw( &block);
  121.         }
  122.     }
  123.  
  124. else    Shell_ForceRectRedraw( r);    /* Don't have to do this if the rect has changed size    */
  125. }
  126.  
  127.  
  128.  
  129.  
  130. void Shell_ReLabelf( Shell_rectblock *r, const char *fmt, ...)
  131. {    va_list args;
  132. va_start( args, fmt);
  133. vsprintf( Shell_string, fmt, args);
  134. va_end( args);
  135. Shell_ReLabel( r, Shell_string);
  136. }
  137.  
  138.  
  139.  
  140.  
  141. void Shell_ReLabelfSlow( Shell_rectblock *r, const char *fmt, ...)
  142. {    va_list args;
  143.     clock_t t = clock();
  144. if ( t - r->last_update >= r->update_time)    {
  145.     r->last_update = t;
  146.     va_start( args, fmt);
  147.     vsprintf( Shell_string, fmt, args);
  148.     va_end( args);
  149.     Shell_ReLabel( r, Shell_string);
  150.     }
  151. }
  152.