forked from GtkSharp/GtkSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
151 lines (118 loc) · 5.09 KB
/
MainWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This is free and unencumbered software released into the public domain.
// Happy coding!!! - GtkSharp Team
using Gtk;
using GtkSource;
using System;
using System.Collections.Generic;
using System.IO;
namespace Samples
{
class MainWindow : Window
{
private HeaderBar _headerBar;
private TreeView _treeView;
private Box _boxContent;
private TreeStore _store;
private Dictionary<string, (Type type, Widget widget)> _items;
private SourceView _textViewCode;
private Notebook _notebook;
public MainWindow() : base(WindowType.Toplevel)
{
// 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;
var hpanned = new HPaned();
hpanned.Position = 200;
_treeView = new TreeView();
_treeView.HeadersVisible = false;
hpanned.Pack1(_treeView, false, true);
_notebook = new Notebook();
var scroll1 = new ScrolledWindow();
var vpanned = new VPaned();
vpanned.Position = 300;
_boxContent = new Box(Orientation.Vertical, 0);
_boxContent.Margin = 8;
vpanned.Pack1(_boxContent, true, true);
vpanned.Pack2(ApplicationOutput.Widget, false, true);
scroll1.Child = vpanned;
_notebook.AppendPage(scroll1, new Label { Text = "Data", Expand = true });
var scroll2 = new ScrolledWindow();
_textViewCode = new SourceView();
_textViewCode.ShowLineNumbers = true;
_textViewCode.Buffer.Language = new LanguageManager().GetLanguage("c-sharp");
_textViewCode.Margin = 3;
scroll2.Child = _textViewCode;
_notebook.AppendPage(scroll2, new Label { Text = "Code", Expand = true });
hpanned.Pack2(_notebook, true, true);
Child = hpanned;
// Fill up data
FillUpTreeView();
// Connect events
_treeView.Selection.Changed += Selection_Changed;
Destroyed += (sender, e) => Application.Quit();
}
private void Selection_Changed(object sender, EventArgs e)
{
if (_treeView.Selection.GetSelected(out TreeIter iter))
{
var s = _store.GetValue(iter, 0).ToString();
while (_boxContent.Children.Length > 0)
_boxContent.Remove(_boxContent.Children[0]);
_notebook.CurrentPage = 0;
_notebook.ShowTabs = false;
if (_items.TryGetValue(s, out var item))
{
_notebook.ShowTabs = true;
if (item.widget == null)
_items[s] = item = (item.type, Activator.CreateInstance(item.type) as Widget);
using (var stream = typeof(ListSection).Assembly.GetManifestResourceStream("GtkSharp.Samples." + item.type.Name + ".cs"))
using (var reader = new StreamReader(stream))
_textViewCode.Buffer.Text = reader.ReadToEnd();
_boxContent.PackStart(item.widget, true, true, 0);
_boxContent.ShowAll();
}
}
}
private void FillUpTreeView()
{
// 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)
{
_store.AppendValues(dict[a.Category], a.ContentType.Name);
_items[a.ContentType.Name] = (type, null);
}
}
}
_treeView.ExpandAll();
}
}
}