/*********************************************************************
 *
 *            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. 
 *
 *************************************************************************/

#define _MBTN_SELECTION_C_
#include "ezfm.h"

/**********************************************************************************/
void setupMBtnSelection      MY_ANSIARGS((void *btn, void *data));
/**********************************************************************************/
static void updateSelectionMenu  MY_ANSIARGS((void *widget, void *data));
static void propCb  MY_ANSIARGS((void *widget, void *data));
static void printCb  MY_ANSIARGS((void *widget, void *data));
static void renameCb  MY_ANSIARGS((void *widget, void *data));
static void reallyDeleteCb  MY_ANSIARGS((void *widget, void *data));
static void cmlCb  MY_ANSIARGS((void *widget, void *data));
static void inTrashCb  MY_ANSIARGS((void *widget, void *data));
static void openWithCb  MY_ANSIARGS((void *widget, void *data));
/**********************************************************************************
 *
 * setup MenuButton 'File'
 */

void setupMBtnSelection(mbar, data)
     EZ_Widget *mbar; void *data;
{
  Toplevel  *toplevel = (Toplevel *)data;
  EZ_Widget *menu, *tmp,*mitem;
  EZ_UnknownDataType unknown;
  
  menu =     EZ_CreateWidgetXrm(EZ_WIDGET_MENU,      NULL,
				"SelectionMenu", "selectionMenu",
				EZ_MENU_TEAR_OFF,    1,
				NULL);
  EZ_SetMenuSetupProcedure(menu, updateSelectionMenu, data);
  
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,              "Properties...",
			     EZ_UNDERLINE,                  1,
			     EZ_CALLBACK,                   propCb, data,
			     NULL);
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_SEPARATOR,      menu,
			     NULL);


  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Open With ...",
			     EZ_UNDERLINE,                  0,
			     EZ_CALLBACK,                   openWithCb, data,
			     NULL);
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_SEPARATOR,      menu,
			     NULL);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Print...",
			     EZ_UNDERLINE,                  0,
			     EZ_CALLBACK,                   printCb, data,
			     NULL);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Rename...",
			     EZ_UNDERLINE,                  0,
			     EZ_CALLBACK,                   renameCb, data,
			     NULL);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Copy, Move...",
			     EZ_UNDERLINE,                  0,
			     EZ_CALLBACK,                   cmlCb, data,
			     NULL);
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_SEPARATOR,      menu,
			     NULL);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Put in trash...",
			     EZ_UNDERLINE,                  2,
			     EZ_CALLBACK,                   inTrashCb, data,
			     NULL);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "Really Delete...",
			     EZ_CALLBACK,                   reallyDeleteCb, data,
			     NULL);

  mitem = EZ_MenuBarAddItemAndMenu(mbar, "Selection", 0, menu);
  EZ_AddWidgetCallBack(mitem, EZ_DESTROY_CALLBACK, mBarItemDestroyCB, menu, 0);
}

/**************************************************************************************
 * Update the Selection Menu
 */
static EZ_Item *getSelectionInToplevel(tp) Toplevel *tp;
{
  EZ_Item *item = NULL;
  if(tp)
    {
      Folder  *folder = tp->folder[0];
      item = getFolderSelection(folder);
      if(item == NULL)
	{
	  folder = tp->folder[1];
	  if(folder) item = getFolderSelection(folder);
	}
    }
  return(item);
}

static void updateSelectionMenu(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);
  if(item == NULL) EZ_DisableWidgetTree(widget);
  else EZ_EnableWidgetTree(widget);
}
/**************************************************************************************
 *
 * popup property dialogue
 */
static void propCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      popupInfoDialogue(NULL, finfo);
    }
  else ezfmWarning("Property Dialogue", "No selection found");
}

/**************************************************************************************
 *
 * print
 */
static void printCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  char *pathname = finfo->pathname;
	  doPrintDialogue(pathname);
	}
    }
  else ezfmWarning("Print", "No selection found");
}
/**************************************************************************************
 *
 *  rename
 */
static void renameCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  char command[1024], tmp[256], *ptr;
	  char *pathname = finfo->pathname;
	  strcpy(tmp, pathname);
	  ptr = strrchr(tmp, '/');
	  if(ptr) ptr[1] = '\0';
	  sprintf(command, "/bin/mv %s %s",pathname,tmp);
	  oneCommandDialogue("Rename", "Please enter the new name", command, 'e',
			     NULL, 0, NULL);
	}
    }
  else ezfmWarning("Rename", "No selection found");
}

/**************************************************************************************
 *
 *  Copy, Move, Link
 */
static void cmlCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  copyMoveLinkCB(NULL, finfo);
	}
    }
  else  ezfmWarning("Copy, Move ...", "No selection found");
}

/**************************************************************************************
 *
 *  delete
 */
static void reallyDeleteCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  char *pathname = finfo->pathname;
	  char command[1024];
	  sprintf(command, "/bin/rm -rf %s", pathname);
	  oneCommandDialogue(" Delete ",  "Please CONFIRM !", command, 0,
			     NULL, 0, NULL);
	}
    }
  else  ezfmWarning("Really delete", "No selection found");
}

/**************************************************************************************
 *
 *  put in trash
 */
static void inTrashCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  char *pathname = finfo->pathname;
	  move2Trashcan(pathname);
	}
    }
  else  ezfmWarning("Put in trash", "No selection found");
}

/**************************************************************************************
 *
 *  Open with ...
 */
static void openWithCb(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_Item *item = getSelectionInToplevel(tp);  
  if(item)
    {
      FileInfo *finfo = EZ_GetItemPtrData(item);
      if(finfo)
	{
	  char     *pathname = finfo->pathname;
	  oneCommandDialogue("Open With ...",  "Please complete the command ... ", pathname, 'e',
			     NULL, 0, NULL);
	}
    }
  else  ezfmWarning("Copy, Move ...", "No selection found");
}
/**************************************************************************************/
#undef _MBTN_SELECTION_C_
