From a434b43fd4ac9db00fa6115ab40449613595f1a2 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Thu, 19 Jun 2003 23:31:19 +0000
Subject: [PATCH] added wxIsDebuggerRunning() (which is going to replace the
 mess in log.cpp)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21268 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/function.tex |  15 ++-
 include/wx/debug.h         |  14 +++
 src/mac/carbon/utils.cpp   | 187 +++++++++++++++++++++++++++++--------
 src/mac/utils.cpp          | 187 +++++++++++++++++++++++++++++--------
 4 files changed, 328 insertions(+), 75 deletions(-)

diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 6df137cc7f..6ac9187696 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -144,6 +144,7 @@ the corresponding topic.
 \helpref{wxIsAbsolutePath}{wxisabsolutepath}\\
 \helpref{wxIsBusy}{wxisbusy}\\
 \helpref{wxIsClipboardFormatAvailable}{wxisclipboardformatavailable}\\
+\helpref{wxIsDebuggerRunning}{wxisdebuggerrunning}\\
 \helpref{wxIsEmpty}{wxisempty}\\
 \helpref{wxIsNaN}{wxisnan}\\
 \helpref{wxIsWild}{wxiswild}\\
@@ -451,7 +452,7 @@ the corresponding functions \helpref{::wxPostEvent}{wxpostevent} and
 
 \wxheading{Include files}
 
-<wx/app.h>
+<wx/event.h>
 
 \section{Process control functions}\label{processfunctions}
 
@@ -3615,6 +3616,18 @@ In release mode this function does nothing.
 
 <wx/debug.h>
 
+
+\membersection{::wxIsDebuggerRunning}\label{wxisdebuggerrunning}
+
+\func{bool}{wxIsDebuggerRunning}{\void}
+
+Returns {\tt true} if the program is running under debugger, {\tt false} 
+otherwise.
+
+Please note that this function is currently only implemented for Mac builds
+using CodeWarrior and always returns {\tt false} elsewhere.
+
+
 \section{Environment access functions}\label{environfunctions}
 
 The functions in this section allow to access (get) or change value of
diff --git a/include/wx/debug.h b/include/wx/debug.h
index 2a8a9b44bc..cae70c2e92 100644
--- a/include/wx/debug.h
+++ b/include/wx/debug.h
@@ -218,5 +218,19 @@ WXDLLEXPORT_DATA(extern const bool) wxFalse;
     wxCOMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size, \
                           wxMAKE_BITSIZE_MSG(type, size))
 
+// ----------------------------------------------------------------------------
+// other miscellaneous debugger-related functions
+// ----------------------------------------------------------------------------
+
+// return true if we're running under debugger
+//
+// currently this only really works under Mac in CodeWarrior builds, it always
+// returns false otherwise
+#ifdef __WXMAC__
+    extern bool WXDLLEXPORT wxIsDebuggerRunning();
+#else // !Mac
+    inline bool WXDLLEXPORT wxIsDebuggerRunning() { return false; }
+#endif // Mac/!Mac
+
 #endif  // _WX_DEBUG_H_
 
diff --git a/src/mac/carbon/utils.cpp b/src/mac/carbon/utils.cpp
index 126527c1be..18e1822aa1 100644
--- a/src/mac/carbon/utils.cpp
+++ b/src/mac/carbon/utils.cpp
@@ -42,6 +42,15 @@
 #include "TextCommon.h"
 #include "TextEncodingConverter.h"
 
+#if defined(__WXMAC__)
+  #include  "wx/mac/private.h"  // includes mac headers
+#endif
+
+#if defined(__MWERKS__) && wxUSE_UNICODE
+    #include <wtime.h>
+#endif
+
+
 #ifndef __DARWIN__
 // defined in unix/utilsunx.cpp for Mac OS X
 
@@ -175,43 +184,6 @@ void wxFlushEvents()
 {
 }
 
-#if WXWIN_COMPATIBILITY_2_2
-
-// Output a debug message, in a system dependent fashion.
-void wxDebugMsg(const char *fmt ...)
-{
-    va_list ap;
-    static char buffer[512];
-
-    if (!wxTheApp->GetWantDebugOutput())
-        return ;
-
-    va_start(ap, fmt);
-
-    vsprintf(buffer,fmt,ap) ;
-    strcat(buffer,";g") ;
-    c2pstr(buffer) ;
-    DebugStr((unsigned char*) buffer) ;
-
-    va_end(ap);
-}
-
-// Non-fatal error: pop up message box and (possibly) continue
-void wxError(const wxString& msg, const wxString& title)
-{
-      if (wxMessageBox(wxString::Format(wxT("%s\nContinue?"),msg), title, wxYES_NO) == wxID_NO )
-        wxExit();
-}
-
-// Fatal error: pop up message box and abort
-void wxFatalError(const wxString& msg, const wxString& title)
-{
-    wxMessageBox(wxString::Format(wxT("%s: %s"),title,msg));
-    wxExit();
-}
-
-#endif // WXWIN_COMPATIBILITY_2_2
-
 #endif // !__DARWIN__
 
 // Emit a beeeeeep
