]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/window.cpp
wxWindow::OnClose() removed completely from wxGTK, wxCloseEvent is now
[wxWidgets.git] / src / msw / window.cpp
index 45ca481c75270823df0044605c3aeee3bfad49ca..ddda342c66c1bdb9c30137b1dc5465fdbac857fc 100644 (file)
 #endif
 
 #ifndef WX_PRECOMP
-#include <stdio.h>
-#include "wx/setup.h"
-#include "wx/menu.h"
-#include "wx/dc.h"
-#include "wx/dcclient.h"
-#include "wx/utils.h"
-#include "wx/app.h"
-#include "wx/panel.h"
-#include "wx/layout.h"
-#include "wx/dialog.h"
-#include "wx/frame.h"
-#include "wx/listbox.h"
-#include "wx/button.h"
-#include "wx/settings.h"
-#include "wx/msgdlg.h"
+    #include "wx/setup.h"
+    #include "wx/menu.h"
+    #include "wx/dc.h"
+    #include "wx/dcclient.h"
+    #include "wx/utils.h"
+    #include "wx/app.h"
+    #include "wx/panel.h"
+    #include "wx/layout.h"
+    #include "wx/dialog.h"
+    #include "wx/frame.h"
+    #include "wx/listbox.h"
+    #include "wx/button.h"
+    #include "wx/settings.h"
+    #include "wx/msgdlg.h"
+
+    #include <stdio.h>
 #endif
 
 #if     wxUSE_OWNER_DRAWN
 
 #include "wx/menuitem.h"
 #include "wx/log.h"
+#include "wx/tooltip.h"
+
 #include "wx/msw/private.h"
 
 #include <string.h>
 
 #ifndef __GNUWIN32__
-#include <shellapi.h>
-#include <mmsystem.h>
+    #include <shellapi.h>
+    #include <mmsystem.h>
 #endif
 
 #ifdef __WIN32__
-#include <windowsx.h>
+    #include <windowsx.h>
+#endif
+
+#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
+#include <commctrl.h>
 #endif
 
-#ifdef __GNUWIN32__
-#include <wx/msw/gnuwin32/extra.h>
+#ifndef __TWIN32__
+    #ifdef __GNUWIN32__
+        #include <wx/msw/gnuwin32/extra.h>
+    #endif
 #endif
 
+// all these are defined in <windows.h>
 #ifdef GetCharWidth
 #undef GetCharWidth
 #endif
@@ -95,13 +105,11 @@ void wxAssociateWinWithHandle(HWND hWnd, wxWindow *win);
 wxWindow *wxFindWinFromHandle(WXHWND hWnd);
 
 #if !USE_SHARED_LIBRARY
-IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxEvtHandler)
+    IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxEvtHandler)
 #endif
 
 BEGIN_EVENT_TABLE(wxWindow, wxEvtHandler)
     EVT_CHAR(wxWindow::OnChar)
-    EVT_KEY_DOWN(wxWindow::OnKeyDown)
-    EVT_KEY_UP(wxWindow::OnKeyUp)
     EVT_ERASE_BACKGROUND(wxWindow::OnEraseBackground)
     EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged)
     EVT_INIT_DIALOG(wxWindow::OnInitDialog)
@@ -177,9 +185,21 @@ bool wxWindow::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
 }
 
 bool wxWindow::MSWNotify(WXWPARAM WXUNUSED(wParam),
-                         WXLPARAM WXUNUSED(lParam),
+                         WXLPARAM lParam,
                          WXLPARAM* WXUNUSED(result))
 {
+#ifdef __WIN95__
+    NMHDR* hdr = (NMHDR *)lParam;
+    if ( hdr->code == TTN_NEEDTEXT && m_tooltip )
+    {
+        TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
+        ttt->lpszText = (char *)m_tooltip->GetTip().c_str();
+
+        // processed
+        return TRUE;
+    }
+#endif
+
     return FALSE;
 }
 
