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 librararies 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>
40 /********************************************************************************/
41 /* countItems - counts the number of items in a null terminated array */
42 /* INPUT: char **items - null terminated array */
43 /* OUTPUT: int lcv - number of items in array */
44 /********************************************************************************/
45 int countItems (char **items)
54 for (lcv = 0; items[lcv]; lcv++);
59 /********************************************************************************/
60 /* TextStringsToXmStrings - Given an array of C text strings returns an */
61 /* array of XmStrings. */
62 /* INPUT: char **text_strings - array of C style strings */
63 /* OUTPUT: XmStringTable xmstrings - an array Motif compound strings */
64 /********************************************************************************/
65 XmStringTable TextStringsToXmStrings (char **text_strings)
67 XmStringTable xmstrings = NULL;
71 count = countItems (text_strings);
72 xmstrings = (XmStringTable) calloc (sizeof(XmString), (count));
73 for (lcv = 0; lcv < count; lcv++)
74 xmstrings[lcv] = (XmString) XmStringCreateSimple (text_strings[lcv]);
76 return ((XmStringTable)xmstrings);
79 /********************************************************************************/
80 /* XmStringToText - Given an XmString returns a C character text string. */
81 /* INPUT: XmString xmstring - a Motif compound string */
82 /* OUTPUT: char *text_string - C style string */
83 /********************************************************************************/
84 char *XmStringToText (XmString xmstring)
86 XmStringContext context;
87 XmStringCharSet charset;
88 XmStringDirection direction;
90 char *text_string = NULL, *temp = NULL;
92 text_string = (char *)calloc (1, sizeof (char));
95 if (!XmStringInitContext (&context, xmstring)) {
96 printf("Can't convert compound string.\n");
99 while (XmStringGetNextSegment (context, &temp, &charset,
100 &direction, &separator)) {
101 text_string = (char *)realloc (text_string, strlen (temp)+1);
102 if (text_string == NULL) {
103 printf("Can't allocate space for file name.\n");
106 text_string = strcpy(text_string, temp);
109 XmStringFreeContext(context);
112 return (text_string);
115 /********************************************************************************/
116 /* delete_all_list_items - removes all items from a list box */
117 /* INPUT: Widget list - id of list widget */
119 /********************************************************************************/
120 void delete_all_list_items (Widget list)
124 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
125 if (item_count > 0) {
126 XmListDeleteItemsPos (list, item_count, 1);
131 /********************************************************************************/
132 /* clear_text_field - removes any text from a text field */
133 /* INPUT: Widget textfield - id of text widget */
135 /********************************************************************************/
136 void clear_text_field (Widget textfield)
138 XmTextPosition last_pos;
141 last_pos = XmTextGetLastPosition (textfield);
142 XmTextReplace (textfield, 0, last_pos, empty);
147 /********************************************************************************/
148 /* clear_text - removes any text from a text widget */
149 /* INPUT: Widget textwid - id of text widget */
151 /********************************************************************************/
152 void clear_text (Widget textwid)
154 XmTextPosition last_pos;
157 last_pos = XmTextGetLastPosition (textwid);
158 XmTextReplace (textwid, 0, last_pos, empty);