view anagram/vaclgui/multiline.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1997-2002 Parsifal Software. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * multiline.cpp
 */

#include <icoordsy.hpp>
#include <ifont.hpp>
//#include <istring.hpp>
#include <windows.h>

#include "multiline.hpp"

//#define INCLUDE_LOGGING
#include "log.h"


MultiLineText &MultiLineText::setText(const char *s) {
  LOGSECTION("MultiLineText::setText");
  textString = s;
  char *p = textString.pointer();
  nLines = 0;
  while (p && *p) {
    LOGV((int) p);
    p = strchr(p, '\n');
    nLines++;
    if (p == 0) {
      break;
    }
    p++;
  }
  LOGV(nLines);
  return *this;
}

Boolean MultiLineText::paintWindow(IPaintEvent &event) {
  LOGSECTION("MultiLineText::paintWindow");
  LOGV(event.rect().asString());
  LOGV(event.rect().movedBy(position()).asString());
  IPresSpaceHandle presSpaceHandle = event.presSpaceHandle();

  font().beginUsingFont(presSpaceHandle);

  int lineWidth = size().width();

  SetBkMode(presSpaceHandle, TRANSPARENT);
  event.clearBackground(event.rect(),IGUIColor::dialogBgnd);
  IPoint where(0, margin.height());
  int i;
  int leading = font().maxSize().height() + font().externalLeading();
  //LOGV(leading);
  char *p = textString.pointer();
  for (i = 0; i < nLines; i++) {
    char *q = strchr(p, '\n');
    if (q) {
      *q = 0;
    }
    char *t = p;
    if (*t =='\v') {
      t++;
    }
    int margin = (lineWidth - font().textWidth(t))/2;
    where.setX(margin);
    IPoint actualWhere = where;
    if (ICoordinateSystem::isConversionNeeded()) {
      actualWhere = ICoordinateSystem::convertToNative(where, size());
      actualWhere.setY(actualWhere.y() - leading);
    }
    event.drawText(p, actualWhere);
    LOGV(p);
    if (q) {
      *q = '\n';
    }
    p = q+1;
    if (q) {
      LOGV(p);
    }
    if (q && *p == '\v') {
      LOGV(vTab);
      where.setY(where.y() + vTab);
      p++;
    }
    where.setY(where.y() + leading);
  }
  return true;
}

ISize MultiLineText::calcMinimumSize() const {
  LOGSECTION("MultiLineText::calcMinimumSize");
  char *p = textString.pointer();
  int maxWidth = 0;
  IPoint where(0, 0);
  int leading = font().maxSize().height() + font().externalLeading();
  for (int i = 0; i < nLines; i++) {
    char *q = strchr(p, '\n');
    if (q) {
      *q = 0;
    }
    int width = font().textWidth(p);
    if (width > maxWidth) {
      maxWidth = width;
    }
    LOGV(width);
    LOGV(p);
    if (q) {
      *q = '\n';
    }
    p = q+1;
    if (q && *p == '\v') {
      where.setY(where.y() + vTab);
      p++;
    }
    where.setY(where.y() + leading);
  }
  //int enWidth = font().avgCharWidth();
  maxWidth += 2*margin.width();

  ISize minSize(maxWidth, where.y() + 2*margin.height());
  LOGV(minSize.asString());
  return minSize;
}