@@ -266,6 +286,8 @@ void wxWindow::Init()
 #if  wxUSE_DRAG_AND_DROP
     m_pDropTarget = NULL;
 #endif
+
+    m_tooltip = NULL;
 }
 
 wxWindow::wxWindow()
@@ -278,6 +300,10 @@ wxWindow::~wxWindow()
 {
     m_isBeingDeleted = TRUE;
 
+    // first of all, delete the things on which nothing else depends
+
+    wxDELETE(m_tooltip);
+
     // JACS - if behaviour is odd, restore this
     // to the start of ~wxWindow. Vadim has changed
     // it to nearer the end. Unsure of side-effects
@@ -290,6 +316,7 @@ wxWindow::~wxWindow()
     // delete themselves.
 #if wxUSE_CONSTRAINTS
     DeleteRelatedConstraints();
+
     if (m_constraints)
     {
         // This removes any dangling pointers to this window
@@ -298,11 +325,9 @@ wxWindow::~wxWindow()
         delete m_constraints;
         m_constraints = NULL;
     }
-    if (m_windowSizer)
-    {
-        delete m_windowSizer;
-        m_windowSizer = NULL;
-    }
+
+    wxDELETE(m_windowSizer);
+
     // If this is a child of a sizer, remove self from parent
     if (m_sizerParent)
         m_sizerParent->RemoveChild((wxWindow *)this);
@@ -499,7 +524,8 @@ void wxWindow::SetDropTarget(wxDropTarget *pDropTarget)
         m_pDropTarget->Register(m_hWnd);
 }
 
-#endif
+#endif // wxUSE_DRAG_AND_DROP
+
 
 //old style file-manager drag&drop support
 // I think we should retain the old-style
@@ -512,6 +538,24 @@ void wxWindow::DragAcceptFiles(bool accept)
         ::DragAcceptFiles(hWnd, (BOOL)accept);
 }
 
+// ----------------------------------------------------------------------------
+// tooltips
+// ----------------------------------------------------------------------------
+
+void wxWindow::SetToolTip(const wxString &tip)
+{
+    SetToolTip(new wxToolTip(tip));
+}
+
+void wxWindow::SetToolTip(wxToolTip *tooltip)
+{
+    if ( m_tooltip )
+        delete m_tooltip;
+
+    m_tooltip = tooltip;
+    m_tooltip->SetWindow(this);
+}
+
 // Get total size
 void wxWindow::GetSize(int *x, int *y) const
 {
@@ -844,10 +888,8 @@ LRESULT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARA
         wnd->m_hWnd = (WXHWND) hWnd;
     }
 
-    // Stop right here if we don't have a valid handle
-    // in our wxWnd object.
+    // Stop right here if we don't have a valid handle in our wxWindow object.
     if (wnd && !wnd->m_hWnd) {
-        //    wxDebugMsg("Warning: could not find a valid handle, wx_win.cc/wxWndProc.\n");
         wnd->m_hWnd = (WXHWND) hWnd;
         long res = wnd->MSWDefWindowProc(message, wParam, lParam );
         wnd->m_hWnd = 0;
@@ -880,7 +922,7 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
 
 #ifdef __WXDEBUG__
     wxLogTrace(wxTraceMessages, "Processing %s(%lx, %lx)",
-        wxGetMessageName(message), wParam, lParam);
+               wxGetMessageName(message), wParam, lParam);
 #endif // __WXDEBUG__
 
     HWND hWnd = (HWND)m_hWnd;
@@ -1163,29 +1205,35 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
         }
 
     case WM_KEYDOWN:
