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 libraries 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 ************************************<+>*************************************/
56 #include <Dt/DtP.h> /* required for DtDirPaths type */
58 #include <codelibs/shellutils.h>
60 #include <Dt/DtNlUtils.h>
62 #include "SharedProcs.h"
72 static char * DELETE_SEPARATOR = "\n ";
75 /************************************************************************
77 * CvtStringToStringList()
78 * Convert a resource string of format:
80 * <item>, <item>, <item>, ...
82 * into an array of strings.
84 ************************************************************************/
87 CvtStringToStringList(
93 String * items = NULL;
94 int size_item_array = 0;
95 String ptr = (String)_DtStripSpaces(string);
98 while ((ptr != NULL) && (*ptr != '\0'))
100 if (num_items >= size_item_array)
102 size_item_array += 20;
104 XtRealloc((char *) items, sizeof(String) * size_item_array);
107 end = DtStrchr(ptr, ',');
111 items[num_items] = strcpy(XtMalloc(strlen(ptr)+1), ptr);
114 ptr = (String)_DtStripSpaces(end + 1);
119 *countPtr = num_items;
124 /************************************************************************
126 * CvtStringListToString
127 * Convert an array of strings into a single string, which can
128 * then be written out to a resource file.
130 ************************************************************************/
133 CvtStringListToString(
139 String string = NULL;
143 * Function for writing an array of string values.
144 * Write all of the information out as a single string, which
145 * we'll parse when we read it back in at a later point in time.
146 * The format for this string is:
148 * <item>, <item>, <item>, ...
153 for (i = 0, offset = 0; i < count; i++)
155 if ((offset + strlen(list[i]) + 3) >= stringSize)
157 /* Grow the buffer */
158 if ((strlen(list[i]) + 3) > 1024)
159 stringSize += strlen(list[i]) + 1024;
162 string = XtRealloc((char *) string, stringSize);
166 (void) sprintf (string + offset, "%s", list[i]);
168 (void) sprintf (string + offset, ", %s", list[i]);
169 offset = strlen(string);
177 /************************************************************************
179 * XtActionProc - VFTextChangeSpace() - this is the callback which gets
180 * called when a user type 'Space' in a text widget which has
181 * this translation tied to it.
183 ***********************************************************************/
189 XtPointer num_params)
192 char *currentDirectoryText;
196 int length, shortest, val = 0;
200 /* get the current text from the widget */
201 value = (char *)XmTextFieldGetString(text);
202 currentDirectoryText = GetRestrictedDirectory(text);
204 /* Extract the path */
207 if(currentDirectoryText == NULL)
209 strcpy (path, value);
213 if (strcmp(currentDirectoryText, "/") == 0)
214 sprintf(path, "%s%s", currentDirectoryText, value);
216 sprintf(path, "%s/%s", currentDirectoryText, value);
219 /* add a '*' at the end of the path so the shellscan will no to return
220 * all possible matches.
222 test = (char *)XtMalloc(strlen(path) + strlen("*") + 1);
223 sprintf(test, "%s%s", path, "*");
225 /* do a shellscan to get all possible matches to the path */
226 temp = (char **)shellscan(test, &val, 0);
227 if(val == 1 && strcmp(*temp, test) != 0)
229 /* there was one and only one match */
230 if(currentDirectoryText == NULL)
231 XmTextFieldSetString(text, *temp);
233 XmTextFieldSetString(text, *temp + strlen(currentDirectoryText));
234 XmTextFieldSetInsertionPosition(text, XmTextFieldGetLastPosition(text));
238 /* more than one filename matches the path */
239 length = strlen(path);
241 /* first find the shortest of the matches */
242 shortest = strlen(temp[0]);
243 for(i = 1; i < val; i++)
250 /* find the most characters which will match in all patterns found.
251 * i.e. if I have /use, /user, /users, and /used, the most chars
252 * which match are /use
255 for(i=0; i < shortest; i++)
257 for(j=0; j < val - 1; j++)
259 if(temp[j][length + i] != temp[j + 1][length + i])
268 temp[0][length + i] = '\0';
270 if(currentDirectoryText == NULL)
271 XmTextFieldSetString(text, *temp);
273 XmTextFieldSetString(text, *temp + strlen(currentDirectoryText));
275 XmTextFieldSetInsertionPosition(text, XmTextFieldGetLastPosition(text));
277 /* ring the bell so the users knows the filename is not complete */
278 XBell(XtDisplay(text), 100);
281 /* no matches found */
282 XBell(XtDisplay(text), 100);
287 /************************************************************************
289 * XtActionProc - CancelOut() - this is the callback which gets
290 * called when a user type 'Space' in a text widget which has
291 * this translation tied to it.
293 ***********************************************************************/
299 XtPointer num_params)
301 FileMgrData *file_mgr_data;
302 FileMgrRec *file_mgr_rec;
305 if(strcmp(widget->core.name, "nameChangeT") == 0)
307 FileViewData *fileViewData;
309 file_mgr_data = ReturnDesktopPtr(XtParent(widget));
310 XtUnmanageChild(widget);
311 XtDestroyWidget(widget);
312 file_mgr_rec = (FileMgrRec *)file_mgr_data->file_mgr_rec;
313 file_mgr_rec->menuStates |= RENAME;
314 if(file_mgr_data->selected_file_count == 1)
316 fileViewData = file_mgr_data->selection_list[0];
317 XmProcessTraversal(fileViewData->widget, XmTRAVERSE_CURRENT);
320 else if(strcmp(widget->core.name, "nameChangeT_DT") == 0)
322 UnpostDTTextField ();
324 else if(strcmp(widget->core.name, FAST_RENAME) == 0)
327 ReturnDesktopPtr(XtParent(XtParent(XtParent(XtParent(widget)))));
328 file_mgr_rec = (FileMgrRec *)file_mgr_data->file_mgr_rec;
330 XmProcessTraversal(file_mgr_rec->file_window, XmTRAVERSE_CURRENT);
331 XtSetArg (args[0], XmNallowShellResize, False);
332 XtSetValues(file_mgr_rec->shell, args, 1);
333 XtUnmanageChild(file_mgr_rec->current_directory_text);
334 XtSetArg (args[0], XmNallowShellResize, True);
335 XtSetValues(file_mgr_rec->shell, args, 1);
336 file_mgr_data->fast_cd_enabled = False;
342 * This is a convenience function used to build an error string, which
343 * is then passed to an error dialog creation function. It will add
344 * the header string to the beginning, the first time this is called
345 * for a particular error string; i.e. when buf == NULL. The string
346 * it creates is owned by the caller, and should be freed up when no
359 Boolean addHeader = False;
361 delta = strlen(DELETE_SEPARATOR) + strlen(string) + 1;
363 buflen = strlen(*buf);
364 if ((buflen + delta) >= *size)
366 if ((*buf == NULL) && header)
368 *size += delta + 512;
371 *buf = XtMalloc(*size);
375 *buf = XtRealloc(*buf, *size);
378 (void) strcpy(*buf, header);
380 (void) strcat(*buf, DELETE_SEPARATOR);
381 (void) strcat(*buf, string);