1. added wxTreeCtrl::GetLastChild (modified MSW, GTK, generic and docs)

2. added more kbd logic to the generic tree ctrl (Home, End are now
   understood, Left does the same thing as under Windows and not the same
   thing as Up arrow)


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1496 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 1999-01-27 15:28:48 +00:00
parent cce4b3fe2b
commit 978f38c238
9 changed files with 271 additions and 175 deletions

View File

@ -274,6 +274,17 @@ Gets the normal item image.
Returns the item label.
\membersection{wxTreeCtrl::GetLastChild}\label{wxtreectrlgetlastchild}
\constfunc{wxTreeItemId}{GetLastChild}{\param{const wxTreeItemId\&}{ item}}
Returns the last child of the item (or 0 if this item has no children).
\wxheading{See also}
\helpref{GetFirstChild}{wxtreectrlgetfirstchild},
\helpref{GetLastChild}{wxtreectrlgetlastchild}
\membersection{wxTreeCtrl::GetNextChild}\label{wxtreectrlgetnextchild}
\constfunc{wxTreeItemId}{GetNextChild}{\param{const wxTreeItemId\&}{ item}, \param{long\& }{cookie}}

View File

@ -333,6 +333,8 @@ public:
wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
// get the next child
wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;

View File

@ -264,8 +264,10 @@ public:
// get the first child of this item
wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
// get the next child
// get the next child (after GetFirstChild or GetNextChild)
wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;

View File

@ -264,8 +264,10 @@ public:
// get the first child of this item
wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
// get the next child
// get the next child (after GetFirstChild or GetNextChild)
wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;

View File

@ -299,6 +299,8 @@ public:
wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
// get the next child
wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
// get the last child of this item - this method doesn't use cookies
wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
// get the next sibling of this item
wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;

View File

