Update of /cvsroot/objecthandler/ObjectHandler/ohxl
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv30934/ohxl
Modified Files:
functioncall.cpp functioncall.hpp
Log Message:
- optimize the test for the Excel Function Wizard
- remove copyright of Jérôme Lecomte since the code originates from MSDN
Index: functioncall.cpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/ohxl/functioncall.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** functioncall.cpp 4 Jan 2007 11:18:36 -0000 1.13
--- functioncall.cpp 10 Jan 2007 09:55:07 -0000 1.14
***************
*** 1,6 ****
/*
! Copyright (C) 2006 Eric Ehlers
! Copyright (C) 1998, 1999, 2001, 2002 Jérôme Lecomte
This file is part of QuantLib, a free-software/open-source library
--- 1,5 ----
/*
! Copyright (C) 2006, 2007 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
***************
*** 125,167 ****
}
-
- // code to determine whether we've been called from the Excel function wizard
- // taken from xlw by Jérôme Lecomte
-
- typedef struct _EnumStruct {
- bool bFuncWiz;
- short hwndXLMain;
- }
- EnumStruct, FAR * LPEnumStruct;
-
- bool CALLBACK EnumProc(HWND hwnd, LPEnumStruct pEnum) {
- const size_t CLASS_NAME_BUFFER = 50;
- // first check the class of the window. Will be szXLDialogClass
- // if function wizard dialog is up in Excel
- char rgsz[CLASS_NAME_BUFFER];
- GetClassName(hwnd, (LPSTR)rgsz, CLASS_NAME_BUFFER);
- if (2 == CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH,
- SUBLANG_ENGLISH_US),SORT_DEFAULT), NORM_IGNORECASE,
- (LPSTR)rgsz, (lstrlen((LPSTR)rgsz)>lstrlen("bosa_sdm_XL"))
- ? lstrlen("bosa_sdm_XL"):-1, "bosa_sdm_XL", -1)) {
- if(LOWORD((DWORD) GetParent(hwnd)) == pEnum->hwndXLMain) {
- pEnum->bFuncWiz = TRUE;
- return false;
- }
- }
- // no luck - continue the enumeration
- return true;
- }
-
- bool FunctionCall::IsCalledByFuncWiz() {
- XLOPER xHwndMain;
- EnumStruct enm;
- Excel(xlGetHwnd, &xHwndMain, 0);
- enm.bFuncWiz = false;
- enm.hwndXLMain = xHwndMain.val.w;
- EnumWindows((WNDENUMPROC) EnumProc, (LPARAM) ((LPEnumStruct) &enm));
- return enm.bFuncWiz;
- }
-
CallerType FunctionCall::getCallerType() {
if (callerType_ == Uninitialized2) {
--- 124,127 ----
***************
*** 178,180 ****
--- 138,223 ----
return callerType_;
}
+
+ // Code to determine whether we've been called from the Excel function wizard.
+ //
+ // This code is called by every function in the Addin and some attempt has been
+ // made to optimize performance e.g. caching variables whose values don't change.
+ //
+ // This code is based on code from MSDN and has inherited some bugs from the
+ // MSDN version:
+ // 1) The Function Wizard dialog is assumed to be any window of class
+ // "bosa_sdm_XL" but that class can also indicate the "Edit->Replace" dialog
+ // 2) The main excel parent window is identified only by the LOWORD half of its
+ // DWORD handle
+ //
+ // Consequently there is the risk that this function will return false positives
+ // i.e. we'll conclude that we've been called from the Function Wizard when in
+ // fact we've been called while
+ // 1) the "Edit->Replace" dialog is active
+ // 2) some other Excel session is displaying the Function Wizard or
+ // "Edit->Replace" dialog
+
+ typedef struct {
+ bool bFuncWiz;
+ short hwndXLMain;
+ } EnumStruct;
+
+ // The class name of the window for the Function Wizard dialog. Various
+ // versions of Excel may suffix this string with additional characters.
+ #define WIZ_ID_BUF "bosa_sdm_XL"
+ #define WIZ_ID_BUF_LEN 12
+
+ // Called by EnumWindows (below) for each open window
+ bool CALLBACK EnumProc(HWND hwnd, EnumStruct *pEnum) {
+
+ // Retrieve the class name of the current window. We only want to
+ // compare the resulting string with the "bosa_sdm_XL" value so we set
+ // the buffer size such that any trailing characters are truncated.
+
+ char class_name[WIZ_ID_BUF_LEN];
+ GetClassName(hwnd, class_name, WIZ_ID_BUF_LEN);
+
+ if (stricmp(class_name, WIZ_ID_BUF) == 0) {
+
+ // Apparently this window is the Excel Function Wizard dialog.
+ // Now check whether the ID of this window's parent matches that of
+ // our main Excel window as returned by xlGetHwnd (below).
+
+ if (LOWORD((DWORD) GetParent(hwnd)) == pEnum->hwndXLMain) {
+ pEnum->bFuncWiz = TRUE; // We've (probably) been called from the wizard
+ return false; // Tell EnumWindows to stop
+ }
+
+ }
+
+ return true; // Tell EnumWindows to continue
+ }
+
+ short int getWinID() {
+
+ // Retrieve the LOWORD half of the DWORD handle
+ // of the main Excel window.
+
+ XLOPER xHwndMain;
+ Excel(xlGetHwnd, &xHwndMain, 0);
+
+ return xHwndMain.val.w;
+ }
+
+ bool FunctionCall::IsCalledByFuncWiz() {
+
+ // ID of the main Excel window. This value is retrieved once
+ // for this running instance of the Addin.
+
+ static short int winID = getWinID();
+
+ // Call EnumWindows which iteratively calls EnumProc which sets
+ // enm.bFuncWiz if the Excel Function Wizard dialog is detected.
+
+ EnumStruct enm = { false, winID };
+ EnumWindows((WNDENUMPROC)EnumProc, (LPARAM)&enm);
+
+ return enm.bFuncWiz;
+ }
+
}
Index: functioncall.hpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/ohxl/functioncall.hpp,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** functioncall.hpp 30 Oct 2006 15:46:51 -0000 1.8
--- functioncall.hpp 10 Jan 2007 09:55:07 -0000 1.9
***************
*** 2,6 ****
/*
Copyright (C) 2006 Eric Ehlers
- Copyright (C) 1998, 1999, 2001, 2002 Jérôme Lecomte
This file is part of QuantLib, a free-software/open-source library
--- 2,5 ----
***************
*** 74,76 ****
#endif
-
--- 73,74 ----
|