]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/window.cpp
(run-time) fix for !wxUSE_IPC build
[wxWidgets.git] / src / msw / window.cpp
index 7996c88ecc9d782a19ce36f219dae76c6c6be1b2..ec39a8be1d37c814c329040582eff6ac716d9178 100644 (file)
 #endif
 
 #ifndef WX_PRECOMP
 #endif
 
 #ifndef WX_PRECOMP
+    #include <windows.h>
+    #include "wx/msw/winundef.h"
+    #include "wx/window.h"
+    #include "wx/accel.h"
     #include "wx/setup.h"
     #include "wx/menu.h"
     #include "wx/dc.h"
     #include "wx/setup.h"
     #include "wx/menu.h"
     #include "wx/dc.h"
     #include "wx/ownerdrw.h"
 #endif
 
     #include "wx/ownerdrw.h"
 #endif
 
-#if     wxUSE_DRAG_AND_DROP
-    #include "wx/msw/ole/droptgt.h"
+#if wxUSE_DRAG_AND_DROP
+    #include "wx/dnd.h"
 #endif
 
 #include "wx/menuitem.h"
 #include "wx/log.h"
 
 #endif
 
 #include "wx/menuitem.h"
 #include "wx/log.h"
 
+#include "wx/msw/private.h"
+
 #if wxUSE_TOOLTIPS
     #include "wx/tooltip.h"
 #endif
 
 #if wxUSE_TOOLTIPS
     #include "wx/tooltip.h"
 #endif
 
+#if wxUSE_CARET
+    #include "wx/caret.h"
+#endif // wxUSE_CARET
+
 #include "wx/intl.h"
 #include "wx/log.h"
 
 #include "wx/intl.h"
 #include "wx/log.h"
 
-#include "wx/msw/private.h"
-
 #include "wx/textctrl.h"
 #include "wx/textctrl.h"
+#include "wx/notebook.h"
 
 #include <string.h>
 
 
 #include <string.h>
 
-#ifndef __GNUWIN32__
+#if !defined(__GNUWIN32__)|| defined(wxUSE_NORLANDER_HEADERS)
     #include <shellapi.h>
     #include <mmsystem.h>
 #endif
     #include <shellapi.h>
     #include <mmsystem.h>
 #endif
     #include <windowsx.h>
 #endif
 
     #include <windowsx.h>
 #endif
 
-#if ( defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__ )
+#if ( defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__ ) || defined(wxUSE_NORLANDER_HEADERS)
     #include <commctrl.h>
 #endif
 
 #ifndef __TWIN32__
     #ifdef __GNUWIN32__
     #include <commctrl.h>
 #endif
 
 #ifndef __TWIN32__
     #ifdef __GNUWIN32__
-        #include <wx/msw/gnuwin32/extra.h>
+        #ifndef wxUSE_NORLANDER_HEADERS
+            #include "wx/msw/gnuwin32/extra.h"
+        #endif
     #endif
 #endif
 
     #endif
 #endif
 
-#include "wx/msw/winundef.h"
-
 // ---------------------------------------------------------------------------
 // macros
 // ---------------------------------------------------------------------------
 
 // standard macros missing from some compilers headers
 #ifndef GET_X_LPARAM
 // ---------------------------------------------------------------------------
 // macros
 // ---------------------------------------------------------------------------
 
 // standard macros missing from some compilers headers
 #ifndef GET_X_LPARAM
-        #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
-        #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
+    #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
+    #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
 #endif // GET_X_LPARAM
 
 // ---------------------------------------------------------------------------
 #endif // GET_X_LPARAM
 
 // ---------------------------------------------------------------------------
@@ -110,14 +119,14 @@ extern MSG s_currentMsg;
 
 wxMenu *wxCurrentPopupMenu = NULL;
 extern wxList WXDLLEXPORT wxPendingDelete;
 
 wxMenu *wxCurrentPopupMenu = NULL;
 extern wxList WXDLLEXPORT wxPendingDelete;
-extern char wxCanvasClassName[];
+extern const wxChar *wxCanvasClassName;
 
 // ---------------------------------------------------------------------------
 // private functions
 // ---------------------------------------------------------------------------
 
 // the window proc for all our windows
 
 // ---------------------------------------------------------------------------
 // private functions
 // ---------------------------------------------------------------------------
 
 // the window proc for all our windows
-LRESULT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message,
+LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message,
                                    WPARAM wParam, LPARAM lParam);
 
 #ifdef  __WXDEBUG__
                                    WPARAM wParam, LPARAM lParam);
 
 #ifdef  __WXDEBUG__
@@ -132,9 +141,7 @@ wxWindow *wxFindWinFromHandle(WXHWND hWnd);
 // event tables
 // ---------------------------------------------------------------------------
 
 // event tables
 // ---------------------------------------------------------------------------
 
-#if !USE_SHARED_LIBRARY
     IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
     IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
-#endif
 
 BEGIN_EVENT_TABLE(wxWindow, wxWindowBase)
     EVT_ERASE_BACKGROUND(wxWindow::OnEraseBackground)
 
 BEGIN_EVENT_TABLE(wxWindow, wxWindowBase)
     EVT_ERASE_BACKGROUND(wxWindow::OnEraseBackground)
@@ -152,8 +159,19 @@ END_EVENT_TABLE()
 // ---------------------------------------------------------------------------
 
 // Find an item given the MS Windows id
 // ---------------------------------------------------------------------------
 
 // Find an item given the MS Windows id
-wxWindow *wxWindow::FindItem(int id) const
+wxWindow *wxWindow::FindItem(long id) const
 {
 {
+    wxControl *item = wxDynamicCast(this, wxControl);
+    if ( item )
+    {
+        // i it we or one of our "internal" children?
+        if ( item->GetId() == id ||
+             (item->GetSubcontrols().Index(id) != wxNOT_FOUND) )
+        {
+            return item;
+        }
+    }
+
     wxWindowList::Node *current = GetChildren().GetFirst();
     while (current)
     {
     wxWindowList::Node *current = GetChildren().GetFirst();
     while (current)
     {
@@ -163,19 +181,6 @@ wxWindow *wxWindow::FindItem(int id) const
         if ( wnd )
             return wnd;
 
         if ( wnd )
             return wnd;
 
-        if ( childWin->IsKindOf(CLASSINFO(wxControl)) )
-        {
-            wxControl *item = (wxControl *)childWin;
-            if ( item->GetId() == id )
-                return item;
-            else
-            {
-                // In case it's a 'virtual' control (e.g. radiobox)
-                if ( item->GetSubcontrols().Member((wxObject *)id) )
-                    return item;
-            }
-        }
-
         current = current->GetNext();
     }
 
         current = current->GetNext();
     }
 
@@ -231,12 +236,6 @@ void wxWindow::Init()
     m_doubleClickAllowed = 0;
     m_winCaptured = FALSE;
 
     m_doubleClickAllowed = 0;
     m_winCaptured = FALSE;
 
-    // caret stuff: initially there is no caret at all
-    m_caretWidth =
-    m_caretHeight = 0;
-    m_caretEnabled =
-    m_caretShown = FALSE;
-
     m_isBeingDeleted = FALSE;
     m_oldWndProc = 0;
     m_useCtl3D = FALSE;
     m_isBeingDeleted = FALSE;
     m_oldWndProc = 0;
     m_useCtl3D = FALSE;
@@ -245,6 +244,11 @@ void wxWindow::Init()
     // wxWnd
     m_hMenu = 0;
 
     // wxWnd
     m_hMenu = 0;
 
+    m_hWnd = 0;
+
+    // pass WM_GETDLGCODE to DefWindowProc()
+    m_lDlgCode = 0;
+
     m_xThumbSize = 0;
     m_yThumbSize = 0;
     m_backgroundTransparent = FALSE;
     m_xThumbSize = 0;
     m_yThumbSize = 0;
     m_backgroundTransparent = FALSE;
@@ -275,11 +279,10 @@ wxWindow::~wxWindow()
     {
         if ( !::DestroyWindow(GetHwnd()) )
             wxLogLastError("DestroyWindow");
     {
         if ( !::DestroyWindow(GetHwnd()) )
             wxLogLastError("DestroyWindow");
-    }
 
 
-    // Restore old Window proc, if required and remove hWnd <-> wxWindow
-    // association
-    UnsubclassWin();
+        // remove hWnd <-> wxWindow association
+        wxRemoveHandleAssociation(this);
+    }
 }
 
 // real construction (Init() must have been called before!)
 }
 
 // real construction (Init() must have been called before!)
