Click here to Skip to main content
Click here to Skip to main content

Tree Editor With Toolbar

By , 1 Feb 2005
 

Introduction

Months ago, I needed a tree editor that could be used conveniently, and I couldn't find any in the Internet, so I did it by myself. I got some code from the article "Tree Editor" by Yossi Patt. Thanks very much!

I added a new class, used for the toolbar to operate the tree editor: CToolbarEx, which is not created originally by me. I'm sorry I forget who created it originally. And I just added some new functions which are used to respond to the toolbar's commands.

Implementation

In ToolbarEx.cpp, you need to add you toolbar's ID.

static const UINT toolbarItems[] =
{
    ID_ADD_SIBLING,                  
    ID_INSERT_CHILD,                 
    ID_EDIT_NAME,                    
    ID_DELETE_ITEM,

    ID_CUT_ITEM,
    ID_COPY_ITEM,
    ID_PASTE_ITEM,

    ID_UP_ITEM,
    ID_DOWN_ITEM,
    ID_LEFT_ITEM,
    ID_RIGHT_ITEM
};


BEGIN_MESSAGE_MAP(CPropToolbar, CToolBarCtrl)
    ON_NOTIFY_RANGE( TTN_NEEDTEXTA, ID_LEFT_ITEM, ID_DOWN_ITEM, OnNeedTextA)
    ON_NOTIFY_RANGE( TTN_NEEDTEXTW, ID_LEFT_ITEM, ID_DOWN_ITEM, OnNeedTextW)
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

The function OnNeedTextA respond to non-Unicode, and OnNeedTextW to Unicode. Of course, you can combine them to one function. The work is left to those who are interested in UNICODE.

In CTreeEditor class, I added a message map to deal with the toolbar's commands:

    ON_COMMAND(ID_ADD_SIBLING, OnAddSibling)
    ON_COMMAND(ID_INSERT_CHILD, OnInsertChild)
    ON_COMMAND(ID_EDIT_NAME, OnRename)
    ON_COMMAND(ID_DELETE_ITEM, OnDeleteItem)

    ON_COMMAND(ID_CUT_ITEM, OnCutItem)
    ON_COMMAND(ID_COPY_ITEM, OnCopyItem)
    ON_COMMAND(ID_PASTE_ITEM, OnPasteItem)

    ON_COMMAND(ID_UP_ITEM, OnUpItem)
    ON_COMMAND(ID_DOWN_ITEM, OnDownItem)
    ON_COMMAND(ID_LEFT_ITEM, OnLeftItem)
    ON_COMMAND(ID_RIGHT_ITEM, OnRightItem)

I think you can get the detailed implementation from my source code. Maybe my code is short of comments, but tidy enough so you can get it.

A Big Bug

Yell, as in my last article "A Property Table Editor", I found the same big bug. Sometimes when you are operating it, or when you minimize it, and then wait for some time (maybe half an hour), you will find that the toolbar has changed as the second picture shows, to be wider.

I don't know what causes that. Maybe someone can tell me or solve it. I will really appreciate him/her very much! You can have a try!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

skybirdcao
Web Developer
China China
Member
A programmer graduated 2003, and worked for one year. Program with C/C++, sometimes use perl, or java, etc. I like CodeProject website, and want to make more friends here. I'm dreaming to beacome a guy in programming.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSolution to the big bug PinmemberJammni+2 Jul '07 - 4:26 
I had the same problem some months ago. You can reproduce the problem (without waiting half an our) when you open the display properties (right click windows desktop...), change a property and then confirm that. In your application you will receive a WM_WININICHANGE event. If tell windows that you handle this event then the resize won't happen.
I had no time to debug the problem in all details so i can't tell you why this helps Wink | ;-)
 
Jammni
GeneralRe: Solution to the big bug PinmemberBigsteve8714 Aug '08 - 9:21 
GeneralThe big bug as you mentioned Pinmemberzhou_wz18 Jan '07 - 23:48 
Dear skybirdcao,
Did you fixed the bug that the abnormal expanding button size?
GeneralBugs PinmemberArt Friesz23 Feb '05 - 15:51 
Thank you for taking the time to provide your code and a short tutorial. In general I am pleased with the behavior of the control. However in incorporating it into my test app I have discovered the following problems:
 
I have 4 folders in the root. Any of these folders is allowed to receive drag and drop files from the root and from the children of the other folders. Therefore the TVINSERTSTRUCT parameters are as follows:

itemex.cChildren = 1; for folders in the root.
itemex.cChildren = 0; for root files and children of the root folders.
 
So for example: A file in folder 3 is allowed to be dragged into folder 2 but is not allowed be made a child of a child in folder 2.
 
The following incorrect behavior has been observed.
 
1) When dropping a file to a folder the icon for the folder changes to index zero of my image list. When I inserted this folder into the tree originally neither the "itemex.iImage" nor the "itemex.iSelectedImage" values were set to imageList index 0.
 

2) If moving a file from one location to another, it disappears unless you drag it onto the folder itself.
 
Thanks for the opportunity to test and learn from you code.
 
Art
GeneralRe: Bugs Pinmemberskybirdcao26 Feb '05 - 20:30 
GeneralFix for Move Left PinmemberBrad Bruce2 Feb '05 - 7:55 
The move left function actually makes the item go above the existing parent. The fix is to change from
hItem = LoadTreeData(hGrandfatherItem, hPrevParentItem);
to
hItem = LoadTreeData(hGrandfatherItem, hParentItem);
in void CTreeCtrlEx::OnMoveLeftItem()
 
Other than that it looks great. Now to track down the toolbar issue (I haven't seen it happen yet)
GeneralNote: it also supports Drag & Drop Pinmemberskybirdcao2 Feb '05 - 0:14 
you can drag any node and drop it to other's node, except it's children's node.
 
skybird

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 2 Feb 2005
Article Copyright 2005 by skybirdcao
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid