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