/*********************************************************************
 *
 *            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_VIEW_C_
#include "ezfm.h"

/**********************************************************************************/
void setupMBtnView      MY_ANSIARGS((void *btn, void *data));
/**********************************************************************************/
static void twoFolders      MY_ANSIARGS((void *widget, void *data));
static void showToolBar     MY_ANSIARGS((void *widget, void *data));
static void newView         MY_ANSIARGS((void *widget, void *data));
static void updateViewMenu  MY_ANSIARGS((void *widget, void *data));
static void lockTwoView     MY_ANSIARGS((void *widget, void *data));
/**********************************************************************************
 *
 * setup MenuButton 'File'
 */
#define V_SPLIT_VIEW     0
#define V_SHOW_TOOLBAR   1

void setupMBtnView(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,
				"ViewMenu", "viewMenu",
				EZ_MENU_TEAR_OFF,    1,
				NULL);
  EZ_SetMenuSetupProcedure(menu, updateViewMenu, data);
  /*
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_CHECK_BUTTON,  menu,
			     EZ_LABEL_STRING,              "Lock Two View",
			     EZ_UNDERLINE,                  0,
			     EZ_CHECK_BUTTON_ON_VALUE,      1,
			     EZ_CHECK_BUTTON_OFF_VALUE,     0,
			     EZ_CHECK_BUTTON_ON_OFF,        0,
			     EZ_CALLBACK,                   lockTwoView, data,
			     EZ_INDICATOR_SIZE_ADJUST,      0,
			     NULL);
			     */
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_CHECK_BUTTON,  menu,
			     EZ_LABEL_STRING,              "Split View",
			     EZ_UNDERLINE,                  0,
			     EZ_CHECK_BUTTON_ON_VALUE,      1,
			     EZ_CHECK_BUTTON_OFF_VALUE,     0,
			     EZ_CHECK_BUTTON_ON_OFF,        0,
			     EZ_CALLBACK,                   twoFolders, data,
			     EZ_INDICATOR_SIZE_ADJUST,      0,
			     NULL);
  unknown.p = tmp;
  EZ_SetWidgetUnknownData(menu, V_SPLIT_VIEW, unknown);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_CHECK_BUTTON,  menu,
			     EZ_LABEL_STRING,              "Show Toolbar",
			     EZ_UNDERLINE,                  5,
			     EZ_CHECK_BUTTON_ON_VALUE,      1,
			     EZ_CHECK_BUTTON_OFF_VALUE,     0,
			     EZ_CHECK_BUTTON_ON_OFF,        1,
			     EZ_CALLBACK,                   showToolBar, data,
			     EZ_INDICATOR_SIZE_ADJUST,      0,
			     NULL);
  unknown.p = tmp;
  EZ_SetWidgetUnknownData(menu, V_SHOW_TOOLBAR, unknown);

  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_SEPARATOR,      menu,
			     NULL);
  
  tmp  =     EZ_CreateWidget(EZ_WIDGET_MENU_NORMAL_BUTTON,  menu,
			     EZ_LABEL_STRING,               "New View...",
			     EZ_UNDERLINE,                  0,
			     EZ_CALLBACK,                   newView, data,
			     NULL);
  
  mitem = EZ_MenuBarAddItemAndMenu(mbar, "View", 0, menu);
  EZ_AddWidgetCallBack(mitem, EZ_DESTROY_CALLBACK, mBarItemDestroyCB, menu, 0);
}

/**************************************************************************************
 * 
 * prompt user for new folder name, then try to open the folder.
 */
