X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c9b142c9f8a33853c9183df8304bd6fe8d945964..18128cbb47b5775f20fb449dd34f36fa31776d51:/src/msw/treectrl.cpp diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp index 298bd38744..d4a3025c38 100644 --- a/src/msw/treectrl.cpp +++ b/src/msw/treectrl.cpp @@ -303,27 +303,24 @@ struct wxTreeViewItem : public TV_ITEM // wxVirutalNode is used in place of a single root when 'hidden' root is // specified. -class wxVirtualNode +class wxVirtualNode : public wxTreeViewItem { public: wxVirtualNode(wxTreeItemData *data) + : wxTreeViewItem(TVI_ROOT, 0) { m_data = data; - - m_tvItem = new wxTreeViewItem(TVI_ROOT, 0); } ~wxVirtualNode() { delete m_data; - delete m_tvItem; } wxTreeItemData *GetData() const { return m_data; } void SetData(wxTreeItemData *data) { delete m_data; m_data = data; } private: - wxTreeViewItem *m_tvItem; wxTreeItemData *m_data; }; @@ -894,6 +891,20 @@ void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) wxTreeViewItem tvItem(item, TVIF_TEXT); tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok DoSetItem(&tvItem); + + // when setting the text of the item being edited, the text control should + // be updated to reflect the new text as well, otherwise calling + // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect + // + // don't use GetEditControl() here because m_textCtrl is not set yet + HWND hwndEdit = TreeView_GetEditControl(GetHwnd()); + if ( hwndEdit ) + { + if ( item == GetSelection() ) + { + ::SetWindowText(hwndEdit, text); + } + } } int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item, @@ -1517,7 +1528,7 @@ wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, return TVI_ROOT; } - return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0, + return DoInsertItem(wxTreeItemId((long)(WXHTREEITEM) 0), (long)(WXHTREEITEM) 0, text, image, selectedImage, data); } @@ -1624,11 +1635,8 @@ void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) wxT("Unknown flag in wxTreeCtrl::DoExpand") ); // A hidden root can be neither expanded nor collapsed. - if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) ) - { - // No action will be taken. - return; - } + wxCHECK_RET( !(m_windowStyle & wxTR_HIDE_ROOT) || (HITEM(item) != TVI_ROOT), + wxT("Can't expand/collapse hidden root node!") ) // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must // emulate them. This behaviour has changed slightly with comctl32.dll @@ -1767,26 +1775,8 @@ void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) } } -wxTextCtrl* wxTreeCtrl::GetEditControl() const +wxTextCtrl *wxTreeCtrl::GetEditControl() const { - // normally, we could try to do something like this to return something - // even when the editing was started by the user and not by calling - // EditLabel() - but as nobody has asked for this so far and there might be - // problems in the code below, I leave it disabled for now (VZ) -#if 0 - if ( !m_textCtrl ) - { - HWND hwndText = TreeView_GetEditControl(GetHwnd()); - if ( hwndText ) - { - m_textCtrl = new wxTextCtrl(this, -1); - m_textCtrl->Hide(); - m_textCtrl->SetHWND((WXHWND)hwndText); - } - //else: not editing label right now - } -#endif // 0 - return m_textCtrl; } @@ -1816,20 +1806,19 @@ wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, DeleteTextCtrl(); + m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); // this is not an error - the TVN_BEGINLABELEDIT handler might have // returned FALSE if ( !hWnd ) { + delete m_textCtrl; + m_textCtrl = NULL; return NULL; } - m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); - m_textCtrl->SetParent(this); - m_textCtrl->SetHWND((WXHWND)hWnd); - m_textCtrl->SubclassWin((WXHWND)hWnd); - + // textctrl is subclassed in MSWOnNotify return m_textCtrl; } @@ -2305,13 +2294,26 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) eventType = wxEVT_COMMAND_TREE_KEY_DOWN; TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; - // we pass 0 as last CreateKeyEvent() parameter because we - // don't have access to the real key press flags here - but as - // it is only used to determin wxKeyEvent::m_altDown flag it's - // not too bad + // fabricate the lParam and wParam parameters sufficiently + // similar to the ones from a "real" WM_KEYDOWN so that + // CreateKeyEvent() works correctly + WXLPARAM lParam = + (::GetKeyState(VK_MENU) & 0x100 ? KF_ALTDOWN : 0) << 16; + + WXWPARAM wParam = info->wVKey; + + int keyCode = wxCharCodeMSWToWX(info->wVKey); + if ( !keyCode ) + { + // wxCharCodeMSWToWX() returns 0 to indicate that this is a + // simple ASCII key + keyCode = wParam; + } + event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, - wxCharCodeMSWToWX(info->wVKey), - 0); + keyCode, + lParam, + wParam); // a separate event for Space/Return if ( !wxIsCtrlDown() && !wxIsShiftDown() && @@ -2532,6 +2534,32 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case TVN_BEGINLABELEDIT: // return TRUE to cancel label editing *result = !event.IsAllowed(); + // set ES_WANTRETURN ( like we do in BeginLabelEdit ) + if(event.IsAllowed()) + { + HWND hText = TreeView_GetEditControl(GetHwnd()); + if(hText != NULL) + { + // MBN: if m_textCtrl already has an HWND, it is a stale + // pointer from a previous edit (because the user + // didn't modify the label before dismissing the control, + // and TVN_ENDLABELEDIT was not sent), so delete it + if(m_textCtrl && m_textCtrl->GetHWND() != 0) + DeleteTextCtrl(); + if(!m_textCtrl) + m_textCtrl = new wxTextCtrl(); + m_textCtrl->SetParent(this); + m_textCtrl->SetHWND((WXHWND)hText); + m_textCtrl->SubclassWin((WXHWND)hText); + + // set wxTE_PROCESS_ENTER style for the text control to + // force it to process the Enter presses itself, otherwise + // they could be stolen from it by the dialog + // navigation code + m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() + | wxTE_PROCESS_ENTER); + } + } break; case TVN_ENDLABELEDIT: @@ -2558,16 +2586,13 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // an image depending on the expanded/collapsed state - bug in // comctl32.dll or our code? { - NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam; - if ( tv->action == TVE_EXPAND ) - { - wxTreeItemId id = (WXHTREEITEM)tv->itemNew.hItem; + NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam; + wxTreeItemId id = (WXHTREEITEM)tv->itemNew.hItem; - int image = GetItemImage(id, wxTreeItemIcon_Expanded); - if ( image != -1 ) - { - RefreshItem(id); - } + int image = GetItemImage(id, wxTreeItemIcon_Expanded); + if ( image != -1 ) + { + RefreshItem(id); } } break; @@ -2575,7 +2600,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case TVN_GETDISPINFO: // NB: so far the user can't set the image himself anyhow, so do it // anyway - but this may change later -// if ( /* !processed && */ 1 ) + //if ( /* !processed && */ 1 ) { wxTreeItemId item = event.m_item; TV_DISPINFO *info = (TV_DISPINFO *)lParam;