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

1.1     ! root        1: /*
        !             2:  * COMMON.C      
        !             3:  *
        !             4:  * Standardized (and centralized) pieces of each OLE2UI dialog function:
        !             5:  *  UStandardValidation     Validates standard fields in each dialog structure
        !             6:  *  UStandardInvocation     Invokes a dialog through DialogBoxIndirectParam
        !             7:  *  LpvStandardInit         Common WM_INITDIALOG processing
        !             8:  *  LpvStandardEntry        Common code to execute on dialog proc entry.
        !             9:  *  FStandardHook           Centralized hook calling function.
        !            10:  *  StandardCleanup         Common exit/cleanup code.
        !            11:  *  OleUIShowDlgItem        Show-Enable/Hide-Disable dialog item
        !            12:  *
        !            13:  * Copyright (c)1992 Microsoft Corporation, All Right Reserved
        !            14:  */
        !            15: 
        !            16: #define STRICT  1
        !            17: #include "ole2ui.h"
        !            18: #include "common.h"
        !            19: #include "utility.h"
        !            20: #include <malloc.h>
        !            21: 
        !            22: 
        !            23: /*
        !            24:  * UStandardValidation
        !            25:  *
        !            26:  * Purpose:
        !            27:  *  Performs validation on the standard pieces of any dialog structure,
        !            28:  *  that is, the fields defined in the OLEUISTANDARD structure.
        !            29:  *
        !            30:  * Parameters:
        !            31:  *  lpUI            const LPOLEUISTANDARD pointing to the shared data of
        !            32:  *                  all structs.
        !            33:  *  cbExpect        const UINT structure size desired by the caller.
        !            34:  *  phDlgMem        const HGLOBAL FAR * in which to store a loaded customized
        !            35:  *                  template, if one exists.
        !            36:  *
        !            37:  * Return Value:
        !            38:  *  UINT            OLEUI_SUCCESS if all validation succeeded.  Otherwise
        !            39:  *                  it will be one of the standard error codes.
        !            40:  */
        !            41: 
        !            42: UINT WINAPI UStandardValidation(const LPOLEUISTANDARD lpUI, const UINT cbExpect
        !            43:                          , const HGLOBAL FAR *phMemDlg)
        !            44:     {
        !            45:     HRSRC       hRes=NULL;
        !            46:     HGLOBAL     hMem=NULL;
        !            47: 
        !            48: 
        !            49:     /*
        !            50:      * 1.  Validate non-NULL pointer parameter.  Note:  We don't validate
        !            51:      *     phDlg since it's not passed from an external source.
        !            52:      */
        !            53:     if (NULL==lpUI)
        !            54:         return OLEUI_ERR_STRUCTURENULL;
        !            55: 
        !            56:     //2.  Validate that the structure is readable and writable.
        !            57:     if (IsBadReadPtr(lpUI, cbExpect) || IsBadWritePtr(lpUI, cbExpect))
        !            58:         return OLEUI_ERR_STRUCTUREINVALID;
        !            59: 
        !            60:     //3.  Validate the structure size
        !            61:     if (cbExpect!=lpUI->cbStruct)
        !            62:         return OLEUI_ERR_CBSTRUCTINCORRECT;
        !            63: 
        !            64:     //4.  Validate owner-window handle.  NULL is considered valid.
        !            65:     if (NULL!=lpUI->hWndOwner && !IsWindow(lpUI->hWndOwner))
        !            66:         return OLEUI_ERR_HWNDOWNERINVALID;
        !            67: 
        !            68:     //5.  Validate the dialog caption.  NULL is considered valid.
        !            69:     if (NULL!=lpUI->lpszCaption && IsBadReadPtr(lpUI->lpszCaption, 1))
        !            70:         return OLEUI_ERR_LPSZCAPTIONINVALID;
        !            71: 
        !            72:     //6.  Validate the hook pointer.  NULL is considered valid.
        !            73:     if ((LPFNOLEUIHOOK)NULL!=lpUI->lpfnHook
        !            74:         && IsBadCodePtr((FARPROC)lpUI->lpfnHook))
        !            75:         return OLEUI_ERR_LPFNHOOKINVALID;
        !            76: 
        !            77:     /*
        !            78:      * 7.  If hInstance is non-NULL, we have to also check lpszTemplate.
        !            79:      *     Otherwise, lpszTemplate is not used and requires no validation.
        !            80:      *     lpszTemplate cannot be NULL if used.
        !            81:      */
        !            82:     if (NULL!=lpUI->hInstance)
        !            83:         {
        !            84:         //Best we can try is one character
        !            85:         if (NULL==lpUI->lpszTemplate || IsBadReadPtr(lpUI->lpszTemplate, 1))
        !            86:             return OLEUI_ERR_LPSZTEMPLATEINVALID;
        !            87: 
        !            88:         hRes=FindResource(lpUI->hInstance, lpUI->lpszTemplate, RT_DIALOG);
        !            89: 
        !            90:         //This is the only thing that catches invalid non-NULL hInstance
        !            91:         if (NULL==hRes)
        !            92:             return OLEUI_ERR_FINDTEMPLATEFAILURE;
        !            93: 
        !            94:         hMem=LoadResource(lpUI->hInstance, hRes);
        !            95: 
        !            96:         if (NULL==hMem)
        !            97:             return OLEUI_ERR_LOADTEMPLATEFAILURE;
        !            98:         }
        !            99: 
        !           100: 
        !           101:     //8.  If hResource is non-NULL, be sure we can lock it.
        !           102:     if (NULL!=lpUI->hResource)
        !           103:         {
        !           104:         if ((LPSTR)NULL==GlobalLock(lpUI->hResource))
        !           105:             return OLEUI_ERR_HRESOURCEINVALID;
        !           106: 
        !           107:         GlobalUnlock(lpUI->hResource);
        !           108:         }
        !           109: 
        !           110:     /*
        !           111:      * Here we have hMem==NULL if we should use the standard template
        !           112:      * or the one in lpUI->hResource.  If hMem is non-NULL, then we
        !           113:      * loaded one from the calling application's resources which the
        !           114:      * caller of this function has to free if it sees any other error.
        !           115:      */
        !           116:     *(HGLOBAL FAR *)phMemDlg=hMem;
        !           117:     return OLEUI_SUCCESS;
        !           118:     }
        !           119: 
        !           120: 
        !           121: 
        !           122: 
        !           123: 
        !           124: /*
        !           125:  * UStandardInvocation
        !           126:  *
        !           127:  * Purpose:
        !           128:  *  Provides standard template loading and calling on DialogBoxIndirectParam
        !           129:  *  for all the OLE UI dialogs.
        !           130:  *
        !           131:  * Parameters:
        !           132:  *  lpDlgProc       DLGPROC of the dialog function.  
        !           133:  *  lpUI            LPOLEUISTANDARD containing the dialog structure.
        !           134:  *  hMemDlg         HGLOBAL containing the dialog template.  If this
        !           135:  *                  is NULL and lpUI->hResource is NULL, then we load
        !           136:  *                  the standard template given the name in lpszStdTemplate
        !           137:  *  lpszStdTemplate LPCSTR standard template to load if hMemDlg is NULL
        !           138:  *                  and lpUI->hResource is NULL.
        !           139:  *
        !           140:  * Return Value:
        !           141:  *  UINT            OLEUI_SUCCESS if all is well, otherwise and error
        !           142:  *                  code.
        !           143:  */
        !           144: 
        !           145: UINT WINAPI UStandardInvocation(DLGPROC lpDlgProc, LPOLEUISTANDARD lpUI
        !           146:                          , HGLOBAL hMemDlg, LPCSTR lpszStdTemplate)
        !           147:     {
        !           148:     HGLOBAL     hTemplate=hMemDlg;
        !           149:     HRSRC       hRes;
        !           150:     int         iRet;
        !           151: 
        !           152:     //Make sure we have a template, then lock it down
        !           153:     if (NULL==hTemplate)
        !           154:         hTemplate=lpUI->hResource;
        !           155: 
        !           156:     if (NULL==hTemplate)
        !           157:         {
        !           158:         hRes=FindResource(ghInst, lpszStdTemplate, RT_DIALOG);
        !           159: 
        !           160:         if (NULL==hRes)
        !           161:             {
        !           162:             return OLEUI_ERR_FINDTEMPLATEFAILURE;
        !           163:             }
        !           164: 
        !           165:         hTemplate=LoadResource(ghInst, hRes);
        !           166: 
        !           167:         if (NULL==hTemplate)
        !           168:             {
        !           169:             return OLEUI_ERR_LOADTEMPLATEFAILURE;
        !           170:             }
        !           171:         }
        !           172: 
        !           173:     /*                                                  
        !           174:      * hTemplate has the template to use, so now we can invoke the dialog.
        !           175:      * Since we have exported all of our dialog procedures using the
        !           176:      * _export keyword, we do not need to call MakeProcInstance,
        !           177:      * we can ue the dialog procedure address directly.
        !           178:      */
        !           179:     
        !           180:     iRet=DialogBoxIndirectParam(ghInst, hTemplate, lpUI->hWndOwner
        !           181:                                 , lpDlgProc, (LPARAM)lpUI);
        !           182: 
        !           183:     /*
        !           184:      * Cleanup the template if we explicitly loaded it.  Caller is
        !           185:      * responsible for already loaded template resources.
        !           186:      */
        !           187:     if (hTemplate!=lpUI->hResource)
        !           188:         FreeResource(hTemplate);
        !           189: 
        !           190:     if (-1==iRet)
        !           191:         return OLEUI_ERR_DIALOGFAILURE;
        !           192: 
        !           193:     //Return the code from EndDialog, generally OLEUI_OK or OLEUI_CANCEL
        !           194:     return (UINT)iRet;
        !           195:     }
        !           196: 
        !           197: 
        !           198: 
        !           199: 
        !           200: 
        !           201: 
        !           202: /*
        !           203:  * LpvStandardInit
        !           204:  *
        !           205:  * Purpose:
        !           206:  *  Default actions for WM_INITDIALOG handling in the dialog, allocating
        !           207:  *  a dialog-specific structure, setting that memory as a dialog property,
        !           208:  *  and creating a small font if necessary setting that font as a property.
        !           209:  *
        !           210:  * Parameters:
        !           211:  *  hDlg            HWND of the dialog
        !           212:  *  cbStruct        UINT size of dialog-specific structure to allocate.
        !           213:  *  fCreateFont     BOOL indicating if we need to create a small Helv
        !           214:  *                  font for this dialog.
        !           215:  *  phFont          HFONT FAR * in which to place a created font.  Can be
        !           216:  *                  NULL if fCreateFont is FALSE.
        !           217:  *
        !           218:  * Return Value:
        !           219:  *  LPVOID          Pointer to global memory allocated for the dialog.
        !           220:  *                  The memory will have been set as a dialog property
        !           221:  *                  using the STRUCTUREPROP label.
        !           222:  */
        !           223: 
        !           224: LPVOID WINAPI LpvStandardInit(HWND hDlg, UINT cbStruct, BOOL fCreateFont, HFONT FAR * phFont)
        !           225:     {
        !           226:     LPVOID      lpv;
        !           227:     HFONT       hFont;
        !           228:     LOGFONT     lf;
        !           229:     HGLOBAL     gh;
        !           230: 
        !           231:     //Must have at least sizeof(OLEUISTANDARD) bytes in cbStruct
        !           232:     if (sizeof(OLEUISTANDARD) > cbStruct || (fCreateFont && NULL==phFont))
        !           233:         return NULL;
        !           234: 
        !           235:     gh=GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, cbStruct);
        !           236: 
        !           237:     if (NULL==gh)
        !           238:         {
        !           239:         PostMessage(hDlg, uMsgEndDialog, OLEUI_ERR_GLOBALMEMALLOC, 0L);
        !           240:         return NULL;
        !           241:         }
        !           242:     lpv = GlobalLock(gh);
        !           243:     SetProp(hDlg, STRUCTUREPROP, gh);
        !           244: 
        !           245:     //Create the non-bold font for result and file texts.  We call
        !           246:     hFont=(HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0L);
        !           247:     GetObject(hFont, sizeof(LOGFONT), &lf);
        !           248:     lf.lfWeight=FW_NORMAL;
        !           249: 
        !           250:     //Attempt to create the font.  If this fails, then we return no font.
        !           251:     *phFont=CreateFontIndirect(&lf);
        !           252: 
        !           253:     //If we couldn't create the font, we'll do with the default.
        !           254:     if (NULL!=*phFont)
        !           255:         SetProp(hDlg, FONTPROP, (HANDLE)*phFont);
        !           256: 
        !           257:     return lpv;
        !           258:     }
        !           259: 
        !           260: 
        !           261: 
        !           262: 
        !           263: 
        !           264: /*
        !           265:  * LpvStandardEntry
        !           266:  *
        !           267:  * Purpose:
        !           268:  *  Retrieves the dialog's structure property and calls the hook
        !           269:  *  as necessary.  This should be called on entry into all dialog
        !           270:  *  procedures.
        !           271:  *
        !           272:  * Parameters:
        !           273:  *  hDlg            HWND of the dialog
        !           274:  *  iMsg            UINT message to the dialog
        !           275:  *  wParam, lParam  WPARAM, LPARAM message parameters
        !           276:  *  puHookResult    UINT FAR * in which this function stores the return value
        !           277:  *                  from the hook if it is called.  If no hook is available,
        !           278:  *                  this will be FALSE.
        !           279:  *
        !           280:  * Return Value:
        !           281:  *  LPVOID          Pointer to the dialog's extra structure held in the
        !           282:  *                  STRUCTUREPROP property.
        !           283:  */
        !           284: 
        !           285: LPVOID WINAPI LpvStandardEntry(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam
        !           286:                       , UINT FAR * puHookResult)
        !           287:     {
        !           288:     LPVOID              lpv = NULL;
        !           289:     HGLOBAL             gh;
        !           290: 
        !           291:     // This will fail under WM_INITDIALOG, where we allocate using StandardInit
        !           292:     gh = GetProp(hDlg, STRUCTUREPROP);
        !           293: 
        !           294:     if (NULL!=puHookResult && NULL!=gh)
        !           295:         {
        !           296:         *puHookResult=0;
        !           297: 
        !           298:         // gh was locked previously, lock and unlock to get lpv
        !           299:         lpv = GlobalLock(gh);
        !           300:         GlobalUnlock(gh);
        !           301: 
        !           302:         //Call the hook for all messages except WM_INITDIALOG
        !           303:         if (NULL!=lpv && WM_INITDIALOG!=iMsg)
        !           304:             *puHookResult=UStandardHook(lpv, hDlg, iMsg, wParam, lParam);
        !           305:         }
        !           306: 
        !           307:     return lpv;
        !           308:     }
        !           309: 
        !           310: 
        !           311: 
        !           312: 
        !           313: /*
        !           314:  * UStandardHook
        !           315:  *
        !           316:  * Purpose:
        !           317:  *  Provides a generic hook calling function assuming that all private
        !           318:  *  dialog structures have a far pointer to their assocated public
        !           319:  *  structure as the first field, and that the first part of the public
        !           320:  *  structure matches an OLEUISTANDARD.
        !           321:  *
        !           322:  * Parameters:
        !           323:  *  pv              PVOID to the dialog structure.
        !           324:  *  hDlg            HWND to send with the call to the hook.
        !           325:  *  iMsg            UINT message to send to the hook.
        !           326:  *  wParam, lParam  WPARAM, LPARAM message parameters
        !           327:  *
        !           328:  * Return Value:
        !           329:  *  UINT            Return value from the hook, zero to indicate that
        !           330:  *                  default action should occur,  nonzero to specify
        !           331:  *                  that the hook did process the message.  In some
        !           332:  *                  circumstances it will be important for the hook to
        !           333:  *                  return a non-trivial non-zero value here, such as
        !           334:  *                  a brush from WM_CTLCOLOR, in which case the caller
        !           335:  *                  should return that value from the dialog procedure.
        !           336:  */
        !           337: 
        !           338: UINT WINAPI UStandardHook(LPVOID lpv, HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
        !           339:     {
        !           340:     LPOLEUISTANDARD     lpUI;
        !           341:     UINT                uRet=0;
        !           342: 
        !           343:     lpUI=*((LPOLEUISTANDARD FAR *)lpv);
        !           344: 
        !           345:     if (NULL!=lpUI && NULL!=lpUI->lpfnHook)
        !           346:         {
        !           347:         /*
        !           348:          * In order for the hook to have the proper DS, they should be
        !           349:          * compiling with -GA -GEs so and usin __export to get everything
        !           350:          * set up properly.
        !           351:          */
        !           352:         uRet=(*lpUI->lpfnHook)(hDlg, iMsg, wParam, lParam);
        !           353:         }
        !           354: 
        !           355:     return uRet;
        !           356:     }
        !           357: 
        !           358: 
        !           359: 
        !           360: 
        !           361: 
        !           362: /*
        !           363:  * StandardCleanup
        !           364:  *
        !           365:  * Purpose:
        !           366:  *  Removes properties and reverses any other standard initiazation
        !           367:  *  done through StandardSetup.
        !           368:  *
        !           369:  * Parameters:
        !           370:  *  lpv             LPVOID containing the private dialog structure.
        !           371:  *  hDlg            HWND of the dialog closing.
        !           372:  *
        !           373:  * Return Value:
        !           374:  *  None
        !           375:  */
        !           376: 
        !           377: void WINAPI StandardCleanup(LPVOID lpv, HWND hDlg)
        !           378:     {
        !           379:     HFONT       hFont;
        !           380:     HGLOBAL     gh;
        !           381: 
        !           382:     hFont=(HFONT)GetProp(hDlg, FONTPROP);
        !           383: 
        !           384:     if (NULL!=hFont)
        !           385:         DeleteObject(hFont);
        !           386: 
        !           387:     RemoveProp(hDlg, FONTPROP);
        !           388: 
        !           389:     gh = RemoveProp(hDlg, STRUCTUREPROP);
        !           390:     if (gh)
        !           391:         {
        !           392:         GlobalUnlock(gh);
        !           393:         GlobalFree(gh);
        !           394:         }
        !           395:     return;
        !           396:     }
        !           397:     
        !           398: 
        !           399: /* StandardShowDlgItem        
        !           400: ** -------------------
        !           401: **    Show & Enable or Hide & Disable a dialog item as appropriate.
        !           402: **    it is NOT sufficient to simply hide the item; it must be disabled
        !           403: **    too or the keyboard accelerator still functions.
        !           404: */
        !           405: void WINAPI StandardShowDlgItem(HWND hDlg, int idControl, int nCmdShow)
        !           406: {
        !           407:     if (SW_HIDE == nCmdShow) {
        !           408:         ShowWindow(GetDlgItem(hDlg, idControl), SW_HIDE);
        !           409:         EnableWindow(GetDlgItem(hDlg, idControl), FALSE);
        !           410:     } else {
        !           411:         ShowWindow(GetDlgItem(hDlg, idControl), SW_SHOWNORMAL);
        !           412:         EnableWindow(GetDlgItem(hDlg, idControl), TRUE);
        !           413:     }
        !           414: }

unix.superglobalmegacorp.com

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