Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcreate / cmnutils.c
1 /* $XConsortium: cmnutils.c /main/4 1995/11/01 16:11:47 rswiston $ */
2 /***************************************************************************/
3 /*                                                                         */
4 /*  Utility Functions                                                      */
5 /*                                                                         */
6 /***************************************************************************/
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <Xm/Xm.h>
11 #include <Xm/RowColumnP.h>
12 #include <Xm/MessageB.h>
13 #include <Xm/Text.h>
14 #include "cmnutils.h"
15
16
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)
23 {
24   int lcv = 0;
25   /*
26   while (items[lcv]) {
27      lcv++;
28   }
29   */
30   if (items) {
31      for (lcv = 0; items[lcv]; lcv++);
32   }
33   return (lcv);
34 }
35
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)
43 {
44 XmStringTable xmstrings = NULL;
45 int count, lcv;
46
47 if (text_strings) {
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]);
52 }
53 return ((XmStringTable)xmstrings);
54 }
55
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)
62 {
63 XmStringContext   context;
64 XmStringCharSet   charset;
65 XmStringDirection direction;
66 Boolean           separator;
67 char              *text_string = NULL, *temp = NULL;
68
69 text_string = (char *)calloc (1, sizeof (char));
70
71 if (xmstring) {
72     if (!XmStringInitContext (&context, xmstring)) {
73       printf("Can't convert compound string.\n");
74       return (NULL);
75     }
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");
81         return (NULL);
82       }
83       text_string = strcpy(text_string, temp);
84     }
85
86     XmStringFreeContext(context);
87     }
88
89 return (text_string);
90 }
91
92 /********************************************************************************/
93 /* delete_all_list_items - removes all items from a list box                    */
94 /* INPUT: Widget list - id of list widget                                       */
95 /* OUTPUT: none                                                                 */
96 /********************************************************************************/
97 void delete_all_list_items (Widget list)
98 {
99 int item_count = 0;
100
101 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
102 if (item_count > 0) {
103   XmListDeleteItemsPos (list, item_count, 1);
104 }
105 return;
106 }
107
108 /********************************************************************************/
109 /* clear_text_field - removes any text from a text field                        */
110 /* INPUT: Widget textfield - id of text widget                                  */
111 /* OUTPUT: none                                                                 */
112 /********************************************************************************/
113 void clear_text_field (Widget textfield)
114 {
115 XmTextPosition last_pos;
116 char *empty = "";
117
118 last_pos = XmTextGetLastPosition (textfield);
119 XmTextReplace (textfield, 0, last_pos, empty);
120
121 return;
122 }
123
124 /********************************************************************************/
125 /* clear_text - removes any text from a text widget                             */
126 /* INPUT: Widget textwid - id of text widget                                    */
127 /* OUTPUT: none                                                                 */
128 /********************************************************************************/
129 void clear_text (Widget textwid)
130 {
131 XmTextPosition last_pos;
132 char *empty = "";
133
134 last_pos = XmTextGetLastPosition (textwid);
135 XmTextReplace (textwid, 0, last_pos, empty);
136
137 return;
138 }