/* $Id: DESKTOP.CPP 2.4 1995/07/20 11:24:33 leon Exp $
 */

#define Uses_MsgBox

#define Uses_TApplication

#define Uses_TButton

#define Uses_TChDirDialog

#define Uses_TDeskTop

#define Uses_TDeskTop

#define Uses_TDialog

#define Uses_TEditWindow

#define Uses_TEditor

#define Uses_TEvent

#define Uses_TFileDialog

#define Uses_TFileEditor

#define Uses_fpstream

#define Uses_TKeys

#define Uses_TMenuBar

#define Uses_TMenuItem

#define Uses_TRect

#define Uses_TScroller

#define Uses_TStaticText

#define Uses_TSubMenu

#define Uses_TTerminal


#include <tvision/tv.h>


#include "config.h"

#include "gisel.h"


#include <stdio.h>

#include <dir.h>


//

// writeView() function ( writes a view object to a resource file )

//


static void writeView(TView *p, void *strm)
{
   fpstream *s = (fpstream *) strm;

   if ((p->state & sfVisible) && (p != TProgram::deskTop->last))
//    if (p->name)

//      cout << p->name << endl;

     *s << p;
}

/**
 **  Stores the Desktop in a resource file
 **/
void TGIselApp::storeDesktop(fpstream& s)
{
  deskTop->forEach(::writeView, &s);
  s << 0;
}


/**
 ** Saves the DeskTop by calling storeDesktop function
 **/
void TGIselApp::saveDesktop()
{
    fpstream *f = new fpstream(DESKTOP_FILE, ios::out|ios::binary);


    if( f )
        {
        TGIselApp::storeDesktop(*f);
        if( !f )
            {
            messageBox("Could not create " DESKTOP_FILE ".", 
                       mfOKButton | mfError);
            delete f;
            ::remove(DESKTOP_FILE);
            return;
            }
        }
    delete f;
}


/**
 ** restores the previously stored Desktop
 **/
void TGIselApp::retrieveDesktop()
{
    struct ffblk ffblk;

    if (findfirst(DESKTOP_FILE, &ffblk, 0))
        messageBox("Could not find desktop file", mfOKButton | mfError);
    else 
        {
        fpstream *f = new fpstream(DESKTOP_FILE, ios::in|ios::binary);
        if( !f )
            messageBox("Could not open desktop file", mfOKButton | mfError);
        else
           {
           TGIselApp::loadDesktop(*f);
           if( !f )
               messageBox("Error reading desktop file", mfOKButton | mfError);
           }
        delete f;
        }
}




//

// closeView() function

//


static void closeView(TView *p, void *p1)
{
    message(p, evCommand, cmClose, p1);
}


/**
 ** Loads desktop for file
 **/
void TGIselApp::loadDesktop(fpstream &s)
{
    TView  *p;

    if (deskTop->valid(cmClose))
        { 
        deskTop->forEach(::closeView, 0);  // Clear the desktop

        do {
           s >> p;
           deskTop->insertBefore(validView(p), deskTop->last);
           }
           while (p != 0);
        }
}







syntax highlighted by Code2HTML, v. 0.9.1