Use C++ linker
[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 <Xm/Xm.h>
33 #include <Xm/RowColumnP.h>
34 #include <Xm/MessageB.h>
35 #include <Xm/Text.h>
36 #include "cmnutils.h"
37
38
39 /********************************************************************************/
40 /* countItems - counts the number of items in a null terminated array           */
41 /* INPUT: char **items - null terminated array                                  */
42 /* OUTPUT: int lcv - number of items in array                                   */
43 /********************************************************************************/
44 int countItems (char **items)
45 {
46   int lcv = 0;
47   /*
48   while (items[lcv]) {
49      lcv++;
50   }
51   */
52   if (items) {
53      for (lcv = 0; items[lcv]; lcv++);
54   }
55   return (lcv);
56 }
57
58 /********************************************************************************/
59 /* TextStringsToXmStrings - Given an array of C text strings returns an         */
60 /*                          array of XmStrings.                                 */
61 /* INPUT:  char **text_strings - array of C style strings                       */
62 /* OUTPUT: XmStringTable  xmstrings - an array Motif compound strings           */
63 /********************************************************************************/
64 XmStringTable TextStringsToXmStrings (char **text_strings)
65 {
66 XmStringTable xmstrings = NULL;
67 int count, lcv;
68
69 if (text_strings) {
70     count = countItems (text_strings);
71     xmstrings = (XmStringTable) calloc (sizeof(XmString), (count));
72     for (lcv = 0; lcv < count; lcv++)
73         xmstrings[lcv] = (XmString) XmStringCreateSimple (text_strings[lcv]);
74 }
75 return ((XmStringTable)xmstrings);
76 }
77
78 /********************************************************************************/
79 /* XmStringToText - Given an XmString returns a C character text string.        */
80 /* INPUT:  XmString  xmstring - a Motif compound string                         */
81 /*  OUTPUT: char *text_string - C style string                                  */
82 /********************************************************************************/
83 char *XmStringToText (XmString xmstring)
84 {
85 XmStringContext   context;
86 XmStringCharSet   charset;
87 XmStringDirection direction;
88 Boolean           separator;
89 char              *text_string = NULL, *temp = NULL;
90
91 text_string = (char *)calloc (1, sizeof (char));
92
93 if (xmstring) {
94     if (!XmStringInitContext (&context, xmstring)) {
95       printf("Can't convert compound string.\n");
96       return (NULL);
97     }
98     while  (XmStringGetNextSegment (context, &temp, &charset,
99                                     &direction, &separator)) {
100       text_string = (char *)realloc (text_string, strlen (temp)+1);
101       if (text_string == NULL) {
102         printf("Can't allocate space for file name.\n");
103         return (NULL);
104       }
105       text_string = strcpy(text_string, temp);
106     }
107
108     XmStringFreeContext(context);
109     }
110
111 return (text_string);
112 }
113
114 /********************************************************************************/
115 /* delete_all_list_items - removes all items from a list box                    */
116 /* INPUT: Widget list - id of list widget                                       */
117 /* OUTPUT: none                                                                 */
118 /********************************************************************************/
119 void delete_all_list_items (Widget list)
120 {
121 int item_count = 0;
122
123 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
124 if (item_count > 0) {
125   XmListDeleteItemsPos (list, item_count, 1);
126 }
127 return;
128 }
129
130 /********************************************************************************/
131 /* clear_text_field - removes any text from a text field                        */
132 /* INPUT: Widget textfield - id of text widget                                  */
133 /* OUTPUT: none                                                                 */
134 /********************************************************************************/
135 void clear_text_field (Widget textfield)
136 {
137 XmTextPosition last_pos;
138 char *empty = "";
139
140 last_pos = XmTextGetLastPosition (textfield);
141 XmTextReplace (textfield, 0, last_pos, empty);
142
143 return;
144 }
145
146 /********************************************************************************/
147 /* clear_text - removes any text from a text widget                             */
148 /* INPUT: Widget textwid - id of text widget                                    */
149 /* OUTPUT: none                                                                 */
150 /********************************************************************************/
151 void clear_text (Widget textwid)
152 {
153 XmTextPosition last_pos;
154 char *empty = "";
155
156 last_pos = XmTextGetLastPosition (textwid);
157 XmTextReplace (textwid, 0, last_pos, empty);
158
159 return;
160 }