/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/



/***************************************************************
 *
 *        View an text file
 */
#define _VIEW_FILE_C_

#include "ezfm.h"
/************************************************************************/
void ezfmViewTextFile MY_ANSIARGS(( void *obj, void *data));
void ezfmEditTextFile MY_ANSIARGS((char *pathname, int flag, char *msg));
/************************************************************************/
typedef struct {
  EZ_Widget *frame, *txt, *dialogue;
  char      *pathname;
  char      *msg;
} viewTxtFileRec;
/************************************************************************/
static void  yesCB(widget, data) EZ_Widget *widget; void *data;
{
  EZ_Widget *top = (EZ_Widget *)EZ_GetWidgetPtrData(widget);
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  if(EZ_TextSaveText(vrec->txt, vrec->pathname) == 0)
    ezfmWarning("Save Failed", "permission problem");
  else if(vrec->msg) ezfmWarning(vrec->msg, NULL);    
  EZ_DestroyWidget(top);
  EZ_DestroyWidget(vrec->frame);
}
static void  noCB(widget, data) EZ_Widget *widget; void *data;
{
  EZ_Widget *top = (EZ_Widget *)EZ_GetWidgetPtrData(widget);
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  EZ_DestroyWidget(top);
  EZ_DestroyWidget(vrec->frame);
}
static void  cancelCB(widget, data) EZ_Widget *widget; void *data;
{ EZ_DestroyWidget((EZ_Widget*)data); }
/************************************************************************/
static void confirmNoSaving(widget, data) EZ_Widget *widget; void *data;
{
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  if(vrec)
    {
      if(!EZ_WidgetExist(vrec->dialogue))
        {
          EZ_Widget *frame, *bframe, *label, *yes, *no, *canc;
          frame = EZ_CreateWidget(EZ_WIDGET_FRAME,    NULL, 
                                  EZ_LABEL_STRING,    "Warning",
                                  EZ_ORIENTATION,     EZ_VERTICAL,
                                  EZ_FILL_MODE,       EZ_FILL_BOTH,
                                  EZ_TRANSIENT,       1,
                                  NULL);
          label = EZ_CreateWidget(EZ_WIDGET_LABEL,    frame,
                                  EZ_LABEL_STRING,    "Buffer has been modified, save it?",
                                  EZ_FONT_ID,          ezfmFontId[LABEL_LARGE_FONT], 
                                  EZ_FOREGROUND,       "red",
                                  EZ_LABEL_SHADOW,     1,1,
                                  EZ_TEXT_LINE_LENGTH, 64,
                                  NULL);
          bframe = EZ_CreateWidget(EZ_WIDGET_FRAME,   frame,
                                   EZ_LABEL_STRING,    " ",
                                   EZ_HEIGHT,         0,
                                   NULL);
          yes    = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, bframe,
                                   EZ_LABEL_STRING,         "Yes",
                                   EZ_UNDERLINE,             0,
                                   EZ_CALLBACK,              yesCB, vrec,
                                   EZ_CLIENT_PTR_DATA,       frame,
                                   NULL);
          no     = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, bframe,
                                   EZ_LABEL_STRING,         "No",
                                   EZ_UNDERLINE,             0,
                                   EZ_CALLBACK,              noCB, vrec,
                                   EZ_CLIENT_PTR_DATA,       frame,
                                   NULL);
          canc =  EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, bframe,
                                  EZ_LABEL_STRING,         "Cancel",
                                  EZ_UNDERLINE,             0,
                                  EZ_CALLBACK,              cancelCB, frame,
                                  NULL);
          vrec->dialogue = frame;
        }
      EZ_DisplayWidgetUnderPointer(vrec->dialogue, 0, 0);
    }
}
/************************************************************************/
static void doClose(widget, data) EZ_Widget *widget; void *data;
{
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  if(vrec)
    {
      EZ_Widget *frame = vrec->frame;
      char      *pathname = vrec->pathname;
      int       destroy = 1;
      if(pathname)
	{
	  EZ_Widget *txt = vrec->txt;
	  if(EZ_TextBufferModified(txt))
	    {
	      destroy = 0;
	      confirmNoSaving(NULL, vrec);
	    }
	}
      if(destroy) EZ_DestroyWidget(frame);
    }
}
static void  doSave(widget, data) EZ_Widget *widget; void *data;
{
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  if(EZ_TextSaveText(vrec->txt, vrec->pathname) == 0)
    ezfmWarning("Save Failed", "permission problem");
  else if(vrec->msg)   ezfmWarning(vrec->msg, NULL);

}
/************************************************************************/
static void destroyCB(widget, data) EZ_Widget *widget; void *data;
{
  viewTxtFileRec *vrec = (viewTxtFileRec *)data;
  if(vrec) 
    {
      char *path = vrec->pathname;
      char *msg = vrec->msg;
      if(path) free(path);
      if(msg) free(msg);
      free(vrec);
    }
}
/************************************************************************/
void ezfmViewTextFile(obj, data)
     void *obj; void *data;
{
  FileInfo *finfo = (FileInfo *)data;
  if(finfo)
    {
      char          *pathname = finfo->pathname;
      ezfmEditTextFile(pathname, 1, NULL);
    }
}
/************************************************************************/
static void enableIfModified(txt, data, et, xev)
     EZ_Widget *txt; void *data; int et; XEvent *xev;
{
  if(xev->type == KeyPress || xev->type == ButtonPress)
    {
      if(EZ_TextBufferModified(txt)) EZ_EnableWidget(data);
    }
}
/************************************************************************/
void ezfmEditTextFile(pathname, eflag, msg)
     char *pathname; int eflag; char *msg;
{
  EZ_Widget     *frame, *bframe, *txt, *cls, *sav;
  viewTxtFileRec *vrec = NULL;

  frame = EZ_CreateWidgetXrm(EZ_WIDGET_FRAME,      NULL,
			     "TextViewer",         "textViewer",
			     EZ_FILL_MODE,         EZ_FILL_BOTH,
			     EZ_ORIENTATION,       EZ_VERTICAL,
			     EZ_PADX,              0,
			     EZ_PADY,              0,
			     EZ_SIZE,              640, 400,
			     EZ_WM_WINDOW_NAME,    pathname,
			     EZ_BG_PIXMAP,          bigPixmap, 
			     EZ_EVENT_HANDLER,      ConfigureNotifyHandler, NULL,
			     NULL);
  txt =   EZ_CreateWidget(EZ_WIDGET_TEXT,          frame,
			  EZ_TEXT_WIDGET_EDITABLE, eflag != 0,
			  NULL);

  vrec = (viewTxtFileRec *)malloc(sizeof( viewTxtFileRec));
  vrec->frame = frame;
  vrec->txt = txt;
  vrec->msg = NULL;
  vrec->dialogue = NULL;

  /* if(eflag)*/
    {
      int len = strlen(pathname);
      vrec->pathname = (char *)malloc( (len+1)*sizeof(char));
      strcpy(vrec->pathname, pathname);
      if(msg)
        {
          int len = strlen(msg);
          vrec->msg = (char *)malloc( (len+1)*sizeof(char));
          strcpy(vrec->msg, msg);
        }
    }
    /*   else vrec->pathname = NULL;*/
  
  bframe = EZ_CreateWidget(EZ_WIDGET_FRAME,        frame,
			   EZ_HEIGHT,              0,
			   NULL);
  
  /*  if(eflag) */
    {
      sav =   EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, bframe,
                              EZ_LABEL_STRING,         "Save",
                              EZ_UNDERLINE,            0,
                              EZ_SIZE,                 0, 0,
                              EZ_CALLBACK,             doSave, vrec,
                              NULL);
      EZ_DisableWidget(sav);
      EZ_AddEventHandler(txt, enableIfModified, sav, 0);
    }
  
  cls =   EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, bframe,
			  EZ_LABEL_STRING,         "Close",
			  EZ_UNDERLINE,            0,
			  EZ_SIZE,                 0, 0,
			  EZ_CALLBACK,             doClose, vrec,
			  EZ_DESTROY_CALLBACK,     destroyCB, vrec,
			  NULL);
  
  EZ_TextLoadFile(txt, pathname);
  EZ_TextBeginningOfBuffer(txt);
  EZ_DisplayWidget(frame);
}  
/************************************************************************/
#undef _VIEW_FILE_C_
