home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / editinit.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  7KB  |  233 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    editinit.c (Editor Initialization)
  6.  * Subroutine:    get_edit_struct()        returns: EditStruct *
  7.  * Subroutine:    init_edit_struct()        returns: void
  8.  * Subroutine:    load_edit_string()        returns: void
  9.  * Subroutine:    load_edit_struct()        returns: void
  10.  * Subroutine:    store_edit_struct()        returns: void
  11.  * Subroutine:    recall_edit_struct()        returns: void
  12.  * Subroutine:    clear_edit_buf()        returns: void
  13.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  14.  *        You may do anything you like with this file except remove
  15.  *        this copyright.  The Smithsonian Astrophysical Observatory
  16.  *        makes no representations about the suitability of this
  17.  *        software for any purpose.  It is provided "as is" without
  18.  *        express or implied warranty.
  19.  * Modified:    {0} Michael VanHilst    initial version          4 July 1989
  20.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  21.  *        {2} MVH load_edit_string() without save        18 March 1990
  22.  *        {n} <who> -- <does what> -- <when>
  23.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. #ifndef VMS
  28. #ifdef SYSV
  29. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  30. #else
  31. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  32. #endif
  33. #else
  34. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  35. #endif
  36.  
  37. #include <X11/Xlib.h>
  38. #include "hfiles/edit.h"
  39.  
  40. /*
  41.  * Subroutine:    get_edit_struct
  42.  * Returns:    Newly allocated popup editor structure
  43.  */
  44. EditStruct *get_edit_struct ( max_char_cnt )
  45.      int max_char_cnt;
  46. {
  47.   EditStruct *edit;
  48.   char *editbuf;
  49.   int line_sz, i;
  50.   char *calloc_errchk();
  51.  
  52.   /* set line_sz to first int boundary after (maxchar + 1) */
  53.   line_sz = ((max_char_cnt + sizeof(int)) / sizeof(int)) * sizeof(int);
  54.   editbuf = calloc_errchk(1, (unsigned int)sizeof(EditStruct) +
  55.               (line_sz * 
  56.                (((STACK_SZ + 2) * sizeof(int)) + STACK_SZ + 1)),
  57.                (char *)NULL);
  58.   if( editbuf == NULL )
  59.     return( NULL );
  60.   edit = (EditStruct *)editbuf;
  61.   editbuf += sizeof(EditStruct);
  62.   edit->string = editbuf;
  63.   for( i=0; i<STACK_SZ; i++ ) {
  64.     editbuf += line_sz;
  65.     edit->buf[i].string = editbuf;
  66.   }
  67.   editbuf += line_sz;
  68.   line_sz = line_sz * sizeof(int);
  69.   edit->pixlen = (int *)editbuf;
  70.   editbuf += line_sz;
  71.   edit->charsz = (int *)editbuf;
  72.   for( i=0; i<STACK_SZ; i++ ) {
  73.     editbuf += line_sz;
  74.     edit->buf[i].charsz = (int *)editbuf;
  75.   }
  76.   edit->max_char_cnt = max_char_cnt;
  77.   edit->pixlen[0] = 0;
  78.   return( edit );
  79. }
  80.  
  81. /*
  82.  * Subroutine:    init_edit_struct
  83.  * Purpose:    Initialize font and drawing info for popup editor structure
  84.  */ 
  85. void init_edit_struct ( display, w, edit, fontstruct, foreground, background )
  86.      Display *display;        /* i: display which has popup window */
  87.      Window w;            /* i: popup window */
  88.      EditStruct *edit;        /* i: struct for edit strings */
  89.      XFontStruct *fontstruct;    /* i: info about the font */
  90.      unsigned long foreground;    /* i: needed for text GC */
  91.      unsigned long background;
  92. {
  93.   edit->display = display;
  94.   edit->ID = w;
  95.   edit->fontstruct = fontstruct;
  96.   edit->font = fontstruct->fid;
  97.   edit->foreground = foreground;
  98.   edit->background = background;
  99.   edit->area_height =
  100.     fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent;
  101.   if( edit->charsz[0] == 0 ) {
  102.     edit->string[0] = ' ';
  103.     edit->charsz[0] = XTextWidth(fontstruct, edit->string, 1);
  104.     edit->pixlen[1] = edit->charsz[0];
  105.   }
  106.   edit->stack_cnt = 0;
  107.   edit->stack_index = -1;
  108. }
  109.  
  110. /*
  111.  * Subroutine:    load_edit_string
  112.  * Purpose:    Store a passed string in the current string buffer
  113.  */
  114. void load_edit_string ( edit, string, char_cnt )
  115.      EditStruct *edit;
  116.      char *string;
  117.      int char_cnt;
  118. {
  119.   int i;
  120.  
  121.   if( char_cnt >= edit->max_char_cnt )
  122.     char_cnt = edit->max_char_cnt - 1;
  123.   (void)strncpy(edit->string, string, char_cnt);
  124.   edit->char_cnt = char_cnt;
  125.   edit->string[char_cnt] = ' ';
  126.   edit->string[char_cnt+1] = '\0';
  127.   for( i=0; i<=char_cnt; i++ ) {
  128.     edit->charsz[i] = XTextWidth(edit->fontstruct, &(edit->string[i]), 1);
  129.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  130.   }
  131.   if( edit->pixlen[char_cnt + 1] > edit->max_pixlen )
  132.     edit->oversize = 1;
  133.   else
  134.     edit->oversize = 0;
  135. }
  136.  
  137. /*
  138.  * Subroutine:    load_edit_struct
  139.  * Purpose:    Store a passed string in the editor storage stack
  140.  * Note:    Also makes it the default entry
  141.  * Exception:    Assume no other stored strings (erases any)
  142.  */
  143. void load_edit_struct ( edit, string, char_cnt )
  144.      EditStruct *edit;
  145.      char *string;
  146.      int char_cnt;
  147. {
  148.   void store_edit_struct(), load_edit_string();
  149.  
  150.   load_edit_string(edit, string, char_cnt);
  151.   store_edit_struct(edit);
  152. }
  153.  
  154. /*
  155.  * Subroutine:    recall_edit_struct
  156.  * Purpose:    Retrieve a line from the storage stack
  157.  */
  158. void recall_edit_struct ( edit, next, clear )
  159.      EditStruct *edit;
  160.      int next;
  161.      int clear;        /* next <0 clears current string */
  162. {
  163.   int i, char_cnt;
  164.   void clear_edit_buf();
  165.  
  166.   if( next >= edit->stack_cnt ) {
  167.     edit->stack_index = edit->stack_cnt - 1;
  168.     return;
  169.   } else if( next < 0 ) {
  170.     if( clear )
  171.       clear_edit_buf (edit);
  172.     edit->stack_index = -1;
  173.     return;
  174.   }
  175.   char_cnt = edit->buf[next].char_cnt;
  176.   edit->char_cnt = char_cnt;
  177.   bcopy(edit->buf[next].string, edit->string, char_cnt + 1);
  178.   bcopy((char *)edit->buf[next].charsz, (char *)edit->charsz,
  179.     (char_cnt + 1) * sizeof(int));
  180.   for( i=0; i<=char_cnt; i++ ) {
  181.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  182.   }
  183.   if( edit->pixlen[edit->char_cnt + 1] > edit->max_pixlen )
  184.     edit->oversize = 1;
  185.   else
  186.     edit->oversize = 0;
  187.   edit->active_position = 0;
  188.   edit->stack_index = next;
  189. }
  190.  
  191. /*
  192.  * Subroutine:    store_edit_struct
  193.  * Purpose:    Put current line in the store stack
  194.  */
  195. void store_edit_struct ( edit )
  196.      EditStruct *edit;
  197. {
  198.   int *charsz, i;
  199.   char *string;
  200.  
  201.   if( edit->stack_cnt < STACK_SZ )
  202.     (edit->stack_cnt)++;
  203.   charsz = edit->buf[edit->stack_cnt-1].charsz;
  204.   string = edit->buf[edit->stack_cnt-1].string;
  205.   for( i=edit->stack_cnt-1; i>0; i-- ) {
  206.     edit->buf[i].char_cnt = edit->buf[i-1].char_cnt;
  207.     edit->buf[i].charsz = edit->buf[i-1].charsz;
  208.     edit->buf[i].string = edit->buf[i-1].string;
  209.   }
  210.   edit->buf[0].char_cnt = edit->char_cnt;
  211.   edit->buf[0].charsz = charsz;
  212.   edit->buf[0].string = string;
  213.   bcopy(edit->string, string, edit->char_cnt + 1);
  214.   bcopy((char *)edit->charsz, (char *)charsz,
  215.     (edit->char_cnt + 1) * sizeof(int));
  216.   edit->active_position = edit->char_cnt;
  217.   edit->stack_index = -1;
  218. }
  219.  
  220. /*
  221.  * Subroutine:    clear_edit_buf
  222.  * Purpose:    Set edit buffer string to no characters
  223.  */
  224. void clear_edit_buf ( edit )
  225.      EditStruct *edit;
  226. {
  227.   edit->string[0] = edit->string[edit->char_cnt];
  228.   edit->charsz[0] = edit->charsz[edit->char_cnt];
  229.   edit->pixlen[1] = edit->charsz[0];
  230.   edit->char_cnt = 0;
  231.   edit->active_position = 0;
  232. }
  233.