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

    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/OString.h>


//-----------------------------------------------------------------
       
void OString::Draw(Display *dpy, XID id, GC gc, int x, int y) {
  XDrawString(dpy, id, gc, x, y, _string, _len);
}

void OString::DrawWrapped(Display *dpy, XID id, GC gc,
                          int x, int y, int w, XFontStruct *font) {
  char *p = _string, *prev = _string, *chunk = _string;
  int tw, th;

  tw = XTextWidth(font, _string, _len);
  if (tw <= w) {
    XDrawString(dpy, id, gc, x, y, _string, _len);
    return;
  }

  th = font->max_bounds.ascent + font->max_bounds.descent + 1;

  while(1) {
    p = strchr(p, ' ');
    if (p == NULL) {
      if (chunk) XDrawString(dpy, id, gc, x, y, chunk, strlen(chunk));
      break;
    }
    tw = XTextWidth(font, chunk, p-chunk);
    if (tw > w) {
      if (prev == chunk)
        prev = ++p;
      else
        p = prev;
      XDrawString(dpy, id, gc, x, y, chunk, prev-chunk-1);
      chunk = prev;
      y += th;
    } else {
      prev = ++p;
    }
  }
}

int OString::GetLines(XFontStruct *font, int w) {
  char *p = _string, *prev = _string, *chunk = _string;
  int tw, nlines;

  nlines = 1;

  tw = XTextWidth(font, _string, _len);
  if (tw <= w) return nlines;

  while(1) {
    p = strchr(p, ' ');
    if (p == NULL) break;
    tw = XTextWidth(font, chunk, p-chunk);
    if (tw > w) {
      if (prev == chunk)
        chunk = prev = ++p;
      else
        p = chunk = prev;
      ++nlines;
    } else {
      prev = ++p;
    }
  }
  return nlines;
}

//-----------------------------------------------------------------  
     
OHotString::OHotString(const char *s) : OString(s) {
  char *p;

  _lastGC = None;
  _off1 = _off2 = 0;

  _hotchar = '\0';
  _hotpos = 0;    // No hotkey defaults the offset to zero

  for (p=_string; *p; ++p) {
    if (*p == '&') {
      if (p[1] == '&') { // escaped & ?
        // copy the string down over it   
        for (char *tmp=p; *tmp; tmp++) tmp[0] = tmp[1];
        continue; // and skip to the key char
      }
      // hot key marker - calculate the offset value 
      _hotpos = (p - _string) +1;
      _hotchar = p[1];
      for (; *p; p++) p[0] = p[1];  // copy down
      break;                        // allow only one hotkey per item
    }
  }
  _len = strlen(_string);  // recompute length
}
     
void OHotString::Draw(Display *dpy, XID id, GC gc, int x, int y) {
  
  XDrawString(dpy, id, gc, x, y, _string, _len);
  
  if (_hotpos > 0) {
    if (_lastGC != gc) {
      XGCValues gcval;
      XFontStruct *font;

      XGetGCValues(dpy, gc, GCFont, &gcval);
      font = XQueryFont(dpy, gcval.font);   
    
      _off1 = XTextWidth(font, _string, _hotpos-1); //+1;
      _off2 = XTextWidth(font, _string, _hotpos) -1;
      _lastGC = gc;
      XFreeFontInfo(NULL, font, 1);
    }
    XDrawLine(dpy, id, gc, x+_off1, y+1, x+_off2, y+1);
  }
}  
