2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: cmnutils.c /main/4 1995/11/01 16:11:47 rswiston $ */
24 /***************************************************************************/
26 /* Utility Functions */
28 /***************************************************************************/
34 #include <Xm/RowColumnP.h>
35 #include <Xm/MessageB.h>
41 /********************************************************************************/
42 /* countItems - counts the number of items in a null terminated array */
43 /* INPUT: char **items - null terminated array */
44 /* OUTPUT: int lcv - number of items in array */
45 /********************************************************************************/
46 int countItems (char **items)
55 for (lcv = 0; items[lcv]; lcv++);
60 /********************************************************************************/
61 /* TextStringsToXmStrings - Given an array of C text strings returns an */
62 /* array of XmStrings. */
63 /* INPUT: char **text_strings - array of C style strings */
64 /* OUTPUT: XmStringTable xmstrings - an array Motif compound strings */
65 /********************************************************************************/
66 XmStringTable TextStringsToXmStrings (char **text_strings)
68 XmStringTable xmstrings = NULL;
72 count = countItems (text_strings);
73 xmstrings = (XmStringTable) calloc (sizeof(XmString), (count));
74 for (lcv = 0; lcv < count; lcv++)
75 xmstrings[lcv] = (XmString) XmStringCreateSimple (text_strings[lcv]);
77 return ((XmStringTable)xmstrings);
80 /********************************************************************************/
81 /* XmStringToText - Given an XmString returns a C character text string. */
82 /* INPUT: XmString xmstring - a Motif compound string */
83 /* OUTPUT: char *text_string - C style string */
84 /********************************************************************************/
85 char *XmStringToText (XmString xmstring)
87 XmStringContext context;
88 XmStringCharSet charset;
89 XmStringDirection direction;
91 char *text_string = NULL, *temp = NULL;
93 text_string = (char *)calloc (1, sizeof (char));
96 if (!XmStringInitContext (&context, xmstring)) {
97 printf("Can't convert compound string.\n");
100 while (XmStringGetNextSegment (context, &temp, &charset,
101 &direction, &separator)) {
102 text_string = (char *)realloc (text_string, strlen (temp)+1);
103 if (text_string == NULL) {
104 printf("Can't allocate space for file name.\n");
107 text_string = strcpy(text_string, temp);
110 XmStringFreeContext(context);
113 return (text_string);
116 /********************************************************************************/
117 /* delete_all_list_items - removes all items from a list box */
118 /* INPUT: Widget list - id of list widget */
120 /********************************************************************************/
121 void delete_all_list_items (Widget list)
123 XtArgVal /* int */ item_count = 0;
125 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
126 if (item_count > 0) {
127 XmListDeleteItemsPos (list, item_count, 1);
132 /********************************************************************************/
133 /* clear_text_field - removes any text from a text field */
134 /* INPUT: Widget textfield - id of text widget */
136 /********************************************************************************/
137 void clear_text_field (Widget textfield)
139 XmTextPosition last_pos;
142 last_pos = XmTextGetLastPosition (textfield);
143 XmTextReplace (textfield, 0, last_pos, empty);
148 /********************************************************************************/
149 /* clear_text - removes any text from a text widget */
150 /* INPUT: Widget textwid - id of text widget */
152 /********************************************************************************/
153 void clear_text (Widget textwid)
155 XmTextPosition last_pos;
158 last_pos = XmTextGetLastPosition (textwid);
159 XmTextReplace (textwid, 0, last_pos, empty);