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

    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.

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

#ifndef __OXWINDOW_H
#define __OXWINDOW_H

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <xclass/OString.h>
#include <xclass/OXClient.h>

#ifndef min
#define min(a,b) (((a)<(b)) ? (a) : (b))
#endif

#ifndef max
#define max(a,b) (((a)>(b)) ? (a) : (b))
#endif


class OTimer;
class OFileHandler;

/* Perhaps I should add two base classes :

OBaseObject : mainly void to have a Smalltalk class framework

OXObject : to group all objects that have a X Window ID (windows,
pixmaps, fonts, etc...)

*/


// X-Window object base class
// --------------------------------------------------------

class OXObject {
protected:
  XID _id;                     // X-Window identifier
  OXClient *_client;           // pointer to the X client

public:
  OXObject() { _id = None; }
//  virtual ~OXObject() {}

  // Access functions :
  const XID GetId() const { return _id; } 
  Display *GetDisplay() const { return _client->GetDisplay(); }
};

// X window base class
// --------------------------------------------------------

class OXWindow : public OXObject {
public:
  OXWindow() {}
  OXWindow(const OXWindow *p, int x, int y,
	   int w, int h, int border = 0,
	   int depth = CopyFromParent,
	   unsigned int clss = CopyFromParent,
	   Visual *visual = CopyFromParent,
	   unsigned long vmask = 0L,
	   XSetWindowAttributes *attr = NULL);
  OXWindow(OXClient *c, Window id, OXWindow *parent = NULL);

  virtual ~OXWindow();

  const class OXWindow *GetParent() const { return _parent; }

  void MapWindow() { XMapWindow(GetDisplay(), _id); }
  void MapSubwindows() { XMapSubwindows(GetDisplay(), _id); }
  void MapRaised() { XMapRaised(GetDisplay(), _id); }
  void UnmapWindow() { XUnmapWindow(GetDisplay(), _id); }
  void DestroyWindow() { XDestroyWindow(GetDisplay(), _id); }
  void RaiseWindow() { XRaiseWindow(GetDisplay(), _id); }
  void LowerWindow() { XLowerWindow(GetDisplay(), _id); }
  void SetBackgroundColor(unsigned int color)
       { XSetWindowBackground(GetDisplay(), _id, color); }
  void SetBackgroundPixmap(Pixmap pixmap)
       { XSetWindowBackgroundPixmap(GetDisplay(), _id, pixmap); }

  virtual int  HandleExpose(XExposeEvent *event) {
    if (event->count == 0) _client->NeedRedraw(this);
    return True;
  }
  virtual int  HandleGraphicsExpose(XGraphicsExposeEvent *event) {
    if (event->count == 0) _client->NeedRedraw(this);
    return True;
  }
  virtual int  HandleEvent(XEvent */*event*/) { return False; }
  virtual int  HandleTimer(OTimer */*t*/) { return False; }
  virtual int  HandleFileEvent(OFileHandler */*fh*/, unsigned int /*mask*/) {
    return False;
  }
  virtual void Move(int x, int y);
  virtual void Resize(int w, int h);
  virtual void MoveResize(int x, int y, int w, int h);
  virtual int  IsMapped();

  friend class OXClient;
  friend class OTimer;
  friend class OFileHandler;

protected:
  virtual void _DoRedraw() {}
  virtual const OXWindow *_GetMainFrame() const {
    return (_parent == _client->GetRoot()) ?
                       this : _parent->_GetMainFrame();
    }

  const class OXWindow *_parent;
  int _needRedraw;
};


#endif  // __OXWINDOW_H
