]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/src/_extras.py
wxUniversal fixes:
[wxWidgets.git] / wxPython / src / _extras.py
index 79bcb574464274c5ec2925a481422cf8483e464f..68677a3da86edcbaf8ad12f66c39523bb4f9daf4 100644 (file)
@@ -6,7 +6,6 @@
 # Author:       Robin Dunn
 #
 # Created:      6/30/97
 # Author:       Robin Dunn
 #
 # Created:      6/30/97
-# RCS-ID:       $Id$
 # Copyright:    (c) 1998 by Total Control Software
 # Licence:      wxWindows license
 #----------------------------------------------------------------------------
 # Copyright:    (c) 1998 by Total Control Software
 # Licence:      wxWindows license
 #----------------------------------------------------------------------------
@@ -29,38 +28,6 @@ def _checkForCallback(obj, name, event, theID=-1):
     except: pass
     else:   obj.Connect(theID, -1, event, cb)
 
     except: pass
     else:   obj.Connect(theID, -1, event, cb)
 
-## def _StdWindowCallbacks(win):
-##     _checkForCallback(win, "OnChar",               wxEVT_CHAR)
-##     _checkForCallback(win, "OnSize",               wxEVT_SIZE)
-##     _checkForCallback(win, "OnEraseBackground",    wxEVT_ERASE_BACKGROUND)
-##     _checkForCallback(win, "OnSysColourChanged",   wxEVT_SYS_COLOUR_CHANGED)
-##     _checkForCallback(win, "OnInitDialog",         wxEVT_INIT_DIALOG)
-##     _checkForCallback(win, "OnPaint",              wxEVT_PAINT)
-##     _checkForCallback(win, "OnIdle",               wxEVT_IDLE)
-
-
-## def _StdFrameCallbacks(win):
-##     _StdWindowCallbacks(win)
-##     _checkForCallback(win, "OnActivate",           wxEVT_ACTIVATE)
-##     _checkForCallback(win, "OnMenuHighlight",      wxEVT_MENU_HIGHLIGHT)
-##     _checkForCallback(win, "OnCloseWindow",        wxEVT_CLOSE_WINDOW)
-
-
-## def _StdDialogCallbacks(win):
-##     _StdWindowCallbacks(win)
-##     _checkForCallback(win, "OnOk",     wxEVT_COMMAND_BUTTON_CLICKED,   wxID_OK)
-##     _checkForCallback(win, "OnApply",  wxEVT_COMMAND_BUTTON_CLICKED,   wxID_APPLY)
-##     _checkForCallback(win, "OnCancel", wxEVT_COMMAND_BUTTON_CLICKED,   wxID_CANCEL)
-##     _checkForCallback(win, "OnCloseWindow", wxEVT_CLOSE_WINDOW)
-##     _checkForCallback(win, "OnCharHook",    wxEVT_CHAR_HOOK)
-
-
-## def _StdOnScrollCallbacks(win):
-##     try:    cb = getattr(win, "OnScroll")
-##     except: pass
-##     else:   EVT_SCROLL(win, cb)
-
-
 
 #----------------------------------------------------------------------
 #----------------------------------------------------------------------
 
 #----------------------------------------------------------------------
 #----------------------------------------------------------------------
@@ -95,6 +62,12 @@ def EVT_KEY_DOWN(win, func):
 def EVT_KEY_UP(win, func):
     win.Connect(-1, -1, wxEVT_KEY_UP, func)
 
 def EVT_KEY_UP(win, func):
     win.Connect(-1, -1, wxEVT_KEY_UP, func)
 
+def EVT_MENU_OPEN(win, func):
+    win.Connect(-1, -1, wxEVT_MENU_OPEN, func)
+
+def EVT_MENU_CLOSE(win, func):
+    win.Connect(-1, -1, wxEVT_MENU_CLOSE, func)
+
 def EVT_MENU_HIGHLIGHT(win, id, func):
     win.Connect(id, -1, wxEVT_MENU_HIGHLIGHT, func)
 
 def EVT_MENU_HIGHLIGHT(win, id, func):
     win.Connect(id, -1, wxEVT_MENU_HIGHLIGHT, func)
 
