#include "xcp.h"

extern Widget list_row_col, request, scroll_window_list_shell, 
              list_okay, list_cancel, request_left, request_right;

extern XtAppContext app;

int in_loop;

void file_copy_okay(), file_copy_cancel();
int get_new_names();

int get_new_names(requester, len)
     copy_files_struct *requester;
     int len;
{
  int x;
  XmString temp_string, request_string;
  char buf[1000];
  Widget *row_col_text, *row_col_label, *row_col_new_name;
  Arg args[2];

  if ((row_col_text = malloc(sizeof(Widget) * len)) == NULL)
    {perror("malloc:");
     exit(2);
   }
  if ((row_col_new_name = malloc(sizeof(Widget) * len)) == NULL)
    {perror("malloc:");
     exit(2);
   }
  if ((row_col_label = malloc(sizeof(Widget) * len)) == NULL)
    {perror("malloc:");
     exit(2);
   }
  request_string = XmStringCreateSimple(requester->string_request);
  XtVaSetValues(request, XmNlabelString, request_string,
			 NULL);
  XmStringFree(request_string);
  request_string = XmStringCreateSimple(requester->source_path);
  XtVaSetValues(request_left, XmNlabelString, request_string,
			 NULL);
  XmStringFree(request_string);
  request_string = XmStringCreateSimple(requester->dest_path);
  XtVaSetValues(request_right, XmNlabelString, request_string,
		NULL);
  XmStringFree(request_string);
  request_string = XmStringCreateSimple(requester->button_okay);
  XtVaSetValues(list_okay,
		XmNlabelString, request_string,
		NULL);
  XmStringFree(request_string);
  request_string = XmStringCreateSimple(requester->button_cancel);
  XtVaSetValues(list_cancel,
		XmNlabelString, request_string,
		NULL);
  XmStringFree(request_string);
  XtSetArg(args[0], XmNrightAttachment, XmATTACH_FORM);
  XtSetArg(args[1], XmNleftAttachment, XmATTACH_FORM);
  for (x=0; x<len; x++){
    sprintf(buf, "button%d", x);
    row_col_text[x]=XmCreateForm(list_row_col, buf, args, 2);
    sprintf(buf, "label%d",x);
    temp_string = XmStringCreateSimple(requester->files[x].source);
    row_col_label[x] = XtVaCreateManagedWidget(buf, xmLabelWidgetClass, 
					       row_col_text[x],
					       XmNlabelString, temp_string,
					       XmNleftAttachment, XmATTACH_FORM,
					       XmNtopAttachment, XmATTACH_FORM,
					       XmNbottomAttachment, XmATTACH_FORM,
					       XmNrightAttachment, XmATTACH_POSITION,
					       XmNrightPosition, 47,
					       NULL);
    sprintf(buf, "new_name%d",x);
    row_col_new_name[x] = XtVaCreateManagedWidget(buf, xmTextFieldWidgetClass, 
						  row_col_text[x],
						  XmNleftAttachment, XmATTACH_POSITION,
						  XmNmaxLength, MAXNAMELEN,
						  XmNleftPosition, 53,
						  XmNrightAttachment, XmATTACH_FORM,
						  XmNtopAttachment, XmATTACH_FORM,
						  XmNbottomAttachment, XmATTACH_FORM,
						  NULL);
  }
  XtManageChildren(row_col_text, len);
  XtPopup(scroll_window_list_shell, XtGrabExclusive);
  in_loop = 0;
  while (!in_loop)
    XtAppProcessEvent(app, XtIMAll);
  XtPopdown(scroll_window_list_shell);
  for (x=0; x<len; x++){
    requester->files[x].dest = XmTextFieldGetString(row_col_new_name[x]);
    XtDestroyWidget(row_col_text[x]);
  }
  free(row_col_new_name);
  free(row_col_label);
  free(row_col_text);
  return(in_loop -1);
}

void file_copy_cancel(w, data, cbs)
     Widget w;
     XtPointer data;
     XmAnyCallbackStruct *cbs;
{
  in_loop = 1;
  return;
}

void file_copy_okay(w, data, cbs)
     Widget w;
     XtPointer data;
     XmAnyCallbackStruct *cbs;
{  
  in_loop = 2;
  return;
}
