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

    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 <xclass/utils.h>
#include <xclass/OXWidgets.h>
#include <xclass/OXClient.h>
#include <xclass/OString.h>

#include <X11/cursorfont.h>
#include <X11/keysym.h>


//-----------------------------------------------------------------

OXLabel::OXLabel(const OXWindow *p, OString *text, GC norm, XFontStruct *font,
                 unsigned int options, unsigned long back) :
    OXFrame(p, 1, 1, options, back) {

    _text = text;
    _tmode = TEXT_CENTER_X | TEXT_CENTER_Y;
    _fontStruct = font;
    _normGC = norm;

    _tw = XTextWidth(_fontStruct, _text->GetString(), _text->GetLength());
    _th = _fontStruct->max_bounds.ascent + _fontStruct->max_bounds.descent;
    Resize(_tw, _th + 1);
}

OXLabel::~OXLabel() {
  if (_text) delete _text;
}

void OXLabel::SetText(OString *new_text) {

  if (_text) delete _text;
  _text = new_text;

  _tw = XTextWidth(_fontStruct, _text->GetString(), _text->GetLength());
  _th = _fontStruct->max_bounds.ascent + _fontStruct->max_bounds.descent;

//  Resize(_tw, _th + 1);

  _client->NeedRedraw(this);
}

void OXLabel::_DoRedraw() {
  int x, y;

  OXFrame::_DoRedraw();
    
  if (_tmode & TEXT_LEFT)
    x = 0;
  else if (_tmode & TEXT_RIGHT)
    x = _w - _tw;
  else
    x = (_w - _tw) >> 1;

  if (_tmode & TEXT_TOP)
    y = 0;
  else if (_tmode & TEXT_BOTTOM)
    y = _h - _th;
  else
    y = (_h - _th) >> 1;

  _text->Draw(GetDisplay(), _id, _normGC, x, y + _fontStruct->max_bounds.ascent);
}
