/*
  Treat some events, chiefly to save the text on exit,
	    
  Copyright (C) 1996, Felix H. Gatzemeier.
  
  This file is part of Ivy.app, the minimal NeXTstep(TM) Editor..
  
  Ivy.app 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.
  
  Ivy.app 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 Ivy.app; see the file COPYING.  If not, write to
  fxg@imib.rwth-aachen.de
  or the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  
  $Header: /home/cvs/Ivy/MiniText.Delegate.m,v 1.4 1996/05/27 10:48:53 felix Exp $
*/

#import "MiniText.Delegate.h"

@implementation MiniText (ApplicationDelegate)

/* As a Window and an Application Delegate, MiniText tracks termination. */
/* ======================
   = -appWillTerminate: =
   ====================== */
-appWillTerminate:sender
{
  if([[NXApp keyWindow] isDocEdited])
  {
    if(NXRunAlertPanel("Document modified",
                       "Save it before quitting?", "Save", "Don't Save", NULL)
       ==NX_ALERTDEFAULT)
    {
      NXStream *ostream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
      if(ostream != NULL)
      {
	[self writeText: ostream];
	NXSaveToFile(ostream, NXArgv[NXArgc-1]);
	NXCloseMemory(ostream, NX_TRUNCATEBUFFER);
      }
    }
  }
  return self;
}

- appDidInit:sender
{
  [self setSel:0:0];
  return self;
}

@end

@implementation MiniText (WindowDelegate)
/* ====================
   = windowWillClose: =
   ==================== */
-windowWillClose:sender
{
  [NXApp terminate:self];
  return self; /* never gets here , but cc doesn't know. */
}
@end

@implementation MiniText (TextDelegate)
/* As a Text Delegate, MiniText tracks document change. */
/* ==================
   = textDidChange: =
   ================== */
-textDidChange:sender
{
  [sender setDelegate:nil];	/* Once set, we never reset docEdited. */
  [[NXApp keyWindow] setDocEdited:YES];
  return self;
}
@end
