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

    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 __OXCLIENT_H
#define __OXCLIENT_H

#include <X11/Xlib.h>
#include <xclass/OXSList.h>


#ifdef __hpux
#define FD_MASK int
#else
#define FD_MASK fd_set
#endif

#define XCE_X_EVENT       (1<<0)
#define XCE_FILE_EVENT    (1<<1)
#define XCE_TIMER_EVENT   (1<<2)
#define XCE_IDLE_EVENT    (1<<3)
#define XCE_ALL_EVENTS    (XCE_X_EVENT | XCE_FILE_EVENT | \
                           XCE_TIMER_EVENT | XCE_IDLE_EVENT)
#define XCE_DONT_WAIT     (1<<4)


extern Atom WM_DELETE_WINDOW;
extern Atom _MOTIF_WM_HINTS;
extern Atom _XCLASS_MESSAGE;


// Forward declarations to avoid include files headaches :-(

class OXWindow;
class OXFrame;
class OGCPool;
class OPicturePool;
class OPicture;
class OTimer;
class OFileHandler;
class OXSTimerList;

// An OXClient is an exception in that is not a real
// OX object (no XID member)

// --------------------------------------------------------
// X-Window client

class OXClient {
public:
  OXClient(const char *DpyName = "");
  ~OXClient();

  Display *GetDisplay() const { return _dpy; }
  int GetScreenNumber() const { return _screen; }
  int GetDisplayWidth() const { return XDisplayWidth(_dpy, _screen); }
  int GetDisplayHeight() const { return XDisplayHeight(_dpy, _screen); }
  int GetDisplayDepth() const { return _depth; }
  const OXWindow *GetRoot() const { return _Root; }
  OXWindow *GetWindowById(Window sw) const;

  unsigned long GetColorByName(const char *name) const;
  unsigned long GetHilite(const unsigned long base_color) const;
  unsigned long GetShadow(const unsigned long base_color) const;

  Colormap GetDefaultColormap() const { return _defaultColormap; }

  void NeedRedraw(OXWindow *w);

  int  RegisterWindow(OXWindow *w);
  int  UnregisterWindow(OXWindow *w);
  int  RegisterTimer(OTimer *t);
  int  UnregisterTimer(OTimer *t);
  int  RegisterFileEvent(OFileHandler *fh);
  int  UnregisterFileEvent(OFileHandler *fh);

  void Run();
  void WaitFor(OXWindow *w);
  void WaitForUnmap(OXWindow *w);

  const OPicture *GetPicture(const char *name);
  const OPicture *GetPicture(const char *name, char **data);
  const OPicture *GetPicture(const char *name, int new_width, int new_height);
  const OPicture *GetPicture(const char *name, char **data,
                                        int new_width, int new_height);
  void FreePicture(const OPicture *pic);

  void StartTip(OXFrame *w);
  void StopTip(OXFrame *w);
  int  GetTipStatus() { return _tip_status; }

protected:
  void MainLoop();
  int  ProcessOneEvent(int EventType, Window wid);
  int  HandleEvent(XEvent *Event);
  int  HandleMaskEvent(XEvent *Event, Window wid);
  int  _DoRedraw();

  unsigned long _backcolor, _forecolor, _hilite, _shadow;
  unsigned long _selbackcolor, _selforecolor, _white, _black;

  Display  *_dpy;
  int _screen, _depth;
  OXWindow *_Root;
  Colormap _defaultColormap;

  int _x_fd;
  OPicturePool *_picturePool;
  OGCPool *_gcPool;
  int _globalNeedRedraw;        // at least one window needs to be redrawn
  OXSList *_wlist;              // list of frames
  OXSList *_idlelist;           // list of idle events handlers
  OXSList *_iolist;             // list of file I/O handlers
  OXSList *_siglist;            // list of signal handlers
  OXSTimerList *_tlist;         // list of timers

  OTimer *_tip_timer;		// special timer for tips
  OXFrame *_tip_frame;		// currently tipped frame
  int _tip_status;

  fd_set readset, writeset, excptset;
};


#endif  // __OXCLIENT_H
