Gtk.jl icon indicating copy to clipboard operation
Gtk.jl copied to clipboard

setindex notation for replacing elements

Open BeastyBlacksmith opened this issue 3 years ago • 6 comments

It would be nice to be able to write

builder["button"] = GtkButton("The new button")

BeastyBlacksmith avatar Jun 30 '21 15:06 BeastyBlacksmith

I'm not very familiar with GtkBuilder, is that something that is supported ? I only had a quick look but didn't found much :

https://developer.gnome.org/gtk3/stable/GtkBuilder.html

jonathanBieler avatar Jul 01 '21 07:07 jonathanBieler

well you can do it manually, I did something like

builder = GtkBuilder(file)
button_parent = builder["parentID"]
destroy(button_parent[1]) # or other index of button
push!(button_parent, GtkButton("The new button")

BeastyBlacksmith avatar Jul 01 '21 11:07 BeastyBlacksmith

what is builder["parentID"]? A container? In particular it would be interesting what button_parent[1] actually is.

tknopp avatar Jul 01 '21 11:07 tknopp

In my case builder["parentID"] is a GtkScrolledWindow, but in principle could be any container and button_parent[1] would be the GtkButton that I want to replace. button_parent[1] would be the same as builder["button"], but one could also loop through the container and check the ids for "button" if the element doesn't know its index in the parent container.

BeastyBlacksmith avatar Jul 01 '21 12:07 BeastyBlacksmith

As I understand you're modifying the container here (the GtkScrolledWindow) and not the builder itself. Essentially you're doing :

x = [zeros(2)]
x[1][1] = 1

Which is different from x[1] = 1.

From the GtkBuilder doc it seems to me it's meant to an immutable object and you'd rather have to modify the input file than the object.

That said it could make sense to define setindex for array-like containers (e.g. GtkNotebook).

jonathanBieler avatar Jul 01 '21 13:07 jonathanBieler

That said it could make sense to define setindex for array-like containers (e.g. GtkNotebook). I think that would also make sense.

Then one could write builder["parentID"][1] = GtkButton(), though kind of my point was, that finding out who is the parent, could be done automatically and make things a great deal easier, since builder["ID"] = element would mean: delete the element known by this name and replace it with this new element under the same name.

BeastyBlacksmith avatar Jul 01 '21 16:07 BeastyBlacksmith