#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "getfiles.h"
#include "fileUtils.h"
#include "misc.h"
#include "nedit_malloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <X11/keysym.h>
#include <Xm/Xm.h>
#include <Xm/XmAll.h>
#include "utils.h"
#include "fileUtils.h"
#include "filedialog.h"
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
#define MAX_ARGS 20
#define PERMS 0666
#define MAX_LIST_KEYSTROKES 100
#define MAX_LIST_KESTROKE_WAIT 2000
#define SET_ONE_RSRC(widget, name, newValue) \
{ \
static Arg tmpargs[
1] = {{name, (XtArgVal)
0}}; \
tmpargs[
0].value = (XtArgVal)newValue; \
XtSetValues(widget, tmpargs,
1); \
}
enum yesNoValues {ynNone, ynYes, ynNo};
static XmString DefaultDirectory =
NULL;
static char* DefaultDirectoryStr =
NULL;
static void createYesNoDialog(Widget parent);
static void createErrorDialog(Widget parent);
static int doYesNoDialog(
const char *msg);
static void doErrorDialog(
const char *errorString,
const char *filename);
static void errorOKCB(Widget w,
caddr_t client_data,
caddr_t call_data);
static void yesNoOKCB(Widget w,
caddr_t client_data,
caddr_t call_data);
static void yesNoCancelCB(Widget w,
caddr_t client_data,
caddr_t call_data);
static Widget YesNoDialog;
static int YesNoResult;
static Widget ErrorDialog;
static int ErrorDone;
int GetExistingFilename(Widget parent,
char *promptString, FileSelection *file)
{
return FileDialog(parent, promptString, file,
FILEDIALOG_OPEN,
NULL);
}
int GetNewFilename(Widget parent,
char *promptString, FileSelection *file,
char *defaultName)
{
return FileDialog(parent, promptString, file,
FILEDIALOG_SAVE, defaultName);
}
char *GetFileDialogDefaultDirectory(
void)
{
char *string;
if (DefaultDirectory ==
NULL)
return NULL;
XmStringGetLtoR(DefaultDirectory, XmSTRING_DEFAULT_CHARSET, &string);
return string;
}
void SetFileDialogDefaultDirectory(
char *dir)
{
if (DefaultDirectory !=
NULL)
XmStringFree(DefaultDirectory);
DefaultDirectory = dir==
NULL ?
NULL : XmStringCreateSimple(dir);
if(DefaultDirectoryStr) {
NEditFree(DefaultDirectoryStr);
}
DefaultDirectoryStr = NEditStrdup(dir);
}
char* GetDefaultDirectoryStr(
void)
{
return DefaultDirectoryStr;
}
static void createYesNoDialog(Widget parent)
{
XmString buttonString;
int n;
Arg args[
MAX_ARGS];
n =
0;
XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
XtSetArg(args[n], XmNtitle,
" "); n++;
YesNoDialog = CreateQuestionDialog(parent,
"yesNo", args, n);
XtAddCallback (YesNoDialog, XmNokCallback, (XtCallbackProc)yesNoOKCB,
NULL);
XtAddCallback (YesNoDialog, XmNcancelCallback,
(XtCallbackProc)yesNoCancelCB,
NULL);
XtUnmanageChild(XmMessageBoxGetChild (YesNoDialog, XmDIALOG_HELP_BUTTON));
buttonString = XmStringCreateSimple(
"Yes");
SET_ONE_RSRC(YesNoDialog, XmNokLabelString, buttonString);
XmStringFree(buttonString);
buttonString = XmStringCreateSimple(
"No");
SET_ONE_RSRC(YesNoDialog, XmNcancelLabelString, buttonString);
XmStringFree(buttonString);
}
static void createErrorDialog(Widget parent)
{
XmString buttonString;
int n;
Arg args[
MAX_ARGS];
n =
0;
XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
XtSetArg(args[n], XmNtitle,
" "); n++;
ErrorDialog = CreateErrorDialog(parent,
"error", args, n);
XtAddCallback(ErrorDialog, XmNcancelCallback, (XtCallbackProc)errorOKCB,
NULL);
XtUnmanageChild(XmMessageBoxGetChild(ErrorDialog, XmDIALOG_OK_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(ErrorDialog, XmDIALOG_HELP_BUTTON));
buttonString = XmStringCreateLtoR(
"OK", XmSTRING_DEFAULT_CHARSET);
XtVaSetValues(ErrorDialog, XmNcancelLabelString, buttonString,
NULL);
XtVaSetValues(XmMessageBoxGetChild(ErrorDialog, XmDIALOG_CANCEL_BUTTON),
XmNmarginWidth,
BUTTON_WIDTH_MARGIN,
NULL);
XmStringFree(buttonString);
}
int OverrideFileDialog(Widget parent,
const char *filename)
{
createYesNoDialog(parent);
int ret = doYesNoDialog(filename);
XtDestroyWidget(YesNoDialog);
YesNoDialog =
NULL;
return ret;
}
void FileOpenErrorDialog(Widget parent,
const char *filename)
{
createErrorDialog(parent);
doErrorDialog(
"Error: can''t open %s ", filename);
return;
}
static int doYesNoDialog(
const char *filename)
{
char string[
255];
XmString mString;
YesNoResult = ynNone;
sprintf(string,
"File %s already exists,\nOk to overwrite?", filename);
mString = XmStringCreateLtoR(string, XmSTRING_DEFAULT_CHARSET);
SET_ONE_RSRC(YesNoDialog, XmNmessageString, mString);
XmStringFree(mString);
ManageDialogCenteredOnPointer(YesNoDialog);
while (YesNoResult == ynNone)
XtAppProcessEvent(XtWidgetToApplicationContext(YesNoDialog), XtIMAll);
XtUnmanageChild(YesNoDialog);
if (YesNoResult == ynYes)
PopDownBugPatch(YesNoDialog);
return YesNoResult == ynYes;
}
static void doErrorDialog(
const char *errorString,
const char *filename)
{
char string[
255];
XmString mString;
ErrorDone = False;
sprintf(string, errorString, filename);
mString = XmStringCreateLtoR(string, XmSTRING_DEFAULT_CHARSET);
SET_ONE_RSRC(ErrorDialog, XmNmessageString, mString);
XmStringFree(mString);
ManageDialogCenteredOnPointer(ErrorDialog);
while (!ErrorDone)
XtAppProcessEvent (XtWidgetToApplicationContext(ErrorDialog), XtIMAll);
XtUnmanageChild(ErrorDialog);
}
static void yesNoOKCB(Widget w,
caddr_t client_data,
caddr_t call_data)
{
YesNoResult = ynYes;
}
static void errorOKCB(Widget w,
caddr_t client_data,
caddr_t call_data)
{
ErrorDone = True;
}
static void yesNoCancelCB(Widget w,
caddr_t client_data,
caddr_t call_data)
{
YesNoResult = ynNo;
}
Widget CreateFormatButtons(
Widget form,
Widget bottom,
int format,
Widget *u,
Widget *d,
Widget *m)
{
XmString str;
Widget formatBtns = XtVaCreateManagedWidget(
"formatBtns",
xmRowColumnWidgetClass, form,
XmNradioBehavior, XmONE_OF_MANY,
XmNorientation, XmHORIZONTAL,
XmNpacking, XmPACK_TIGHT,
XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, bottom,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNleftOffset,
5,
XmNrightOffset,
5,
XmNbottomOffset,
2,
NULL);
XtVaCreateManagedWidget(
"formatBtns", xmLabelWidgetClass, formatBtns,
XmNlabelString, str=XmStringCreateSimple(
"Format:"),
NULL);
XmStringFree(str);
*u = XtVaCreateManagedWidget(
"unixFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, str = XmStringCreateSimple(
"Unix"),
XmNset, format ==
UNIX_FILE_FORMAT,
XmNuserData, (XtPointer)
UNIX_FILE_FORMAT,
XmNmarginHeight,
0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic,
'U',
NULL);
XmStringFree(str);
*d = XtVaCreateManagedWidget(
"dosFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, str = XmStringCreateSimple(
"DOS"),
XmNset, format ==
DOS_FILE_FORMAT,
XmNuserData, (XtPointer)
DOS_FILE_FORMAT,
XmNmarginHeight,
0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic,
'O',
NULL);
XmStringFree(str);
*m= XtVaCreateManagedWidget(
"macFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, str = XmStringCreateSimple(
"Macintosh"),
XmNset, format ==
MAC_FILE_FORMAT,
XmNuserData, (XtPointer)
MAC_FILE_FORMAT,
XmNmarginHeight,
0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic,
'M',
NULL);
XmStringFree(str);
return formatBtns;
}