Annotation of mstools/ole20/samples/ole2ui/insobj.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * INSOBJ.C
        !             3:  *
        !             4:  * Implements the OleUIInsertObject function which invokes the complete
        !             5:  * Insert Object dialog.  Makes use of the OleChangeIcon function in
        !             6:  * ICON.C.
        !             7:  *
        !             8:  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
        !             9:  */
        !            10: 
        !            11: #define STRICT  1
        !            12: #include "ole2ui.h"
        !            13: #include <commdlg.h>
        !            14: #include <memory.h>
        !            15: #include <direct.h>
        !            16: #include <malloc.h>
        !            17: #include <dos.h>
        !            18: #include "common.h"
        !            19: #include "utility.h"
        !            20: #include "icon.h"
        !            21: #include "insobj.h"
        !            22: #include "resimage.h"
        !            23: #include "iconbox.h"
        !            24: #include "geticon.h"
        !            25: 
        !            26: #define IS_FILENAME_DELIM(c)    ( (c) == '\\' || (c) == '/' || (c) == ':' )
        !            27: 
        !            28: /*
        !            29:  * OleUIInsertObject
        !            30:  *
        !            31:  * Purpose:
        !            32:  *  Invokes the standard OLE Insert Object dialog box allowing the
        !            33:  *  user to select an object source and classname as well as the option
        !            34:  *  to display the object as itself or as an icon.
        !            35:  *
        !            36:  * Parameters:
        !            37:  *  lpIO            LPOLEUIINSERTOBJECT pointing to the in-out structure
        !            38:  *                  for this dialog.
        !            39:  *
        !            40:  * Return Value:
        !            41:  *  UINT            OLEUI_SUCCESS or OLEUI_OK if all is well, otherwise
        !            42:  *                  an error value.
        !            43:  */
        !            44: 
        !            45: STDAPI_(UINT) OleUIInsertObject(LPOLEUIINSERTOBJECT lpIO)
        !            46:     {
        !            47:     UINT        uRet;
        !            48:     HGLOBAL     hMemDlg=NULL;
        !            49:     HRESULT     hrErr;
        !            50: 
        !            51:     uRet=UStandardValidation((LPOLEUISTANDARD)lpIO, sizeof(OLEUIINSERTOBJECT)
        !            52:                              , &hMemDlg);
        !            53: 
        !            54:     if (OLEUI_SUCCESS!=uRet)
        !            55:         return uRet;
        !            56: 
        !            57:     //Now we can do Insert Object specific validation.
        !            58: 
        !            59: 
        !            60:     // NULL is NOT valid for lpszFile
        !            61:     if (   (NULL == lpIO->lpszFile) 
        !            62:         || (IsBadReadPtr(lpIO->lpszFile, lpIO->cchFile))
        !            63:         || (IsBadWritePtr(lpIO->lpszFile, lpIO->cchFile)) )
        !            64:      uRet=OLEUI_IOERR_LPSZFILEINVALID;
        !            65: 
        !            66:     if (NULL != lpIO->lpszFile 
        !            67:         && (lpIO->cchFile <= 0 || lpIO->cchFile > OLEUI_CCHPATHMAX)) 
        !            68:      uRet=OLEUI_IOERR_CCHFILEINVALID;
        !            69: 
        !            70:     if (0!=lpIO->cClsidExclude)
        !            71:         {
        !            72:         if (NULL!=lpIO->lpClsidExclude && IsBadReadPtr(lpIO->lpClsidExclude
        !            73:             , lpIO->cClsidExclude*sizeof(CLSID)))
        !            74:         uRet=OLEUI_IOERR_LPCLSIDEXCLUDEINVALID;
        !            75:         }
        !            76: 
        !            77:     //If we have flags to create any object, validate necessary data.
        !            78:     if (lpIO->dwFlags & (IOF_CREATENEWOBJECT | IOF_CREATEFILEOBJECT | IOF_CREATELINKOBJECT))
        !            79:         {
        !            80:         if (NULL!=lpIO->lpFormatEtc
        !            81:             && IsBadReadPtr(lpIO->lpFormatEtc, sizeof(FORMATETC)))
        !            82:             uRet=OLEUI_IOERR_LPFORMATETCINVALID;
        !            83: 
        !            84:         if (NULL!=lpIO->ppvObj && IsBadWritePtr(lpIO->ppvObj, sizeof(LPVOID)))
        !            85:             uRet=OLEUI_IOERR_PPVOBJINVALID;
        !            86: 
        !            87:         if (NULL!=lpIO->lpIOleClientSite
        !            88:             && IsBadReadPtr(lpIO->lpIOleClientSite->lpVtbl, sizeof(IOleClientSiteVtbl)))
        !            89:             uRet=OLEUI_IOERR_LPIOLECLIENTSITEINVALID;
        !            90: 
        !            91:         if (NULL!=lpIO->lpIStorage
        !            92:             && IsBadReadPtr(lpIO->lpIStorage->lpVtbl, sizeof(IStorageVtbl)))
        !            93:             uRet=OLEUI_IOERR_LPISTORAGEINVALID;
        !            94:         }
        !            95: 
        !            96:     if (OLEUI_ERR_STANDARDMIN <= uRet)
        !            97:         {
        !            98:         if (NULL!=hMemDlg)
        !            99:             FreeResource(hMemDlg);
        !           100: 
        !           101:         return uRet;
        !           102:         }
        !           103: 
        !           104:     //Now that we've validated everything, we can invoke the dialog.
        !           105:     uRet=UStandardInvocation(InsertObjectDialogProc, (LPOLEUISTANDARD)lpIO
        !           106:                              , hMemDlg, MAKEINTRESOURCE(IDD_INSERTOBJECT));
        !           107: 
        !           108: 
        !           109:     //Stop here if we cancelled or had an error.
        !           110:     if (OLEUI_SUCCESS !=uRet && OLEUI_OK!=uRet)
        !           111:         return uRet;
        !           112: 
        !           113: 
        !           114:     /*
        !           115:      * If any of the flags specify that we're to create objects on return
        !           116:      * from this dialog, then do so.  If we encounter an error in this
        !           117:      * processing, we return OLEUI_IOERR_SCODEHASERROR.  Since the
        !           118:      * three select flags are mutually exclusive, we don't have to
        !           119:      * if...else here, just if each case (keeps things cleaner that way).
        !           120:      */
        !           121: 
        !           122:     lpIO->sc=S_OK;
        !           123: 
        !           124:     //Check if Create New was selected and we have IOF_CREATENEWOBJECT
        !           125:     if ((lpIO->dwFlags & IOF_SELECTCREATENEW) && (lpIO->dwFlags & IOF_CREATENEWOBJECT))
        !           126:         {
        !           127:         hrErr=OleCreate(&lpIO->clsid, &lpIO->iid, lpIO->oleRender
        !           128:             , lpIO->lpFormatEtc, lpIO->lpIOleClientSite, lpIO->lpIStorage
        !           129:             , lpIO->ppvObj);
        !           130:         lpIO->sc = GetScode(hrErr);
        !           131:         }
        !           132: 
        !           133:     //Try Create From File
        !           134:     if ((lpIO->dwFlags & IOF_SELECTCREATEFROMFILE))
        !           135:         {
        !           136:         if (!(lpIO->dwFlags & IOF_CHECKLINK) && (lpIO->dwFlags & IOF_CREATEFILEOBJECT))
        !           137:             {
        !           138:             hrErr=OleCreateFromFile(&CLSID_NULL, lpIO->lpszFile, &lpIO->iid
        !           139:                 , lpIO->oleRender, lpIO->lpFormatEtc, lpIO->lpIOleClientSite
        !           140:                 , lpIO->lpIStorage, lpIO->ppvObj);
        !           141:             lpIO->sc = GetScode(hrErr);
        !           142:             }
        !           143: 
        !           144:         if ((lpIO->dwFlags & IOF_CHECKLINK) && (lpIO->dwFlags & IOF_CREATELINKOBJECT))
        !           145:             {
        !           146:             hrErr=OleCreateLinkToFile(lpIO->lpszFile, &lpIO->iid
        !           147:                 , lpIO->oleRender, lpIO->lpFormatEtc, lpIO->lpIOleClientSite
        !           148:                 , lpIO->lpIStorage, lpIO->ppvObj);
        !           149:             lpIO->sc = GetScode(hrErr);
        !           150:             }
        !           151:         }
        !           152: 
        !           153:     //If we tried but failed a create option, then return the appropriate error
        !           154:     if (S_OK!=lpIO->sc)
        !           155:         uRet=OLEUI_IOERR_SCODEHASERROR;
        !           156: 
        !           157:     return uRet;
        !           158:     }
        !           159: 
        !           160: 
        !           161: 
        !           162: 
        !           163: 
        !           164: /*
        !           165:  * InsertObjectDialogProc
        !           166:  *
        !           167:  * Purpose:
        !           168:  *  Implements the OLE Insert Object dialog as invoked through the
        !           169:  *  OleUIInsertObject function.
        !           170:  */
        !           171: 
        !           172: BOOL CALLBACK EXPORT InsertObjectDialogProc(HWND hDlg, UINT iMsg
        !           173:     , WPARAM wParam, LPARAM lParam)
        !           174:     {
        !           175:     LPOLEUIINSERTOBJECT     lpOIO;
        !           176:     LPINSERTOBJECT          lpIO;
        !           177:     OLEUICHANGEICON         ci;
        !           178:     UINT                    i;
        !           179:     BOOL                    fCheck=FALSE;
        !           180:     UINT                    uRet=0;
        !           181: 
        !           182:     //Declare Win16/Win32 compatible WM_COMMAND parameters.
        !           183:     COMMANDPARAMS(wID, wCode, hWndMsg);
        !           184: 
        !           185:     //This will fail under WM_INITDIALOG, where we allocate it.
        !           186:     lpIO=(LPINSERTOBJECT)LpvStandardEntry(hDlg, iMsg, wParam, lParam, &uRet);
        !           187: 
        !           188:     //If the hook processed the message, we're done.
        !           189:     if (0!=uRet)
        !           190:         return (BOOL)uRet;
        !           191: 
        !           192:     //Process help message from Change Icon
        !           193:     if (iMsg==uMsgHelp)
        !           194:         {
        !           195:         PostMessage(lpIO->lpOIO->hWndOwner, uMsgHelp, wParam, lParam);
        !           196:         return FALSE;
        !           197:         }
        !           198: 
        !           199:     //Process the temination message
        !           200:     if (iMsg==uMsgEndDialog)
        !           201:         {
        !           202:         InsertObjectCleanup(hDlg);
        !           203:         StandardCleanup(lpIO, hDlg);
        !           204:         EndDialog(hDlg, wParam);
        !           205:         return TRUE;
        !           206:         }
        !           207: 
        !           208:     switch (iMsg)
        !           209:         {
        !           210:         case WM_INITDIALOG:
        !           211:             return FInsertObjectInit(hDlg, wParam, lParam);
        !           212: 
        !           213:         case WM_COMMAND:
        !           214:             switch (wID)
        !           215:                 {
        !           216:                 case ID_IO_CREATENEW:
        !           217:                     FToggleObjectSource(hDlg, lpIO, IOF_SELECTCREATENEW);
        !           218:                     break;
        !           219: 
        !           220:                 case ID_IO_CREATEFROMFILE:
        !           221:                     FToggleObjectSource(hDlg, lpIO, IOF_SELECTCREATEFROMFILE);
        !           222:                     break;
        !           223: 
        !           224:                 case ID_IO_LINKFILE:
        !           225:                     fCheck=IsDlgButtonChecked(hDlg, wID);
        !           226: 
        !           227:                     if (fCheck)
        !           228:                         lpIO->dwFlags |=IOF_CHECKLINK;
        !           229:                     else
        !           230:                         lpIO->dwFlags &=~IOF_CHECKLINK;
        !           231: 
        !           232:                     //Results change here, so be sure to update it.
        !           233:                     SetInsertObjectResults(hDlg, lpIO);
        !           234:                     UpdateClassIcon(hDlg, lpIO, NULL);
        !           235:                     break;
        !           236: 
        !           237:                 case ID_IO_OBJECTTYPELIST:
        !           238:                     switch (wCode)
        !           239:                         {
        !           240:                         case LBN_SELCHANGE:
        !           241:                             UpdateClassIcon(hDlg, lpIO, hWndMsg);
        !           242:                             SetInsertObjectResults(hDlg, lpIO);
        !           243:                             break;
        !           244: 
        !           245:                         case LBN_DBLCLK:
        !           246:                             //Same as pressing OK.
        !           247:                             SendCommand(hDlg, IDOK, BN_CLICKED, hWndMsg);
        !           248:                             break;
        !           249:                         }
        !           250:                     break;
        !           251: 
        !           252: 
        !           253:                 case ID_IO_FILEDISPLAY:
        !           254:                     //If there are characters, enable OK and Display As Icon
        !           255:                     if (EN_CHANGE==wCode)
        !           256:                     {
        !           257:                         lpIO->fFileDirty = TRUE;
        !           258:                         lpIO->fFileValid = FALSE;
        !           259: 
        !           260:                         lpIO->fFileSelected=
        !           261:                             (0L!=SendMessage(hWndMsg, EM_LINELENGTH, 0, 0L));
        !           262:                         EnableWindow(GetDlgItem(hDlg, ID_IO_LINKFILE), lpIO->fFileSelected);
        !           263:                         EnableWindow(GetDlgItem(hDlg, ID_IO_DISPLAYASICON), lpIO->fFileSelected);
        !           264:                         EnableWindow(GetDlgItem(hDlg, ID_IO_CHANGEICON), lpIO->fFileSelected);
        !           265:                         EnableWindow(GetDlgItem(hDlg, IDOK), lpIO->fFileSelected);
        !           266:                     }
        !           267: 
        !           268:                     if (EN_KILLFOCUS==wCode && NULL!=lpIO) 
        !           269:                     {
        !           270:                         if (FValidateInsertFile(hDlg,FALSE,&lpIO->nErrCode)) {
        !           271:                             lpIO->fFileDirty = FALSE;
        !           272:                             lpIO->fFileValid = TRUE;
        !           273:                             UpdateClassIcon(hDlg, lpIO, NULL);
        !           274:                             UpdateClassType(hDlg, lpIO, TRUE);
        !           275:                         } else {
        !           276:                             lpIO->fFileDirty = FALSE;
        !           277:                             lpIO->fFileValid = FALSE;
        !           278:                             UpdateClassType(hDlg, lpIO, FALSE);
        !           279:                         }
        !           280:                     }
        !           281:                     break;
        !           282: 
        !           283: 
        !           284:                 case ID_IO_DISPLAYASICON:
        !           285:                     fCheck=IsDlgButtonChecked(hDlg, wID);
        !           286:                     EnableWindow(GetDlgItem(hDlg, ID_IO_CHANGEICON), fCheck);
        !           287: 
        !           288:                     if (fCheck)
        !           289:                         lpIO->dwFlags |=IOF_CHECKDISPLAYASICON;
        !           290:                     else
        !           291:                         lpIO->dwFlags &=~IOF_CHECKDISPLAYASICON;
        !           292: 
        !           293:                     //Update the internal flag based on this checking
        !           294:                     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !           295:                         lpIO->fAsIconNew=fCheck;
        !           296:                     else
        !           297:                         lpIO->fAsIconFile=fCheck;
        !           298: 
        !           299:                     //Re-read the class icon on Display checked
        !           300:                     if (fCheck) 
        !           301:                     {
        !           302:                         if (lpIO->dwFlags & IOF_SELECTCREATEFROMFILE)
        !           303:                         {
        !           304:                           if (FValidateInsertFile(hDlg, TRUE,&lpIO->nErrCode))
        !           305:                           {
        !           306:                             lpIO->fFileDirty = FALSE;
        !           307:                             lpIO->fFileValid = TRUE;
        !           308:                             UpdateClassIcon(hDlg, lpIO,
        !           309:                                             GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST));
        !           310: 
        !           311:                             UpdateClassType(hDlg, lpIO, TRUE);
        !           312:                           }
        !           313: 
        !           314:                           else
        !           315:                           {
        !           316:                             HWND hWndEC;
        !           317: 
        !           318:                             lpIO->fAsIconFile= FALSE;
        !           319:                             lpIO->fFileDirty = FALSE;
        !           320:                             lpIO->fFileValid = FALSE;
        !           321:                             SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGESET, 0, 0L);
        !           322:                             UpdateClassType(hDlg, lpIO, FALSE);
        !           323: 
        !           324:                             lpIO->dwFlags &=~IOF_CHECKDISPLAYASICON;
        !           325:                             CheckDlgButton(hDlg, ID_IO_DISPLAYASICON, 0);
        !           326: 
        !           327:                             hWndEC = GetDlgItem(hDlg, ID_IO_FILEDISPLAY);
        !           328:                             SetFocus(hWndEC);
        !           329:                             SendMessage(hWndEC, EM_SETSEL, 0, MAKELPARAM(0, (WORD)-1));
        !           330:                             return TRUE;
        !           331:                           }
        !           332:                         }
        !           333:                         else
        !           334:                           UpdateClassIcon(hDlg, lpIO,
        !           335:                                           GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST));
        !           336:                     }
        !           337: 
        !           338: 
        !           339:                     //Results change here, so be sure to update it.
        !           340:                     SetInsertObjectResults(hDlg, lpIO);
        !           341: 
        !           342: 
        !           343:                     /*
        !           344:                      * Show or hide controls as appropriate.  Do the icon
        !           345:                      * display last because it will take some time to repaint.
        !           346:                      * If we do it first then the dialog looks too sluggish.
        !           347:                      */
        !           348:                     i=(fCheck) ? SW_SHOWNORMAL : SW_HIDE;
        !           349:                     StandardShowDlgItem(hDlg, ID_IO_CHANGEICON, i);
        !           350:                     StandardShowDlgItem(hDlg, ID_IO_ICONDISPLAY, i);
        !           351: 
        !           352:                     break;
        !           353: 
        !           354: 
        !           355:                 case ID_IO_CHANGEICON:
        !           356:                 {
        !           357: 
        !           358:                     LPMALLOC  pIMalloc;
        !           359:                     HWND      hList;
        !           360:                     LPSTR     pszString, pszCLSID;
        !           361: 
        !           362:                     int       iCurSel;
        !           363: 
        !           364:                     // if we're in SELECTCREATEFROMFILE mode, then we need to Validate
        !           365:                     // the contents of the edit control first.
        !           366: 
        !           367:                     if (lpIO->dwFlags & IOF_SELECTCREATEFROMFILE)
        !           368:                     {
        !           369:                        if (   lpIO->fFileDirty 
        !           370:                            && !FValidateInsertFile(hDlg, TRUE, &lpIO->nErrCode) )
        !           371:                        {
        !           372:                           HWND hWndEC;
        !           373: 
        !           374:                           lpIO->fFileDirty = TRUE;
        !           375:                           hWndEC = GetDlgItem(hDlg, ID_IO_FILEDISPLAY);
        !           376:                           SetFocus(hWndEC);
        !           377:                           SendMessage(hWndEC, EM_SETSEL, 0, MAKELPARAM(0, (WORD)-1));
        !           378:                           return TRUE;
        !           379:                        }
        !           380:                        else
        !           381:                           lpIO->fFileDirty = FALSE;
        !           382:                     }
        !           383: 
        !           384: 
        !           385: 
        !           386:                     //Initialize the structure for the hook.
        !           387:                     _fmemset((LPOLEUICHANGEICON)&ci, 0, sizeof(ci));
        !           388: 
        !           389:                     ci.hMetaPict=(HGLOBAL)SendDlgItemMessage(hDlg
        !           390:                         , ID_IO_ICONDISPLAY, IBXM_IMAGEGET, 0, 0L);
        !           391: 
        !           392:                     ci.cbStruct =sizeof(ci);
        !           393:                     ci.hWndOwner=hDlg;
        !           394:                     ci.dwFlags  =CIF_SELECTCURRENT;
        !           395: 
        !           396:                     if (lpIO->dwFlags & IOF_SHOWHELP)
        !           397:                         ci.dwFlags |= CIF_SHOWHELP;
        !           398: 
        !           399: 
        !           400: 
        !           401: 
        !           402:                     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !           403:                     {
        !           404:                        // Initialize clsid...
        !           405:                        if (NOERROR != CoGetMalloc(MEMCTX_TASK, &pIMalloc))
        !           406:                          return FALSE;
        !           407: 
        !           408:                        pszString = (LPSTR)pIMalloc->lpVtbl->Alloc(pIMalloc, OLEUI_CCHKEYMAX + OLEUI_CCHCLSIDSTRING);
        !           409: 
        !           410: 
        !           411:                        hList = GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST);
        !           412:                        iCurSel = (int)SendMessage(hList, LB_GETCURSEL, 0, 0L);
        !           413:                        SendMessage(hList, LB_GETTEXT, iCurSel, (LONG)pszString);
        !           414: 
        !           415:                        pszCLSID = PointerToNthField(pszString, 2, '\t');
        !           416: 
        !           417:                        CLSIDFromString((LPSTR)pszCLSID, (LPCLSID)&(ci.clsid));
        !           418: 
        !           419:                        pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszString);
        !           420:                        pIMalloc->lpVtbl->Release(pIMalloc);
        !           421:                     }
        !           422:                     else  // IOF_SELECTCREATEFROMFILE
        !           423:                     {
        !           424: 
        !           425:                        char  szFileName[OLEUI_CCHPATHMAX];
        !           426: 
        !           427:                        GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, (LPSTR)szFileName, OLEUI_CCHPATHMAX);
        !           428: 
        !           429:                        if (NOERROR != GetClassFile((LPSTR)szFileName, (LPCLSID)&(ci.clsid)))
        !           430:                        {  
        !           431:                           LPSTR lpszExtension;
        !           432:                           int   istrlen;
        !           433: 
        !           434:                           istrlen = lstrlen(szFileName);
        !           435: 
        !           436:                           lpszExtension = (LPSTR)szFileName + istrlen -1;
        !           437: 
        !           438:                           while ( (lpszExtension > szFileName) &&
        !           439:                                   (*lpszExtension != '.') )
        !           440:                             lpszExtension--;
        !           441: 
        !           442:                           GetAssociatedExecutable(lpszExtension, (LPSTR)ci.szIconExe);
        !           443:                           ci.cchIconExe = lstrlen(ci.szIconExe);
        !           444:                           ci.dwFlags |= CIF_USEICONEXE;
        !           445: 
        !           446:                        }
        !           447:                     }
        !           448: 
        !           449: 
        !           450:                     //Let the hook in to customize Change Icon if desired.
        !           451:                     uRet=UStandardHook(lpIO, hDlg, uMsgChangeIcon
        !           452:                         , 0, (LONG)(LPSTR)&ci);
        !           453: 
        !           454:                     if (0==uRet)
        !           455:                         uRet=(UINT)(OLEUI_OK==OleUIChangeIcon(&ci));
        !           456: 
        !           457:                     //Update the display and itemdata if necessary.
        !           458:                     if (0!=uRet)
        !           459:                     {
        !           460: 
        !           461:                         /*
        !           462:                          * OleUIChangeIcon will have already freed our
        !           463:                          * current hMetaPict that we passed in when OK is
        !           464:                          * pressed in that dialog.  So we use 0L as lParam
        !           465:                          * here so the IconBox doesn't try to free the
        !           466:                          * metafilepict again.
        !           467:                          */
        !           468:                         SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGESET
        !           469:                             , (WPARAM)ci.hMetaPict, 0L);
        !           470: 
        !           471:                         if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !           472:                           SendMessage(hList, LB_SETITEMDATA, iCurSel, MAKELPARAM(ci.hMetaPict, 0));
        !           473:                     }
        !           474:                 }
        !           475:                     break;
        !           476: 
        !           477: 
        !           478:                 case ID_IO_FILE:
        !           479:                     {
        !           480:                     /*
        !           481:                      * To allow the hook to customize the browse dialog, we
        !           482:                      * send OLEUI_MSG_BROWSE.  If the hook returns FALSE
        !           483:                      * we use the default, otherwise we trust that it retrieved
        !           484:                      * a filename for us.  This mechanism prevents hooks from
        !           485:                      * trapping ID_IO_BROWSE to customize the dialog and from
        !           486:                      * trying to figure out what we do after we have the name.
        !           487:                      */
        !           488: 
        !           489:                     char    szTemp[OLEUI_CCHPATHMAX];
        !           490:                     char    szInitialDir[OLEUI_CCHPATHMAX];
        !           491:                     DWORD   dwOfnFlags;
        !           492:                     int     nChars;
        !           493:                     BOOL    fUseInitialDir = FALSE;
        !           494: 
        !           495: 
        !           496:                     nChars = GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, (LPSTR)szTemp, OLEUI_CCHPATHMAX);
        !           497: 
        !           498:                     if (FValidateInsertFile(hDlg, FALSE, &lpIO->nErrCode))
        !           499:                     {
        !           500: 
        !           501:                         int istrlen;
        !           502: 
        !           503:                         GetFileTitle((LPSTR)szTemp, lpIO->szFile, OLEUI_CCHPATHMAX);
        !           504: 
        !           505:                         istrlen = lstrlen(lpIO->szFile);
        !           506: 
        !           507:                         lstrcpyn((LPSTR)szInitialDir, szTemp, nChars - istrlen);
        !           508:                         fUseInitialDir = TRUE;
        !           509: 
        !           510:                     }
        !           511:                     else  // file name isn't valid...lop off end of szTemp to get a
        !           512:                           // valid directory
        !           513:                     {
        !           514: #if defined( WIN32 )
        !           515:                         char szBuffer[OLEUI_CCHPATHMAX];
        !           516:                         DWORD Attribs;
        !           517: 
        !           518:                         lstrcpyn(szBuffer, szTemp, sizeof(szBuffer)-1);
        !           519:                         szBuffer[sizeof(szBuffer)-1] = '\0';
        !           520: 
        !           521:                         if ('\\' == szBuffer[nChars-1])
        !           522:                            szBuffer[nChars-1] = '\0';
        !           523: 
        !           524:                         Attribs = GetFileAttributes(szBuffer);
        !           525:                         if (Attribs != 0xffffffff && 
        !           526:                            (Attribs & FILE_ATTRIBUTE_DIRECTORY) )
        !           527:                         {
        !           528:                            lstrcpy(szInitialDir, (LPSTR)szBuffer);
        !           529:                            fUseInitialDir = TRUE;
        !           530:                         }
        !           531: #else 
        !           532:                         static char szBuffer[OLEUI_CCHPATHMAX];
        !           533:                         static int  attrib ;
        !           534: 
        !           535:                         lstrcpyn(szBuffer, szTemp, sizeof(szBuffer)-1);
        !           536:                         szBuffer[sizeof(szBuffer)-1] = '\0';
        !           537: 
        !           538:                         if ('\\' == szBuffer[nChars-1])
        !           539:                            szBuffer[nChars-1] = '\0';
        !           540: 
        !           541:                         AnsiToOem(szBuffer, szBuffer);
        !           542:                         if(0 == _dos_getfileattr(szBuffer, &attrib))
        !           543:                         {
        !           544:                            lstrcpy(szInitialDir, (LPSTR)szBuffer);
        !           545:                            fUseInitialDir = TRUE;
        !           546:                         }
        !           547: #endif
        !           548:                         *lpIO->szFile = '\0';
        !           549:                     }
        !           550: 
        !           551:                     uRet=UStandardHook(lpIO, hDlg, uMsgBrowse
        !           552:                         , OLEUI_CCHPATHMAX, (LPARAM)(LPSTR)lpIO->szFile);
        !           553: 
        !           554:                     dwOfnFlags = OFN_FILEMUSTEXIST;
        !           555: 
        !           556: // dont show the help button: easiest fix for #4567, since there is no hook proc
        !           557: //                    if (lpIO->lpOIO->dwFlags & IOF_SHOWHELP)
        !           558: //                       dwOfnFlags |= OFN_SHOWHELP;
        !           559: 
        !           560:                     if (0==uRet)
        !           561:                         uRet=(UINT)Browse(hDlg, 
        !           562:                                           lpIO->szFile, 
        !           563:                                           fUseInitialDir ? (LPSTR)szInitialDir : NULL, 
        !           564:                                           OLEUI_CCHPATHMAX, 
        !           565:                                           IDS_FILTERS, 
        !           566:                                           dwOfnFlags);
        !           567: 
        !           568:                     //Only update if the file changed.
        !           569:                     if (0!=uRet && 0!=lstrcmpi(szTemp, lpIO->szFile))
        !           570:                     {
        !           571:                         SetDlgItemText(hDlg, ID_IO_FILEDISPLAY, lpIO->szFile);
        !           572:                         lpIO->fFileSelected=TRUE;
        !           573: 
        !           574:                         if (FValidateInsertFile(hDlg, TRUE, &lpIO->nErrCode))
        !           575:                         {
        !           576:                           lpIO->fFileDirty = FALSE;
        !           577:                           lpIO->fFileValid = TRUE;
        !           578:                           UpdateClassIcon(hDlg, lpIO, NULL);
        !           579:                           UpdateClassType(hDlg, lpIO, TRUE);
        !           580:                         }
        !           581:                         else  // filename is invalid - set focus back to ec
        !           582:                         {
        !           583:                           HWND hWnd;
        !           584: 
        !           585:                           lpIO->fFileDirty = FALSE;
        !           586:                           lpIO->fFileValid = FALSE;
        !           587:                           hWnd = GetDlgItem(hDlg, ID_IO_FILEDISPLAY);
        !           588:                           SetFocus(hWnd);
        !           589:                           SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(0, (WORD)-1));
        !           590:                         }
        !           591: 
        !           592:                         //Once we have a file, Display As Icon is always enabled
        !           593:                         EnableWindow(GetDlgItem(hDlg, ID_IO_DISPLAYASICON), TRUE);
        !           594: 
        !           595:                         //As well as OK
        !           596:                         EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
        !           597: 
        !           598:                     }
        !           599:                 }
        !           600:                 break;
        !           601: 
        !           602: 
        !           603:                 case IDOK:
        !           604:                 {
        !           605:                     HWND    hListBox;
        !           606:                     WORD    iCurSel;
        !           607:                     char    szBuffer[OLEUI_CCHKEYMAX + OLEUI_CCHCLSIDSTRING];  
        !           608:                     LPSTR   lpszCLSID;
        !           609: 
        !           610:                     if ((HWND)(LOWORD(lParam)) != GetFocus())
        !           611:                       SetFocus((HWND)(LOWORD(lParam)));
        !           612: 
        !           613: 
        !           614: 
        !           615:                     // If the file name is clean (already validated), or
        !           616:                     // if Create New is selected, then we can skip this part.
        !           617: 
        !           618:                     if (  (lpIO->dwFlags & IOF_SELECTCREATEFROMFILE)  
        !           619:                        && (TRUE == lpIO->fFileDirty) )
        !           620:                     {
        !           621: 
        !           622:                         if (FValidateInsertFile(hDlg, TRUE, &lpIO->nErrCode))
        !           623:                         {
        !           624:                           lpIO->fFileDirty = FALSE;
        !           625:                           lpIO->fFileValid = TRUE;
        !           626:                           UpdateClassIcon(hDlg, lpIO, NULL);
        !           627:                           UpdateClassType(hDlg, lpIO, TRUE);
        !           628:                         }
        !           629:                         else  // filename is invalid - set focus back to ec
        !           630:                         {
        !           631:                           HWND hWnd;
        !           632: 
        !           633:                           lpIO->fFileDirty = FALSE;
        !           634:                           lpIO->fFileValid = FALSE;
        !           635:                           hWnd = GetDlgItem(hDlg, ID_IO_FILEDISPLAY);
        !           636:                           SetFocus(hWnd);
        !           637:                           SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(0, (WORD)-1));
        !           638:                           UpdateClassType(hDlg, lpIO, FALSE);
        !           639:                         }
        !           640:                         
        !           641:                         return TRUE;  // eat this message
        !           642:                     }
        !           643:                     else if (  (lpIO->dwFlags & IOF_SELECTCREATEFROMFILE)  
        !           644:                        && (FALSE == lpIO->fFileValid) )
        !           645:                     {
        !           646:                         // filename is invalid - set focus back to ec
        !           647:                         HWND hWnd;
        !           648:                         char        szFile[OLEUI_CCHPATHMAX];
        !           649: 
        !           650:                         if (0!=GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, 
        !           651:                                             szFile, sizeof(szFile)))
        !           652:                         {
        !           653:                             OpenFileError(hDlg, lpIO->nErrCode, szFile);
        !           654:                         }
        !           655:                         lpIO->fFileDirty = FALSE;
        !           656:                         lpIO->fFileValid = FALSE;
        !           657:                         hWnd = GetDlgItem(hDlg, ID_IO_FILEDISPLAY);
        !           658:                         SetFocus(hWnd);
        !           659:                         SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(0, (WORD)-1));
        !           660:                         UpdateClassType(hDlg, lpIO, FALSE);
        !           661:                         return TRUE;  // eat this message
        !           662:                     }
        !           663: 
        !           664:                     //Copy the necessary information back to the original struct
        !           665:                     lpOIO=lpIO->lpOIO;
        !           666:                     lpOIO->dwFlags=lpIO->dwFlags;
        !           667: 
        !           668:                     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !           669:                     {
        !           670:                        hListBox=GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST);
        !           671:                        iCurSel=(WORD)SendMessage(hListBox, LB_GETCURSEL, 0, 0);
        !           672: 
        !           673:                        if (lpIO->dwFlags & IOF_CHECKDISPLAYASICON)
        !           674:                        {
        !           675:                            lpOIO->hMetaPict=(HGLOBAL)SendMessage(hListBox, 
        !           676:                                LB_GETITEMDATA, iCurSel, 0L);
        !           677: 
        !           678:                            /*
        !           679:                             * Set the item data to 0 here so that the cleanup
        !           680:                             * code doesn't delete the metafile.
        !           681:                             */
        !           682:                            SendMessage(hListBox, LB_SETITEMDATA, iCurSel, 0L);
        !           683:                        }
        !           684:                        else
        !           685:                            lpOIO->hMetaPict = (HGLOBAL)NULL;
        !           686: 
        !           687:                        SendMessage(hListBox, LB_GETTEXT, iCurSel
        !           688:                            , (LPARAM)(LPSTR)szBuffer);
        !           689: 
        !           690:                        lpszCLSID=PointerToNthField((LPSTR)szBuffer, 2, '\t');
        !           691:                        CLSIDFromString(lpszCLSID, &lpOIO->clsid);
        !           692: 
        !           693:                     }
        !           694:                     else  // IOF_SELECTCREATEFROMFILE
        !           695:                     {  
        !           696:                        if (lpIO->dwFlags & IOF_CHECKDISPLAYASICON)
        !           697:                        {
        !           698:                              // get metafile here
        !           699:                           lpOIO->hMetaPict = (HGLOBAL)SendDlgItemMessage(hDlg, 
        !           700:                                                                          ID_IO_ICONDISPLAY, 
        !           701:                                                                          IBXM_IMAGEGET, 
        !           702:                                                                          0, 0L);
        !           703: 
        !           704: 
        !           705:                        }
        !           706:                        else
        !           707:                          lpOIO->hMetaPict = (HGLOBAL)NULL;
        !           708: 
        !           709:                     }
        !           710: 
        !           711:                        GetDlgItemText(hDlg, ID_IO_FILEDISPLAY,
        !           712:                                       lpIO->szFile, lpOIO->cchFile);
        !           713: 
        !           714:                        lstrcpyn(lpOIO->lpszFile, lpIO->szFile, lpOIO->cchFile);
        !           715: 
        !           716:                        SendMessage(hDlg, uMsgEndDialog, OLEUI_OK, 0L);
        !           717:                 }
        !           718:                 break;
        !           719: 
        !           720:                 case IDCANCEL:
        !           721:                     SendMessage(hDlg, uMsgEndDialog, OLEUI_CANCEL, 0L);
        !           722:                     break;
        !           723: 
        !           724:                 case ID_OLEUIHELP:
        !           725:                     PostMessage(lpIO->lpOIO->hWndOwner, uMsgHelp
        !           726:                                 , (WPARAM)hDlg, MAKELPARAM(IDD_INSERTOBJECT, 0));
        !           727:                     break;
        !           728:                 }
        !           729:             break;
        !           730:         }
        !           731: 
        !           732:     return FALSE;
        !           733:     }
        !           734: 
        !           735: 
        !           736: 
        !           737: 
        !           738: /*
        !           739:  * FInsertObjectInit
        !           740:  *
        !           741:  * Purpose:
        !           742:  *  WM_INITIDIALOG handler for the Insert Object dialog box.
        !           743:  *
        !           744:  * Parameters:
        !           745:  *  hDlg            HWND of the dialog
        !           746:  *  wParam          WPARAM of the message
        !           747:  *  lParam          LPARAM of the message
        !           748:  *
        !           749:  * Return Value:
        !           750:  *  BOOL            Value to return for WM_INITDIALOG.
        !           751:  */
        !           752: 
        !           753: BOOL FInsertObjectInit(HWND hDlg, WPARAM wParam, LPARAM lParam)
        !           754:     {
        !           755:     LPOLEUIINSERTOBJECT     lpOIO;
        !           756:     LPINSERTOBJECT          lpIO;
        !           757:     RECT                    rc;
        !           758:     DWORD                   dw;
        !           759:     HFONT                   hFont;
        !           760:     HWND                    hList;
        !           761:     UINT                    u;
        !           762:     BOOL                    fCheck;
        !           763:     char                   *pch;
        !           764: 
        !           765:     //1.  Copy the structure at lParam into our instance memory.
        !           766:     lpIO=(LPINSERTOBJECT)LpvStandardInit(hDlg, sizeof(INSERTOBJECT), TRUE, &hFont);
        !           767: 
        !           768:     //PvStandardInit send a termination to us already.
        !           769:     if (NULL==lpIO)
        !           770:         return FALSE;
        !           771: 
        !           772:     lpOIO=(LPOLEUIINSERTOBJECT)lParam;
        !           773: 
        !           774:     //2.  Save the original pointer and copy necessary information.
        !           775:     lpIO->lpOIO  =lpOIO;
        !           776:     lpIO->dwFlags=lpOIO->dwFlags;
        !           777:     lpIO->clsid  =lpOIO->clsid;
        !           778: 
        !           779:     if ( (lpOIO->lpszFile) && ('\0' != *lpOIO->lpszFile) )
        !           780:         lstrcpyn((LPSTR)lpIO->szFile, lpOIO->lpszFile, OLEUI_CCHPATHMAX);
        !           781:     else 
        !           782:         *(lpIO->szFile) = '\0';
        !           783: 
        !           784:     lpIO->hMetaPictFile = (HGLOBAL)NULL;
        !           785: 
        !           786:     //3.  If we got a font, send it to the necessary controls.
        !           787:     if (NULL!=hFont)
        !           788:         {
        !           789:         SendDlgItemMessage(hDlg, ID_IO_RESULTTEXT,  WM_SETFONT, (WPARAM)hFont, 0L);
        !           790:         SendDlgItemMessage(hDlg, ID_IO_FILETYPE,  WM_SETFONT, (WPARAM)hFont, 0L);
        !           791:         }
        !           792: 
        !           793: 
        !           794:     //4.  Fill the Object Type listbox with entries from the reg DB.
        !           795:     hList=GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST);
        !           796:     UFillClassList(hList, lpOIO->cClsidExclude, lpOIO->lpClsidExclude
        !           797:         , (BOOL)(lpOIO->dwFlags & IOF_VERIFYSERVERSEXIST));
        !           798: 
        !           799:     //Set the tab width in the list to push all the tabs off the side.
        !           800:     GetClientRect(hList, &rc);
        !           801:     dw=GetDialogBaseUnits();
        !           802:     rc.right =(8*rc.right)/LOWORD(dw);  //Convert pixels to 2x dlg units.
        !           803:     SendMessage(hList, LB_SETTABSTOPS, 1, (LPARAM)(LPINT)&rc.right);
        !           804: 
        !           805: 
        !           806:     //5. Initilize the file name display to cwd if we don't have any name.
        !           807:     if ('\0' == *(lpIO->szFile))
        !           808:     {
        !           809:          pch=_getcwd(NULL, OLEUI_CCHPATHMAX);
        !           810:          if (*(pch+lstrlen(pch)-1) != '\\')
        !           811:             lstrcat(pch, "\\");  // put slash on end of cwd
        !           812:          SetDlgItemText(hDlg, ID_IO_FILEDISPLAY, pch);
        !           813:          lpIO->fFileDirty = TRUE;  // cwd is not a valid filename
        !           814:          #ifndef __TURBOC__
        !           815:          free(pch);
        !           816:          #endif
        !           817:     }
        !           818:     else
        !           819:     {
        !           820:         SetDlgItemText(hDlg, ID_IO_FILEDISPLAY, lpIO->szFile);
        !           821: 
        !           822:         if (FValidateInsertFile(hDlg, FALSE, &lpIO->nErrCode))
        !           823:           lpIO->fFileDirty = FALSE;
        !           824:         else
        !           825:           lpIO->fFileDirty = TRUE;
        !           826:     }
        !           827: 
        !           828: 
        !           829:     //6.  Initialize the selected type radiobutton.
        !           830:     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !           831:     {
        !           832:         StandardShowDlgItem(hDlg, ID_IO_FILETEXT, SW_HIDE);
        !           833:         StandardShowDlgItem(hDlg, ID_IO_FILETYPE, SW_HIDE);
        !           834:         StandardShowDlgItem(hDlg, ID_IO_FILEDISPLAY, SW_HIDE);
        !           835:         StandardShowDlgItem(hDlg, ID_IO_FILE, SW_HIDE);
        !           836:         StandardShowDlgItem(hDlg, ID_IO_LINKFILE, SW_HIDE);
        !           837: 
        !           838:         CheckRadioButton(hDlg, ID_IO_CREATENEW, ID_IO_CREATEFROMFILE, ID_IO_CREATENEW);
        !           839: 
        !           840:         lpIO->fAsIconNew=(0L!=(lpIO->dwFlags & IOF_CHECKDISPLAYASICON));
        !           841:         SetFocus(hList);
        !           842:     }
        !           843:     else
        !           844:     {
        !           845:         /*
        !           846:          * Use pszType as the initial File.  If there's no initial
        !           847:          * file then we have to remove any check from Display As
        !           848:          * Icon.  We also check Link if so indicated for this option.
        !           849:          */
        !           850:         StandardShowDlgItem(hDlg, ID_IO_OBJECTTYPELIST, SW_HIDE);
        !           851:         StandardShowDlgItem(hDlg, ID_IO_OBJECTTYPETEXT, SW_HIDE);
        !           852: 
        !           853:         // Don't preselect display as icon if the filename isn't valid
        !           854:         if (TRUE == lpIO->fFileDirty)
        !           855:             lpIO->dwFlags &= ~(IOF_CHECKDISPLAYASICON);
        !           856: 
        !           857:         if (IOF_DISABLELINK & lpIO->dwFlags)
        !           858:             StandardShowDlgItem(hDlg, ID_IO_LINKFILE, SW_HIDE);
        !           859:         else
        !           860:         {
        !           861:             CheckDlgButton(hDlg, ID_IO_LINKFILE
        !           862:                 , (BOOL)(0L!=(lpIO->dwFlags & IOF_CHECKLINK)));
        !           863:         }
        !           864: 
        !           865:         CheckRadioButton(hDlg, ID_IO_CREATENEW, ID_IO_CREATEFROMFILE, ID_IO_CREATEFROMFILE);
        !           866: 
        !           867:         lpIO->fAsIconFile=(0L!=(lpIO->dwFlags & IOF_CHECKDISPLAYASICON));
        !           868:         SetFocus(GetDlgItem(hDlg, ID_IO_FILEDISPLAY));
        !           869:     }
        !           870: 
        !           871: 
        !           872:     //7.  Initialize the Display as Icon state
        !           873:     fCheck=(BOOL)(lpIO->dwFlags & IOF_CHECKDISPLAYASICON);
        !           874:     u=fCheck ? SW_SHOWNORMAL : SW_HIDE;
        !           875: 
        !           876:     StandardShowDlgItem(hDlg, ID_IO_CHANGEICON, u);
        !           877:     StandardShowDlgItem(hDlg, ID_IO_ICONDISPLAY, u);
        !           878: 
        !           879:     CheckDlgButton(hDlg, ID_IO_DISPLAYASICON, fCheck);
        !           880: 
        !           881: 
        !           882:     //8.  Show or hide the help button
        !           883:     if (!(lpIO->dwFlags & IOF_SHOWHELP))
        !           884:         StandardShowDlgItem(hDlg, ID_OLEUIHELP, SW_HIDE);
        !           885: 
        !           886: 
        !           887:     //9.  Initialize the result display
        !           888:     UpdateClassIcon(hDlg, lpIO, GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST));
        !           889:     SetInsertObjectResults(hDlg, lpIO);
        !           890: 
        !           891:     //10.  Change the caption
        !           892:     if (NULL!=lpOIO->lpszCaption)
        !           893:         SetWindowText(hDlg, lpOIO->lpszCaption);
        !           894: 
        !           895: 
        !           896:     //All Done:  call the hook with lCustData
        !           897:     UStandardHook(lpIO, hDlg, WM_INITDIALOG, wParam, lpOIO->lCustData);
        !           898: 
        !           899:     /*
        !           900:      * We either set focus to the listbox or the edit control.  In either
        !           901:      * case we don't want Windows to do any SetFocus, so we return FALSE.
        !           902:      */
        !           903:     return FALSE;
        !           904:     }
        !           905: 
        !           906: 
        !           907: 
        !           908: 
        !           909: 
        !           910: 
        !           911: /*
        !           912:  * UFillClassList
        !           913:  *
        !           914:  * Purpose:
        !           915:  *  Enumerates available OLE object classes from the registration
        !           916:  *  database and fills a listbox with those names.
        !           917:  *
        !           918:  *  Note that this function removes any prior contents of the listbox.
        !           919:  *
        !           920:  * Parameters:
        !           921:  *  hList           HWND to the listbox to fill.
        !           922:  *  cIDEx           UINT number of CLSIDs to exclude in lpIDEx
        !           923:  *  lpIDEx          LPCLSID to CLSIDs to leave out of the listbox.
        !           924:  *  fVerify         BOOL indicating if we are to validate existence of
        !           925:  *                  servers before putting them in the list.
        !           926:  *
        !           927:  * Return Value:
        !           928:  *  UINT            Number of strings added to the listbox, -1 on failure.
        !           929:  */
        !           930: 
        !           931: UINT UFillClassList(HWND hList, UINT cIDEx, LPCLSID lpIDEx, BOOL fVerify)
        !           932:     {
        !           933:     DWORD       dw;
        !           934:     UINT        cStrings=0;
        !           935:     UINT        i;
        !           936:     UINT        cch;
        !           937:     HKEY        hKey;
        !           938:     LONG        lRet;
        !           939:     HFILE       hFile;
        !           940:     OFSTRUCT    of;
        !           941:     BOOL        fExclude;
        !           942:     LPMALLOC    pIMalloc;
        !           943:     LPSTR       pszExec;
        !           944:     LPSTR       pszClass;
        !           945:     LPSTR       pszKey;
        !           946:     LPSTR       pszID;
        !           947:     CLSID       clsid;
        !           948: 
        !           949:     //Get some work buffers
        !           950:     if (NOERROR!=CoGetMalloc(MEMCTX_TASK, &pIMalloc))
        !           951:         return (UINT)-1;
        !           952: 
        !           953:     pszExec=(LPSTR)pIMalloc->lpVtbl->Alloc(pIMalloc, OLEUI_CCHKEYMAX*4);
        !           954: 
        !           955:     if (NULL==pszExec)
        !           956:         {
        !           957:         pIMalloc->lpVtbl->Release(pIMalloc);
        !           958:         return (UINT)-1;
        !           959:         }
        !           960: 
        !           961:     pszClass=pszExec+OLEUI_CCHKEYMAX;
        !           962:     pszKey=pszClass+OLEUI_CCHKEYMAX;
        !           963:     pszID=pszKey+OLEUI_CCHKEYMAX;
        !           964: 
        !           965:     //Open up the root key.
        !           966:     lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
        !           967: 
        !           968:     if ((LONG)ERROR_SUCCESS!=lRet)
        !           969:         {
        !           970:         pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszExec);
        !           971:         pIMalloc->lpVtbl->Release(pIMalloc);
        !           972:         return (UINT)-1;
        !           973:         }
        !           974: 
        !           975:     //Clean out the existing strings.
        !           976:     SendMessage(hList, LB_RESETCONTENT, 0, 0L);
        !           977: 
        !           978:     cStrings=0;
        !           979: 
        !           980:     while (TRUE)
        !           981:         {
        !           982:         lRet=RegEnumKey(hKey, cStrings++, pszClass, OLEUI_CCHKEYMAX);
        !           983: 
        !           984:         if ((LONG)ERROR_SUCCESS!=lRet)
        !           985:             break;
        !           986: 
        !           987:         //Cheat on lstrcat by using lstrcpy after this string, saving time
        !           988:         cch=lstrlen(pszClass);
        !           989: 
        !           990:         //Check for a \protocol\StdFileEditing\server entry.
        !           991:         lstrcpy(pszClass+cch, "\\protocol\\StdFileEditing\\server");
        !           992: 
        !           993:         dw=OLEUI_CCHKEYMAX;
        !           994:         lRet=RegQueryValue(hKey, pszClass, pszKey, &dw);
        !           995: 
        !           996:         if ((LONG)ERROR_SUCCESS==lRet) 
        !           997:             {
        !           998:             /*
        !           999:             * Check if the EXE actually exists.  By default we don't do this
        !          1000:             * to bring up the dialog faster.  If an application wants to be
        !          1001:             * stringent, they can provide IOF_VERIFYSERVERSEXIST.
        !          1002:             */
        !          1003: 
        !          1004:             hFile = !HFILE_ERROR;
        !          1005: 
        !          1006:             if (fVerify) 
        !          1007:                 hFile=OpenFile(pszKey, &of, OF_EXIST);
        !          1008: 
        !          1009:             if (HFILE_ERROR!=hFile)
        !          1010:                 {
        !          1011:                 dw=OLEUI_CCHKEYMAX;
        !          1012:                 *(pszClass+cch)=0;  // set back to rootkey
        !          1013:                 // Get full user type name
        !          1014:                 lRet=RegQueryValue(hKey, pszClass, pszKey, &dw);
        !          1015: 
        !          1016:                 if ((LONG)ERROR_SUCCESS!=lRet)
        !          1017:                     continue;   // error getting type name--skip this class
        !          1018: 
        !          1019:                 //Tell the code below to get the string for us.
        !          1020:                 pszID=NULL;
        !          1021:                 }
        !          1022:             }
        !          1023:         else
        !          1024:             {
        !          1025:             /*
        !          1026:              * No \protocol\StdFileEditing\server entry.  Look to see if
        !          1027:              * there's an Insertable entry.  If there is, then use the
        !          1028:              * Clsid to look at CLSID\clsid\LocalServer and \InprocServer
        !          1029:              */
        !          1030: 
        !          1031:             lstrcpy(pszClass+cch, "\\Insertable");
        !          1032: 
        !          1033:             dw=OLEUI_CCHKEYMAX;
        !          1034:             lRet=RegQueryValue(hKey, pszClass, pszKey, &dw);
        !          1035: 
        !          1036:             if ((LONG)ERROR_SUCCESS!=lRet)
        !          1037:                 continue;   // Insertable NOT found--skip this class
        !          1038: 
        !          1039:             //Get memory for pszID
        !          1040:             pszID=pIMalloc->lpVtbl->Alloc(pIMalloc, OLEUI_CCHKEYMAX);
        !          1041: 
        !          1042:             if (NULL==pszID)
        !          1043:                 continue;
        !          1044: 
        !          1045:             *(pszClass+cch)=0;  // set back to rootkey
        !          1046:             lstrcat(pszClass+cch, "\\CLSID");
        !          1047: 
        !          1048:             dw=OLEUI_CCHKEYMAX;
        !          1049:             lRet=RegQueryValue(hKey, pszClass, pszID, &dw);
        !          1050: 
        !          1051:             if ((LONG)ERROR_SUCCESS!=lRet)
        !          1052:                 continue;   // CLSID subkey not found
        !          1053: 
        !          1054:             lstrcpy(pszExec, "CLSID\\");
        !          1055:             lstrcat(pszExec, pszID);
        !          1056: 
        !          1057:             //CLSID\ is 6, dw contains pszID length.
        !          1058:             cch=6+(UINT)dw;
        !          1059: 
        !          1060:             lstrcpy(pszExec+cch, "\\LocalServer");
        !          1061:             dw=OLEUI_CCHKEYMAX;
        !          1062:             lRet=RegQueryValue(hKey, pszExec, pszKey, &dw);
        !          1063: 
        !          1064:             if ((LONG)ERROR_SUCCESS!=lRet)
        !          1065:                 {
        !          1066:                 //Try InprocServer
        !          1067:                 lstrcpy(pszExec+cch, "\\InProcServer");
        !          1068:                 dw=OLEUI_CCHKEYMAX;
        !          1069:                 lRet=RegQueryValue(hKey, pszExec, pszKey, &dw);
        !          1070: 
        !          1071:                 if ((LONG)ERROR_SUCCESS!=lRet)
        !          1072:                 continue;
        !          1073:                 }
        !          1074: 
        !          1075:             if (fVerify)
        !          1076:                 {
        !          1077:                 if (HFILE_ERROR==OpenFile(pszKey, &of, OF_EXIST))
        !          1078:                     continue;
        !          1079:                 }
        !          1080: 
        !          1081:             dw=OLEUI_CCHKEYMAX;
        !          1082:             lRet=RegQueryValue(hKey, pszExec, pszKey, &dw);
        !          1083:             *(pszExec+cch)=0;   //Remove \\*Server
        !          1084: 
        !          1085:             if ((LONG)ERROR_SUCCESS!=lRet)
        !          1086:                 continue;
        !          1087:             }
        !          1088: 
        !          1089:         //Get CLSID to add to listbox.
        !          1090:         if (NULL==pszID)
        !          1091:             {
        !          1092:             CLSIDFromProgID(pszClass, &clsid);
        !          1093:             StringFromCLSID(&clsid, &pszID);
        !          1094:             }
        !          1095:         else
        !          1096:             CLSIDFromString(pszID, &clsid);
        !          1097: 
        !          1098:         //Check if this CLSID is in the exclusion list.
        !          1099:         fExclude=FALSE;
        !          1100: 
        !          1101:         for (i=0; i < cIDEx; i++)
        !          1102:             {
        !          1103:             if (IsEqualCLSID(&clsid, (LPCLSID)(lpIDEx+i)))
        !          1104:                 {
        !          1105:                 fExclude=TRUE;
        !          1106:                 break;
        !          1107:                 }
        !          1108:             }
        !          1109: 
        !          1110:         if (fExclude)
        !          1111:             continue;
        !          1112: 
        !          1113:         //We go through all the conditions, add the string.
        !          1114:         lstrcat(pszKey, "\t");
        !          1115:         lstrcat(pszKey, pszID);
        !          1116:         SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)pszKey);
        !          1117: 
        !          1118:         //We always allocated this regardless of the path
        !          1119:         pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszID);
        !          1120:         }
        !          1121: 
        !          1122: 
        !          1123:     //Select the first item by default
        !          1124:     SendMessage(hList, LB_SETCURSEL, 0, 0L);
        !          1125:     RegCloseKey(hKey);
        !          1126: 
        !          1127:     pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszExec);
        !          1128:     pIMalloc->lpVtbl->Release(pIMalloc);
        !          1129: 
        !          1130:     return cStrings;
        !          1131:     }
        !          1132: 
        !          1133: 
        !          1134: 
        !          1135: 
        !          1136: 
        !          1137: /*
        !          1138:  * FToggleObjectSource
        !          1139:  *
        !          1140:  * Purpose:
        !          1141:  *  Handles enabling, disabling, showing, and flag manipulation when the
        !          1142:  *  user changes between Create New, Insert File, and Link File in the
        !          1143:  *  Insert Object dialog.
        !          1144:  *
        !          1145:  * Parameters:
        !          1146:  *  hDlg            HWND of the dialog
        !          1147:  *  lpIO            LPINSERTOBJECT pointing to the dialog structure
        !          1148:  *  dwOption        DWORD flag indicating the option just selected:
        !          1149:  *                  IOF_SELECTCREATENEW or IOF_SELECTCREATEFROMFILE
        !          1150:  *
        !          1151:  * Return Value:
        !          1152:  *  BOOL            TRUE if the option was already selected, FALSE otherwise.
        !          1153:  */
        !          1154: 
        !          1155: BOOL FToggleObjectSource(HWND hDlg, LPINSERTOBJECT lpIO, DWORD dwOption)
        !          1156:     {
        !          1157:     BOOL        fTemp;
        !          1158:     UINT        uTemp;
        !          1159:     DWORD       dwTemp;
        !          1160:     int         i;
        !          1161: 
        !          1162:     //Skip all of this if we're already selected.
        !          1163:     if (lpIO->dwFlags & dwOption)
        !          1164:         return TRUE;
        !          1165: 
        !          1166: 
        !          1167:     // if we're switching from "from file" to "create new" and we've got
        !          1168:     // an icon for "from file", then we need to save it so that we can 
        !          1169:     // show it if the user reselects "from file".
        !          1170: 
        !          1171:     if ( (IOF_SELECTCREATENEW == dwOption) &&
        !          1172:          (lpIO->dwFlags & IOF_CHECKDISPLAYASICON) )
        !          1173:       lpIO->hMetaPictFile = (HGLOBAL)SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGEGET, 0, 0L);
        !          1174: 
        !          1175:     /*
        !          1176:      * 1.  Change the Display As Icon checked state to reflect the
        !          1177:      *     selection for this option, stored in the fAsIcon* flags.
        !          1178:      */
        !          1179:     fTemp=(IOF_SELECTCREATENEW==dwOption) ? lpIO->fAsIconNew : lpIO->fAsIconFile;
        !          1180: 
        !          1181:     if (fTemp)
        !          1182:         lpIO->dwFlags |=IOF_CHECKDISPLAYASICON;
        !          1183:     else
        !          1184:         lpIO->dwFlags &=~IOF_CHECKDISPLAYASICON;
        !          1185: 
        !          1186:     CheckDlgButton(hDlg, ID_IO_DISPLAYASICON
        !          1187:          , (BOOL)(0L!=(lpIO->dwFlags & IOF_CHECKDISPLAYASICON)));
        !          1188: 
        !          1189:     EnableWindow(GetDlgItem(hDlg, ID_IO_CHANGEICON), fTemp);
        !          1190: 
        !          1191:     /*
        !          1192:      * 2.  Display Icon:  Enabled on Create New or on Create from File if
        !          1193:      *     there is a selected file.
        !          1194:      */
        !          1195:     fTemp=(IOF_SELECTCREATENEW==dwOption) ? TRUE : lpIO->fFileSelected;
        !          1196:     EnableWindow(GetDlgItem(hDlg, ID_IO_DISPLAYASICON), fTemp);
        !          1197: 
        !          1198:     //OK and Link follow the same enabling as Display As Icon.
        !          1199:     EnableWindow(GetDlgItem(hDlg, IDOK), fTemp);
        !          1200:     EnableWindow(GetDlgItem(hDlg, ID_IO_LINKFILE), fTemp);
        !          1201: 
        !          1202:     //3.  Enable Browse... when Create from File is selected.
        !          1203:     fTemp=(IOF_SELECTCREATENEW==dwOption);
        !          1204:     EnableWindow(GetDlgItem(hDlg, ID_IO_FILE),        !fTemp);
        !          1205:     EnableWindow(GetDlgItem(hDlg, ID_IO_FILEDISPLAY), !fTemp);
        !          1206: 
        !          1207:     /*
        !          1208:      * 4.  Switch between Object Type listbox on Create New and
        !          1209:      *     file buttons on others.
        !          1210:      */
        !          1211:     uTemp=(fTemp) ? SW_SHOWNORMAL : SW_HIDE;
        !          1212:     StandardShowDlgItem(hDlg, ID_IO_OBJECTTYPELIST, uTemp);
        !          1213:     StandardShowDlgItem(hDlg, ID_IO_OBJECTTYPETEXT, uTemp);
        !          1214: 
        !          1215:     uTemp=(fTemp) ? SW_HIDE : SW_SHOWNORMAL;
        !          1216:     StandardShowDlgItem(hDlg, ID_IO_FILETEXT, uTemp);
        !          1217:     StandardShowDlgItem(hDlg, ID_IO_FILETYPE, uTemp);
        !          1218:     StandardShowDlgItem(hDlg, ID_IO_FILEDISPLAY, uTemp);
        !          1219:     StandardShowDlgItem(hDlg, ID_IO_FILE, uTemp);
        !          1220: 
        !          1221:     //Link is always hidden if IOF_DISABLELINK is set.
        !          1222:     if (IOF_DISABLELINK & lpIO->dwFlags)
        !          1223:         uTemp=SW_HIDE;
        !          1224: 
        !          1225:     StandardShowDlgItem(hDlg, ID_IO_LINKFILE, uTemp);  //last use of uTemp
        !          1226: 
        !          1227: 
        !          1228:     //5.  Clear out existing any flags selection and set the new one
        !          1229:     dwTemp=IOF_SELECTCREATENEW | IOF_SELECTCREATEFROMFILE;
        !          1230:     lpIO->dwFlags=(lpIO->dwFlags & ~dwTemp) | dwOption;
        !          1231: 
        !          1232: 
        !          1233:     /*
        !          1234:      * Show or hide controls as appropriate.  Do the icon
        !          1235:      * display last because it will take some time to repaint.
        !          1236:      * If we do it first then the dialog looks too sluggish.
        !          1237:      */
        !          1238: 
        !          1239:     i=(lpIO->dwFlags & IOF_CHECKDISPLAYASICON) ? SW_SHOWNORMAL : SW_HIDE;
        !          1240:     StandardShowDlgItem(hDlg, ID_IO_CHANGEICON, i);
        !          1241:     StandardShowDlgItem(hDlg, ID_IO_ICONDISPLAY, i);
        !          1242: 
        !          1243: 
        !          1244:     //6.Change result display
        !          1245:     SetInsertObjectResults(hDlg, lpIO);
        !          1246: 
        !          1247:     /*
        !          1248:      * 7.  For Create New, twiddle the listbox to think we selected it
        !          1249:      *     so it updates the icon from the object type.
        !          1250:      *
        !          1251:      *     For Insert or Link file, set the focus to the filename button
        !          1252:      *     and update the icon if necessary.
        !          1253:      */
        !          1254:     if (fTemp)
        !          1255:         UpdateClassIcon(hDlg, lpIO, GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST));
        !          1256: 
        !          1257:     else
        !          1258:     {
        !          1259:         if (lpIO->fAsIconFile && (NULL != lpIO->hMetaPictFile) )
        !          1260:         {
        !          1261:            SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGESET, (WPARAM)lpIO->hMetaPictFile, 0L);
        !          1262:            lpIO->hMetaPictFile = 0;
        !          1263:         }
        !          1264:         else
        !          1265:            UpdateClassIcon(hDlg, lpIO, NULL);
        !          1266: 
        !          1267:         SetFocus(GetDlgItem(hDlg, ID_IO_FILE));
        !          1268:     }
        !          1269: 
        !          1270:     return FALSE;
        !          1271:     }
        !          1272: 
        !          1273: 
        !          1274: /*
        !          1275:  * UpdateClassType
        !          1276:  *
        !          1277:  * Purpose:
        !          1278:  *  Updates static text control to reflect current file type.  Assumes 
        !          1279:  *  a valid filename.
        !          1280:  *
        !          1281:  * Parameters
        !          1282:  *  hDlg            HWND of the dialog box.
        !          1283:  *  lpIO            LPINSERTOBJECT pointing to the dialog structure
        !          1284:  *  fSet            TRUE to set the text, FALSE to explicitly clear it
        !          1285:  *
        !          1286:  * Return Value:
        !          1287:  *  None
        !          1288:  */
        !          1289: 
        !          1290: void UpdateClassType(HWND hDlg, LPINSERTOBJECT lpIO, BOOL fSet)
        !          1291: {
        !          1292: 
        !          1293:    CLSID clsid;
        !          1294:    char  szFileName[OLEUI_CCHPATHMAX];
        !          1295:    char  szFileType[OLEUI_CCHLABELMAX];
        !          1296: 
        !          1297:    *szFileType = '\0';
        !          1298: 
        !          1299:    if (fSet)
        !          1300:    {
        !          1301:       GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, (LPSTR)szFileName, OLEUI_CCHPATHMAX);
        !          1302: 
        !          1303:       if (NOERROR == GetClassFile((LPSTR)szFileName, &clsid) ) 
        !          1304:         OleStdGetUserTypeOfClass(&clsid, szFileType, OLEUI_CCHLABELMAX, NULL);
        !          1305: 
        !          1306:    }
        !          1307: 
        !          1308:    SetDlgItemText(hDlg, ID_IO_FILETYPE, (LPSTR)szFileType);
        !          1309: 
        !          1310:    return;
        !          1311: }
        !          1312: 
        !          1313: 
        !          1314: /*
        !          1315:  * UpdateClassIcon
        !          1316:  *
        !          1317:  * Purpose:
        !          1318:  *  Handles LBN_SELCHANGE for the Object Type listbox.  On a selection
        !          1319:  *  change, we extract an icon from the server handling the currently
        !          1320:  *  selected object type using the utility function HIconFromClass.
        !          1321:  *  Note that we depend on the behavior of FillClassList to stuff the
        !          1322:  *  object class after a tab in the listbox string that we hide from
        !          1323:  *  view (see WM_INITDIALOG).
        !          1324:  *
        !          1325:  * Parameters
        !          1326:  *  hDlg            HWND of the dialog box.
        !          1327:  *  lpIO            LPINSERTOBJECT pointing to the dialog structure
        !          1328:  *  hList           HWND of the Object Type listbox.
        !          1329:  *
        !          1330:  * Return Value:
        !          1331:  *  None
        !          1332:  */
        !          1333: 
        !          1334: void UpdateClassIcon(HWND hDlg, LPINSERTOBJECT lpIO, HWND hList)
        !          1335:     {
        !          1336:     UINT        iSel;
        !          1337:     DWORD       cb;
        !          1338:     LPMALLOC    pIMalloc;
        !          1339:     LPSTR       pszName, pszCLSID, pszTemp;
        !          1340:     HGLOBAL     hMetaPict;
        !          1341: 
        !          1342:     LRESULT     dwRet;
        !          1343: 
        !          1344: 
        !          1345:     //If Display as Icon is not selected, exit
        !          1346:     if (!(lpIO->dwFlags & IOF_CHECKDISPLAYASICON))
        !          1347:         return;
        !          1348: 
        !          1349:     /*
        !          1350:      * When we change object type selection, get the new icon for that
        !          1351:      * type into our structure and update it in the display.  We use the
        !          1352:      * class in the listbox when Create New is selected or the association
        !          1353:      * with the extension in Create From File.
        !          1354:      */
        !          1355: 
        !          1356:     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !          1357:         {
        !          1358:         iSel=(UINT)SendMessage(hList, LB_GETCURSEL, 0, 0L);
        !          1359: 
        !          1360:         if (LB_ERR==(int)iSel)
        !          1361:             return;
        !          1362: 
        !          1363:         //Check to see if we've already got the hMetaPict for this item
        !          1364:         dwRet=SendMessage(hList, LB_GETITEMDATA, (WPARAM)iSel, 0L);
        !          1365: 
        !          1366:         hMetaPict=(HGLOBAL)(UINT)dwRet;
        !          1367: 
        !          1368:         if (hMetaPict)
        !          1369:             {
        !          1370:             //Yep, we've already got it, so just display it and return.
        !          1371:             SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGESET, (WPARAM)hMetaPict, 0L);
        !          1372:             return;
        !          1373:             }
        !          1374: 
        !          1375:         iSel=(UINT)SendMessage(hList, LB_GETCURSEL, 0, 0L);
        !          1376: 
        !          1377:         if (LB_ERR==(int)iSel)
        !          1378:             return;
        !          1379: 
        !          1380:         //Allocate a string to hold the entire listbox string
        !          1381:         cb=SendMessage(hList, LB_GETTEXTLEN, iSel, 0L);
        !          1382:         }
        !          1383:     else
        !          1384:         cb=OLEUI_CCHPATHMAX;
        !          1385: 
        !          1386:     if (NOERROR!=CoGetMalloc(MEMCTX_TASK, &pIMalloc))
        !          1387:         return;
        !          1388: 
        !          1389:     pszName=(LPSTR)pIMalloc->lpVtbl->Alloc(pIMalloc, cb+1);
        !          1390: 
        !          1391:     if (NULL==pszName)
        !          1392:         {
        !          1393:         pIMalloc->lpVtbl->Release(pIMalloc);
        !          1394:         return;
        !          1395:         }
        !          1396: 
        !          1397:     *pszName=0;
        !          1398: 
        !          1399:     //Get the clsid we want.
        !          1400:     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !          1401:     {
        !          1402:         //Grab the classname string from the list
        !          1403:         SendMessage(hList, LB_GETTEXT, iSel, (LONG)pszName);
        !          1404: 
        !          1405:         //Set pointer to CLSID (string)
        !          1406:         pszCLSID=PointerToNthField(pszName, 2, '\t');
        !          1407: 
        !          1408:         //Null terminate pszName string
        !          1409:         pszTemp=AnsiPrev(pszName, pszCLSID);
        !          1410: 
        !          1411:         *pszTemp='\0';
        !          1412:         CLSIDFromString(pszCLSID, &lpIO->clsid);
        !          1413: 
        !          1414:         hMetaPict = GetIconOfClass(ghInst, &lpIO->clsid, NULL, TRUE);
        !          1415:     }
        !          1416: 
        !          1417:     else
        !          1418:     {
        !          1419:         //Get the class from the filename
        !          1420:         GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, pszName, OLEUI_CCHPATHMAX);
        !          1421: 
        !          1422:         hMetaPict = GetIconOfFile(ghInst, 
        !          1423:                                   pszName, 
        !          1424:                                   lpIO->dwFlags & IOF_CHECKLINK ? TRUE : FALSE);
        !          1425:     }
        !          1426: 
        !          1427: 
        !          1428:     //Replace the current display with this new one.
        !          1429:     SendDlgItemMessage(hDlg, ID_IO_ICONDISPLAY, IBXM_IMAGESET, (WPARAM)hMetaPict, 0L);
        !          1430: 
        !          1431:     //Enable or disable "Change Icon" button depending on whether
        !          1432:     //we've got a valid filename or not.
        !          1433:     EnableWindow(GetDlgItem(hDlg, ID_IO_CHANGEICON), hMetaPict ? TRUE : FALSE);
        !          1434: 
        !          1435:     //Save the hMetaPict so that we won't have to re-create
        !          1436:     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !          1437:         SendMessage(hList, LB_SETITEMDATA, (WPARAM)iSel, MAKELPARAM(hMetaPict,0));
        !          1438: 
        !          1439:     pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszName);
        !          1440:     pIMalloc->lpVtbl->Release(pIMalloc);
        !          1441:     return;
        !          1442:     }
        !          1443: 
        !          1444: 
        !          1445: 
        !          1446: 
        !          1447: /*
        !          1448:  * SetInsertObjectResults
        !          1449:  *
        !          1450:  * Purpose:
        !          1451:  *  Centralizes setting of the Result and icon displays in the Insert Object
        !          1452:  *  dialog.  Handles loading the appropriate string from the module's
        !          1453:  *  resources and setting the text, displaying the proper result picture,
        !          1454:  *  and showing the proper icon.
        !          1455:  *
        !          1456:  * Parameters:
        !          1457:  *  hDlg            HWND of the dialog box so we can access controls.
        !          1458:  *  lpIO            LPINSERTOBJECT in which we assume that the
        !          1459:  *                  current radiobutton and Display as Icon selections
        !          1460:  *                  are set.  We use the state of those variables to
        !          1461:  *                  determine which string we use.
        !          1462:  *
        !          1463:  * Return Value:
        !          1464:  *  None
        !          1465:  */
        !          1466: 
        !          1467: void SetInsertObjectResults(HWND hDlg, LPINSERTOBJECT lpIO)
        !          1468:     {
        !          1469:     LPSTR       pszT, psz1, psz2, psz3, psz4, pszTemp;
        !          1470:     UINT        i, iString1, iString2, iImage, cch;
        !          1471:     LPMALLOC    pIMalloc;
        !          1472:     BOOL        fAsIcon;
        !          1473: 
        !          1474:     /*
        !          1475:      * We need scratch memory for loading the stringtable string, loading
        !          1476:      * the object type from the listbox, and constructing the final string.
        !          1477:      * We therefore allocate three buffers as large as the maximum message
        !          1478:      * length (512) plus the object type, guaranteeing that we have enough
        !          1479:      * in all cases.
        !          1480:      */
        !          1481:     i=(UINT)SendDlgItemMessage(hDlg, ID_IO_OBJECTTYPELIST, LB_GETCURSEL, 0, 0L);
        !          1482:     cch=512+(UINT)SendDlgItemMessage(hDlg, ID_IO_OBJECTTYPELIST, LB_GETTEXTLEN, i, 0L);
        !          1483: 
        !          1484:     if (NOERROR!=CoGetMalloc(MEMCTX_TASK, &pIMalloc))
        !          1485:         return;
        !          1486: 
        !          1487:     pszTemp=(LPSTR)pIMalloc->lpVtbl->Alloc(pIMalloc, (DWORD)(4*cch));
        !          1488: 
        !          1489:     if (NULL==pszTemp)
        !          1490:         {
        !          1491:         pIMalloc->lpVtbl->Release(pIMalloc);
        !          1492:         return;
        !          1493:         }
        !          1494: 
        !          1495:     psz1=pszTemp;
        !          1496:     psz2=psz1+cch;
        !          1497:     psz3=psz2+cch;
        !          1498:     psz4=psz3+cch;
        !          1499: 
        !          1500:     fAsIcon=(0L!=(lpIO->dwFlags & IOF_CHECKDISPLAYASICON));
        !          1501: 
        !          1502:     if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !          1503:         {
        !          1504:         iString1 = fAsIcon ? IDS_IORESULTNEWICON : IDS_IORESULTNEW;
        !          1505:         iString2 = 0;
        !          1506:         iImage   = fAsIcon ? RESULTIMAGE_EMBEDICON : RESULTIMAGE_EMBED;
        !          1507:         }
        !          1508: 
        !          1509:     if (lpIO->dwFlags & IOF_SELECTCREATEFROMFILE)
        !          1510:         {
        !          1511:         //Pay attention to Link checkbox
        !          1512:         if (lpIO->dwFlags & IOF_CHECKLINK)
        !          1513:             {
        !          1514:             iString1 = fAsIcon ? IDS_IORESULTLINKFILEICON1 : IDS_IORESULTLINKFILE1;
        !          1515:             iString2 = fAsIcon ? IDS_IORESULTLINKFILEICON2 : IDS_IORESULTLINKFILE2;
        !          1516:             iImage =fAsIcon ? RESULTIMAGE_LINKICON : RESULTIMAGE_LINK;
        !          1517:             }
        !          1518:         else
        !          1519:             {
        !          1520:             iString1 = IDS_IORESULTFROMFILE1;
        !          1521:             iString2 = fAsIcon ? IDS_IORESULTFROMFILEICON2 : IDS_IORESULTFROMFILE2;
        !          1522:             iImage =fAsIcon ? RESULTIMAGE_EMBEDICON : RESULTIMAGE_EMBED;
        !          1523:             }
        !          1524:         }
        !          1525: 
        !          1526:     //Default is an empty string.
        !          1527:     *psz1=0;
        !          1528: 
        !          1529:     if (0!=LoadString(ghInst, iString1, psz1, cch))
        !          1530:     {
        !          1531: 
        !          1532:            // Load second string, if necessary
        !          1533:         if (   (0 != iString2) 
        !          1534:             && (0 != LoadString(ghInst, iString2, psz4, cch)) )
        !          1535:         {
        !          1536:           lstrcat(psz1, psz4);  // concatenate strings together.
        !          1537:         }
        !          1538: 
        !          1539: 
        !          1540: 
        !          1541:         //In Create New, do the extra step of inserting the object type string
        !          1542:         if (lpIO->dwFlags & IOF_SELECTCREATENEW)
        !          1543:         {
        !          1544:             SendDlgItemMessage(hDlg, ID_IO_OBJECTTYPELIST, LB_GETTEXT
        !          1545:                               , i, (LONG)psz2);
        !          1546: 
        !          1547:             //Null terminate at any tab (before the classname)
        !          1548:             pszT=psz2;
        !          1549:             while ('\t'!=*pszT && 0!=*pszT)
        !          1550:                 pszT++;
        !          1551:             *pszT=0;
        !          1552: 
        !          1553:             //Build the string and point psz1 to it.
        !          1554:             wsprintf(psz3, psz1, psz2);
        !          1555:             psz1=psz3;
        !          1556:         }
        !          1557:     }
        !          1558: 
        !          1559:     //If LoadString failed, we simply clear out the results (*psz1=0 above)
        !          1560:     SetDlgItemText(hDlg, ID_IO_RESULTTEXT, psz1);
        !          1561: 
        !          1562:     //Go change the image and Presto!  There you have it.
        !          1563:     SendDlgItemMessage(hDlg, ID_IO_RESULTIMAGE, RIM_IMAGESET, iImage, 0L);
        !          1564: 
        !          1565:     pIMalloc->lpVtbl->Free(pIMalloc, (LPVOID)pszTemp);
        !          1566:     pIMalloc->lpVtbl->Release(pIMalloc);
        !          1567:     return;
        !          1568:     }
        !          1569: 
        !          1570: 
        !          1571: 
        !          1572: /*
        !          1573:  * FValidateInsertFile
        !          1574:  *
        !          1575:  * Purpose:
        !          1576:  *  Given a possibly partial pathname from the file edit control,
        !          1577:  *  attempt to locate the file and if found, store the full path
        !          1578:  *  in the edit control ID_IO_FILEDISPLAY.
        !          1579:  *
        !          1580:  * Parameters:
        !          1581:  *  hDlg            HWND of the dialog box.
        !          1582:  *  fTellUser       BOOL TRUE if function should tell user, FALSE if
        !          1583:  *                   function should validate silently.
        !          1584:  *
        !          1585:  * Return Value:
        !          1586:  *  BOOL            TRUE if the file is acceptable, FALSE otherwise.
        !          1587:  */
        !          1588: 
        !          1589: BOOL FValidateInsertFile(HWND hDlg, BOOL fTellUser, UINT FAR* lpnErrCode)
        !          1590:     {
        !          1591:     OFSTRUCT    of;
        !          1592:     HFILE       hFile;
        !          1593:     char        szFile[OLEUI_CCHPATHMAX];
        !          1594: 
        !          1595:     *lpnErrCode = 0;
        !          1596:     /*
        !          1597:      * To validate we attempt OpenFile on the string.  If OpenFile
        !          1598:      * fails then we display an error.  If not, OpenFile will store
        !          1599:      * the complete path to that file in the OFSTRUCT which we can
        !          1600:      * then stuff in the edit control.
        !          1601:      */
        !          1602: 
        !          1603:     if (0==GetDlgItemText(hDlg, ID_IO_FILEDISPLAY, szFile, sizeof(szFile)))
        !          1604:         return FALSE;   // #4569 : return FALSE when there is no text in ctl
        !          1605: 
        !          1606:     hFile=OpenFile(szFile, &of, OF_EXIST);
        !          1607: 
        !          1608:     // if file is currently open (ie. sharing violation) OleCreateFromFile 
        !          1609:     // and OleCreateLinkToFile can still succeed; do not consider it an 
        !          1610:     // error.
        !          1611:     if (HFILE_ERROR==hFile && 0x0020/*sharing violation*/!=of.nErrCode)
        !          1612:     {
        !          1613:        *lpnErrCode = of.nErrCode;
        !          1614:        if (fTellUser)  
        !          1615:            OpenFileError(hDlg, of.nErrCode, szFile);
        !          1616:        return FALSE;
        !          1617:     }
        !          1618: 
        !          1619:     //OFSTRUCT contains an OEM name, not ANSI as we need for the edit box.
        !          1620:     OemToAnsi(of.szPathName, of.szPathName);
        !          1621:     SetDlgItemText(hDlg, ID_IO_FILEDISPLAY, of.szPathName);
        !          1622:     return TRUE;
        !          1623:     }
        !          1624: 
        !          1625: 
        !          1626: /*
        !          1627:  * InsertObjectCleanup
        !          1628:  *
        !          1629:  * Purpose:
        !          1630:  *  Clears cached icon metafiles from those stored in the listbox.
        !          1631:  *
        !          1632:  * Parameters:
        !          1633:  *  hDlg            HWND of the dialog.
        !          1634:  *
        !          1635:  * Return Value:
        !          1636:  *  None
        !          1637:  */
        !          1638: 
        !          1639: void InsertObjectCleanup(HWND hDlg)
        !          1640:     {
        !          1641:     HWND      hList;
        !          1642:     UINT      iItems;
        !          1643:     HGLOBAL   hMetaPict;
        !          1644:     LRESULT   dwRet;
        !          1645:     UINT      i;
        !          1646: 
        !          1647:     hList=GetDlgItem(hDlg, ID_IO_OBJECTTYPELIST);
        !          1648:     iItems=(UINT)SendMessage(hList, LB_GETCOUNT, 0, 0L);
        !          1649: 
        !          1650:     for (i=0; i < iItems; i++)
        !          1651:         {
        !          1652:         dwRet=SendMessage(hList, LB_GETITEMDATA, (WPARAM)i, 0L);
        !          1653: 
        !          1654:         //Cast of LRESULT to UINT to HGLOBAL portable to Win32.
        !          1655:         hMetaPict=(HGLOBAL)(UINT)dwRet;
        !          1656: 
        !          1657:         if (hMetaPict)
        !          1658:             OleUIMetafilePictIconFree(hMetaPict);
        !          1659:         }
        !          1660: 
        !          1661:     return;
        !          1662:     }

unix.superglobalmegacorp.com

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