}
bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size,
}
bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size,
{
Init();
wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
{
Init();
wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
+wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
+{
+ wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
+
+ wxArrayTreeItems& children = item.m_pItem->GetChildren();
+ return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
+}
+
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
{
wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
{
wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
size_t n = (size_t)(index + 1);
return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
size_t n = (size_t)(index + 1);
return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
"previous item in wxTreeCtrl::InsertItem() is not a sibling" );
return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
}
"previous item in wxTreeCtrl::InsertItem() is not a sibling" );
return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
}
m_anchor->GetSize( x, y );
y += 2*m_lineHeight;
int x_pos = GetScrollPos( wxHORIZONTAL );
m_anchor->GetSize( x, y );
y += 2*m_lineHeight;
int x_pos = GetScrollPos( wxHORIZONTAL );
- SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
+ SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
}
wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
wxClassInfo* WXUNUSED(textCtrlClass) )
{
}
wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
wxClassInfo* WXUNUSED(textCtrlClass) )
{
wxCHECK_RET( itemId.IsOk(), "invalid tree item" );
wxGenericTreeItem *item = itemId.m_pItem;
wxCHECK_RET( itemId.IsOk(), "invalid tree item" );
wxGenericTreeItem *item = itemId.m_pItem;
wxCHECK_RET( !s_treeBeingSorted,
"wxTreeCtrl::SortChildren is not reentrant" );
wxCHECK_RET( !s_treeBeingSorted,
"wxTreeCtrl::SortChildren is not reentrant" );
s_treeBeingSorted = this;
children.Sort(tree_ctrl_compare_func);
s_treeBeingSorted = NULL;
s_treeBeingSorted = this;
children.Sort(tree_ctrl_compare_func);
s_treeBeingSorted = NULL;
- // render bold items in bold
- wxFont fontOld;
- wxFont fontNew;
-
- if ( item->IsBold() )
- {
- fontOld = dc.GetFont();
- if (fontOld.Ok())
- {
- // @@ is there any better way to make a bold variant of old font?
- fontNew = wxFont( fontOld.GetPointSize(),
- fontOld.GetFamily(),
- fontOld.GetStyle(),
- wxBOLD,
- fontOld.GetUnderlined());
- dc.SetFont(fontNew);
- }
- else
- {
- wxFAIL_MSG("wxDC::GetFont() failed!");
- }
- }
+ /* render bold items in bold */
+ wxFont fontOld;
+ wxFont fontNew;
+
+ if (item->IsBold())
+ {
+ fontOld = dc.GetFont();
+ if (fontOld.Ok())
+ {
+ /* @@ is there any better way to make a bold variant of old font? */
+ fontNew = wxFont( fontOld.GetPointSize(),
+ fontOld.GetFamily(),
+ fontOld.GetStyle(),
+ wxBOLD,
+ fontOld.GetUnderlined());
+ dc.SetFont(fontNew);
+ }
+ else
+ {
+ wxFAIL_MSG("wxDC::GetFont() failed!");
+ }
+ }
- int image_h = 0;
- int image_w = 0;
- if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
- {
- m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
- image_w += 4;
- }
- else if (item->GetImage() != -1)
- {
- m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
- image_w += 4;
- }
+ int image_h = 0;
+ int image_w = 0;
+ if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
+ {
+ m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
+ image_w += 4;
+ }
+ else if (item->GetImage() != -1)
+ {
+ m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
+ image_w += 4;
+ }
- dc.DrawRectangle( item->GetX()-2, item->GetY()-2, image_w+text_w+4, text_h+4 );
+ dc.DrawRectangle( item->GetX()-2, item->GetY()-2, image_w+text_w+4, text_h+4 );
- if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
- {
- dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
- m_imageListNormal->Draw( item->GetSelectedImage(), dc,
- item->GetX(), item->GetY()-1,
- wxIMAGELIST_DRAW_TRANSPARENT );
- dc.DestroyClippingRegion();
- }
- else if (item->GetImage() != -1)
- {
- dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
- m_imageListNormal->Draw( item->GetImage(), dc,
- item->GetX(), item->GetY()-1,
- wxIMAGELIST_DRAW_TRANSPARENT );
- dc.DestroyClippingRegion();
- }
+ if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
+ {
+ dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
+ m_imageListNormal->Draw( item->GetSelectedImage(), dc,
+ item->GetX(), item->GetY()-1,
+ wxIMAGELIST_DRAW_TRANSPARENT );
+ dc.DestroyClippingRegion();
+ }
+ else if (item->GetImage() != -1)
+ {
+ dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, text_h );
+ m_imageListNormal->Draw( item->GetImage(), dc,
+ item->GetX(), item->GetY()-1,
+ wxIMAGELIST_DRAW_TRANSPARENT );
+ dc.DestroyClippingRegion();
+ }
- dc.SetBackgroundMode(wxTRANSPARENT);
- dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() );
+ dc.SetBackgroundMode(wxTRANSPARENT);
+ dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY() );
}
void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
{
}
void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
{
- item->SetX( horizX+33 );
- item->SetY( y-m_lineHeight/3 );
- item->SetHeight( m_lineHeight );
+ item->SetX( horizX+33 );
+ item->SetY( y-m_lineHeight/3 );
+ item->SetHeight( m_lineHeight );
- if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
- {
- int startX = horizX;
- int endX = horizX + 10;
+ if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
+ {
+ int startX = horizX;
+ int endX = horizX + 10;
- dc.DrawLine( startX, y, endX, y );
+ dc.DrawLine( startX, y, endX, y );
- if (item->HasPlus())
- {
- dc.DrawLine( horizX+20, y, horizX+30, y );
- dc.SetPen( *wxGREY_PEN );
- dc.SetBrush( *wxWHITE_BRUSH );
- dc.DrawRectangle( horizX+10, y-4, 11, 9 );
- dc.SetPen( *wxBLACK_PEN );
- dc.DrawLine( horizX+13, y, horizX+18, y );
-
- if (!item->IsExpanded())
- dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
- }
+ if (item->HasPlus())
+ {
+ dc.DrawLine( horizX+20, y, horizX+30, y );
+ dc.SetPen( *wxGREY_PEN );
+ dc.SetBrush( *wxWHITE_BRUSH );
+ dc.DrawRectangle( horizX+10, y-4, 11, 9 );
+ dc.SetPen( *wxBLACK_PEN );
+ dc.DrawLine( horizX+13, y, horizX+18, y );
+
+ if (!item->IsExpanded())
+ {
+ dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
+ }
+ }
- if (item->HasHilight())
- {
- dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
+ if (item->HasHilight())
+ {
+ dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
- dc.SetPen( *wxBLACK_PEN );
- dc.SetTextForeground( *wxBLACK );
- dc.SetBrush( *wxWHITE_BRUSH );
- }
- else
- {
- dc.SetBrush( *wxWHITE_BRUSH );
- dc.SetPen( *wxTRANSPARENT_PEN );
+ dc.SetPen( *wxBLACK_PEN );
+ dc.SetTextForeground( *wxBLACK );
+ dc.SetBrush( *wxWHITE_BRUSH );
+ }
+ else
+ {
+ dc.SetBrush( *wxWHITE_BRUSH );
+ dc.SetPen( *wxTRANSPARENT_PEN );
- if ( item->IsExpanded() )
- {
- int semiOldY = y;
-
- wxArrayTreeItems& children = item->GetChildren();
- size_t count = children.Count();
- for ( size_t n = 0; n < count; n++ )
- {
- y += m_lineHeight;
- semiOldY = y;
+ if (item->IsExpanded())
+ {
+ int semiOldY = y;
- PaintLevel( children[n], dc, level+1, y );
- }
+ wxArrayTreeItems& children = item->GetChildren();
+ size_t count = children.Count();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ y += m_lineHeight;
+ semiOldY = y;
+ PaintLevel( children[n], dc, level+1, y );
+ }
- // it may happen that the item is expanded but has no items (when you
- // delete all its children for example) - don't draw the vertical line
- // in this case
- if ( count > 0 )
- dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
- }
+ /* it may happen that the item is expanded but has no items (when you
+ * delete all its children for example) - don't draw the vertical line
+ * in this case */
+ if (count > 0) dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
+ }
- wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
- te.m_code = event.KeyCode();
- te.SetEventObject( this );
- GetEventHandler()->ProcessEvent( te );
+ wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
+ te.m_code = event.KeyCode();
+ te.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( te );
- case ' ':
- case WXK_RETURN:
- {
- wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
- event.m_item = m_current;
- event.m_code = 0;
- event.SetEventObject( this );
- GetEventHandler()->ProcessEvent( event );
- }
- break;
+ case ' ':
+ case WXK_RETURN:
+ {
+ wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
+ event.m_item = m_current;
+ event.m_code = 0;
+ event.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( event );
+ }
+ break;
- case WXK_UP:
- {
- wxTreeItemId prev = GetPrevSibling( m_current );
- if (!prev)
- {
- prev = GetParent( m_current );
- long cockie = 0;
- wxTreeItemId current = m_current;
- if (current == GetFirstChild( prev, cockie ))
- {
- // otherwise we return to where we came from
- SelectItem( prev );
- EnsureVisible( prev );
- break;
- }
- }
- if (prev)
- {
- while (IsExpanded(prev))
- {
- int c = (int)GetChildrenCount( prev, FALSE );
- long cockie = 0;
- prev = GetFirstChild( prev, cockie );
- for (int i = 0; i < c-1; i++)
- prev = GetNextSibling( prev );
- }
- SelectItem( prev );
- EnsureVisible( prev );
- }
- }
- break;
- case WXK_LEFT:
- {
- wxTreeItemId prev = GetPrevSibling( m_current );
- if (prev != 0)
- {
- SelectItem( prev );
- EnsureVisible( prev );
- }
- else
- {
- prev = GetParent( m_current );
- if (prev)
- {
- EnsureVisible( prev );
- SelectItem( prev );
- }
- }
- }
- break;
+ // up goes to the previous sibling or to the last of its children if
+ // it's expanded
+ case WXK_UP:
+ {
+ wxTreeItemId prev = GetPrevSibling( m_current );
+ if (!prev)
+ {
+ prev = GetParent( m_current );
+ long cockie = 0;
+ wxTreeItemId current = m_current;
+ if (current == GetFirstChild( prev, cockie ))
+ {
+ // otherwise we return to where we came from
+ SelectItem( prev );
+ EnsureVisible( prev );
+ break;
+ }
+ }
+ if (prev)
+ {
+ while (IsExpanded(prev))
+ {
+ int c = (int)GetChildrenCount( prev, FALSE );
+ long cockie = 0;
+ prev = GetFirstChild( prev, cockie );
+ for (int i = 0; i < c-1; i++)
+ prev = GetNextSibling( prev );
+ }
+ SelectItem( prev );
+ EnsureVisible( prev );
+ }
+ }
+ break;
- case WXK_DOWN:
- {
- if (IsExpanded(m_current))
- {
- long cookie = 0;
- wxTreeItemId child = GetFirstChild( m_current, cookie );
- SelectItem( child );
- EnsureVisible( child );
- }
- else
- {
- wxTreeItemId next = GetNextSibling( m_current );
- if (next == 0)
- {
- wxTreeItemId current = m_current;
- while (current && !next)
+ case WXK_RIGHT:
+ // this works the same as the down arrow except that we also expand the
+ // item if it wasn't expanded yet
+ Expand(m_current);
+ // fall through
+
+ case WXK_DOWN:
- current = GetParent( current );
- if (current) next = GetNextSibling( current );
+ if (IsExpanded(m_current))
+ {
+ long cookie = 0;
+ wxTreeItemId child = GetFirstChild( m_current, cookie );
+ SelectItem( child );
+ EnsureVisible( child );
+ }
+ else
+ {
+ wxTreeItemId next = GetNextSibling( m_current );
+ if (next == 0)
+ {
+ wxTreeItemId current = m_current;
+ while (current && !next)
+ {
+ current = GetParent( current );
+ if (current) next = GetNextSibling( current );
+ }
+ }
+ if (next != 0)
+ {
+ SelectItem( next );
+ EnsureVisible( next );
+ }
+ }
- default:
- event.Skip();
- }
+ // <End> selects the last visible tree item
+ case WXK_END:
+ {
+ wxTreeItemId last = GetRootItem();
+
+ while ( last.IsOk() && IsExpanded(last) )
+ {
+ wxTreeItemId lastChild = GetLastChild(last);
+
+ // it may happen if the item was expanded but then all of
+ // its children have been deleted - so IsExpanded() returned
+ // TRUE, but GetLastChild() returned invalid item
+ if ( !lastChild )
+ break;
+
+ last = lastChild;
+ }
+
+ if ( last.IsOk() )
+ {
+ EnsureVisible( last );
+ SelectItem( last );
+ }
+ }
+ break;
+
+ // <Home> selects the root item
+ case WXK_HOME:
+ {
+ wxTreeItemId prev = GetRootItem();
+ if (prev)
+ {
+ EnsureVisible( prev );
+ SelectItem( prev );
+ }
+ }
+ break;
+
+ default:
+ event.Skip();
+ }
- if ( event.LeftDClick() )
- {
- wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
- event.m_item = item;
- event.m_code = 0;
- event.SetEventObject( this );
- GetEventHandler()->ProcessEvent( event );
- }
+ if (item == NULL) return; /* we hit the blank area */
- if ( onButton )
- {
- Toggle( item );
- }
+ if (event.Dragging())
+ {
+ if (m_dragCount == 2) /* small drag latency (3?) */
+ {
+ m_dragCount = 0;
+
+ wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
+ nevent.m_item = m_current;
+ nevent.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(nevent);
+ }
+ else
+ {
+ m_dragCount++;
+ }
+ return;
+ }
+
+ if (!IsSelected(item)) SelectItem(item); /* we dont support multiple selections, BTW */
+
+ if (event.LeftDClick())
+ {
+ wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
+ event.m_item = item;
+ event.m_code = 0;
+ event.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( event );
+ }
+
+ if (onButton)
+ {
+ Toggle( item );
+ }
-// -----------------------------------------------------------------------------
-void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item,
- wxDC &dc,
- int level,
- int &y )
+
+void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
- item->SetX( horizX+33 );
- item->SetY( y-m_lineHeight/3-2 );
- item->SetHeight( m_lineHeight );
+ item->SetX( horizX+33 );
+ item->SetY( y-m_lineHeight/3-2 );
+ item->SetHeight( m_lineHeight );
- wxArrayTreeItems& children = item->GetChildren();
- size_t count = children.Count();
- for ( size_t n = 0; n < count; n++ )
- {
- y += m_lineHeight;
- CalculateLevel( children[n], dc, level+1, y );
- }
+ wxArrayTreeItems& children = item->GetChildren();
+ size_t count = children.Count();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ y += m_lineHeight;
+ CalculateLevel( children[n], dc, level+1, y ); /* recurse */
+ }
- int y = m_lineHeight / 2 + 2;
- CalculateLevel( m_anchor, dc, 0, y );
+ int y = m_lineHeight / 2 + 2;
+ CalculateLevel( m_anchor, dc, 0, y ); /* start recursion */
- wxClientDC dc(this);
- PrepareDC( dc );
-
- wxRect rect;
- rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
- rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
- rect.width = 1000;
- rect.height = dc.GetCharHeight() + 6;
- Refresh( TRUE, &rect );
+ wxClientDC dc(this);
+ PrepareDC( dc );
+
+ wxRect rect;
+ rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
+ rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
+ rect.width = 1000;
+ rect.height = dc.GetCharHeight() + 6;
+
+ Refresh( TRUE, &rect );