Skip to content
Snippets Groups Projects
undolist.rb 11.2 KiB
Newer Older
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
#
# This is a "pure Ruby" implementation of the FXUndoList and
# FXCommand classes from the standard FOX distribution. Since those
# classes are independent of the rest of FOX this is a simpler (and probably
# more efficient) approach than trying to wrap the original C++ classes.
# 
# Notes (by Jeroen, lifted from FXUndoList.cpp):
# 
# * When a command is undone, it's moved to the redo list.
# * When a command is redone, it's 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 <em>mark</em> 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 FXUndoList#undo, then
#   the mark can be reached by calling FXUndoList#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!

require 'fox16/responder'

module Fox

  #
  # The undo list manages a list of undoable (and redoable) commands for a FOX
  # application; it works hand-in-hand with subclasses of FXCommand and is
  # an application of the well-known <em>Command</em> pattern. Your application
  # code should implement any number of command classes and then add then to an
  # FXUndoList instance. For an example of how this works, see the textedit
  # example program from the FXRuby distribution.
  # 
  # == Class Constants
  # 
  # [FXUndoList::ID_UNDO]	Message identifier for the undo method.
  #				When a +SEL_COMMAND+ message with this identifier
  #				is sent to an undo list, it undoes the last command.
  #				FXUndoList also provides a +SEL_UPDATE+ handler for this
  #				identifier, that enables or disables the sender
  #				depending on whether it's possible to undo.
  #
  # [FXUndoList::ID\_UNDO\_ALL]	Message identifier for the "undo all" method. FXUndoList handles both
  #				the +SEL_COMMAND+ and +SEL_UPDATE+ messages for this message
  #				identifier.
  #
  # [FXUndoList::ID_REDO]	Message identifier for the redo method. When a +SEL_COMMAND+ message
  #				with this identifier is sent to an undo list, it redoes the last command.
  #				FXUndoList also provides a +SEL_UPDATE+ handler for this identifier,
  #				that enables or disables the sender depending on whether it's possible to
  #				redo.
  #
  # [FXUndoList::ID\_REDO\_ALL]	Message identifier for the "redo all" method. FXUndoList handles both
  #				the +SEL_COMMAND+ and +SEL_UPDATE+ messages for this message
  #				identifier.
  #
  # [FXUndoList::ID_CLEAR]	Message identifier for the "clear" method. FXUndoList handles both
  #				the +SEL_COMMAND+ and +SEL_UPDATE+ messages for this message
  #				identifier.
  #
  # [FXUndoList::ID_REVERT]	Message identifier for the "revert" method. FXUndoList handles both
  #				the +SEL_COMMAND+ and +SEL_UPDATE+ messages for this message
  #				identifier.
  #
  class FXUndoList < FXObject

    include Responder

    ID_CLEAR,
    ID_REVERT,
    ID_UNDO,
    ID_REDO,
    ID_UNDO_ALL,
    ID_REDO_ALL,
    ID_LAST = enum(0, 7)

    #
    # Returns an initialized FXUndoList instance.
    #
    def initialize
      # Be sure to call base class initialize
      super

      # Set up the message map for this instance
      FXMAPFUNC(SEL_COMMAND, ID_CLEAR,    "onCmdClear")
      FXMAPFUNC(SEL_UPDATE,  ID_CLEAR,    "onUpdClear")
      FXMAPFUNC(SEL_COMMAND, ID_REVERT,   "onCmdRevert")
      FXMAPFUNC(SEL_UPDATE,  ID_REVERT,   "onUpdRevert")
      FXMAPFUNC(SEL_COMMAND, ID_UNDO,     "onCmdUndo")
      FXMAPFUNC(SEL_UPDATE,  ID_UNDO,     "onUpdUndo")
      FXMAPFUNC(SEL_COMMAND, ID_REDO,     "onCmdRedo")
      FXMAPFUNC(SEL_UPDATE,  ID_REDO,     "onUpdRedo")
      FXMAPFUNC(SEL_COMMAND, ID_UNDO_ALL, "onCmdUndoAll")
      FXMAPFUNC(SEL_UPDATE,  ID_UNDO_ALL, "onUpdUndo")
      FXMAPFUNC(SEL_COMMAND, ID_REDO_ALL, "onCmdRedoAll")
      FXMAPFUNC(SEL_UPDATE,  ID_REDO_ALL, "onUpdRedo")

      # Instance variables
      @undolist = []
      @redolist = []
      @marker = nil
      @size = 0
    end

    #
    # Cut the redo list
    #
    def cut
      @redolist.clear
      unless @marker.nil?
        @marker = nil if @marker < 0
      end
    end

    #
    # Add new _command_ (an FXCommand instance) to the list.
    # If _doit_ is +true+, the command is also executed.
    #
    def add(command, doit=false)
      # Cut redo list
      cut

      # No command given?
      return true if command.nil?

      # Add it to the end of the undo list
      @undolist.push(command)

      # Execute it right now?
      command.redo if doit

      # Update size
      @size += command.size	# measured after redo

      # Update the mark distance
      @marker = @marker + 1 unless @marker.nil?

      # Done
      return true
    end

    #
    # Undo last command.
    #
    def undo
      unless @undolist.empty?
	command = @undolist.pop
	@size -= command.size
	command.undo
	@redolist.push(command)
	@marker = @marker - 1 unless @marker.nil?
	return true
      end
      return false
    end

    #
    # Redo next command
    #
    def redo
      unless @redolist.empty?
	command = @redolist.pop
	command.redo
	@undolist.push(command)
	@size += command.size
	@marker = @marker + 1 unless @marker.nil?
	return true
      end
      return false
    end

    #
    # Undo all commands
    #
    def undoAll
      undo while canUndo?
    end

    #
    # Redo all commands
    #
    def redoAll
      redo while canRedo?
    end

    #
    # Revert to marked
    #
    def revert
      unless @marker.nil?
	undo while (@marker > 0)
	redo while (@marker < 0)
	return true
      end
      return false
    end

    #
    # Return +true+ if we can still undo some commands
    # (i.e. the undo list is not empty).
    #
    def canUndo?
      (@undolist.empty? == false)
    end

    #
    # Return +true+ if we can still redo some commands
    # (i.e. the redo list is not empty).
    #
    def canRedo?
      (@redolist.empty? == false)
    end

    #
    # Return +true+ if there is a previously marked
    # state that we can revert to.
    #
    def canRevert?
      (@marker != nil) && (@marker != 0)
    end

    #
    # Returns the current undo command.
    #
    def current
      @undolist.last
    end

    #
    # Return the name of the first available undo command.
    # If no undo command is available, returns +nil+.
    #
    def undoName
      if canUndo?
        current.undoName
      else
        nil
      end
    end
    
    #
    # Return the name of the first available redo command.
    # If no redo command is available, returns +nil+.
    #
    def redoName
      if canRedo?
        @redolist.last.redoName
      else
        nil
      end
    end

    #
    # Returns the number of undo records.
    #
    def undoCount
      @undolist.size
    end

    #
    # Returns the total size of undo information.
    #
    def undoSize
      @size
    end

    #
    # Clear the list
    #
    def clear
      @undolist.clear
      @redolist.clear
      @marker = nil
      @size = 0
    end

    #
    # Trim undo list down to at most _nc_ commands.
    #
    def trimCount(nc)
      if @undolist.size > nc
	numRemoved = @undolist.size - nc
	@undolist[0, numRemoved].each { |command| @size -= command.size }
	@undolist[0, numRemoved] = nil
	@marker = nil if (@marker != nil && @marker > @undolist.size)
      end
    end

    #
    # Trim undo list down to at most _size_.
    #
    def trimSize(sz)
      if @size > sz
	s = 0
	@undolist.reverse.each_index { |i|
          j = @undolist.size - (i + 1)
          s += @undolist[j].size
          @undolist[j] = nil if (s > sz)
	}
	@undolist.compact!
	@marker = nil if (@marker != nil && @marker > @undolist.size)
      end
    end

    #
    # Mark current state
    #
    def mark
      @marker = 0
    end

    #
    # Unmark undo list
    #
    def unmark
      @marker = nil
    end

    #
    # Return +true+ if the undo list is marked.
    #
    def marked?
      @marker == 0
    end

    def onCmdUndo(sender, sel, ptr) # :nodoc:
      undo
      return 1
    end

    def onUpdUndo(sender, sel, ptr) # :nodoc:
      if canUndo?
	sender.handle(self, MKUINT(FXWindow::ID_ENABLE, SEL_COMMAND), nil)
      else
	sender.handle(self, MKUINT(FXWindow::ID_DISABLE, SEL_COMMAND), nil)
      end
      return 1
    end

    def onCmdRedo(sender, sel, ptr) # :nodoc:
      self.redo
      return 1
    end

    def onUpdRedo(sender, sel, ptr) # :nodoc:
      if canRedo?
	sender.handle(self, MKUINT(FXWindow::ID_ENABLE, SEL_COMMAND), nil)
      else
	sender.handle(self, MKUINT(FXWindow::ID_DISABLE, SEL_COMMAND), nil)
      end
      return 1
    end

    def onCmdClear(sender, sel, ptr) # :nodoc:
      clear
      return 1
    end

    def onUpdClear(sender, sel, ptr) # :nodoc:
      if canUndo? || canRedo?
	sender.handle(self, MKUINT(FXWindow::ID_ENABLE, SEL_COMMAND), nil)
      else
	sender.handle(self, MKUINT(FXWindow::ID_DISABLE, SEL_COMMAND), nil)
      end
      return 1
    end

    def onCmdRevert(sender, sel, ptr) # :nodoc:
      revert
      return 1
    end

    def onUpdRevert(sender, sel, ptr) # :nodoc:
      if canRevert?
	sender.handle(self, MKUINT(FXWindow::ID_ENABLE, SEL_COMMAND), nil)
      else
	sender.handle(self, MKUINT(FXWindow::ID_DISABLE, SEL_COMMAND), nil)
      end
      return 1
    end

    def onCmdUndoAll(sender, sel, ptr) # :nodoc:
      undoAll
      return 1
    end

    def onCmdRedoAll(sender, sel, ptr) # :nodoc:
      redoAll
      return 1
    end
  end

  #
  # FXCommand is an "abstract" base class for your application's commands. At a
  # minimum, your concrete subclasses of FXCommand should implement the
  # #undo, #redo, #undoName, and #redoName methods.
  #
  class FXCommand
    #
    # Undo this command; this should save enough information for a
    # subsequent redo.
    #
    def undo
      raise NotImpError
    end

    #
    # Redo this command; this should save enough information for a
    # subsequent undo.
    #
    def redo
      raise NotImpError
    end

    #
    # Name of the undo command to be shown on a button or menu command;
    # for example, "Undo Delete".
    #
    def undoName
      raise NotImpError
    end

    #
    # Name of the redo command to be shown on a button or menu command;
    # for example, "Redo Delete".
    #
    def redoName
      raise NotImpError
    end

    #
    # Returns the size of the information in the undo record, i.e. the
    # number of bytes required to store it in memory. This is only used
    # by the FXUndoList#trimSize method, which can be called to reduce
    # the memory use of the undo list to a certain size.
    #
    def size
      0
    end
  end
end