-
October 11th, 2012, 10:31 AM
#1
How To Handle An Open Document
First off I am still a noob so be nice
I have an MFC program created from the app wizard. It is an MDI program, reading/writing text files using Serialize. I can read the document and know that the entire document was read into my buffer without any errors. This was verified by compaing the number of bytes read with the file length.
How do I get the document to display in my main/child window?
How do I read the document from the window so I can save it back to the file?
Sample lines of code would be great!!
Thanks in advance for answering simple questions from a confused old fart
John
-
October 11th, 2012, 10:46 AM
#2
Re: How To Handle An Open Document
- You display the document data neither in main nor in child window. You display it in one or more Views. And it is up to you to choose the types of the Views to display the document data.
- If some data displayed in a View was changed then you can use CView::GetDocument method to access the document and change the corresponding data in it.
- Save document data is just opposed to get it. If you use some type or ReadFile to read - then use corresponding WriteFile to write/save.
If you use CArchive class to read data (using operator <<()) then use operator >>() to save... - Document/View Architecture
Victor Nijegorodov
-
October 11th, 2012, 11:13 AM
#3
Re: How To Handle An Open Document
Hi Victor,
Thanks for the information and the link.
Since the document is not showing up in my window, what must I do to display it after reading it into memory? I have seen commands like UpdateAll() or UpdateView() (I think) but what must I do once the document is in memory. My window just remains blank.
Thanks again!
John
-
October 11th, 2012, 11:23 AM
#4
Re: How To Handle An Open Document
It is usually UpdateAllViews. Then in each View you have to implement (override) OnUpdate method that will be called in respond on the UpdateAllViews.
In OnUpdate implemetation you put some code to update current View using document data (via GetDocument method).
Have a look at the Scribble Sample: MDI Drawing Application and MDIDOCVW Sample: Demonstrates MDI Using Doc/View Architecture
Victor Nijegorodov
-
October 11th, 2012, 11:56 AM
#5
Re: How To Handle An Open Document
If your view is derived from CView, you'll need to handle all the text rendering yourself, which would be a pain. A better idea would be to derive your view from CEditView and it should do everything for you.
-
October 11th, 2012, 12:49 PM
#6
Re: How To Handle An Open Document
I am using the CEditView for my view class.
Where do I place the UpdateAllViews() code? Is it right after I open and read my file? Here is my code where I read the file:
Code:
void CFoo::ReadFileData(CArchive& ar)
{
BSTR unicodestr;
char* ansistr = new char[0x100];
CString sTempNew = STR_EMPTY;
CString sTempOld = STR_EMPTY;
DWORD nBytesRead = 0;
UINT nr;
WCHAR pb[0x100];
CParseDataSubRoutines pParseDataSubRoutines;
theApp.DoWaitCursor(TRUE);
while (nBytesRead < dwFileLength) {
// Read the data from the UNICODE file
nr = ar.Read( pb, 0x100 );
unicodestr = (LPWSTR ) pb;
// Convert the data from UNICODE to ANSI. Use nr/2 because of the UNICODE
WideCharToMultiByte(CP_ACP, 0, unicodestr, nr/2, ansistr, nr/2, NULL, NULL);
// Now move it to string format
sTempNew = ansistr;
// Add the new data to the old data
sTempNew = sTempOld + sTempNew.Left(nr/2);
// Push the data back into the window
sTempOld = sTempNew;
// Show the bytes read
nBytesRead+= nr;
}
// Strip off the first byte, part of the file ID (0xff) I think...
sTempNew = sTempNew.Mid(1,sTempNew.GetLength());
// Parse the long string of ANSI data into individual strings
pParseDataSubRoutines.ParseDataIntoIndividualLines(sTempNew);
delete[] ansistr;
theApp.DoWaitCursor(FALSE);
}
Before I added the read routine, I was able to see my data in the window, line by line, after the file was read be the default MFC read/Serialize reader routine. Maybe I am not reading the file correctly or maybe some how I need to change a default pointer to where my data is stored in memory?
John
-
October 11th, 2012, 12:55 PM
#7
Re: How To Handle An Open Document
UpdateAllViews is used by the document to tell the view that something has changed and gives the view a chance to do whatever it needs to. I've never used the CEditView and you seem to be writing code outside of the Doc/View architecture, so just given that snipped, I can't tell you what's wrong. I can tell you it's best to let MFC do as much of the work as possible.
Last edited by GCDEF; October 11th, 2012 at 01:16 PM.
-
October 11th, 2012, 12:59 PM
#8
Re: How To Handle An Open Document
You seem to read your file in some local variables/objects that go out of scope after your ReadFileData method exits.
Besides, there is no need to:
- create char* ansistr array in the heap, using is easier and better
- use CWinApp::DoWaitCursor(TRUE)/CWinApp::DoWaitCursor(FALSE); use instead
Victor Nijegorodov
-
October 11th, 2012, 01:12 PM
#9
Re: How To Handle An Open Document
The code above should have showed CFooDoc::ReadFileData(void). It does reside in my Doc class.
I understand about the Wait cursor, thanks.
So, let's say I just delete the read file function all together. Now when I open a file it would be handled by the default, built in code. From what I am reading, if I use a pDoc->GetDocument() that will pull my file data into a variable which would allow me to manipulate it? Then how would I push it back to the Window as an updated document with changes that were made by other functions?
I know I must be making this harder than it needs to be. I just need to:
1. Open a file and read it into memory. That works by default.
2. How do I access that data directly so I can manipulate it?
3. How do I push those updates back to the screen so I can see them?
4. Then I would use GetDocument() to retrieve the data so it can be Serialized back to the disc.
Coding is a hobby. I learned assembly language as my first language on a 24-bit mainframe (SEL 840) that ran on a Flight Simulator. Then I progressed to a 16-bit Texas Instruments 980 computer, and eventually was able to exchange data between the computers. That seemed easy compared to what I am now trying to teach myself. I learn fast when I have an example that works. I have books, but just can't seem to find the right answer to getting this to work. Been at it for a few weeks now and have learned a lot, just not what I need
A specific line or two of code would be very beneficial.
Thanks for the help.
John
-
October 11th, 2012, 01:16 PM
#10
Re: How To Handle An Open Document
I just used the wizard to create a new app with CEditView as the base class. I wrote no code at all. I compiled it, ran it and opened a text file. It displayed it properly. Again, I didn't write a single line of code. The framework did it all.
-
October 11th, 2012, 01:31 PM
#11
Re: How To Handle An Open Document
Yes, I agree with the wizard and that it works. No arguments. But I still don't understand how to manipulate the data.
I have a parse routine created and works on test data. So when I open a file and read it (using the default wizard code), I need to be able to manually manipulate that code. I want to parse it, put it in say 10 different variables, make changes to it and send it back to the screen (all through functions and not direct interface with the users). The idea is I am reading the data, validating it and if it is incorrect, I am making the change, after it has been read, and then updated on the screen to show the change.
I can parse the data using a test file. It works.
Do I use GetDocument() to pull the data down from the window so I can then parse/process it manually? If so, how do I execute that comand? Is it something like:
Code:
CDocument* myDoc = pDoc->GetDocument();
Then does my data reside in myDoc and I can parse it?
Once my data is parsed, how do Ipush myDoc back into the window so it shows the updated value?
Thanks for your patience
John
-
October 11th, 2012, 01:47 PM
#12
Re: How To Handle An Open Document
Again, I haven't worked with the CEditView, but looking at the documentation, it looks like you'd call GetEditCtrl from the view class to get its edit control, then use the various CEdit or CWnd functions to get and manipulate the text. GetWindowText() would get you all the text in the edit control and put it in a CString. You could do what you want with it from there, then use SetWindowText() to update the edit control with the new text.
It appears in this case, the CEdit control actually maintains the text and the doc is just used to transfer it between the window and the file.
-
October 11th, 2012, 01:54 PM
#13
Re: How To Handle An Open Document
Ah ha, light bulb came on. Will give that a shot. Seems logical and jives with some other stuff I have been reading and trying.
THANKS!
John
-
October 11th, 2012, 01:55 PM
#14
Re: How To Handle An Open Document
Originally Posted by JohnWHagen
Do I use GetDocument() to pull the data down from the window so I can then parse/process it manually? If so, how do I execute that comand? Is it something like:
Code:
CDocument* myDoc = pDoc->GetDocument();
GetDocument() is a method of CView class, not a CDocument one.
It is used to get/set data from/to Document instance to/from View instance.
Document instance itself should not ever try to directly access any View instance.
Victor Nijegorodov
-
October 11th, 2012, 01:57 PM
#15
Re: How To Handle An Open Document
Originally Posted by JohnWHagen
Ah ha, light bulb came on. Will give that a shot. Seems logical and jives with some other stuff I have been reading and trying.
THANKS!
John
Hope it helps, but keep in mind, that's not usually the way Doc/View works. Typically the document does maintain the data and the view renders it. This seems to deviate from the norm somewhat.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|