Change the directory(XmFileSelectionBox)

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

6 posts / 0 new
Last post
Change the directory(XmFileSelectionBox)

Submitted by Anonymous on Fri, 06/15/2001 - 16:34. Developers
Before popping up an XmFileSelectionDialog I would like to change the directory.
I try with XmFileSelectionDoSearch, but noting append.
If someone can show me, who to do this, I really appreciate.

cabvale

With the following code I understood main aspects of FileSelectionBox. I hope it`ll help you.

regards

#include
#include
#include
#include

void dir_search();
void file_search();
void name();

Display *display;
Screen *screen;
int screen_num;
GC gc;
XtAppContext control;

/*********************************************************************************************************/

int main(int argc,char **argv)
{
Font font_id;
Widget mater;
int i;
/**/

mater=XtAppInitialize(&control,"MATER",NULL,0,&argc,argv,NULL,NULL,0);
display = XtDisplay(mater);
screen_num=DefaultScreen(display);
gc=DefaultGC(display,screen_num);
font_id=XLoadFont(display,"fixed");
XSetFont(display,gc,font_id);
name();
XtAppMainLoop(control);
return(0);
}

/*********************************************************************************************************/

void name()
/* Scelta del nome del file */
{
Widget shella,
name,
mesbox,
help_button,
ok_button,
cancel_button,
label[4],
row[4],
acq,
**array;
XmString cancella,
pattern,
directory;
Arg ArgList[10],
lista[8];
int n=0;

/***/

XtSetArg(lista[0],XmNwidth,500);
shella=XtAppCreateShell(NULL,NULL,applicationShellWidgetClass,display,lista,1);
row[0]=XtVaCreateManagedWidget("Selezione",xmRowColumnWidgetClass, shella,XmNorientation, XmVERTICAL,NULL);

directory=XmStringCreate("/",XmFONTLIST_DEFAULT_TAG);
pattern=XmStringCreate("*",XmFONTLIST_DEFAULT_TAG);
XtSetArg(ArgList[n],XmNdirectory,directory);n++;
XtSetArg(ArgList[n],XmNpattern,pattern);n++;

XtSetArg(ArgList[n],XmNdirSearchProc,dir_search);n++;
XtSetArg(ArgList[n],XmNfileSearchProc,file_search);n++;

name=XtCreateManagedWidget("File_Box",xmFileSelectionBoxWidgetClass,row[0],ArgList,n);
XtPopup(shella,XtGrabNone);
XFlush(display);
XtAppMainLoop(control);
}

/*********************************************************************************************************/

void dir_search(Widget widget,XtPointer search_data)
{
char *dir,*dirname;
XmString dirnames[256];
int i = 0,n,j;
DIR *dd;
struct dirent **ee;
struct stat s_buf;

XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) search_data;

if (!XmStringGetLtoR (cbs->dir, XmFONTLIST_DEFAULT_TAG, &dir))
return; /* can`t do anything */

chdir(dir);

n=scandir(dir,&ee,0,alphasort);
for(j=0;j<=n-1;j++)
{
stat(ee[j]->d_name,&s_buf);
if(S_ISDIR(s_buf.st_mode))
if((ee[j]->d_name[0] != `.`) ||
((ee[j]->d_name[0] == `.`) && (strlen(ee[j]->d_name) <= 2)))
{
dirname=XtMalloc( strlen(dir) + 1 + strlen(ee[j]->d_name) + 1);
sprintf(dirname,"%s%s",dir,&ee[j]->d_name);
dirnames[i] = XmStringCreateLocalized(dirname);
i++;
}
}

if (i != 0)
{
XtVaSetValues (widget,
XmNdirListItems, dirnames,
XmNdirListItemCount, i,
XmNlistUpdated, True,
XmNdirectoryValid, True,
NULL);
}
else
XtVaSetValues (widget,
XmNdirListItems, NULL,
XmNdirListItemCount, 0,
XmNlistUpdated, True,
XmNdirectoryValid, True,
NULL);

}

/*********************************************************************************************************/

void file_search(Widget widget,XtPointer search_data)
{
char *name, *p,*dir,*filename,*pattern,*strtmp;
XmString names[256]; /* maximum of 256 files in dir */
int i = 0,j,n,yy,k;
DIR *dd;
struct dirent **ee;
struct stat s_buf;

XmFileSelectionBoxCallbackStruct *cbs =
(XmFileSelectionBoxCallbackStruct *) search_data;

if (!XmStringGetLtoR (cbs->dir, XmFONTLIST_DEFAULT_TAG, &dir))
return; /* can`t do anything */

n=scandir(dir,&ee,0,alphasort);
for(j=0;j<=n-1;j++)
{
stat(ee[j]->d_name,&s_buf);
if(S_ISREG(s_buf.st_mode))
{
if(ee[j]->d_name[0] != `.`)
{
XmStringGetLtoR (cbs->pattern, XmFONTLIST_DEFAULT_TAG, &pattern);
if(strlen(pattern) == 1)
{
filename=XtMalloc( strlen(dir) + 1 + strlen(ee[j]->d_name) + 1);
sprintf(filename,"%s%s",dir,&ee[j]->d_name);
names[i] = XmStringCreateLocalized(filename);
i++;
}
else
{
p=rindex(pattern,`*`);
if (p == pattern)
strtmp=(pattern+1);
if ((p - pattern) > 0)
{
k= p - pattern;
for(yy=0;yy strtmp[yy]=pattern[yy];
}
if (p == NULL)
strtmp=pattern;
name=strstr(ee[j]->d_name,strtmp);
if( name != NULL)
{
filename=XtMalloc( strlen(dir) + 1 + strlen(ee[j]->d_name) + 1);
sprintf(filename,"%s%s",dir,&ee[j]->d_name);
names[i] = XmStringCreateLocalized(filename);
i++;
}
}
}
}
}

if (i)
{
XtVaSetValues (widget,
XmNfileListItems, names,
XmNfileListItemCount, i,
XmNlistUpdated, True,
NULL);
}
else
XtVaSetValues (widget,
XmNfileListItems, NULL,
XmNfileListItemCount, 0,
XmNlistUpdated, True,
NULL);

}

Anonymous

thank you very much cabvale, you are my saviour ...

ICS_support

Can`t you just change the XmNdirectory?

ICS_support

Can`t you just change the XmNdirectory?

cabvale

Yes, it is the simple method to change the directory, but I made that code because I had other problems.