@@ -107,6 +80,9 @@ def EVT_SET_FOCUS(win, func):
 def EVT_KILL_FOCUS(win, func):
     win.Connect(-1, -1, wxEVT_KILL_FOCUS, func)
 
 def EVT_KILL_FOCUS(win, func):
     win.Connect(-1, -1, wxEVT_KILL_FOCUS, func)
 
+def EVT_CHILD_FOCUS(win, func):
+    win.Connect(-1, -1, wxEVT_CHILD_FOCUS, func)
+
 def EVT_ACTIVATE(win, func):
     win.Connect(-1, -1, wxEVT_ACTIVATE, func)
 
 def EVT_ACTIVATE(win, func):
     win.Connect(-1, -1, wxEVT_ACTIVATE, func)
 
@@ -128,6 +104,9 @@ def EVT_INIT_DIALOG(win, func):
 def EVT_SYS_COLOUR_CHANGED(win, func):
     win.Connect(-1, -1, wxEVT_SYS_COLOUR_CHANGED, func)
 
 def EVT_SYS_COLOUR_CHANGED(win, func):
     win.Connect(-1, -1, wxEVT_SYS_COLOUR_CHANGED, func)
 
+def EVT_DISPLAY_CHANGED(win, func):
+    win.Connect(-1, -1, wxEVT_DISPLAY_CHANGED, func)
+
 def EVT_SHOW(win, func):
     win.Connect(-1, -1, wxEVT_SHOW, func)
 
 def EVT_SHOW(win, func):
     win.Connect(-1, -1, wxEVT_SHOW, func)
 
@@ -201,6 +180,8 @@ def EVT_LEAVE_WINDOW(win, func):
 def EVT_ENTER_WINDOW(win, func):
     win.Connect(-1, -1, wxEVT_ENTER_WINDOW, func)
 
 def EVT_ENTER_WINDOW(win, func):
     win.Connect(-1, -1, wxEVT_ENTER_WINDOW, func)
 
+def EVT_MOUSEWHEEL(win, func):
+    win.Connect(-1, -1, wxEVT_MOUSEWHEEL, func)
 
 # all mouse events
 def EVT_MOUSE_EVENTS(win, func):
 
 # all mouse events
 def EVT_MOUSE_EVENTS(win, func):
@@ -217,6 +198,9 @@ def EVT_MOUSE_EVENTS(win, func):
     win.Connect(-1, -1, wxEVT_LEAVE_WINDOW,  func)
     win.Connect(-1, -1, wxEVT_ENTER_WINDOW,  func)
 
     win.Connect(-1, -1, wxEVT_LEAVE_WINDOW,  func)
     win.Connect(-1, -1, wxEVT_ENTER_WINDOW,  func)
 
+def EVT_MOUSE_CAPTURE_CHANGED(win, func):
+    win.Connect(-1, -1, wxEVT_MOUSE_CAPTURE_CHANGED, func)
+
 # EVT_COMMAND
 def EVT_COMMAND(win, id, cmd, func):
     win.Connect(id, -1, cmd, func)
 # EVT_COMMAND
 def EVT_COMMAND(win, id, cmd, func):
     win.Connect(id, -1, cmd, func)
@@ -392,6 +376,12 @@ def EVT_TEXT(win, id, func):
 def EVT_TEXT_ENTER(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_TEXT_ENTER, func)
 
 def EVT_TEXT_ENTER(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_TEXT_ENTER, func)
 
+def EVT_TEXT_URL(win, id, func):
+    win.Connect(id, -1, wxEVT_COMMAND_TEXT_URL, func)
+
+def EVT_TEXT_MAXLEN(win, id, func):
+    win.Connect(id, -1, wxEVT_COMMAND_TEXT_MAXLEN, func)
+
 def EVT_MENU(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_MENU_SELECTED, func)
 
 def EVT_MENU(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_MENU_SELECTED, func)
 
