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

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. //
  4. // OWL.CPP
  5. //
  6.  
  7. #include <dos.h>
  8. #include <mem.h>
  9. #include "owl.h"
  10.  
  11. // AllocCSToDSAlias was not declared in windows.h
  12.  
  13. extern "C" WORD FAR PASCAL AllocCSToDSAlias(WORD);
  14.  
  15. long FAR PASCAL _export StdWndProc(HWND HWindow, WORD Message, WORD WParam,
  16.                    LONG LParam);
  17.  
  18. typedef struct TObjectInstance {
  19.     char Code;
  20.     WORD  Offset;
  21.     union Ptr {
  22.     struct TObjectInstance FAR *Next;
  23.     Object FAR *ObjectPtr;
  24.     }ptr;
  25. }FAR *PObjectInstance;
  26.  
  27. typedef struct TInstanceBlock {
  28.     WORD Next;
  29.     char Code[5];
  30.     void FAR *WndProcPtr;
  31.     struct TObjectInstance Instances[34];
  32. }FAR *PInstanceBlock;
  33.  
  34.  
  35. WORD InstBlockList = 0;
  36. PObjectInstance InstFreeList = NULL;
  37. FARPROC StdWndProcInstance = NULL;
  38.  
  39. //Creation window pointers for InitWndProc
  40. PTWindowsObject CreationWindow = NULL;
  41. PTWindowsObject DlgCreationWindow = NULL;
  42.  
  43. // Return pointer to TWindowsObject given a window handle
  44.  
  45. PTWindowsObject GetObjectPtr(HWND HWindow)
  46. {
  47.   PObjectInstance AnInstance;
  48.  
  49.   if ( IsWindow(HWindow) )
  50.   {
  51.     AnInstance = (PObjectInstance)GetWindowLong(HWindow, GWL_WNDPROC);
  52.     if ((BYTE)(AnInstance->Code) == 0xE8)
  53.       if (AnInstance->Offset == (2-3) - FP_OFF(AnInstance) )
  54.         if ( *(WORD FAR *)MK_FP(FP_SEG(AnInstance),2)  == 0x2E5B )
  55.           return (PTWindowsObject)(AnInstance->ptr.ObjectPtr);
  56.  
  57.     if ( (FARPROC)GetClassLong(HWindow, GCL_WNDPROC) ==
  58.          (FARPROC)DefDlgProc )
  59.     {  // If it's a dialog get Instance from where it's stored.
  60.       AnInstance = (PObjectInstance)GetWindowLong(HWindow, 4);
  61.       if ( AnInstance )
  62.         if ((BYTE)(AnInstance->Code) == 0xE8)
  63.           if (AnInstance->Offset == (2-3) - FP_OFF(AnInstance) )
  64.             if ( *(WORD FAR *)MK_FP(FP_SEG(AnInstance),2)  == 0x2E5B )
  65.               return (PTWindowsObject)(AnInstance->ptr.ObjectPtr);
  66.     }
  67.   }
  68.   return NULL;
  69. }
  70.  
  71. long FAR PASCAL _export StdWndProc(HWND HWindow, WORD Message, WORD WParam,
  72.                    LONG LParam)
  73. {
  74.     PTWindowsObject myself;
  75.     TMessage Msg;
  76.  
  77.     Msg.Receiver = HWindow;  Msg.Message = Message;
  78.     Msg.WParam = WParam;     Msg.LParam = LParam;
  79.     Msg.Result = 1L;
  80.     myself = (PTWindowsObject)MK_FP(_ES,_BX);  // ES:BX set from the thunk
  81.     myself->HWindow = HWindow;
  82.     if ( Message >= 0x8000 )
  83.       myself->DefWndProc(Msg);
  84.     else
  85.       myself->DispatchAMessage(Message, Msg, &TWindowsObject::DefWndProc);
  86.   return Msg.Result;
  87. }
  88.  
  89. // Implementation of Initialization window procedure.
  90. inline LRESULT __InitWndProc(
  91.              HWND HWindow, UINT Message, WPARAM WParam, LPARAM LParam
  92.              )
  93. {
  94.   PTWindowsObject myself;
  95.  
  96.   if ( DlgCreationWindow )
  97.   {      // DlgCreationWindow is my parent dialog, I'm being created by it
  98.     WORD MyId = GetWindowWord(HWindow, GWW_ID);
  99.     myself = DlgCreationWindow->ChildWithId(MyId);
  100.   }
  101.   else
  102.   {
  103.      // I'm being created by TWindow::Create()
  104.     myself = CreationWindow;
  105.   }
  106.  
  107.   SetWindowLong(HWindow, GWL_WNDPROC, (long)(myself->GetInstance()));
  108.   return (* (long (FAR PASCAL *)(HWND, WORD, WORD, DWORD))
  109.             (myself->GetInstance()))(HWindow, Message, WParam, LParam);
  110. }
  111.  
  112. // Win31 Initialization window procedure.
  113. #ifdef WIN31
  114. LRESULT FAR PASCAL _export
  115.   InitWndProc(HWND HWindow, UINT Message, WPARAM WParam, LPARAM LParam)
  116. {
  117.   return __InitWndProc(HWindow, Message, WParam, LParam);
  118. }
  119. #endif
  120.  
  121. // Win30 Initialization window procedure.
  122. #ifdef WIN30
  123. LONG FAR PASCAL _export
  124.   InitWndProc(HWND_30 HWindow, WORD Message, WORD WParam, LONG LParam)
  125. {
  126.   return __InitWndProc(
  127.          HWND(HWindow), UINT(Message), WPARAM(WParam), LPARAM(LParam));
  128. }
  129. #endif
  130.  
  131.  
  132. WNDPROC MakeObjectInstance(PTWindowsObject P)
  133. {
  134.     char BlockCode[5] ;
  135.     PInstanceBlock Block;
  136.     PObjectInstance Instance;
  137.     WNDPROC ObjInstance;
  138.  
  139.     // POP BX
  140.     BlockCode[0]=0x5b;
  141.     // LES BX, CS:[BX]
  142.     BlockCode[1]=0x2e;
  143.     BlockCode[2]=0xc4;
  144.     BlockCode[3]=0x1f;
  145.     // JMP FAR StdWndProc
  146.     BlockCode[4]=0xea;
  147.  
  148.     if (InstFreeList == NULL)
  149.     {
  150.         Block = (PInstanceBlock)GlobalLock(GlobalAlloc(
  151.                  GMEM_FIXED | GMEM_DDESHARE | GMEM_NOT_BANKED, sizeof(TInstanceBlock)));
  152.         Block->Next = InstBlockList;
  153.     _fmemcpy(Block->Code, BlockCode, 5);
  154.         Block->WndProcPtr = StdWndProcInstance;
  155.         Instance = Block->Instances;
  156.         do {
  157.             Instance->Code = 0xE8;
  158.             Instance->Offset = (2 - 3) - FP_OFF(Instance);
  159.               Instance->ptr.Next = InstFreeList;
  160.               InstFreeList = Instance;
  161.         Instance = (PObjectInstance)MK_FP(FP_SEG(Instance),
  162.                           FP_OFF(Instance)+sizeof(TObjectInstance));
  163.     } while (FP_OFF(Instance) != sizeof(TInstanceBlock));
  164.         InstBlockList = FP_SEG(Block);
  165.         PrestoChangoSelector(FP_SEG(Block), FP_SEG(Block));
  166.     }
  167.     ObjInstance = (WNDPROC)InstFreeList;
  168.     Instance = (PObjectInstance)MK_FP(
  169.             AllocCSToDSAlias(FP_SEG(InstFreeList)),
  170.             FP_OFF(InstFreeList));
  171.     InstFreeList = Instance->ptr.Next;
  172.     Instance->ptr.ObjectPtr = (Object FAR *)P;
  173.     FreeSelector(FP_SEG(Instance));
  174.     return (ObjInstance);
  175. }
  176.  
  177. void FreeObjectInstance(WNDPROC P)
  178. {
  179.     PObjectInstance Instance;
  180.  
  181.     Instance = (PObjectInstance) MK_FP(
  182.             AllocCSToDSAlias(FP_SEG(P)),
  183.             FP_OFF(P) );
  184.  
  185.     Instance->ptr.Next = InstFreeList;
  186.     FreeSelector(FP_SEG(Instance));
  187.     InstFreeList = (PObjectInstance)P;
  188. }
  189.  
  190. unsigned short far _EXPOWLFUNC OWLGetVersion()
  191. {
  192.     return OWLVersion;
  193. }
  194.