1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/********************************************************************************
* *
* U n d o / R e d o - a b l e C o m m a n d *
* *
*********************************************************************************
* Copyright (C) 2000,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXAccelTable.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXUndoList.h"
/*
Notes:
- When a command is undone, its moved to the redo list.
- When a command is redone, its moved back to the undo list.
- Whenever adding a new command, the redo list is deleted.
- At any time, you can trim down the undo list down to a given
maximum size or a given number of undo records. This should
keep the memory overhead within sensible bounds.
- To keep track of when we get back to an "unmodified" state, a mark
can be set. The mark is basically a counter which is incremented
with every undo record added, and decremented when undoing a command.
When we get back to 0, we are back to the "unmodified" state.
If, after setting the mark, we have called undo(), then the mark can be
reached by calling redo().
If the marked position is in the redo-list, then adding a new undo
record will cause the redo-list to be deleted, and the marked position
will become unreachable.
The marked state may also become unreachable when the undo list is trimmed.
- You can call also kill the redo list without adding a new command
to the undo list, although this may cause the marked position to
become unreachable.
- We measure the size of the undo-records in the undo-list; when the
records are moved to the redo-list, they usually contain different
information!
- Because we may need to know during execution of a command whether this
was due to undoing or directly issued by the user, we keep a flag "working"
which is true during a undo or redo operation.
- Command groups are collections of smaller undo/redo records which are
executed as a unit, so that large operations may be broken up into
smaller units. This allows written fewer, simpler undo/redo records
for some basic operations where otherwise one would have to write
many, complex undo/redo records.
- Command groups may be arbitrarily nested.
- FXCommand is now derived from FXObject; this means (1) FXCommands can
send messages, and (2) can also receive messages. It is hoped that
this can be used to interface FXCommand directly with targets via
message exchange, i.e. obviate the need to write glue code.
*/
#define NOMARK 2147483647 // No mark is set
using namespace FX;
/*******************************************************************************/
namespace FX {
// Object implementation
FXIMPLEMENT_ABSTRACT(FXCommand,FXObject,nullptr,0)
// Default implementation of undo name is just "Undo"
FXString FXCommand::undoName() const { return "Undo"; }
// Default implementation of redo name is just "Redo"
FXString FXCommand::redoName() const { return "Redo"; }
// Allow merging is false by default
FXbool FXCommand::canMerge() const { return false; }
// Don't merge by default
FXbool FXCommand::mergeWith(FXCommand*){ return false; }
// Default returns size of undo record itself
FXuint FXCommand::size() const { return sizeof(FXCommand); }
/*******************************************************************************/
// Object implementation
FXIMPLEMENT(FXCommandGroup,FXCommand,nullptr,0)
// Undoing a command group undoes each sub command
void FXCommandGroup::undo(){
FXCommand *command;
while(undolist){
command=undolist;
undolist=undolist->next;
command->undo();
command->next=redolist;
redolist=command;
}
}
// Undoing a command group undoes each sub command
void FXCommandGroup::redo(){
FXCommand *command;
while(redolist){
command=redolist;
redolist=redolist->next;
command->redo();
command->next=undolist;
undolist=command;
}
}
// Return the size of the information in the undo command group.
FXuint FXCommandGroup::size() const {
FXuint result=sizeof(FXCommandGroup);
FXCommand *command;
for(command=undolist; command; command=command->next){
result+=command->size();
}
for(command=redolist; command; command=command->next){
result+=command->size();
}
return result;
}
// Destrying the command group destroys the subcommands
FXCommandGroup::~FXCommandGroup(){
FXCommand *command;
while(redolist){
command=redolist;
redolist=redolist->next;
delete command;
}
while(undolist){
command=undolist;
undolist=undolist->next;
delete command;
}
delete group;
}
/*******************************************************************************/
// Map
FXDEFMAP(FXUndoList) FXUndoListMap[]={
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_CLEAR, FXUndoList::onCmdClear),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_CLEAR, FXUndoList::onUpdClear),
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REVERT, FXUndoList::onCmdRevert),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REVERT, FXUndoList::onUpdRevert),
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO, FXUndoList::onCmdUndo),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO, FXUndoList::onUpdUndo),
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO, FXUndoList::onCmdRedo),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO, FXUndoList::onUpdRedo),
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_UNDO_ALL, FXUndoList::onCmdUndoAll),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_ALL, FXUndoList::onUpdUndo),
FXMAPFUNC(SEL_COMMAND, FXUndoList::ID_REDO_ALL, FXUndoList::onCmdRedoAll),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_ALL, FXUndoList::onUpdRedo),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_UNDO_COUNT, FXUndoList::onUpdUndoCount),
FXMAPFUNC(SEL_UPDATE, FXUndoList::ID_REDO_COUNT, FXUndoList::onUpdRedoCount),
};
// Object implementation
FXIMPLEMENT(FXUndoList,FXCommandGroup,FXUndoListMap,ARRAYNUMBER(FXUndoListMap))
// Make new empty undo list
FXUndoList::FXUndoList(){
undocount=0;
redocount=0;
marker=NOMARK;
space=0;
working=false;
}
// Mark current state
void FXUndoList::mark(){
marker=0;
}
// Unmark undo list
void FXUndoList::unmark(){
marker=NOMARK;
}
// Check if marked
FXbool FXUndoList::marked() const {
return (group==nullptr) && (marker==0);
}
// Cut the redo list; can no longer revert to marked
// state if mark is inside the redo list.
void FXUndoList::cut(){
FXCommand *command;
if(marker<0) marker=NOMARK;
while(redolist){
command=redolist;
redolist=redolist->next;
delete command;
}
redolist=nullptr;
redocount=0;
}
// Add new command, executing if desired
void FXUndoList::add(FXCommand* command,FXbool doit,FXbool merge){
FXCommandGroup* g=this;
FXuint oldsize=0;
// Must pass a command
if(!command){ fxerror("FXCommandGroup::add: NULL command argument.\n"); }
// Adding undo while in the middle of doing something!
if(working){ fxerror("FXCommandGroup::add: already working on undo or redo.\n"); }
working=true;
// Cut redo list
cut();
// Execute command
if(doit) command->redo();
// Hunt for end of group chain
while(g->group){ g=g->group; }
// Old size of previous record
if(g->undolist) oldsize=g->undolist->size();
// Try to merge commands when desired and possible
if(merge && g->undolist && !marked() && command->canMerge() && g->undolist->mergeWith(command)){
// Account for merge
if(this==g){
// Update space, which is the new size less the old size
space+=g->undolist->size()-oldsize;
}
// Delete incoming command that was merged
delete command;
}
// Append new command to undo list
else{
// Append incoming command
command->next=g->undolist;
g->undolist=command;
// Account for one more undo step
if(this==g){
// Update space, add the size of the new command
space+=command->size();
// Update marker and undo counter
if(marker!=NOMARK) marker++;
undocount++;
}
}
FXTRACE((100,"FXUndoList::add: space=%d undocount=%d marker=%d\n",space,undocount,marker));
working=false;
}
// Begin a new undo command group
void FXUndoList::begin(FXCommandGroup *command){
FXCommandGroup* g=this;
// Must pass a command group
if(!command){ fxerror("FXCommandGroup::begin: NULL command argument.\n"); }
// Calling begin while in the middle of doing something!
if(working){ fxerror("FXCommandGroup::begin: already working on undo or redo.\n"); }
// Cut redo list
cut();
// Hunt for end of group chain
while(g->group){ g=g->group; }
// Add to end
g->group=command;
}
// End undo command group
void FXUndoList::end(){
FXCommandGroup *command;
FXCommandGroup *g=this;
// Must have called begin
if(!g->group){ fxerror("FXCommandGroup::end: no matching call to begin.\n"); }
// Calling end while in the middle of doing something!
if(working){ fxerror("FXCommandGroup::end: already working on undo or redo.\n"); }
// Hunt for one above end of group chain
while(g->group->group){ g=g->group; }
// Unlink from group chain
command=g->group;
g->group=nullptr;
// Add to group if non-empty
if(!command->empty()){
// Append new command to undo list
command->next=g->undolist;
g->undolist=command;
// Update marker and undo count
if(this==g){
// Update space of completed command group
space+=command->size();
// Update marker and undo counter
if(marker!=NOMARK) marker++;
undocount++;
}
}
// Or delete if empty
else{
// Delete bottom group
delete command;
}
}
// Abort undo command group
void FXUndoList::abort(){
FXCommandGroup *g=this;
// Must be called after begin
if(!g->group){ fxerror("FXCommandGroup::abort: no matching call to begin.\n"); }
// Calling abort while in the middle of doing something!
if(working){ fxerror("FXCommandGroup::abort: already working on undo or redo.\n"); }
// Hunt for one above end of group chain
while(g->group->group){ g=g->group; }
// Delete bottom group
delete g->group;
// New end of chain
g->group=nullptr;
}
// Undo last command
void FXUndoList::undo(){
FXCommand *command;
if(group){ fxerror("FXCommandGroup::undo: cannot call undo inside begin-end block.\n"); }
if(undolist){
working=true;
command=undolist; // Remove from undolist BEFORE undo
undolist=undolist->next;
space-=command->size(); // Measure BEFORE undo!
command->undo();
command->next=redolist; // Hang into redolist AFTER undo
redolist=command;
undocount--;
redocount++;
if(marker!=NOMARK) marker--;
FXTRACE((100,"FXUndoList::undo: space=%d undocount=%d redocount=%d marker=%d\n",space,undocount,redocount,marker));
working=false;
}
}
// Redo next command
void FXUndoList::redo(){
FXCommand *command;
if(group){ fxerror("FXCommandGroup::redo: cannot call undo inside begin-end block.\n"); }
if(redolist){
working=true;
command=redolist; // Remove from redolist BEFORE redo
redolist=redolist->next;
command->redo();
space+=command->size(); // Measure AFTER redo!
command->next=undolist; // Hang into undolist AFTER redo
undolist=command;
undocount++;
redocount--;
if(marker!=NOMARK) marker++;
FXTRACE((100,"FXUndoList::redo: space=%d undocount=%d redocount=%d marker=%d\n",space,undocount,redocount,marker));
working=false;
}
}
// Undo all commands
void FXUndoList::undoAll(){
while(canUndo()) undo();
}
// Redo all commands
void FXUndoList::redoAll(){
while(canRedo()) redo();
}
// Revert to marked
void FXUndoList::revert(){
if(marker!=NOMARK){
while(marker>0) undo();
while(marker<0) redo();
}
}
// Can we undo more commands
FXbool FXUndoList::canUndo() const {
return undolist!=nullptr;
}
// Can we redo more commands
FXbool FXUndoList::canRedo() const {
return redolist!=nullptr;
}
// Can revert to marked
FXbool FXUndoList::canRevert() const {
return marker!=NOMARK && marker!=0;
}
// Return name of the first undo command available, if any
FXString FXUndoList::undoName() const {
if(undolist) return undolist->undoName();
return FXString::null;
}
// Return name of the first redo command available, if any
FXString FXUndoList::redoName() const {
if(redolist) return redolist->redoName();
return FXString::null;
}
// Clear list
void FXUndoList::clear(){
FXCommand *command;
FXTRACE((100,"FXUndoList::clear: space=%d undocount=%d redocount=%d marker=%d\n",space,undocount,redocount,marker));
while(redolist){
command=redolist;
redolist=redolist->next;
delete command;
}
while(undolist){
command=undolist;
undolist=undolist->next;
delete command;
}
delete group;
redolist=nullptr;
undolist=nullptr;
marker=NOMARK;
undocount=0;
redocount=0;
group=nullptr;
space=0;
}
// Clear undo list
long FXUndoList::onCmdClear(FXObject*,FXSelector,void*){
clear();
return 1;
}
// Update Clear undo list
long FXUndoList::onUpdClear(FXObject* sender,FXSelector,void*){
sender->handle(this,(canUndo()||canRedo())?FXSEL(SEL_COMMAND,FXWindow::ID_ENABLE):FXSEL(SEL_COMMAND,FXWindow::ID_DISABLE),nullptr);
return 1;
}
// Revert to marked
long FXUndoList::onCmdRevert(FXObject*,FXSelector,void*){
revert();
return 1;
}
// Update revert to marked
long FXUndoList::onUpdRevert(FXObject* sender,FXSelector,void*){
sender->handle(this,canRevert()?FXSEL(SEL_COMMAND,FXWindow::ID_ENABLE):FXSEL(SEL_COMMAND,FXWindow::ID_DISABLE),nullptr);
return 1;
}
// Undo last command
long FXUndoList::onCmdUndo(FXObject*,FXSelector,void*){
undo();
return 1;
}
// Undo all commands
long FXUndoList::onCmdUndoAll(FXObject*,FXSelector,void*){
undoAll();
return 1;
}
// Update undo last command
long FXUndoList::onUpdUndo(FXObject* sender,FXSelector,void*){
sender->handle(this,canUndo()?FXSEL(SEL_COMMAND,FXWindow::ID_ENABLE):FXSEL(SEL_COMMAND,FXWindow::ID_DISABLE),nullptr);
return 1;
}
// Redo last command
long FXUndoList::onCmdRedo(FXObject*,FXSelector,void*){
redo();
return 1;
}
// Redo all commands
long FXUndoList::onCmdRedoAll(FXObject*,FXSelector,void*){
redoAll();
return 1;
}
// Update redo last command
long FXUndoList::onUpdRedo(FXObject* sender,FXSelector,void*){
sender->handle(this,canRedo()?FXSEL(SEL_COMMAND,FXWindow::ID_ENABLE):FXSEL(SEL_COMMAND,FXWindow::ID_DISABLE),nullptr);
return 1;
}
// Update undo count
long FXUndoList::onUpdUndoCount(FXObject* sender,FXSelector,void*){
sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&undocount);
return 1;
}
// Update redo count
long FXUndoList::onUpdRedoCount(FXObject* sender,FXSelector,void*){
sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&redocount);
return 1;
}
// Size of undo information
FXuint FXUndoList::size() const {
return space;
}
// Trim undo list down to at most nc records
void FXUndoList::trimCount(FXint nc){
FXTRACE((100,"FXUndoList::trimCount: was: space=%d undocount=%d; marker=%d ",space,undocount,marker));
if(undocount>nc){
FXCommand **pp=&undolist;
FXCommand *p=*pp;
FXint i=0;
while(p && i<nc){
pp=&p->next;
p=*pp;
i++;
}
while(*pp){
p=*pp;
*pp=p->next;
space-=p->size();
undocount--;
delete p;
}
if(marker>undocount) marker=NOMARK;
}
FXTRACE((100,"now: space=%d undocount=%d; marker=%d\n",space,undocount,marker));
}
// Trim undo list down to at most size sz
void FXUndoList::trimSize(FXuint sz){
FXTRACE((100,"FXUndoList::trimSize: was: space=%d undocount=%d; marker=%d ",space,undocount,marker));
if(space>sz){
FXCommand **pp=&undolist;
FXCommand *p=*pp;
FXuint s=0;
while(p && (s=s+p->size())<=sz){
pp=&p->next;
p=*pp;
}
while(*pp){
p=*pp;
*pp=p->next;
space-=p->size();
undocount--;
delete p;
}
if(marker>undocount) marker=NOMARK;
}
FXTRACE((100,"now: space=%d undocount=%d; marker=%d\n",space,undocount,marker));
}
}
|