-    {
         MSWOnKeyDown((WORD) wParam, lParam);
-#if 0
         // we consider these message "not interesting"
         if ( wParam == VK_SHIFT || wParam == VK_CONTROL )
             return Default();
 
-        // Avoid duplicate messages to OnChar
-        if ( (wParam != VK_ESCAPE) && (wParam != VK_SPACE) &&
-            (wParam != VK_RETURN) && (wParam != VK_BACK) &&
-            (wParam != VK_TAB) )
+        // Avoid duplicate messages to OnChar for these special keys
+        switch ( wParam )
         {
-            MSWOnChar((WORD)wParam, lParam);
-            if ( ::GetKeyState(VK_CONTROL) & 0x100 )
-                return Default();
+            case VK_ESCAPE:
+            case VK_SPACE:
+            case VK_RETURN:
+            case VK_BACK:
+            case VK_TAB:
+            case VK_LEFT:
+            case VK_RIGHT:
+            case VK_DOWN:
+            case VK_UP:
+                if ( ::GetKeyState(VK_CONTROL) & 0x100 )
+                    MSWOnChar((WORD)wParam, lParam);
+                break;
+
+            default:
+                MSWOnChar((WORD)wParam, lParam);
+                if ( ::GetKeyState(VK_CONTROL) & 0x100 )
+                    return Default();
+                break;
         }
-        else if ( ::GetKeyState(VK_CONTROL) & 0x100 )
-            MSWOnChar((WORD)wParam, lParam);
-        else
-            return Default();
-#endif
+
         break;
-    }
 
     case WM_KEYUP:
     {
@@ -1397,6 +1445,7 @@ long wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
     default:
         return MSWDefWindowProc(message, wParam, lParam );
     }
+
     return 0; // Success: we processed this command.
 }
 
@@ -1420,7 +1469,7 @@ 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 != NULL, "attempt to add a NULL hWnd to window list" );
+    wxCHECK_RET( hWnd != (HWND) NULL, "attempt to add a NULL hWnd to window list" );
 
     if ( !wxWinHandleList->Find((long)hWnd) )
         wxWinHandleList->Append((long)hWnd, win);
@@ -1663,6 +1712,13 @@ bool wxWindow::MSWOnSetFocus(WXHWND WXUNUSED(hwnd))
             ::ShowCaret((HWND) GetHWND());
     }
 
+    // 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 *)parent)->SetLastFocus(this);
+    }
+
     wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
     event.SetEventObject(this);
     if (!GetEventHandler()->ProcessEvent(event))
@@ -1888,26 +1944,36 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
         if ( msg->message != WM_KEYDOWN )
             bProcess = FALSE;
 
