CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Oct 2012
    Posts
    15

    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

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How To Handle An Open Document

    1. 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.
    2. 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.
    3. 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...
    4. Document/View Architecture
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2012
    Posts
    15

    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

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  6. #6
    Join Date
    Oct 2012
    Posts
    15

    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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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
      Code:
      char ansistr[100];
      is easier and better
    • use CWinApp::DoWaitCursor(TRUE)/CWinApp::DoWaitCursor(FALSE); use
      Code:
      CWaitCursor cw;
      instead
    Victor Nijegorodov

  9. #9
    Join Date
    Oct 2012
    Posts
    15

    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  11. #11
    Join Date
    Oct 2012
    Posts
    15

    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

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  13. #13
    Join Date
    Oct 2012
    Posts
    15

    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

  14. #14
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: How To Handle An Open Document

    Quote Originally Posted by JohnWHagen View Post
    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

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How To Handle An Open Document

    Quote Originally Posted by JohnWHagen View Post
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured