1 /* $XConsortium: cmnutils.c /main/4 1995/11/01 16:11:47 rswiston $ */
2 /***************************************************************************/
4 /* Utility Functions */
6 /***************************************************************************/
11 #include <Xm/RowColumnP.h>
12 #include <Xm/MessageB.h>
17 /********************************************************************************/
18 /* countItems - counts the number of items in a null terminated array */
19 /* INPUT: char **items - null terminated array */
20 /* OUTPUT: int lcv - number of items in array */
21 /********************************************************************************/
22 int countItems (char **items)
31 for (lcv = 0; items[lcv]; lcv++);
36 /********************************************************************************/
37 /* TextStringsToXmStrings - Given an array of C text strings returns an */
38 /* array of XmStrings. */
39 /* INPUT: char **text_strings - array of C style strings */
40 /* OUTPUT: XmStringTable xmstrings - an array Motif compound strings */
41 /********************************************************************************/
42 XmStringTable TextStringsToXmStrings (char **text_strings)
44 XmStringTable xmstrings = NULL;
48 count = countItems (text_strings);
49 xmstrings = (XmStringTable) calloc (sizeof(XmString), (count));
50 for (lcv = 0; lcv < count; lcv++)
51 xmstrings[lcv] = (XmString) XmStringCreateSimple (text_strings[lcv]);
53 return ((XmStringTable)xmstrings);
56 /********************************************************************************/
57 /* XmStringToText - Given an XmString returns a C character text string. */
58 /* INPUT: XmString xmstring - a Motif compound string */
59 /* OUTPUT: char *text_string - C style string */
60 /********************************************************************************/
61 char *XmStringToText (XmString xmstring)
63 XmStringContext context;
64 XmStringCharSet charset;
65 XmStringDirection direction;
67 char *text_string = NULL, *temp = NULL;
69 text_string = (char *)calloc (1, sizeof (char));
72 if (!XmStringInitContext (&context, xmstring)) {
73 printf("Can't convert compound string.\n");
76 while (XmStringGetNextSegment (context, &temp, &charset,
77 &direction, &separator)) {
78 text_string = (char *)realloc (text_string, strlen (temp)+1);
79 if (text_string == NULL) {
80 printf("Can't allocate space for file name.\n");
83 text_string = strcpy(text_string, temp);
86 XmStringFreeContext(context);
92 /********************************************************************************/
93 /* delete_all_list_items - removes all items from a list box */
94 /* INPUT: Widget list - id of list widget */
96 /********************************************************************************/
97 void delete_all_list_items (Widget list)
101 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
102 if (item_count > 0) {
103 XmListDeleteItemsPos (list, item_count, 1);
108 /********************************************************************************/
109 /* clear_text_field - removes any text from a text field */
110 /* INPUT: Widget textfield - id of text widget */
112 /********************************************************************************/
113 void clear_text_field (Widget textfield)
115 XmTextPosition last_pos;
118 last_pos = XmTextGetLastPosition (textfield);
119 XmTextReplace (textfield, 0, last_pos, empty);
124 /********************************************************************************/
125 /* clear_text - removes any text from a text widget */
126 /* INPUT: Widget textwid - id of text widget */
128 /********************************************************************************/
129 void clear_text (Widget textwid)
131 XmTextPosition last_pos;
134 last_pos = XmTextGetLastPosition (textwid);
135 XmTextReplace (textwid, 0, last_pos, empty);