static void twoFolders(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  int jnk, state = EZ_GetCheckButtonState(widget, &jnk);

  if(state > 0) /* on */
    {
      if(tp->folder[1] == NULL)
	{
	  Folder    *folder = tp->folder[0];
	  FileInfo  *finfo = folder->finfo;
	  char      *pathname = finfo->pathname;
	  EZ_FreezeWidget(tp->folderframe);
	  tp->folder[1] = createNewFolder(pathname, tp, 1, 0);
	  EZ_UnFreezeWidget(tp->folderframe);
	  if(EZ_WidgetIsViewable(tp->toplevel))
	    EZ_DisplayWidget(tp->toplevel);
	  updateFolder(tp->folder[1], 0);  
	  if(folder->bgpixmap != NULL)
	    {
	      EZ_LabelPixmap *pxm = EZ_CreateLabelPixmapFromImageFile(folder->bgpixmap);
	      setObjBgPixmap(tp->folder[1], folder->bgpixmap, pxm);
	    }
	}
    }
  else
    {
      Folder *tmp = tp->folder[1];
      if( tmp != NULL )
	{
	  int w0, h0, w1, h1;
	  EZ_GetWidgetDimension(tmp->frame, &w1, &h1);
	  EZ_GetWidgetDimension(tp->folder[0]->frame, &w0, &h0);

	  tp->folder[1] = NULL;
	  EZ_FreezeWidget(tp->folderframe);
	  EZ_DestroyWidget(tp->panehandle);
	  tp->panehandle = NULL;
	  EZ_DestroyWidget(tmp->frame);
	  EZ_ConfigureWidget(tp->folder[0]->frame,
			     EZ_SIZE,          w0+w1, h0,
			     EZ_FORGET_SIZE,   True, 
			     0);
	  EZ_UnFreezeWidget(tp->folderframe);
	  tp->activeFolder = tp->folder[0];
	  setToplevelName(tp);
	}
    }
}
/******************************************************************************/
static void updateViewMenu(menu, data)
     void *menu; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  EZ_UnknownDataType dd;

  /* two view */
  {
    EZ_Widget *twoView;
    int   status = (tp->folder[1] != NULL);
    EZ_GetWidgetUnknownData(menu, V_SPLIT_VIEW, &dd);
    twoView = (EZ_Widget *)dd.p;
    if(status) EZ_SetCheckButtonState(twoView, 1);
    else  EZ_SetCheckButtonState(twoView, 0);
  }
  /* show toolbar */
  {
    EZ_Widget *mitem, *tbar = tp->toolbar;
    int status = EZ_WidgetIsViewable(tbar);
  
    EZ_GetWidgetUnknownData(menu, V_SHOW_TOOLBAR, &dd);
    mitem = (EZ_Widget *)dd.p;  
    if(status) EZ_SetCheckButtonState(mitem, 1);
    else  EZ_SetCheckButtonState(mitem, 0);  
  }
}
/******************************************************************************/
static void showToolBar(widget, data)
     EZ_Widget *widget; void *data;
{ 
  Toplevel *tp = (Toplevel *)data;
  int jnk, state = EZ_GetCheckButtonState(widget, &jnk);

  if(state == 0) EZ_DeActivateWidget(tp->toolbar); /* hide */
  else EZ_ActivateWidget(tp->toolbar);             /* show */
  EZ_DisplayWidget(tp->toplevel);
}
/******************************************************************************
 *
 *   start a new view
 */
static void newView(widget, data)
     EZ_Widget *widget; void *data;
{
  Toplevel *tp = (Toplevel *)data;
  char      *pathname = NULL;
  if(tp)
    {
      FileInfo  *dinfo;
      Folder    *folder = tp->activeFolder;
      if(!folder || folder !=  tp->folder[1]) folder = tp->folder[0];
      dinfo = folder->finfo;
      pathname = dinfo->pathname;
    }
  startFolder(NULL, pathname, "New View", "Please enter a folder name");
}
static void lockTwoView(widget, data)
     EZ_Widget *widget; void *data;
{
}
/******************************************************************************/
#undef SPLIT_VIEW 
#undef V_SHOW_TOOLBAR
/******************************************************************************/
#undef _MBTN_VIEW_C_


