home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- TER_OLE.C
- TER Object Linking and Embedding functions
-
- TER Editor Developer's Kit
- Sub Systems, Inc.
- Software License Agreement (1994)
- ----------------------------------
- This license agreement allows the purchaser the right to modify the
- source code to incorporate in an application larger than the editor itself.
- The target application must not be a programmer's utility 'like' a text editor.
- Sub Systems, Inc. reserves the right to prosecute anybody found to be
- making illegal copies of the executable software or compiling the source
- code for the purpose of selling there after.
- ===============================================================================*/
-
- /*******************************************************************************
- InitOle:
- Create the OLE client structure
- *******************************************************************************/
- BOOL InitOle(PTERWND w)
- {
- OLESTATUS status;
- BYTE DocName[129];
-
-
- // initialize variables
- TerClientVtbl.CallBack=NULL;// Ole client callback function
- OleStreamVtbl.Get=NULL; // Ole stream get callback function
- OleStreamVtbl.Put=NULL; // Ole stream put callback function
- lhDoc=0; // document handle
-
- // Create the client structure
- if ( NULL==(TerClientVtbl.CallBack=(CLIENT_CALLBACK) MakeProcInstance((FARPROC)ClientCallback,hTerInst))
- || NULL==(OleStreamVtbl.Get=(OLESTREAM_GET) MakeProcInstance((FARPROC)TerOleGet,hTerInst))
- || NULL==(OleStreamVtbl.Put=(OLESTREAM_PUT) MakeProcInstance((FARPROC)TerOlePut,hTerInst)) ){
- MessageBox(hTerWnd,"Can not create ole callback function instances!",NULL,MB_OK);
- return FALSE;
- }
-
- TerClient.client.lpvtbl=&TerClientVtbl;
- TerClient.TerData=w; // store the handle of the data area
-
- OleStream.stream.lpstbl=&OleStreamVtbl;
- OleStream.TerData=w;
-
- // register the document
- wsprintf(DocName,"%s (%x)",TerArg.file,(UINT)hTerWnd);
- if (OLE_OK!=(status=OleRegisterClientDoc(TER_CLASS,DocName,0,&lhDoc))) return PrintOleErrorMessage(w,"Document Registration Error",status);
-
- // enable drag/drop
- DragAcceptFiles(hTerWnd,TRUE);
-
- return TRUE;
- }
-
- /*******************************************************************************
- ExitOle:
- Exit ole logic. Called by CloseTer
- *******************************************************************************/
- BOOL ExitOle(PTERWND w)
- {
- DragAcceptFiles(hTerWnd,FALSE);
-
- #if !defined(WIN32)
- if (TerClientVtbl.CallBack) FreeProcInstance((FARPROC)TerClientVtbl.CallBack);
- if (OleStreamVtbl.Get) FreeProcInstance((FARPROC)OleStreamVtbl.Get);
- if (OleStreamVtbl.Put) FreeProcInstance((FARPROC)OleStreamVtbl.Put);
- #endif
- if (lhDoc) OleRevokeClientDoc(lhDoc);
-
- return TRUE;
- }
-
- /*******************************************************************************
- ClientCallback:
- The OLE client callback function.
- *******************************************************************************/
- int CALLBACK _export ClientCallback(LPOLECLIENT client, OLE_NOTIFICATION notice, LPOLEOBJECT pObject)
- {
- PTERWND w;
- OLESTATUS status;
- MSG msg;
- int i,OleItem;
-
- w=((struct StrClient far *)client)->TerData; // retrieve the TER window data pointer
-
- if (notice==OLE_RELEASE) {
- WaitForOle--; // wait is over
- if (pObject) {
- OleReleaseMethod=OleQueryReleaseMethod(pObject);
- if (OLE_OK!=(status=OleQueryReleaseError(pObject))) PrintOleErrorMessage(w,"Object Release Error",status);
- }
- return TRUE;
- }
- if (notice!=OLE_SAVED && notice!=OLE_CHANGED && notice!=OLE_CLOSED) return TRUE;
- if (pObject==NULL) return TRUE;
-
- // get the item number
- OleItem=-1;
- for (i=0;i<TotalFonts;i++) {
- if (!TerFont[i].InUse
- || !(TerFont[i].style&PICT)
- || TerFont[i].ObjectType==OBJ_NONE) continue;
- if (TerFont[i].pObject==pObject) {
- OleItem=i;
- break;
- }
- }
- if (OleItem<0) return TRUE;
-
- if (notice==OLE_SAVED) TerFont[OleItem].OleNotice|=OLENOT_SAVED;
- if (notice==OLE_CHANGED) TerFont[OleItem].OleNotice|=OLENOT_CHANGED;
- if (notice==OLE_CLOSED) TerFont[OleItem].OleNotice|=OLENOT_CLOSED;
-
- // Post a message to the TER window to activate idle time activity
- if (!PeekMessage(&msg,hTerWnd,0,0,PM_NOREMOVE|PM_NOYIELD)) PostMessage(hTerWnd,TER_IDLE,0,0L);
-
- return TRUE;
- }
-
- /*******************************************************************************
- OlePostProcessing:
- This function is called by the TER window main loop to process any
- pending messages from the ClientCallback function.
- *******************************************************************************/
- OlePostProcessing(PTERWND w)
- {
- OLESTATUS status;
- HANDLE hData;
- struct StrFont font;
- int i,OleItem;
- BOOL repaint=FALSE;
-
- // examine each object for pending OLE notification
- for (OleItem=0;OleItem<TotalFonts;OleItem++) {
- if ( !TerFont[OleItem].InUse
- || !(TerFont[OleItem].style&PICT)
- || TerFont[OleItem].ObjectType==OBJ_NONE
- || TerFont[OleItem].OleNotice==0) {
- continue;
- }
-
- // process OLE_SAVED notice
- if (TerFont[OleItem].OleNotice&(OLENOT_SAVED|OLENOT_CHANGED)) {
- // update the presentation data
- status=OleGetData(TerFont[OleItem].pObject,CF_METAFILEPICT,&hData);
- if (status!=OLE_OK && status!=OLE_WARN_DELETE_DATA) {
- PrintOleErrorMessage(w,"Error getting ole data (OlePostProcessing)",status);
- goto END;
- }
-
- font=TerFont[OleItem]; // make a copy of the exiting font structure
- InitTerObject(w,OleItem); // freeup this slot
- if (TerCreateMetafile(w,(HGLOBAL)hData,OleItem)<0) {
- TerFont[OleItem]=font; // restore the previous structure
- goto END;
- }
- if (status==OLE_WARN_DELETE_DATA) DeleteMetaFile((HMETAFILE)hData); // delete metafile
-
- // Apply the previous flags
- TerFont[OleItem].ObjectType=font.ObjectType;
- TerFont[OleItem].pObject=font.pObject;
- TerFont[OleItem].OleNotice=font.OleNotice;
-
- // Apply the previous size
- if (TerFont[OleItem].ObjectType==OBJ_EMBED_INSERT) { // item being inserted
- TerFont[OleItem].ObjectType=OBJ_EMBED;
- TerInsertObjectId(hTerWnd,OleItem,TRUE); // this is a new object to be inserted
- }
- else {
- TerFont[OleItem].PictHeight=font.PictHeight;
- TerFont[OleItem].PictWidth=font.PictWidth;
- TerFont[OleItem].height=font.height;
- for (i=0;i<256;i++) TerFont[OleItem].CharWidth[i]=font.CharWidth[i];
- XlateSizeForPrt(w,OleItem); // convert size to printer resolution
-
- if (font.hMeta) DeleteMetaFile(font.hMeta); // delete the previous metafile
- }
-
- TerArg.modified++; // file modified
- }
- if (TerFont[OleItem].OleNotice&OLENOT_CLOSED) {
- if (TerFont[OleItem].ObjectType==OBJ_EMBED_TEMP) { // delete the temporary object
- status=OleDelete(TerFont[OleItem].pObject);
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,TerFont[OleItem].pObject);
- TerFont[OleItem].ObjectType=OBJ_NONE;
- TerFont[OleItem].pObject=NULL;
- }
- else if (TerFont[OleItem].ObjectType==OBJ_EMBED_INSERT) { // item not inserted
- TerFont[OleItem].InUse=FALSE;
- }
- }
-
- repaint=TRUE;
-
- END:
- TerFont[OleItem].OleNotice=0;
- }
-
- if (repaint) {
- //** reset the text map as the contents of the picture has changed
- for (i=0;i<TotalSegments;i++) DeleteTextSeg(w,i);
- TotalSegments=0;
- PaintTer(w);
- }
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerPasteSpecial:
- Accept a string from the user along with other parameters and search for
- the first instances of the string.
- *******************************************************************************/
- TerPasteSpecial(PTERWND w)
- {
- int index,len,i;
- int select;
- UINT ClipFormat,PasteFormat;
- long BuffSize;
- BYTE class[MAX_WIDTH+1],doc[MAX_WIDTH+1],item[MAX_WIDTH+1],PasteFmts[MAX_PASTE_FORMATS];
- BOOL PasteLink=FALSE;
-
- if (WaitForOle) return TRUE; // ole operation in progress
- if (FontsReleased) RecreateFonts(w,hTerDC); // recreate fonts
-
- // Build the source description string
- ClipFormat=GetOleClipItem(w,class,doc,item);
-
- if (ClipFormat>0) {
- // get the class name
- BuffSize=sizeof(TempString);
- RegQueryValue(HKEY_CLASSES_ROOT,class,TempString,&BuffSize);
-
- // Build the item string
- len=lstrlen(doc);
- for (i=len;i>0;i--) if (doc[i-1]=='\\') break;
- lstrcpy(TempString1,&(doc[i]));
- if (lstrlen(TempString1)>0) lstrcat(TempString1," ");
- lstrcat(TempString1,item);
- }
- else {
- lstrcpy(TempString,"Unknown");
- lstrcpy(TempString1,"");
- }
-
- // build the datatype table
- for (i=0;i<MAX_PASTE_FORMATS;i++) PasteFmts[i]=FALSE; // list of available formats
- if (IsClipboardFormatAvailable(OwnerLinkClipFormat)) PasteFmts[PASTE_OBJECT]=TRUE;
- if (IsClipboardFormatAvailable(CF_METAFILEPICT)) PasteFmts[PASTE_PICT]=TRUE;
- if (IsClipboardFormatAvailable(CF_BITMAP)) PasteFmts[PASTE_BM]=TRUE;
- if (IsClipboardFormatAvailable(CF_DIB)) PasteFmts[PASTE_DIB]=TRUE;
- if (IsClipboardFormatAvailable(RtfClipFormat)) PasteFmts[PASTE_RTF]=TRUE;
- if (IsClipboardFormatAvailable(CF_TEXT)) PasteFmts[PASTE_TEXT]=TRUE;
-
- // check if atleast one format available
- for (i=0;i<MAX_PASTE_FORMATS;i++) if (PasteFmts[i]) break;
- if (i==MAX_PASTE_FORMATS) return TRUE; // no formats available
-
- index=CallDialogBox(w,"PasteSpecialParam",PasteSpecialParam,(long)(LPBYTE)PasteFmts);
- if (index<0) return TRUE; // operation cancelled
-
- // extract the selected clipboard format
- if (index>=MAX_PASTE_FORMATS) { // Is object linking selected
- index-=MAX_PASTE_FORMATS;
- PasteLink=TRUE;
- }
- for (i=0;i<MAX_PASTE_FORMATS;i++) {
- if (PasteFmts[i]) index--;
- if (index<0) break;
- }
- select=i; // selected format
-
- // get the paste format
- if (select==PASTE_TEXT) PasteFormat=CF_TEXT;
- else if (select==PASTE_RTF) PasteFormat=RtfClipFormat;
- else if (select==PASTE_PICT) PasteFormat=CF_METAFILEPICT;
- else if (select==PASTE_DIB) PasteFormat=CF_DIB;
- else if (select==PASTE_BM) PasteFormat=CF_BITMAP;
- else PasteFormat=NativeClipFormat;
-
- // Retrieve the text formats from the clipboard
- if (select==PASTE_RTF || select==PASTE_TEXT) return CopyFromClipboard(w,PasteFormat);
-
- // Process the ole objects
- if (select==PASTE_OBJECT) return TerOleFromClipboard(w,PasteFormat,PasteLink);
-
- // Process the non-ole picture formats
- if (select==PASTE_PICT || select==PASTE_DIB || select==PASTE_BM) return TerPastePicture(hTerWnd,PasteFormat,NULL,0,ALIGN_BOT,TRUE);
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerOleFromClipboard:
- Embed an OLE object from clipboard.
- *******************************************************************************/
- TerOleFromClipboard(PTERWND w, UINT format,BOOL PasteLink)
- {
- int pict;
- BYTE ObjectName[20];
- OLESTATUS status;
- HANDLE hData;
- LPOLEOBJECT pObject;
-
- // Erase any previously highlighted text
- if (HilightType==HILIGHT_CHAR) BlockDelete(w);
-
- if ((pict=FindOpenSlot(w))==-1) return FALSE; // find an empty slot
- wsprintf(ObjectName,"Ter Object# %d",pict);
-
- // create from clipboard
- if (!OpenClipboard(hTerWnd)) return FALSE;
- if (PasteLink) status=OleCreateLinkFromClip("StdFileEditing",&(TerClient.client),lhDoc,ObjectName,&pObject,olerender_format,CF_METAFILEPICT);
- else status=OleCreateFromClip("StdFileEditing",&(TerClient.client),lhDoc,ObjectName,&pObject,olerender_format,CF_METAFILEPICT);
- if (status==OLE_ERROR_PROTOCOL) {
- if (PasteLink) status=OleCreateLinkFromClip("Static",&(TerClient.client),lhDoc,ObjectName,&pObject,olerender_format,CF_METAFILEPICT);
- else status=OleCreateFromClip("Static",&(TerClient.client),lhDoc,ObjectName,&pObject,olerender_format,CF_METAFILEPICT);
- }
- CloseClipboard();
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) return PrintOleErrorMessage(w,"OleCreateFromClip",status);
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,pObject);
-
- // Get the metafile data
- status=OleGetData(pObject,CF_METAFILEPICT,&hData);
- if (status!=OLE_OK && status!=OLE_WARN_DELETE_DATA) return PrintOleErrorMessage(w,"Error getting ole data (TerOleCreateFromClipboard)",status);
-
- pict=TerCreateMetafile(w,(HGLOBAL)hData,-1); // extract metafile
- if (status==OLE_WARN_DELETE_DATA) DeleteMetaFile((HMETAFILE)hData); // delete metafile
- if (PasteLink) TerFont[pict].ObjectType=OBJ_LINK;
- else TerFont[pict].ObjectType=OBJ_EMBED;
-
- TerFont[pict].pObject=pObject;
-
- TerInsertObjectId(hTerWnd,pict,TRUE); // insert this object into text
- FitPictureInFrame(w,CurLine,FALSE);
-
- return TRUE;
- }
-
- /******************************************************************************
- TerInsertObject:
- Insert a new ole object. This function activates the OLE server. the
- second part of this function (TerInsertObjectId) is called from the
- ole post processing function to actually insert the object into the
- text.
- ******************************************************************************/
- BOOL TerInsertObject(PTERWND w)
- {
- int SubKey;
- long BuffSize;
- BYTE class[100];
- int pict;
- BYTE ObjectName[20];
- OLESTATUS status;
-
-
- if ((SubKey=CallDialogBox(w,"InsertObjectParam",InsertObjectParam,0L))<0) return TRUE;
-
- // get the class name
- RegEnumKey(HKEY_CLASSES_ROOT,SubKey,class,sizeof(class)-1);
- BuffSize=sizeof(TempString)-1;
- RegQueryValue(HKEY_CLASSES_ROOT,class,TempString,&BuffSize);
-
- // create the object
- if ((pict=FindOpenSlot(w))==-1) return FALSE; // find an empty slot to make the object name
- TerFont[pict].InUse=TRUE;
- TerFont[pict].ObjectType=OBJ_EMBED_INSERT; // object to be inserted
- TerFont[pict].style=PICT;
- TerFont[pict].PictType=PICT_METAFILE;
- TerFont[pict].hMeta=0;
-
- wsprintf(ObjectName,"Ter Object# %d",pict);
-
- status=OleCreate("StdFileEditing",&(TerClient.client),class,lhDoc,ObjectName,&(TerFont[pict].pObject),olerender_format,CF_METAFILEPICT);
- if (status==OLE_ERROR_PROTOCOL) {
- status=OleCreate("Static",&(TerClient.client),class,lhDoc,ObjectName,&(TerFont[pict].pObject),olerender_format,CF_METAFILEPICT);
- }
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) {
- TerFont[pict].InUse=FALSE;
- return PrintOleErrorMessage(w,"TerInsertObject",status);
- }
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,TerFont[pict].pObject);
-
- return TRUE;
- }
-
- /******************************************************************************
- TerInsertDragObject:
- Insert an ole object inserted using drag/drop. Only the first file
- in the drag/drop buffer is processed.
- ******************************************************************************/
- BOOL TerInsertDragObject(PTERWND w,HDROP hDrop)
- {
- int pict;
- BYTE ObjectName[20];
- OLESTATUS status;
- POINT pt;
- DWORD lParam;
- BYTE file[129];
-
- // Extract the dropped file
- if (!DragQueryPoint(hDrop,&pt)) return TRUE;
- DragQueryFile(hDrop,0,file,sizeof(file)-1); // get the first file
- DragFinish(hDrop); // release the drag buffer
-
- // get cursor location at the drag point
- lParam=pt.y;
- lParam=lParam<<16; // high word
- lParam=lParam+pt.x;
- TerMousePos(w,lParam);
- CurLine=MouseLine;
- CurCol=MouseCol;
-
- // create the object
- if ((pict=FindOpenSlot(w))==-1) return FALSE; // find an empty slot to make the object name
- TerFont[pict].InUse=TRUE;
- TerFont[pict].ObjectType=OBJ_EMBED_INSERT; // object to be inserted
- TerFont[pict].style=PICT;
- TerFont[pict].PictType=PICT_METAFILE;
- TerFont[pict].hMeta=0;
-
- wsprintf(ObjectName,"Ter Object# %d",pict);
-
- status=OleCreateFromFile("StdFileEditing",&(TerClient.client),"Package",file,lhDoc,ObjectName,&(TerFont[pict].pObject),olerender_format,CF_METAFILEPICT);
- if (status==OLE_ERROR_PROTOCOL) {
- status=OleCreateFromFile("Static",&(TerClient.client),"Package",file,lhDoc,ObjectName,&(TerFont[pict].pObject),olerender_format,CF_METAFILEPICT);
- }
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) {
- TerFont[pict].InUse=FALSE;
- return PrintOleErrorMessage(w,"TerInsertObject",status);
- }
-
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,TerFont[pict].pObject);
- OlePostProcessing(w); // shows changes as we don't have focus now
-
- return TRUE;
- }
-
- /******************************************************************************
- TerEditOle:
- If the current item represents an OLE object, this functions calls the
- server to update the item.
- ******************************************************************************/
- BOOL TerEditOle(PTERWND w)
- {
- int obj;
- OLESTATUS status;
- RECT rect;
-
- // get current font id
- obj=GetCurCfmt(w,CurLine,CurCol);
- if (!(TerFont[obj].style&PICT)) return FALSE;
- if (TerArg.ReadOnly || TerFont[obj].style&PROTECT) return FALSE;
-
- // If necessary, convert picture objects to temporary OLE objects
- if (TerFont[obj].ObjectType==OBJ_NONE) TerCreateTempOle(w,obj);
- if (TerFont[obj].ObjectType==OBJ_NONE) return FALSE;
-
- // build the bounding rectangle
- rect.left=ColToUnits(w,CurCol,CurLine,LEFT);
- rect.right=rect.left+TerFont[obj].CharWidth[0];
- rect.top=LineToUnits(w,CurLine);
- rect.bottom=rect.top+TerFont[obj].height;
-
- // Call server to edit the object
- status=OleActivate(TerFont[obj].pObject,0,TRUE,TRUE,hTerWnd,&rect);
- if (status==OLE_ERROR_LAUNCH) {
- TerFont[obj].ObjectType=OBJ_NONE; // restore to plain picture
- MessageBox(hTerWnd,"Server program not available","Server Launch Error",MB_OK);
- return FALSE;
- }
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) return PrintOleErrorMessage(w,"Ole Activation",status);
- if (status==OLE_WAIT_FOR_RELEASE) {
- TerOleWaitForRelease(w,TerFont[obj].pObject);
- if (OLE_OK!=OleQueryReleaseError(TerFont[obj].pObject)) return FALSE;
- }
-
- return TRUE;
- }
-
- /******************************************************************************
- TerCreateTempOle:
- This function creates a temporary ole object from a metafile picture. The
- temporary metafile object is used to edit picture using MSDRAW.
- ******************************************************************************/
- TerCreateTempOle(PTERWND w,int obj)
- {
- OLESTATUS status;
- BYTE ObjectName[20];
- LPOLEOBJECT pObject;
- HANDLE hMeta,hNative;
- LPWORD pNative;
- long MetaSize,bmWidth,bmHeight;
- METAHEADER far *hdr; // metafile header
- struct StrFont SaveDIBObj,SaveMetaObj;
-
- if (!(TerFont[obj].style&PICT)) return FALSE;
- if (TerFont[obj].ObjectType!=OBJ_NONE) return FALSE;
-
- // Convert DIB to metafile if necessary
- if (TerFont[obj].PictType==PICT_DIBITMAP) {
- SaveDIBObj=TerFont[obj]; // save DIB object
- if (DIB2Metafile(w,obj)) {
- SaveMetaObj=TerFont[obj];// save metafile object
- TerFont[obj]=SaveDIBObj; // restore to delete
- DeleteTerObject(w,obj);
- TerFont[obj]=SaveMetaObj;// restore metafile object
- }
- }
- if (TerFont[obj].PictType!=PICT_METAFILE) return FALSE;
-
- // create an OLE object
- wsprintf(ObjectName,"Ter Object# %d",obj);
- TerFont[obj].ObjectType=OBJ_EMBED_TEMP; // temporary embedded object
- status=OleCreateInvisible("StdFileEditing",&(TerClient.client),MSDRAW_CLASS,lhDoc,ObjectName,&pObject,olerender_format,CF_METAFILEPICT,FALSE);
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) {
- TerFont[obj].ObjectType=OBJ_NONE; // restore to plain picture
- return PrintOleErrorMessage(w,"TerCreateTempOle(A)",status);
- }
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,pObject);
-
- // get the object data
- hMeta=TerGetMetaFileBits(TerFont[obj].hMeta); // convert to global handle
- if (NULL==(hdr=(METAHEADER far *)GlobalLock(hMeta)) ) {
- MessageBox(hTerWnd,"Error accessing meta file data",NULL,MB_OK);
- TerFont[obj].ObjectType=OBJ_NONE; // restore to plain picture
- return FALSE;
- }
- MetaSize=(hdr->mtSize*2)+3*sizeof(WORD); // add 3 integers for additional header
- if ( NULL==(hNative=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,MetaSize)) // allocate memory for the text data in our format
- || NULL==(pNative=GlobalLock(hNative)) ){
- MessageBox(hTerWnd,"Ran out of memory","TerCreateTempOle",MB_OK);
- TerFont[obj].ObjectType=OBJ_NONE; // restore to plain picture
- return FALSE;
- }
- // copy data
- bmHeight=ScrToPointsY(TerFont[obj].bmHeight); // objure height in pointsize
- bmWidth=ScrToPointsX(TerFont[obj].bmWidth); // objure width in pointsize
- pNative[0]=MM_ANISOTROPIC; // mapping mode
- pNative[1]=(WORD)((bmWidth*2500)/72); // width in HIMETRIC
- pNative[2]=(WORD)((bmHeight*2500)/72); // height in HIMETIC
- HugeMove(hdr,&(pNative[3]),hdr->mtSize*2); // move the rtf data
-
- GlobalUnlock(hNative);
- GlobalUnlock(hMeta);
- TerFont[obj].hMeta=TerSetMetaFileBits(hMeta); // convert to metafile handle
-
- // set the object data
- status=OleSetData(pObject,NativeClipFormat,hNative);
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,pObject);
- if (status!=OLE_OK) {
- TerFont[obj].ObjectType=OBJ_NONE; // restore to plain picture
- return PrintOleErrorMessage(w,"TerCreateTempOle(B)",status);
- }
-
- TerFont[obj].pObject=pObject; // temporary object pointer
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerOleLoad:
- This function supplies the ole data in the temporary handle (TerFont.hObject).
- and creates the OLE object. This functions frees the temporary memory
- handle.
- *******************************************************************************/
- TerOleLoad(PTERWND w,int obj)
- {
- BYTE ObjectName[20];
- OLESTATUS status;
-
- if (!TerFont[obj].hObject) return FALSE;
-
- hOleData=TerFont[obj].hObject; // initialize the buffer
- pOleData=NULL;
-
- if (NULL==(pOleData=GlobalLock(hOleData)) ){
- MessageBox(hTerWnd,"Ran out of memory","TerOleLoad",MB_OK);
- return FALSE;
- }
-
- // load the object
- wsprintf(ObjectName,"Ter Object# %d",obj);
- OleStream.MaxSize=TerFont[obj].ObjectSize; // object size
- OleStream.offset=0; // beginning offset in the buffer
- status=OleLoadFromStream(&(OleStream.stream),"StdFileEditing",&(TerClient.client),lhDoc,ObjectName,&(TerFont[obj].pObject));
- if (status==OLE_ERROR_PROTOCOL) {
- OleStream.offset=0; // beginning offset in the buffer
- status=OleLoadFromStream(&(OleStream.stream),"Static",&(TerClient.client),lhDoc,ObjectName,&(TerFont[obj].pObject));
- }
-
- if (status!=OLE_OK && status!=OLE_WAIT_FOR_RELEASE) return PrintOleErrorMessage(w,"TerOleLoad",status);
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,TerFont[obj].pObject);
-
- GlobalUnlock(hOleData);
- GlobalFree(hOleData);
- TerFont[obj].hObject=NULL;
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerOleUnload:
- This function delete the OLE object.
- *******************************************************************************/
- TerOleUnload(PTERWND w,int obj)
- {
- OLESTATUS status;
- status=OleDelete(TerFont[obj].pObject);
- if (status==OLE_WAIT_FOR_RELEASE) TerOleWaitForRelease(w,TerFont[obj].pObject);
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerOleQuerySize:
- This function returns the size of the specified object storage.
- *******************************************************************************/
- BOOL TerOleQuerySize(PTERWND w, int obj, DWORD far *size)
- {
- (*size)=0;
- if (OLE_OK!=OleQuerySize(TerFont[obj].pObject,size)) return FALSE;
-
- return TRUE;
- }
-
- /*******************************************************************************
- GetOleStorageData:
- This function extracts the OLE storage data in a global buffer. This
- function allocates a new global buffer and leaves it locked. The calling
- fuction must unlock and free the buffer when done.
- *******************************************************************************/
- GetOleStorageData(PTERWND w,int obj)
- {
- DWORD ObjectSize;
- LPOLEOBJECT pObject=TerFont[obj].pObject;
-
- hOleData=NULL; // initialize the buffer
- pOleData=NULL;
-
- // get the data size requirement and allocate memory
- if (OLE_OK!=OleQuerySize(pObject,&ObjectSize) || ObjectSize==0) {
- MessageBox(hTerWnd,"Error getting object size!","GetOleStorageData",MB_OK);
- return FALSE;
- }
-
- if ( NULL==(hOleData=GlobalAlloc(GMEM_MOVEABLE,ObjectSize)) // allocate memory for the ole data
- || NULL==(pOleData=GlobalLock(hOleData)) ){
- MessageBox(hTerWnd,"Ran out of memory","GetOleStorageData",MB_OK);
- return FALSE;
- }
-
- // set to object to the global buffer
- OleStream.MaxSize=ObjectSize; // size of the object to read
- OleStream.offset=0; // beginning buffer offset
- if (OLE_OK!=OleSaveToStream(pObject,&(OleStream.stream))) {
- MessageBox(hTerWnd,"Error getting object data","GetOleStorageData",MB_OK);
- GlobalUnlock(hOleData);
- GlobalFree(hOleData);
- return FALSE;
- }
-
- return TRUE;
- }
-
- /*******************************************************************************
- TerOleWaitForRelease:
- Wait for an ole async operation to be over
- *******************************************************************************/
- TerOleWaitForRelease(PTERWND w,LPOLEOBJECT pObject)
- {
- MSG msg;
-
- WaitForOle++; // suspend Ter window message processing
-
- while (WaitForOle) {
- // message loop
- while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- return TRUE;
- }
-
- /*******************************************************************************
- PrintOleErrorMessage:
- Prints an OLE error message and return FALSE
- *******************************************************************************/
- BOOL PrintOleErrorMessage(PTERWND w,LPBYTE topic,OLESTATUS status)
- {
- BYTE string[40];
-
- if (status==OLE_ERROR_ALREADY_REGISTERED) MessageBox(hTerWnd,"Already Registered!",topic,MB_OK);
- else if (status==OLE_ERROR_MEMORY) MessageBox(hTerWnd,"Ran out of memory!",topic,MB_OK);
- else if (status==OLE_ERROR_NAME) MessageBox(hTerWnd,"Naming Error or Duplicate Name!",topic,MB_OK);
- else if (status==OLE_ERROR_CLIPBOARD) MessageBox(hTerWnd,"Failed to get/set clipboard data",topic,MB_OK);
- else if (status==OLE_ERROR_FORMAT) MessageBox(hTerWnd,"Requested format not available",topic,MB_OK);
- else if (status==OLE_ERROR_HANDLE) MessageBox(hTerWnd,"OLE_ERROR_HANDLE",topic,MB_OK);
- else if (status==OLE_ERROR_OPTION) MessageBox(hTerWnd,"OLE_ERROR_OPTION",topic,MB_OK);
- else if (status==OLE_ERROR_PROTOCOL) MessageBox(hTerWnd,"OLE_ERROR_PROTOCOL",topic,MB_OK);
- else if (status==OLE_ERROR_OBJECT) MessageBox(hTerWnd,"Error: Not a valid object",topic,MB_OK);
- else if (status==OLE_ERROR_LAUNCH) MessageBox(hTerWnd,"Error: Server Not Available",topic,MB_OK);
- else if (status==OLE_ERROR_CLASS) MessageBox(hTerWnd,"Error: Invalid Ole Class",topic,MB_OK);
- else if (status==OLE_ERROR_POKE_NATIVE) MessageBox(hTerWnd,"Server Does Not Accept Data",topic,MB_OK);
- else if (status==OLE_BUSY) MessageBox(hTerWnd,"Object Busy",topic,MB_OK);
- else {
- wsprintf(string,"Ole Error #%d Encountered!",(UINT)status);
- MessageBox(hTerWnd,string,topic,MB_OK);
- }
-
- return FALSE;
- }
-