-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbb59bf
commit 523feb0
Showing
16 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/Samples/Gtk-4.0/GridView/CustomObjectGridViewWindow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using Gtk; | ||
using static Gtk.GridView; | ||
using static Gtk.SignalListItemFactory; | ||
using ListStore = Gio.ListStore; | ||
|
||
namespace GridViewSample; | ||
|
||
public class ItemData : GObject.Object | ||
{ | ||
public string ImagePath { get; set; } | ||
public string Text { get; set; } | ||
public string Description { get; set; } | ||
|
||
public ItemData(string imagePath, string text, string description) | ||
: base(true, Array.Empty<GObject.ConstructArgument>()) | ||
{ | ||
ImagePath = imagePath; | ||
Text = text; | ||
Description = description; | ||
} | ||
} | ||
|
||
public class CustomObjectGridViewWindow : Window | ||
{ | ||
private readonly ListStore _model; | ||
|
||
public CustomObjectGridViewWindow() | ||
{ | ||
Title = "Gtk::GridView (Gio::ListStore)"; | ||
SetDefaultSize(400, 400); | ||
|
||
_model = ListStore.New(ItemData.GetGType()); | ||
_model.Append(new ItemData("Resources/number-1.svg", "One", "One")); | ||
_model.Append(new ItemData("Resources/number-2.svg", "Two", "Two")); | ||
_model.Append(new ItemData("Resources/number-3.svg", "Three", "Three")); | ||
_model.Append(new ItemData("Resources/number-4.svg", "Four", "Four")); | ||
_model.Append(new ItemData("Resources/number-5.svg", "Five", "Five")); | ||
_model.Append(new ItemData("Resources/number-6.svg", "Six", "Six")); | ||
_model.Append(new ItemData("Resources/number-7.svg", "Seven", "Seven")); | ||
_model.Append(new ItemData("Resources/number-8.svg", "Eight", "Eight")); | ||
_model.Append(new ItemData("Resources/number-9.svg", "Nine", "Nine")); | ||
|
||
var selectionModel = SingleSelection.New(_model); | ||
var listItemFactory = SignalListItemFactory.New(); | ||
listItemFactory.OnSetup += SetupSignalHandler; | ||
listItemFactory.OnBind += BindSignalHandler; | ||
|
||
var gridView = GridView.New(selectionModel, listItemFactory); | ||
gridView.OnActivate += OnGridViewOnActiveHandler; | ||
|
||
var scrolledWindow = ScrolledWindow.New(); | ||
scrolledWindow.Child = gridView; | ||
Child = scrolledWindow; | ||
} | ||
|
||
private void SetupSignalHandler(SignalListItemFactory sender, SetupSignalArgs args) | ||
{ | ||
if (args.Object is not ListItem listItem) | ||
{ | ||
return; | ||
} | ||
|
||
var box = Box.New(Orientation.Vertical, 2); | ||
box.SetSizeRequest(100, 100); | ||
listItem.Child = box; | ||
|
||
var image = Image.New(); | ||
box.Append(image); | ||
var label = Label.New(null); | ||
box.Append(label); | ||
} | ||
|
||
private void BindSignalHandler(SignalListItemFactory sender, BindSignalArgs args) | ||
{ | ||
if (args.Object is not ListItem listItem) | ||
{ | ||
return; | ||
} | ||
|
||
if (listItem.Child is not Box box) return; | ||
if (box.GetFirstChild() is not Image image) return; | ||
if (image.GetNextSibling() is not Label label) return; | ||
if (listItem.Item is not ItemData itemData) return; | ||
|
||
image.SetFromFile(itemData.ImagePath); | ||
label.SetText(itemData.Text); | ||
} | ||
|
||
private void OnGridViewOnActiveHandler(GridView sender, ActivateSignalArgs args) | ||
{ | ||
var itemData = _model.GetObject(args.Position) as ItemData; | ||
if (itemData is null) return; | ||
|
||
Console.WriteLine($"Selected Text: {itemData.Text}, Description: {itemData.Description}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Libs\Gtk-4.0\Gtk-4.0.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="$(ProjectDir)Resources\**" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var application = Gtk.Application.New("org.gir.core", Gio.ApplicationFlags.FlagsNone); | ||
application.OnActivate += (sender, args) => | ||
{ | ||
var buttonStringListGridView = CreateButton("String List GridView Window"); | ||
buttonStringListGridView.OnClicked += (_, _) => new GridViewSample.StringListGridViewWindow().Show(); | ||
|
||
var buttonCustomObjectGridView = CreateButton("Custom Object GridView Window"); | ||
buttonCustomObjectGridView.OnClicked += (_, _) => new GridViewSample.CustomObjectGridViewWindow().Show(); | ||
|
||
var gtkBox = Gtk.Box.New(Gtk.Orientation.Vertical, 0); | ||
gtkBox.Append(buttonStringListGridView); | ||
gtkBox.Append(buttonCustomObjectGridView); | ||
|
||
var window = Gtk.ApplicationWindow.New((Gtk.Application) sender); | ||
window.Title = "GridView Sample"; | ||
window.SetDefaultSize(300, 300); | ||
window.Child = gtkBox; | ||
window.Show(); | ||
}; | ||
return application.RunWithSynchronizationContext(null); | ||
|
||
static Gtk.Button CreateButton(string label) | ||
{ | ||
var button = Gtk.Button.New(); | ||
button.Label = label; | ||
button.SetMarginTop(12); | ||
button.SetMarginBottom(12); | ||
button.SetMarginStart(12); | ||
button.SetMarginEnd(12); | ||
return button; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Number SVG files were downloaded from https://www.svgrepo.com/vectors/number/. | ||
Credit for SVG files goes to [SVG Repo](https://www.svgrepo.com) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Gtk; | ||
using static Gtk.SignalListItemFactory; | ||
|
||
namespace GridViewSample; | ||
|
||
public class StringListGridViewWindow : Window | ||
{ | ||
public StringListGridViewWindow() | ||
{ | ||
Title = "Gtk::GridView (Gio::ListStore)"; | ||
SetDefaultSize(400, 400); | ||
|
||
var stringList = StringList.New(new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" }); | ||
var selectionModel = NoSelection.New(stringList); | ||
var listItemFactory = SignalListItemFactory.New(); | ||
listItemFactory.OnSetup += SetupSignalHandler; | ||
listItemFactory.OnBind += BindSignalHandler; | ||
|
||
var gridView = GridView.New(selectionModel, listItemFactory); | ||
|
||
var scrolledWindow = ScrolledWindow.New(); | ||
scrolledWindow.Child = gridView; | ||
Child = scrolledWindow; | ||
} | ||
|
||
private void SetupSignalHandler(SignalListItemFactory sender, SetupSignalArgs args) | ||
{ | ||
var listItem = args.Object as ListItem; | ||
if (listItem is null) | ||
{ | ||
return; | ||
} | ||
|
||
var label = Label.New(null); | ||
label.WidthRequest = 100; | ||
label.HeightRequest = 100; | ||
listItem.Child = label; | ||
} | ||
|
||
private void BindSignalHandler(SignalListItemFactory sender, BindSignalArgs args) | ||
{ | ||
var listItem = args.Object as ListItem; | ||
if (listItem is null) | ||
{ | ||
return; | ||
} | ||
|
||
var label = listItem.Child as Label; | ||
if (label is null) | ||
{ | ||
return; | ||
} | ||
|
||
var item = listItem.Item as StringObject; | ||
label.SetText(item?.String ?? "NOT FOUND"); | ||
} | ||
} |