2005-03-20 16:38:04 +03:00
|
|
|
//
|
|
|
|
// FLUID undo support for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2021-08-30 23:00:59 +03:00
|
|
|
// Copyright 1998-2021 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:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2005-03-20 16:38:04 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-03-20 16:38:04 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2005-03-20 16:38:04 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "undo.h"
|
2021-12-08 17:52:15 +03:00
|
|
|
|
|
|
|
#include "fluid.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "Fl_Type.h"
|
2022-12-01 03:00:12 +03:00
|
|
|
#include "Fl_Widget_Type.h"
|
2021-12-11 21:43:00 +03:00
|
|
|
#include "widget_browser.h"
|
2021-12-08 17:52:15 +03:00
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
2022-12-01 03:00:12 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
2005-03-20 16:38:04 +03:00
|
|
|
#include <FL/Fl_Preferences.H>
|
2021-12-08 17:52:15 +03:00
|
|
|
#include <FL/Fl_Menu_Bar.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
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2022-12-01 03:00:12 +03:00
|
|
|
extern Fl_Window* the_panel;
|
2005-03-20 16:38:04 +03:00
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
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?
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
|
2019-08-29 18:17:37 +03:00
|
|
|
// Return the undo filename.
|
|
|
|
// The filename is constructed in a static internal buffer and
|
|
|
|
// this buffer is overwritten by every call of this function.
|
|
|
|
// The return value is a pointer to this internal string.
|
|
|
|
static char *undo_filename(int level) {
|
|
|
|
static char undo_path[FL_PATH_MAX] = ""; // Undo path
|
|
|
|
static unsigned int undo_path_len = 0; // length w/o filename
|
|
|
|
|
|
|
|
if (!undo_path_len) {
|
|
|
|
fluid_prefs.getUserdataPath(undo_path, sizeof(undo_path));
|
2021-08-30 23:00:59 +03:00
|
|
|
undo_path_len = (unsigned int)strlen(undo_path);
|
2019-08-29 18:17:37 +03:00
|
|
|
}
|
2005-03-20 16:38:04 +03:00
|
|
|
|
2019-08-29 18:17:37 +03:00
|
|
|
// append filename: "undo_PID_LEVEL.fl"
|
|
|
|
snprintf(undo_path + undo_path_len,
|
2020-07-01 19:03:10 +03:00
|
|
|
sizeof(undo_path) - undo_path_len - 1,
|
|
|
|
"undo_%d_%d.fl", getpid(), level);
|
2019-08-29 18:17:37 +03:00
|
|
|
return undo_path;
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Redo menu callback
|
|
|
|
void redo_cb(Fl_Widget *, void *) {
|
2020-05-24 19:02:24 +03:00
|
|
|
int undo_item = main_menubar->find_index(undo_cb);
|
2020-05-24 23:24:48 +03:00
|
|
|
int redo_item = main_menubar->find_index(redo_cb);
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
if (undo_current >= undo_last) return;
|
|
|
|
|
|
|
|
undo_suspend();
|
2022-12-01 03:00:12 +03:00
|
|
|
if (widget_browser) widget_browser->save_scroll_position();
|
|
|
|
int reload_panel = (the_panel && the_panel->visible());
|
2019-08-29 18:17:37 +03:00
|
|
|
if (!read_file(undo_filename(undo_current + 1), 0)) {
|
2005-03-20 16:38:04 +03:00
|
|
|
// Unable to read checkpoint file, don't redo...
|
2021-12-17 20:34:51 +03:00
|
|
|
widget_browser->rebuild();
|
2005-03-20 16:38:04 +03:00
|
|
|
undo_resume();
|
|
|
|
return;
|
|
|
|
}
|
2022-12-01 03:00:12 +03:00
|
|
|
if (reload_panel) {
|
|
|
|
for (Fl_Type *t = Fl_Type::first; t; t=t->next) {
|
|
|
|
if (t->is_widget() && t->selected)
|
|
|
|
t->open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (widget_browser) widget_browser->restore_scroll_position();
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
undo_current ++;
|
|
|
|
|
|
|
|
// Update modified flag...
|
|
|
|
set_modflag(undo_current != undo_save);
|
2021-12-17 20:34:51 +03:00
|
|
|
widget_browser->rebuild();
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
// Update undo/redo menu items...
|
2020-05-24 23:24:48 +03:00
|
|
|
if (undo_current >= undo_last) Main_Menu[redo_item].deactivate();
|
2020-05-24 19:02:24 +03:00
|
|
|
Main_Menu[undo_item].activate();
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Undo menu callback
|
|
|
|
void undo_cb(Fl_Widget *, void *) {
|
2020-05-24 19:02:24 +03:00
|
|
|
int undo_item = main_menubar->find_index(undo_cb);
|
|
|
|
int redo_item = main_menubar->find_index(redo_cb);
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
if (undo_current <= 0) return;
|
|
|
|
|
|
|
|
if (undo_current == undo_last) {
|
2019-08-29 18:17:37 +03:00
|
|
|
write_file(undo_filename(undo_current));
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
undo_suspend();
|
2021-12-11 21:43:00 +03:00
|
|
|
// Undo first deletes all widgets which resets the widget_tree browser.
|
2021-12-17 20:34:51 +03:00
|
|
|
// Save the current scroll position, so we don't scroll back to 0 at undo.
|
|
|
|
if (widget_browser) widget_browser->save_scroll_position();
|
2022-12-01 03:00:12 +03:00
|
|
|
int reload_panel = (the_panel && the_panel->visible());
|
2019-08-29 18:17:37 +03:00
|
|
|
if (!read_file(undo_filename(undo_current - 1), 0)) {
|
2005-03-20 16:38:04 +03:00
|
|
|
// Unable to read checkpoint file, don't undo...
|
2021-12-17 20:34:51 +03:00
|
|
|
widget_browser->rebuild();
|
2005-03-20 16:38:04 +03:00
|
|
|
undo_resume();
|
|
|
|
return;
|
|
|
|
}
|
2022-12-01 03:00:12 +03:00
|
|
|
if (reload_panel) {
|
|
|
|
for (Fl_Type *t = Fl_Type::first; t; t=t->next) {
|
|
|
|
if (t->is_widget() && t->selected)
|
|
|
|
t->open();
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 21:43:00 +03:00
|
|
|
// Restore old browser position.
|
|
|
|
// Ideally, we would save the browser position insied the undo file.
|
2021-12-17 20:34:51 +03:00
|
|
|
if (widget_browser) widget_browser->restore_scroll_position();
|
2005-03-20 16:38:04 +03:00
|
|
|
|
|
|
|
undo_current --;
|
|
|
|
|
|
|
|
// Update modified flag...
|
|
|
|
set_modflag(undo_current != undo_save);
|
|
|
|
|
|
|
|
// Update undo/redo menu items...
|
2020-05-24 19:02:24 +03:00
|
|
|
if (undo_current <= 0) Main_Menu[undo_item].deactivate();
|
|
|
|
Main_Menu[redo_item].activate();
|
2021-12-17 20:34:51 +03:00
|
|
|
widget_browser->rebuild();
|
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() {
|
2020-05-24 19:02:24 +03:00
|
|
|
int undo_item = main_menubar->find_index(undo_cb);
|
|
|
|
int redo_item = main_menubar->find_index(redo_cb);
|
2019-08-29 18:17:37 +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...
|
2019-08-29 18:17:37 +03:00
|
|
|
const char *filename = undo_filename(undo_current);
|
|
|
|
if (!write_file(filename)) {
|
2005-03-20 16:38:04 +03:00
|
|
|
// 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...
|
2020-05-24 19:02:24 +03:00
|
|
|
Main_Menu[undo_item].activate();
|
|
|
|
Main_Menu[redo_item].deactivate();
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear undo buffer
|
|
|
|
void undo_clear() {
|
2020-05-24 23:24:48 +03:00
|
|
|
int undo_item = main_menubar->find_index(undo_cb);
|
|
|
|
int redo_item = main_menubar->find_index(redo_cb);
|
2005-03-20 16:38:04 +03:00
|
|
|
// Remove old checkpoint files...
|
|
|
|
for (int i = 0; i <= undo_max; i ++) {
|
2019-08-29 18:17:37 +03:00
|
|
|
fl_unlink(undo_filename(i));
|
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;
|
2020-05-24 23:24:48 +03:00
|
|
|
|
|
|
|
// Disable the Undo and Redo menu items...
|
|
|
|
Main_Menu[undo_item].deactivate();
|
|
|
|
Main_Menu[redo_item].deactivate();
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resume undo checkpoints
|
|
|
|
void undo_resume() {
|
2022-12-10 15:11:49 +03:00
|
|
|
undo_paused--;
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Suspend undo checkpoints
|
|
|
|
void undo_suspend() {
|
2022-12-10 15:11:49 +03:00
|
|
|
undo_paused++;
|
2005-03-20 16:38:04 +03:00
|
|
|
}
|