home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / BOCOLE.PAK / BOLEHELP.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  6KB  |  195 lines

  1. //
  2. //**************************************************************************
  3. //
  4. // BOleHelp.cpp -- Implements message hooks for catching F1 in the dialogs
  5. //
  6. // Copyright (c) 1993,94 by Borland International, Inc. All rights reserved
  7. //
  8. //**************************************************************************
  9.  
  10. #include "BOle.h"
  11. #include "BOleSvc.h"
  12. extern "C" {
  13. #include "Ole2UI.h"
  14. #include "Common.h"
  15. }
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. // GetBOleHook -- Examine the parameters to our hook proc and determine if 
  34. //                it's a message we should handle. If so, return the hook 
  35. //                out of the window properties
  36. //
  37. HHOOK GetBOleHook (int code, WPARAM wParam, LPARAM lParam)
  38. {
  39.     HHOOK hHook = NULL;
  40.  
  41.     // Make sure the message is one we should handle
  42.     //
  43.     if (code != MSGF_DIALOGBOX                 || 
  44.         ((LPMSG) lParam)->message != WM_KEYDOWN ||
  45.         ((LPMSG) lParam)->wParam != VK_F1)
  46.          return hHook; 
  47.  
  48.     // The window property "Structure" contains the private part of the
  49.     // dialog struct (INSERTOBJECT is private, OLEUIINSERTOBJECT is public)
  50.     //
  51.     HWND hwnd = GetParent (((LPMSG) lParam)->hwnd);
  52.     HANDLE hOleUIStd = GetProp (hwnd, TEXT(STRUCTUREPROP));
  53.     if (!hOleUIStd)
  54.         return hHook;
  55.  
  56.     // The data model in OLE2UI is limiting here. If we want to get the
  57.     // pointer to the public part of a dialog struct, there's nothing to
  58.     // cast this pointer to. OleUIStandard doesn't have the pointer in the
  59.     // beginning like the private declarations of the dialog box structs!
  60.     //
  61.     LPOLEUISTANDARD pOleUIStd = (LPOLEUISTANDARD) ((LPLONG) GlobalLock (hOleUIStd))[0];
  62.     if (!pOleUIStd)
  63.         return hHook;
  64.  
  65.     // If we're a system-wide hook, make sure this hook was installed in
  66.     // this task before returning it.
  67.     //
  68.  
  69.     hHook = pOleUIStd->hHook;
  70.     
  71.     GlobalUnlock (hOleUIStd);
  72.     return hHook;
  73. }
  74.  
  75. // MyHookProc is a MessageProc callback for SetWindowsHookEx which allows
  76. // us to catch messages during our dialog boxes. We're looking for F1
  77. // keystrokes which would otherwise be eaten by the control with focus.
  78. //
  79. extern "C" LRESULT _loadds CALLBACK MyHookProc (int code, WPARAM wParam, LPARAM lParam)
  80. {
  81.     HHOOK hHook = GetBOleHook (code, wParam, lParam);
  82.     if (hHook) {
  83.  
  84.         // The window which really gets the message is the control, so the
  85.         // dialog is its parent
  86.         //
  87.         HWND w = GetParent (((LPMSG) lParam)->hwnd);
  88. #ifdef ANSI
  89.         ::PostMessageA(w, RegisterWindowMessageA(HELPMSGSTRINGA), 0, 0);
  90. #else
  91.         ::PostMessage(w, RegisterWindowMessage(HELPMSGSTRING), 0, 0);
  92. #endif
  93.         return ::CallNextHookEx (hHook, code, wParam, lParam);
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.     return 0; 
  100. }
  101.  
  102. // EnterBOleDialog has two basic jobs:
  103. // 1. Install and remove the Windows hook which allows us to catch
  104. //    F1 keystrokes while we're running a BOleUI dialog box.
  105. // 2. Make sure we do the right enabling of modeless dialogs around
  106. //    the system
  107. //
  108. void _IFUNC BOleService::EnterBOleDialog (BOOL fEnter, HHOOK& hHook, HTASK& hTask)
  109. {
  110.     // If we're closing this dialog box, remove the Windows hook 
  111.     //
  112.     if (!fEnter) {
  113.         ::UnhookWindowsHook (WH_MSGFILTER, MyHookProc);
  114.  
  115.         OnModalDialog (FALSE);
  116.         return;
  117.     }
  118.  
  119.     BOOL fHookSuccess = TRUE;
  120.  
  121.     // Find the address of the callback for the Windows hook
  122.     //
  123.     HOOKPROC hookProc = (HOOKPROC)MyHookProc; //
  124.     if (!hookProc) 
  125.         fHookSuccess = FALSE;
  126.  
  127.     // Install the Windows hook
  128.     //
  129.     if (fHookSuccess) {
  130. #ifndef WIN32
  131.         hHook = ::SetWindowsHook (WH_MSGFILTER, hookProc);
  132.  
  133. #else
  134. #ifdef ANSI
  135.         hHook = ::SetWindowsHookExA(WH_MSGFILTER, hookProc, NULL, GetCurrentThreadId() );
  136. #else
  137.         hHook = ::SetWindowsHookEx(WH_MSGFILTER, hookProc, NULL, GetCurrentThreadId() );
  138. #endif
  139. #endif
  140.         if (!hHook) 
  141.             fHookSuccess = FALSE;
  142.  
  143.         // Pass back the current "task"
  144.         //
  145. #ifndef    WIN32
  146.         hTask = GetCurrentTask();
  147. #else
  148.         hTask = (HTASK)GetCurrentThread();
  149. #endif
  150.     }
  151.  
  152.     // Use the same thing Bolero clients do to en/disable modeless dialogs
  153.     //
  154.     OnModalDialog (TRUE);
  155. }
  156.  
  157. // BOleHelpNotify is a C function which is exposed to the OLE2UI dialog
  158. // boxes (but not to other DLLs) for them to call back to get help from 
  159. // the application.
  160. //
  161. // 2 minor hacks are used here to solve the language differences between
  162. // the Bolero header files (C++ only) and OLE2UI (C, and I don't have the 
  163. // time to convert it to C++)
  164. //
  165. // 1. We're using the resource IDs of the dialog boxes to identify which 
  166. //    dialog box to provide help on since OLE2UI can't compile the 
  167. //    BOleDialogHelp enum.
  168. //
  169. // 2. The pCastToApp parameter is used because OLE2UI can't compile the
  170. //    class definition for IBApplication.
  171. //
  172. extern "C" void BOleHelpNotify (DWORD pCastToApp, int dialogResId)
  173. {
  174.     BOleDialogHelp helpCode;
  175.     switch (dialogResId) {
  176.         case IDD_INSERTOBJECT : helpCode = BOLE_HELP_BROWSE;          break;
  177.         case IDD_PASTESPECIAL : helpCode = BOLE_HELP_BROWSECLIPBOARD; break;
  178.         case IDD_CONVERT      : helpCode = BOLE_HELP_CONVERT;         break;
  179.         case IDD_EDITLINKS    : helpCode = BOLE_HELP_BROWSELINKS;     break;
  180.         case IDD_CHANGEICON   : helpCode = BOLE_HELP_CHANGEICON;      break;
  181.         case IDD_INSERTFILEBROWSE
  182.                               : helpCode = BOLE_HELP_FILEOPEN;        break;
  183.         case IDD_CHANGESOURCE : helpCode = BOLE_HELP_SOURCESET;       break;
  184.         case IDD_CHANGEICONBROWSE
  185.                               : helpCode = BOLE_HELP_ICONFILEOPEN;    break;
  186.         default               : return;
  187.     }
  188.  
  189.     PIBApplication pIApp = (PIBApplication) pCastToApp;
  190.     pIApp->DialogHelpNotify (helpCode);
  191. }
  192.  
  193.  
  194.  
  195.