garetxe on master
haskell-gi-base 0.26.3 haskell-gi-0.26 Fix for nullable parameter in r… (compare)
Is there an example project or howto how to use GTK4 + haskell-gi and stack?
Unfortunately nothing that I am aware of. It should be possible to do by adding enough packages to your stack.yaml
, there is some relevant discussion in haskell-gi/haskell-gi#339
cabal
before going into that. At least for building haskell-gi
generated bindings it is often easier with cabal
.
How would you translate
new Gtk.Window [ On #destroy Gtk.mainQuit ]
`
to code that doesn't use labels?
new
(from GI.Gtk) without specifying the bins type, i don't seem to be able to
myNew = new
(with GI.Gtk in scope)
• Overlapping instances for GI.Constructible a0 tag0
arising from a use of ‘GI.new’
Matching instances:
instance [overlappable] (GI.GObject a, tag ~ 'GI.AttrConstruct) =>
GI.Constructible a tag
-- Defined in ‘Data.GI.Base.Constructible’
instance (tag ~ 'GI.AttrSet) =>
GI.Constructible G.AccelGroupEntry tag
-- Defined in ‘GI.Gtk.Structs.AccelGroupEntry’
instance (tag ~ 'GI.AttrSet) => GI.Constructible G.AccelKey tag
-- Defined in ‘GI.Gtk.Structs.AccelKey’
...plus 35 others
...plus 95 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
(The choice depends on the instantiation of ‘a0, tag0’
To pick the first instance above, use IncoherentInstances
when compiling the other instance declarations)
• In the expression: GI.new
In an equation for ‘myNew’: myNew = GI.new
|
97 | myNew = G.new
| ^^^^^
boxPackStart box widget expand fill padding
widgetDestroy
which appears to trigger the crash.
I'm then destroying the widget with
widgetDestroy
which appears to trigger the crash.
This sounds a bit like you are double freeing. Is it necessary to call widgetDestroy
in your code? Every widget in Gtk is reference counted. In particular if you create a widget and add it to a box then it will have a refcount of 2: Haskell keeps one (which it will drop once the Haskell object is garbage collected) and the container keeps another.
(By the way, when debugging crashes it is useful to run with the following environment vars set:
export G_SLICE="debug-blocks"
export MALLOC_CHECK_=2
export MALLOC_PERTURB_=$(($RANDOM % 255 + 1))
export HASKELL_GI_DEBUG_MEM=1
)
And another question: how would I replicate https://hackage.haskell.org/package/gi-gtk-4.0.5/docs/GI-Gtk-Objects-Scale.html#v:scaleNew with https://hackage.haskell.org/package/haskell-gi-base-0.26.0/docs/Data-GI-Base-Constructible.html#v:new , since it has some additional parameters ?
In general it should be perfectly fine to call scaleNew
, but in this case it might be possible to make it work with new
too. Have you tried setting the orientation
and adjustment
attributes? Something like
scale <- new Gtk.Scale [#orientation := ..., #adjustment := ...]
data HPackBox = HPackBox
instance (IsDescendantOf Box a, IsDescendantOf Widget b,
Constructible b tag, MonadIO m, GObject a, GObject b,
input ~ ((a, Bool, Bool, Word32), (ManagedPtr b -> b, [AttrOp b tag])), output ~ m b) =>
ApplyAB HPackBox input output where
applyAB _ ((box, expand, fill, padding), (ctr, attrs)) = do
widget <- new ctr attrs
boxPackStart box widget expand fill padding
return widget
data HDestroyWidget = HDestroyWidget
instance (IsWidget o) => ApplyAB HDestroyWidget o (IO ()) where
applyAB _ widget = widgetDestroy widget
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Switch, []) .*. -- crashes ?
(Scale, []) .*.
HNil)
So my question is if every newX method can be replicated with new and attributes.
Ah, I see, thanks for the explanation. (Cool approach!) I hesitate to say that all widgets can be created in this way (I cannot think of an exception now, but I'm not sure), but this certainly seems to be the direction of travel.
containerRemove
doesn't cause the crash.
Yes, this is definitely safer. By doing this you are telling the container to drop its reference to the widget, but you are not invalidating it, so the widget is not becoming invalid "behind Haskell's back", so to speak
So my question is if every newX method can be replicated with new and attributes.
Ah, I see, thanks for the explanation. (Cool approach!) I hesitate to say that all widgets can be created in this way (I cannot think of an exception now, but I'm not sure), but this certainly seems to be the direction of travel.
I tried the following and it worked !
adjustment <- new Adjustment [#value := 0.0, #lower := 0.0, #upper := 51, #stepIncrement := 0.1, #pageIncrement := 1.0, #pageSize := 1.0]
scale <- new Scale [ #orientation := OrientationVertical, #adjustment := adjustment]
So that means that every "xNew" method has corresponding attributes that are generated automatically with the same name as the name given to the function "xNew"'s arguments in the documentation ?
I tried the following and it worked !
Great :)
So that means that every "xNew" method has corresponding attributes that are generated automatically with the same name as the name given to the function "xNew"'s arguments in the documentation ?
It is not automatic, unfortunately, the writer of the C code has to make sure that the relevant arguments are exposes as GObject properties. But in practice, in Gtk at least, it seems to almost always be the case, and all the different constructors are just convenience wrappers over g_object_new
, setting the properties. Something like what you see in gtk_button_new_with_label
.
new
with attributes in haskell-gi
.