rtf streaming and the clipboard


this article was contributed by gary kuehn

i had a need to copy multiple views derived from cricheditview to the clipboard. the sample code below contains an rtf streamout function with its associated callback function and a code snippet for copying multiple views into a single cf_rtf clipboard image.

the basic premise was to programmatically cycle through all the pertinent cricheditviews in order to create a composite rtf report that could be placed on the clipboard with a single click of the mouse. if your application contains many read-only type views from database queries or numerical calculations this method will provide a nice snapshot of the resultant data set.

i created a user defined id_stream_views message that is handled by the cuimainwindow::oneditbothpanes method. menu and toolbar button control associated with the id_stream_views can be found in the cuimainwindow::onupdateeditbothpanes ui handler. (note: cuimainwindow is derived from cframewnd)

the oneditbothpanes copies both views from my static splitter window onto the clipboard. the idea is simple, walk through each view and stream out the data from the underlying cricheditctrl. the oneditbothpanes function is only accessible if both panes are derived from a cricheditview. (see onupdateeditbothpanes). this restriction allows me to cycle through each replaceable view and not have to worry if it can support a streamread method. the area which still needs work is the implied rtf reader. the function as it stands employs a quick hack on the rtf stream.


void cuimainwindow::oneditbothpanes() 
{
  // get rtf streams from pane 0 and pane 1.
  coledatasource* pdatasource = new coledatasource; 

  cstring strpane0;
  cstring strpane1;

  cuiview* pview;

  setactiveview((cview *)m_splitterwnd.getpane(0,0));
  pview = cuiview::getview();
  pview->streamread(strpane0);

  setactiveview((cview *)m_splitterwnd.getpane(0,1));
  pview = cuiview::getview();
  pview->streamread(strpane1);

  // we are going to concatenate the two rtf streams together
  // therefore drop the ending paren on the first stream as well
  // as the starting header on the second stream

  csharedfile file (gmem_moveable | gmem_ddeshare | gmem_zeroinit);
  cstring strtemp; 

  // find ending parenthesis and position just before it
  strtemp = strpane0.left (strpane0.reversefind('}'));
  file.write (strtemp, strtemp.getlength());

  // drop rtf header information through \fonttbl data because
  // you can not, rather, should not nest rtf with header information
  // notice i break the rules till i finish my rtf reader
  //
  // rtf version 1.5 specification defines the following header syntax:
  //    \rtf<charset> \deff? <fonttbl><filetb>?<colortbl>?<stylesheet>?
  //    <listtables>?<revtbl>?
  // 
  strtemp = strpane1;
  char *pdest = strstr((lpctstr)strtemp, _t("{\\colortbl"));
  int  offset = 0;
  if (pdest != null)
  {
    offset = pdest - (lpctstr)strtemp;
  }

  // complete rtf stream with a closing parenthesis and write to shared file
  //
  strtemp = strpane1.mid (offset);
  strtemp += "}";
  file.write (strtemp, strtemp.getlength());

  uint format = ::registerclipboardformat (cf_rtf);
  hglobal hmem = file.detach();
#if _mfc_ver <= 0x0421
  ::globalunlock(hmem);
#endif
  pdatasource->cacheglobaldata (format, hmem);
  pdatasource->setclipboard();
}


void cuimainwindow::onupdateeditbothpanes(ccmdui* pcmdui) 
{
  // if either pane isn't a cuiview class disable button
  // cuiview is a cricheditview derived class

  cobject* objpane0;
  cobject* objpane1;

  objpane0 = m_splitterwnd.getpane(0,0);
  objpane1 = m_splitterwnd.getpane(0,1);

  if ( (objpane0->iskindof(runtime_class(cuiview))) &&
       (objpane1->iskindof(runtime_class(cuiview))) )
    pcmdui->enable(1);
  else
    pcmdui->enable(0);
}


void cuiview::streamread (cstring& rtfstring)
{
  csharedfile sf;

  editstream es;
  es.dwcookie = (dword)&sf;
  es.dwerror = 0;
  es.pfncallback = streamoutctrl;
 
  getricheditctrl().streamout(sf_rtf , es);
 
  dword dw = sf.getlength();
  int nsize = int_max;

  if (dw write(pbbuff, cb);
  *pcb = cb;

  return 0;
}

updated 4 april 1998