@@ -289,9 +292,10 @@ bool wxWindow::Create(wxWindow *parent, wxWindowID id,
                       long style,
                       const wxString& name)
 {
                       long style,
                       const wxString& name)
 {
-    wxCHECK_MSG( parent, FALSE, "can't create wxWindow without parent" );
+    wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindow without parent") );
 
 
-    CreateBase(parent, id, pos, size, style, name);
+    if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
+        return FALSE;
 
     parent->AddChild(this);
 
 
     parent->AddChild(this);
 
@@ -316,6 +320,14 @@ bool wxWindow::Create(wxWindow *parent, wxWindowID id,
         msflags |= WS_BORDER;
     }
 
         msflags |= WS_BORDER;
     }
 
+    // calculate the value to return from WM_GETDLGCODE handler
+    if ( GetWindowStyleFlag() & wxWANTS_CHARS )
+    {
+        // want everything: i.e. all keys and WM_CHAR message
+        m_lDlgCode = DLGC_WANTARROWS | DLGC_WANTCHARS |
+                     DLGC_WANTTAB | DLGC_WANTMESSAGE;
+    }
+
     MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL,
               pos.x, pos.y,
               WidthDefault(size.x), HeightDefault(size.y),
     MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL,
               pos.x, pos.y,
               WidthDefault(size.x), HeightDefault(size.y),
@@ -356,6 +368,15 @@ bool wxWindow::Enable(bool enable)
     if ( hWnd )
         ::EnableWindow(hWnd, (BOOL)enable);
 
     if ( hWnd )
         ::EnableWindow(hWnd, (BOOL)enable);
 
+    wxWindowList::Node *node = GetChildren().GetFirst();
+    while ( node )
+    {
+        wxWindow *child = node->GetData();
+        child->Enable(enable);
+
+        node = node->GetNext();
+    }
+
     return TRUE;
 }
 
     return TRUE;
 }
 
@@ -431,9 +452,9 @@ bool wxWindow::SetFont(const wxFont& font)
     {
         WXHANDLE hFont = m_font.GetResourceHandle();
 
     {
         WXHANDLE hFont = m_font.GetResourceHandle();
 
-        wxASSERT_MSG( hFont, _T("should have valid font") );
+        wxASSERT_MSG( hFont, wxT("should have valid font") );
 
 
-        ::SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
+        ::SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
     }
 
     return TRUE;
     }
 
     return TRUE;
@@ -447,7 +468,7 @@ bool wxWindow::SetCursor(const wxCursor& cursor)
     }
 
     wxASSERT_MSG( m_cursor.Ok(),
     }
 
     wxASSERT_MSG( m_cursor.Ok(),
-                  _T("cursor must be valid after call to the base version"));
+                  wxT("cursor must be valid after call to the base version"));
 
     HWND hWnd = GetHwnd();
 
 
     HWND hWnd = GetHwnd();
 
@@ -784,12 +805,15 @@ void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
 
 void wxWindow::SubclassWin(WXHWND hWnd)
 {
 
 void wxWindow::SubclassWin(WXHWND hWnd)
 {
-    wxASSERT_MSG( !m_oldWndProc, "subclassing window twice?" );
+    wxASSERT_MSG( !m_oldWndProc, wxT("subclassing window twice?") );
+
+    HWND hwnd = (HWND)hWnd;
+    wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in SubclassWin") );
 
 
-    wxAssociateWinWithHandle((HWND)hWnd, this);
+    wxAssociateWinWithHandle(hwnd, this);
 
 
-    m_oldWndProc = (WXFARPROC) GetWindowLong((HWND) hWnd, GWL_WNDPROC);
-    SetWindowLong((HWND) hWnd, GWL_WNDPROC, (LONG) wxWndProc);
+    m_oldWndProc = (WXFARPROC) GetWindowLong(hwnd, GWL_WNDPROC);
+    SetWindowLong(hwnd, GWL_WNDPROC, (LONG) wxWndProc);
 }
 
 void wxWindow::UnsubclassWin()
 }
 
 void wxWindow::UnsubclassWin()
@@ -797,16 +821,19 @@ void wxWindow::UnsubclassWin()
     wxRemoveHandleAssociation(this);
 
     // Restore old Window proc
     wxRemoveHandleAssociation(this);
 
     // Restore old Window proc
-    if ( GetHwnd() )
+    HWND hwnd = GetHwnd();
+    if ( hwnd )
     {
     {
-        FARPROC farProc = (FARPROC) GetWindowLong(GetHwnd(), GWL_WNDPROC);
+        m_hWnd = 0;
+
+        wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in UnsubclassWin") );
+
+        FARPROC farProc = (FARPROC) GetWindowLong(hwnd, GWL_WNDPROC);
         if ( (m_oldWndProc != 0) && (farProc != (FARPROC) m_oldWndProc) )
         {
         if ( (m_oldWndProc != 0) && (farProc != (FARPROC) m_oldWndProc) )
         {
-            SetWindowLong(GetHwnd(), GWL_WNDPROC, (LONG) m_oldWndProc);
+            SetWindowLong(hwnd, GWL_WNDPROC, (LONG) m_oldWndProc);
             m_oldWndProc = 0;
         }
             m_oldWndProc = 0;
         }
-
-        m_hWnd = 0;
     }
 }
 
     }
 }
 
@@ -836,7 +863,8 @@ WXDWORD wxWindow::MakeExtendedStyle(long style, bool eliminateBorders)
 // Determines whether native 3D effects or CTL3D should be used,
 // applying a default border style if required, and returning an extended
 // style to pass to CreateWindowEx.
 // Determines whether native 3D effects or CTL3D should be used,
 // applying a default border style if required, and returning an extended
 // style to pass to CreateWindowEx.
-WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D)
+WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle,
+                                     bool *want3D) const
 {
     // If matches certain criteria, then assume no 3D effects
     // unless specifically requested (dealt with in MakeExtendedStyle)
 {
     // If matches certain criteria, then assume no 3D effects
     // unless specifically requested (dealt with in MakeExtendedStyle)
@@ -891,7 +919,7 @@ WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D)
     return exStyle;
 }
 
     return exStyle;
 }
 