@ -294,7 +294,7 @@ wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
onButton = TRUE;
return this;
}
int w = m_width;
if (m_image != -1) w += 20;
@ -359,20 +359,20 @@ void wxTreeCtrl::Init()
m_imageListNormal =
m_imageListState = (wxImageList *) NULL;
m_dragCount = 0;
}
bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size,
long style,
const wxValidator &validator,
const wxString& name )
long style,
const wxValidator &validator,
const wxString& name )
{
Init();
wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
SetValidator( validator );
SetBackgroundColour( *wxWHITE );
@ -556,7 +556,7 @@ wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) co
wxArrayTreeItems& children = item.m_pItem->GetChildren();
if ( (size_t)cookie < children.Count() )
{
return item.m_pItem->GetChildren().Item(cookie++);
return children.Item(cookie++);
}
else
{
@ -565,6 +565,14 @@ wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) co
}
}
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() : children.Last();
}
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
{
wxCHECK_MSG( item.IsOk(), wxTreeItemId(), "invalid tree item" );
@ -778,9 +786,9 @@ void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
{
wxGenericTreeItem *item = itemId.m_pItem;
if ( !item->HasPlus() )
if ( !item->HasPlus() )
return;
if ( item->IsExpanded() )
return;
@ -898,7 +906,7 @@ void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
int start_y = 0;
ViewStart( &start_x, &start_y );
start_y *= 10;
int client_h = 0;
int client_w = 0;
GetClientSize( &client_w, &client_h );
@ -910,7 +918,7 @@ void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
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 );
return;
}
@ -973,7 +981,7 @@ void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
wxCHECK_RET( itemId.IsOk(), "invalid tree item" );
wxGenericTreeItem *item = itemId.m_pItem;
wxCHECK_RET( !s_treeBeingSorted,
"wxTreeCtrl::SortChildren is not reentrant" );
@ -983,7 +991,7 @@ void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
s_treeBeingSorted = this;
children.Sort(tree_ctrl_compare_func);
s_treeBeingSorted = NULL;
m_dirty = TRUE;
}
//else: don't make the tree dirty as nothing changed
@ -1036,7 +1044,7 @@ void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
/* render bold items in bold */
wxFont fontOld;
wxFont fontNew;
if (item->IsBold())
{
fontOld = dc.GetFont();
@ -1066,7 +1074,7 @@ void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
{
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 );
@ -1136,9 +1144,9 @@ void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &
dc.DrawLine( horizX+13, y, horizX+18, y );
if (!item->IsExpanded())
{
{
dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
}
}
}
if (item->HasHilight())
@ -1213,154 +1221,187 @@ void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
{
m_hasFocus = TRUE;
if (m_current) RefreshLine( m_current );
}
void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
{
m_hasFocus = FALSE;
if (m_current) RefreshLine( m_current );
}
void wxTreeCtrl::OnChar( wxKeyEvent &event )
{
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 );
if (m_current == 0)
{
event.Skip();
return;
}
if (m_current == 0)
{
event.Skip();
return;
}
switch (event.KeyCode())
{
case '+':
case WXK_ADD:
if (m_current->HasPlus() && !IsExpanded(m_current))
{
Expand(m_current);
}
break;
case '-':
case WXK_SUBTRACT:
if (IsExpanded(m_current))
{
Collapse(m_current);
}
break;
case '*':
case WXK_MULTIPLY:
Toggle(m_current);
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;
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:
{
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)
switch (event.KeyCode())
{
case '+':
case WXK_ADD:
if (m_current->HasPlus() && !IsExpanded(m_current))
{
current = GetParent( current );
if (current) next = GetNextSibling( current );
Expand(m_current);
}
}
if (next != 0)
{
SelectItem( next );
EnsureVisible( next );
}
}
}
break;
break;
default:
event.Skip();
}
case '-':
case WXK_SUBTRACT:
if (IsExpanded(m_current))
{
Collapse(m_current);
}
break;
case '*':
case WXK_MULTIPLY:
Toggle(m_current);
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;
// 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;
// left arrow goes to the parent
case WXK_LEFT:
{
wxTreeItemId prev = GetParent( m_current );
if (prev)
{
EnsureVisible( prev );
SelectItem( prev );
}
}
break;
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:
{
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 );
}
}
}
break;
// <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();
}
}
wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
@ -1376,7 +1417,7 @@ void wxTreeCtrl::OnMouse( wxMouseEvent &event )
if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return;
if ( !m_anchor ) return;
wxClientDC dc(this);
PrepareDC(dc);
long x = dc.DeviceToLogicalX( (long)event.GetX() );
@ -1384,7 +1425,7 @@ void wxTreeCtrl::OnMouse( wxMouseEvent &event )
bool onButton = FALSE;
wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
if (item == NULL) return; /* we hit the blank area */
if (event.Dragging())
@ -1392,7 +1433,7 @@ void wxTreeCtrl::OnMouse( wxMouseEvent &event )
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);
@ -1404,7 +1445,7 @@ void wxTreeCtrl::OnMouse( wxMouseEvent &event )
}
return;
}
if (!IsSelected(item)) SelectItem(item); /* we dont support multiple selections, BTW */
if (event.LeftDClick())
@ -1448,7 +1489,7 @@ void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, i
if ( !item->IsExpanded() )
{
/* we dont need to calculate collapsed branches */
/* we dont need to calculate collapsed branches */
return;
}
@ -1507,7 +1548,7 @@ void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
rect.width = 1000;
rect.height = dc.GetCharHeight() + 6;
Refresh( TRUE, &rect );
}

View File

