dtcreate: warning prevention.
[oweals/cde.git] / cde / programs / dtcreate / cmnutils.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: cmnutils.c /main/4 1995/11/01 16:11:47 rswiston $ */
24 /***************************************************************************/
25 /*                                                                         */
26 /*  Utility Functions                                                      */
27 /*                                                                         */
28 /***************************************************************************/
29
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <Xm/Xm.h>
34 #include <Xm/RowColumnP.h>
35 #include <Xm/MessageB.h>
36 #include <Xm/Text.h>
37 #include "cmnutils.h"
38
39
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)
46 {
47   int lcv = 0;
48   /*
49   while (items[lcv]) {
50      lcv++;
51   }
52   */
53   if (items) {
54      for (lcv = 0; items[lcv]; lcv++);
55   }
56   return (lcv);
57 }
58
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)
66 {
67 XmStringTable xmstrings = NULL;
68 int count, lcv;
69
70 if (text_strings) {
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]);
75 }
76 return ((XmStringTable)xmstrings);
77 }
78
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)
85 {
86 XmStringContext   context;
87 XmStringCharSet   charset;
88 XmStringDirection direction;
89 Boolean           separator;
90 char              *text_string = NULL, *temp = NULL;
91
92 text_string = (char *)calloc (1, sizeof (char));
93
94 if (xmstring) {
95     if (!XmStringInitContext (&context, xmstring)) {
96       printf("Can't convert compound string.\n");
97       return (NULL);
98     }
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");
104         return (NULL);
105       }
106       text_string = strcpy(text_string, temp);
107     }
108
109     XmStringFreeContext(context);
110     }
111
112 return (text_string);
113 }
114
115 /********************************************************************************/
116 /* delete_all_list_items - removes all items from a list box                    */
117 /* INPUT: Widget list - id of list widget                                       */
118 /* OUTPUT: none                                                                 */
119 /********************************************************************************/
120 void delete_all_list_items (Widget list)
121 {
122 int item_count = 0;
123
124 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
125 if (item_count > 0) {
126   XmListDeleteItemsPos (list, item_count, 1);
127 }
128 return;
129 }
130
131 /********************************************************************************/
132 /* clear_text_field - removes any text from a text field                        */
133 /* INPUT: Widget textfield - id of text widget                                  */
134 /* OUTPUT: none                                                                 */
135 /********************************************************************************/
136 void clear_text_field (Widget textfield)
137 {
138 XmTextPosition last_pos;
139 char *empty = "";
140
141 last_pos = XmTextGetLastPosition (textfield);
142 XmTextReplace (textfield, 0, last_pos, empty);
143
144 return;
145 }
146
147 /********************************************************************************/
148 /* clear_text - removes any text from a text widget                             */
149 /* INPUT: Widget textwid - id of text widget                                    */
150 /* OUTPUT: none                                                                 */
151 /********************************************************************************/
152 void clear_text (Widget textwid)
153 {
154 XmTextPosition last_pos;
155 char *empty = "";
156
157 last_pos = XmTextGetLastPosition (textwid);
158 XmTextReplace (textwid, 0, last_pos, empty);
159
160 return;
161 }