@@ -868,3 +840,144 @@ void wxMacStringToPascal( const wxChar * from , StringPtr to , bool pc2macEncodi
 
 #endif //TARGET_CARBON
 
+// ----------------------------------------------------------------------------
+// debugging support
+// ----------------------------------------------------------------------------
+
+#if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
+
+// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
+
+#ifndef __MetroNubUtils__
+#include "MetroNubUtils.h"
+#endif
+
+#ifndef __GESTALT__
+#include <Gestalt.h>
+#endif
+
+#if TARGET_API_MAC_CARBON
+
+    #include <CodeFragments.h>
+
+    EXTERN_API_C( long )
+    CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
+
+    ProcPtr gCallUniversalProc_Proc = NULL;
+
+#endif
+
+static MetroNubUserEntryBlock*    gMetroNubEntry = NULL;
+
+static long fRunOnce = false;
+
+/* ---------------------------------------------------------------------------
+        IsMetroNubInstalled
+   --------------------------------------------------------------------------- */
+
+Boolean IsMetroNubInstalled()
+{
+    if (!fRunOnce)
+    {
+        long result, value;
+
+        fRunOnce = true;
+        gMetroNubEntry = NULL;
+
+        if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
+        {
+            /* look for MetroNub's Gestalt selector */
+            if (Gestalt(kMetroNubUserSignature, &result) == noErr)
+            {
+
+            #if TARGET_API_MAC_CARBON
+                if (gCallUniversalProc_Proc == NULL)
+                {
+                    CFragConnectionID   connectionID;
+                    Ptr                 mainAddress;
+                    Str255              errorString;
+                    ProcPtr             symbolAddress;
+                    OSErr               err;
+                    CFragSymbolClass    symbolClass;
+
+                    symbolAddress = NULL;
+                    err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
+                                           &connectionID, &mainAddress, errorString);
+
+                    if (err != noErr)
+                    {
+                        gCallUniversalProc_Proc = NULL;
+                        goto end;
+                    }
+
+                    err = FindSymbol(connectionID, "\pCallUniversalProc",
+                                    (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
+
+                    if (err != noErr)
+                    {
+                        gCallUniversalProc_Proc = NULL;
+                        goto end;
+                    }
+                }
+            #endif
+
+                {
+                    MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
+
+                    /* make sure the version of the API is compatible */
+                    if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
+                        kMetroNubUserAPIVersion <= block->apiHiVersion)
+                        gMetroNubEntry = block;        /* success! */
+                }
+
+            }
+        }
+    }
+
+end:
+
+#if TARGET_API_MAC_CARBON
+    return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
+#else
+    return (gMetroNubEntry != NULL);
+#endif
+}
+
+/* ---------------------------------------------------------------------------
+        IsMWDebuggerRunning                                            [v1 API]
+   --------------------------------------------------------------------------- */
+
+Boolean IsMWDebuggerRunning()
+{
+    if (IsMetroNubInstalled())
+        return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
+    else
+        return false;
+}
+
+/* ---------------------------------------------------------------------------
+        AmIBeingMWDebugged                                            [v1 API]
+   --------------------------------------------------------------------------- */
+
+Boolean AmIBeingMWDebugged()
+{
+    if (IsMetroNubInstalled())
+        return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
+    else
+        return false;
+}
+
+extern bool WXDLLEXPORT wxIsDebuggerRunning()
+{
+    return IsMWDebuggerRunning() && AmIBeingMWDebugged();
+}
+
+#else
+
+extern bool WXDLLEXPORT wxIsDebuggerRunning()
+{
+    return false;
+}
+
+#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
+
diff --git a/src/mac/utils.cpp b/src/mac/utils.cpp
index 126527c1be..18e1822aa1 100644
--- a/src/mac/utils.cpp
+++ b/src/mac/utils.cpp
@@ -42,6 +42,15 @@
 #include "TextCommon.h"
 #include "TextEncodingConverter.h"
 
+#if defined(__WXMAC__)
+  #include  "wx/mac/private.h"  // includes mac headers
+#endif
+
+#if defined(__MWERKS__) && wxUSE_UNICODE
+    #include <wtime.h>
+#endif
+
+
 #ifndef __DARWIN__
 // defined in unix/utilsunx.cpp for Mac OS X
 
@@ -175,43 +184,6 @@ void wxFlushEvents()
 {
 }
 
-#if WXWIN_COMPATIBILITY_2_2
-
-// Output a debug message, in a system dependent fashion.
-void wxDebugMsg(const char *fmt ...)
-{
-    va_list ap;
-    static char buffer[512];
-
-    if (!wxTheApp->GetWantDebugOutput())
-        return ;
-
-    va_start(ap, fmt);
-
-    vsprintf(buffer,fmt,ap) ;
-    strcat(buffer,";g") ;
-    c2pstr(buffer) ;
-    DebugStr((unsigned char*) buffer) ;
-
-    va_end(ap);
-}
-
-// Non-fatal error: pop up message box and (possibly) continue
-void wxError(const wxString& msg, const wxString& title)
-{
-      if (wxMessageBox(wxString::Format(wxT("%s\nContinue?"),msg), title, wxYES_NO) == wxID_NO )
-        wxExit();
-}
-
-// Fatal error: pop up message box and abort
-void wxFatalError(const wxString& msg, const wxString& title)
-{
-    wxMessageBox(wxString::Format(wxT("%s: %s"),title,msg));
-    wxExit();
-}
-
-#endif // WXWIN_COMPATIBILITY_2_2
-
 #endif // !__DARWIN__
 
 // Emit a beeeeeep
@@ -868,3 +840,144 @@ void wxMacStringToPascal( const wxChar * from , StringPtr to , bool pc2macEncodi
 
 #endif //TARGET_CARBON
 
+// ----------------------------------------------------------------------------
+// debugging support
+// ----------------------------------------------------------------------------
+
+#if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
+
+// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
+
+#ifndef __MetroNubUtils__
+#include "MetroNubUtils.h"
+#endif
+
+#ifndef __GESTALT__
+#include <Gestalt.h>
+#endif
+
+#if TARGET_API_MAC_CARBON
+
+    #include <CodeFragments.h>
+
+    EXTERN_API_C( long )
+    CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
+
+    ProcPtr gCallUniversalProc_Proc = NULL;
+
+#endif
+
+static MetroNubUserEntryBlock*    gMetroNubEntry = NULL;
+
+static long fRunOnce = false;
+
+/* ---------------------------------------------------------------------------
+        IsMetroNubInstalled
+   --------------------------------------------------------------------------- */
+
+Boolean IsMetroNubInstalled()
+{
+    if (!fRunOnce)
+    {
+        long result, value;
+
+        fRunOnce = true;
+        gMetroNubEntry = NULL;
+
+        if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
+        {
+            /* look for MetroNub's Gestalt selector */
+            if (Gestalt(kMetroNubUserSignature, &result) == noErr)
+            {
+
+            #if TARGET_API_MAC_CARBON
+                if (gCallUniversalProc_Proc == NULL)
+                {
+                    CFragConnectionID   connectionID;
+                    Ptr                 mainAddress;
+                    Str255              errorString;
+                    ProcPtr             symbolAddress;
+                    OSErr               err;
+                    CFragSymbolClass    symbolClass;
+
+                    symbolAddress = NULL;
+                    err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
+                                           &connectionID, &mainAddress, errorString);
+
+                    if (err != noErr)
+                    {
+                        gCallUniversalProc_Proc = NULL;
+                        goto end;
+                    }
+
+                    err = FindSymbol(connectionID, "\pCallUniversalProc",
+                                    (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
+
+                    if (err != noErr)
+                    {
+                        gCallUniversalProc_Proc = NULL;
+                        goto end;
+                    }
+                }
+            #endif
+
+                {
+                    MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
+
+                    /* make sure the version of the API is compatible */
+                    if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
+                        kMetroNubUserAPIVersion <= block->apiHiVersion)
+                        gMetroNubEntry = block;        /* success! */
+                }
+
+            }
+        }
+    }
+
+end:
+
+#if TARGET_API_MAC_CARBON
+    return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
+#else
+    return (gMetroNubEntry != NULL);
+#endif
+}
+
+/* ---------------------------------------------------------------------------
+        IsMWDebuggerRunning                                            [v1 API]
+   --------------------------------------------------------------------------- */
+
+Boolean IsMWDebuggerRunning()
+{
+    if (IsMetroNubInstalled())
+        return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
+    else
+        return false;
+}
+
+/* ---------------------------------------------------------------------------
+        AmIBeingMWDebugged                                            [v1 API]
+   --------------------------------------------------------------------------- */
+
+Boolean AmIBeingMWDebugged()
+{
+    if (IsMetroNubInstalled())
+        return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
+    else
+        return false;
+}
+
+extern bool WXDLLEXPORT wxIsDebuggerRunning()
+{
+    return IsMWDebuggerRunning() && AmIBeingMWDebugged();
+}
+
+#else
+
+extern bool WXDLLEXPORT wxIsDebuggerRunning()
+{
+    return false;
+}
+
+#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
+
-- 
2.50.0