@ -6,7 +6,7 @@
// Created: 07/05/98
// RCS-ID: $Id$
// Copyright: (c) Denis Pershin
// Licence: wxWindows licence
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
@ -110,7 +110,7 @@ bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
y = 0;
m_needParent = TRUE;
printf("precreate\n");
PreCreation( parent, id, pos, size, style, name );
@ -135,14 +135,14 @@ printf("5\n");
printf("Robert's new insertion code :-)\n");
m_parent->AddChild( this );
(m_parent->m_insertCallback)( m_parent, this );
printf("postcreate\n");
PostCreation();
gtk_widget_realize(GTK_WIDGET(m_tree));
Show(TRUE);
return TRUE;
}
@ -348,6 +348,16 @@ wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) co
return GTK_TREE_ITEM(g_list_nth(GTK_TREE(parent)->children, cookie)->data);
}
wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
{
GtkTreeItem *p = (GtkTreeItem *)item;
GtkWidget *parent = GTK_WIDGET(p)->parent;
wxCHECK_MSG( GTK_IS_TREE(parent), NULL, "invalid tree item" );
return GTK_TREE_ITEM(g_list_last(GTK_TREE(parent)->children)->data);
}
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const {
GtkTreeItem *p = (GtkTreeItem *)item;
GtkWidget *parent = GTK_WIDGET(p)->parent;
@ -469,13 +479,13 @@ wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
const wxString& text, int image, int selectedImage,
wxTreeItemData *data) {
wxTreeItemData *data) {
return p_InsertItem(parent, text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::p_InsertItem(GtkTreeItem *p,
const wxString& text, int image, int selectedImage,
wxTreeItemData *data) {
wxTreeItemData *data) {
GtkTreeItem *item;
printf("begin insert\n");
@ -719,7 +729,7 @@ long wxTreeCtrl::GetChild(long item) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}
@ -732,7 +742,7 @@ long wxTreeCtrl::GetFirstVisibleItem(void) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}
@ -747,7 +757,7 @@ long wxTreeCtrl::GetNextVisibleItem(long item) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}

View File

@ -6,7 +6,7 @@
// Created: 07/05/98
// RCS-ID: $Id$
// Copyright: (c) Denis Pershin
// Licence: wxWindows licence
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
@ -110,7 +110,7 @@ bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
y = 0;
m_needParent = TRUE;
printf("precreate\n");
PreCreation( parent, id, pos, size, style, name );
@ -135,14 +135,14 @@ printf("5\n");
printf("Robert's new insertion code :-)\n");
m_parent->AddChild( this );
(m_parent->m_insertCallback)( m_parent, this );
printf("postcreate\n");
PostCreation();
gtk_widget_realize(GTK_WIDGET(m_tree));
Show(TRUE);
return TRUE;
}
@ -348,6 +348,16 @@ wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) co
return GTK_TREE_ITEM(g_list_nth(GTK_TREE(parent)->children, cookie)->data);
}
wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
{
GtkTreeItem *p = (GtkTreeItem *)item;
GtkWidget *parent = GTK_WIDGET(p)->parent;
wxCHECK_MSG( GTK_IS_TREE(parent), NULL, "invalid tree item" );
return GTK_TREE_ITEM(g_list_last(GTK_TREE(parent)->children)->data);
}
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const {
GtkTreeItem *p = (GtkTreeItem *)item;
GtkWidget *parent = GTK_WIDGET(p)->parent;
@ -469,13 +479,13 @@ wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
const wxString& text, int image, int selectedImage,
wxTreeItemData *data) {
wxTreeItemData *data) {
return p_InsertItem(parent, text, image, selectedImage, data);
}
wxTreeItemId wxTreeCtrl::p_InsertItem(GtkTreeItem *p,
const wxString& text, int image, int selectedImage,
wxTreeItemData *data) {
wxTreeItemData *data) {
GtkTreeItem *item;
printf("begin insert\n");
@ -719,7 +729,7 @@ long wxTreeCtrl::GetChild(long item) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}
@ -732,7 +742,7 @@ long wxTreeCtrl::GetFirstVisibleItem(void) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}
@ -747,7 +757,7 @@ long wxTreeCtrl::GetNextVisibleItem(long item) const {
if (next != NULL)
return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
return (-1);
}

View File

@ -457,6 +457,22 @@ wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
return l;
}
wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
{
// can this be done more efficiently?
long cookie;
wxTreeItemId childLast,
child = GetFirstChild(last, cookie);
while ( child.IsOk() )
{
childLast = child;
child = GetNextChild(last, cookie);
}
return childLast;
}
wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
{
return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));