home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgc / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-06  |  6.0 KB  |  208 lines

  1. /*
  2. ** xgc
  3. **
  4. ** text.c
  5. **
  6. ** How to make a text widget that returns a string when the cursor
  7. ** leaves its window.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <X11/Intrinsic.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/Xaw/Form.h>
  14. #include <X11/Xaw/Label.h>
  15. #include <X11/Xaw/AsciiText.h>
  16. #include "xgc.h"
  17.  
  18. static void WriteText();
  19. extern void interpret();
  20.  
  21. extern XStuff X;
  22. extern XtAppContext appcontext;
  23.  
  24. /* the strings which are displayed on the screen, edited, and sent
  25.    to interpret() */
  26. static char textstrings[NUMTEXTWIDGETS][80];
  27.  
  28. static char oldtextstrings[NUMTEXTWIDGETS][80];
  29.  
  30. static char *defaultstrings[NUMTEXTWIDGETS] = {"0","6x10","0","1"};
  31.  
  32. /* The labels displayed next to them */
  33. static char *labels[NUMTEXTWIDGETS] = {"Line Width","Font","Foreground",
  34.                        "Background"};
  35.  
  36. /* the first half of what gets sent to interpret() */
  37. static char *names[NUMTEXTWIDGETS] = {"linewidth ","font ","foreground ",
  38.                       "background "};
  39.  
  40. /* create_text_choice(w,type,length,width)
  41. ** ---------------------------------------
  42. ** Inside w (a form), creates an editable text widget of width width.  The
  43. ** user can enter a string of up to length characters.  type is one of
  44. ** the constants defined in xgc.h; it decides things like the label,
  45. ** what string will be displayed and edited, etc.  When the pointer leaves
  46. ** the widget, the widget does the appropriate thing with the text
  47. ** inside it; the user doesn't have to press an "enter" button or anything.
  48. ** Returns the text widget which the user will edit.
  49. */
  50.  
  51. Widget
  52. create_text_choice(w,type,length,width)
  53.      Widget w;
  54.      int type;
  55.      int length, width;
  56. {
  57.   char translationtable[600];    /* for adding the new action (calling
  58.                    WriteText() when the pointer leaves) */
  59.  
  60.   static XtActionsRec actionTable[] = {    /* likewise */
  61.     {"WriteText",  WriteText},
  62.     {"Nothing",    NULL}
  63.   };
  64.  
  65.   static Arg labelargs[] = {
  66.     {XtNborderWidth,   (XtArgVal) 0},
  67.     {XtNjustify,       (XtArgVal) XtJustifyRight}
  68.   };
  69.  
  70.   static Arg textargs[] = {
  71.     {XtNeditType,   (XtArgVal) XawtextEdit},
  72.     {XtNstring,     (XtArgVal) NULL},
  73.     {XtNlength,     (XtArgVal) NULL},
  74.     {XtNwidth,      (XtArgVal) NULL},
  75.     {XtNhorizDistance, (XtArgVal) 10},
  76.     {XtNfromHoriz,  (XtArgVal) NULL},
  77.     {XtNinsertPosition, (XtArgVal) NULL},
  78.     {XtNuseStringInPlace, (XtArgVal) True}
  79.   };
  80.  
  81.   static Widget text;        /* the text widget */
  82.   static Widget label;        /* the label widget */
  83.  
  84.   /* Disable keys which would cause the cursor to go off the single
  85.   ** line that we want to display.  If the pointer leaves the window,
  86.   ** update the GC accordingly.  The integer passed to WriteText is
  87.   ** so it knows what type of widget was just updated. */
  88.  
  89.   sprintf(translationtable,
  90.      "<Leave>:      WriteText(%d)\n\
  91.      Ctrl<Key>J:    Nothing()\n\
  92.      Ctrl<Key>M:    Nothing()\n\
  93.      <Key>Linefeed: Nothing()\n\
  94.      <Key>Return:   Nothing()\n\
  95.      Ctrl<Key>O:    Nothing()\n\
  96.      Meta<Key>I:    Nothing()\n\
  97.      Ctrl<Key>N:    Nothing()\n\
  98.      Ctrl<Key>P:    Nothing()\n\
  99.      Ctrl<Key>Z:    Nothing()\n\
  100.      Meta<Key>Z:    Nothing()\n\
  101.      Ctrl<Key>V:    Nothing()\n\
  102.      Meta<Key>V:    Nothing()",type);
  103.  
  104.   /* label uses type to find out what its title is */
  105.   label = XtCreateManagedWidget(labels[type],labelWidgetClass,w,
  106.                 labelargs,XtNumber(labelargs));
  107.   
  108.   /* text uses type to find out what its string is */
  109.   switch (type) {
  110.   case TForeground:
  111.     sprintf(textstrings[type],"%d",(int) X.gcv.foreground);
  112.     sprintf(oldtextstrings[type],"%d",(int) X.gcv.foreground);
  113.     break;
  114.   case TBackground:
  115.     sprintf(textstrings[type],"%d",(int) X.gcv.background);
  116.     sprintf(oldtextstrings[type],"%d",(int) X.gcv.background);
  117.     break;
  118.   default:
  119.     strcpy(textstrings[type],defaultstrings[type]);
  120.     strcpy(oldtextstrings[type],defaultstrings[type]);
  121.   }
  122.   textargs[1].value = (XtArgVal) textstrings[type];
  123.   textargs[2].value = (XtArgVal) length;
  124.   textargs[3].value = (XtArgVal) width;
  125.   textargs[5].value = (XtArgVal) label;
  126.   textargs[6].value = (XtArgVal) strlen(textstrings[type]);
  127.  
  128.   text = XtCreateManagedWidget("text", asciiTextWidgetClass,w,
  129.                    textargs,XtNumber(textargs));
  130.  
  131.   /* Register the actions and translations */
  132.  
  133.   XtAppAddActions(appcontext,actionTable,XtNumber(actionTable));
  134.   XtOverrideTranslations(text,XtParseTranslationTable(translationtable));
  135.  
  136.   return(text);
  137. }
  138.  
  139. /* WriteText(w,event,params,num_params)
  140. ** ------------------------------------
  141. ** Makes an appropriate string and sends it off to interpret().
  142. ** It's an ActionProc, thus the funny arguments.
  143. */
  144.  
  145. /*ARGSUSED*/
  146. static void
  147. WriteText(w,event,params,num_params)
  148.      Widget w;
  149.      XEvent *event;
  150.      String *params;        /* the type is in here */
  151.      int *num_params;
  152. {
  153.   char mbuf[80];
  154.   int type;            /* which string # to send */
  155.  
  156.   type = atoi(params[0]);
  157.   if (strcmp(textstrings[type],oldtextstrings[type])) {
  158.     strcpy(oldtextstrings[type],textstrings[type]);
  159.     sprintf(mbuf,names[type]);    /* the right first half */
  160.     strcat(mbuf,textstrings[type]); /* the right second half */
  161.     strcat(mbuf,"\n");        /* the right newline */
  162.     interpret(mbuf);        /* send it off */
  163.   }
  164. }
  165.  
  166. /* change_text(w,type,newtext)
  167. ** ------------------------
  168. ** Changes the text in the text widget w of type type to newtext.
  169. */
  170.  
  171. void
  172. change_text(w,newtext)
  173.      Widget w;
  174.      String newtext;
  175. {
  176.   XawTextBlock text;        /* the new text */
  177.   XawTextPosition first, last;    /* boundaries of the old text */
  178.   String oldtext;        /* the old text */
  179.  
  180.   static Arg textargs[] = {
  181.     {XtNstring, NULL}
  182.   };
  183.  
  184.   /* Initialize the XawTextBlock. */
  185.  
  186.   if (!newtext)
  187.       newtext = "";
  188.   text.firstPos = 0;
  189.   text.length = strlen(newtext);
  190.   text.ptr = newtext;
  191.   text.format = FMT8BIT;
  192.  
  193.   /* Find the old text, so we can get its length, so we know how
  194.   ** much of it to update. */
  195.  
  196.   textargs[0].value = (XtArgVal) &oldtext;
  197.   XtGetValues(w,textargs,XtNumber(textargs));
  198.   first = XawTextTopPosition(w);
  199.   if (!oldtext)
  200.       oldtext = "";
  201.   last = (XawTextPosition) strlen(oldtext)+1;
  202.  
  203.   /* Replace it with the new text. */
  204.  
  205.   XawTextReplace(w, first, last, &text);
  206. }
  207.  
  208.