view anagram/guisupport/windowreg.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.
 *
 * windowreg.cpp
 */

#include "ctrlpanel.hpp"  // sigh... XXX (for syntaxFileId/syntaxFileRect)
#include "vaclgui.hpp"    //         XXX (for cascadeOrigin/cascadeOffset)
#include "windowreg.h"

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


void WindowRegistry::registerId(AgFrame *window, 
				AgString title, AgAction action) {
  LOGSECTION("WindowRegistry::registerId");
  int id = window->id();
  int first = 0, last = registry.size();
  LOGV(id) LCV(title.pointer()) LCV(last);
  int firstClone = -1;
  int firstViewNumber = 0;
  int maxView = 1;

  while (first < last) {
    registry[first++].isActive = 0;
  }
  first = 0;
  while (first < last) {
    WindowRecord &windowRecord = registry[first];
    if (windowRecord.id == id) {
      windowRecord.action = action;
      windowRecord.title = title;
      windowRecord.window = window;
      windowRecord.isActive = 1;
      return;
    }
    if (windowRecord.title == title) {
      int viewNumber = windowRecord.window->windowTitle.viewNumber();
      if (firstClone == -1) {
        firstClone = first;
        firstViewNumber = viewNumber;
      }
      if (maxView < viewNumber) {
	maxView = viewNumber;
      }
    }
    first++;
  }
  LOGV(maxView);
  registry.push(WindowRecord());
  LOGV(first) LCV(registry.size());
  WindowRecord &windowRecord = registry[first];
  LOGV((int) &registry[first]);
  LOGV((int) &windowRecord);
  windowRecord.id = id;
  windowRecord.action = action;
  windowRecord.title = title;
  windowRecord.window = window;
  windowRecord.isActive = 1;
  if (firstClone == -1) {
    return;
  }
  if (firstViewNumber == 0) {
    LOGV(firstClone);
    registry[firstClone].window->windowTitle.setViewNumber(1);
  }
  window->windowTitle.setViewNumber(maxView+1);
  LOGS("registration complete");
}

void WindowRegistry::refresh(AgString title) {
  LOGSECTION("WindowRegistry::refresh");
  int first = 0, last = registry.size();
  LOGV(title.pointer()) LCV(last);
  while (first < last) {
    WindowRecord &windowRecord = registry[first];
    if (windowRecord.title == title) {
      windowRecord.window->refresh();
    }
    first++;
  }
}

void WindowRegistry::remove(int id) {
  LOGSECTION("WindowRegistry::remove");
  LOGV(id);
  int first = 0, last = registry.size();
  while (first < last) {
    if (registry[first].id == id) {
      LOGV(registry[first].title.pointer());
      if (id == syntaxFileId) {
        LOGV(syntaxFileId);
        LOGV(syntaxFileRect.asString());
        syntaxFileRect = registry[first].window->rect();
        LOGV(syntaxFileRect.asString());
        syntaxFileId = 0;
      }
      last--;
      while (first < last) {
	registry[first] = registry[first+1];
	first++;
      }
      registry.pop();
      break;
    }
    first++;
  }
  if (registry.size() == 0) {
    cascadeOffset = cascadeOrigin;
  }
}

AgFrame *WindowRegistry::find(AgString title) {
  LOGSECTION("WindowRegistry::find(AgString)");
  LOGV(title.pointer());
  int first = 0, last = registry.size();
  LOGV(last);
  while (first < last) {
    LOGV(registry[first].title.pointer());
    if (registry[first].title == title) {
      return registry[first].window;
    }
    first++;
  }
  return NULL;
}

AgFrame *WindowRegistry::find(int id) {
  LOGSECTION("WindowRegistry::find(int)");
  int first = 0, last = registry.size();
  while (first < last) {
    LOGV(registry[first].id);
    LOGV(registry[first].title.pointer());
    if (registry[first].id == id) {
      return registry[first].window;
    }
    first++;
  }
  return NULL;
}

AgString WindowRegistry::getTitle(int id) {
  int first = 0, last = registry.size();
  while (first < last) {
    if (registry[first].id == id) {
      return registry[first].title;
    }
    first++;
  }
  return AgString();
}

/*
void WindowRegistry::bubbleUp(int id) {
  LOGSECTION("WindowRegistry::bubbleUp");
  if (cascadeFlag) return;
  int first = 0, last = registry.size() - 1;
  while (first < last) {
    LOGV(registry[first].id) LCV(registry[first].title.pointer());
    if (registry[first].id == id) {
      LOGS("found it");
      WindowRecord save = registry[first];
      for (int i = first; i < last; i++) registry[i] = registry[i+1];
      registry[last] = save;
      return;
    }
    first++;
  }
}
*/

void WindowRegistry::markActive(int id) {
  LOGSECTION("WindowRegistry::markActive");
  if (cascadeFlag) {
    return;
  }
  int first = 0, last = registry.size();
  while (first < last) {
    registry[first++].isActive = 0;
  }
  first = 0;
  while (first < last) {
    LOGV(registry[first].id) LCV(registry[first].title.pointer());
    if (registry[first].id == id) {
      LOGS("found it");
      registry[first].isActive = 1;
      return;
    }
    first++;
  }
}

void WindowRegistry::clearActive() {
  LOGSECTION("WindowRegistry::clearActive");
  int first = 0, last = registry.size();
  while (first < last) {
    registry[first++].isActive = 0;
  }
}

int WindowRegistry::action(int id) {
  LOGSECTION("WindowRegistry::action");
  int first = 0, last = registry.size();
  while (first < last) {
    if (registry[first].id == id) {
      registry[first].action.perform();
      return 1;
    }
    first++;
  }
  LOGS("command not recognized");
  return 0;
}

int WindowRegistry::action(AgString title) {
  LOGSECTION("WindowRegistry::action(AgString)");
  LOGV(title.pointer());
  int first = 0, last = registry.size();
  LOGV(last);
  while (first < last) {
    LOGV(registry[first].title.pointer());
    if (registry[first].title == title) {
      registry[first].action.perform();
      return 1;
    }
    first++;
  }
  return 0;
}