Solved

Copy,cut,paste,undo,redo in C++

Posted on 2009-04-07
4
2,127 Views
Last Modified: 2013-11-17
I am trying to create an application like Notepad. Where can I find functions for copy,cut,paste? Also , there will be more than one Edit objects, so , how can i make the undo function to undo the last general action? (I cannot use Edit1->Undo() because there are multiple Edit objects)

Thanks
0
Comment
Question by:Chrysaor
  • 3
4 Comments
 
LVL 4

Expert Comment

by:KrisTheComputerGuy
ID: 24092482
I found a tutorial on how to use the Command software design pattern to support multiple undos.

http://kapo-cpp.blogspot.com/2007/10/multilevel-undo-with-command-pattern.html

Design patterns can be tricky at first. But their pretty cool stuff.
0
 
LVL 4

Accepted Solution

by:
KrisTheComputerGuy earned 250 total points
ID: 24092762
I guess I'm a bit confused on the question. I had assumed your looking for a multi-undo solution where the use can undo and redo multiple times. But rereading the question it seems much simpler now. The previous post has a lot of jargon in it related to UML and advanced Object Oriented Concepts. It'll likely be overkill & overwhelming. Sorry about that.


If however you only need a single undo/redo solution that works for the last text box changed, then I can suggest the following solution.

Cut copy, paste, undo & redo are likely already built into the objects themselves. Check the documentation by pressing F1 while a class is selected in the editor. Borand Builder objects have an event that is called when the object's value is changed. I forget the name of the event.

Do you know how to use pointers? They are useful. For a single undo, all you need is a global pointer in your software that remembers (points to) the last object which had its value changed. Have the events update the global pointer, assigning the .this pointer to the global.  Then have the undo button simply do a call on that pointer. Don't forget to set you initialize your pointer to NULL (and to check for null in the undo/redo) to prevent catastrophies.

For redo you can simply setup another pointer that gets assigned the value of the undo pointer when undo is selected by the user. Setting the undo to NULL.



Also: Check out this tutorial for some great info on using borland C++ builder
http://www.yevol.com/en/bcb/
http://www.yevol.com/en/bcb/Lesson25.htm   (memos & rich edit boxes)
0
 
LVL 4

Expert Comment

by:KrisTheComputerGuy
ID: 24092796
You can find a tutorial on pointers at:

http://www.cplusplus.com/doc/tutorial/pointers/
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 24094875
Hmmm. Don't think taht pointers can solve the issue without a general design. For a single undo or redo the user can take the functionality of the edit control anyhow (CTRL+Z or CTRL+Y). But if you want to go back multiple steps done in different edit controls, check-boxes, comboboxes, you rarely can do that by switching a few pointers.

You better make a new class/struct - e. g. named Operation - which has the following members:

   - ID of the control which was changed
   - contents before change
   - contents after change

and a container - e. g. a std::vector<Operation> - where you can store new objects of that class together with a 'current' index which always points to that slot of the container which was current.

Then for any major update of the user - you create a new Operation which protocols that update and add it to the container.

In case of an 'undo', you read the last operation from container and make it reverse, i. e. put the previous contents back to the control and decrement the 'current'. In case of a redo go forward (if any entries were available) and increment the current. If there are new updates while the current was not last you delete all forward operations and make the new update the last.

To detect 'major updates' you could handle teh WM_SET_FOCUS message when the user leaves one control and goes to the next. Or, if you want to track partial changes in a big edit control, you have to log all single key-strokes or updates of an edit control and the operation structure needs to be adapted to taht.
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now