home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / EXCEPT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.6 KB  |  153 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TXOwl.
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/except.h>
  9. #include <owl/module.h>
  10.  
  11. //
  12. // mbModalFlag() determines the best MB modal flag to use in the current
  13. // situation. Uses MB_TASKMODAL if under NT, or the task/thread has at least
  14. // one toplevel window. Uses MB_SYSTEMMODAL for win32s/win16 when there are
  15. // no windows.
  16. //
  17. #if defined(BI_PLAT_WIN32)
  18.  
  19. bool CALLBACK threadHasWnd(HWND, bool* has) { *has = true; return false; }
  20.  
  21. static unsigned mbModalFlag()
  22. {
  23.   if (!(::GetVersion()&0x80000000))  // NT can always open task modal
  24.     return MB_TASKMODAL;
  25.   bool has = false;
  26.   ::EnumThreadWindows(GetCurrentThreadId(), (WNDENUMPROC)threadHasWnd,
  27.                       LPARAM(&has));
  28.   return has ? MB_TASKMODAL : MB_SYSTEMMODAL;
  29. }
  30.  
  31. #else
  32.  
  33. bool CALLBACK taskHasWnd(HWND, bool far* has) { *has = true; return false; }
  34.  
  35. static unsigned mbModalFlag()
  36. {
  37.   bool has = false;
  38.   ::EnumTaskWindows(GetCurrentTask(), (WNDENUMPROC)taskHasWnd,
  39.                     LPARAM((bool far*)&has));
  40.   return has ? MB_TASKMODAL : MB_SYSTEMMODAL;
  41. }
  42.  
  43. #endif
  44.  
  45. //
  46. // Global exception handler used when an application object is not available.
  47. // May be overriden by user code by redefining this function.  If a valid
  48. // application object is found by GetApplicationObject, then the virtual
  49. // TModule::Error(TXOwl& x, char* caption, bool canResume) is usually used
  50. // instead.
  51. //
  52. int _OWLFUNC
  53. HandleGlobalException(xmsg& x, char* caption, char* canResume)
  54. {
  55.   char errorStr[255];
  56.   int  buttons = MB_OK;
  57.   int  len = x.why().length();
  58.  
  59.   if (!caption)
  60.     caption = "Unhandled Exception";
  61.   if (len)
  62.     strcpy(errorStr, x.why().c_str());
  63.   else {
  64.     strcpy(errorStr, "Unknown Exception");
  65.     len = strlen(errorStr);
  66.   }
  67.   if (canResume) {
  68.     buttons = MB_YESNO;
  69.     errorStr[len] = '\n';
  70.     strcpy(errorStr+len+1, canResume);
  71.   }
  72.   return ::MessageBox(0, errorStr, caption,
  73.                       mbModalFlag() | MB_ICONSTOP | buttons) == IDYES ? 0 : -1;
  74. }
  75.  
  76. //
  77. // Static member function used to convert a resource id to a 'string'.  This
  78. // is necessary since we must pass a string to the xmsg base class
  79. // constructor.  Sets found to true if the resource was located, otherwise
  80. // false.  In either case, the string is initialized to something
  81. // printable.
  82. //
  83. string
  84. TXOwl::ResourceIdToString(bool* found, unsigned resId, TModule* module)
  85. {
  86.   char buf[128];
  87.  
  88.   bool status = (module && module->LoadString(resId, buf, sizeof(buf)) != 0);
  89.   if (found)
  90.     *found = status;
  91.  
  92.   if (!status)
  93.     wsprintf(buf, "Exception #%u (no message available).", resId);
  94.  
  95.   string rscStr(buf);
  96.   return rscStr;
  97. }
  98.  
  99. TXOwl::TXOwl(const string& msg, uint resId)
  100.   : TXBase(msg), ResId(resId)
  101. {
  102. }
  103.  
  104. TXOwl::TXOwl(unsigned resId, TModule* module)
  105.   : TXBase(ResourceIdToString(0, resId, module)),
  106.     ResId(resId)
  107. {
  108. }
  109.  
  110. TXOwl::~TXOwl()
  111. {
  112. }
  113.  
  114. int
  115. TXOwl::Unhandled(TModule* app, unsigned promptResId)
  116. {
  117.   return app->Error(*this, IDS_OWLEXCEPTION, promptResId);
  118. }
  119.  
  120. TXOwl*
  121. TXOwl::Clone()
  122. {
  123.   return new TXOwl(*this);
  124. }
  125.  
  126. void
  127. TXOwl::Throw()
  128. {
  129.   THROW( *this );
  130. }
  131.  
  132. TXOutOfMemory::TXOutOfMemory()
  133. :
  134.   TXOwl(IDS_OUTOFMEMORY)
  135. {
  136. }
  137.  
  138. TXOutOfMemory::~TXOutOfMemory()
  139. {
  140. }
  141.  
  142. TXOutOfMemory*
  143. TXOutOfMemory::Clone()
  144. {
  145.   return new TXOutOfMemory(*this);
  146. }
  147.  
  148. void
  149. TXOutOfMemory::Throw()
  150. {
  151.   THROW( *this );
  152. }
  153.