-#if WXWIN_COMPATIBILITY_2
+#if WXWIN_COMPATIBILITY
 // If nothing defined for this, try the parent.
 // E.g. we may be a button loaded from a resource, with no callback function
 // defined.
 // If nothing defined for this, try the parent.
 // E.g. we may be a button loaded from a resource, with no callback function
 // defined.
@@ -1006,7 +1034,7 @@ void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
 // drag and drop
 // ---------------------------------------------------------------------------
 
 // drag and drop
 // ---------------------------------------------------------------------------
 
-#if    wxUSE_DRAG_AND_DROP
+#if wxUSE_DRAG_AND_DROP
 
 void wxWindow::SetDropTarget(wxDropTarget *pDropTarget)
 {
 
 void wxWindow::SetDropTarget(wxDropTarget *pDropTarget)
 {
@@ -1067,28 +1095,33 @@ void wxWindow::DoGetSize(int *x, int *y) const
 void wxWindow::DoGetPosition(int *x, int *y) const
 {
     HWND hWnd = GetHwnd();
 void wxWindow::DoGetPosition(int *x, int *y) const
 {
     HWND hWnd = GetHwnd();
-    HWND hParentWnd = 0;
-    if ( GetParent() )
-        hParentWnd = (HWND) GetParent()->GetHWND();
 
     RECT rect;
     GetWindowRect(hWnd, &rect);
 
 
     RECT rect;
     GetWindowRect(hWnd, &rect);
 
-    // Since we now have the absolute screen coords, if there's a parent we
-    // must subtract its top left corner
     POINT point;
     point.x = rect.left;
     point.y = rect.top;
     POINT point;
     point.x = rect.left;
     point.y = rect.top;
-    if ( hParentWnd )
-    {
-        ::ScreenToClient(hParentWnd, &point);
-    }
 
 
-    // We may be faking the client origin. So a window that's really at (0,
-    // 30) may appear (to wxWin apps) to be at (0, 0).
-    if ( GetParent() )
+    // we do the adjustments with respect to the parent only for the "real"
+    // children, not for the dialogs/frames
+    if ( !IsTopLevel() )
     {
     {
-        wxPoint pt(GetParent()->GetClientAreaOrigin());
+        HWND hParentWnd = 0;
+        wxWindow *parent = GetParent();
+        if ( parent )
+            hParentWnd = GetWinHwnd(parent);
+
+        // Since we now have the absolute screen coords, if there's a parent we
+        // must subtract its top left corner
+        if ( hParentWnd )
+        {
+            ::ScreenToClient(hParentWnd, &point);
+        }
+
+        // We may be faking the client origin. So a window that's really at (0,
+        // 30) may appear (to wxWin apps) to be at (0, 0).
+        wxPoint pt(parent->GetClientAreaOrigin());
         point.x -= pt.x;
         point.y -= pt.y;
     }
         point.x -= pt.x;
         point.y -= pt.y;
     }
@@ -1145,35 +1178,79 @@ void wxWindow::DoGetClientSize(int *x, int *y) const
         *y = rect.bottom;
 }
 
         *y = rect.bottom;
 }
 
+void wxWindow::DoMoveWindow(int x, int y, int width, int height)
+{
+    if ( !::MoveWindow(GetHwnd(), x, y, width, height, TRUE) )
+    {
+        wxLogLastError("MoveWindow");
+    }
+}
+
+// set the size of the window: if the dimensions are positive, just use them,
+// but if any of them is equal to -1, it means that we must find the value for
+// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
+// which case -1 is a valid value for x and y)
+//
+// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
+// the width/height to best suit our contents, otherwise we reuse the current
+// width/height
 void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
 {
 void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
 {
+    // get the current size and position...
     int currentX, currentY;
     GetPosition(&currentX, &currentY);
     int currentW,currentH;
     GetSize(&currentW, &currentH);
 
     int currentX, currentY;
     GetPosition(&currentX, &currentY);
     int currentW,currentH;
     GetSize(&currentW, &currentH);
 
-    if ( x == currentX && y == currentY && width == currentW && height == currentH )
+    // ... and don't do anything (avoiding flicker) if it's already ok
+    if ( x == currentX && y == currentY &&
+         width == currentW && height == currentH )
+    {
         return;
         return;
+    }
 
 
-    int actualWidth = width;
-    int actualHeight = height;
-    int actualX = x;
-    int actualY = y;
-    if ( x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
-        actualX = currentX;
-    if ( y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
-        actualY = currentY;
+    if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
+        x = currentX;
+    if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
+        y = currentY;
 
 
-    AdjustForParentClientOrigin(actualX, actualY, sizeFlags);
+    AdjustForParentClientOrigin(x, y, sizeFlags);
 
 
+    wxSize size(-1, -1);
     if ( width == -1 )
     if ( width == -1 )
-        actualWidth = currentW;
+    {
+        if ( sizeFlags & wxSIZE_AUTO_WIDTH )
+        {
+            size = DoGetBestSize();
+            width = size.x;
+        }
+        else
+        {
+            // just take the current one
+            width = currentW;
+        }
+    }
+
     if ( height == -1 )
     if ( height == -1 )
-        actualHeight = currentH;
+    {
+        if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
+        {
+            if ( size.x == -1 )
+            {
+                size = DoGetBestSize();
+            }
+            //else: already called DoGetBestSize() above
 
 
-    HWND hWnd = GetHwnd();
-    if ( hWnd )
-        MoveWindow(hWnd, actualX, actualY, actualWidth, actualHeight, (BOOL)TRUE);
+            height = size.y;
+        }
+        else
+        {
+            // just take the current one
+            height = currentH;
+        }
+    }
+
+    DoMoveWindow(x, y, width, height);
 }
 
 void wxWindow::DoSetClientSize(int width, int height)
 }
 
 void wxWindow::DoSetClientSize(int width, int height)
@@ -1207,7 +1284,7 @@ void wxWindow::DoSetClientSize(int width, int height)
         ::ScreenToClient(hParentWnd, &point);
     }
 
         ::ScreenToClient(hParentWnd, &point);
     }
 
-    MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)TRUE);
+    DoMoveWindow(point.x, point.y, actual_width, actual_height);
 
     wxSizeEvent event(wxSize(width, height), m_windowId);
     event.SetEventObject(this);
 
     wxSizeEvent event(wxSize(width, height), m_windowId);
     event.SetEventObject(this);
@@ -1225,10 +1302,16 @@ wxPoint wxWindow::GetClientAreaOrigin() const
 // a toolbar that it manages itself).
 void wxWindow::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
 {
 // a toolbar that it manages itself).
 void wxWindow::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
 {
-    if ( ((sizeFlags & wxSIZE_NO_ADJUSTMENTS) == 0) && GetParent() )
+    // don't do it for the dialogs/frames - they float independently of their
+    // parent
+    if ( !IsTopLevel() )
     {
     {
-        wxPoint pt(GetParent()->GetClientAreaOrigin());
-        x += pt.x; y += pt.y;
+        wxWindow *parent = GetParent();
+        if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
+        {
+            wxPoint pt(parent->GetClientAreaOrigin());
+            x += pt.x; y += pt.y;
+        }
     }
 }
 
     }
 }
 
@@ -1260,7 +1343,8 @@ int wxWindow::GetCharWidth() const
     return lpTextMetric.tmAveCharWidth;
 }
 
     return lpTextMetric.tmAveCharWidth;
 }
 
