home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / TextSrc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  15.3 KB  |  520 lines

  1. /* $XConsortium: TextSrc.c,v 1.11 91/02/20 17:58:08 converse Exp $ */
  2.  
  3. /*
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Chris Peterson, MIT X Consortium.
  24.  *
  25.  * Much code taken from X11R3 String and Disk Sources.
  26.  */
  27.  
  28. /*
  29.  * TextSrc.c - TextSrc object. (For use with the text widget).
  30.  *
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #include <X11/IntrinsicP.h>
  36. #include <X11/StringDefs.h>
  37. #include <X11/Xaw/XawInit.h>
  38. #include <X11/Xaw/TextSrcP.h>
  39. #include <X11/Xmu/CharSet.h>
  40.  
  41. /****************************************************************
  42.  *
  43.  * Full class record constant
  44.  *
  45.  ****************************************************************/
  46.  
  47. /* Private Data */
  48.  
  49. #define offset(field) XtOffsetOf(TextSrcRec, textSrc.field)
  50. static XtResource resources[] = {
  51.     {XtNeditType, XtCEditType, XtREditMode, sizeof(XawTextEditType), 
  52.         offset(edit_mode), XtRString, "read"},
  53. };
  54.  
  55. static void ClassInitialize(), ClassPartInitialize(), SetSelection();
  56. static void CvtStringToEditMode();
  57. static Boolean ConvertSelection();
  58. static XawTextPosition Search(), Scan(), Read();
  59. static int Replace();
  60.  
  61. #define SuperClass        (&objectClassRec)
  62. TextSrcClassRec textSrcClassRec = {
  63.   {
  64. /* core_class fields */    
  65.     /* superclass          */    (WidgetClass) SuperClass,
  66.     /* class_name          */    "TextSrc",
  67.     /* widget_size          */    sizeof(TextSrcRec),
  68.     /* class_initialize       */    ClassInitialize,
  69.     /* class_part_initialize    */    ClassPartInitialize,
  70.     /* class_inited           */    FALSE,
  71.     /* initialize          */    NULL,
  72.     /* initialize_hook        */    NULL,
  73.     /* realize              */    NULL,
  74.     /* actions              */    NULL,
  75.     /* num_actions          */    0,
  76.     /* resources          */    resources,
  77.     /* num_resources          */    XtNumber(resources),
  78.     /* xrm_class          */    NULLQUARK,
  79.     /* compress_motion          */    FALSE,
  80.     /* compress_exposure      */    FALSE,
  81.     /* compress_enterleave    */    FALSE,
  82.     /* visible_interest          */    FALSE,
  83.     /* destroy              */    NULL,
  84.     /* resize              */    NULL,
  85.     /* expose              */    NULL,
  86.     /* set_values          */    NULL,
  87.     /* set_values_hook        */    NULL,
  88.     /* set_values_almost    */    NULL,
  89.     /* get_values_hook        */    NULL,
  90.     /* accept_focus         */    NULL,
  91.     /* version            */    XtVersion,
  92.     /* callback_private       */    NULL,
  93.     /* tm_table               */    NULL,
  94.     /* query_geometry        */    NULL,
  95.     /* display_accelerator    */    NULL,
  96.     /* extension        */    NULL
  97.   },
  98. /* textSrc_class fields */
  99.   {
  100.     /* Read                     */      Read,
  101.     /* Replace                  */      Replace,
  102.     /* Scan                     */      Scan,
  103.     /* Search                   */      Search,
  104.     /* SetSelection             */      SetSelection,
  105.     /* ConvertSelection         */      ConvertSelection
  106.   }
  107. };
  108.  
  109. WidgetClass textSrcObjectClass = (WidgetClass)&textSrcClassRec;
  110.  
  111. static void 
  112. ClassInitialize ()
  113. {
  114.     XawInitializeWidgetSet ();
  115.     XtAddConverter(XtRString, XtREditMode,   CvtStringToEditMode,   NULL, 0);
  116. }
  117.  
  118.  
  119. static void
  120. ClassPartInitialize(wc)
  121. WidgetClass wc;
  122. {
  123.   register TextSrcObjectClass t_src, superC;
  124.  
  125.   t_src = (TextSrcObjectClass) wc;
  126.   superC = (TextSrcObjectClass) t_src->object_class.superclass;
  127.  
  128. /* 
  129.  * We don't need to check for null super since we'll get to TextSrc
  130.  * eventually.
  131.  */
  132.  
  133.     if (t_src->textSrc_class.Read == XtInheritRead) 
  134.       t_src->textSrc_class.Read = superC->textSrc_class.Read;
  135.  
  136.     if (t_src->textSrc_class.Replace == XtInheritReplace) 
  137.       t_src->textSrc_class.Replace = superC->textSrc_class.Replace;
  138.  
  139.     if (t_src->textSrc_class.Scan == XtInheritScan) 
  140.       t_src->textSrc_class.Scan = superC->textSrc_class.Scan;
  141.  
  142.     if (t_src->textSrc_class.Search == XtInheritSearch) 
  143.       t_src->textSrc_class.Search = superC->textSrc_class.Search;
  144.  
  145.     if (t_src->textSrc_class.SetSelection == XtInheritSetSelection) 
  146.       t_src->textSrc_class.SetSelection = superC->textSrc_class.SetSelection;
  147.  
  148.     if (t_src->textSrc_class.ConvertSelection == XtInheritConvertSelection) 
  149.       t_src->textSrc_class.ConvertSelection =
  150.                                    superC->textSrc_class.ConvertSelection;
  151. }
  152.  
  153. /************************************************************
  154.  *
  155.  * Class specific methods.
  156.  *
  157.  ************************************************************/
  158.  
  159. /*    Function Name: Read
  160.  *    Description: This function reads the source.
  161.  *    Arguments: w - the TextSrc Object.
  162.  *                 pos - position of the text to retreive.
  163.  * RETURNED        text - text block that will contain returned text.
  164.  *                 length - maximum number of characters to read.
  165.  *    Returns: The number of characters read into the buffer.
  166.  */
  167.  
  168. /* ARGSUSED */
  169. static XawTextPosition
  170. Read(w, pos, text, length)
  171. Widget w;
  172. XawTextPosition pos;
  173. XawTextBlock *text;    
  174. int length;        
  175. {
  176.   XtAppError(XtWidgetToApplicationContext(w), 
  177.          "TextSrc Object: No read function is defined.");
  178. }
  179.  
  180. /*    Function Name: Replace.
  181.  *    Description: Replaces a block of text with new text.
  182.  *    Arguments: src - the Text Source Object.
  183.  *                 startPos, endPos - ends of text that will be removed.
  184.  *                 text - new text to be inserted into buffer at startPos.
  185.  *    Returns: XawEditError.
  186.  */
  187.  
  188. /*ARGSUSED*/
  189. static int 
  190. Replace (w, startPos, endPos, text)
  191. Widget w;
  192. XawTextPosition startPos, endPos;
  193. XawTextBlock *text;
  194. {
  195.   return(XawEditError);
  196. }
  197.  
  198. /*    Function Name: Scan
  199.  *    Description: Scans the text source for the number and type
  200.  *                   of item specified.
  201.  *    Arguments: w - the TextSrc Object.
  202.  *                 position - the position to start scanning.
  203.  *                 type - type of thing to scan for.
  204.  *                 dir - direction to scan.
  205.  *                 count - which occurance if this thing to search for.
  206.  *                 include - whether or not to include the character found in
  207.  *                           the position that is returned. 
  208.  *    Returns: EXITS WITH AN ERROR MESSAGE.
  209.  *
  210.  */
  211.  
  212. /* ARGSUSED */
  213. static 
  214. XawTextPosition 
  215. Scan (w, position, type, dir, count, include)
  216. Widget                w;
  217. XawTextPosition       position;
  218. XawTextScanType       type;
  219. XawTextScanDirection  dir;
  220. int               count;
  221. Boolean                  include;
  222. {
  223.   XtAppError(XtWidgetToApplicationContext(w), 
  224.          "TextSrc Object: No SCAN function is defined.");
  225. }
  226.  
  227. /*    Function Name: Search
  228.  *    Description: Searchs the text source for the text block passed
  229.  *    Arguments: w - the TextSource Object.
  230.  *                 position - the position to start scanning.
  231.  *                 dir - direction to scan.
  232.  *                 text - the text block to search for.
  233.  *    Returns: XawTextSearchError.
  234.  */
  235.  
  236. /* ARGSUSED */
  237. static XawTextPosition 
  238. Search(w, position, dir, text)
  239. Widget                w;
  240. XawTextPosition       position;
  241. XawTextScanDirection  dir;
  242. XawTextBlock *        text;
  243. {
  244.   return(XawTextSearchError);
  245. }
  246.  
  247. /*    Function Name: ConvertSelection
  248.  *    Description: Dummy selection converter.
  249.  *    Arguments: w - the TextSrc object.
  250.  *                 selection - the current selection atom.
  251.  *                 target    - the current target atom.
  252.  *                 type      - the type to conver the selection to.
  253.  * RETURNED        value, length - the return value that has been converted.
  254.  * RETURNED        format    - the format of the returned value.
  255.  *    Returns: TRUE if the selection has been converted.
  256.  *
  257.  */
  258.  
  259. /* ARGSUSED */
  260. static Boolean
  261. ConvertSelection(w, selection, target, type, value, length, format)
  262. Widget w;
  263. Atom * selection, * target, * type;
  264. XtPointer * value;
  265. unsigned long * length;
  266. int * format;
  267. {
  268.   return(FALSE);
  269. }
  270.  
  271. /*    Function Name: SetSelection
  272.  *    Description: allows special setting of the selection.
  273.  *    Arguments: w - the TextSrc object.
  274.  *                 left, right - bounds of the selection.
  275.  *                 selection - the selection atom.
  276.  *    Returns: none
  277.  */
  278.  
  279. /* ARGSUSED */
  280. static void
  281. SetSelection(w, left, right, selection)
  282. Widget w;
  283. XawTextPosition left, right;
  284. Atom selection;
  285. {
  286.   /* This space intentionally left blank. */
  287. }
  288.  
  289.  
  290. #define done(address, type) \
  291.         { toVal->size = sizeof(type); toVal->addr = (caddr_t) address; }
  292.  
  293. /* ARGSUSED */
  294. static void 
  295. CvtStringToEditMode(args, num_args, fromVal, toVal)
  296. XrmValuePtr args;        /* unused */
  297. Cardinal    *num_args;    /* unused */
  298. XrmValuePtr    fromVal;
  299. XrmValuePtr    toVal;
  300. {
  301.   static XawTextEditType editType;
  302.   static  XrmQuark  QRead, QAppend, QEdit;
  303.   XrmQuark    q;
  304.   char        lowerName[BUFSIZ];
  305.   static Boolean inited = FALSE;
  306.     
  307.   if ( !inited ) {
  308.     QRead   = XrmPermStringToQuark(XtEtextRead);
  309.     QAppend = XrmPermStringToQuark(XtEtextAppend);
  310.     QEdit   = XrmPermStringToQuark(XtEtextEdit);
  311.     inited = TRUE;
  312.   }
  313.  
  314.   XmuCopyISOLatin1Lowered (lowerName, (char *)fromVal->addr);
  315.   q = XrmStringToQuark(lowerName);
  316.  
  317.   if       (q == QRead)          editType = XawtextRead;
  318.   else if (q == QAppend)         editType = XawtextAppend;
  319.   else if (q == QEdit)           editType = XawtextEdit;
  320.   else {
  321.     done(NULL, 0);
  322.     return;
  323.   }
  324.   done(&editType, XawTextEditType);
  325.   return;
  326. }
  327.  
  328.  
  329.  
  330. /************************************************************
  331.  *
  332.  * Public Functions.
  333.  *
  334.  ************************************************************/
  335.  
  336. /*    Function Name: XawTextSourceRead
  337.  *    Description: This function reads the source.
  338.  *    Arguments: w - the TextSrc Object.
  339.  *                 pos - position of the text to retreive.
  340.  * RETURNED        text - text block that will contain returned text.
  341.  *                 length - maximum number of characters to read.
  342.  *    Returns: The number of characters read into the buffer.
  343.  */
  344.  
  345. XawTextPosition
  346. #if NeedFunctionPrototypes
  347. XawTextSourceRead(Widget w, XawTextPosition pos, XawTextBlock *text,
  348.           int length)
  349. #else
  350. XawTextSourceRead(w, pos, text, length)
  351. Widget w;
  352. XawTextPosition pos;
  353. XawTextBlock *text;    
  354. int length;
  355. #endif
  356. {
  357.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  358.  
  359.   return((*class->textSrc_class.Read)(w, pos, text, length));
  360. }
  361.  
  362. /*    Function Name: XawTextSourceReplace.
  363.  *    Description: Replaces a block of text with new text.
  364.  *    Arguments: src - the Text Source Object.
  365.  *                 startPos, endPos - ends of text that will be removed.
  366.  *                 text - new text to be inserted into buffer at startPos.
  367.  *    Returns: XawEditError or XawEditDone.
  368.  */
  369.  
  370. /*ARGSUSED*/
  371. int
  372. #if NeedFunctionPrototypes
  373. XawTextSourceReplace (Widget w, XawTextPosition startPos, 
  374.               XawTextPosition endPos, XawTextBlock *text)
  375. #else
  376. XawTextSourceReplace (w, startPos, endPos, text)
  377. Widget w;
  378. XawTextPosition startPos, endPos;
  379. XawTextBlock *text;
  380. #endif
  381. {
  382.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  383.  
  384.   return((*class->textSrc_class.Replace)(w, startPos, endPos, text));
  385. }
  386.  
  387. /*    Function Name: XawTextSourceScan
  388.  *    Description: Scans the text source for the number and type
  389.  *                   of item specified.
  390.  *    Arguments: w - the TextSrc Object.
  391.  *                 position - the position to start scanning.
  392.  *                 type - type of thing to scan for.
  393.  *                 dir - direction to scan.
  394.  *                 count - which occurance if this thing to search for.
  395.  *                 include - whether or not to include the character found in
  396.  *                           the position that is returned. 
  397.  *    Returns: The position of the text.
  398.  *
  399.  */
  400.  
  401. XawTextPosition
  402. #if NeedFunctionPrototypes
  403. XawTextSourceScan(Widget w, XawTextPosition position,
  404. #if NeedWidePrototypes
  405.           int type, int dir,
  406. #else
  407.           XawTextScanType type, XawTextScanDirection dir,
  408. #endif
  409.           int count,
  410. #if NeedWidePrototypes
  411.           int include)
  412. #else
  413.           Boolean include)
  414. #endif
  415. #else
  416. XawTextSourceScan(w, position, type, dir, count, include)
  417. Widget                w;
  418. XawTextPosition       position;
  419. XawTextScanType       type;
  420. XawTextScanDirection  dir;
  421. int               count;
  422. Boolean                  include;
  423. #endif
  424. {
  425.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  426.  
  427.   return((*class->textSrc_class.Scan)(w, position, type, dir, count, include));
  428. }
  429.  
  430. /*    Function Name: XawTextSourceSearch
  431.  *    Description: Searchs the text source for the text block passed
  432.  *    Arguments: w - the TextSource Object.
  433.  *                 position - the position to start scanning.
  434.  *                 dir - direction to scan.
  435.  *                 text - the text block to search for.
  436.  *    Returns: The position of the text we are searching for or
  437.  *               XawTextSearchError.
  438.  */
  439.  
  440. XawTextPosition 
  441. #if NeedFunctionPrototypes
  442. XawTextSourceSearch(Widget w, XawTextPosition position,
  443. #if NeedWidePrototypes
  444.             int dir,
  445. #else
  446.             XawTextScanDirection dir,
  447. #endif
  448.             XawTextBlock *text)
  449. #else
  450. XawTextSourceSearch(w, position, dir, text)
  451. Widget                w;
  452. XawTextPosition       position;
  453. XawTextScanDirection  dir;
  454. XawTextBlock *        text;
  455. #endif
  456. {
  457.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  458.  
  459.   return((*class->textSrc_class.Search)(w, position, dir, text));
  460. }
  461.  
  462. /*    Function Name: XawTextSourceConvertSelection
  463.  *    Description: Dummy selection converter.
  464.  *    Arguments: w - the TextSrc object.
  465.  *                 selection - the current selection atom.
  466.  *                 target    - the current target atom.
  467.  *                 type      - the type to conver the selection to.
  468.  * RETURNED        value, length - the return value that has been converted.
  469.  * RETURNED        format    - the format of the returned value.
  470.  *    Returns: TRUE if the selection has been converted.
  471.  *
  472.  */
  473.  
  474. Boolean
  475. #if NeedFunctionPrototypes
  476. XawTextSourceConvertSelection(Widget w, Atom *selection, Atom *target, 
  477.                   Atom *type, XtPointer *value,
  478.                   unsigned long *length, int *format)
  479. #else
  480. XawTextSourceConvertSelection(w, selection, 
  481.                   target, type, value, length, format)
  482. Widget w;
  483. Atom * selection, * target, * type;
  484. XtPointer * value;
  485. unsigned long * length;
  486. int * format;
  487. #endif
  488. {
  489.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  490.  
  491.   return((*class->textSrc_class.ConvertSelection)(w, selection, target, type,
  492.                           value, length, format));
  493. }
  494.  
  495. /*    Function Name: XawTextSourceSetSelection
  496.  *    Description: allows special setting of the selection.
  497.  *    Arguments: w - the TextSrc object.
  498.  *                 left, right - bounds of the selection.
  499.  *                 selection - the selection atom.
  500.  *    Returns: none
  501.  */
  502.  
  503. void
  504. #if NeedFunctionPrototypes
  505. XawTextSourceSetSelection(Widget w, XawTextPosition left, 
  506.               XawTextPosition right, Atom selection)
  507. #else
  508. XawTextSourceSetSelection(w, left, right, selection)
  509. Widget w;
  510. XawTextPosition left, right;
  511. Atom selection;
  512. #endif
  513. {
  514.   TextSrcObjectClass class = (TextSrcObjectClass) w->core.widget_class;
  515.  
  516.   (*class->textSrc_class.SetSelection)(w, left, right, selection);
  517. }
  518.  
  519.  
  520.