view anagram/vaclgui/fileview.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.
 *
 * fileview.cpp
 */

#include "agcstack.h"
#include "dspar.hpp"
#include "fileview.hpp"
#include "vaclgui.hpp"

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


FileView::FileView(IWindow *owner, text_file &file_)
  : AgView(owner)
  , file(file_)
  , dataColorChange(this, onColorChange)
  , fontChange(this, onFontChange)
{
  LOGSECTION("FileView::FileView");
  LOGV(file.name.pointer());
  LOGV(file.size());
  LOGV((int) file.text.pointer());
  dataColorChange.attach(&ColorSpec::syntaxFile);
  dataColorChange.attach(&ColorSpec::activeCursor);
  dataColorChange.attach(&ColorSpec::inactiveCursor);

  setFont(FontSpec::syntaxFile);
  fontChange.attach(&FontSpec::syntaxFile);
}

FileView::~FileView() {
  LOGSECTION("FileView::~FileView");
}

void FileView::onFontChange() {
  setFont(FontSpec::syntaxFile);
  tableWidth = 0;
  doLayout();
}

AgString FileView::getLine(unsigned ln) {
  //LOGSECTION("FileView::getLine", Log::off);
  //LOGSECTION_OFF("FileView::getLine");
  LOGSECTION_ON("FileView::getLine");
  LOGV(ln);
  AgCharStack line;
  if (ln >= file.size().y) {
    return AgString();
  }
  char *p = file.line(ln);
  LOGV((int) p);
  while (*p && *p != '\n') {
    char c = *p++;
    if (c == '\r') {
      continue;
    }
    line.push(c);
  }
  AgString lineString = line.popString();
  LOGV(lineString);
  return lineString;
  //return line.popString();
}

void FileWindow::init(text_file &file, IRectangle initialRect) {
  LOGSECTION("FileWindow::init");
  windowId = id();

  AgString viewName = file.name.lastCut("\\/").rightX().lastCut('.').leftX();
  setClient(&fileView);
  IFont viewFont  = FontSpec::syntaxFile;
  LOGV(viewName.pointer());
  registerTitle(viewName);
  LOGV(initialRect.asString()) LCV(initialRect.area());
  if (initialRect.area() <= 0) {
    int height = viewFont.externalLeading() + viewFont.maxSize().height();
    height *= 15;
/*
    int width = fileView.suggestSize().width();
    int minWidth = 60*viewFont.avgCharWidth();
    if (width < minWidth) {
      width = minWidth;
    }
*/
    int width = 80*viewFont.avgCharWidth();
    LOGV(width);
    LOGV(viewFont.name()) LCV(viewFont.pointSize()) LCV(viewFont.isFixed());
    if (viewFont.isFixed()) {
      width = 80*viewFont.maxSize().width();
    }
    LOGV(width);
    ISize size(width, height);
    LOGV(size.asString());
    sizeTo(frameRectFor(IRectangle(size)).size());
    //positionFrame();
  }
  else {
    moveSizeTo(adjustPos(initialRect));
  }

  windowTitle.setViewText(viewName.pointer());
}

Boolean FileView::findNext(AgString s) {
  LOGSECTION("FileView::findNext");
  cint loc = getCursorLocation();
  int flag;

  if (cursorLineHighlight) {
    loc.y++;
    if (loc.y >= nLines()) {
      return false;
    }
    loc.x = 0;
    flag = file.findNext(loc, s);
    if (!flag) {
      loc = getCursorLocation();
    }
  }
  else {
    flag = file.findNext(loc, s);
  }
  setCursorLocation(loc);
  return flag;
}

Boolean FileView::findPrev(AgString s) {
  LOGSECTION("FileView::findPrev");
  cint loc = getCursorLocation();
  int flag;
  if (cursorLineHighlight) {
    loc.x = 0;
    flag = file.findPrev(loc, s);
    if (!flag) {
      loc = getCursorLocation();
    }
  }
  else {
    flag = file.findPrev(loc,s);
  }
  setCursorLocation(loc);
  return flag;
}