www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GTKD TreeView - Delete TreeView SubItem with Button?

reply TheDGuy <loder.feuer googlemail.com> writes:
Hello,

is it possible, to delete a selected TreeView SubItem with a 
Button-Click?

My Code currently looks like this:

import gtk.MainWindow;
import gtk.Box;
import gtk.Main;

import gtk.Button;
import gdk.Event;
import gtk.Widget;

import List, Languages;

void main(string[] args){
	Main.init(args);
	MainWindow win = new MainWindow("TreeView Example");
	win.setDefaultSize(500,300);

	Box box = new Box(Orientation.VERTICAL, 0);

	auto list =  new List();
	auto german = list.addCountry("German", "Germany");
	auto city = list.addCountryCity(german, "German", "Munich");

	auto sweden = list.addCountry("Swedish", "Sweden");
	auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm");

	auto treeView = new LanguageTree(list);
	box.packStart(treeView, true, true, 0);

	auto btn_Delete = new Button("Delete");
	box.add(btn_Delete);

	win.add(box);
	win.showAll();
	Main.run();
}

class DeleteButton : Button
{
	this(in string text)
	{
		super(text);
		addOnButtonRelease(&del);
	}
	private bool del(Event event, Widget widget)
	{
		return true;
	}

}

But i neither know how i get the currently selected item nor how 
i could delete it within the "del"-Event of the "DeleteButton"?
Nov 20 2015
parent reply Michael Robertson <mlizard32 yahoo.com> writes:
On Friday, 20 November 2015 at 17:09:50 UTC, TheDGuy wrote:
 Hello,

 is it possible, to delete a selected TreeView SubItem with a 
 Button-Click?

 My Code currently looks like this:

 import gtk.MainWindow;
 import gtk.Box;
 import gtk.Main;

 import gtk.Button;
 import gdk.Event;
 import gtk.Widget;

 import List, Languages;

 void main(string[] args){
 	Main.init(args);
 	MainWindow win = new MainWindow("TreeView Example");
 	win.setDefaultSize(500,300);

 	Box box = new Box(Orientation.VERTICAL, 0);

 	auto list =  new List();
 	auto german = list.addCountry("German", "Germany");
 	auto city = list.addCountryCity(german, "German", "Munich");

 	auto sweden = list.addCountry("Swedish", "Sweden");
 	auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm");

 	auto treeView = new LanguageTree(list);
 	box.packStart(treeView, true, true, 0);

 	auto btn_Delete = new Button("Delete");
 	box.add(btn_Delete);

 	win.add(box);
 	win.showAll();
 	Main.run();
 }

 class DeleteButton : Button
 {
 	this(in string text)
 	{
 		super(text);
 		addOnButtonRelease(&del);
 	}
 	private bool del(Event event, Widget widget)
 	{
 		return true;
 	}

 }

 But i neither know how i get the currently selected item nor 
 how i could delete it within the "del"-Event of the 
 "DeleteButton"?
