view anagram/vaclgui/multiline.cpp @ 15:f5acaf0c8a29

Don't cast through "volatile int". Causes a gcc warning nowadays. XXX: should put something else back here to frighten the optimizer
author David A. Holland
date Tue, 31 May 2022 01:00:55 -0400
parents 13d2b8934445
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;
}