Skip to content

Commit

Permalink
convenience constructor for GtkSignalListItemFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Sep 4, 2023
1 parent b0a8aa9 commit 9f4ae65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 2 additions & 4 deletions examples/filteredlistview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ push!(win, box)

modelValues = string.(names(Gtk4))
model = GtkStringList(modelValues)
factory = GtkSignalListItemFactory()

function setup_cb(f, li)
set_child(li,GtkLabel(""))
Expand All @@ -22,6 +21,8 @@ function bind_cb(f, li)
label.label = text
end

factory = GtkSignalListItemFactory(setup_cb, bind_cb)

function match(list_item, user_data)
itemLeaf = Gtk4.GLib.find_leaf_type(list_item)
item = convert(itemLeaf, list_item)
Expand All @@ -35,9 +36,6 @@ filteredModel = GtkFilterListModel(GLib.GListModel(model), filter)
list = GtkListView(GtkSelectionModel(GtkSingleSelection(GLib.GListModel(filteredModel))), factory)
list.vexpand = true

signal_connect(setup_cb, factory, "setup")
signal_connect(bind_cb, factory, "bind")

signal_connect(entry, :search_changed) do w
@idle_add Gtk4.G_.changed(filter, Gtk4.FilterChange_DIFFERENT)
end
Expand Down
5 changes: 1 addition & 4 deletions examples/listview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sw = GtkScrolledWindow()
push!(win, sw)

model = GtkStringList(string.(names(Gtk4)))
factory = GtkSignalListItemFactory()

function setup_cb(f, li)
set_child(li,GtkLabel(""))
Expand All @@ -17,9 +16,7 @@ function bind_cb(f, li)
label.label = text
end

factory = GtkSignalListItemFactory(setup_cb, bind_cb)
list = GtkListView(GtkSelectionModel(GtkSingleSelection(GLib.GListModel(model))), factory)

signal_connect(setup_cb, factory, "setup")
signal_connect(bind_cb, factory, "bind")

sw[] = list
18 changes: 16 additions & 2 deletions src/lists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ GtkGridView(model=nothing; kwargs...) = GtkGridView(model, nothing; kwargs...)

GtkColumnView(; kwargs...) = GtkColumnView(nothing; kwargs...)
GtkColumnViewColumn(title=""; kwargs...) = GtkColumnViewColumn(title, nothing; kwargs...)
push!(cv::GtkColumnView, cvc::GtkColumnViewColumn) = (G_.append_column(cv,cvc); cv)

getindex(li::GtkListItem) = G_.get_item(li)
set_child(li::GtkListItem, w) = G_.set_child(li, w)
Expand All @@ -69,11 +70,24 @@ get_child(te::GtkTreeExpander) = G_.get_child(te)
get_item(trl::GtkTreeListRow) = G_.get_item(trl)

function GtkTreeListModel(root::GListModel, passthrough, autoexpand, create_func)
create_cfunc = @cfunction($create_func, Ptr{GObject}, (Ptr{GObject}, Ptr{Nothing}))
ret = ccall(("gtk_tree_list_model_new", libgtk4), Ptr{GObject}, (Ptr{GObject}, Cint, Cint, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), root, passthrough, autoexpand, create_cfunc, C_NULL, C_NULL)
create_cfunc = @cfunction(GtkTreeListModelCreateModelFunc, Ptr{GObject}, (Ptr{GObject},Ref{Function}))
ref, deref = GLib.gc_ref_closure(create_func)
ret = ccall(("gtk_tree_list_model_new", libgtk4), Ptr{GObject}, (Ptr{GObject}, Cint, Cint, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), root, passthrough, autoexpand, create_cfunc, ref, deref)
convert(GtkTreeListModel, ret, true)
end

"""
GtkSignalListItemFactory(setup_cb, bind_cb)
Create a `GtkSignalListItemFactory` and immediately connect "setup" and "bind" callback functions `setup_cb` and `bind_cb`, respectively.
"""
function GtkSignalListItemFactory(@nospecialize(setup_cb::Function), @nospecialize(bind_cb::Function))
factory = GtkSignalListItemFactory()
signal_connect(setup_cb, factory, "setup")
signal_connect(bind_cb, factory, "bind")
factory
end

## GtkListBox
setindex!(lb::GtkListBox, w::GtkWidget, i::Integer) = (G_.insert(lb, w, i - 1); lb[i])
getindex(lb::GtkListBox, i::Integer) = G_.get_row_at_index(lb, i - 1)
Expand Down

0 comments on commit 9f4ae65

Please sign in to comment.