Skip to content

Commit

Permalink
StringListGridViewWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifsoofi committed Feb 21, 2024
1 parent fbb59bf commit 523feb0
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/GirCore.Libs.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"Samples\\Gtk-4.0\\DrawingArea\\DrawingArea.csproj",
"Samples\\Gtk-4.0\\FontDialog\\FontDialog.csproj",
"Samples\\Gtk-4.0\\ListView\\ListView.csproj",
"Samples\\Gtk-4.0\\GridView\\GridView.csproj",
"Samples\\Gtk-4.0\\Window\\Window.csproj",
"Samples\\GtkSource-5\\GtkSourceView\\GtkSourceView.csproj",
"Samples\\WebKit-6.0\\JavascriptCall\\JavascriptCall.csproj",
Expand Down
15 changes: 15 additions & 0 deletions src/GirCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Samples\Gtk-4.0\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListView", "Samples\Gtk-4.0\ListView\ListView.csproj", "{3F6B722F-5192-46BC-A389-5E40ABA9041B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GridView", "Samples\Gtk-4.0\GridView\GridView.csproj", "{EE13D29A-42BC-4694-9980-D0498AA15115}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -857,6 +859,18 @@ Global
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x64.Build.0 = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x86.ActiveCfg = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x86.Build.0 = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|x64.ActiveCfg = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|x64.Build.0 = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|x86.ActiveCfg = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Debug|x86.Build.0 = Debug|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|Any CPU.Build.0 = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|x64.ActiveCfg = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|x64.Build.0 = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|x86.ActiveCfg = Release|Any CPU
{EE13D29A-42BC-4694-9980-D0498AA15115}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BF7F9B0B-CB43-4161-BFAD-C6EE479FC86B} = {386AE10F-B7AC-4C97-AC5C-202D3662A868}
Expand Down Expand Up @@ -930,5 +944,6 @@ Global
{B75DC27B-C90A-47E6-909F-C55D6204F35D} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
{48FEE015-C1AD-41EE-A39D-E441F03AC5A3} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
{3F6B722F-5192-46BC-A389-5E40ABA9041B} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
{EE13D29A-42BC-4694-9980-D0498AA15115} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
EndGlobalSection
EndGlobal
97 changes: 97 additions & 0 deletions src/Samples/Gtk-4.0/GridView/CustomObjectGridViewWindow.cs
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}");
}
}
17 changes: 17 additions & 0 deletions src/Samples/Gtk-4.0/GridView/GridView.csproj
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>
31 changes: 31 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Program.cs
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;
}
2 changes: 2 additions & 0 deletions src/Samples/Gtk-4.0/GridView/README.md
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)
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/Samples/Gtk-4.0/GridView/Resources/number-9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions src/Samples/Gtk-4.0/GridView/StringListGridViewWindow.cs
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");
}
}

0 comments on commit 523feb0

Please sign in to comment.