From 687b070ea2892d2e829f1c1a2c2088fd3b31d8bf Mon Sep 17 00:00:00 2001 From: cra0zy Date: Sun, 21 Jan 2018 19:54:58 +0100 Subject: [PATCH] [Samples] Setup more of base + add button examples --- Source/Samples/ApplicationOutput.cs | 33 ++++++--- Source/Samples/MainWindow.cs | 1 + Source/Samples/Program.cs | 21 +++--- Source/Samples/{ => Sections}/Category.cs | 0 .../Sections/Dialogs/AboutDialogCategory.cs | 14 ---- .../Sections/Dialogs/AboutDialogSection.cs | 14 ++++ Source/Samples/Sections/ListSection.cs | 46 +++++++++++++ .../{ => Sections}/SectionAttribute.cs | 0 .../Sections/Widgets/ButtonCategory.cs | 17 ----- .../Samples/Sections/Widgets/ButtonSection.cs | 68 +++++++++++++++++++ 10 files changed, 162 insertions(+), 52 deletions(-) rename Source/Samples/{ => Sections}/Category.cs (100%) delete mode 100644 Source/Samples/Sections/Dialogs/AboutDialogCategory.cs create mode 100644 Source/Samples/Sections/Dialogs/AboutDialogSection.cs create mode 100644 Source/Samples/Sections/ListSection.cs rename Source/Samples/{ => Sections}/SectionAttribute.cs (100%) delete mode 100644 Source/Samples/Sections/Widgets/ButtonCategory.cs create mode 100644 Source/Samples/Sections/Widgets/ButtonSection.cs diff --git a/Source/Samples/ApplicationOutput.cs b/Source/Samples/ApplicationOutput.cs index 78ed06efd..446ee7675 100644 --- a/Source/Samples/ApplicationOutput.cs +++ b/Source/Samples/ApplicationOutput.cs @@ -1,23 +1,23 @@ using System; -using System.Diagnostics; using Gtk; namespace Samples { static class ApplicationOutput { - public static Widget Widget { get; set; } - private static ScrolledWindow _scrolledWindow; - private static TextView _textView; + private static readonly ScrolledWindow _scrolledWindow; + private static readonly TextView _textView; static ApplicationOutput() { var vbox = new VBox(); - var labelTitle = new Label(); - labelTitle.Text = "Application Output:"; - labelTitle.Margin = 4; - labelTitle.Xalign = 0f; + var labelTitle = new Label + { + Text = "Application Output:", + Margin = 4, + Xalign = 0f + }; vbox.PackStart(labelTitle, false, true, 0); _scrolledWindow = new ScrolledWindow(); @@ -26,18 +26,29 @@ namespace Samples vbox.PackStart(_scrolledWindow, true, true, 0); Widget = vbox; + + _textView.SizeAllocated += TextView_SizeAllocated; + } + + public static Widget Widget { get; set; } + + private static void TextView_SizeAllocated(object o, SizeAllocatedArgs args) + { + _textView.ScrollToIter(_textView.Buffer.EndIter, 0, false, 0, 0); } public static void WriteLine(object o, string e) { - WriteLine("[" + Environment.TickCount + "] " + o.GetType().ToString() + ": " + e); + WriteLine("[" + Environment.TickCount + "] " + o.GetType() + ": " + e); } public static void WriteLine(string line) { var enditer = _textView.Buffer.EndIter; - _textView.Buffer.Insert(ref enditer, line + Environment.NewLine); - _textView.ScrollToIter(enditer, 0, false, 0, 0); + if (_textView.Buffer.Text.Length > 0) + line = Environment.NewLine + line; + _textView.Buffer.Insert(ref enditer, line); + } } } \ No newline at end of file diff --git a/Source/Samples/MainWindow.cs b/Source/Samples/MainWindow.cs index 63f1e4d78..e6d0c34af 100644 --- a/Source/Samples/MainWindow.cs +++ b/Source/Samples/MainWindow.cs @@ -40,6 +40,7 @@ namespace Samples vpanned.Position = 400; _boxContent = new Box(Orientation.Vertical, 0); + _boxContent.Margin = 8; vpanned.Pack1(_boxContent, true, true); vpanned.Pack2(ApplicationOutput.Widget, false, true); diff --git a/Source/Samples/Program.cs b/Source/Samples/Program.cs index 447cd1953..90fc12af6 100644 --- a/Source/Samples/Program.cs +++ b/Source/Samples/Program.cs @@ -48,16 +48,17 @@ namespace Samples private static void AboutActivated(object sender, EventArgs e) { - var dialog = new AboutDialog(); - dialog.TransientFor = Win; - dialog.ProgramName = "GtkSharp Sample Application"; - dialog.Version = "1.0.0.0"; - dialog.Comments = "A sample application for the GtkSharp project."; - dialog.LogoIconName = "system-run-symbolic"; - dialog.License = "This sample application is licensed under public domain."; - dialog.Website = "https://www.github.com/GtkSharp/GtkSharp"; - dialog.WebsiteLabel = "GtkSharp Website"; - + var dialog = new AboutDialog + { + TransientFor = Win, + ProgramName = "GtkSharp Sample Application", + Version = "1.0.0.0", + Comments = "A sample application for the GtkSharp project.", + LogoIconName = "system-run-symbolic", + License = "This sample application is licensed under public domain.", + Website = "https://www.github.com/GtkSharp/GtkSharp", + WebsiteLabel = "GtkSharp Website" + }; dialog.Run(); dialog.Hide(); } diff --git a/Source/Samples/Category.cs b/Source/Samples/Sections/Category.cs similarity index 100% rename from Source/Samples/Category.cs rename to Source/Samples/Sections/Category.cs diff --git a/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs b/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs deleted file mode 100644 index 496faf292..000000000 --- a/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Gtk; - -namespace Samples -{ - [SectionAttribute(Name = "AboutDialog", Category = Category.Dialogs)] - class AboutDialogCategory : Box - { - public AboutDialogCategory() : base(Orientation.Vertical, 0) - { - - } - } -} \ No newline at end of file diff --git a/Source/Samples/Sections/Dialogs/AboutDialogSection.cs b/Source/Samples/Sections/Dialogs/AboutDialogSection.cs new file mode 100644 index 000000000..8468dded1 --- /dev/null +++ b/Source/Samples/Sections/Dialogs/AboutDialogSection.cs @@ -0,0 +1,14 @@ +using System; +using Gtk; + +namespace Samples +{ + [Section(Name = "AboutDialog", Category = Category.Dialogs)] + class AboutDialogSection : Box + { + public AboutDialogSection() : base(Orientation.Vertical, 0) + { + + } + } +} \ No newline at end of file diff --git a/Source/Samples/Sections/ListSection.cs b/Source/Samples/Sections/ListSection.cs new file mode 100644 index 000000000..e6679b32b --- /dev/null +++ b/Source/Samples/Sections/ListSection.cs @@ -0,0 +1,46 @@ +using System; +using Gtk; + +namespace Samples +{ + public class ListSection : Box + { + private Grid _grid; + private int _position; + + public ListSection() : base(Orientation.Vertical, 0) + { + _position = 0; + _grid = new Grid + { + RowSpacing = 6, + ColumnSpacing = 6 + }; + + PackStart(_grid, false, true, 0); + PackStart(new VBox(), true, true, 0); + } + + public void AddItem((string, Widget) turp) + { + AddItem(turp.Item1, turp.Item2); + } + + public void AddItem(string label, Widget widget) + { + _grid.Attach(new Label + { + Text = label, + Hexpand = true, + Halign = Align.Start + }, 0, _position, 1, 1); + + var hbox = new HBox(); + hbox.PackStart(new VBox(), true, true, 0); + hbox.PackStart(widget, false, true, 0); + + _grid.Attach(hbox, 1, _position, 1, 1); + _position++; + } + } +} diff --git a/Source/Samples/SectionAttribute.cs b/Source/Samples/Sections/SectionAttribute.cs similarity index 100% rename from Source/Samples/SectionAttribute.cs rename to Source/Samples/Sections/SectionAttribute.cs diff --git a/Source/Samples/Sections/Widgets/ButtonCategory.cs b/Source/Samples/Sections/Widgets/ButtonCategory.cs deleted file mode 100644 index 004279d2d..000000000 --- a/Source/Samples/Sections/Widgets/ButtonCategory.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using Gtk; - -namespace Samples -{ - [SectionAttribute(Name = "Button", Category = Category.Widgets)] - class ButtonCategory : Box - { - public ButtonCategory() : base(Orientation.Vertical, 0) - { - var btn = new Button("Click Me"); - PackStart(btn, true, true, 0); - - btn.Clicked += (sender, e) => ApplicationOutput.WriteLine(sender, "Clicked"); - } - } -} \ No newline at end of file diff --git a/Source/Samples/Sections/Widgets/ButtonSection.cs b/Source/Samples/Sections/Widgets/ButtonSection.cs new file mode 100644 index 000000000..7fee6e499 --- /dev/null +++ b/Source/Samples/Sections/Widgets/ButtonSection.cs @@ -0,0 +1,68 @@ +using Gtk; + +namespace Samples +{ + [Section(Name = "Button", Category = Category.Widgets)] + class ButtonSection : ListSection + { + public ButtonSection() + { + AddItem(CreateSimpleButton()); + AddItem(CreateStockButton()); + AddItem(CreateImageButton()); + AddItem(CreateImageTextButton()); + AddItem(CreateActionButton()); + } + + public (string, Widget) CreateSimpleButton() + { + var btn = new Button("Simple Button"); + btn.Clicked += (sender, e) => ApplicationOutput.WriteLine(sender, "Clicked"); + + return ("Simple button:", btn); + } + + public (string, Widget) CreateStockButton() + { + var btn = new Button(Stock.About); + btn.Clicked += (sender, e) => ApplicationOutput.WriteLine(sender, "Clicked"); + + return ("Stock button:", btn); + } + + public (string, Widget) CreateImageButton() + { + var btn = new Button(); + btn.AlwaysShowImage = true; + btn.Image = Image.NewFromIconName("document-new-symbolic", IconSize.Button); + btn.Clicked += (sender, e) => ApplicationOutput.WriteLine(sender, "Clicked"); + + return ("Image button:", btn); + } + + public (string, Widget) CreateImageTextButton() + { + var btn = new Button(); + btn.Label = "Some text"; + btn.ImagePosition = PositionType.Top; + btn.AlwaysShowImage = true; + btn.Image = Image.NewFromIconName("document-new-symbolic", IconSize.Button); + btn.Clicked += (sender, e) => ApplicationOutput.WriteLine(sender, "Clicked"); + + return ("Image and text button:", btn); + } + + public (string, Widget) CreateActionButton() + { + var sa = new GLib.SimpleAction("SampleAction", null); + sa.Activated += (sender, e) => ApplicationOutput.WriteLine(sender, "SampleAction Activated"); + Program.App.AddAction(sa); + + var btn = new Button(); + btn.Label = "SampleAction Button"; + btn.ActionName = "app.SampleAction"; + + return ("Action button:", btn); + } + } +} \ No newline at end of file