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 /* $TOG: Common.c /main/5 1998/05/07 13:27:44 rafi $ */
24 /************************************<+>*************************************
25 ****************************************************************************
29 * COMPONENT_NAME: Desktop File Manager (dtfile)
31 * DESCRIPTION: Source file for some shared dialog code
33 * FUNCTIONS: AddString
35 * CvtStringListToString
36 * CvtStringToStringList
39 * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
40 * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
41 * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
42 * (c) Copyright 1993, 1994, 1995 Novell, Inc.
44 ****************************************************************************
45 ************************************<+>*************************************/
55 #include <Dt/DtP.h> /* required for DtDirPaths type */
56 #include <Dt/DtNlUtils.h>
58 #include "SharedProcs.h"
68 static char * DELETE_SEPARATOR = "\n ";
71 /************************************************************************
73 * CvtStringToStringList()
74 * Convert a resource string of format:
76 * <item>, <item>, <item>, ...
78 * into an array of strings.
80 ************************************************************************/
83 CvtStringToStringList(
89 String * items = NULL;
90 int size_item_array = 0;
91 String ptr = (String)_DtStripSpaces(string);
94 while ((ptr != NULL) && (*ptr != '\0'))
96 if (num_items >= size_item_array)
98 size_item_array += 20;
100 XtRealloc((char *) items, sizeof(String) * size_item_array);
103 end = DtStrchr(ptr, ',');
107 items[num_items] = strcpy(XtMalloc(strlen(ptr)+1), ptr);
110 ptr = (String)_DtStripSpaces(end + 1);
115 *countPtr = num_items;
120 /************************************************************************
122 * CvtStringListToString
123 * Convert an array of strings into a single string, which can
124 * then be written out to a resource file.
126 ************************************************************************/
129 CvtStringListToString(
135 String string = NULL;
139 * Function for writing an array of string values.
140 * Write all of the information out as a single string, which
141 * we'll parse when we read it back in at a later point in time.
142 * The format for this string is:
144 * <item>, <item>, <item>, ...
149 for (i = 0, offset = 0; i < count; i++)
151 if ((offset + strlen(list[i]) + 3) >= stringSize)
153 /* Grow the buffer */
154 if ((strlen(list[i]) + 3) > 1024)
155 stringSize += strlen(list[i]) + 1024;
158 string = XtRealloc((char *) string, stringSize);
162 (void) sprintf (string + offset, "%s", list[i]);
164 (void) sprintf (string + offset, ", %s", list[i]);
165 offset = strlen(string);
173 /************************************************************************
175 * XtActionProc - VFTextChangeSpace() - this is the callback which gets
176 * called when a user type 'Space' in a text widget which has
177 * this translation tied to it.
179 ***********************************************************************/
185 XtPointer num_params)
188 char *currentDirectoryText;
192 int length, shortest, val = 0;
196 /* get the current text from the widget */
197 value = (char *)XmTextFieldGetString(text);
198 currentDirectoryText = GetRestrictedDirectory(text);
200 /* Extract the path */
203 if(currentDirectoryText == NULL)
205 strcpy (path, value);
209 if (strcmp(currentDirectoryText, "/") == 0)
210 sprintf(path, "%s%s", currentDirectoryText, value);
212 sprintf(path, "%s/%s", currentDirectoryText, value);
215 /* add a '*' at the end of the path so the shellscan will no to return
216 * all possible matches.
218 test = (char *)XtMalloc(strlen(path) + strlen("*") + 1);
219 sprintf(test, "%s%s", path, "*");
221 /* do a shellscan to get all possible matches to the path */
222 temp = (char **)shellscan(test, &val, 0);
223 if(val == 1 && strcmp(*temp, test) != 0)
225 /* there was one and only one match */
226 if(currentDirectoryText == NULL)
227 XmTextFieldSetString(text, *temp);
229 XmTextFieldSetString(text, *temp + strlen(currentDirectoryText));
230 XmTextFieldSetInsertionPosition(text, XmTextFieldGetLastPosition(text));
234 /* more than one filename matches the path */
235 length = strlen(path);
237 /* first find the shortest of the matches */
238 shortest = strlen(temp[0]);
239 for(i = 1; i < val; i++)
246 /* find the most characters which will match in all patterns found.
247 * i.e. if I have /use, /user, /users, and /used, the most chars
248 * which match are /use
251 for(i=0; i < shortest; i++)
253 for(j=0; j < val - 1; j++)
255 if(temp[j][length + i] != temp[j + 1][length + i])
264 temp[0][length + i] = '\0';
266 if(currentDirectoryText == NULL)
267 XmTextFieldSetString(text, *temp);
269 XmTextFieldSetString(text, *temp + strlen(currentDirectoryText));
271 XmTextFieldSetInsertionPosition(text, XmTextFieldGetLastPosition(text));
273 /* ring the bell so the users knows the filename is not complete */
274 XBell(XtDisplay(text), 100);
277 /* no matches found */
278 XBell(XtDisplay(text), 100);
283 /************************************************************************
285 * XtActionProc - CancelOut() - this is the callback which gets
286 * called when a user type 'Space' in a text widget which has
287 * this translation tied to it.
289 ***********************************************************************/
295 XtPointer num_params)
297 FileMgrData *file_mgr_data;
298 FileMgrRec *file_mgr_rec;
301 if(strcmp(widget->core.name, "nameChangeT") == 0)
303 FileViewData *fileViewData;
305 file_mgr_data = ReturnDesktopPtr(XtParent(widget));
306 XtUnmanageChild(widget);
307 XtDestroyWidget(widget);
308 file_mgr_rec = (FileMgrRec *)file_mgr_data->file_mgr_rec;
309 file_mgr_rec->menuStates |= RENAME;
310 if(file_mgr_data->selected_file_count == 1)
312 fileViewData = file_mgr_data->selection_list[0];
313 XmProcessTraversal(fileViewData->widget, XmTRAVERSE_CURRENT);
316 else if(strcmp(widget->core.name, "nameChangeT_DT") == 0)
318 UnpostDTTextField ();
320 else if(strcmp(widget->core.name, FAST_RENAME) == 0)
323 ReturnDesktopPtr(XtParent(XtParent(XtParent(XtParent(widget)))));
324 file_mgr_rec = (FileMgrRec *)file_mgr_data->file_mgr_rec;
326 XmProcessTraversal(file_mgr_rec->file_window, XmTRAVERSE_CURRENT);
327 XtSetArg (args[0], XmNallowShellResize, False);
328 XtSetValues(file_mgr_rec->shell, args, 1);
329 XtUnmanageChild(file_mgr_rec->current_directory_text);
330 XtSetArg (args[0], XmNallowShellResize, True);
331 XtSetValues(file_mgr_rec->shell, args, 1);
332 file_mgr_data->fast_cd_enabled = False;
338 * This is a convenience function used to build an error string, which
339 * is then passed to an error dialog creation function. It will add
340 * the header string to the beginning, the first time this is called
341 * for a particular error string; i.e. when buf == NULL. The string
342 * it creates is owned by the caller, and should be freed up when no
355 Boolean addHeader = False;
357 delta = strlen(DELETE_SEPARATOR) + strlen(string) + 1;
359 buflen = strlen(*buf);
360 if ((buflen + delta) >= *size)
362 if ((*buf == NULL) && header)
364 *size += delta + 512;
367 *buf = XtMalloc(*size);
371 *buf = XtRealloc(*buf, *size);
374 (void) strcpy(*buf, header);
376 (void) strcat(*buf, DELETE_SEPARATOR);
377 (void) strcat(*buf, string);