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

    This file is part of notepad95, a text editor for fvwm95.
    Copyright (C) 1997, Harald Radke.                   

    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 <stdio.h>

#include <xclass/OXClient.h>
#include <xclass/OXFrame.h>
#include <xclass/OXWidgets.h>
#include <xclass/OXScrollBar.h>
#include <xclass/OTimer.h>
#include "OText.h"

#define UNCHANGED       -1
#define MARGIN           3

#define      XC_TEXTEDIT        51
#define         TXT_HSBCHANGE    1
#define         TXT_VSBCHANGE    2
#define         TXT_SBPOS        3
#define         TXT_POSCHANGE    4
#define         TXT_ISMARKED     5
#define         TXT_CLICK        6


class OXTextFrame : public OXFrame {

public:
  Atom sel_prop;

  OXTextFrame(OXWindow *parent, int w, int h, unsigned int options,
              unsigned long back = _defaultFrameBackground);
  virtual ~OXTextFrame();
  bool  LoadFile(char *fname);
  bool SaveFile(char *fname);
  bool IsSaved();
  void Clear();
  bool Cut();
  bool Copy();
  bool Paste();
  bool Delete();
  bool SelectAll();
  bool LineBreak();
  bool Search(char *string, bool direction, bool caseSensitive);
  bool Goto(long line);

  int  GetLines() const { return text->RowCount(); }
  int  GetCols() const { return text->ColCount(); }
  int  GetVisibleLines() const { return ((_h-MARGIN) / _th); }
  int  GetVisibleCols() const { return  ((_w-MARGIN) / _tw); }
  void SetTopLine(int new_top);
  void SetTopCol(int new_top);
  void HandleScrollTimer ();
 
  virtual int HandleSelectionRequest (XSelectionRequestEvent *event);
  virtual int HandleSelection (XSelectionEvent *event);
  virtual int HandleTimer (OTimer *t);
  virtual int HandleButton(XButtonEvent *event);
  virtual int HandleKey(XKeyEvent *event);
  virtual int HandleMotion(XMotionEvent *event);  

  virtual int HandleExpose(XExposeEvent *event) {
    DrawRegion(event->x, event->y, event->width, event->height);
    DrawCursor(1);
    return True;
  } 
  virtual int HandleGraphicsExpose(XGraphicsExposeEvent *event) {
    DrawRegion(event->x, event->y, event->width, event->height);    
     DrawCursor(1);
    return True;
 }


protected:
  XFontStruct *font;
  GC _gc, _gc_marked,_gc_crs0, _gc_crs1;;
  int _th, _tw;
  int scrolling;
  int cursorstate;
  bool pasteok;
  bool is_marked;
  bool marking;
  OText *text, *clipboard;
  OTimer *timer, *csr_timer;
  Coord current;
  Coord visible;
  Coord marked_start, marked_end;
  int  marked_from_x, marked_from_y;

  void DrawCursor(int mode);
  void DrawLine(int row, int mode);
  void DrawRegion(int x, int y, int w, int h);
  void Mark(Coord pos);

  void ScrollWindow(int new_top_row, bool direction);
  void HSBChange(long row, long col);
  void VSBChange(long row, long col);
  void SBPosChange(long row, long col);
  void SetCurrent(Coord new_coord);
  void AdjustPos();

  void InsChar(char character);
  void DelChar();
  void BreakLine();
  void TabKey();
  void PrevChar();
  void NextChar();
  void LineUp();
  void LineDown();
  void ScreenUp();
  void ScreenDown();
  void Home();
  void End();
};

