Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GtkListBox wrapper and example (with filtering) #37

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading