Annotation of mstools/ole20/samples/ole2ui/oleutl.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * OLEUTL.C
                      3:  *
                      4:  * Miscellaneous utility functions for OLE 2.0 Applications:
                      5:  *
                      6:  *  Function                      Purpose
                      7:  *  -------------------------------------------------------------------
                      8:  *  SetDCToDrawInHimetricRect     Sets up an HIMETRIC mapping mode in a DC.
                      9:  *  ResetOrigDC                   Performs the opposite of 
                     10:  *                                SetDCToDrawInHimetricRect
                     11:  *  XformRectInPixelsToHimetric   Converts a rect into HiMetric units
                     12:  *  XformRectInHimetricToPixels   Converts a rect from HiMetric units
                     13:  *  XformSizeInPixelsToHimetric   Converts a SIZEL into HiMetric units
                     14:  *  XformSizeInHimetricToPixels   Converts a SIZEL from HiMetric units
                     15:  *  AreRectsEqual                 Compares to Rect's
                     16:  *
                     17:  *  ParseCmdLine                  Determines if -Embedding exists
                     18:  *  OpenOrCreateRootStorage       Creates a root docfile for OLE storage
                     19:  *  CommitStorage                 Commits all changes in a docfile
                     20:  *  CreateChildStorage            Creates child storage in another storage
                     21:  *  OpenChildStorage              Opens child storage in another storage
                     22:  *
                     23:  *
                     24:  * Copyright (c)1992 Microsoft Corporation, All Right Reserved
                     25:  */
                     26: 
                     27: 
                     28: #define STRICT  1
                     29: #include "ole2ui.h"
                     30: #include <stdlib.h>
                     31: #include <ctype.h>
                     32: 
                     33: //Internal function to this module
                     34: static LPSTR GetWord(LPSTR lpszSrc, LPSTR lpszDst);
                     35: 
                     36: 
                     37: /*
                     38:  * SetDCToAnisotropic
                     39:  *
                     40:  * Purpose:
                     41:  *  Setup the correspondence between the rect in device unit (Viewport) and
                     42:  *  the rect in logical unit (Window) so that the proper scaling of
                     43:  *  coordinate systems will be calculated. set up both the Viewport and
                     44:  *  the window as follows:
                     45:  *
                     46:  *      1) ------------------ ( 2
                     47:  *      |                     |
                     48:  *      |                     |
                     49:  *      |                     |
                     50:  *      |                     |
                     51:  *      |                     |
                     52:  *      3) ------------------ ( 4
                     53:  *
                     54:  *      Origin   = P3
                     55:  *      X extent = P2x - P3x
                     56:  *      Y extent = P2y - P3y
                     57:  *
                     58:  * Parameters:
                     59:  *  hDC             HDC to affect
                     60:  *  lprcPhysical    LPRECT containing the physical (device) extents of DC
                     61:  *  lprcLogical     LPRECT containing the logical extents
                     62:  *  lprcWindowOld   LPRECT in which to preserve the window for ResetOrigDC
                     63:  *  lprcViewportOld LPRECT in which to preserver the viewport for ResetOrigDC
                     64:  *
                     65:  * Return Value:
                     66:  *  int             The original mapping mode of the DC.
                     67:  */
                     68: 
                     69: STDAPI_(int) SetDCToAnisotropic(
                     70:         HDC hDC,
                     71:         LPRECT lprcPhysical, LPRECT lprcLogical,
                     72:         LPRECT lprcWindowOld, LPRECT lprcViewportOld)
                     73: {
                     74:     int     nMapModeOld=SetMapMode(hDC, MM_ANISOTROPIC);
                     75:     
                     76:     SetWindowOrgEx(hDC, lprcLogical->left, lprcLogical->bottom, (LPPOINT)&lprcWindowOld->left);
                     77:     SetWindowExtEx(hDC, (lprcLogical->right-lprcLogical->left), (lprcLogical->top-lprcLogical->bottom), (LPSIZE)&lprcWindowOld->right);
                     78:     SetViewportOrgEx(hDC, lprcPhysical->left, lprcPhysical->bottom, (LPPOINT)&lprcViewportOld->left);
                     79:     SetViewportExtEx(hDC, (lprcPhysical->right-lprcPhysical->left), (lprcPhysical->top-lprcPhysical->bottom), (LPSIZE)&lprcViewportOld->right);
                     80: 
                     81:     return nMapModeOld;
                     82: }
                     83: 
                     84:     
                     85: /*
                     86:  * SetDCToDrawInHimetricRect
                     87:  *
                     88:  * Purpose:
                     89:  *  Setup the correspondence between the rect in pixels (Viewport) and
                     90:  *  the rect in HIMETRIC (Window) so that the proper scaling of
                     91:  *  coordinate systems will be calculated. set up both the Viewport and
                     92:  *  the window as follows:
                     93:  *
                     94:  *      1) ------------------ ( 2
                     95:  *      |                     |
                     96:  *      |                     |
                     97:  *      |                     |
                     98:  *      |                     |
                     99:  *      |                     |
                    100:  *      3) ------------------ ( 4
                    101:  *
                    102:  *      Origin   = P3
                    103:  *      X extent = P2x - P3x
                    104:  *      Y extent = P2y - P3y
                    105:  *
                    106:  * Parameters:
                    107:  *  hDC             HDC to affect
                    108:  *  lprcPix         LPRECT containing the pixel extents of DC
                    109:  *  lprcHiMetric    LPRECT to receive the himetric extents
                    110:  *  lprcWindowOld   LPRECT in which to preserve the window for ResetOrigDC
                    111:  *  lprcViewportOld LPRECT in which to preserver the viewport for ResetOrigDC
                    112:  *
                    113:  * Return Value:
                    114:  *  int             The original mapping mode of the DC.
                    115:  */
                    116: STDAPI_(int) SetDCToDrawInHimetricRect(
                    117:     HDC hDC,
                    118:     LPRECT lprcPix, LPRECT lprcHiMetric,
                    119:     LPRECT lprcWindowOld, LPRECT lprcViewportOld)
                    120:     {
                    121:     int     nMapModeOld=SetMapMode(hDC, MM_ANISOTROPIC);
                    122:     BOOL    fSystemDC  =FALSE;
                    123: 
                    124:     if (NULL==hDC)
                    125:         {
                    126:         hDC=GetDC(NULL);
                    127:         fSystemDC=TRUE;
                    128:         }
                    129: 
                    130:     XformRectInPixelsToHimetric(hDC, lprcPix, lprcHiMetric);
                    131: 
                    132:     SetWindowOrgEx(hDC, lprcHiMetric->left, lprcHiMetric->bottom, (LPPOINT)&lprcWindowOld->left);
                    133:     SetWindowExtEx(hDC, (lprcHiMetric->right-lprcHiMetric->left), (lprcHiMetric->top-lprcHiMetric->bottom), (LPSIZE)&lprcWindowOld->right);
                    134:     SetViewportOrgEx(hDC, lprcPix->left, lprcPix->bottom, (LPPOINT)&lprcViewportOld->left);
                    135:     SetViewportExtEx(hDC, (lprcPix->right-lprcPix->left), (lprcPix->top-lprcPix->bottom), (LPSIZE)&lprcViewportOld->right);
                    136: 
                    137:     if (fSystemDC)
                    138:         ReleaseDC(NULL, hDC);
                    139: 
                    140:     return nMapModeOld;
                    141:     }
                    142: 
                    143: 
                    144: 
                    145: /*
                    146:  * ResetOrigDC
                    147:  *
                    148:  * Purpose:
                    149:  *  Restores a DC set to draw in himetric from SetDCToDrawInHimetricRect.
                    150:  *
                    151:  * Parameters:
                    152:  *  hDC             HDC to restore
                    153:  *  nMapModeOld     int original mapping mode of hDC
                    154:  *  lprcWindowOld   LPRECT filled in SetDCToDrawInHimetricRect
                    155:  *  lprcViewportOld LPRECT filled in SetDCToDrawInHimetricRect
                    156:  *
                    157:  * Return Value:
                    158:  *  int             Same as nMapModeOld.
                    159:  */
                    160: 
                    161: STDAPI_(int) ResetOrigDC(
                    162:     HDC hDC, int nMapModeOld,
                    163:     LPRECT lprcWindowOld, LPRECT lprcViewportOld)
                    164:     {
                    165:     POINT     pOld;
                    166: 
                    167:     SetMapMode(hDC, nMapModeOld);
                    168: 
                    169:     SetWindowOrgEx(hDC,   lprcWindowOld->left,    lprcWindowOld->top,      (LPPOINT)&pOld);
                    170:     SetWindowExtEx(hDC,   lprcWindowOld->right,   lprcWindowOld->bottom,   (LPSIZE)&pOld);
                    171:     SetViewportOrgEx(hDC, lprcViewportOld->left,  lprcViewportOld->top,    (LPPOINT)&pOld);
                    172:     SetViewportExtEx(hDC, lprcViewportOld->right, lprcViewportOld->bottom, (LPSIZE)&pOld);
                    173: 
                    174:     return nMapModeOld;
                    175:     }
                    176: 
                    177: 
                    178: 
                    179: 
                    180: 
                    181: 
                    182: /*
                    183:  * XformRectInPixelsToHimetric
                    184:  * XformRectInHimetricToPixels
                    185:  *
                    186:  * Purpose:
                    187:  *  Convert a rectangle between pixels of a given hDC and HIMETRIC units
                    188:  *  as manipulated in OLE.  If the hDC is NULL, then a screen DC is used
                    189:  *  and assumes the MM_TEXT mapping mode.
                    190:  *
                    191:  * Parameters:
                    192:  *  hDC             HDC providing reference to the pixel mapping.  If
                    193:  *                  NULL, a screen DC is used.
                    194:  *  lprcSrc         LPRECT providing the rectangle to convert.  This
                    195:  *                  contains pixels in XformRectInPixelsToHimetric and
                    196:  *                  logical HiMetric units in the complement function.
                    197:  *  lprcDst         LPRECT providing the rectangle to receive converted units.
                    198:  *                  This contains pixels in XformRectInPixelsToHimetric and
                    199:  *                  logical HiMetric units in the complement function.
                    200:  *
                    201:  * Return Value:
                    202:  *  None
                    203:  *
                    204:  * NOTE:
                    205:  *  When displaying on the screen, Window apps display everything enlarged
                    206:  *  from its actual size so that it is easier to read. For example, if an
                    207:  *  app wants to display a 1in. horizontal line, that when printed is
                    208:  *  actually a 1in. line on the printed page, then it will display the line
                    209:  *  on the screen physically larger than 1in. This is described as a line
                    210:  *  that is "logically" 1in. along the display width. Windows maintains as
                    211:  *  part of the device-specific information about a given display device:
                    212:  *      LOGPIXELSX -- no. of pixels per logical in along the display width
                    213:  *      LOGPIXELSY -- no. of pixels per logical in along the display height
                    214:  *
                    215:  *  The following formula converts a distance in pixels into its equivalent
                    216:  *  logical HIMETRIC units:
                    217:  *
                    218:  *      DistInHiMetric = (HIMETRIC_PER_INCH * DistInPix)
                    219:  *                      -------------------------------
                    220:  *                            PIXELS_PER_LOGICAL_IN
                    221:  *
                    222:  * Rect in Pixels (MM_TEXT):
                    223:  *
                    224:  *              0---------- X
                    225:  *              |
                    226:  *              |       1) ------------------ ( 2   P1 = (rc.left, rc.top)
                    227:  *              |       |                     |     P2 = (rc.right, rc.top)
                    228:  *              |       |                     |     P3 = (rc.left, rc.bottom)
                    229:  *              |       |                     |     P4 = (rc.right, rc.bottom)
                    230:  *                      |                     |
                    231:  *              Y       |                     |
                    232:  *                      3) ------------------ ( 4
                    233:  *
                    234:  *              NOTE:   Origin   = (P1x, P1y)
                    235:  *                      X extent = P4x - P1x
                    236:  *                      Y extent = P4y - P1y
                    237:  *
                    238:  *
                    239:  * Rect in Himetric (MM_HIMETRIC):
                    240:  *
                    241:  *
                    242:  *                      1) ------------------ ( 2   P1 = (rc.left, rc.top)
                    243:  *              Y       |                     |     P2 = (rc.right, rc.top)
                    244:  *                      |                     |     P3 = (rc.left, rc.bottom)
                    245:  *              |       |                     |     P4 = (rc.right, rc.bottom)
                    246:  *              |       |                     |
                    247:  *              |       |                     |
                    248:  *              |       3) ------------------ ( 4
                    249:  *              |
                    250:  *              0---------- X
                    251:  *
                    252:  *              NOTE:   Origin   = (P3x, P3y)
                    253:  *                      X extent = P2x - P3x
                    254:  *                      Y extent = P2y - P3y
                    255:  *
                    256:  *
                    257:  */
                    258: 
                    259: STDAPI_(void) XformRectInPixelsToHimetric(
                    260:     HDC hDC, LPRECT lprcPix, LPRECT lprcHiMetric)
                    261:     {
                    262:     int     iXppli;     //Pixels per logical inch along width
                    263:     int     iYppli;     //Pixels per logical inch along height
                    264:     int     iXextInPix=(lprcPix->right-lprcPix->left);
                    265:     int     iYextInPix=(lprcPix->bottom-lprcPix->top);
                    266:     BOOL    fSystemDC=FALSE;
                    267: 
                    268:     if (NULL==hDC || GetDeviceCaps(hDC, LOGPIXELSX) == 0)
                    269:         {
                    270:         hDC=GetDC(NULL);
                    271:         fSystemDC=TRUE;
                    272:         }
                    273: 
                    274:     iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
                    275:     iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
                    276: 
                    277:     //We got pixel units, convert them to logical HIMETRIC along the display
                    278:     lprcHiMetric->right = MAP_PIX_TO_LOGHIM(iXextInPix, iXppli);
                    279:     lprcHiMetric->top   = MAP_PIX_TO_LOGHIM(iYextInPix, iYppli);
                    280: 
                    281:     lprcHiMetric->left    = 0;
                    282:     lprcHiMetric->bottom  = 0;
                    283: 
                    284:     if (fSystemDC)
                    285:         ReleaseDC(NULL, hDC);
                    286: 
                    287:     return;
                    288:     }
                    289: 
                    290: 
                    291: 
                    292: STDAPI_(void) XformRectInHimetricToPixels(
                    293:     HDC hDC, LPRECT lprcHiMetric, LPRECT lprcPix)
                    294:     {
                    295:     int     iXppli;     //Pixels per logical inch along width
                    296:     int     iYppli;     //Pixels per logical inch along height
                    297:     int     iXextInHiMetric=(lprcHiMetric->right-lprcHiMetric->left);
                    298:     int     iYextInHiMetric=(lprcHiMetric->bottom-lprcHiMetric->top);
                    299:     BOOL    fSystemDC=FALSE;
                    300: 
                    301:     if (NULL==hDC || GetDeviceCaps(hDC, LOGPIXELSX) == 0)
                    302:         {
                    303:         hDC=GetDC(NULL);
                    304:         fSystemDC=TRUE;
                    305:         }
                    306: 
                    307:     iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
                    308:     iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
                    309: 
                    310:     //We got pixel units, convert them to logical HIMETRIC along the display
                    311:     lprcPix->right = MAP_LOGHIM_TO_PIX(iXextInHiMetric, iXppli);
                    312:     lprcPix->top   = MAP_LOGHIM_TO_PIX(iYextInHiMetric, iYppli);
                    313: 
                    314:     lprcPix->left  = 0;
                    315:     lprcPix->bottom= 0;
                    316: 
                    317:     if (fSystemDC)
                    318:         ReleaseDC(NULL, hDC);
                    319: 
                    320:     return;
                    321:     }
                    322: 
                    323: 
                    324: 
                    325: 
                    326: /*
                    327:  * XformSizeInPixelsToHimetric
                    328:  * XformSizeInHimetricToPixels
                    329:  *
                    330:  * Functions to convert a SIZEL structure (Size functions) or
                    331:  * an int (Width functions) between a device coordinate system and
                    332:  * logical HiMetric units.
                    333:  *
                    334:  * Parameters:
                    335:  *  hDC             HDC providing reference to the pixel mapping.  If
                    336:  *                  NULL, a screen DC is used.
                    337:  *
                    338:  *  Size Functions:
                    339:  *  lpSizeSrc       LPSIZEL providing the structure to convert.  This
                    340:  *                  contains pixels in XformSizeInPixelsToHimetric and
                    341:  *                  logical HiMetric units in the complement function.
                    342:  *  lpSizeDst       LPSIZEL providing the structure to receive converted
                    343:  *                  units.  This contains pixels in 
                    344:  *                  XformSizeInPixelsToHimetric and logical HiMetric
                    345:  *                  units in the complement function.
                    346:  *
                    347:  *  Width Functions:
                    348:  *  iWidth          int containing the value to convert.
                    349:  *
                    350:  * Return Value:
                    351:  *  Size Functions:     None
                    352:  *  Width Functions:    Converted value of the input parameters.
                    353:  *
                    354:  * NOTE:
                    355:  *  When displaying on the screen, Window apps display everything enlarged
                    356:  *  from its actual size so that it is easier to read. For example, if an
                    357:  *  app wants to display a 1in. horizontal line, that when printed is
                    358:  *  actually a 1in. line on the printed page, then it will display the line
                    359:  *  on the screen physically larger than 1in. This is described as a line
                    360:  *  that is "logically" 1in. along the display width. Windows maintains as
                    361:  *  part of the device-specific information about a given display device:
                    362:  *      LOGPIXELSX -- no. of pixels per logical in along the display width
                    363:  *      LOGPIXELSY -- no. of pixels per logical in along the display height
                    364:  *
                    365:  *  The following formula converts a distance in pixels into its equivalent
                    366:  *  logical HIMETRIC units:
                    367:  *
                    368:  *      DistInHiMetric = (HIMETRIC_PER_INCH * DistInPix)
                    369:  *                       -------------------------------
                    370:  *                           PIXELS_PER_LOGICAL_IN
                    371:  *
                    372:  */
                    373: 
                    374: STDAPI_(void) XformSizeInPixelsToHimetric(
                    375:     HDC hDC, LPSIZEL lpSizeInPix, LPSIZEL lpSizeInHiMetric)
                    376:     {
                    377:     int     iXppli;     //Pixels per logical inch along width
                    378:     int     iYppli;     //Pixels per logical inch along height
                    379:     BOOL    fSystemDC=FALSE;
                    380: 
                    381:     if (NULL==hDC || GetDeviceCaps(hDC, LOGPIXELSX) == 0)
                    382:         {
                    383:         hDC=GetDC(NULL);
                    384:         fSystemDC=TRUE;
                    385:         }
                    386: 
                    387:     iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
                    388:     iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
                    389: 
                    390:     //We got pixel units, convert them to logical HIMETRIC along the display
                    391:     lpSizeInHiMetric->cx = (long)MAP_PIX_TO_LOGHIM((int)lpSizeInPix->cx, iXppli);
                    392:     lpSizeInHiMetric->cy = (long)MAP_PIX_TO_LOGHIM((int)lpSizeInPix->cy, iYppli);
                    393: 
                    394:     if (fSystemDC)
                    395:         ReleaseDC(NULL, hDC);
                    396: 
                    397:     return;
                    398:     }
                    399: 
                    400: 
                    401: STDAPI_(void) XformSizeInHimetricToPixels(
                    402:     HDC hDC, LPSIZEL lpSizeInHiMetric, LPSIZEL lpSizeInPix)
                    403:     {
                    404:     int     iXppli;     //Pixels per logical inch along width
                    405:     int     iYppli;     //Pixels per logical inch along height
                    406:     BOOL    fSystemDC=FALSE;
                    407: 
                    408:     if (NULL==hDC || GetDeviceCaps(hDC, LOGPIXELSX) == 0)
                    409:         {
                    410:         hDC=GetDC(NULL);
                    411:         fSystemDC=TRUE;
                    412:         }
                    413: 
                    414:     iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
                    415:     iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
                    416: 
                    417:     //We got logical HIMETRIC along the display, convert them to pixel units
                    418:     lpSizeInPix->cx = (long)MAP_LOGHIM_TO_PIX((int)lpSizeInHiMetric->cx, iXppli);
                    419:     lpSizeInPix->cy = (long)MAP_LOGHIM_TO_PIX((int)lpSizeInHiMetric->cy, iYppli);
                    420: 
                    421:     if (fSystemDC)
                    422:         ReleaseDC(NULL, hDC);
                    423: 
                    424:     return;
                    425:     }
                    426: 
                    427: 
                    428: 
                    429: /* AreRectsEqual
                    430: ** -------------
                    431: **    Returns TRUE if all fields of the two Rect's are equal, 
                    432: **    else FALSE. 
                    433: */
                    434: STDAPI_(BOOL) AreRectsEqual(LPRECT lprc1, LPRECT lprc2)
                    435: {
                    436:     if ((lprc1->top == lprc2->top) &&
                    437:         (lprc1->left == lprc2->left) &&
                    438:         (lprc1->right == lprc2->right) &&
                    439:         (lprc1->bottom == lprc2->bottom))
                    440:         return TRUE;
                    441: 
                    442:     return FALSE;
                    443: }
                    444: 
                    445: 
                    446: /*
                    447:  * ParseCmdLine
                    448:  *
                    449:  * Parses the Windows command line which was passed to WinMain.
                    450:  * This function determines if the -Embedding switch has been given.
                    451:  *
                    452:  */
                    453: 
                    454: STDAPI_(void) ParseCmdLine(
                    455:     LPSTR lpszLine,
                    456:     BOOL FAR* lpfEmbedFlag,
                    457:     LPSTR szFileName)
                    458: {
                    459:     int i=0;
                    460:     char szBuf[256];
                    461: 
                    462:     if(lpfEmbedFlag)
                    463:         *lpfEmbedFlag = FALSE;
                    464:     szFileName[0]='\0';             // NULL string
                    465: 
                    466:     // skip blanks
                    467:     while(isspace(*lpszLine)) lpszLine++;
                    468: 
                    469:     if(!*lpszLine)   // No filename or options, so start a fresh document.
                    470:         return;
                    471: 
                    472:     // Check for "-Embedding" or "/Embedding" and set fEmbedding.
                    473:     if(lpfEmbedFlag && (*lpszLine == '-' || *lpszLine == '/')) {
                    474:         lpszLine++;
                    475:         lpszLine = GetWord(lpszLine, szBuf);
                    476:         *lpfEmbedFlag = !lstrcmp(szBuf, EMBEDDINGFLAG);
                    477:     }
                    478: 
                    479:     // skip blanks
                    480:     while(isspace(*lpszLine)) lpszLine++;
                    481: 
                    482:     // set szFileName to argument
                    483:     while(lpszLine[i]) {
                    484:         szFileName[i]=lpszLine[i];
                    485:         i++;
                    486:     }
                    487:     szFileName[i]='\0';
                    488: }
                    489: 
                    490: 
                    491: /* GetWord
                    492:  * -------
                    493:  *
                    494:  * LPSTR lpszSrc - Pointer to a source string
                    495:  * LPSTR lpszDst - Pointer to destination buffer
                    496:  *
                    497:  * Will copy one space-terminated or null-terminated word from the source
                    498:  * string to the destination buffer.
                    499:  * returns: pointer to next character following the word.
                    500:  */
                    501: static LPSTR GetWord(LPSTR lpszSrc, LPSTR lpszDst)
                    502: {
                    503:     while (*lpszSrc && !isspace(*lpszSrc)) 
                    504:         *lpszDst++ = *lpszSrc++;
                    505: 
                    506:     *lpszDst = '\0';
                    507:     return lpszSrc;
                    508: }
                    509: 
                    510: 
                    511: 
                    512: 
                    513: 

unix.superglobalmegacorp.com

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