?
Solved

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

Posted on 2009-04-07
4
Medium Priority
?
2,208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 1000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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