Solved

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

Posted on 2009-04-07
4
2,140 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

785 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