home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / MODULE.CPP < prev    next >
Text File  |  1995-08-29  |  7KB  |  259 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* ---------------------------------------------------------
  4.    MODULE.CPP
  5.    Implementation for the TModule class.  TModule defines the
  6.    basic behavior for OWL libraries and applications.
  7.    --------------------------------------------------------- */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <alloc.h>
  13. #include <applicat.h>
  14. #include <module.h>
  15. #include <mdi.h>
  16. #include <safepool.h>
  17.  
  18. #if defined(__DLL__)
  19. extern "C"
  20.     unsigned _WinAllocFlag;
  21. #endif
  22.  
  23. PTModule Module = (PTModule)NULL;
  24. extern long FAR PASCAL _export StdWndProc(HWND, WORD, WORD, LONG);
  25. extern FARPROC StdWndProcInstance;
  26. extern PTWindowsObject GetObjectPtr(HWND);
  27.  
  28. // The following version of _fstrdup is required in order to ensure
  29. // the allocation is done in GMEM_DDESHARE memory.  The local reference
  30. // to malloc will cause the _WinAllocFlag flag to be respected.
  31.  
  32. #if defined(__DLL__)
  33.  
  34. static LPSTR myfstrdup(LPSTR s)
  35. {
  36.         char far *p;
  37.         unsigned n;
  38.  
  39.         n = strlen(s) + 1;
  40.         if ((p = (char far *)malloc(n)) != NULL)
  41.                 memcpy(p, s, n);
  42.         return (p);
  43. }
  44.  
  45. #endif // defined(__DLL__)
  46.  
  47. /* Implementation of Constructor for a TModule object. */
  48. inline void TModule::__TModule(
  49.  
  50.                LPSTR AName, HINSTANCE AnInstance, LPSTR ACmdLine
  51.         
  52.                )
  53. {
  54.   Status = 0;
  55.   Name = AName;
  56.   hInstance = AnInstance;
  57.  
  58. #if defined(__DLL__)
  59.   // ensure lpCmdLine is allocated in GMEM_DDESHARE memory when code is
  60.   // in OWL DLL.
  61.   lpCmdLine = myfstrdup(ACmdLine? ACmdLine: "");
  62. #else
  63.   lpCmdLine = _fstrdup(ACmdLine? ACmdLine: "");
  64. #endif // defined(__DLL__)
  65.  
  66.   if ( !Module )  // our Module global is for the OWL-DLL
  67.     Module = this;
  68.  
  69.   if ( !StdWndProcInstance )
  70.     StdWndProcInstance = MakeProcInstance((FARPROC)StdWndProc, hInstance);
  71.   if ( !StdWndProcInstance )
  72.     Status = EM_INVALIDMODULE;
  73. }
  74.  
  75. #ifdef WIN31
  76. /* WIN31 Constructor for a TModule object. */
  77. TModule::TModule(LPSTR AName, HINSTANCE AnInstance, LPSTR ACmdLine)
  78. {
  79.   __TModule(AName, AnInstance, ACmdLine);
  80. }
  81. #endif
  82.  
  83. #ifdef WIN30
  84. /* WIN30 Constructor for a TModule object. */
  85. TModule::TModule(LPSTR AName, HINSTANCE_30 AnInstance, LPSTR ACmdLine)
  86. {
  87.   __TModule(AName, HINSTANCE( AnInstance ), ACmdLine);
  88. }
  89. #endif
  90.  
  91. TModule::~TModule()
  92. {
  93.   if ( HIWORD(lpCmdLine) )
  94.     farfree(lpCmdLine);
  95.   if ( this == Module ) // our Module global is for the OWL-DLL
  96.     FreeProcInstance(StdWndProcInstance);
  97. }
  98.  
  99. /* check if safety pool has been dipped into */
  100. BOOL TModule::LowMemory()
  101. {
  102.   return SafetyPool::IsExhausted();
  103. }
  104.  
  105. /* Restores memory to the safety pool. */
  106. void TModule::RestoreMemory()
  107. {
  108.   if ( LowMemory() )
  109.     SafetyPool::Allocate();
  110. }
  111.  
  112. /* Determines whether or not the passed TWindowsObject can be considered
  113.    valid.  Returns a pointer to the TWindowsObject if valid. If invalid,
  114.    calls Error and disposes of the TWindowsObject, returning  NULL.  A
  115.    TWindowsObject is considered invalid if a low memory condition exists
  116.    or if the TWindowsObject has a non-zero status. */
  117. PTWindowsObject TModule::ValidWindow(PTWindowsObject AWindowsObject)
  118. {
  119.   if ( AWindowsObject )
  120.   {
  121.     if ( LowMemory() )
  122.     {
  123.       Error(EM_OUTOFMEMORY);
  124.       delete AWindowsObject;
  125.       RestoreMemory();
  126.     }
  127.     else
  128.       if ( AWindowsObject->Status != 0 )
  129.       {
  130.         Error(AWindowsObject->Status);
  131.         delete AWindowsObject;
  132.       }
  133.       else
  134.         return AWindowsObject;
  135.   }
  136.   return NULL;
  137. }
  138.  
  139. /* Attempts to create a TWindowsObject, after performing checks of
  140.    safety pool usage (and subsequent cleanup in low memory conditions).
  141.    If a valid TWindowsObject could not be created, calls Error,
  142.    disposes of the TWindowsObject and returns a NULL pointer. */
  143. PTWindowsObject TModule::MakeWindow(PTWindowsObject AWindowsObject)
  144. {
  145.   if ( AWindowsObject && ValidWindow(AWindowsObject) )
  146.     if ( (AWindowsObject->Create()) )
  147.       return AWindowsObject;
  148.     else
  149.     {
  150.       Error(AWindowsObject->Status);
  151.       AWindowsObject->ShutDownWindow();
  152.     }
  153.   return NULL;
  154. }
  155.  
  156. /* Attempts to execute the supplied TDialog, if the TDialog is valid.
  157.   If valid (the call to ValidWindow returns TRUE), calls Execute, and,
  158.   if a valid dialog could not be created, disposes of the TDialog.
  159.   Calls Error if Execute results in a non-zero Status.  Returns the
  160.   result of the call to Execute (or IDCANCEL if not called). */
  161. int TModule::ExecDialog(PTWindowsObject ADialog)
  162. {
  163.   int ReturnValue;
  164.  
  165.   if ( ValidWindow(ADialog) )
  166.   {
  167.     ReturnValue = ((PTDialog)ADialog)->Execute();
  168.     if ( ReturnValue < 0 )
  169.     {
  170.       Error(ADialog->Status);
  171.       delete ADialog;
  172.     }
  173.     return ReturnValue;
  174.   }
  175.   return IDCANCEL;
  176. }
  177.  
  178. HWND TModule::GetClientHandle(HWND AnHWindow)
  179. {
  180.   HWND CurWindow;
  181.   char ClassName[10];
  182.  
  183.   if ( AnHWindow )
  184.   {
  185.     if ( 0 != (CurWindow = GetWindow(AnHWindow, GW_CHILD)) )
  186.     {
  187.       do {
  188.         GetClassName(CurWindow, ClassName, sizeof ClassName);
  189.         if ( _fstrnicmp(ClassName, "MDICLIENT", 10) == 0 )
  190.           return CurWindow;
  191.       } while ( 0 != (CurWindow = GetWindow(CurWindow, GW_HWNDNEXT)) );
  192.     }
  193.   }
  194.   return 0;
  195. }
  196.  
  197. inline PTWindowsObject TModule::__GetParentObject(HWND ParentHandle)
  198. {
  199.   PTWindowsObject AWindowsObject;
  200.   HWND ClientHandle;
  201.  
  202.   if ( NULL != (AWindowsObject = GetObjectPtr(ParentHandle)) )
  203.     return AWindowsObject;
  204.   else
  205.     if ( 0 != (ClientHandle = GetClientHandle(ParentHandle)) )
  206.       return (PTWindowsObject)(new TMDIFrame(ParentHandle, ClientHandle,
  207.                   this));
  208.     else
  209.       return (PTWindowsObject)(new TWindow(ParentHandle, this));
  210. }
  211.  
  212. #ifdef WIN31
  213. PTWindowsObject TModule::GetParentObject(HWND ParentHandle)
  214.   { return __GetParentObject( ParentHandle ); }
  215. #endif
  216.  
  217. #ifdef WIN30
  218. PTWindowsObject TModule::GetParentObject(HWND_30 ParentHandle)
  219.   { return __GetParentObject( HWND( ParentHandle ) ); }
  220. #endif
  221.  
  222. /* Simple error handler; may be redefined to process errors. */
  223. void TModule::Error(int ErrorCode)
  224. {
  225.   char ErrorString[51];
  226.   PTModule TmpModule;
  227.  
  228.   wsprintf(ErrorString, "Error received: error code = %d\nOK to proceed ?",
  229.           ErrorCode);
  230.   TmpModule = GetApplicationObject();
  231.   if ( MessageBox(0, ErrorString,
  232.                   TmpModule ? TmpModule->Name : Name,
  233.                   MB_ICONSTOP | MB_YESNO) == IDNO )
  234.     PostAppMessage(GetCurrentTask(), WM_QUIT, 0, 0);
  235. }
  236.  
  237. #ifdef __DLL__
  238. int FAR PASCAL LibMain(HINSTANCE hInstance, WORD /*wDataSeg*/ ,
  239.                WORD cbHeapSize , LPSTR lpCmdLine )
  240. {
  241.    // ensure module is allocate in memory belonging to OWL DLL.
  242.    unsigned SavedWinAllocFlag = _WinAllocFlag;
  243.    _WinAllocFlag |= GMEM_DDESHARE;
  244.    new TModule("ObjectWindows DLL", hInstance, lpCmdLine);
  245.    _WinAllocFlag = SavedWinAllocFlag;
  246.  
  247.     if ( cbHeapSize > 0 )
  248.       UnlockData(0);
  249.     return Module->Status == 0;
  250. }
  251.  
  252. int FAR PASCAL WEP ( int /*bSystemExit*/ )
  253. {
  254.     delete Module;
  255.  
  256.     return 1;
  257. }
  258. #endif
  259.