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

    This file is part of Xclass95, a Win95-looking GUI toolkit.
    Copyright (C) 1996, 1997 David Barth, Hector Peraza.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

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


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<xclass/ODimension.h>

#ifndef False 
 #define False (1==0)
 #define True (1==1)
#endif


class OLongPosition {
public:
  // attributes
  long x, y;

  // Constructors
  OLongPosition() {}
  OLongPosition(long xc, long yc)
               { x = xc; y = yc; }
  OLongPosition(const OPosition &p)
               { x = p.x; y = p.y; }
};

class OText;

class OTextLine
{
 protected:
	char *_string;
	unsigned long _length;
	OTextLine *_prev, *_next;


 public:
	OTextLine();
	OTextLine(OTextLine *line);
	OTextLine(char *string);
	~OTextLine();

	void Clear();
	unsigned long GetLineLength() {return _length;}

	void DelText(unsigned long pos, unsigned long length);
	void InsText(unsigned long pos, char *text);
	char *GetText(unsigned long pos, unsigned long length);

	void DelChar(unsigned long pos);
        void InsChar(unsigned long pos, char character);
        char GetChar(unsigned long pos);

	friend class OText;

};


class OText
{
  public:
        OText();
        OText(OText *text);
        OText(char *string);
        ~OText();                               

        void Clear();
        bool Load(const char *fn, long startpos= 0, long length = -1); 
        bool Save(const char *fn);              
        bool Append(const char *fn);              
        bool IsSaved() {return isSaved;}

                                        
        bool DelChar(OLongPosition pos);    
        bool InsChar(OLongPosition pos, char c);                
        char GetChar(OLongPosition pos);

        bool DelText(OLongPosition start, OLongPosition end);
        bool InsText(OLongPosition pos, char *buf);
        bool InsText(OLongPosition ins_pos, OText *src, OLongPosition 
			start_src, OLongPosition end_src);
	bool AddText(OText *text);

        bool DelLine(unsigned long pos);
        char *GetLine(OLongPosition pos, unsigned long length);
        bool BreakLine(OLongPosition pos);               
	bool InsLine(unsigned long row,char *string);
                                                        
        long RowCount() {return rowCount;}
        long ColCount() {return colCount;}

        long GetLineLength(long row);
        long GetLongestLine() {return longestLine;} 

        bool Search(OLongPosition *foundPos, OLongPosition star, char *searchString, 
                        bool direction, bool caseSensitive); 
        bool Replace(OLongPosition pos, char *oldText, char *newText, bool direction,
                        bool caseSensitive);

	void SetLineColor(long row, unsigned long color);
        unsigned long GetLineColor(long row);

  protected:
        bool isSaved;
        OTextLine *first, *current;
        long current_row;
        long rowCount, colCount;
        long longestLine;
 
        void _OText();
        bool SetCurrentRow(long row);
        void LongestLine();
        long UpSearchBM(char *actualLine, char *searchPattern, long len, 
                                bool cS); 
        long DownSearchBM(char *actualLine, char *searchPattern, long len,
                                bool cS); 
        bool CharEqual(char first, char second, bool cS);
 };

