[Avida-SVN] r1761 - development/source/targets/viewer-text

ofria at myxo.css.msu.edu ofria at myxo.css.msu.edu
Sat Jul 7 08:47:32 PDT 2007


Author: ofria
Date: 2007-07-07 11:47:31 -0400 (Sat, 07 Jul 2007)
New Revision: 1761

Added:
   development/source/targets/viewer-text/cTextWindow.cc
   development/source/targets/viewer-text/cTextWindow.h
Log:
Text window managers for the new viewer.


Added: development/source/targets/viewer-text/cTextWindow.cc
===================================================================
--- development/source/targets/viewer-text/cTextWindow.cc	                        (rev 0)
+++ development/source/targets/viewer-text/cTextWindow.cc	2007-07-07 15:47:31 UTC (rev 1761)
@@ -0,0 +1,242 @@
+/*
+ *  cTextWindow.cc
+ *  Avida
+ *
+ *  Created by Charles on 7/6/07
+ *  Copyright 1999-2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#include "cTextWindow.h"
+
+#include "cCoreView_Info.h"
+
+#include <cstdlib>
+
+using namespace std;
+
+
+cTextWindow::cTextWindow(cTextWindow * parent, cCoreView_Info & info)
+  : m_win_id(NULL), m_parent_window(parent), m_info(info)
+{
+  m_info.fp << "cTW::cTextWindow -- Base constructor for " << this << endl; // DEBUG!!!!!
+}
+
+
+cTextWindow::cTextWindow(cTextWindow * parent, cCoreView_Info & info, int y_size, int x_size, int y_start, int x_start)
+  : m_parent_window(parent), m_info(info)
+{
+  // Build this window.
+  m_win_id = newwin(y_size, x_size, y_start, x_start);
+
+  m_info.fp << "cTW::cTextWindow -- Full constructor for " << this << "; id=" << m_win_id << endl; // DEBUG!!!!!
+
+
+  // If a parent was given, register with it.
+  if (m_parent_window != NULL) m_parent_window->AddSubWindow(this);
+
+//   keypad(m_win_id, 1);        // Allows the keypad to be used.
+//   NoDelay();
+}
+
+
+cTextWindow::~cTextWindow()
+{
+  m_info.fp << "cTW::~cTextWindow -- Destructor for " << this << endl; // DEBUG!!!!!
+
+  // Delete all sub-windows...
+  for (int i = 0; i < m_sub_windows.GetSize(); i++) {
+    if (m_sub_windows[i]) delete m_sub_windows[i];
+  }
+
+  // And cleanup this window.
+  werase(m_win_id);
+  wrefresh(m_win_id);
+  delwin(m_win_id);
+}
+
+void cTextWindow::Construct(int y_size, int x_size, int y_start, int x_start)
+{
+  assert(m_win_id == NULL);
+  
+  m_win_id = newwin(y_size, x_size, y_start, x_start);
+
+  m_info.fp << "cTW::Construct() for " << this << "; new id = " << m_win_id << endl; // DEBUG!!!!!
+
+  // If a parent was given, register with it.
+  if (m_parent_window != NULL) m_parent_window->AddSubWindow(this);
+  keypad(m_win_id, 1);        // Allows the keypad to be used.
+}
+
+
+
+int cTextWindow::AddSubWindow(cTextWindow * in_window)
+{
+  assert(in_window != NULL);
+
+  m_info.fp << "cTW::AddSubWindow(" << in_window << ") for " << this << endl; // DEBUG!!!!!
+
+  // Scan existing array for open spots...
+  for (int i = 0; i < m_sub_windows.GetSize(); i++) {
+    if (m_sub_windows[i] == NULL) {
+      m_sub_windows[i] = in_window;
+      return i;
+    }
+  }
+  return m_sub_windows.Push(in_window);
+}
+
+
+cTextWindow * cTextWindow::DetachSubWindow(int id)
+{
+  assert(m_sub_windows[id] != NULL);
+  cTextWindow * out_window = m_sub_windows[id];
+  m_sub_windows[id] = NULL;
+  return out_window;
+}
+
+
+void cTextWindow::CloseSubWindow(int id)
+{
+  assert(m_sub_windows[id] != NULL);
+  delete m_sub_windows[id];
+  m_sub_windows[id] = NULL;
+}
+
+
+void cTextWindow::Refresh()
+{
+  m_info.fp << "cTW::Refresh() for " << this << endl; // DEBUG!!!!!
+
+  RefreshSelf();
+  for (int i = 0; i < m_sub_windows.GetSize(); i++) {
+    if (m_sub_windows[i]) m_sub_windows[i]->Refresh();
+  }
+}
+
+
+void cTextWindow::Redraw()
+{
+  m_info.fp << "cTW::Redraw() for " << this << endl; // DEBUG!!!!!
+
+  RedrawSelf();
+  for (int i = 0; i < m_sub_windows.GetSize(); i++) {
+    if (m_sub_windows[i]) m_sub_windows[i]->Redraw();
+  }
+}
+
+void cTextWindow::Clear()
+{
+  m_info.fp << "cTW::Clear() for " << this << endl; // DEBUG!!!!!
+
+  ClearMain();
+  for (int i = 0; i < m_sub_windows.GetSize(); i++) {
+    if (m_sub_windows[i]) m_sub_windows[i]->Clear();
+  }
+}
+
+
+void cTextWindow::Print(const char * fmt, ...)
+{
+  va_list argp;
+  char buf[BUFSIZ];
+  
+  va_start(argp, fmt);
+  vsprintf(buf, fmt, argp);
+  va_end(argp);
+  waddstr(m_win_id, buf);
+}
+
+
+void cTextWindow::Print(int in_y, int in_x, const char * fmt, ...)
+{
+  va_list argp;
+  char buf[BUFSIZ];
+  
+  va_start(argp, fmt);
+  vsprintf(buf, fmt, argp);
+  va_end(argp);
+  wmove(m_win_id, in_y, in_x);
+  waddstr(m_win_id, buf);
+
+  m_info.fp << "cTW::Print(" << buf << ") for " << this << endl; // DEBUG!!!!!
+}
+
+
+void cTextWindow::PrintBinary(int in_y, int in_x, unsigned int value)
+{
+  for (int i = 0; i < 32; i++) {
+    if ((value >> i) & 1) Print(in_y, in_x+31-i, '1');
+    else Print(in_y, in_x+31-i, '0');
+  }
+}
+
+void cTextWindow::Box(int y, int x, int h, int w)
+{
+  m_info.fp << "cTW::Box(inputs!!) for " << this << endl; // DEBUG!!!!!
+
+  int i;
+  for (i = 1; i < h - 1; i++) {
+    mvwaddch(m_win_id, i + y, x, ACS_VLINE);
+    mvwaddch(m_win_id, i + y, x + w - 1, ACS_VLINE);
+  }
+  for (i = 1; i < w - 1; i++) {
+    mvwaddch(m_win_id, y, i + x, ACS_HLINE);
+    mvwaddch(m_win_id, y + h - 1, i + x, ACS_HLINE);
+  }
+  mvwaddch(m_win_id, y, x, ACS_ULCORNER);
+  mvwaddch(m_win_id, y + h - 1, x, ACS_LLCORNER);
+  mvwaddch(m_win_id, y, x + w - 1, ACS_URCORNER);
+  mvwaddch(m_win_id, y + h - 1, x + w - 1, ACS_LRCORNER);
+}
+
+void cTextWindow::VLine(int in_x)
+{
+  mvwaddch(m_win_id, 0, in_x, ACS_TTEE);
+  mvwaddch(m_win_id, GetHeight() - 1, in_x, ACS_BTEE);
+  for (int i = 1; i < GetHeight() - 1; i++) {
+    mvwaddch(m_win_id, i, in_x, ACS_VLINE);
+  }
+}
+
+void cTextWindow::VLine(int in_x, int start_y, int length)
+{
+  mvwaddch(m_win_id, start_y, in_x, ACS_TTEE);
+  mvwaddch(m_win_id, start_y + length - 1, in_x, ACS_BTEE);
+  for (int i = 1; i < length - 1; i++) {
+    mvwaddch(m_win_id, start_y+i, in_x, ACS_VLINE);
+  }
+}
+
+void cTextWindow::HLine(int in_y)
+{
+  mvwaddch(m_win_id, in_y, 0, ACS_LTEE);
+  mvwaddch(m_win_id, in_y, GetWidth() - 1, ACS_RTEE);
+  for (int i = 1; i < GetWidth() - 1; i++) {
+    mvwaddch(m_win_id, in_y, i, ACS_HLINE);
+  }
+}
+
+void cTextWindow::HLine(int in_y, int start_x, int length)
+{
+  mvwaddch(m_win_id, in_y, start_x, ACS_LTEE);
+  mvwaddch(m_win_id, in_y, start_x + length - 1, ACS_RTEE);
+  for (int i = 1; i < length - 1; i++) {
+    mvwaddch(m_win_id, in_y, start_x + i, ACS_HLINE);
+  }
+}

Added: development/source/targets/viewer-text/cTextWindow.h
===================================================================
--- development/source/targets/viewer-text/cTextWindow.h	                        (rev 0)
+++ development/source/targets/viewer-text/cTextWindow.h	2007-07-07 15:47:31 UTC (rev 1761)
@@ -0,0 +1,131 @@
+/*
+ *  cTextWindow.h
+ *  Avida
+ *
+ *  Created by Charles on 7/6/07
+ *  Copyright 1999-2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef cTextWindow_h
+#define cTextWindow_h
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
+#ifndef ncurses_defs_h
+#include "ncurses-defs.h"
+#endif
+
+class cCoreView_Info;
+
+class cTextWindow {
+protected:
+  WINDOW * m_win_id;
+
+  cTextWindow * m_parent_window;
+  tArray<cTextWindow *> m_sub_windows;
+
+  cCoreView_Info & m_info;
+
+  // Internal Functions
+  void RefreshSelf() { wrefresh(m_win_id); }
+  void RedrawSelf() { touchwin(m_win_id); wrefresh(m_win_id); }
+
+public:
+  cTextWindow(cTextWindow * parent, cCoreView_Info & info);
+  cTextWindow(cTextWindow * parent, cCoreView_Info & info, int y_size, int x_size, int y_start=0, int x_start=0);
+  ~cTextWindow();
+
+  void Construct(int y_size, int x_size, int y_start=0, int x_start=0);
+
+  // ------ Accessors ------
+  cTextWindow * GetParentWindow() { return m_parent_window; }
+  cTextWindow * GetSubWindow(int id) { return m_sub_windows[id]; }
+
+  // These function return the number of characters wide or high
+  // (respectively) that the screen is.
+#if AVIDA_PLATFORM(WINDOWS)
+  // Windows returns the screen width and height
+  inline int GetWidth() { return m_win_id->_maxx; }
+  inline int GetHeight() { return m_win_id->_maxy; }
+#else
+  // Elsewhere returns the max x and y coordinates, like expected
+  inline int GetWidth() { return m_win_id->_maxx + 1; }
+  inline int GetHeight() { return m_win_id->_maxy + 1; }
+#endif
+
+
+  // ------ Dealing with sub-windows ------
+  int AddSubWindow(cTextWindow * in_window);
+  cTextWindow * DetachSubWindow(int id);
+  void CloseSubWindow(int id);
+
+
+  // ------ General Functions ------
+  void Refresh();
+  void Redraw();
+  void Clear();
+
+  // ------ Printing Text ------
+  // The following functions print characters onto the screen.  If they
+  // begin with an y, x then they first move to those coords.
+
+  void Print(chtype ch) { waddch(m_win_id, ch); }
+  void Print(int in_y, int in_x, chtype ch) { mvwaddch(m_win_id, in_y, in_x, ch); }
+  void Print(const char * fmt, ...);
+  void Print(int in_y, int in_x, const char * fmt, ...);
+  void PrintBinary(int in_y, int in_x, unsigned int value);
+
+
+  // ------ Drawing & Erasing ------
+  // Move the active position of the cursor.
+  inline void Move(int new_y, int new_x) { wmove(m_win_id, new_y, new_x); }
+
+  // These functions clear sections of the screen.
+  inline void ClearMain() { wclear(m_win_id); }
+  inline void ClearToBot() { wclrtobot(m_win_id); }
+  inline void ClearToEOL() { wclrtoeol(m_win_id); }
+
+  // Various 'graphic' functions.  Box() draws a box-frame around the window.
+  // With arguments, it draws a box starting at x, y, with size w by h.
+  // VLine and HLine draw lines across the screen ending in the appropriate
+  // facing T's (hence, they are used to cut boxes in half.  With two
+  // coords and a length, they only draw the line from the specified start,
+  // to the specified distance.
+  inline void Box() { box(m_win_id, 0, 0); }
+  void Box(int x, int y, int w, int h);
+  void VLine(int in_x);
+  void VLine(int in_x, int start_y, int length);
+  void HLine(int in_y);
+  void HLine(int in_y, int start_x, int length);
+
+  // These functions check or set the screen colors (BG is always black)
+  inline int HasColors() { return has_colors(); }
+  inline void SetColor(int color) {
+    wattrset(m_win_id, COLOR_PAIR(color));
+  }
+  inline void SetBoldColor(int color) {
+    wattrset(m_win_id, COLOR_PAIR(color) | A_BOLD);
+  }
+
+};
+
+#endif
+




More information about the Avida-cvs mailing list