@@ -468,59 +458,6 @@ def EVT_NOTEBOOK_PAGE_CHANGING(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, func)
 
 
     win.Connect(id, -1, wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, func)
 
 
-# wxTreeCtrl events
-def EVT_TREE_BEGIN_DRAG(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_BEGIN_DRAG, func)
-
-def EVT_TREE_BEGIN_RDRAG(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_BEGIN_RDRAG, func)
-
-def EVT_TREE_BEGIN_LABEL_EDIT(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, func)
-
-def EVT_TREE_END_LABEL_EDIT(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_END_LABEL_EDIT, func)
-
-def EVT_TREE_GET_INFO(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_GET_INFO, func)
-
-def EVT_TREE_SET_INFO(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_SET_INFO, func)
-
-def EVT_TREE_ITEM_EXPANDED(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_EXPANDED, func)
-
-def EVT_TREE_ITEM_EXPANDING(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_EXPANDING, func)
-
-def EVT_TREE_ITEM_COLLAPSED(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_COLLAPSED, func)
-
-def EVT_TREE_ITEM_COLLAPSING(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_COLLAPSING, func)
-
-def EVT_TREE_SEL_CHANGED(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_SEL_CHANGED, func)
-
-def EVT_TREE_SEL_CHANGING(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_SEL_CHANGING, func)
-
-def EVT_TREE_KEY_DOWN(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_KEY_DOWN, func)
-
-def EVT_TREE_DELETE_ITEM(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_DELETE_ITEM, func)
-
-def EVT_TREE_ITEM_ACTIVATED(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_ACTIVATED, func)
-
-def EVT_TREE_ITEM_RIGHT_CLICK(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, func)
-
-def EVT_TREE_ITEM_MIDDLE_CLICK(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK, func)
-
-
 # wxSpinButton
 def EVT_SPIN_UP(win, id, func):
     win.Connect(id, -1, wxEVT_SCROLL_LINEUP, func)
 # wxSpinButton
 def EVT_SPIN_UP(win, id, func):
     win.Connect(id, -1, wxEVT_SCROLL_LINEUP, func)
@@ -532,8 +469,6 @@ def EVT_SPIN(win, id, func):
     win.Connect(id, -1, wxEVT_SCROLL_THUMBTRACK,func)
 
 
     win.Connect(id, -1, wxEVT_SCROLL_THUMBTRACK,func)
 
 
-
-
 # wxTaskBarIcon
 def EVT_TASKBAR_MOVE(win, func):
     win.Connect(-1, -1, wxEVT_TASKBAR_MOVE, func)
 # wxTaskBarIcon
 def EVT_TASKBAR_MOVE(win, func):
     win.Connect(-1, -1, wxEVT_TASKBAR_MOVE, func)
@@ -557,35 +492,6 @@ def EVT_TASKBAR_RIGHT_DCLICK(win, func):
     win.Connect(-1, -1, wxEVT_TASKBAR_RIGHT_DCLICK, func)
 
 
     win.Connect(-1, -1, wxEVT_TASKBAR_RIGHT_DCLICK, func)
 
 
-## # wxGrid  *** THE OLD ONE ***
-## def EVT_GRID_SELECT_CELL(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_SELECT_CELL, fn)
-
-## def EVT_GRID_CREATE_CELL(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CREATE_CELL, fn)
-
-## def EVT_GRID_CHANGE_LABELS(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CHANGE_LABELS, fn)
-
-## def EVT_GRID_CHANGE_SEL_LABEL(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CHANGE_SEL_LABEL, fn)
-
-## def EVT_GRID_CELL_CHANGE(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CELL_CHANGE, fn)
-
-## def EVT_GRID_CELL_LCLICK(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CELL_LCLICK, fn)
-
-## def EVT_GRID_CELL_RCLICK(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_CELL_RCLICK, fn)
-
-## def EVT_GRID_LABEL_LCLICK(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_LABEL_LCLICK, fn)
-
-## def EVT_GRID_LABEL_RCLICK(win, fn):
-##     win.Connect(-1, -1, wxEVT_GRID_LABEL_RCLICK, fn)
-
-
 # wxSashWindow
 def EVT_SASH_DRAGGED(win, id, func):
     win.Connect(id, -1, wxEVT_SASH_DRAGGED, func)
 # wxSashWindow
 def EVT_SASH_DRAGGED(win, id, func):
     win.Connect(id, -1, wxEVT_SASH_DRAGGED, func)
@@ -600,58 +506,6 @@ def EVT_CALCULATE_LAYOUT(win, func):
     win.Connect(-1, -1, wxEVT_EVT_CALCULATE_LAYOUT, func)
 
 
     win.Connect(-1, -1, wxEVT_EVT_CALCULATE_LAYOUT, func)
 
 
-# wxListCtrl
-def EVT_LIST_BEGIN_DRAG(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_BEGIN_DRAG, func)
-
-def EVT_LIST_BEGIN_RDRAG(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_BEGIN_RDRAG, func)
-
-def EVT_LIST_BEGIN_LABEL_EDIT(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, func)
-
-def EVT_LIST_END_LABEL_EDIT(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_END_LABEL_EDIT, func)
-
-def EVT_LIST_DELETE_ITEM(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_DELETE_ITEM, func)
-
-def EVT_LIST_DELETE_ALL_ITEMS(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, func)
-
-def EVT_LIST_GET_INFO(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_GET_INFO, func)
-
-def EVT_LIST_SET_INFO(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_SET_INFO, func)
-
-def EVT_LIST_ITEM_SELECTED(win, id, func):
-    win.Connect(id, -1,  wxEVT_COMMAND_LIST_ITEM_SELECTED, func)
-
-def EVT_LIST_ITEM_DESELECTED(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_ITEM_DESELECTED, func)
-
-def EVT_LIST_KEY_DOWN(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_KEY_DOWN, func)
-
-def EVT_LIST_INSERT_ITEM(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_INSERT_ITEM, func)
-
-def EVT_LIST_COL_CLICK(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_COL_CLICK, func)
-
-def EVT_LIST_ITEM_RIGHT_CLICK(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, func)
-
-def EVT_LIST_ITEM_MIDDLE_CLICK(win, id, func):
-    win.Connect(id, -1, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK, func)
-
-def EVT_LIST_ITEM_ACTIVATED(win, id, func):
-    win.Connect(id, -1,  wxEVT_COMMAND_LIST_ITEM_ACTIVATED, func)
-
-
-
-
 #wxSplitterWindow
 def EVT_SPLITTER_SASH_POS_CHANGING(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, func)
 #wxSplitterWindow
 def EVT_SPLITTER_SASH_POS_CHANGING(win, id, func):
     win.Connect(id, -1, wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, func)
@@ -677,10 +531,10 @@ def EVT_END_PROCESS(eh, id, func):
 
 # wxJoyStick
 def EVT_JOY_DOWN(win, func):
 
 # wxJoyStick
 def EVT_JOY_DOWN(win, func):
-    win.Connect(-1, -1, wxEVT_JOY_DOWN, func)
+    win.Connect(-1, -1, wxEVT_JOY_BUTTON_DOWN, func)
 
 def EVT_JOY_UP(win, func):
 
 def EVT_JOY_UP(win, func):
-    win.Connect(-1, -1, wxEVT_JOY_UP, func)
+    win.Connect(-1, -1, wxEVT_JOY_BUTTON_UP, func)
 
 def EVT_JOY_MOVE(win, func):
     win.Connect(-1, -1, wxEVT_JOY_MOVE, func)
 
 def EVT_JOY_MOVE(win, func):
     win.Connect(-1, -1, wxEVT_JOY_MOVE, func)
@@ -689,11 +543,21 @@ def EVT_JOY_ZMOVE(win, func):
     win.Connect(-1, -1, wxEVT_JOY_ZMOVE, func)
 
 def EVT_JOYSTICK_EVENTS(win, func):
     win.Connect(-1, -1, wxEVT_JOY_ZMOVE, func)
 
 def EVT_JOYSTICK_EVENTS(win, func):
-    win.Connect(-1, -1, wxEVT_JOY_DOWN, func)
-    win.Connect(-1, -1, wxEVT_JOY_UP, func)
+    win.Connect(-1, -1, wxEVT_JOY_BUTTON_DOWN, func)
+    win.Connect(-1, -1, wxEVT_JOY_BUTTON_UP, func)
     win.Connect(-1, -1, wxEVT_JOY_MOVE, func)
     win.Connect(-1, -1, wxEVT_JOY_ZMOVE, func)
 
     win.Connect(-1, -1, wxEVT_JOY_MOVE, func)
     win.Connect(-1, -1, wxEVT_JOY_ZMOVE, func)
 
+
+def EVT_TOGGLEBUTTON(win, id, func):
+    win.Connect(id, -1, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, func)
+
+
+
+def EVT_CONTEXT_MENU(win, func):
+    win.Connect(-1, -1, wxEVT_CONTEXT_MENU, func)
+
+
 #----------------------------------------------------------------------
 
 class wxTimer(wxPyTimer):
 #----------------------------------------------------------------------
 
 class wxTimer(wxPyTimer):
@@ -706,34 +570,46 @@ class wxTimer(wxPyTimer):
             self.SetOwner(evtHandler, id)
 
 #----------------------------------------------------------------------
             self.SetOwner(evtHandler, id)
 
 #----------------------------------------------------------------------
-# Some wxWin methods can take "NULL" as parameters, but the shadow classes
-# expect an object with the SWIG pointer as a 'this' member.  This class
-# and instance fools the shadow into passing the NULL pointer.
-
-## NOTE:  This is not needed anymore as None can be passed instead and
-#         will be interpreted as NULL.
+# aliases
 
 
-class _NullObj:
-    this = 'NULL'       # SWIG converts this to (void*)0
+wxColor      = wxColour
+wxNamedColor = wxNamedColour
+wxPen        = wxPyPen
+wxScrollbar  = wxScrollBar
 
 
-NULL = _NullObj()
 
 
+# backwards compatibility
+wxNoRefBitmap       = wxBitmap
+wxPyDefaultPosition = wxDefaultPosition
+wxPyDefaultSize     = wxDefaultSize
+NULL                = None
+wxSystemSettings_GetSystemColour = wxSystemSettings_GetColour
+wxSystemSettings_GetSystemFont   = wxSystemSettings_GetFont
+wxSystemSettings_GetSystemMetric = wxSystemSettings_GetMetric
 
 #----------------------------------------------------------------------
 
 #----------------------------------------------------------------------
-# aliases
+# wxGTK sets the locale when initialized.  Doing this at the Python
+# level should set it up to match what GTK is doing at the C level.
+if wxPlatform == "__WXGTK__":
+    try:
+        import locale
+        locale.setlocale(locale.LC_ALL, "")
+    except:
+        pass
 
 
-wxColor      = wxColour
-wxNamedColor = wxNamedColour
 
 
-wxPyDefaultPosition.Set(-1,-1)
-wxPyDefaultSize.Set(-1,-1)
 
 
-# aliases so that C++ documentation applies:
-wxDefaultPosition  = wxPyDefaultPosition
-wxDefaultSize      = wxPyDefaultSize
+#----------------------------------------------------------------------
+# wxWindows version numbers.  wxPython version is in __version__.
+
+wxMAJOR_VERSION   = wxc.wxMAJOR_VERSION
+wxMINOR_VERSION   = wxc.wxMINOR_VERSION
+wxRELEASE_NUMBER  = wxc.wxRELEASE_NUMBER
+wxVERSION_STRING  = wxc.wxVERSION_STRING
+wxVERSION_NUMBER  = wxc.wxVERSION_NUMBER
+
+wxVERSION = (wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER)
 
 
-# backwards compatibility
-wxNoRefBitmap      = wxBitmap
 
 #----------------------------------------------------------------------
 # This helper function will take a wxPython object and convert it to
 
 #----------------------------------------------------------------------
 # This helper function will take a wxPython object and convert it to
