Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » TreeCtrl » Restrict drag&drop to one level
Restrict drag&drop to one level [message #25209] Sun, 14 February 2010 11:14 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hello!

I've got a little problem here. I want to have a TreeCtrl with drag and drop implemented in such a way, that items from one level can not be inserted into different level. An example to illustrate:
+ Graphs
  + Graph1
  | + Data1
  | + Data2
  + Graph2
    + Data3
I need to allow user to drag any Data into any Graph and disable dropping it into any other level. Also, any Graph must not be dropped anywhere else then top level.

I think this is quite common situation, so there should be easy solution. I'm probably just missing something Smile Bellow is the code I got so far, but it has two problems. First, it doesn't work properly with MultiSelect(). Second, it paints insert marks even in positions where drop is not allowed, which might confuse user. I hope someone can give me some hint how to solve this. The code:
#include "CtrlLib/CtrlLib.h"
using namespace Upp;

struct App : TopWindow {
	typedef App CLASSNAME;
	TreeCtrl   tree;

	void DropInsert(int parent, int ii, PasteClip& d){
		if(AcceptInternal<TreeCtrl>(d, "graph")) {
			if(GetLevel(parent)!=0) {d.Reject();return;}
			tree.InsertDrop(parent, ii, d);
			tree.SetFocus();
			LOG("accepted graph");
			return;
		}
		if(AcceptInternal<TreeCtrl>(d, "data")) {
			if(GetLevel(parent)!=1) {d.Reject();return;}
			tree.InsertDrop(parent, ii, d);
			tree.SetFocus();
			LOG("Accepted series");
			return;
		}
	}

	void Drag()	{
		int type=GetLevel(tree.GetCursor());
		switch(type) {
		case 1:
			if(tree.DoDragAndDrop(InternalClip(tree, "graph"),tree.GetDragSample()) == DND_MOVE)
				tree.RemoveSelection();
			break;
		case 2:
			if(tree.DoDragAndDrop(InternalClip(tree, "data"),tree.GetDragSample()) == DND_MOVE)
				tree.RemoveSelection();
			break;
		}
	}
	
	int GetLevel(int id){
		int i=0;
		while(id!=0){
			id=tree.GetParent(id);
			i++;
		}
		return i;
	}
	
	App() {
		Add(tree.SizePos());
		tree.SetRoot(Image(), "Graphs");
		for(int i=1;i<3;i++){
			int id=tree.Add(0,Image(),"Graph "+AsString(i));
			for(int j=1;j<3;j++)
				tree.Add(id,Image(),"Data "+AsString(i)+"/"+AsString(j));
		}
		tree.OpenDeep(0);
		tree.WhenDropInsert = THISBACK(DropInsert);
		tree.WhenDrag = THISBACK(Drag);
		tree.MultiSelect();
		Sizeable();
	}
};

GUI_APP_MAIN
{
	App().Run();
}


Best regards,
Honza
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Distorted GUI / memory leak
Next Topic: Suppress drawing the root 0 node
Goto Forum:
  


Current Time: Fri Apr 26 22:56:36 CEST 2024

Total time taken to generate the page: 0.07195 seconds