Click here to Skip to main content
15,300,338 members
Articles / Desktop Programming / MFC
Article
Posted 1 Feb 2005

Stats

69.9K views
2.3K downloads
27 bookmarked

Tree Editor With Toolbar

Rate me:
Please Sign up or sign in to vote.
4.57/5 (6 votes)
1 Feb 20051 min read
A tree editor control with toolbar, that can be edited conveniently.

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

Share

About the Author

skybirdcao
Web Developer
China China
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.

Comments and Discussions

 
GeneralSolution to the big bug Pin
Jammni+2-Jul-07 4:26
MemberJammni+2-Jul-07 4:26 
GeneralRe: Solution to the big bug Pin
Bigsteve8714-Aug-08 9:21
MemberBigsteve8714-Aug-08 9:21 
Awesome Jammni! That would have been a deal breaker. I bumbled around a bit before I got it right. Here's what I added incase anyone is having trouble getting this working.

In ToolbarEx.h add:

public:
afx_msg void OnWinIniChange(LPCTSTR lpszSection);

In ToolbarEx.cpp add:

ON_WM_WININICHANGE()

to the message map declarations, and the function:

void CToolbarEx::OnWinIniChange(LPCTSTR lpszSection)
{
// We need to prevent this message from being handled
//CWnd::OnWinIniChange(lpszSection);
}

Stephen
GeneralThe big bug as you mentioned Pin
zhou_wz18-Jan-07 23:48
Memberzhou_wz18-Jan-07 23:48 
GeneralBugs Pin
Big Art23-Feb-05 15:51
MemberBig Art23-Feb-05 15:51 
GeneralRe: Bugs Pin
skybirdcao26-Feb-05 20:30
Memberskybirdcao26-Feb-05 20:30 
GeneralFix for Move Left Pin
Brad Bruce2-Feb-05 7:55
MemberBrad Bruce2-Feb-05 7:55 
GeneralNote: it also supports Drag & Drop Pin
skybirdcao2-Feb-05 0:14
Memberskybirdcao2-Feb-05 0:14 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.