@@ -757,40 +633,79 @@ wxNoRefBitmap      = wxBitmap
 #
 
 def wxPyTypeCast(obj, typeStr):
 #
 
 def wxPyTypeCast(obj, typeStr):
+    if obj is None:
+        return None
+    theClass = globals()[typeStr+"Ptr"]
+    typeStr = __wxPyPtrTypeMap.get(typeStr, typeStr)
     if hasattr(obj, "this"):
     if hasattr(obj, "this"):
+        # if already the right type then just return it
+        if isinstance(obj, theClass) or obj.__class__ is theClass:
+            return obj
         newPtr = ptrcast(obj.this, typeStr+"_p")
     else:
         newPtr = ptrcast(obj, typeStr+"_p")
         newPtr = ptrcast(obj.this, typeStr+"_p")
     else:
         newPtr = ptrcast(obj, typeStr+"_p")
-    theClass = globals()[typeStr+"Ptr"]
     theObj = theClass(newPtr)
     if hasattr(obj, "this"):
         theObj.thisown = obj.thisown
     return theObj
 
 
     theObj = theClass(newPtr)
     if hasattr(obj, "this"):
         theObj.thisown = obj.thisown
     return theObj
 
 
+#----------------------------------------------------------------------------
+
+class wxPyDeadObjectError(AttributeError):
+    pass
+
+class _wxPyDeadObject:
+    """
+    Instances of wx objects that are OOR capable will have their __class__
+    changed to this class when the C++ object is deleted.  This should help
+    prevent crashes due to referencing a bogus C++ pointer.
+    """
+    reprStr = "wxPython wrapper for DELETED %s object! (The C++ object no longer exists.)"
+    attrStr = "The C++ part of the %s object has been deleted, attribute access no longer allowed."
+
+    def __repr__( self ):
+        if not hasattr(self, "_name"):
+            self._name = "[unknown]"
+        return self.reprStr % self._name
+
+    def __getattr__( self, *args ):
+        if not hasattr(self, "_name"):
+            self._name = "[unknown]"
+        raise wxPyDeadObjectError( self.attrStr % self._name )
+
+    def __nonzero__(self):
+        return 0
+
+
+#----------------------------------------------------------------------
 #----------------------------------------------------------------------
 
 class wxPyOnDemandOutputWindow:
     def __init__(self, title = "wxPython: stdout/stderr"):
         self.frame  = None
         self.title  = title
 #----------------------------------------------------------------------
 
 class wxPyOnDemandOutputWindow:
     def __init__(self, title = "wxPython: stdout/stderr"):
         self.frame  = None
         self.title  = title
-
+        self.parent = None
 
     def SetParent(self, parent):
         self.parent = parent
 
 
     def SetParent(self, parent):
         self.parent = parent
 
-
     def OnCloseWindow(self, event):
         if self.frame != None:
             self.frame.Destroy()
         self.frame = None
         self.text  = None
 
     def OnCloseWindow(self, event):
         if self.frame != None:
             self.frame.Destroy()
         self.frame = None
         self.text  = None
 
-
-    # this provides the file-like behaviour
+    # These methods provide the file-like output behaviour.
     def write(self, str):
     def write(self, str):
+        if not wxThread_IsMain():
+            # Aquire the GUI mutex before making GUI calls.  Mutex is released
+            # when locker is deleted at the end of this function.
+            locker = wxMutexGuiLocker()
+
         if not self.frame:
         if not self.frame:
