Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

840 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