2005-03-20 16:38:04 +03:00
|
|
|
//
|
|
|
|
// "$Id$"
|
|
|
|
//
|
|
|
|
// FLUID undo support for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2017-10-15 13:18:53 +03:00
|
|
|
// Copyright 1998-2017 by Bill Spitzak and others.
|
2005-03-20 16:38:04 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
2005-03-20 16:38:04 +03:00
|
|
|
//
|
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include "Fl_Type.h"
|
|
|
|
#include "undo.h"
|
|
|
|
#include <FL/Fl_Preferences.H>
|
2010-12-20 00:20:10 +03:00
|
|
|
#include <FL/filename.H>
|
2005-03-20 16:38:04 +03:00
|
|
|
#include "../src/flstring.h"
|
2017-10-15 13:18:53 +03:00
|
|
|
|
2018-02-09 17:39:42 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2005-04-11 02:33:34 +04:00
|
|
|
# include <io.h>
|
2005-03-20 16:38:04 +03:00
|
|
|
# include <windows.h>
|
2005-04-13 20:32:52 +04:00
|
|
|
# define getpid (int)GetCurrentProcessId
|
2005-03-20 16:38:04 +03:00
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
2018-02-09 17:39:42 +03:00
|
|
|
#endif // _WIN32 && !__CYGWIN__
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
extern Fl_Preferences fluid_prefs; // FLUID preferences
|
|
|
|
extern Fl_Menu_Item Main_Menu[]; // Main menu
|
|
|
|
|
2005-03-20 20:41:56 +03:00
|
|
|
#define UNDO_ITEM 25 // Undo menu item index
|
|
|
|
#define REDO_ITEM 26 // Redo menu item index
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// This file implements an undo system using temporary files; ideally
|
|
|
|
// we'd like to do this in memory, however the current data structures
|
|
|
|
// and design aren't well-suited... Instead, we save and restore
|
|
|
|
// checkpoint files.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
int undo_current = 0; // Current undo level in buffer
|
|
|
|
int undo_last = 0; // Last undo level in buffer
|
|
|
|
int undo_max = 0; // Maximum undo level used
|
|
|
|
int undo_save = -1; // Last undo level that was saved
|
|
|
|
static int undo_paused = 0; // Undo checkpointing paused?
|
|
|
|
|
|
|
|
|
|
|
|
// Return the undo filename
|
|
|
|
static char *undo_filename(int level, char *buf, int bufsize) {
|
2010-12-20 00:20:10 +03:00
|
|
|
static char undo_path[FL_PATH_MAX] = ""; // Undo path
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
if (!undo_path[0]) fluid_prefs.getUserdataPath(undo_path, sizeof(undo_path));
|
|
|
|
|
2005-03-20 20:41:56 +03:00
|
|
|
snprintf(buf, bufsize, "%sundo_%d_%d.fl", undo_path, getpid(), level);
|
2005-03-20 16:38:04 +03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Redo menu callback
|
|
|
|
void redo_cb(Fl_Widget *, void *) {
|
2010-12-20 00:20:10 +03:00
|
|
|
char filename[FL_PATH_MAX]; // Undo checkpoint file
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
if (undo_current >= undo_last) return;
|
|
|
|
|
|
|
|
undo_suspend();
|
|
|
|
if (!read_file(undo_filename(undo_current + 1, filename, sizeof(filename)), 0)) {
|
|
|
|
// Unable to read checkpoint file, don't redo...
|
|
|
|
undo_resume();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_current ++;
|
|
|
|
|
|
|
|
// Update modified flag...
|
|
|
|
set_modflag(undo_current != undo_save);
|
|
|
|
|
|
|
|
// Update undo/redo menu items...
|
|
|
|
if (undo_current >= undo_last) Main_Menu[REDO_ITEM].deactivate();
|
|
|
|
Main_Menu[UNDO_ITEM].activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undo menu callback
|
|
|
|
void undo_cb(Fl_Widget *, void *) {
|
2010-12-20 00:20:10 +03:00
|
|
|
char filename[FL_PATH_MAX]; // Undo checkpoint file
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
if (undo_current <= 0) return;
|
|
|
|
|
|
|
|
if (undo_current == undo_last) {
|
|
|
|
write_file(undo_filename(undo_current, filename, sizeof(filename)));
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_suspend();
|
|
|
|
if (!read_file(undo_filename(undo_current - 1, filename, sizeof(filename)), 0)) {
|
|
|
|
// Unable to read checkpoint file, don't undo...
|
|
|
|
undo_resume();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_current --;
|
|
|
|
|
|
|
|
// Update modified flag...
|
|
|
|
set_modflag(undo_current != undo_save);
|
|
|
|
|
|
|
|
// Update undo/redo menu items...
|
|
|
|
if (undo_current <= 0) Main_Menu[UNDO_ITEM].deactivate();
|
|
|
|
Main_Menu[REDO_ITEM].activate();
|
2006-05-05 22:12:05 +04:00
|
|
|
undo_resume();
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save current file to undo buffer
|
|
|
|
void undo_checkpoint() {
|
2010-12-20 00:20:10 +03:00
|
|
|
char filename[FL_PATH_MAX]; // Undo checkpoint filename
|
2005-03-20 16:38:04 +03:00
|
|
|
|
2005-03-20 17:35:18 +03:00
|
|
|
// printf("undo_checkpoint(): undo_current=%d, undo_paused=%d, modflag=%d\n",
|
|
|
|
// undo_current, undo_paused, modflag);
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
// Don't checkpoint if undo_suspend() has been called...
|
|
|
|
if (undo_paused) return;
|
|
|
|
|
|
|
|
// Save the current UI to a checkpoint file...
|
|
|
|
if (!write_file(undo_filename(undo_current, filename, sizeof(filename)))) {
|
|
|
|
// Don't attempt to do undo stuff if we can't write a checkpoint file...
|
|
|
|
perror(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the saved level...
|
|
|
|
if (modflag && undo_current <= undo_save) undo_save = -1;
|
|
|
|
else if (!modflag) undo_save = undo_current;
|
|
|
|
|
|
|
|
// Update the current undo level...
|
|
|
|
undo_current ++;
|
|
|
|
undo_last = undo_current;
|
|
|
|
if (undo_current > undo_max) undo_max = undo_current;
|
|
|
|
|
|
|
|
// Enable the Undo and disable the Redo menu items...
|
|
|
|
Main_Menu[UNDO_ITEM].activate();
|
|
|
|
Main_Menu[REDO_ITEM].deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear undo buffer
|
|
|
|
void undo_clear() {
|
2010-12-20 00:20:10 +03:00
|
|
|
char filename[FL_PATH_MAX]; // Undo checkpoint filename
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Remove old checkpoint files...
|
|
|
|
for (int i = 0; i <= undo_max; i ++) {
|
2017-10-15 13:18:53 +03:00
|
|
|
fl_unlink(undo_filename(i, filename, sizeof(filename)));
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset current, last, and save indices...
|
|
|
|
undo_current = undo_last = undo_max = 0;
|
|
|
|
if (modflag) undo_save = -1;
|
|
|
|
else undo_save = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume undo checkpoints
|
|
|
|
void undo_resume() {
|
|
|
|
undo_paused = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suspend undo checkpoints
|
|
|
|
void undo_suspend() {
|
|
|
|
undo_paused = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// End of "$Id$".
|
|
|
|
//
|