fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

undo.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // FLUID undo support for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl.H>
20 #include "Fl_Type.h"
21 #include "undo.h"
22 #include <FL/Fl_Preferences.H>
23 #include <FL/filename.H>
24 #include "../src/flstring.h"
25 #if defined(WIN32) && !defined(__CYGWIN__)
26 # include <io.h>
27 # include <windows.h>
28 # define getpid (int)GetCurrentProcessId
29 # ifndef __WATCOMC__
30 // Visual C++ 2005 incorrectly displays a warning about the use of POSIX APIs
31 // on Windows, which is supposed to be POSIX compliant...
32 # define unlink _unlink
33 # endif // !__WATCOMC__
34 #else
35 # include <unistd.h>
36 #endif // WIN32 && !__CYGWIN__
37 
38 
39 extern Fl_Preferences fluid_prefs; // FLUID preferences
40 extern Fl_Menu_Item Main_Menu[]; // Main menu
41 
42 #define UNDO_ITEM 25 // Undo menu item index
43 #define REDO_ITEM 26 // Redo menu item index
44 
45 
46 //
47 // This file implements an undo system using temporary files; ideally
48 // we'd like to do this in memory, however the current data structures
49 // and design aren't well-suited... Instead, we save and restore
50 // checkpoint files.
51 //
52 
53 
54 int undo_current = 0; // Current undo level in buffer
55 int undo_last = 0; // Last undo level in buffer
56 int undo_max = 0; // Maximum undo level used
57 int undo_save = -1; // Last undo level that was saved
58 static int undo_paused = 0; // Undo checkpointing paused?
59 
60 
61 // Return the undo filename
62 static char *undo_filename(int level, char *buf, int bufsize) {
63  static char undo_path[FL_PATH_MAX] = ""; // Undo path
64 
65 
66  if (!undo_path[0]) fluid_prefs.getUserdataPath(undo_path, sizeof(undo_path));
67 
68  snprintf(buf, bufsize, "%sundo_%d_%d.fl", undo_path, getpid(), level);
69  return buf;
70 }
71 
72 
73 // Redo menu callback
74 void redo_cb(Fl_Widget *, void *) {
75  char filename[FL_PATH_MAX]; // Undo checkpoint file
76 
77  if (undo_current >= undo_last) return;
78 
79  undo_suspend();
80  if (!read_file(undo_filename(undo_current + 1, filename, sizeof(filename)), 0)) {
81  // Unable to read checkpoint file, don't redo...
82  undo_resume();
83  return;
84  }
85 
86  undo_current ++;
87 
88  // Update modified flag...
90 
91  // Update undo/redo menu items...
94 }
95 
96 // Undo menu callback
97 void undo_cb(Fl_Widget *, void *) {
98  char filename[FL_PATH_MAX]; // Undo checkpoint file
99 
100  if (undo_current <= 0) return;
101 
102  if (undo_current == undo_last) {
104  }
105 
106  undo_suspend();
107  if (!read_file(undo_filename(undo_current - 1, filename, sizeof(filename)), 0)) {
108  // Unable to read checkpoint file, don't undo...
109  undo_resume();
110  return;
111  }
112 
113  undo_current --;
114 
115  // Update modified flag...
117 
118  // Update undo/redo menu items...
121  undo_resume();
122 }
123 
124 // Save current file to undo buffer
126  char filename[FL_PATH_MAX]; // Undo checkpoint filename
127 
128 // printf("undo_checkpoint(): undo_current=%d, undo_paused=%d, modflag=%d\n",
129 // undo_current, undo_paused, modflag);
130 
131  // Don't checkpoint if undo_suspend() has been called...
132  if (undo_paused) return;
133 
134  // Save the current UI to a checkpoint file...
136  // Don't attempt to do undo stuff if we can't write a checkpoint file...
137  perror(filename);
138  return;
139  }
140 
141  // Update the saved level...
142  if (modflag && undo_current <= undo_save) undo_save = -1;
143  else if (!modflag) undo_save = undo_current;
144 
145  // Update the current undo level...
146  undo_current ++;
149 
150  // Enable the Undo and disable the Redo menu items...
153 }
154 
155 // Clear undo buffer
156 void undo_clear() {
157  char filename[FL_PATH_MAX]; // Undo checkpoint filename
158 
159 
160  // Remove old checkpoint files...
161  for (int i = 0; i <= undo_max; i ++) {
162  unlink(undo_filename(i, filename, sizeof(filename)));
163  }
164 
165  // Reset current, last, and save indices...
167  if (modflag) undo_save = -1;
168  else undo_save = 0;
169 }
170 
171 // Resume undo checkpoints
172 void undo_resume() {
173  undo_paused = 0;
174 }
175 
176 // Suspend undo checkpoints
177 void undo_suspend() {
178  undo_paused = 1;
179 }
180 
181 
182 //
183 // End of "$Id$".
184 //
REDO_ITEM
#define REDO_ITEM
Definition: undo.cxx:43
Fl.H
buf
static char * buf
Definition: fl_encoding_mac_roman.cxx:76
undo_resume
void undo_resume()
Definition: undo.cxx:172
Fl_Menu_Item::activate
void activate()
Definition: Fl_Menu_Item.H:343
fluid_prefs
Fl_Preferences fluid_prefs
undo_cb
void undo_cb(Fl_Widget *, void *)
Definition: undo.cxx:97
undo_save
int undo_save
Definition: undo.cxx:57
read_file
int read_file(const char *filename, int merge)
Definition: file.cxx:493
filename.H
Fl_Preferences::getUserdataPath
char getUserdataPath(char *path, int pathlen)
Creates a path that is related to the preferences file and that is usable for additional application ...
Definition: Fl_Preferences.cxx:885
modflag
int modflag
Definition: fluid.cxx:122
snprintf
#define snprintf
Definition: flstring.h:64
Fl_Type.h
redo_cb
void redo_cb(Fl_Widget *, void *)
Definition: undo.cxx:74
undo_suspend
void undo_suspend()
Definition: undo.cxx:177
undo_paused
static int undo_paused
Definition: undo.cxx:58
undo_checkpoint
void undo_checkpoint()
Definition: undo.cxx:125
FL_PATH_MAX
#define FL_PATH_MAX
Definition: filename.H:38
undo.h
filename
static const char * filename
Definition: fluid.cxx:119
Fl_Menu_Item::deactivate
void deactivate()
Definition: Fl_Menu_Item.H:348
Fl_Widget
Definition: Fl_Widget.H:101
UNDO_ITEM
#define UNDO_ITEM
Definition: undo.cxx:42
Main_Menu
Fl_Menu_Item Main_Menu[]
Definition: fluid.cxx:1058
undo_max
int undo_max
Definition: undo.cxx:56
undo_clear
void undo_clear()
Definition: undo.cxx:156
set_modflag
void set_modflag(int mf)
Definition: fluid.cxx:1687
write_file
int write_file(const char *filename, int selected_only)
Definition: file.cxx:312
Fl_Menu_Item
Definition: Fl_Menu_Item.H:112
Fl_Preferences
Fl_Preferences provides methods to store user settings between application starts.
Definition: Fl_Preferences.H:60
undo_last
int undo_last
Definition: undo.cxx:55
undo_current
int undo_current
Definition: undo.cxx:54
Fl_Preferences.H
undo_filename
static char * undo_filename(int level, char *buf, int bufsize)
Definition: undo.cxx:62