/usr/share/gtk-sharp3-examples/GtkDemo/DemoMain.cs is in gtk-sharp3-examples 2.99.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Gdk;
using Gtk;
using Pango;
namespace GtkDemo
{
public class DemoMain
{
private Gtk.Window window;
private TextBuffer infoBuffer = new TextBuffer (null);
private TextBuffer sourceBuffer = new TextBuffer (null);
private TreeView treeView;
private TreeStore store;
private TreeIter oldSelection = TreeIter.Zero;
public static void Main (string[] args)
{
Application.Init ();
new DemoMain ();
Application.Run ();
}
public DemoMain ()
{
SetupDefaultIcon ();
window = new Gtk.Window ("Gtk# Code Demos");
window.SetDefaultSize (600, 400);
window.DeleteEvent += new DeleteEventHandler (WindowDelete);
HBox hbox = new HBox (false, 0);
window.Add (hbox);
treeView = CreateTree ();
hbox.PackStart (treeView, false, false, 0);
Notebook notebook = new Notebook ();
hbox.PackStart (notebook, true, true, 0);
notebook.AppendPage (CreateText (infoBuffer, false), new Label ("_Info"));
TextTag heading = new TextTag ("heading");
heading.Font = "Sans 18";
infoBuffer.TagTable.Add (heading);
notebook.AppendPage (CreateText (sourceBuffer, true), new Label ("_Source"));
window.ShowAll ();
}
private void LoadFile (string filename)
{
Stream file = Assembly.GetExecutingAssembly ().GetManifestResourceStream (filename);
if (file != null) {
LoadStream (file);
} else if (File.Exists (filename)) {
file = File.OpenRead (filename);
LoadStream (file);
} else {
infoBuffer.Text = String.Format ("{0} was not found.", filename);
sourceBuffer.Text = String.Empty;
}
Fontify ();
}
private enum LoadState {
Title,
Info,
SkipWhitespace,
Body
};
private void LoadStream (Stream file)
{
StreamReader sr = new StreamReader (file);
LoadState state = LoadState.Title;
bool inPara = false;
infoBuffer.Text = "";
sourceBuffer.Text = "";
TextIter infoIter = infoBuffer.EndIter;
TextIter sourceIter = sourceBuffer.EndIter;
// mostly broken comment parsing for splitting
// out the special comments to display in the infobuffer
string line, trimmed;
while (sr.Peek () != -1) {
line = sr.ReadLine ();
trimmed = line.Trim ();
switch (state) {
case LoadState.Title:
if (trimmed.StartsWith ("/* ")) {
infoBuffer.InsertWithTagsByName (ref infoIter, trimmed.Substring (3), "heading");
state = LoadState.Info;
}
break;
case LoadState.Info:
if (trimmed == "*") {
infoBuffer.Insert (ref infoIter, "\n");
inPara = false;
} else if (trimmed.StartsWith ("* ")) {
if (inPara)
infoBuffer.Insert (ref infoIter, " ");
infoBuffer.Insert (ref infoIter, trimmed.Substring (2));
inPara = true;
} else if (trimmed.StartsWith ("*/"))
state = LoadState.SkipWhitespace;
break;
case LoadState.SkipWhitespace:
if (trimmed != "") {
state = LoadState.Body;
goto case LoadState.Body;
}
break;
case LoadState.Body:
sourceBuffer.Insert (ref sourceIter, line + "\n");
break;
}
}
sr.Close ();
file.Close ();
}
// this is to highlight the sourceBuffer
private void Fontify ()
{
}
private void SetupDefaultIcon ()
{
Gdk.Pixbuf pixbuf = Gdk.Pixbuf.LoadFromResource ("gtk-logo-rgb.gif");
if (pixbuf != null) {
// The gtk-logo-rgb icon has a white background
// make it transparent instead
Pixbuf transparent = pixbuf.AddAlpha (true, 0xff, 0xff, 0xff);
Gtk.Window.DefaultIconList = new Gdk.Pixbuf [] { transparent };
}
}
private TreeView CreateTree ()
{
TreeView view = new TreeView ();
view.Model = FillTree ();
CellRendererText cr = new CellRendererText ();
TreeViewColumn column = new TreeViewColumn ("Widget (double click for demo)", cr, "text", 0);
column.AddAttribute (cr, "style" , 2);
view.AppendColumn (column);
view.Selection.Changed += new EventHandler (TreeChanged);
view.RowActivated += new RowActivatedHandler (RowActivated);
view.ExpandAll ();
view.SetSizeRequest (200, -1);
view.Selection.Mode = Gtk.SelectionMode.Browse;
return view;
}
private ScrolledWindow CreateText (TextBuffer buffer, bool IsSource)
{
ScrolledWindow scrolledWindow = new ScrolledWindow ();
scrolledWindow.SetPolicy (PolicyType.Automatic, PolicyType.Automatic);
scrolledWindow.ShadowType = ShadowType.In;
TextView textView = new TextView (buffer);
textView.Editable = false;
textView.CursorVisible = false;
scrolledWindow.Add (textView);
if (IsSource) {
FontDescription fontDescription = FontDescription.FromString ("monospace");
textView.OverrideFont (fontDescription);
textView.WrapMode = Gtk.WrapMode.None;
} else {
// Make it a bit nicer for text
textView.WrapMode = Gtk.WrapMode.Word;
textView.PixelsAboveLines = 2;
textView.PixelsBelowLines = 2;
}
return scrolledWindow;
}
private TreeStore FillTree ()
{
// title, filename, italic
store = new TreeStore (typeof (string), typeof (System.Type), typeof (bool));
Dictionary<string, TreeIter> parents = new Dictionary<string, TreeIter> ();
TreeIter parent;
Type[] types = Assembly.GetExecutingAssembly ().GetTypes ();
foreach (Type t in types) {
object[] att = t.GetCustomAttributes (typeof (DemoAttribute), false);
foreach (DemoAttribute demo in att) {
if (demo.Parent != null) {
if (!parents.ContainsKey (demo.Parent))
parents.Add (demo.Parent, store.AppendValues (demo.Parent));
parent = parents[demo.Parent];
store.AppendValues (parent, demo.Label, t, false);
} else {
store.AppendValues (demo.Label, t, false);
}
}
}
store.SetSortColumnId (0, SortType.Ascending);
return store;
}
private void TreeChanged (object o, EventArgs args)
{
TreeIter iter;
ITreeModel model;
if (treeView.Selection.GetSelected (out model, out iter)) {
Type type = (Type) model.GetValue (iter, 1);
if (type != null) {
object[] atts = type.GetCustomAttributes (typeof (DemoAttribute), false);
string file = ((DemoAttribute) atts[0]).Filename;
LoadFile (file);
}
model.SetValue (iter, 2, true);
if (!oldSelection.Equals (TreeIter.Zero))
model.SetValue (oldSelection, 2, false);
oldSelection = iter;
}
}
private void RowActivated (object o, RowActivatedArgs args)
{
TreeIter iter;
if (treeView.Model.GetIter (out iter, args.Path)) {
Type type = (Type) treeView.Model.GetValue (iter, 1);
if (type != null)
Activator.CreateInstance (type);
}
}
private void WindowDelete (object o, DeleteEventArgs args)
{
Application.Quit ();
args.RetVal = true;
}
}
}
|