GtkSharp/Source/Samples/MainWindow.cs

146 lines
4.9 KiB
C#
Raw Normal View History

2018-01-25 23:13:36 +00:00
// This is free and unencumbered software released into the public domain.
// Happy coding!!! - GtkSharp Team
2018-01-17 22:35:07 +00:00
using Gtk;
2018-01-20 19:09:27 +00:00
using System;
using System.Collections.Generic;
2018-01-21 19:44:00 +00:00
using System.IO;
2018-01-17 22:35:07 +00:00
namespace Samples
{
class MainWindow : Window
{
2018-01-20 19:09:27 +00:00
private HeaderBar _headerBar;
private TreeView _treeView;
private Box _boxContent;
private TreeStore _store;
private Dictionary<string, (Type type, Widget widget)> _items;
2018-01-21 19:44:00 +00:00
private TextView _textViewCode;
private Notebook _notebook;
2018-01-20 19:09:27 +00:00
public MainWindow() : base(WindowType.Toplevel)
{
// Setup GUI
WindowPosition = WindowPosition.Center;
DefaultSize = new Gdk.Size(800, 600);
2018-01-17 22:35:07 +00:00
2018-01-20 19:09:27 +00:00
_headerBar = new HeaderBar();
_headerBar.ShowCloseButton = true;
_headerBar.Title = "GtkSharp Sample Application";
2018-01-17 22:35:07 +00:00
2018-01-20 19:09:27 +00:00
var btnClickMe = new Button();
btnClickMe.AlwaysShowImage = true;
btnClickMe.Image = Image.NewFromIconName("document-new-symbolic", IconSize.Button);
_headerBar.PackStart(btnClickMe);
2018-01-17 22:35:07 +00:00
2018-01-20 19:09:27 +00:00
Titlebar = _headerBar;
var hpanned = new HPaned();
hpanned.Position = 200;
2018-01-20 19:09:27 +00:00
_treeView = new TreeView();
_treeView.HeadersVisible = false;
hpanned.Pack1(_treeView, false, true);
2018-01-21 19:44:00 +00:00
_notebook = new Notebook();
2018-01-20 19:09:27 +00:00
2018-01-21 19:44:00 +00:00
var scroll1 = new ScrolledWindow();
var vpanned = new VPaned();
vpanned.Position = 300;
2018-01-20 19:09:27 +00:00
_boxContent = new Box(Orientation.Vertical, 0);
_boxContent.Margin = 8;
vpanned.Pack1(_boxContent, true, true);
vpanned.Pack2(ApplicationOutput.Widget, false, true);
2018-01-21 19:44:00 +00:00
scroll1.Child = vpanned;
_notebook.AppendPage(scroll1, new Label { Text = "Data", Expand = true });
var scroll2 = new ScrolledWindow();
_textViewCode = new TextView();
_textViewCode.Margin = 3;
scroll2.Child = _textViewCode;
_notebook.AppendPage(scroll2, new Label { Text = "Code", Expand = true });
2018-01-21 19:44:00 +00:00
hpanned.Pack2(_notebook, true, true);
2018-01-20 19:09:27 +00:00
Child = hpanned;
2018-01-20 19:09:27 +00:00
// Fill up data
FillUpTreeView();
// Connect events
_treeView.Selection.Changed += Selection_Changed;
Destroyed += (sender, e) => Application.Quit();
2018-01-17 22:35:07 +00:00
}
2018-01-20 19:09:27 +00:00
private void Selection_Changed(object sender, EventArgs e)
2018-01-17 22:35:07 +00:00
{
2018-01-20 19:09:27 +00:00
if (_treeView.Selection.GetSelected(out TreeIter iter))
{
var s = _store.GetValue(iter, 0).ToString();
while (_boxContent.Children.Length > 0)
_boxContent.Remove(_boxContent.Children[0]);
2018-01-21 19:44:00 +00:00
_notebook.CurrentPage = 0;
_notebook.ShowTabs = false;
2018-01-20 19:09:27 +00:00
if (_items.TryGetValue(s, out var item))
{
2018-01-21 19:44:00 +00:00
_notebook.ShowTabs = true;
2018-01-20 19:09:27 +00:00
if (item.widget == null)
_items[s] = item = (item.type, Activator.CreateInstance(item.type) as Widget);
2018-01-21 19:44:00 +00:00
using (var stream = typeof(ListSection).Assembly.GetManifestResourceStream("GtkSharp.Samples." + item.type.Name + ".cs"))
using (var reader = new StreamReader(stream))
_textViewCode.Buffer.Text = reader.ReadToEnd();
2018-01-20 19:09:27 +00:00
_boxContent.PackStart(item.widget, true, true, 0);
_boxContent.ShowAll();
}
}
2018-01-17 22:35:07 +00:00
}
2018-01-20 19:09:27 +00:00
private void FillUpTreeView()
2018-01-17 22:35:07 +00:00
{
2018-01-20 19:09:27 +00:00
// 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<Category, TreeIter>();
foreach (var category in Enum.GetValues(typeof(Category)))
dict[(Category)category] = _store.AppendValues(category.ToString());
// Fill up categories
_items = new Dictionary<string, (Type type, Widget widget)>();
var maintype = typeof(SectionAttribute);
foreach (var type in maintype.Assembly.GetTypes())
{
foreach (var attribute in type.GetCustomAttributes(true))
{
if (attribute is SectionAttribute a)
{
2018-01-27 20:47:02 +00:00
_store.AppendValues(dict[a.Category], a.ContentType.Name);
_items[a.ContentType.Name] = (type, null);
2018-01-20 19:09:27 +00:00
}
}
}
_treeView.ExpandAll();
2018-01-17 22:35:07 +00:00
}
}
2018-01-20 19:09:27 +00:00
}