diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..84580c985 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,57 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "BuildOutput/Samples/Samples.dll", + "args": [], + "cwd": "${workspaceFolder}", + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/bin/Debug//.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart", + "launchBrowser": { + "enabled": true, + "args": "${auto-detect-url}", + "windows": { + "command": "cmd.exe", + "args": "/C start ${auto-detect-url}" + }, + "osx": { + "command": "open" + }, + "linux": { + "command": "xdg-open" + } + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..c998d0e04 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.exclude": { + "**/obj/": true + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..10040fd8f --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet build Source/Samples/Samples.csproj", + "type": "shell", + "group": "build", + "presentation": { + "reveal": "silent" + }, + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Source/Samples/Category.cs b/Source/Samples/Category.cs new file mode 100644 index 000000000..48210cffd --- /dev/null +++ b/Source/Samples/Category.cs @@ -0,0 +1,9 @@ + +namespace Samples +{ + enum Category + { + Widgets, + Dialogs + } +} \ No newline at end of file diff --git a/Source/Samples/MainWindow.cs b/Source/Samples/MainWindow.cs index 071748690..5ded3d31d 100644 --- a/Source/Samples/MainWindow.cs +++ b/Source/Samples/MainWindow.cs @@ -1,37 +1,116 @@ -using System; using Gtk; -using UI = Gtk.Builder.ObjectAttribute; +using System; +using System.Collections.Generic; namespace Samples { class MainWindow : Window { -#pragma warning disable 0649 - [UI] private Label _label1; - [UI] private Button _button1; -#pragma warning restore 0649 + private HeaderBar _headerBar; + private HPaned _panned; + private TreeView _treeView; + private Box _boxContent; + private TreeStore _store; + private Dictionary _items; - private int _counter; - - public MainWindow() : this(new Builder("MainWindow.glade")) { } - - private MainWindow(Builder builder) : base(builder.GetObject("MainWindow").Handle) + public MainWindow() : base(WindowType.Toplevel) { - builder.Autoconnect(this); - - DeleteEvent += Window_DeleteEvent; - _button1.Clicked += Button1_Clicked; + // Setup GUI + WindowPosition = WindowPosition.Center; + DefaultSize = new Gdk.Size(800, 600); + + _headerBar = new HeaderBar(); + _headerBar.ShowCloseButton = true; + _headerBar.Title = "GtkSharp Sample Application"; + + var btnClickMe = new Button(); + btnClickMe.AlwaysShowImage = true; + btnClickMe.Image = Image.NewFromIconName("document-new-symbolic", IconSize.Button); + _headerBar.PackStart(btnClickMe); + + Titlebar = _headerBar; + + _panned = new HPaned(); + _panned.Position = 200; + + _treeView = new TreeView(); + _treeView.HeadersVisible = false; + _panned.Pack1(_treeView, false, true); + + _boxContent = new Box(Orientation.Vertical, 0); + _panned.Pack2(_boxContent, true, true); + + Child = _panned; + + // Fill up data + FillUpTreeView(); + + // Connect events + _treeView.Selection.Changed += Selection_Changed; + Destroyed += (sender, e) => Application.Quit(); } - private void Window_DeleteEvent(object sender, DeleteEventArgs a) + private void Selection_Changed(object sender, EventArgs e) { - Application.Quit(); + if (_treeView.Selection.GetSelected(out TreeIter iter)) + { + var s = _store.GetValue(iter, 0).ToString(); + + while (_boxContent.Children.Length > 0) + _boxContent.Remove(_boxContent.Children[0]); + + if (_items.TryGetValue(s, out var item)) + { + if (item.widget == null) + _items[s] = item = (item.type, Activator.CreateInstance(item.type) as Widget); + + _boxContent.PackStart(item.widget, true, true, 0); + _boxContent.ShowAll(); + } + + } } - private void Button1_Clicked(object sender, EventArgs a) + private void FillUpTreeView() { - _counter++; - _label1.Text = "Hello World! This button has been clicked " + _counter + " time(s)."; + // Init cells + var cellName = new CellRendererText(); + + // Init columns + var columeSections = new TreeViewColumn(); + columeSections.Title = "Sections"; + columeSections.PackStart(cellName, true); + + columeSections.AddAttribute(cellName, "text", 0); + + _treeView.AppendColumn(columeSections); + + // Init treeview + _store = new TreeStore(typeof(string)); + _treeView.Model = _store; + + // Setup category base + var dict = new Dictionary(); + foreach (var category in Enum.GetValues(typeof(Category))) + dict[(Category)category] = _store.AppendValues(category.ToString()); + + // Fill up categories + _items = new Dictionary(); + var maintype = typeof(SectionAttribute); + + foreach (var type in maintype.Assembly.GetTypes()) + { + foreach (var attribute in type.GetCustomAttributes(true)) + { + if (attribute is SectionAttribute a) + { + _store.AppendValues(dict[a.Category], a.Name); + _items[a.Name] = (type, null); + } + } + } + + _treeView.ExpandAll(); } } -} +} \ No newline at end of file diff --git a/Source/Samples/MainWindow.glade b/Source/Samples/MainWindow.glade deleted file mode 100644 index a13c41b20..000000000 --- a/Source/Samples/MainWindow.glade +++ /dev/null @@ -1,46 +0,0 @@ - - - - - False - Example Window - 480 - 240 - - - True - False - 4 - 4 - 4 - 4 - vertical - - - True - False - Hello World! - - - True - True - 0 - - - - - Click me! - True - False - True - - - False - True - 1 - - - - - - diff --git a/Source/Samples/Program.cs b/Source/Samples/Program.cs index 877424780..64b2282d8 100644 --- a/Source/Samples/Program.cs +++ b/Source/Samples/Program.cs @@ -5,19 +5,67 @@ namespace Samples { class Program { + public static Application App; + + public static Window Win; + [STAThread] public static void Main(string[] args) { Application.Init(); - var app = new Application("org.Samples.Samples", GLib.ApplicationFlags.None); - app.Register(GLib.Cancellable.Current); + App = new Application("org.Samples.Samples", GLib.ApplicationFlags.None); + App.Register(GLib.Cancellable.Current); - var win = new MainWindow(); - app.AddWindow(win); + Win = new MainWindow(); + App.AddWindow(Win); - win.Show(); + var menu = new GLib.Menu(); + menu.AppendItem(new GLib.MenuItem("Help", "app.help")); + menu.AppendItem(new GLib.MenuItem("About", "app.about")); + menu.AppendItem(new GLib.MenuItem("Quit", "app.quit")); + App.AppMenu = menu; + + var helpAction = new GLib.SimpleAction("help", null); + helpAction.Activated += HelpActivated; + App.AddAction(helpAction); + + var aboutAction = new GLib.SimpleAction("about", null); + aboutAction.Activated += AboutActivated; + App.AddAction(aboutAction); + + var quitAction = new GLib.SimpleAction("quit", null); + quitAction.Activated += QuitActivated; + App.AddAction(quitAction); + + Win.ShowAll(); Application.Run(); } + + private static void HelpActivated(object sender, EventArgs e) + { + + } + + 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"; + + dialog.Run(); + dialog.Hide(); + } + + private static void QuitActivated(object sender, EventArgs e) + { + Application.Quit(); + } } } diff --git a/Source/Samples/SectionAttribute.cs b/Source/Samples/SectionAttribute.cs new file mode 100644 index 000000000..a6b3fec11 --- /dev/null +++ b/Source/Samples/SectionAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace Samples +{ + class SectionAttribute : Attribute + { + public string Name { get; set; } + + public Category Category { get; set; } + } +} \ No newline at end of file diff --git a/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs b/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs new file mode 100644 index 000000000..496faf292 --- /dev/null +++ b/Source/Samples/Sections/Dialogs/AboutDialogCategory.cs @@ -0,0 +1,14 @@ +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/Widgets/ButtonCategory.cs b/Source/Samples/Sections/Widgets/ButtonCategory.cs new file mode 100644 index 000000000..9c1b2014d --- /dev/null +++ b/Source/Samples/Sections/Widgets/ButtonCategory.cs @@ -0,0 +1,15 @@ +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); + } + } +} \ No newline at end of file