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

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

#include <X11/Xlib.h>

#include <xclass/OXFrame.h>
#include <xclass/OXWidgets.h>
#include <xclass/OString.h>
#include <xclass/OTextBuffer.h>
#include <xclass/OPicture.h>
#include <xclass/ODimension.h>


//--- Button states

#define BUTTON_UP         0
#define BUTTON_DOWN       1
#define BUTTON_ENGAGED    2
#define BUTTON_DISABLED   3

//--- Button states

#define BUTTON_NORMAL     0
#define BUTTON_STAYDOWN   1
#define BUTTON_ONOFF      2

//--- Just a hack to keep compatability

#define AllowStayDown SetType 

class OXWidget;

//-----------------------------------------------------------------
// Abstract base class for buttons

class OXButton : public OXFrame, public OXWidget {
protected:
  static GC _defaultGC;
  static GC _hibckgndGC;
  GC _normGC;

public:
  OXButton(const OXWindow *p, int ID, GC norm = _defaultGC,
           unsigned int option = RAISED_FRAME | DOUBLE_BORDER);

  virtual int HandleButton(XButtonEvent *event);
  virtual int HandleCrossing(XCrossingEvent *event);
  virtual void SetState(int state);
  virtual int  GetState() const { return _state; }
  virtual void SetType(int a) { _type = a; }

  friend class OXClient;

protected:
  int _tw, _th, _type;
  int _state, _prevstate, _click;
};

class OXTextButton : public OXButton {
protected:
  static XFontStruct *_defaultFontStruct;
  XFontStruct *_fontStruct;

public:
  OXTextButton(const OXWindow *p, OHotString *s, int ID,
           GC norm = _defaultGC, XFontStruct *font = _defaultFontStruct,
           unsigned int option = RAISED_FRAME | DOUBLE_BORDER);
  virtual ~OXTextButton();

  virtual ODimension GetDefaultSize() const { return ODimension(_tw+8, _th+7); }

  virtual int HandleKey(XKeyEvent *event);
  void SetTextJustify(const int tmode) { _tmode = tmode; } 
  const OHotString *GetText() const { return _label; }
  void SetText(OHotString *new_label);
  
  friend class OXClient;

protected:
  virtual void _DoRedraw();

  OHotString *_label;
  int _tmode, _hkeycode;
};

class OXPictureButton : public OXButton {
protected:
  virtual void _DoRedraw();

  const OPicture *_pic;

public:
  OXPictureButton(const OXWindow *p, const OPicture *pic, int ID,
                  GC norm = _defaultGC,
                  unsigned int option = RAISED_FRAME | DOUBLE_BORDER);
};

class OXCheckButton : public OXButton {
protected:
  static XFontStruct *_defaultFontStruct;
  XFontStruct *_fontStruct;
  static GC _defaultGC;

public:
  OXCheckButton(const OXWindow *p, OHotString *s, int ID,
                GC norm = _defaultGC, XFontStruct *font = _defaultFontStruct,
                unsigned int option = 0);
  virtual ~OXCheckButton();

  virtual ODimension GetDefaultSize() const { return ODimension(_tw+22, _th+2); }

  virtual int HandleButton(XButtonEvent *event);
  virtual int HandleCrossing(XCrossingEvent *event);
  virtual void SetState(int state) { _SetState(_prevstate = state); }

  friend class OXClient;

protected:
  virtual void _DoRedraw();
  void _SetState(int state);

  OHotString *_label;
};

class OXRadioButton : public OXButton {
protected:
  static const OPicture *_on, *_off;
  static XFontStruct *_defaultFontStruct;
  XFontStruct *_fontStruct;
  static GC _defaultGC;

public:
  OXRadioButton(const OXWindow *p, OHotString *s, int ID,
                GC norm = _defaultGC, XFontStruct *font = _defaultFontStruct,
                unsigned int option = 0);
  virtual ~OXRadioButton();

  virtual ODimension GetDefaultSize() const { return ODimension(_tw+22, _th+2); }

  virtual int HandleButton(XButtonEvent *event);
  virtual int HandleCrossing(XCrossingEvent *event);
  virtual void SetState(int state) { _SetState(_prevstate = state); }

  friend class OXClient;

protected:
  virtual void _DoRedraw();
  void _SetState(int state);

  OHotString *_label;
};

#endif  // __OXBUTTON_H