-void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
+void wxWindow::GetTextExtent(const wxString& string,
+                             int *x, int *y,
                              int *descent, int *externalLeading,
                              const wxFont *theFont) const
 {
                              int *descent, int *externalLeading,
                              const wxFont *theFont) const
 {
@@ -1282,7 +1366,7 @@ void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
 
     SIZE sizeRect;
     TEXTMETRIC tm;
 
     SIZE sizeRect;
     TEXTMETRIC tm;
-    GetTextExtentPoint(dc, (const char *)string, (int)string.Length(), &sizeRect);
+    GetTextExtentPoint(dc, string, (int)string.Length(), &sizeRect);
     GetTextMetrics(dc, &tm);
 
     if ( fontToUse && fnt && hfontOld )
     GetTextMetrics(dc, &tm);
 
     if ( fontToUse && fnt && hfontOld )
@@ -1290,56 +1374,81 @@ void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
 
     ReleaseDC(hWnd, dc);
 
 
     ReleaseDC(hWnd, dc);
 
-    if ( x ) *x = sizeRect.cx;
-    if ( y ) *y = sizeRect.cy;
-    if ( descent ) *descent = tm.tmDescent;
-    if ( externalLeading ) *externalLeading = tm.tmExternalLeading;
+    if ( x )
+        *x = sizeRect.cx;
+    if ( y )
+        *y = sizeRect.cy;
+    if ( descent )
+        *descent = tm.tmDescent;
+    if ( externalLeading )
+        *externalLeading = tm.tmExternalLeading;
 }
 
 }
 
+#if wxUSE_CARET && WXWIN_COMPATIBILITY
 // ---------------------------------------------------------------------------
 // Caret manipulation
 // ---------------------------------------------------------------------------
 
 void wxWindow::CreateCaret(int w, int h)
 {
 // ---------------------------------------------------------------------------
 // Caret manipulation
 // ---------------------------------------------------------------------------
 
 void wxWindow::CreateCaret(int w, int h)
 {
-    m_caretWidth = w;
-    m_caretHeight = h;
-    m_caretEnabled = TRUE;
+    SetCaret(new wxCaret(this, w, h));
 }
 
 void wxWindow::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
 {
 }
 
 void wxWindow::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
 {
-    // Not implemented
+    wxFAIL_MSG("not implemented");
 }
 
 void wxWindow::ShowCaret(bool show)
 {
 }
 
 void wxWindow::ShowCaret(bool show)
 {
-    if ( m_caretEnabled )
-    {
-        if ( show )
-            ::ShowCaret(GetHwnd());
-        else
-            ::HideCaret(GetHwnd());
-        m_caretShown = show;
-    }
+    wxCHECK_RET( m_caret, "no caret to show" );
+
+    m_caret->Show(show);
 }
 
 void wxWindow::DestroyCaret()
 {
 }
 
 void wxWindow::DestroyCaret()
 {
-    m_caretEnabled = FALSE;
+    SetCaret(NULL);
 }
 
 void wxWindow::SetCaretPos(int x, int y)
 {
 }
 
 void wxWindow::SetCaretPos(int x, int y)
 {
-    ::SetCaretPos(x, y);
+    wxCHECK_RET( m_caret, "no caret to move" );
+
+    m_caret->Move(x, y);
 }
 
 void wxWindow::GetCaretPos(int *x, int *y) const
 {
 }
 
 void wxWindow::GetCaretPos(int *x, int *y) const
 {
+    wxCHECK_RET( m_caret, "no caret to get position of" );
+
+    m_caret->GetPosition(x, y);
+}
+#endif // wxUSE_CARET
+
+// ---------------------------------------------------------------------------
+// popup menu
+// ---------------------------------------------------------------------------
+
+bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
+{
+    menu->SetInvokingWindow(this);
+    menu->UpdateUI();
+
+    HWND hWnd = GetHwnd();
+    HMENU hMenu = GetHmenuOf(menu);
     POINT point;
     POINT point;
-    ::GetCaretPos(&point);
-    *x = point.x;
-    *y = point.y;
+    point.x = x;
+    point.y = y;
+    ::ClientToScreen(hWnd, &point);
+    wxCurrentPopupMenu = menu;
+    ::TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL);
+    wxYield();
+    wxCurrentPopupMenu = NULL;
+
+    menu->SetInvokingWindow(NULL);
+
+    return TRUE;
 }
 
 // ===========================================================================
 }
 
 // ===========================================================================
