+ m_currentEdit = item.m_pItem;
+
+ wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
+ te.m_item = m_currentEdit;
+ te.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( te );
+
+ if (!te.IsAllowed()) return;
+
+ // We have to call this here because the label in
+ // question might just have been added and no screen
+ // update taken place.
+ if (m_dirty) wxYield();
+
+ wxString s = m_currentEdit->GetText();
+ int x = m_currentEdit->GetX();
+ int y = m_currentEdit->GetY();
+ int w = m_currentEdit->GetWidth();
+ int h = m_currentEdit->GetHeight();
+
+ int image_h = 0;
+ int image_w = 0;
+
+ int image = m_currentEdit->GetCurrentImage();
+ if ( image != NO_IMAGE )
+ {
+ if ( m_imageListNormal )
+ {
+ m_imageListNormal->GetSize( image, image_w, image_h );
+ image_w += 4;
+ }
+ else
+ {
+ wxFAIL_MSG(_T("you must create an image list to use images!"));
+ }
+ }
+ x += image_w;
+ w -= image_w + 4; // I don't know why +4 is needed
+
+ wxClientDC dc(this);
+ PrepareDC( dc );
+ x = dc.LogicalToDeviceX( x );
+ y = dc.LogicalToDeviceY( y );
+
+ wxTreeTextCtrl *text = new wxTreeTextCtrl(
+ this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
+ text->SetFocus();
+}
+
+void wxTreeCtrl::OnRenameTimer()
+{
+ Edit( m_current );
+}
+
+void wxTreeCtrl::OnRenameAccept()
+{
+ wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
+ le.m_item = m_currentEdit;
+ le.SetEventObject( this );
+ le.m_label = m_renameRes;
+ GetEventHandler()->ProcessEvent( le );
+
+ if (!le.IsAllowed()) return;
+
+ SetItemText( m_currentEdit, m_renameRes );
+}
+
+void wxTreeCtrl::OnMouse( wxMouseEvent &event )
+{
+ if ( !m_anchor ) return;
+
+ // we process left mouse up event (enables in-place edit), right down
+ // (pass to the user code), left dbl click (activate item) and
+ // dragging/moving events for items drag-and-drop
+ if ( !(event.LeftUp() ||
+ event.RightDown() ||
+ event.LeftDClick() ||
+ event.Dragging() ||
+ ((event.Moving() || event.RightUp()) && m_isDragging)) )
+ {
+ event.Skip();
+
+ return;
+ }
+
+ wxClientDC dc(this);
+ PrepareDC(dc);
+ wxCoord x = dc.DeviceToLogicalX( event.GetX() );
+ wxCoord y = dc.DeviceToLogicalY( event.GetY() );
+
+ int flags = 0;
+ wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
+
+ bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
+
+ if ( event.Dragging() && !m_isDragging )
+ {
+ if (m_dragCount == 0)
+ m_dragStart = wxPoint(x,y);
+
+ m_dragCount++;
+
+ if (m_dragCount != 3)
+ {
+ // wait until user drags a bit further...
+ return;
+ }
+
+ wxEventType command = event.RightIsDown()
+ ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
+ : wxEVT_COMMAND_TREE_BEGIN_DRAG;
+
+ wxTreeEvent nevent( command, GetId() );
+ nevent.m_item = m_current;
+ nevent.SetEventObject(this);
+
+ // by default the dragging is not supported, the user code must
+ // explicitly allow the event for it to take place
+ nevent.Veto();
+
+ if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
+ {
+ // we're going to drag this item
+ m_isDragging = TRUE;
+
+ // remember the old cursor because we will change it while
+ // dragging
+ m_oldCursor = m_cursor;
+
+ // in a single selection control, hide the selection temporarily
+ if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
+ {
+ m_oldSelection = GetSelection().m_pItem;
+
+ if ( m_oldSelection )
+ {
+ m_oldSelection->SetHilight(FALSE);
+ RefreshLine(m_oldSelection);
+ }
+ }
+
+ CaptureMouse();
+ }
+ }
+ else if ( event.Moving() )
+ {
+ if ( item != m_dropTarget )
+ {
+ // unhighlight the previous drop target
+ DrawDropEffect(m_dropTarget);
+
+ m_dropTarget = item;
+
+ // highlight the current drop target if any
+ DrawDropEffect(m_dropTarget);
+
+ wxYield();
+ }
+ }
+ else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
+ {
+ // erase the highlighting
+ DrawDropEffect(m_dropTarget);
+
+ // generate the drag end event
+ wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
+
+ event.m_item = item;
+ event.m_pointDrag = wxPoint(x, y);
+ event.SetEventObject(this);
+
+ (void)GetEventHandler()->ProcessEvent(event);
+
+ m_isDragging = FALSE;
+ m_dropTarget = (wxGenericTreeItem *)NULL;
+
+ if ( m_oldSelection )
+ {
+ m_oldSelection->SetHilight(TRUE);
+ RefreshLine(m_oldSelection);
+ m_oldSelection = (wxGenericTreeItem *)NULL;
+ }
+
+ ReleaseMouse();
+
+ SetCursor(m_oldCursor);
+
+ wxYield();
+ }
+ else
+ {
+ // here we process only the messages which happen on tree items
+
+ m_dragCount = 0;
+
+ if (item == NULL) return; /* we hit the blank area */
+
+ if ( event.RightDown() )
+ {
+ wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
+ nevent.m_item = item;
+ nevent.m_code = 0;
+ nevent.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(nevent);
+ }
+ else if ( event.LeftUp() && (item == m_current) &&
+ (flags & wxTREE_HITTEST_ONITEMLABEL) &&
+ HasFlag(wxTR_EDIT_LABELS) )
+ {
+ m_renameTimer->Start( 100, TRUE );
+ }
+ else
+ {
+ // how should the selection work for this event?
+ bool is_multiple, extended_select, unselect_others;
+ EventFlagsToSelType(GetWindowStyleFlag(),
+ event.ShiftDown(),
+ event.ControlDown(),
+ is_multiple, extended_select, unselect_others);
+
+ if ( onButton )
+ {
+ Toggle( item );
+ if ( is_multiple )
+ return;
+ }
+
+ SelectItem(item, unselect_others, extended_select);
+
+ if ( event.LeftDClick() )
+ {
+ wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
+ nevent.m_item = item;
+ nevent.m_code = 0;
+ nevent.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( nevent );
+ }
+ }
+ }
+}
+
+void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
+{
+ /* after all changes have been done to the tree control,
+ * we actually redraw the tree when everything is over */
+
+ if (!m_dirty)
+ return;
+
+ m_dirty = FALSE;
+
+ CalculatePositions();
+ Refresh();
+ AdjustMyScrollbars();
+}
+
+void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
+{
+ wxCoord text_w = 0;
+ wxCoord text_h = 0;
+
+ if (item->IsBold())
+ dc.SetFont(m_boldFont);
+
+ dc.GetTextExtent( item->GetText(), &text_w, &text_h );
+ text_h+=2;
+
+ // restore normal font
+ dc.SetFont( m_normalFont );
+
+ int image_h = 0;
+ int image_w = 0;
+ int image = item->GetCurrentImage();
+ if ( image != NO_IMAGE )
+ {
+ if ( m_imageListNormal )
+ {
+ m_imageListNormal->GetSize( image, image_w, image_h );
+ image_w += 4;
+ }
+ }
+
+ int total_h = (image_h > text_h) ? image_h : text_h;
+
+ if (total_h < 40)
+ total_h += 2; // at least 2 pixels
+ else
+ total_h += total_h/10; // otherwise 10% extra spacing
+
+ item->SetHeight(total_h);
+ if (total_h>m_lineHeight)
+ m_lineHeight=total_h;
+
+ item->SetWidth(image_w+text_w+2);