dtwm: basic multihead(xinerama only) support
[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 <Xm/List.h>
38 #include "cmnutils.h"
39
40
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)
47 {
48   int lcv = 0;
49   /*
50   while (items[lcv]) {
51      lcv++;
52   }
53   */
54   if (items) {
55      for (lcv = 0; items[lcv]; lcv++);
56   }
57   return (lcv);
58 }
59
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)
67 {
68 XmStringTable xmstrings = NULL;
69 int count, lcv;
70
71 if (text_strings) {
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]);
76 }
77 return ((XmStringTable)xmstrings);
78 }
79
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)
86 {
87 XmStringContext   context;
88 XmStringCharSet   charset;
89 XmStringDirection direction;
90 Boolean           separator;
91 char              *text_string = NULL, *temp = NULL;
92
93 text_string = (char *)calloc (1, sizeof (char));
94
95 if (xmstring) {
96     if (!XmStringInitContext (&context, xmstring)) {
97       printf("Can't convert compound string.\n");
98       return (NULL);
99     }
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");
105         return (NULL);
106       }
107       text_string = strcpy(text_string, temp);
108     }
109
110     XmStringFreeContext(context);
111     }
112
113 return (text_string);
114 }
115
116 /********************************************************************************/
117 /* delete_all_list_items - removes all items from a list box                    */
118 /* INPUT: Widget list - id of list widget                                       */
119 /* OUTPUT: none                                                                 */
120 /********************************************************************************/
121 void delete_all_list_items (Widget list)
122 {
123 XtArgVal /* int */ item_count = 0;
124
125 XtVaGetValues (list, XmNitemCount, &item_count, NULL);
126 if (item_count > 0) {
127   XmListDeleteItemsPos (list, item_count, 1);
128 }
129 return;
130 }
131
132 /********************************************************************************/
133 /* clear_text_field - removes any text from a text field                        */
134 /* INPUT: Widget textfield - id of text widget                                  */
135 /* OUTPUT: none                                                                 */
136 /********************************************************************************/
137 void clear_text_field (Widget textfield)
138 {
139 XmTextPosition last_pos;
140 char *empty = "";
141
142 last_pos = XmTextGetLastPosition (textfield);
143 XmTextReplace (textfield, 0, last_pos, empty);
144
145 return;
146 }
147
148 /********************************************************************************/
149 /* clear_text - removes any text from a text widget                             */
150 /* INPUT: Widget textwid - id of text widget                                    */
151 /* OUTPUT: none                                                                 */
152 /********************************************************************************/
153 void clear_text (Widget textwid)
154 {
155 XmTextPosition last_pos;
156 char *empty = "";
157
158 last_pos = XmTextGetLastPosition (textwid);
159 XmTextReplace (textwid, 0, last_pos, empty);
160
161 return;
162 }