@@ -1360,6 +1469,10 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
     {
         // intercept dialog navigation keys
         MSG *msg = (MSG *)pMsg;
     {
         // intercept dialog navigation keys
         MSG *msg = (MSG *)pMsg;
+
+        // here we try to do all the job which ::IsDialogMessage() usually does
+        // internally
+#if 0
         bool bProcess = TRUE;
         if ( msg->message != WM_KEYDOWN )
             bProcess = FALSE;
         bool bProcess = TRUE;
         if ( msg->message != WM_KEYDOWN )
             bProcess = FALSE;
@@ -1370,6 +1483,7 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
         if ( bProcess )
         {
             bool bCtrlDown = (::GetKeyState(VK_CONTROL) & 0x100) != 0;
         if ( bProcess )
         {
             bool bCtrlDown = (::GetKeyState(VK_CONTROL) & 0x100) != 0;
+            bool bShiftDown = (::GetKeyState(VK_SHIFT) & 0x100) != 0;
 
             // WM_GETDLGCODE: ask the control if it wants the key for itself,
             // don't process it if it's the case (except for Ctrl-Tab/Enter
 
             // WM_GETDLGCODE: ask the control if it wants the key for itself,
             // don't process it if it's the case (except for Ctrl-Tab/Enter
@@ -1386,13 +1500,16 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
             switch ( msg->wParam )
             {
                 case VK_TAB:
             switch ( msg->wParam )
             {
                 case VK_TAB:
-                    if ( lDlgCode & DLGC_WANTTAB ) {
+                    // assume that nobody wants Shift-TAB for himself - if we
+                    // don't do it there is no easy way for a control to grab
+                    // TABs but still let Shift-TAB work as navugation key
+                    if ( (lDlgCode & DLGC_WANTTAB) && !bShiftDown ) {
                         bProcess = FALSE;
                     }
                     else {
                         // Ctrl-Tab cycles thru notebook pages
                         bWindowChange = bCtrlDown;
                         bProcess = FALSE;
                     }
                     else {
                         // Ctrl-Tab cycles thru notebook pages
                         bWindowChange = bCtrlDown;
-                        bForward = !(::GetKeyState(VK_SHIFT) & 0x100);
+                        bForward = !bShiftDown;
                     }
                     break;
 
                     }
                     break;
 
@@ -1412,27 +1529,40 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
 
                 case VK_RETURN:
                     {
 
                 case VK_RETURN:
                     {
-                        if ( lDlgCode & DLGC_WANTMESSAGE )
+                        if ( (lDlgCode & DLGC_WANTMESSAGE) && !bCtrlDown )
                         {
                             // control wants to process Enter itself, don't
                             // call IsDialogMessage() which would interpret
                             // it
                             return FALSE;
                         }
                         {
                             // control wants to process Enter itself, don't
                             // call IsDialogMessage() which would interpret
                             // it
                             return FALSE;
                         }
-#ifndef __WIN16__
-                        wxButton *btnDefault = GetDefaultItem();
-                        if ( btnDefault && !bCtrlDown )
+                        else if ( lDlgCode & DLGC_BUTTON )
                         {
                         {
-                            // if there is a default button, Enter should
-                            // press it
-                            (void)::SendMessage((HWND)btnDefault->GetHWND(),
-                                                BM_CLICK, 0, 0);
-                            return TRUE;
+                            // buttons want process Enter themselevs
+                            bProcess = FALSE;
+                        }
+                        else
+                        {
+                            wxPanel *panel = wxDynamicCast(this, wxPanel);
+                            wxButton *btn = NULL;
+                            if ( panel )
+                            {
+                                // panel may have a default button which should
+                                // be activated by Enter
+                                btn = panel->GetDefaultItem();
+                            }
+
+                            if ( btn && btn->IsEnabled() )
+                            {
+                                // if we do have a default button, do press it
+                                btn->MSWCommand(BN_CLICKED, 0 /* unused */);
+
+                                return TRUE;
+                            }
+                            // else: but if it does not it makes sense to make
+                            //       it work like a TAB - and that's what we do.
+                            //       Note that Ctrl-Enter always works this way.
                         }
                         }
-                        // else: but if there is not it makes sense to make it
-                        //       work like a TAB - and that's what we do.
-                        //       Note that Ctrl-Enter always works this way.
-#endif
                     }
                     break;
 
                     }
                     break;
 
@@ -1448,9 +1578,48 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
                 event.SetEventObject(this);
 
                 if ( GetEventHandler()->ProcessEvent(event) )
                 event.SetEventObject(this);
 
                 if ( GetEventHandler()->ProcessEvent(event) )
+                {
+                    wxButton *btn = wxDynamicCast(FindFocus(), wxButton);
+                    if ( btn )
+                    {
+                        // the button which has focus should be default
+                        btn->SetDefault();
+                    }
+
                     return TRUE;
                     return TRUE;
+                }
+            }
+        }
+#else
+        // let ::IsDialogMessage() do almost everything and handle just the
+        // things it doesn't here: Ctrl-TAB for switching notebook pages
+        if ( msg->message == WM_KEYDOWN )
+        {
+            // don't process system keys here
+            if ( !(HIWORD(msg->lParam) & KF_ALTDOWN) )
+            {
+                if ( (msg->wParam == VK_TAB) &&
+                     (::GetKeyState(VK_CONTROL) & 0x100) != 0 )
+                {
+                    // find the first notebook parent and change its page
+                    wxWindow *win = this;
+                    wxNotebook *nbook = NULL;
+                    while ( win && !nbook )
+                    {
+                        nbook = wxDynamicCast(win, wxNotebook);
+                        win = win->GetParent();
+                    }
+
+                    if ( nbook )
+                    {
+                        bool forward = !(::GetKeyState(VK_SHIFT) & 0x100);
+
+                        nbook->AdvanceSelection(forward);
+                    }
+                }
             }
         }
             }
         }
+#endif // 0
 
         if ( ::IsDialogMessage(GetHwnd(), msg) )
             return TRUE;
 
         if ( ::IsDialogMessage(GetHwnd(), msg) )
             return TRUE;
@@ -1471,10 +1640,7 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
 
 bool wxWindow::MSWTranslateMessage(WXMSG* pMsg)
 {
 
 bool wxWindow::MSWTranslateMessage(WXMSG* pMsg)
 {
-    return m_acceleratorTable.Ok() &&
-           ::TranslateAccelerator(GetHwnd(),
-                                  GetTableHaccel(m_acceleratorTable),
-                                  (MSG *)pMsg);
+    return m_acceleratorTable.Translate(this, pMsg);
 }
 
 // ---------------------------------------------------------------------------
 }
 
 // ---------------------------------------------------------------------------
@@ -1552,7 +1718,7 @@ void wxWindow::UnpackScroll(WXWPARAM wParam, WXLPARAM lParam,
 void wxWindow::UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
                               WXWORD *nCtlColor, WXHDC *hdc, WXHWND *hwnd)
 {
 void wxWindow::UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
                               WXWORD *nCtlColor, WXHDC *hdc, WXHWND *hwnd)
 {
-    *control = (WXHWND)LOWORD(lParam);
+    *hwnd = (WXHWND)LOWORD(lParam);
     *nCtlColor = (int)HIWORD(lParam);
     *hdc = (WXHDC)wParam;
 }
     *nCtlColor = (int)HIWORD(lParam);
     *hdc = (WXHDC)wParam;
 }
@@ -1576,11 +1742,11 @@ void wxWindow::UnpackMenuSelect(WXWPARAM wParam, WXLPARAM lParam,
 wxWindow *wxWndHook = NULL;
 
 // Main window proc
 wxWindow *wxWndHook = NULL;
 
 // Main window proc
-LRESULT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     // trace all messages - useful for the debugging
 #ifdef __WXDEBUG__
 {
     // trace all messages - useful for the debugging
 #ifdef __WXDEBUG__
-    wxLogTrace(wxTraceMessages, "Processing %s(wParam=%8lx, lParam=%8lx)",
+    wxLogTrace(wxTraceMessages, wxT("Processing %s(wParam=%8lx, lParam=%8lx)"),
                wxGetMessageName(message), wParam, lParam);
 #endif // __WXDEBUG__
 
                wxGetMessageName(message), wParam, lParam);
 #endif // __WXDEBUG__
 
@@ -1590,6 +1756,12 @@ LRESULT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARA
     // it with wxWindow stored in wxWndHook
     if ( !wnd && wxWndHook )
     {
     // it with wxWindow stored in wxWndHook
     if ( !wnd && wxWndHook )
     {
+#if 0 // def __WXDEBUG__
+        char buf[512];
+        ::GetClassNameA((HWND) hWnd, buf, 512);
+        wxString className(buf);
+#endif
+
         wxAssociateWinWithHandle(hWnd, wxWndHook);
         wnd = wxWndHook;
         wxWndHook = NULL;
         wxAssociateWinWithHandle(hWnd, wxWndHook);
         wnd = wxWndHook;
         wxWndHook = NULL;
@@ -1694,7 +1866,20 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
             break;
 
         case WM_MOUSEMOVE:
             break;
 
         case WM_MOUSEMOVE:
+           {
+                short x = LOWORD(lParam);
+                short y = HIWORD(lParam);
+
+                processed = HandleMouseMove(x, y, wParam);
+           }
+           break;
+
         case WM_LBUTTONDOWN:
         case WM_LBUTTONDOWN:
+           // set focus to this window
+           SetFocus();
+
+           // fall through
+
         case WM_LBUTTONUP:
         case WM_LBUTTONDBLCLK:
         case WM_RBUTTONDOWN:
         case WM_LBUTTONUP:
         case WM_LBUTTONDBLCLK:
         case WM_RBUTTONDOWN:
@@ -1704,8 +1889,8 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
         case WM_MBUTTONUP:
         case WM_MBUTTONDBLCLK:
             {
         case WM_MBUTTONUP:
         case WM_MBUTTONDBLCLK:
             {
-                int x = LOWORD(lParam);
-                int y = HIWORD(lParam);
+                short x = LOWORD(lParam);
+                short y = HIWORD(lParam);
 
                 processed = HandleMouseEvent(message, x, y, wParam);
             }
 
                 processed = HandleMouseEvent(message, x, y, wParam);
             }
@@ -1768,6 +1953,15 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
             }
             break;
 
             }
             break;
 
+        case WM_GETDLGCODE:
+            if ( m_lDlgCode )
+            {
+                rc.result = m_lDlgCode;
+                processed = TRUE;
+            }
+            //else: get the dlg code from the DefWindowProc()
+            break;
+
         case WM_KEYDOWN:
             // If this has been processed by an event handler,
             // return 0 now (we've handled it).
         case WM_KEYDOWN:
             // If this has been processed by an event handler,
             // return 0 now (we've handled it).
@@ -1795,7 +1989,12 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
                 case VK_RETURN:
                 case VK_BACK:
                 case VK_TAB:
                 case VK_RETURN:
                 case VK_BACK:
                 case VK_TAB:
-                    processed = TRUE;
+                case VK_ADD:
+                case VK_SUBTRACT:
+                    // but set processed to FALSE, not TRUE to still pass them to
+                    // the control's default window proc - otherwise built-in
+                    // keyboard handling won't work
+                    processed = FALSE;
 
                     break;
 
 
                     break;
 
@@ -1926,7 +2125,7 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
             break;
 
         case WM_GETMINMAXINFO:
             break;
 
         case WM_GETMINMAXINFO:
-            processed = HandleGetMinMaxInfo((LPMINMAXINFO)lParam);
+            processed = HandleGetMinMaxInfo((MINMAXINFO*)lParam);
             break;
 
         case WM_SETCURSOR:
             break;
 
         case WM_SETCURSOR:
@@ -1947,7 +2146,7 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
     if ( !processed )
     {
 #ifdef __WXDEBUG__
     if ( !processed )
     {
 #ifdef __WXDEBUG__
-        wxLogTrace(wxTraceMessages, "Forwarding %s to DefWindowProc.",
+        wxLogTrace(wxTraceMessages, wxT("Forwarding %s to DefWindowProc."),
                    wxGetMessageName(message));
 #endif // __WXDEBUG__
         rc.result = MSWDefWindowProc(message, wParam, lParam);
                    wxGetMessageName(message));
 #endif // __WXDEBUG__
         rc.result = MSWDefWindowProc(message, wParam, lParam);
@@ -1983,19 +2182,44 @@ wxWindow *wxFindWinFromHandle(WXHWND hWnd)
     return (wxWindow *)node->Data();
 }
 
     return (wxWindow *)node->Data();
 }
 
+#if 0 // def __WXDEBUG__
+static int gs_AssociationCount = 0;
+#endif
+
 void wxAssociateWinWithHandle(HWND hWnd, wxWindow *win)
 {
     // adding NULL hWnd is (first) surely a result of an error and
     // (secondly) breaks menu command processing
     wxCHECK_RET( hWnd != (HWND)NULL,
 void wxAssociateWinWithHandle(HWND hWnd, wxWindow *win)
 {
     // adding NULL hWnd is (first) surely a result of an error and
     // (secondly) breaks menu command processing
     wxCHECK_RET( hWnd != (HWND)NULL,
-                 "attempt to add a NULL hWnd to window list ignored" );
+                 wxT("attempt to add a NULL hWnd to window list ignored") );
+
+
+    wxWindow *oldWin = wxFindWinFromHandle((WXHWND) hWnd);
+    if ( oldWin && (oldWin != win) )
+    {
+        wxString str(win->GetClassInfo()->GetClassName());
+        wxLogError(wxT("Bug! Found existing HWND %X for new window of class %s"), (int) hWnd, (const wxChar*) str);
+    }
+    else if (!oldWin)
+    {
+#if 0 // def __WXDEBUG__
+        gs_AssociationCount ++;
+        wxLogDebug("+ Association %d", gs_AssociationCount);
+#endif
 
 
-    if ( !wxWinHandleList->Find((long)hWnd) )
         wxWinHandleList->Append((long)hWnd, win);
         wxWinHandleList->Append((long)hWnd, win);
+    }
 }
 
 void wxRemoveHandleAssociation(wxWindow *win)
 {
 }
 
 void wxRemoveHandleAssociation(wxWindow *win)
 {
+#if 0 // def __WXDEBUG__
+    if (wxWinHandleList->Member(win))
+    {
+        wxLogDebug("- Association %d", gs_AssociationCount);
+        gs_AssociationCount --;
+    }
+#endif
     wxWinHandleList->DeleteObject(win);
 }
 
     wxWinHandleList->DeleteObject(win);
 }
 
@@ -2015,16 +2239,16 @@ void wxWindow::MSWDetachWindowMenu()
         int i;
         for (i = 0; i < N; i++)
         {
         int i;
         for (i = 0; i < N; i++)
         {
-            char buf[100];
+            wxChar buf[100];
             int chars = GetMenuString(hMenu, i, buf, 100, MF_BYPOSITION);
             if ( !chars )
             {
             int chars = GetMenuString(hMenu, i, buf, 100, MF_BYPOSITION);
             if ( !chars )
             {
-                wxLogLastError("GetMenuString");
+                wxLogLastError(wxT("GetMenuString"));
 
                 continue;
             }
 
 
                 continue;
             }
 
-            if ( strcmp(buf, "&Window") == 0 )
+            if ( wxStrcmp(buf, wxT("&Window")) == 0 )
             {
                 RemoveMenu(hMenu, i, MF_BYPOSITION);
 
             {
                 RemoveMenu(hMenu, i, MF_BYPOSITION);
 
@@ -2036,15 +2260,15 @@ void wxWindow::MSWDetachWindowMenu()
 
 bool wxWindow::MSWCreate(int id,
                          wxWindow *parent,
 
 bool wxWindow::MSWCreate(int id,
                          wxWindow *parent,
-                         const char *wclass,
+                         const wxChar *wclass,
                          wxWindow *wx_win,
                          wxWindow *wx_win,
-                         const char *title,
+                         const wxChar *title,
                          int x,
                          int y,
                          int width,
                          int height,
                          WXDWORD style,
                          int x,
                          int y,
                          int width,
                          int height,
                          WXDWORD style,
-                         const char *dialog_template,
+                         const wxChar *dialog_template,
                          WXDWORD extendedStyle)
 {
     int x1 = CW_USEDEFAULT;
                          WXDWORD extendedStyle)
 {
     int x1 = CW_USEDEFAULT;
@@ -2068,7 +2292,7 @@ bool wxWindow::MSWCreate(int id,
     if ( width > -1 ) width1 = width;
     if ( height > -1 ) height1 = height;
 
     if ( width > -1 ) width1 = width;
     if ( height > -1 ) height1 = height;
 
-    HWND hParent = NULL;
+    HWND hParent = (HWND)NULL;
     if ( parent )
         hParent = (HWND) parent->GetHWND();
 
     if ( parent )
         hParent = (HWND) parent->GetHWND();
 
@@ -2089,7 +2313,22 @@ bool wxWindow::MSWCreate(int id,
             return FALSE;
         }
 
             return FALSE;
         }
 
-        ::MoveWindow(GetHwnd(), x1, y1, width1, height1, FALSE);
+        // ::SetWindowLong(GWL_EXSTYLE) doesn't work for the dialogs, so try
+        // to take care of (at least some) extended style flags ourselves
+        if ( extendedStyle & WS_EX_TOPMOST )
+        {
+            if ( !::SetWindowPos(GetHwnd(), HWND_TOPMOST, 0, 0, 0, 0,
+                                 SWP_NOSIZE | SWP_NOMOVE) )
+            {
+                wxLogLastError(wxT("SetWindowPos"));
+            }
+        }
+
+        // move the dialog to its initial position without forcing repainting
+        if ( !::MoveWindow(GetHwnd(), x1, y1, width1, height1, FALSE) )
+        {
+            wxLogLastError(wxT("MoveWindow"));
+        }
     }
     else
     {
     }
     else
     {
@@ -2097,9 +2336,15 @@ bool wxWindow::MSWCreate(int id,
         if ( style & WS_CHILD )
             controlId = id;
 
         if ( style & WS_CHILD )
             controlId = id;
 
+        wxString className(wclass);
+        if ( GetWindowStyleFlag() & wxNO_FULL_REPAINT_ON_RESIZE )
+        {
+            className += wxT("NR");
+        }
+
         m_hWnd = (WXHWND)CreateWindowEx(extendedStyle,
         m_hWnd = (WXHWND)CreateWindowEx(extendedStyle,
-                                        wclass,
-                                        title ? title : "",
+                                        className,
+                                        title ? title : wxT(""),
                                         style,
                                         x1, y1,
                                         width1, height1,
                                         style,
                                         x1, y1,
                                         width1, height1,
@@ -2118,7 +2363,18 @@ bool wxWindow::MSWCreate(int id,
     }
 
     wxWndHook = NULL;
     }
 
     wxWndHook = NULL;
-    wxWinHandleList->Append((long)m_hWnd, this);
+#ifdef __WXDEBUG__
+    wxNode* node = wxWinHandleList->Member(this);
+    if (node)
+    {
+        HWND hWnd = (HWND) node->GetKeyInteger();
+        if (hWnd != (HWND) m_hWnd)
+        {
+            wxLogError(wxT("A second HWND association is being added for the same window!"));
+        }
+    }
+#endif
+    wxAssociateWinWithHandle((HWND) m_hWnd, this);
 
     return TRUE;
 }
 
     return TRUE;
 }
@@ -2170,10 +2426,10 @@ bool wxWindow::MSWOnNotify(int WXUNUSED(idCtrl),
 {
 #if wxUSE_TOOLTIPS
     NMHDR* hdr = (NMHDR *)lParam;
 {
 #if wxUSE_TOOLTIPS
     NMHDR* hdr = (NMHDR *)lParam;
-    if ( hdr->code == TTN_NEEDTEXT && m_tooltip )
+    if ( (int)hdr->code == TTN_NEEDTEXT && m_tooltip )
     {
         TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
     {
         TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
-        ttt->lpszText = (char *)m_tooltip->GetTip().c_str();
+        ttt->lpszText = (wxChar *)m_tooltip->GetTip().c_str();
 
         // processed
         return TRUE;
 
         // processed
         return TRUE;
@@ -2193,7 +2449,7 @@ bool wxWindow::HandleQueryEndSession(long logOff, bool *mayEnd)
     wxCloseEvent event(wxEVT_QUERY_END_SESSION, -1);
     event.SetEventObject(wxTheApp);
     event.SetCanVeto(TRUE);
     wxCloseEvent event(wxEVT_QUERY_END_SESSION, -1);
     event.SetEventObject(wxTheApp);
     event.SetCanVeto(TRUE);
-    event.SetLoggingOff(logOff == ENDSESSION_LOGOFF);
+    event.SetLoggingOff(logOff == (long)ENDSESSION_LOGOFF);
 
     bool rc = wxTheApp->ProcessEvent(event);
 
 
     bool rc = wxTheApp->ProcessEvent(event);
 
@@ -2216,7 +2472,7 @@ bool wxWindow::HandleEndSession(bool endSession, long logOff)
     wxCloseEvent event(wxEVT_END_SESSION, -1);
     event.SetEventObject(wxTheApp);
     event.SetCanVeto(FALSE);
     wxCloseEvent event(wxEVT_END_SESSION, -1);
     event.SetEventObject(wxTheApp);
     event.SetCanVeto(FALSE);
-    event.SetLoggingOff( (logOff == ENDSESSION_LOGOFF) );
+    event.SetLoggingOff( (logOff == (long)ENDSESSION_LOGOFF) );
     if ( (this == wxTheApp->GetTopWindow()) && // Only send once
         wxTheApp->ProcessEvent(event))
     {
     if ( (this == wxTheApp->GetTopWindow()) && // Only send once
         wxTheApp->ProcessEvent(event))
     {
@@ -2277,26 +2533,19 @@ bool wxWindow::HandleActivate(int state,
 
 bool wxWindow::HandleSetFocus(WXHWND WXUNUSED(hwnd))
 {
 
 bool wxWindow::HandleSetFocus(WXHWND WXUNUSED(hwnd))
 {
+#if wxUSE_CARET
     // Deal with caret
     // Deal with caret
-    if ( m_caretEnabled && (m_caretWidth > 0) && (m_caretHeight > 0) )
+    if ( m_caret )
     {
     {
-        if ( ::CreateCaret(GetHwnd(), NULL, m_caretWidth, m_caretHeight) )
-        {
-            if ( m_caretShown )
-            {
-                if ( !::ShowCaret(GetHwnd()) )
-                    wxLogLastError("ShowCaret");
-            }
-        }
-        else
-            wxLogLastError("CreateCaret");
+        m_caret->OnSetFocus();
     }
     }
+#endif // wxUSE_CARET
 
     // panel wants to track the window which was the last to have focus in it
 
     // panel wants to track the window which was the last to have focus in it
-    wxWindow *parent = GetParent();
-    if ( parent && parent->IsKindOf(CLASSINFO(wxPanel)) )
+    wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
+    if ( panel )
     {
     {
-        ((wxPanel *)parent)->SetLastFocus(GetId());
+        panel->SetLastFocus(this);
     }
 
     wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
     }
 
     wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
@@ -2307,12 +2556,13 @@ bool wxWindow::HandleSetFocus(WXHWND WXUNUSED(hwnd))
 
 bool wxWindow::HandleKillFocus(WXHWND WXUNUSED(hwnd))
 {
 
 bool wxWindow::HandleKillFocus(WXHWND WXUNUSED(hwnd))
 {
+#if wxUSE_CARET
     // Deal with caret
     // Deal with caret
-    if ( m_caretEnabled )
+    if ( m_caret )
     {
     {
-        if ( !::DestroyCaret() )
-            wxLogLastError("DestroyCaret");
+        m_caret->OnKillFocus();
     }
     }
+#endif // wxUSE_CARET
 
     wxFocusEvent event(wxEVT_KILL_FOCUS, m_windowId);
     event.SetEventObject(this);
 
     wxFocusEvent event(wxEVT_KILL_FOCUS, m_windowId);
     event.SetEventObject(this);
@@ -2356,7 +2606,7 @@ bool wxWindow::HandleDropFiles(WXWPARAM wParam)
     int wIndex;
     for (wIndex=0; wIndex < (int)gwFilesDropped; wIndex++)
     {
     int wIndex;
     for (wIndex=0; wIndex < (int)gwFilesDropped; wIndex++)
     {
-        DragQueryFile (hFilesInfo, wIndex, (LPSTR) wxBuffer, 1000);
+        DragQueryFile (hFilesInfo, wIndex, (LPTSTR) wxBuffer, 1000);
         files[wIndex] = wxBuffer;
     }
     DragFinish (hFilesInfo);
         files[wIndex] = wxBuffer;
     }
     DragFinish (hFilesInfo);
@@ -2426,13 +2676,13 @@ bool wxWindow::HandleSetCursor(WXHWND hWnd,
     return FALSE;
 }
 
     return FALSE;
 }
 
-#if wxUSE_OWNER_DRAWN
 // ---------------------------------------------------------------------------
 // owner drawn stuff
 // ---------------------------------------------------------------------------
 
 bool wxWindow::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct)
 {
 // ---------------------------------------------------------------------------
 // owner drawn stuff
 // ---------------------------------------------------------------------------
 
 bool wxWindow::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct)
 {
+#if wxUSE_OWNER_DRAWN
     // is it a menu item?
     if ( id == 0 )
     {
     // is it a menu item?
     if ( id == 0 )
     {
@@ -2461,12 +2711,14 @@ bool wxWindow::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct)
     {
         return ((wxControl *)item)->MSWOnDraw(itemStruct);
     }
     {
         return ((wxControl *)item)->MSWOnDraw(itemStruct);
     }
-    else
-        return FALSE;
+#endif // USE_OWNER_DRAWN
+
+    return FALSE;
 }
 
 bool wxWindow::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
 {
 }
 
 bool wxWindow::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
 {
+#if wxUSE_OWNER_DRAWN
     // is it a menu item?
     if ( id == 0 )
     {
     // is it a menu item?
     if ( id == 0 )
     {
@@ -2484,10 +2736,9 @@ bool wxWindow::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
     {
         return ((wxControl *)item)->MSWOnMeasure(itemStruct);
     }
     {
         return ((wxControl *)item)->MSWOnMeasure(itemStruct);
     }
-
+#endif  // owner-drawn menus
     return FALSE;
 }
     return FALSE;
 }
-#endif  // owner-drawn menus
 
 // ---------------------------------------------------------------------------
 // colours and palettes
 
 // ---------------------------------------------------------------------------
 // colours and palettes
@@ -2731,14 +2982,32 @@ bool wxWindow::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
         return popupMenu->MSWCommand(cmd, id);
     }
 
         return popupMenu->MSWCommand(cmd, id);
     }
 
-    wxWindow *win = FindItem(id);
-    if ( !win )
+    wxWindow *win;
+    if ( cmd == 0 || cmd == 1 ) // menu or accel - use id
+    {
+        // must cast to a signed type before comparing with other ids!
+        win = FindItem((signed short)id);
+    }
+    else
     {
     {
+        // find it from HWND - this works even with the broken programs using
+        // the same ids for different controls
         win = wxFindWinFromHandle(control);
     }
 
     if ( win )
         return win->MSWCommand(cmd, id);
         win = wxFindWinFromHandle(control);
     }
 
     if ( win )
         return win->MSWCommand(cmd, id);
+    else
+    {
+        // If no child window, it may be an accelerator, e.g. for
+        // a popup menu command.
+
+        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
+        event.SetEventObject(this);
+        event.SetId(id);
+        event.SetInt(id);
+        return ProcessEvent(event);
+    }
 
     return FALSE;
 }
 
     return FALSE;
 }
@@ -3067,7 +3336,7 @@ bool wxWindow::HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags)
             break;
 
         default:
             break;
 
         default:
-            wxFAIL_MSG("no such joystick event");
+            wxFAIL_MSG(wxT("no such joystick event"));
 
             return FALSE;
     }
 
             return FALSE;
     }
@@ -3124,8 +3393,11 @@ bool wxWindow::MSWOnScroll(int orientation, WXWORD wParam,
         event.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
         break;
 
         event.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
         break;
 
-    case SB_THUMBTRACK:
     case SB_THUMBPOSITION:
     case SB_THUMBPOSITION:
+        event.m_isScrolling = FALSE;
+        /* fall-through */
+
+    case SB_THUMBTRACK:
         event.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
         break;
 
         event.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
         break;
 
@@ -3140,7 +3412,7 @@ bool wxWindow::MSWOnScroll(int orientation, WXWORD wParam,
 // global functions
 // ===========================================================================
 
 // global functions
 // ===========================================================================
 
-void wxGetCharSize(WXHWND wnd, int *x, int *y,wxFont *the_font)
+void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont *the_font)
 {
     TEXTMETRIC tm;
     HDC dc = ::GetDC((HWND) wnd);
 {
     TEXTMETRIC tm;
     HDC dc = ::GetDC((HWND) wnd);
@@ -3150,7 +3422,7 @@ void wxGetCharSize(WXHWND wnd, int *x, int *y,wxFont *the_font)
     {
         //    the_font->UseResource();
         //    the_font->RealizeResource();
     {
         //    the_font->UseResource();
         //    the_font->RealizeResource();
-        fnt = (HFONT)the_font->GetResourceHandle();
+        fnt = (HFONT)((wxFont *)the_font)->GetResourceHandle(); // const_cast
         if ( fnt )
             was = (HFONT) SelectObject(dc,fnt);
     }
         if ( fnt )
             was = (HFONT) SelectObject(dc,fnt);
     }
@@ -3160,8 +3432,11 @@ void wxGetCharSize(WXHWND wnd, int *x, int *y,wxFont *the_font)
         SelectObject(dc,was);
     }
     ReleaseDC((HWND)wnd, dc);
         SelectObject(dc,was);
     }
     ReleaseDC((HWND)wnd, dc);
-    *x = tm.tmAveCharWidth;
-    *y = tm.tmHeight + tm.tmExternalLeading;
+
+    if ( x )
+        *x = tm.tmAveCharWidth;
+    if ( y )
+        *y = tm.tmHeight + tm.tmExternalLeading;
 
     //  if ( the_font )
     //    the_font->ReleaseResource();
 
     //  if ( the_font )
     //    the_font->ReleaseResource();
@@ -3348,17 +3623,22 @@ void wxSetKeyboardHook(bool doIt)
     {
         wxTheKeyboardHookProc = MakeProcInstance((FARPROC) wxKeyboardHook, wxGetInstance());
         wxTheKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) wxTheKeyboardHookProc, wxGetInstance(),
     {
         wxTheKeyboardHookProc = MakeProcInstance((FARPROC) wxKeyboardHook, wxGetInstance());
         wxTheKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) wxTheKeyboardHookProc, wxGetInstance(),
+
 #if defined(__WIN32__) && !defined(__TWIN32__)
             GetCurrentThreadId());
         //      (DWORD)GetCurrentProcess()); // This is another possibility. Which is right?
 #else
 #if defined(__WIN32__) && !defined(__TWIN32__)
             GetCurrentThreadId());
         //      (DWORD)GetCurrentProcess()); // This is another possibility. Which is right?
 #else
-        GetCurrentTask());
+            GetCurrentTask());
 #endif
     }
     else
     {
         UnhookWindowsHookEx(wxTheKeyboardHook);
 #endif
     }
     else
     {
         UnhookWindowsHookEx(wxTheKeyboardHook);
+        // avoids mingw warning about statement with no effect (FreeProcInstance
+        // doesn't do anything under Win32)
+#ifndef __GNUWIN32__
         FreeProcInstance(wxTheKeyboardHookProc);
         FreeProcInstance(wxTheKeyboardHookProc);
+#endif
     }
 }
 
     }
 }