Sign up

Comparing whether widgets are the same

I am having a problem trying to decide if a Widget instance in a Box is the same as a Widget instance I have a reference to. Rust allows this with expressions such as:

if control_window.main_box.get_children()[0] == control_window.label {

I tried:

if (mainBox.getChildren().first() == label) {

but this is failing. There appear to be no examples of getChildren use in the GtkD demos. Trawling the Web let me to https://github.com/gtkd-developers/GtkD/issues/138 where Gerald ended up asking:

Just out of curiosity, if you want to know if two widgets are equal (i.e wrap the same GTK3 widget), is the best practice to compare the pointer returned from getWidgetStruct or getStruct? I assume if the pointers are the same the GTK3 widget is the same even if different D objects are wrapping them?

I guess I should find out what Gerald ended up doing as I think the problem I have is exactly the one he had.

Re: Comparing whether widgets are the same

On Tue, 26 Jun 2018 11:24:10 GMT, Russel Winder wrote:

I guess I should find out what Gerald ended up doing as I think the problem I have is exactly the one he had.

Here is the function I am using for widget equality:

https://github.com/gnunn1/tilix/blob/master/source/gx/gtk/util.d#L271

I wonder if it would make sense for gobject.ObjectG to implement opEquals in GtkD?

Re: Comparing whether widgets are the same

One more thing, I also use this template quite a bit to return all children of a specific type since Tilix depends heavily on using Paned (i.e. splitters) and because of the way it works everything gets nested in multiple levels (Terminal within Paned within Paned within Paned).

https://github.com/gnunn1/tilix/blob/master/source/gx/gtk/util.d#L177

Re: Comparing whether widgets are the same

By checking addresses, it would appear that:

if (mainBox.getChildren().first().data() is label.getLabelStruct()) {

does the right thing. But I am worried I am missing something about what is actually being tested here.

Re: Comparing whether widgets are the same

On 26-06-18 14:45, Gerald Nunn wrote:

On Tue, 26 Jun 2018 11:24:10 GMT, Russel Winder wrote:

I guess I should find out what Gerald ended up doing as I think the problem I have is exactly the one he had.

Here is the function I am using for widget equality:

https://github.com/gnunn1/tilix/blob/master/source/gx/gtk/util.d#L271

I wonder if it would make sense for gobject.ObjectG to implement opEquals in GtkD?

That might be a good addition.

GtkD will try to return the existing D object if it exists. But there
are some cases where it doesn't.

Or a version of getChildren that returns a Array of Widgets.

Re: Comparing whether widgets are the same

On 26-06-18 14:54, Russel Winder wrote:

By checking addresses, it would appear that:

if (mainBox.getChildren().first().data() is label.getLabelStruct()) {

does the right thing. But I am worried I am missing something about what is actually being tested here.

getChildren return a linked list, the data property of the elements will
hold a reference to the child widget.

getLabelStruct returns the reference GtkD holds on the widget.

Both whould be a GtkLabel* in this case.