Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
[Gtk] Add ComboBoxEntry.Completes support
Browse files Browse the repository at this point in the history
  • Loading branch information
sevoku committed Dec 18, 2019
1 parent 6fab1f0 commit 9e18300
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Xwt.Gtk/Xwt.GtkBackend/ComboBoxBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void SetSource (IListDataSource source, IBackend sourceBackend)
Widget.Model = model.Store;
} else
Widget.Model = b.Store;
OnSourceSet ();
}

public int SelectedRow {
Expand All @@ -127,6 +128,8 @@ public int SelectedRow {
}
#endregion

protected virtual void OnSourceSet () { }

#region ICellRendererTarget implementation
public void PackStart (object target, Gtk.CellRenderer cr, bool expand)
{
Expand Down
66 changes: 61 additions & 5 deletions Xwt.Gtk/Xwt.GtkBackend/ComboBoxEntryBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ namespace Xwt.GtkBackend
public class ComboBoxEntryBackend: ComboBoxBackend, IComboBoxEntryBackend
{
TextEntryBackend entryBackend;
int textColumn = 0;
bool completes;

public bool Completes
{
get
{
if (Widget?.Model == null)
return completes;
return entryBackend.TextEntry?.Completion?.Model == Widget.Model;
}
set
{
completes = value;

if (!completes)
{
// unset model only if not using custom completions through TextEntryBackend
if (entryBackend.TextEntry.Completion?.Model == Widget.Model)
entryBackend.TextEntry.Completion.Model = null;
} else {
BindCompletion ();
}
}
}

protected override Gtk.Widget CreateWidget ()
{
Expand All @@ -39,9 +64,43 @@ protected override Gtk.Widget CreateWidget ()
return c;
}

void BindCompletion()
{
var completion = entryBackend.TextEntry.Completion;
if (completion == null) {
completion = entryBackend.TextEntry.Completion = GtkBackend.TextEntryBackend.CreateCompletion();
}
completion.Model = Widget.Model;
SyncCompletionColumn (completion, textColumn);
}

static void SyncCompletionColumn (Gtk.EntryCompletion completion, int textColumn)
{
if (completion.TextColumn != textColumn) {
// Gtk.EntryCompletion.TextColumn is using gtk_entry_completion_set_text_column
// which will create a new Cell on each change. Since we don't want this "convenience",
// we need to use the "text_column" property instead, as suggested in
// https://developer.gnome.org/gtk2/stable/GtkEntryCompletion.html#gtk-entry-completion-set-text-column
using (GLib.Value val = new GLib.Value(textColumn)) {
completion.SetProperty("text_column", val);
}
}
}

protected override void OnSourceSet()
{
base.OnSourceSet();
if (completes && entryBackend.TextEntry?.Completion?.Model != Widget.Model) {
BindCompletion ();
}
}

public void SetTextColumn (int column)
{
textColumn = column;
Widget.SetTextColumn (column);
if (Completes)
SyncCompletionColumn (entryBackend.TextEntry.Completion, textColumn);
}

public ITextEntryBackend TextEntryBackend {
Expand All @@ -59,17 +118,14 @@ protected override void OnSetBackgroundColor (Xwt.Drawing.Color color)

class CustomComboEntryBackend: TextEntryBackend
{
Gtk.Entry entry;

public CustomComboEntryBackend (Gtk.Entry entry)
{
this.entry = entry;
Widget = entry;
}

public override void Initialize ()
{
Widget = entry;
entry.Show ();
TextEntry.Show ();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Xwt.Gtk/Xwt.GtkBackend/SearchTextEntryBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SearchTextEntryBackend: TextEntryBackend, ISearchTextEntryBackend
{
SearchEntry searchEntry;

protected override Gtk.Entry TextEntry {
internal protected override Gtk.Entry TextEntry {
get {
return searchEntry.Entry;
}
Expand Down
4 changes: 2 additions & 2 deletions Xwt.Gtk/Xwt.GtkBackend/TextEntryBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Initialize ()
Widget.Show ();
}

protected virtual Gtk.Entry TextEntry {
internal protected virtual Gtk.Entry TextEntry {
get { return (Gtk.Entry)base.Widget; }
}

Expand Down Expand Up @@ -221,7 +221,7 @@ public void SetCompletionMatchFunc (Func<string, string, bool> matchFunc)
};
}

Gtk.EntryCompletion CreateCompletion ()
internal static Gtk.EntryCompletion CreateCompletion ()
{
return new Gtk.EntryCompletion () {
PopupCompletion = true,
Expand Down

0 comments on commit 9e18300

Please sign in to comment.