Merge pull request #32 from pedrohex/samplescont

Samples continuation
This commit is contained in:
Harry 2018-03-22 01:25:33 +01:00 committed by GitHub
commit ed21af67d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 30 deletions

View file

@ -0,0 +1,97 @@
// This is free and unencumbered software released into the public domain.
// Happy coding!!! - GtkSharp Team
using Gtk;
namespace Samples
{
[Section(ContentType = typeof(ComboBox), Category = Category.Widgets)]
class ComboBoxSection : ListSection
{
public ComboBoxSection()
{
AddItem(CreateSimpleComboBox());
AddItem(CreateMultiColumnComboBox());
AddItem(CreateEditableComboBox());
}
public (string, Widget) CreateSimpleComboBox()
{
// initialize with a simple string list
var combo = new ComboBox(
new string[]
{
"Combo Entry 1",
"Combo Entry 2",
"Combo Entry 3",
"Combo Entry 4"
}
);
// event to notify for index changes in our combo
combo.Changed += (sender, e) =>
ApplicationOutput.WriteLine(sender, $"Index changed to:{((ComboBox)sender).Active}");
// set the active selection to index 2 (Combo Entry 3)
combo.Active = 2;
return ("Simple combo:", combo);
}
public (string, Widget) CreateMultiColumnComboBox()
{
// create a store for our combo
var store = new ListStore(typeof(string), typeof(string), typeof(bool));
// lets append some stock icons, passing the icon names, and a simple text column
store.AppendValues("dialog-warning", "Warning", true);
store.AppendValues("process-stop", "Stop", false);
store.AppendValues("document-new", "New", true);
store.AppendValues("edit-clear", "Clear", true);
// create cells
var imageCell = new CellRendererPixbuf();
var textCell = new CellRendererText();
// create the combo and pass the values in
var combo = new ComboBox(store);
combo.PackStart(imageCell, true);
combo.PackStart(textCell, true);
// add combo attributes to show in columns
combo.AddAttribute(imageCell, "icon-name", 0);
combo.AddAttribute(textCell, "text", 1);
// lets use the store bool values to control sensitive rows
// Process-stop (store index one) should be disabled in this sample
// For a ComboBox item to be disabled, all cell renderers for the item need to have
// their sensitivity disabled
combo.AddAttribute(imageCell, "sensitive", 2);
combo.AddAttribute(textCell, "sensitive", 2);
// listen to index changed on combo
combo.Changed += (sender, e) =>
ApplicationOutput.WriteLine(sender, $"Index changed to:{((ComboBox)sender).Active}");
// lets preselect the first option
combo.Active = 0;
return ("Combo with Icons and Text:", combo);
}
public (string, Widget) CreateEditableComboBox()
{
var combo = ComboBoxText.NewWithEntry();
combo.AppendText("Example 1");
combo.AppendText("Example 2");
combo.AppendText("Example 3");
combo.AppendText("Example 4");
// combos with entry have a real entry inside it
// we can use it by
combo.Entry.PlaceholderText = "Write something";
return ("Combo with entry:", combo);
}
}
}

View file

@ -18,6 +18,7 @@ namespace Samples
AddItem(CreateCustomActionsEntry()); AddItem(CreateCustomActionsEntry());
AddItem(CreateProgressEntry()); AddItem(CreateProgressEntry());
AddItem(CreateCompletionEntry()); AddItem(CreateCompletionEntry());
AddItem(CreateInsensitiveEntry());
} }
public (string, Widget) CreateSimpleEntry() public (string, Widget) CreateSimpleEntry()
@ -123,44 +124,40 @@ namespace Samples
store.AppendValues("Better and bigger example"); store.AppendValues("Better and bigger example");
store.AppendValues("Some other example"); store.AppendValues("Some other example");
// create tree column // assign treemodel as the completion
var column = new TreeViewColumn(); completion.Model = store;
column.Reorderable = false;
column.SortIndicator = false;
column.Clickable = true;
column.SortColumnId = 0;
// create cells // lets override the default match function so we can use the contains mode
var cell = new CellRendererText(); // instead of the default startswith
column.PackStart(cell, true); completion.MatchFunc = (EntryCompletion comp, string key, TreeIter iter) =>
// set cell functions
column.SetCellDataFunc(cell, (TreeViewColumn tree_column, CellRenderer cellRenderer, ITreeModel tree_model, TreeIter iter) =>
{ {
var m = tree_model.GetValue(iter, 0); if (string.IsNullOrEmpty(key))
(cellRenderer as CellRendererText).Text = (string)m; return false;
});
// model filter
var filter = new TreeModelFilter(store, null);
filter.VisibleFunc = (model, iter) =>
{
if (string.IsNullOrEmpty(entry.Text))
return true;
var o = model.GetValue(iter, 0); var o = comp.Model.GetValue(iter, 0);
var searchString = o as string; var stringToSearch = o as string;
if (!string.IsNullOrEmpty(searchString))
return searchString.StartsWith(entry.Text, System.StringComparison.InvariantCultureIgnoreCase);
return true; if (!string.IsNullOrEmpty(stringToSearch))
return stringToSearch.IndexOf(key, System.StringComparison.InvariantCultureIgnoreCase) >= 0;
return false;
}; };
// assign treemodel as the completion
completion.Model = filter;
completion.TextColumn = 0; completion.TextColumn = 0;
return ("Completion Entry:",entry); return ("Completion Entry:",entry);
} }
public (string, Widget) CreateInsensitiveEntry()
{
var entry = new Entry();
entry.Text = "Cannot change this";
entry.Sensitive = false;
return ("Insensitive entry:", entry);
}
} }
} }

View file

@ -68,7 +68,7 @@ namespace Samples
public (string, Widget) CreatePulseProgressBar() public (string, Widget) CreatePulseProgressBar()
{ {
// this is used when application can report progress // this is used when application can't report progress
var pb = new ProgressBar(); var pb = new ProgressBar();