-            self.frame = wxFrame(self.parent, -1, self.title)
+            self.frame = wxFrame(self.parent, -1, self.title,
+                                 style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
             self.text  = wxTextCtrl(self.frame, -1, "",
                                     style = wxTE_MULTILINE|wxTE_READONLY)
             self.frame.SetSize(wxSize(450, 300))
             self.text  = wxTextCtrl(self.frame, -1, "",
                                     style = wxTE_MULTILINE|wxTE_READONLY)
             self.frame.SetSize(wxSize(450, 300))
@@ -798,13 +713,11 @@ class wxPyOnDemandOutputWindow:
             EVT_CLOSE(self.frame, self.OnCloseWindow)
         self.text.AppendText(str)
 
             EVT_CLOSE(self.frame, self.OnCloseWindow)
         self.text.AppendText(str)
 
-
     def close(self):
         if self.frame != None:
     def close(self):
         if self.frame != None:
-            self.frame.Destroy()
-        self.frame = None
-        self.text  = None
-
+            if not wxThread_IsMain():
+                locker = wxMutexGuiLocker()
+            self.frame.Close()
 
 
 _defRedirect = (wxPlatform == '__WXMSW__')
 
 
 _defRedirect = (wxPlatform == '__WXMSW__')
@@ -837,7 +750,6 @@ class wxApp(wxPyApp):
     def SetTopWindow(self, frame):
         if self.stdioWin:
             self.stdioWin.SetParent(frame)
     def SetTopWindow(self, frame):
         if self.stdioWin:
             self.stdioWin.SetParent(frame)
-            sys.stderr = sys.stdout = self.stdioWin
         wxPyApp.SetTopWindow(self, frame)
 
 
         wxPyApp.SetTopWindow(self, frame)
 
 
@@ -851,18 +763,18 @@ class wxApp(wxPyApp):
             sys.stdout = sys.stderr = open(filename, 'a')
         else:
             self.stdioWin = self.outputWindowClass() # wxPyOnDemandOutputWindow
             sys.stdout = sys.stderr = open(filename, 'a')
         else:
             self.stdioWin = self.outputWindowClass() # wxPyOnDemandOutputWindow
+            sys.stdout = sys.stderr = self.stdioWin
 
 
     def RestoreStdio(self):
         sys.stdout, sys.stderr = self.saveStdio
 
 
     def RestoreStdio(self):
         sys.stdout, sys.stderr = self.saveStdio
-        if self.stdioWin != None:
-            self.stdioWin.close()
+
 
 #----------------------------------------------------------------------------
 
 class wxPySimpleApp(wxApp):
 
 #----------------------------------------------------------------------------
 
 class wxPySimpleApp(wxApp):
-    def __init__(self):
-        wxApp.__init__(self, 0)
+    def __init__(self, flag=0):
+        wxApp.__init__(self, flag)
     def OnInit(self):
         return true
 
     def OnInit(self):
         return true
 
@@ -882,10 +794,10 @@ class wxPyWidgetTester(wxApp):
         self.frame.Show(true)
 
 #----------------------------------------------------------------------------
         self.frame.Show(true)
 
 #----------------------------------------------------------------------------
-# DO NOT hold any other references to this object.  This is how we know when
-# to cleanup system resources that wxWin is holding.  When this module is
-# unloaded, the refcount on __cleanMeUp goes to zero and it calls the
-# wxApp_CleanUp function.
+# DO NOT hold any other references to this object.  This is how we
+# know when to cleanup system resources that wxWin is holding.  When
+# the sys module is unloaded, the refcount on sys.__wxPythonCleanup
+# goes to zero and it calls the wxApp_CleanUp function.
 
 class __wxPyCleanup:
     def __init__(self):
 
 class __wxPyCleanup:
     def __init__(self):
@@ -893,8 +805,15 @@ class __wxPyCleanup:
     def __del__(self):
         self.cleanup()
 
     def __del__(self):
         self.cleanup()
 
-__cleanMeUp = __wxPyCleanup()
-#----------------------------------------------------------------------------
+sys.__wxPythonCleanup = __wxPyCleanup()
 
 
+## # another possible solution, but it gets called too early...
+## if sys.version[0] == '2':
+##     import atexit
+##     atexit.register(wxc.wxApp_CleanUp)
+## else:
+##     sys.exitfunc = wxc.wxApp_CleanUp
 
 
 
 
+#----------------------------------------------------------------------------
+#----------------------------------------------------------------------------