-        if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
+        if ( bProcess && (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
             bProcess = FALSE;
 
-        bool bCtrlDown = (::GetKeyState(VK_CONTROL) & 0x100) != 0;
+        if ( bProcess )
+        {
+            bool bCtrlDown = (::GetKeyState(VK_CONTROL) & 0x100) != 0;
 
-        // WM_GETDLGCODE: if the control wants it for itself, don't process it
-        // (except for Ctrl-Tab combination which is always processed)
-        LONG lDlgCode = 0;
-        if ( bProcess && !bCtrlDown ) {
-            lDlgCode = ::SendMessage(msg->hwnd, WM_GETDLGCODE, 0, 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
+            // combinations which are always processed)
+            LONG lDlgCode = 0;
+            if ( !bCtrlDown )
+            {
+                lDlgCode = ::SendMessage(msg->hwnd, WM_GETDLGCODE, 0, 0);
+            }
+
+            bool bForward = TRUE,
+                 bWindowChange = FALSE;
 
-        bool bForward = TRUE;
-        if ( bProcess ) {
-            switch ( msg->wParam ) {
+            switch ( msg->wParam ) 
+            {
                 case VK_TAB:
-                    if ( lDlgCode & DLGC_WANTTAB )  // FALSE for Ctrl-Tab
+                    if ( lDlgCode & DLGC_WANTTAB ) {
                         bProcess = FALSE;
-                    else
+                    }
+                    else {
+                        // Ctrl-Tab cycles thru notebook pages
+                        bWindowChange = bCtrlDown;
                         bForward = !(::GetKeyState(VK_SHIFT) & 0x100);
+                    }
                     break;
 
                 case VK_UP:
@@ -1924,23 +1990,59 @@ bool wxWindow::MSWProcessMessage(WXMSG* pMsg)
                         bProcess = FALSE;
                     break;
 
+                case VK_RETURN:
+                    {
+                        if ( lDlgCode & DLGC_WANTMESSAGE )
+                        {
+                            // control wants to process Enter itself, don't
+                            // call IsDialogMessage() which would interpret
+                            // it
+                            return FALSE;
+                        }
+
+                        wxButton *btnDefault = GetDefaultItem();
+                        if ( btnDefault && !bCtrlDown )
+                        {
+                            // if there is a default button, Enter should
+                            // press it
+                            (void)::SendMessage((HWND)btnDefault->GetHWND(),
+                                                BM_CLICK, 0, 0);
+                            return TRUE;
+                        }
+                        // 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.
+                    }
+                    break;
+
                 default:
                     bProcess = FALSE;
             }
-        }
 
-        if ( bProcess ) {
-            wxNavigationKeyEvent event;
-            event.SetDirection(bForward);
-            event.SetWindowChange(bCtrlDown);
-            event.SetEventObject(this);
+            if ( bProcess )
+            {
+                wxNavigationKeyEvent event;
+                event.SetDirection(bForward);
+                event.SetWindowChange(bWindowChange);
+                event.SetEventObject(this);
 
-            if ( GetEventHandler()->ProcessEvent(event) )
-                return TRUE;
+                if ( GetEventHandler()->ProcessEvent(event) )
+                    return TRUE;
+            }
         }
 
-        return ::IsDialogMessage((HWND)GetHWND(), msg) != 0;
+        if ( ::IsDialogMessage((HWND)GetHWND(), msg) )
+            return TRUE;
+    }
+#if wxUSE_TOOLTIPS
+    if ( m_tooltip )
+    {
+        // relay mouse move events to the tooltip control
+        MSG *msg = (MSG *)pMsg;
+        if ( msg->message == WM_MOUSEMOVE )
+            m_tooltip->RelayEvent(pMsg);
     }
+#endif // wxUSE_TOOLTIPS
 
     return FALSE;
 }
@@ -2157,7 +2259,6 @@ void wxWindow::MSWOnMButtonDown(int x, int y, WXUINT flags)
 
 void wxWindow::MSWOnMButtonUp(int x, int y, WXUINT flags)
 {
-    //wxDebugMsg("MButtonUp\n") ;
     wxMouseEvent event(wxEVT_MIDDLE_UP);
 
     event.m_x = x; event.m_y = y;
@@ -2963,7 +3064,7 @@ void wxSetKeyboardHook(bool doIt)
     {
         wxTheKeyboardHookProc = MakeProcInstance((FARPROC) wxKeyboardHook, wxGetInstance());
         wxTheKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) wxTheKeyboardHookProc, wxGetInstance(),
-#ifdef __WIN32__
+#if defined(__WIN32__) && !defined(__TWIN32__)
             GetCurrentThreadId());
         //      (DWORD)GetCurrentProcess()); // This is another possibility. Which is right?
 #else
@@ -3047,13 +3148,6 @@ void wxWindow::Centre(int direction)
 
 }
 
-/* TODO (maybe)
-void wxWindow::OnPaint()
-{
-PaintSelectionHandles();
-}
-*/
-
 void wxWindow::WarpPointer (int x_pos, int y_pos)
 {
     // Move the pointer to (x_pos,y_pos) coordinates. They are expressed in
@@ -3527,7 +3621,7 @@ WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D)
 
     // 5) If this isn't a Win95 app, and we are using CTL3D, remove border
     // effects from extended style
-#if CTL3D
+#if wxUSE_CTL3D
     if ( *want3D )
         nativeBorder = FALSE;
 #endif
@@ -3537,7 +3631,7 @@ WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D)
     // If we want 3D, but haven't specified a border here,
     // apply the default border style specified.
     // TODO what about non-Win95 WIN32? Does it have borders?
-#if defined(__WIN95__) && !CTL3D
+#if defined(__WIN95__) && !wxUSE_CTL3D
     if (defaultBorderStyle && (*want3D) && ! ((m_windowStyle & wxDOUBLE_BORDER) || (m_windowStyle & wxRAISED_BORDER) ||
         (m_windowStyle & wxSTATIC_BORDER) || (m_windowStyle & wxSIMPLE_BORDER) ))
         exStyle |= defaultBorderStyle; // WS_EX_CLIENTEDGE ;
@@ -3548,18 +3642,6 @@ WXDWORD wxWindow::Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D)
 
 void wxWindow::OnChar(wxKeyEvent& event)
 {
-/* I'm commenting this out because otherwise, we lose tabs in e.g. a text window (see MDI sample)
- * (JACS, 14/01/99)
-    if ( event.KeyCode() == WXK_TAB ) {
-        // propagate the TABs to the parent - it's up to it to decide what
-        // to do with it
-        if ( GetParent() ) {
-            if ( GetParent()->GetEventHandler()->ProcessEvent(event) )
-                return;
-        }
-    }
-*/
-
     bool isVirtual;
     int id = wxCharCodeWXToMSW((int)event.KeyCode(), &isVirtual);
 
@@ -3570,21 +3652,6 @@ void wxWindow::OnChar(wxKeyEvent& event)
         (void) MSWDefWindowProc(m_lastMsg, (WPARAM) id, m_lastLParam);
 }
 
-void wxWindow::OnKeyDown(wxKeyEvent& event)
-{
-    Default();
-}
-
-void wxWindow::OnKeyUp(wxKeyEvent& event)
-{
-    Default();
-}
-
-void wxWindow::OnPaint(wxPaintEvent& event)
-{
-    Default();
-}
-
 bool wxWindow::IsEnabled(void) const
 {
     return (::IsWindowEnabled((HWND) GetHWND()) != 0);
@@ -3608,7 +3675,7 @@ bool wxWindow::TransferDataToWindow()
         if ( child->GetValidator() && /* child->GetValidator()->Ok() && */
             !child->GetValidator()->TransferToWindow() )
         {
-            wxMessageBox("Application Error", "Could not transfer data to window", wxOK|wxICON_EXCLAMATION);
+            wxLogError(_("Could not transfer data to window"));
             return FALSE;
         }
 
@@ -3990,16 +4057,17 @@ void wxWindow::SetConstraintSizes(bool recurse)
             winName = "unnamed";
         else
             winName = GetName();
-        wxDebugMsg("Constraint(s) not satisfied for window of type %s, name %s:\n", (const char *)windowClass, (const char *)winName);
+        wxLogDebug("Constraint(s) not satisfied for window of type %s, name %s:",
+                    (const char *)windowClass, (const char *)winName);
         if (!constr->left.GetDone())
-            wxDebugMsg("  unsatisfied 'left' constraint.\n");
+            wxLogDebug("  unsatisfied 'left' constraint.");
         if (!constr->right.GetDone())
-            wxDebugMsg("  unsatisfied 'right' constraint.\n");
+            wxLogDebug("  unsatisfied 'right' constraint.");
         if (!constr->width.GetDone())
-            wxDebugMsg("  unsatisfied 'width' constraint.\n");
+            wxLogDebug("  unsatisfied 'width' constraint.");
         if (!constr->height.GetDone())
-            wxDebugMsg("  unsatisfied 'height' constraint.\n");
-        wxDebugMsg("Please check constraints: try adding AsIs() constraints.\n");
+            wxLogDebug("  unsatisfied 'height' constraint.");
+        wxLogDebug("Please check constraints: try adding AsIs() constraints.\n");
     }
 
     if (recurse)
@@ -4425,6 +4493,7 @@ long wxWindow::MSWGetDlgCode()
 
 bool wxWindow::AcceptsFocus() const
 {
+    // invisible and disabled controls don't need focus
     return IsShown() && IsEnabled();
 }