home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IDRAW / HISTORY.C < prev    next >
C/C++ Source or Header  |  1980-01-05  |  4KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: history.c,v 1.8 89/10/09 14:48:05 linton Exp $
  24. // implements class History.
  25.  
  26. #include "history.h"
  27. #include "istring.h"
  28. #include "listchange.h"
  29. #include <InterViews/interactor.h>
  30. #include <stdio.h>
  31.  
  32. // History sets its maximum number of changes and creates the history.
  33.  
  34. History::History (Interactor* i) {
  35.     const char* def = i->GetAttribute("history");
  36.     char* definition = strdup(def); // some sscanfs write to their format...
  37.     if (sscanf(definition, "%d", &maxhistory) != 1) {
  38.     maxhistory = 20;    // default if we can't parse definition
  39.     fprintf(stderr, "can't parse attribute for history, ");
  40.     fprintf(stderr, "value set to %d\n", maxhistory);
  41.     }
  42.     delete definition;
  43.     changelist = new ChangeList;
  44. }
  45.  
  46. // ~History frees storage allocated for the changes.
  47.  
  48. History::~History () {
  49.     delete changelist;
  50. }
  51.  
  52. // IsEmpty returns true if there are no changes stored in the list.
  53.  
  54. boolean History::IsEmpty () {
  55.     return changelist->Size() == 0;
  56. }
  57.  
  58. // Clear deletes all of the stored changes.  Calling Clear prevents
  59. // dangling pointers from being referenced if the user tries to undo a
  60. // stored change that was made before a New, Revert, or Open command.
  61.  
  62. void History::Clear () {
  63.     changelist->DeleteAll();
  64. }
  65.  
  66. // Do truncates the history at its current point, trims the history to
  67. // its last maxhistory-1 changes, performs the change, appends the
  68. // change, and leaves the current point at the end of the history.
  69.  
  70. void History::Do (ChangeNode* changenode) {
  71.     while (!changelist->AtEnd()) {
  72.     changelist->DeleteCur();
  73.     }
  74.     changelist->First();
  75.     while (changelist->Size() >= maxhistory) {
  76.     changelist->DeleteCur();
  77.     }
  78.     changenode->Do();
  79.     changelist->Append(changenode);
  80.     changelist->Last();
  81.     changelist->Next();
  82. }
  83.  
  84. // Redo redoes a stored change in the history and steps over it unless
  85. // the current point is already at the end of the history.
  86.  
  87. void History::Redo () {
  88.     if (!changelist->AtEnd()) {
  89.     ChangeNode* changenode = changelist->GetCur();
  90.     changenode->Do();
  91.     changelist->Next();
  92.     }
  93. }
  94.  
  95. // Undo steps back in the history and undoes a stored change unless
  96. // the current point is already at the beginning of the history.
  97.  
  98. void History::Undo () {
  99.     changelist->Prev();
  100.     if (!changelist->AtEnd()) {
  101.     ChangeNode* changenode = changelist->GetCur();
  102.     changenode->Undo();
  103.     } else {
  104.     changelist->Next();
  105.     }
  106. }
  107.