Skip to content

Commit

Permalink
Add GtkListBox wrapper and example (with filtering) (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
nHackel authored Aug 19, 2023
1 parent db757b0 commit c9146c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/listbox.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Gtk4

win = GtkWindow("ListBox demo with filter")
box = GtkBox(:v)
entry = GtkSearchEntry()
sw = GtkScrolledWindow()
push!(box, entry)
push!(box, sw)
push!(win, box)

listBox = GtkListBox()
foreach(x-> push!(listBox, GtkLabel(x)), string.(names(Gtk4)))

function match(ptr, _)
row = convert(GtkListBoxRowLeaf, ptr)
label = row.child
result = startswith(label.label, entry.text)
return result ? Cint(1) : Cint(0)
end

Gtk4.set_filter_func(listBox, (row, data) -> match(row, data))

signal_connect(entry, :search_changed) do w
@idle_add Gtk4.G_.invalidate_filter(listBox)
end

sw[] = listBox
listBox.vexpand = true
19 changes: 19 additions & 0 deletions src/lists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ function GtkTreeListModel(root::GListModel, passthrough, autoexpand, create_func
convert(GtkTreeListModel, ret, true)
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)

push!(lb::GtkListBox, w::GtkWidget) = (G_.append(lb, w); lb)
pushfirst!(lb::GtkListBox, w::GtkWidget) = (G_.prepend(lb, w); lb)
insert!(lb::GtkListBox, i::Integer, w::GtkWidget) = (G_.insert(lb, w, i - 1); lb)

#empty!(lb::GtkListBox) = (ccall(("gtk_list_box_remove_all", libgtk4), Nothing, (Ptr{GObject},), lb); lb)

delete!(lb::GtkListBox, w::GtkWidget) = (G_.remove(lb, w); lb)

function set_filter_func(lb::GtkListBox, match::Function)
create_cfunc = @cfunction($match, Cint, (Ptr{GObject}, Ptr{Nothing}))
ccall(("gtk_list_box_set_filter_func", libgtk4), Nothing, (Ptr{GObject}, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), lb, create_cfunc, C_NULL, C_NULL)
return nothing
end


## GtkCustomFilter

function GtkCustomFilter(match::Function)
Expand Down

0 comments on commit c9146c8

Please sign in to comment.