/* Initialize a rather simple text object for editing, read a file,
   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.  */

#import "MiniText.h"
#import <ansi/float.h>	/* For FLT_MAX */
#import <stdlib.h>	/* For atof */
@implementation MiniText:Text
/* ================
   =  -initFrame: =
   ================
  Set up a Fixed-Pitch - Font Text view with the file from the command line already loaded, the frame appropriately resized. The input frame is still used for the position.
  To achieve resizing, the view is initialized with maximum width. The font is set and the text read. So, it is rewrapped for the max witdh and sizeToFit will shrink-wrap the view to the space the text occupies. A low bound on the frame is put by minSize. */
-initFrame:(const NXRect *)frameRect
{
  /* Set up the superclass with the designated initializer.
    ======================================================== */
  const char	*defStr;
  static NXRect	myFrame = {origin:{x:0.0,y:0.0},
													 size:{width:MAXWIDTHVALUEFLOAT, height:FLT_MAX}};

  /* A high bound on resizing is set here, practically only on width, because I don't want a horizontal scroller. This bound is take from the defaults database. */
  myFrame.origin = frameRect->origin;
  if((defStr = NXGetDefaultValue(DEFAULTOWNER, MAXWIDTHNAME)) != NULL)
    myFrame.size.width  = atof(defStr);

  self = [super	initFrame:&myFrame text:"" alignment:NX_LEFTALIGNED];
  if(self != nil)
  {
    static NXSize	myMinSize = {width:30.0, height:10.0};	/*some nonzero size*/
    NXStream		*istream;
    
    /* Set the Font in the empty object.
      ===================================
	Shouldn't try to resize or rewrap now.
	The font is System Monospaced at a size stored in the defaults.
	Thus, the font can be changed wirhout a user interface. */
    defStr = NXGetDefaultValue(DEFAULTOWNER, FONTSIZENAME);
    [self setFont: [Font
		userFixedPitchFontOfSize: atof(defStr ? defStr : FONTSIZEVALUE)
		matrix: NX_FLIPPEDMATRIX]];
    
    /* When the appearence is defined, set the sizing stuff.
      =======================================================
      The sizing principle: this text sizes itself to a size appropriate for the data. A low bound is visibility (empty texts should not produce invisibly tiny Text's). Thusly resizable, the Text reads the data and rewraps and resizes itself approprately. Views up the hierarchy are created later (WATCHOUT! Assumption!) so they don't have to be notified of this first resizing. */
    [self setMinSize:&myMinSize];
    [self setMaxSize:&(myFrame.size)];
    
    /* The Text may get longer or shorter by itself (input), wider only through the here inaptly named autosizing mechanism. */
    [self setVertResizable: YES];
  
    /* As a rubber Text, read the text.
      ================================== */
    istream = NXMapFile(NXArgv[NXArgc-1], NX_READONLY);
    if(istream != NULL)
    {
      /* Read the text and shrinkwrap the view. */
      [self readText: istream];
      NXClose(istream);
      [self sizeToFit];

      /* Now that we have reached a mature size, set the final options.
				================================================================
				The frame size changes should still be handed to the superview. */
      [self notifyAncestorWhenFrameChanged :YES];
      /* Superviews may request the Text to resize, too. */
      [self setAutosizing: NX_HEIGHTSIZABLE | NX_WIDTHSIZABLE];
      /* In a quite twisted way, the Text object is its own delegate, so it can
	setDocEdited: easily when receiving textDidChange:. */
      [self setDelegate:self];
      /* A gray background otherwise. More efficient,too. */
      [self setOpaque: YES];
			

    } else {
      NXRunAlertPanel("Fatal", "Could not load file \"%s\".",
	  "Terminate", NULL, NULL, NXArgv[NXArgc-1]);
      /* Should be nil, but the documentation on Object and Responder disagree on that. I just assume it does what I deem right. */
      self = [self free];
    }  
  }
  return self;
}
@end
