/**************************************************************************

    This file is part of desktop95, a desktop manager for fvwm95.
    Copyright (C) 1996, 1997 David Barth, Hector Peraza.

    This program 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 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

**************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <xclass/OXClient.h>
#include <xclass/OXWindow.h>
#include <xclass/OXFrame.h>
#include <xclass/OXWidgets.h>
#include <xclass/OXMsgBox.h>
#include <xclass/OString.h>
#include <xclass/OPicture.h>
#include <xclass/utils.h>

#include "OXNewClient.h"
#include "OXFolder.h"

#include "main.h"

//-------------------------------------------------------------------

OXDesktopClient *clientX;
OXDesktopMainFrame *mainWindow;

OXFolderContainer *fileWindow;
OXPopupMenu *objectMenu;
OXPopupMenu *sendToMenu;

char *AppName;
char *StartDir;

const OPicture *Folder_s;
const OPicture *ExeDoc_s;
const OPicture *TextDoc_s;

int NewBrowser = True;


main(int argc, char *argv[]) {
  char *p;

  if ((p = strrchr(argv[0], '/')) == NULL)
    AppName = argv[0];
  else
    AppName = ++p;

  StartDir = new char[strlen(getenv("HOME"))+strlen("/.desktop")+1];
  sprintf(StartDir,"%s/.desktop", getenv("HOME"));

  clientX = new OXDesktopClient;

  mainWindow = new OXDesktopMainFrame(clientX->GetRoot(), 400, 200);

  Folder_s  = clientX->GetPicture("folder.s.xpm");
  ExeDoc_s  = clientX->GetPicture("app.s.xpm");
  TextDoc_s = clientX->GetPicture("doc.s.xpm");

  if (!Folder_s)
    FatalError("Missing pixmap: \"folder.s.xpm\"");
  if (!ExeDoc_s)
    FatalError("Missing pixmap: \"app.s.xpm\"");
  if (!TextDoc_s)
    FatalError("Missing pixmap: \"doc.s.xpm\"");

  fileWindow->ChangeDirectory(new OString(StartDir));
  mainWindow->MapSubwindows();
  fileWindow->ArrangeFolders();

  // we need to use GetDefault...() to initialize the layout algorithm...
  // mainWindow->Resize(mainWindow->GetDefaultSize());
  // mainWindow->Resize(400, 200);

  mainWindow->MoveResize(-100, -100, 90, 40);
  mainWindow->SetWMPosition(-100, -100);
  mainWindow->MapWindow();

  clientX->Run();
}

OXDesktopMainFrame::OXDesktopMainFrame(const OXWindow *p, int w, int h) :
  OXMainFrame(p, w, h) {
  char tmp[BUFSIZ];

  //-------------- create popup menus

  sendToMenu = new OXPopupMenu(_client->GetRoot());
  sendToMenu->AddEntry(new OHotString("Fax Recipient"),  21, _client->GetPicture("fax.t.xpm"));
  sendToMenu->AddEntry(new OHotString("Mail Recipient"), 22, _client->GetPicture("mail.t.xpm"));
  sendToMenu->AddEntry(new OHotString("My Briefcase"),   23, _client->GetPicture("briefcase.t.xpm"));

  objectMenu = new OXPopupMenu(_client->GetRoot());
  objectMenu->AddEntry(new OHotString("&Open"),    1501);
  //objectMenu->AddEntry(new OHotString("&Explore"), 1502);
  //objectMenu->AddEntry(new OHotString("&Find..."), 1503);
  objectMenu->AddSeparator();
  objectMenu->AddPopup(new OHotString("Se&nd To"), sendToMenu);
  objectMenu->AddSeparator();
  objectMenu->AddEntry(new OHotString("Cu&t"),   M_EDIT_CUT);
  objectMenu->AddEntry(new OHotString("&Copy"),  M_EDIT_COPY);
  objectMenu->AddEntry(new OHotString("&Paste"), M_EDIT_PASTE);
  objectMenu->AddSeparator();
  //objectMenu->AddEntry(new OHotString("Create &Shortcut"), M_FILE_NEWSHORTCUT);
  objectMenu->AddEntry(new OHotString("&Delete"),          M_FILE_DELETE);
  //objectMenu->AddEntry(new OHotString("Rena&me"), M_FILE_RENAME);
  objectMenu->AddSeparator();
  objectMenu->AddEntry(new OHotString("P&roperties"), M_FILE_PROPS);
  objectMenu->SetDefaultEntry(1501);

  sendToMenu->Associate(this);
  objectMenu->Associate(this);

  _newMenu = new OXPopupMenu(_client->GetRoot());
  _newMenu->AddEntry(new OHotString("&Folder"),       M_FILE_NEWFOLDER);
  _newMenu->AddEntry(new OHotString("&Shortcut"),     M_FILE_NEWSHORTCUT);
  _newMenu->AddSeparator();
  _newMenu->AddEntry(new OHotString("Bitmap Image"),  113);
  _newMenu->AddEntry(new OHotString("Text Document"), 113);
  _newMenu->AddEntry(new OHotString("Briefcase"),     113);

  _newMenu->DisableEntry(M_FILE_NEWSHORTCUT);

  _sortMenu = new OXPopupMenu(_client->GetRoot());
  _sortMenu->AddEntry(new OHotString("By &Name"),      M_VIEW_ARRANGE_BYNAME);
  _sortMenu->AddEntry(new OHotString("By &Type"),      M_VIEW_ARRANGE_BYTYPE);
  _sortMenu->AddEntry(new OHotString("By &Size"),      M_VIEW_ARRANGE_BYSIZE);
  _sortMenu->AddEntry(new OHotString("By &Date"),      M_VIEW_ARRANGE_BYDATE);
  _sortMenu->AddSeparator();
  _sortMenu->AddEntry(new OHotString("Lin&e up Icons"), M_VIEW_LINEUP);
  _sortMenu->AddEntry(new OHotString("&Auto Arrange"), M_VIEW_ARRANGE_AUTO);
  _sortMenu->CheckEntry(M_VIEW_ARRANGE_AUTO);
  _sortMenu->RCheckEntry(M_VIEW_ARRANGE_BYNAME, 
                         M_VIEW_ARRANGE_BYNAME, M_VIEW_ARRANGE_BYDATE);

  _sortMenu->Associate(this);


  fileWindow = new OXFolderContainer(this, 500, 250, HORIZONTAL_FRAME,
                                     _whitePixel);

  SetWindowName("fOX desktop");
  SetClassHints("Desktop", "Desktop");

  SetMWMHints(MWM_DECOR_ALL, MWM_FUNC_ALL, MWM_INPUT_MODELESS);

  MapSubwindows();

  Resize(GetDefaultSize());

  XSelectInput(GetDisplay(), _id, ExposureMask | FocusChangeMask);
}

OXDesktopMainFrame::~OXDesktopMainFrame() {
  delete fileWindow; 
 
  delete _newMenu;
  delete _sortMenu;

  delete objectMenu;
  delete sendToMenu;
}

int OXDesktopMainFrame::HandleFocusChange(XFocusChangeEvent *event) {
  if ((event->mode == NotifyNormal) &&
      (event->detail != NotifyPointer)) {
    if (event->type == FocusIn) {
    } else {
      fileWindow->UnselectAll();
    }
  }
  return True;
}

int OXDesktopMainFrame::ProcessMessage(long msg, long parm1, long parm2) {

  switch (GET_MSG(msg)) {

    case XC_COMMAND:    // buttons and menu commands
      switch(GET_SUBMSG(msg)) {

        // process menu and buttons together
        case CM_BUTTON:
        case CM_MENU:
          switch(parm1) {

            case M_FILE_CLOSE:
              delete this; // mainWindow;
              delete clientX;
              exit(0);
              break;

            case M_VIEW_ARRANGE_BYNAME:
              _sortMenu->RCheckEntry(M_VIEW_ARRANGE_BYNAME,
                                     M_VIEW_ARRANGE_BYNAME, M_VIEW_ARRANGE_BYDATE);
              fileWindow->Sort(SORT_BY_NAME);
              break;

            case M_VIEW_ARRANGE_BYTYPE:
              _sortMenu->RCheckEntry(M_VIEW_ARRANGE_BYTYPE,
                                     M_VIEW_ARRANGE_BYNAME, M_VIEW_ARRANGE_BYDATE);
              fileWindow->Sort(SORT_BY_TYPE);
              break;

            case M_VIEW_ARRANGE_BYSIZE:
              _sortMenu->RCheckEntry(M_VIEW_ARRANGE_BYSIZE,
                                     M_VIEW_ARRANGE_BYNAME, M_VIEW_ARRANGE_BYDATE);
              fileWindow->Sort(SORT_BY_SIZE);
              break;

            case M_VIEW_ARRANGE_BYDATE:
              _sortMenu->RCheckEntry(M_VIEW_ARRANGE_BYDATE,
                                     M_VIEW_ARRANGE_BYNAME, M_VIEW_ARRANGE_BYDATE);
              fileWindow->Sort(SORT_BY_DATE);
              break;

            case M_VIEW_REFRESH:
              fileWindow->DisplayDirectory();
              break;

            case 99:
              fileWindow->ChangeDirectory(new OString(".."));
              break;

            default:
              XBell(GetDisplay(), 0);
              break;

          } // switch(parm1)
          break;

        default:
          break;

      } // switch(GET_SUBMSG(msg))
      break;

    default:
      break;

  } // switch(GET_MSG(msg))

  return True;
}
