CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    Switch from txt to rtf during runtime

    While running a Doc/View SDI, is there any way to switch from text mode to rtf mode during runtime? Search reveals nothing.

    Say I have an editor and I want the app to use text, I can set the ctor
    as follows.
    Code:
    CEditorDoc::CEditorDoc()
    {
    	// TODO: add one-time construction code here
    	m_bRTF = FALSE;
    }
    But once I've done that and compiled the app, while it's running, is there a way for the user (or programmer) to change the mode back to RTF? One solution that occurred to me is to use 2 document classes, but that's a hassle.

    Thanks.
    mpliam

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

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by Mike Pliam View Post
    ... One solution that occurred to me is to use 2 document classes.
    Yes!
    Quote Originally Posted by Mike Pliam View Post
    ... but that's a hassle.
    Why???
    Just move from SDI to MDI with two document classes/types!
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Switch from txt to rtf during runtime

    Well, my understanding is that "document" in document-view model is not exactly the document saved in some format. Instead, it is just a bunch of data to be interpreted and presented by view. Based on this, I don't see any reason for introducing the second "document".
    Best regards,
    Igor

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

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by Igor Vartanov View Post
    Well, my understanding is that "document" in document-view model is not exactly the document saved in some format. Instead, it is just a bunch of data to be interpreted and presented by view. Based on this, I don't see any reason for introducing the second "document".
    The one reason I see is that some "document" data contained by .rtf file cannot be appropriately saved in a plain .txt file.
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by VictorN View Post
    .rtf file cannot be appropriately saved in a plain .txt file.
    Why not? A richer data always can be transformed to its simpler version.
    Best regards,
    Igor

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by Igor Vartanov View Post
    A richer data always can be transformed to its simpler version.
    Sure it can!
    But not always can it be transformed from a simpler to its richer version!
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by VictorN View Post
    But not always can it be transformed from a simpler to its richer version!
    This is what I was going to ask next. What's the purpose of that switching? In case the original data is RTF-formatted, and the task is just to switch between representations in "view" at runtime, there's no need to take a multi-document approach, as the source data anyway remains unaffected.
    Best regards,
    Igor

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

    Re: Switch from txt to rtf during runtime

    Quote Originally Posted by Igor Vartanov View Post
    This is what I was going to ask next. What's the purpose of that switching? In case the original data is RTF-formatted, and the task is just to switch between representations in "view" at runtime, there's no need to take a multi-document approach, as the source data anyway remains unaffected.
    Agree!
    Victor Nijegorodov

  9. #9
    Join Date
    May 2002
    Posts
    1,798

    Re: Switch from txt to rtf during runtime

    I have built a math/statistical editor. This editor is an intermediate app that sends calculated results via a dll parser to an output application. I would like the USER to be able to use ANY available text editor to write and save scripts to be later processed by my editor. One advantage of using RTF is that images can be embedded in the script that would further enhance the script with demo diagrams, etc. But that means that the editor would have to load and interpret only RTF files. If one attempts to load a TXT file into an RTF doc, the view is blank. And loading an RTF file in a TXT mode doc loads all the RTF codes which is impossible for the script parser (which was built to parse plain text) to interpret. After playing around with this for some time, I have concluded that being able to detect which file type it is and then display it appropriately would be most useful and avoid User confusion. But I admit that I have not thought carefully through this yet.

    It turns out that I have labored over this for a number of years. Here's a post back in 2002 that provides a nice method for converting from TXT <--> RTF. I participated in this thread way back then.

    Painless streaming of long rich text from/to CRichEditView Posted by Paul C Maddox on January 18th, 2002
    http://www.codeguru.com/cpp/controls...chEditView.htm
    Last edited by Mike Pliam; May 5th, 2015 at 07:42 PM.
    mpliam

  10. #10
    Join Date
    May 2002
    Posts
    1,798

    Re: Switch from txt to rtf during runtime

    Here's how to implement loading of either text or rtf files using CRichEditView and CRichEditDoc:

    Set in the document class ctor: m_bRTF = TRUE; (default) to implement rtf serialization

    Override OnOpenDocument.

    Code:
    BOOL CEditorDoc::OnOpenDocument(LPCTSTR lpszPathName)
    {
    	if (!CRichEditDoc::OnOpenDocument(lpszPathName))
    		return FALSE;
    
    	// TODO:  Add your specialized creation code here
    
    	m_csFilepath = lpszPathName;
    	ReadChunk(m_csFilepath);
    
    	return TRUE;
    
    }// OnOpenDocument(LPCTSTR lpszPathName)
    
    // check to see if the first 11 file chars are '\\rtf1\\ans'.
    // if not, assume text, load the file as text, and set the text to the view
    void CEditorDoc::ReadChunk(LPCTSTR lpszFilepath)
    {
    	char buf[11];
    	memset(buf, 0x00, 11);
    	ifstream f;
    	f.open(lpszFilepath, ios::in | ios::binary);
    	if(!f) { TRACE1("Failed to open %s\n", lpszFilepath); return; }
    	else { TRACE1("opened %s .\n", lpszFilepath); }
    
    	f.read((char*) buf, (streamsize) 10);
    
    	f.close();
    
    	char buf2[11];
    	memset(buf2, 0x00, 11);
    	strcpy_s(buf2, 11, "{\\rtf1\\ans");
    
    	_RPT1(0, "buf = : %s\n", buf);
    	_RPT1(0, "buf2 =: %s\n", buf2);
    	int ncmp = strcmp(buf, buf2);
    	_RPT1(0, "\nncmp = %d\n", ncmp);
    
    	if(ncmp == 0) 
    	{
    		 _RPT0(0, "Its an RTF file.\n");
    		 CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
    		 pMain->m_nTextMode = CMainFrame::nRTF;
    		 // update the status bar
    		 pMain->UpdateStatusBarTextMode(CMainFrame::nRTF);
    
    	}
    	else
    	{
    		_RPT0(0, "It's probably a text file.\n"); 
    		 CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
    		 pMain->m_nTextMode = CMainFrame::nTXT;
    		 // update the status bar
    		 pMain->UpdateStatusBarTextMode(CMainFrame::nTXT);
    
    	}
    
    	CEditorView * pView = (CEditorView*)GetView();
    
    	if(ncmp != 0)  // file is text so make it rtf to display
    	{
    		FILE * pFile;
    		long lSize;
    		char * buffer;
    		size_t result;
    
    		wstring ws = lpszFilepath;
    		string s(ws.begin(), ws.end());
    		pFile = fopen ( s.c_str(), "rb" );
    		if (pFile == NULL) { TRACE0("File error\n"); return; }
    
    		// obtain file size:
    		fseek (pFile , 0 , SEEK_END);
    		lSize = ftell (pFile);
    		rewind (pFile);
    
    		// allocate memory to contain the whole file:
    		//wbuffer = (wchar_t*) malloc (sizeof(wchar_t)*lSize+1);
    		buffer = new char [ lSize+1 ];
    		if (buffer == NULL) { TRACE0("Memory error\n"); return; }
    		memset(buffer, 0x00, lSize+1);
    
    		// copy the file into the buffer:
    		result = fread (buffer, 1, lSize, pFile);
    		if (result != lSize) { TRACE0("Reading error\n"); return; }
    	
    		// the whole file is now loaded in the memory buffer.
    		_RPT1(0, "buffer =: %s\n", buffer);
    
    		// terminate
    		fclose (pFile);
    		
    		string ss = string(buffer);
    		wstring wss(ss.begin(), ss.end());
    		pView->SetWindowTextW(wss.c_str());
    		delete [] buffer;
    		buffer = NULL;
    	}
    
    }// ReadChunk(LPCTSTR lpszFilepath)
    I've also added an StatusBar updater to indicate whether or not the loaded file was txt or rtf.
    Saving the file as rtf or text can be a File menu item.
    mpliam

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