Annotation of mstools/samples/ole/clidemo/stream.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * stream.c - io stream function callbacks
                      3:  *
                      4:  * Created by Microsoft Corporation.
                      5:  * (c) Copyright Microsoft Corp. 1990 - 1992  All Rights Reserved
                      6:  */
                      7: 
                      8: /***************************************************************************
                      9:  * This file contains all routines that directly and indirectly deal with
                     10:  * file i/o.  The OLE stream call back functions exist in this file.     
                     11:  **************************************************************************/
                     12: 
                     13: //*** INCLUDES ***
                     14: 
                     15: #include <windows.h>
                     16: #include <ole.h>
                     17: 
                     18: #include "global.h"
                     19: #include "utility.h"
                     20: #include "stream.h"
                     21: #include "object.h"
                     22: #include "demorc.h"
                     23: 
                     24: //*** Globals ***
                     25: 
                     26: BOOL fLoadFile = FALSE;
                     27: 
                     28: /***************************************************************************
                     29:  *  ReadStream() - OLE Callback Function (Get)       
                     30:  *
                     31:  *  This function is pointed to from the OLESTREAM vtbl; it is Get.
                     32:  *
                     33:  *  returns DWORD  - number of bytes actually read
                     34:  **************************************************************************/
                     35: 
                     36: DWORD  APIENTRY ReadStream(           //* ENTRY:
                     37:    LPAPPSTREAM    lpStream,            //* application stream pointer
                     38:    LPSTR          lpstr,               //* string pointer
                     39:    DWORD          cb                   //* byte count
                     40: ){
                     41: 
                     42:    return _lread(lpStream->fh, lpstr, cb);
                     43: 
                     44: }
                     45: 
                     46: /***************************************************************************
                     47:  *  WriteStream() - OLE Callback function (Put)
                     48:  *
                     49:  *  This function is pointed to from the OLESTREAM vtbl; it is Put.
                     50:  *
                     51:  *  Returns DWORD  - number of bytes actually written
                     52:  **************************************************************************/
                     53: 
                     54: DWORD  APIENTRY WriteStream(           //* ENTRY:
                     55:    LPAPPSTREAM    lpStream,            //* application stream pointer 
                     56:    LPSTR          lpstr,               //* string pointer
                     57:    DWORD          cb                   //* number of bytes to write
                     58: ){
                     59: 
                     60:    return _lwrite(lpStream->fh, lpstr, cb);
                     61: 
                     62: }
                     63: 
                     64: /****************************************************************************
                     65:  *  ReadFromFile()
                     66:  *
                     67:  *  This function reads OLE objects from a file. If the document 
                     68:  *  contains manual links, the user will be prompted to update those links.
                     69:  *
                     70:  *  Returns BOOL  - TRUE if the read(s) were successful
                     71:  ***************************************************************************/
                     72: 
                     73: BOOL FAR ReadFromFile(                 //* ENTRY:
                     74:    LPAPPSTREAM    lpStream,            //* application stream pointer
                     75:    LHCLIENTDOC    lhcDoc,              //* document handle
                     76:    LPOLECLIENT    lpClient             //* pointer to OLE client structure
                     77: ){                                     //* LOCAL:
                     78:    BOOL           bReturn = FALSE;     //* return value
                     79:    INT            cFileObjects;        //* number of file objects
                     80: 
                     81:    Hourglass(TRUE);
                     82:    fLoadFile = TRUE;
                     83: 
                     84:    SetFilePointer((HANDLE)lpStream->fh, 0, NULL, 0);
                     85:                                        //* in the file
                     86:    if (_lread(lpStream->fh, (LPSTR)&cFileObjects, sizeof(INT)) < sizeof(INT))
                     87:       goto Error;
                     88: 
                     89:    for (; cFileObjects; --cFileObjects) 
                     90:    {
                     91:       if (!ObjRead(lpStream,lhcDoc,lpClient)) 
                     92:       {
                     93:          ErrorMessage(E_FAILED_TO_READ_OBJECT);
                     94:          goto Error;
                     95:       }
                     96:    }
                     97:    
                     98:    ShowDoc(lhcDoc,1);
                     99:    UpdateLinks(lhcDoc);
                    100: 
                    101:    bReturn = TRUE;                     //* SUCCESS
                    102: 
                    103: Error:                                 //* ERROR Tag
                    104:     
                    105:    Hourglass(FALSE);
                    106:    fLoadFile = FALSE;
                    107:    return bReturn;                     //* return
                    108: 
                    109: }
                    110: 
                    111: /****************************************************************************
                    112:  *  ObjRead()
                    113:  *
                    114:  *  Rread an object from the specified file. The file pointer will 
                    115:  *  be advanced past the object.
                    116:  *
                    117:  *  HANDLE fh     - DOS file handle of file to be read from
                    118:  *
                    119:  *  returns HWND  - window handle to item window containing the OLE object
                    120:  ***************************************************************************/
                    121: 
                    122: BOOL FAR ObjRead(                      //* ENTRY:
                    123:    LPAPPSTREAM    lpStream,            //* application stream pointer
                    124:    LHCLIENTDOC    lhcDoc,              //* document handle
                    125:    LPOLECLIENT    lpClient             //* pointer to OLE client structure
                    126: ){                                     //* LOCAL:
                    127:    APPITEMPTR     pItem;               //* application item pointer
                    128:    LPOLEOBJECT    lpObject;            //* pointer ole object 
                    129:    LONG           otObject;            //* type of object 
                    130:    RECT           rcObject;            //* object rect 
                    131:    CHAR           szTmp[CBOBJNAMEMAX]; //* temporary string buffer
                    132:    CHAR           szProto[PROTOCOL_STRLEN+1];//* protocol string
                    133:    INT            i;                   //* index
                    134: 
                    135:    if (_lread(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
                    136:       return FALSE;
                    137: 
                    138:    if (_lread(lpStream->fh, szProto, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
                    139:       return FALSE;
                    140: 
                    141:    for (i=0; szProto[i] != ' '; i++);
1.1.1.2 ! root      142:    szProto[i] = 0;
1.1       root      143: 
                    144:    ValidateName( szTmp );
                    145: 
                    146:    if (!(pItem = PreItemCreate(lpClient, TRUE, lhcDoc))) 
                    147:       return FALSE;
                    148: 
                    149:    if (Error(OleLoadFromStream((LPOLESTREAM)&(lpStream->olestream), 
                    150:          szProto,(LPOLECLIENT)&(pItem->oleclient), lhcDoc, szTmp, &lpObject))) 
                    151:       goto Error;
                    152: 
                    153:    if (_lread(lpStream->fh, (LPSTR)&rcObject, sizeof(RECT)) < sizeof(RECT))
                    154:       goto Error;
                    155:    
                    156:    if (_lread(lpStream->fh, (LPSTR)&otObject, sizeof(LONG)) < sizeof(LONG))
                    157:       goto Error;
                    158: 
                    159:    if (PostItemCreate(lpObject, otObject, &rcObject, pItem))
                    160:    {
                    161:       pItem->fNew = TRUE;
                    162:       ObjSetBounds(pItem);
                    163:       return TRUE;                     //* SUCCESS return
                    164:    }
                    165:    else
                    166:       return FALSE;
                    167: 
                    168: Error:                                 //* ERROR Tag
                    169: 
                    170:    FreeAppItem(pItem);
                    171:    return FALSE;
                    172: 
                    173: }
                    174: 
                    175: /*************************************************************************
                    176:  *  WriteToFile()
                    177:  *
                    178:  *  Write current document to a file.
                    179:  *
                    180:  *  returns BOOL - TRUE if file successfully written
                    181:  ************************************************************************/
                    182: 
                    183: BOOL FAR WriteToFile(                  //* ENTRY:
                    184:    LPAPPSTREAM    lpStream             //* application stream pointer
                    185: ){                                     //* LOCAL:
                    186:    INT            iObjectsWritten=0;   //* counter of objects written to file
                    187:    APPITEMPTR     pItem;               //* application Item pointer
                    188:    
                    189:    UpdateFromOpenServers();
                    190:       
                    191:    SetFilePointer((HANDLE)lpStream->fh, 0, NULL, 0);
                    192:    
                    193:    Hourglass(TRUE);
                    194: 
                    195:    if (_lwrite(lpStream->fh, (LPSTR)&iObjects, sizeof(INT)) < sizeof(INT))
                    196:       goto Error;
                    197: 
                    198:    for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
                    199:    {
                    200:       if (!ObjWrite(lpStream, pItem)) 
                    201:          goto Error;
                    202:       iObjectsWritten++;
                    203:    }
                    204: 
                    205:    if (iObjectsWritten != iObjects) 
                    206:       goto Error;
                    207: 
                    208: 
                    209:    Dirty(DOC_CLEAN);
                    210:    Hourglass(FALSE);
                    211:    return(TRUE);                       //* SUCCESS return
                    212: 
                    213: Error:                                 //* ERROR Tag
                    214:     
                    215:    Hourglass(FALSE);
                    216:    return(FALSE);                      //* ERROR return
                    217: 
                    218: }
                    219: 
                    220: /****************************************************************************
                    221:  *  ObjWrite()
                    222:  *
                    223:  *  This function writes an object to the specified
                    224:  *  file. The file pointer will be advanced past the end of
                    225:  *  the written object.
                    226: 
                    227:  *  Returns BOOL - TRUE if object written successfully
                    228:  ***************************************************************************/
                    229: 
                    230: BOOL FAR ObjWrite(                     //* ENTRY:
                    231:    LPAPPSTREAM    lpStream,            //* application stream pointer
                    232:    APPITEMPTR     pItem                //* application item pointer
                    233: ){                                     //* LOCAL:
                    234:    POINT           pt;                  //* center of rec point
                    235:    RECT            rc;                  //* bounding rectangle
                    236:    UINT            cbTmp = CBOBJNAMEMAX;
                    237:    CHAR            szTmp[PROTOCOL_STRLEN];//* protocol string
                    238: 
                    239:    OleQueryName(pItem->lpObject, szTmp, &cbTmp);
                    240: 
                    241:    if (_lwrite(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
                    242:       return FALSE;
                    243: 
                    244:    if (pItem->otObject == OT_STATIC)
                    245:       wsprintf(szTmp, "%-15s", STATICP);
                    246:    else   
                    247:       wsprintf(szTmp, "%-15s", STDFILEEDITING);
                    248: 
                    249:    if (_lwrite(lpStream->fh, szTmp, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
                    250:       return FALSE;
                    251: 
                    252:    if (Error(OleSaveToStream(pItem->lpObject, (LPOLESTREAM)&(lpStream->olestream))))
                    253:       return FALSE;
                    254: 
                    255:    GetClientRect(pItem->hwnd, (LPRECT)&rc);
                    256:    pt = *(LPPOINT)&rc;
                    257:    ClientToScreen(pItem->hwnd, (LPPOINT)&pt);
                    258:    ScreenToClient(hwndFrame, (LPPOINT)&pt);
                    259:    OffsetRect(
                    260:       &rc, 
                    261:       pt.x - rc.left - GetSystemMetrics(SM_CXFRAME),
                    262:       pt.y - rc.top  - GetSystemMetrics(SM_CYFRAME) 
                    263:    );
                    264: 
                    265:    if (_lwrite(lpStream->fh, (LPSTR)&rc, sizeof(RECT)) < sizeof(RECT)
                    266:          || _lwrite(lpStream->fh, (LPSTR)&(pItem->otObject), sizeof(LONG)) < sizeof(LONG))
                    267:       return FALSE;
                    268: 
                    269:    return TRUE;                        //* SUCCESS return
                    270: 
                    271: }
                    272: 
                    273: /****************************************************************************
                    274:  * UpdateLinks()
                    275:  *
                    276:  * Get the most up to date rendering information and show it.  
                    277:  ***************************************************************************/
                    278: 
                    279: static VOID UpdateLinks(               //* ENTRY
                    280:    LHCLIENTDOC    lhcDoc               //* client document handle
                    281: ){                                     //* LOCAL:
                    282:    INT            i=0;                 //* index
                    283:    APPITEMPTR     pItem;               //* temporary item pointer
                    284:    CHAR           szUpdate[CBMESSAGEMAX];//* update message?
                    285: 
                    286:    for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
                    287:    {
                    288:       if (pItem->lhcDoc == lhcDoc && pItem->otObject == OT_LINK)
                    289:       {   
                    290:          if (!i)
                    291:          {
                    292:             LoadString(hInst, IDS_UPDATELINKS, szUpdate, CBMESSAGEMAX);
                    293:             if (MessageBox(hwndFrame, szUpdate, szAppName,
                    294:                MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
                    295:                break; 
                    296:             i++;
                    297:          }
                    298:          Error(OleUpdate(pItem->lpObject));
                    299:       }
                    300:    }
                    301: 
                    302:    WaitForAllObjects();
                    303: 
                    304: }
                    305: 
                    306: /****************************************************************************
                    307:  * UpdateFromOpenServers()
                    308:  *
                    309:  * Get the most up to date rendering information before storing it.  
                    310:  ***************************************************************************/
                    311: 
                    312: static VOID UpdateFromOpenServers(VOID)
                    313: {                                      //* LOCAL:
                    314:    APPITEMPTR pItem;                   //* temporary item pointer
                    315:    APPITEMPTR pItemNext;
                    316: 
                    317:    for (pItem = GetTopItem(); pItem; pItem = pItemNext) 
                    318:    {
                    319:       pItemNext = GetNextItem(pItem); 
                    320:       if (pItem->otObject == OT_EMBEDDED || 
                    321:          (pItem->uoObject == oleupdate_oncall 
                    322:                && pItem->otObject == OT_LINK ))  
                    323: 
                    324:          if (OleQueryOpen(pItem->lpObject) == OLE_OK)
                    325:          {  
                    326:             CHAR szMessage[2*CBMESSAGEMAX];
                    327:             CHAR szBuffer[CBMESSAGEMAX];
                    328:             UINT cb = CBOBJNAMEMAX;       //* The name will be the server window title.
                    329:             CHAR szTmp[CBOBJNAMEMAX];     //* when the object is edited. 
                    330: 
                    331:             Error(OleQueryName(pItem->lpObject,szTmp,&cb));
                    332:             LoadString(hInst, IDS_UPDATE_OBJ, szBuffer, CBMESSAGEMAX);
                    333:             wsprintf(szMessage, szBuffer, (LPSTR)szTmp);
                    334: 
                    335:             if (MessageBox(hwndFrame, szMessage, szAppName, MB_YESNO | MB_ICONEXCLAMATION) == IDYES) 
                    336:             {
                    337:                Error(OleUpdate(pItem->lpObject));
                    338:                WaitForObject(pItem);
                    339:             }
                    340:             if (!pItem->fVisible)
                    341:                ObjDelete(pItem, OLE_OBJ_DELETE);
                    342:          }
                    343: 
                    344:    }
                    345: 
                    346:    WaitForAllObjects();
                    347: 
                    348: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.