view anagram/vaclgui/multiline.cpp @ 24:a4899cdfc2d6 default tip

Obfuscate the regexps to strip off the IBM compiler's copyright banners. I don't want bots scanning github to think they're real copyright notices because that could cause real problems.
author David A. Holland
date Mon, 13 Jun 2022 00:40:23 -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;
}