HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL;
+// another helper class: set the variable to true during its lifetime and reset
+// it to false when it is destroyed
+//
+// it is currently always used with wxTreeCtrl::m_changingSelection
+class TempSetter
+{
+public:
+ TempSetter(bool& var) : m_var(var)
+ {
+ wxASSERT_MSG( !m_var, "variable shouldn't be already set" );
+ m_var = true;
+ }
+
+ ~TempSetter()
+ {
+ m_var = false;
+ }
+
+private:
+ bool& m_var;
+
+ wxDECLARE_NO_COPY_CLASS(TempSetter);
+};
+
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
m_pVirtualRoot = NULL;
m_dragStarted = false;
m_focusLost = true;
+ m_changingSelection = false;
m_triggerStateImageClick = false;
// initialize the global array of events now as it can't be done statically
}
}
- if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
{
- wxLogLastError(wxT("TreeView_DeleteItem"));
- return;
+ TempSetter set(m_changingSelection);
+ if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
+ {
+ wxLogLastError(wxT("TreeView_DeleteItem"));
+ return;
+ }
}
if ( !selected )
}
else
{
- ::SelectItem(GetHwnd(), HITEM(next), false);
- TreeView_SelectItem(GetHwnd(), 0);
+ DoUnselectItem(next);
+ ClearFocusedItem();
}
}
}
if ( IsTreeEventAllowed(changingEvent) )
{
- TreeView_SelectItem(GetHwnd(), 0);
+ ClearFocusedItem();
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, wxTreeItemId());
}
else
{
- TreeView_SelectItem(GetHwnd(), 0);
+ ClearFocusedItem();
}
}
for ( size_t n = 0; n < count; n++ )
{
- ::UnselectItem(GetHwnd(), HITEM(selections[n]));
+ DoUnselectItem(selections[n]);
}
m_htSelStart.Unset();
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- TreeView_SelectItem(GetHwnd(), 0);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this);
changedEvent.m_itemOld = htFocus;
}
}
+void wxTreeCtrl::DoSelectItem(const wxTreeItemId& item, bool select)
+{
+ TempSetter set(m_changingSelection);
+
+ ::SelectItem(GetHwnd(), HITEM(item), select);
+}
+
void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
{
wxCHECK_RET( !IsHiddenRoot(item), _T("can't select hidden root item") );
if ( IsTreeEventAllowed(changingEvent) )
{
HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
- ::SelectItem(GetHwnd(), HITEM(item), select);
+ DoSelectItem(item, select);
if ( !htFocus )
{
- ::SetFocus(GetHwnd(), HITEM(item));
+ SetFocusedItem(item);
}
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
}
else // ok
{
- ::SetFocus(GetHwnd(), HITEM(item));
+ SetFocusedItem(item);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, item);
}
}
+void wxTreeCtrl::ClearFocusedItem()
+{
+ TempSetter set(m_changingSelection);
+
+ if ( !TreeView_SelectItem(GetHwnd(), 0) )
+ {
+ wxLogLastError(wxT("TreeView_SelectItem"));
+ }
+}
+
+void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item)
+{
+ TempSetter set(m_changingSelection);
+
+ ::SetFocus(GetHwnd(), HITEM(item));
+}
+
+void wxTreeCtrl::DoUnselectItem(const wxTreeItemId& item)
+{
+ TempSetter set(m_changingSelection);
+
+ ::UnselectItem(GetHwnd(), HITEM(item));
+}
+
+void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId& item)
+{
+ TempSetter set(m_changingSelection);
+
+ ::ToggleItemSelection(GetHwnd(), HITEM(item));
+}
+
// ----------------------------------------------------------------------------
// sorting stuff
// ----------------------------------------------------------------------------
if ( IsTreeEventAllowed(changingEvent) )
{
- ::ToggleItemSelection(GetHwnd(), htSel);
+ DoToggleItemSelection(wxTreeItemId(htSel));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htSel);
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), htSel);
+ DoSelectItem(wxTreeItemId(htSel));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htSel);
if ( !bCtrl && !bShift )
{
wxArrayTreeItemIds selections;
- size_t count = GetSelections(selections);
wxTreeItemId next;
- if ( htSel && count > 0 )
+ if ( htSel )
{
next = vkey == VK_UP
? TreeView_GetPrevVisible(GetHwnd(), htSel)
if ( IsHiddenRoot(next) )
next = TreeView_GetChild(GetHwnd(), HITEM(next));
-
- if ( vkey == VK_DOWN )
- {
- wxTreeItemId next2 = TreeView_GetNextVisible(
- GetHwnd(), HITEM(next));
- if ( next2 )
- next = next2;
- }
}
if ( !next.IsOk() )
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), HITEM(next));
- ::SetFocus(GetHwnd(), HITEM(next));
+ DoSelectItem(next);
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, next);
}
}
- ::SetFocus(GetHwnd(), HITEM(next));
+ SetFocusedItem(next);
}
break;
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), HITEM(next));
- ::SetFocus(GetHwnd(), HITEM(next));
+ DoSelectItem(next);
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, next);
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), HITEM(next));
- ::SetFocus(GetHwnd(), HITEM(next));
+ DoSelectItem(next);
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
changedEvent.m_itemOld = htSel;
SelectRange(GetHwnd(),
HITEM(m_htSelStart), HITEM(next),
SR_UNSELECT_OTHERS);
- ::SetFocus(GetHwnd(), HITEM(next));
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, next);
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), HITEM(next));
- ::SetFocus(GetHwnd(), HITEM(next));
+ DoSelectItem(next);
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, next);
{
DoUnselectAll();
m_htSelStart.Unset();
- ::SelectItem(GetHwnd(), HITEM(next));
- ::SetFocus(GetHwnd(), HITEM(next));
+ DoSelectItem(next);
+ SetFocusedItem(next);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, next);
if ( !isMultiple )
break;
- processed = true;
m_htClickedItem.Unset();
if ( !(tvht.flags & TVHT_ONITEM) )
Collapse(htItem);
}
}
+
+ processed = true;
}
+ m_focusLost = false;
break;
}
+ processed = true;
SetFocus();
m_htClickedItem = (WXHTREEITEM) htItem;
m_ptClick = wxPoint(x, y);
if ( IsTreeEventAllowed(changingEvent) )
{
// toggle selected state
- ::ToggleItemSelection(GetHwnd(), htItem);
+ DoToggleItemSelection(wxTreeItemId(htItem));
- ::SetFocus(GetHwnd(), htItem);
+ SetFocusedItem(wxTreeItemId(htItem));
// reset on any click without Shift
m_htSelStart.Unset();
}
else
{
- ::SelectItem(GetHwnd(), htItem);
+ DoSelectItem(wxTreeItemId(htItem));
}
- ::SetFocus(GetHwnd(), htItem);
+ SetFocusedItem(wxTreeItemId(htItem));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htItem);
// clicked outside of the present selection, otherwise,
// perform the deselection on mouse-up, this allows
// multiple drag and drop to work.
-
if ( !IsItemSelected(GetHwnd(), htItem))
{
wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), htItem);
- ::SetFocus(GetHwnd(), htItem);
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htItem);
}
else
{
- ::SetFocus(GetHwnd(), htItem);
+ SetFocusedItem(wxTreeItemId(htItem));
}
}
else // click on a single selected item
// since previous click
if ( m_focusLost )
{
- TreeView_SelectItem(GetHwnd(), 0);
- ::SelectItem(GetHwnd(), htItem);
+ ClearFocusedItem();
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
+ }
+ else
+ {
+ processed = false;
}
-
- processed = false;
}
// reset on any click without Shift
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), htItem);
- ::SetFocus(GetHwnd(), htItem);
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htItem);
if ( IsTreeEventAllowed(changingEvent) )
{
DoUnselectAll();
- ::SelectItem(GetHwnd(), htItem);
- ::SetFocus(GetHwnd(), htItem);
+ DoSelectItem(wxTreeItemId(htItem));
+ SetFocusedItem(wxTreeItemId(htItem));
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
this, htItem);
// use the key to update the selection if it was left
// unprocessed
MSWHandleSelectionKey(wParam);
-
- // pretend that we did process it in any case as we already
- // generated an event for it
- processed = true;
}
+ // pretend that we did process it in any case as we already
+ // generated an event for it
+ processed = true;
+
//default: for all the other keys leave processed as false so that
// the tree control generates a TVN_KEYDOWN for us
}
// we have to handle both messages:
case TVN_SELCHANGEDA:
case TVN_SELCHANGEDW:
- if ( !HasFlag(wxTR_MULTIPLE) )
+ if ( !HasFlag(wxTR_MULTIPLE) || !m_changingSelection )
{
eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
}
case TVN_SELCHANGINGA:
case TVN_SELCHANGINGW:
- if ( !HasFlag(wxTR_MULTIPLE) )
+ if ( !HasFlag(wxTR_MULTIPLE) || !m_changingSelection )
{
if ( eventType == wxEVT_NULL )
eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;