TreeIter iter = treeView.getSelectedIter; then you can use the TreeIter to delete the item. If your List is a ListStore or TreeStore, it has a remove method that you can call. If its a custom store you will have to implement it yourself like this class List:TreeModel { ..... void remove(TreeIter iter) { //maybe remove item from your actual data structure or other processing //send rowDeleted signal rowDeleted(iter); } ..... }
Nov 20 2015
parent reply TheDGuy <loder.feuer googlemail.com> writes:
Thanks for your reply,

now it gets more clear for me but how am i able to access the 
TreeView within the CustomButton-Class? If i declare TreeView and 
TreeStore public i get "static variable list cannot be read at 
compile time"?

import gtk.MainWindow;
import gtk.Box;
import gtk.Main;

import gtk.Button;
import gdk.Event;
import gtk.Widget;

import List, Languages;

auto list = new List();
auto treeView = new LanguageTree(list);
void main(string[] args){
	Main.init(args);
	MainWindow win = new MainWindow("TreeView Example");
	win.setDefaultSize(500,300);

	Box box = new Box(Orientation.VERTICAL, 0);

	auto german = list.addCountry("German", "Germany");
	auto city = list.addCountryCity(german, "German", "Munich");

	auto sweden = list.addCountry("Swedish", "Sweden");
	auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm");

	//auto treeView = new LanguageTree(list);
	box.packStart(treeView, true, true, 0);

	auto btn_Delete = new Button("Delete");
	box.add(btn_Delete);

	win.add(box);
	win.showAll();
	Main.run();
}

class DeleteButton : Button
{
	this(in string text)
	{
		super(text);
		addOnButtonRelease(&del);
	}
	private bool del(Event event, Widget widget)
	{
		list.remove(treeView.getSelectedIter);
		return true;
	}

}
Nov 20 2015
parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 20 Nov 2015 18:57:10 +0000, TheDGuy wrote:

 Thanks for your reply,
 
 now it gets more clear for me but how am i able to access the TreeView
 within the CustomButton-Class? If i declare TreeView and TreeStore
 public i get "static variable list cannot be read at compile time"?
 
 auto list = new List();
 auto treeView = new LanguageTree(list);
You need to declare them at the module scope but initialize them inside your main function, so: List list; LanguageTree treeView; then inside your main function: list = new List(); treeView = new LanguageTree(list);
Nov 20 2015
parent reply TheDGuy <loder.feuer googlemail.com> writes:
Thanks for your reply,

is it also possible to do it like this?

import gtk.MainWindow;
import gtk.Box;
import gtk.Main;

import gtk.Button;
import gdk.Event;
import gtk.Widget;

import List, Languages;

void main(string[] args){
	Main.init(args);
	MainWindow win = new MainWindow("TreeView Example");
	win.setDefaultSize(500,300);

	Box box = new Box(Orientation.VERTICAL, 0);

	auto list = new List();
	auto german = list.addCountry("German", "Germany");
	auto city = list.addCountryCity(german, "German", "Munich");

	auto sweden = list.addCountry("Swedish", "Sweden");
	auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm");

	auto treeView = new LanguageTree(list);

	auto btn_Delete = new DeleteButton("Delete", treeView, list);
	box.packStart(treeView, true, true, 0);
	box.add(btn_Delete);

	win.add(box);
	win.showAll();
	Main.run();
}

class DeleteButton : Button
{
	private LanguageTree mLT;
	private List mTStore;
	this(in string text, LanguageTree lT, List tStore){
		super(text);
		this.mLT = lT;
		this.mTStore = tStore;
		addOnButtonRelease(&del);
	}
	private bool del(Event event, Widget widget){

		mTStore.remove(mLT.getSelectedIter);
		return true;
	}
}

and in my TreeStore-Class:

	public void remove(TreeIter iter)
	{
		rowDeleted(iter);
	}

but now i get the error:

"rowDeleted is not callable using argument types (TreeIter)"?
Nov 20 2015
parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 20 Nov 2015 19:34:01 +0000, TheDGuy wrote:

 Thanks for your reply,
 
 is it also possible to do it like this?
 
Yeah, that'll work.
 but now i get the error:
 
 "rowDeleted is not callable using argument types (TreeIter)"?
rowDeleted is just to emit the signal that a row has been removed, not actually remove it. You probably want to use the ListStore's remove method.
Nov 20 2015
parent reply TheDGuy <loder.feuer googlemail.com> writes:
So like this?

	public void remove(TreeIter iter)
	{
		this.remove(iter);
	}

If i do this, i get a stackoverflow error :(
Nov 20 2015
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 20 Nov 2015 19:45:25 +0000, TheDGuy wrote:

 So like this?
 
 	public void remove(TreeIter iter)
 	{
 		this.remove(iter);
 	}
 
 If i do this, i get a stackoverflow error :(
That's creating an infinite recursion as the method simply calls itself. Assuming the class inherits from ListStore, you can just erase the remove method entirely and let the base class handle it. If you want to do something extra, use: override void remove(TreeIter iter) { // Anything custom super.remove(iter); // Anything custom }
Nov 20 2015
parent TheDGuy <loder.feuer googlemail.com> writes:
Ahh, okay. Thank you very much for your help, now everything 
works fine :)
Nov 20 2015
prev sibling parent Michael Robertson <mlizard32 yahoo.com> writes:
On Friday, 20 November 2015 at 19:45:26 UTC, TheDGuy wrote:
 So like this?

 	public void remove(TreeIter iter)
 	{
 		this.remove(iter);
 	}

 If i do this, i get a stackoverflow error :(
Your List inherits from TreeStore correct? just get rid of your custom remove method entirely, or correctly override it public override void remove(TreeIter iter) { super.remove(iter); }
Nov 20 2015