text.c 6.1 KB

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