From 4da41dbe673ca0b02a1bd7903975bddba2945fc4 Mon Sep 17 00:00:00 2001 From: John Luke Date: Sun, 12 Dec 2004 22:11:44 +0000 Subject: [PATCH] use reflection to handle the TreeView and launching of the demos Add a DemoAttribute to all the demos to support this svn path=/trunk/gtk-sharp/; revision=37669 --- sample/GtkDemo/DemoApplicationWindow.cs | 1 + sample/GtkDemo/DemoAttribute.cs | 36 +++++ sample/GtkDemo/DemoButtonBox.cs | 1 + sample/GtkDemo/DemoClipboard.cs | 1 + sample/GtkDemo/DemoColorSelection.cs | 1 + sample/GtkDemo/DemoDialog.cs | 1 + sample/GtkDemo/DemoDrawingArea.cs | 1 + sample/GtkDemo/DemoEditableCells.cs | 1 + sample/GtkDemo/DemoEntryCompletion.cs | 1 + sample/GtkDemo/DemoExpander.cs | 1 + sample/GtkDemo/DemoHyperText.cs | 1 + sample/GtkDemo/DemoImages.cs | 1 + sample/GtkDemo/DemoListStore.cs | 1 + sample/GtkDemo/DemoMain.cs | 174 ++++++++---------------- sample/GtkDemo/DemoMenus.cs | 1 + sample/GtkDemo/DemoPanes.cs | 1 + sample/GtkDemo/DemoPixbuf.cs | 2 +- sample/GtkDemo/DemoSizeGroup.cs | 1 + sample/GtkDemo/DemoStockBrowser.cs | 1 + sample/GtkDemo/DemoTextView.cs | 1 + sample/GtkDemo/DemoTreeStore.cs | 1 + sample/GtkDemo/DemoUIManager.cs | 1 + sample/GtkDemo/Makefile.am | 1 + sample/GtkDemo/TODO | 1 - 24 files changed, 110 insertions(+), 123 deletions(-) create mode 100644 sample/GtkDemo/DemoAttribute.cs diff --git a/sample/GtkDemo/DemoApplicationWindow.cs b/sample/GtkDemo/DemoApplicationWindow.cs index 1ef4f781a..2720962d8 100644 --- a/sample/GtkDemo/DemoApplicationWindow.cs +++ b/sample/GtkDemo/DemoApplicationWindow.cs @@ -17,6 +17,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Application Window", "DemoApplicationWindow.cs")] public class DemoApplicationWindow : Window { // for the statusbar diff --git a/sample/GtkDemo/DemoAttribute.cs b/sample/GtkDemo/DemoAttribute.cs new file mode 100644 index 000000000..20a6997ff --- /dev/null +++ b/sample/GtkDemo/DemoAttribute.cs @@ -0,0 +1,36 @@ +using System; + +namespace GtkDemo +{ + [AttributeUsage (AttributeTargets.Class)] + public class DemoAttribute : Attribute + { + string label, filename, parent; + + public DemoAttribute (string label, string filename) : this (label, filename, null) + { + } + + public DemoAttribute (string label, string filename, string parent) + { + this.label = label; + this.filename = filename; + this.parent = parent; + } + + public string Filename { + get { return filename; } + } + + public string Label { + get { return label; } + } + + public string Parent { + get { + return parent; + } + } + } +} + diff --git a/sample/GtkDemo/DemoButtonBox.cs b/sample/GtkDemo/DemoButtonBox.cs index b39606366..4c7a02e4e 100644 --- a/sample/GtkDemo/DemoButtonBox.cs +++ b/sample/GtkDemo/DemoButtonBox.cs @@ -16,6 +16,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Button Boxes", "DemoButtonBox.cs")] public class DemoButtonBox : Gtk.Window { public DemoButtonBox () : base ("Button Boxes") diff --git a/sample/GtkDemo/DemoClipboard.cs b/sample/GtkDemo/DemoClipboard.cs index 0a847349b..e6aaf5e1d 100644 --- a/sample/GtkDemo/DemoClipboard.cs +++ b/sample/GtkDemo/DemoClipboard.cs @@ -3,6 +3,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Clipboard", "DemoClipboard.cs")] public class DemoClipboard : Gtk.Window { Entry pasteEntry, copyEntry; diff --git a/sample/GtkDemo/DemoColorSelection.cs b/sample/GtkDemo/DemoColorSelection.cs index 8a4ccd2b8..334907647 100644 --- a/sample/GtkDemo/DemoColorSelection.cs +++ b/sample/GtkDemo/DemoColorSelection.cs @@ -18,6 +18,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Color Selection", "DemoColorSelection.cs")] public class DemoColorSelection : Gtk.Window { private Gdk.Color color; diff --git a/sample/GtkDemo/DemoDialog.cs b/sample/GtkDemo/DemoDialog.cs index b59aae712..197c1db70 100644 --- a/sample/GtkDemo/DemoDialog.cs +++ b/sample/GtkDemo/DemoDialog.cs @@ -22,6 +22,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Dialog and Message Boxes", "DemoDialog.cs")] public class DemoDialog : Gtk.Window { private Entry entry1; diff --git a/sample/GtkDemo/DemoDrawingArea.cs b/sample/GtkDemo/DemoDrawingArea.cs index 90961c3eb..7cd632105 100644 --- a/sample/GtkDemo/DemoDrawingArea.cs +++ b/sample/GtkDemo/DemoDrawingArea.cs @@ -28,6 +28,7 @@ using Gdk; namespace GtkDemo { + [Demo ("Drawing Area", "DemoDrawingArea.cs")] public class DemoDrawingArea : Gtk.Window { private static Pixmap pixmap = null; diff --git a/sample/GtkDemo/DemoEditableCells.cs b/sample/GtkDemo/DemoEditableCells.cs index 6c7dadf0b..815eea1c2 100644 --- a/sample/GtkDemo/DemoEditableCells.cs +++ b/sample/GtkDemo/DemoEditableCells.cs @@ -20,6 +20,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Editable Cells", "DemoEditableCells.cs", "Tree View")] public class DemoEditableCells : Gtk.Window { private ListStore store; diff --git a/sample/GtkDemo/DemoEntryCompletion.cs b/sample/GtkDemo/DemoEntryCompletion.cs index d407c6c36..d23220d02 100644 --- a/sample/GtkDemo/DemoEntryCompletion.cs +++ b/sample/GtkDemo/DemoEntryCompletion.cs @@ -3,6 +3,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Entry Completion", "DemoEntryCompletion.cs")] public class DemoEntryCompletion : Dialog { public DemoEntryCompletion () : base ("Demo Entry Completion", null, DialogFlags.DestroyWithParent) diff --git a/sample/GtkDemo/DemoExpander.cs b/sample/GtkDemo/DemoExpander.cs index 2ef683c18..53ee0747b 100644 --- a/sample/GtkDemo/DemoExpander.cs +++ b/sample/GtkDemo/DemoExpander.cs @@ -3,6 +3,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Expander", "DemoExpander.cs")] public class DemoExpander : Gtk.Dialog { public DemoExpander () : base ("Demo Expander", null, DialogFlags.DestroyWithParent) diff --git a/sample/GtkDemo/DemoHyperText.cs b/sample/GtkDemo/DemoHyperText.cs index 2441d1371..d48cf2dbd 100644 --- a/sample/GtkDemo/DemoHyperText.cs +++ b/sample/GtkDemo/DemoHyperText.cs @@ -9,6 +9,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Hyper Text", "DemoHyperText.cs", "Text Widget")] public class DemoHyperText : Gtk.Window { bool hoveringOverLink = false; diff --git a/sample/GtkDemo/DemoImages.cs b/sample/GtkDemo/DemoImages.cs index 056431f7d..2c507b329 100644 --- a/sample/GtkDemo/DemoImages.cs +++ b/sample/GtkDemo/DemoImages.cs @@ -34,6 +34,7 @@ using Gdk; namespace GtkDemo { + [Demo ("Images", "DemoImages.cs")] public class DemoImages : Gtk.Window { private Gtk.Image progressiveImage; diff --git a/sample/GtkDemo/DemoListStore.cs b/sample/GtkDemo/DemoListStore.cs index 28280ac58..d914d7a98 100644 --- a/sample/GtkDemo/DemoListStore.cs +++ b/sample/GtkDemo/DemoListStore.cs @@ -21,6 +21,7 @@ using Gtk; namespace GtkDemo { + [Demo ("List Store", "DemoListStore.cs", "Tree View")] public class DemoListStore : Gtk.Window { ListStore store; diff --git a/sample/GtkDemo/DemoMain.cs b/sample/GtkDemo/DemoMain.cs index 9eb57a9a0..9f0d831b7 100644 --- a/sample/GtkDemo/DemoMain.cs +++ b/sample/GtkDemo/DemoMain.cs @@ -6,6 +6,7 @@ // Copyright (C) 2003, Ximian Inc. using System; +using System.Collections; using System.IO; using System.Reflection; @@ -154,139 +155,70 @@ namespace GtkDemo private TreeStore FillTree () { // title, filename, italic - store = new TreeStore (typeof (string), typeof (string), typeof (bool)); - TreeIter parent; + store = new TreeStore (typeof (string), typeof (System.Type), typeof (bool)); + Hashtable parents = new Hashtable (); + TreeIter parent; - store.AppendValues ("Application Window", "DemoApplicationWindow.cs", false); - store.AppendValues ("Button Boxes", "DemoButtonBox.cs", false); - store.AppendValues ("Change Display (0%)", "DemoChangeDisplay.cs", false); - store.AppendValues ("Clipboard", "DemoClipboard.cs", false); - store.AppendValues ("Color Selector", "DemoColorSelection.cs", false); - store.AppendValues ("Dialog and Message Boxes", "DemoDialog.cs", false); - store.AppendValues ("Drawing Area", "DemoDrawingArea.cs", false); - store.AppendValues ("Entry Completion", "DemoEntryCompletion.cs", false); - store.AppendValues ("Expander", "DemoExpander.cs", false); - store.AppendValues ("Images", "DemoImages.cs", false); - store.AppendValues ("Menus", "DemoMenus.cs", false); - store.AppendValues ("Paned Widget", "DemoPanes.cs", false); - store.AppendValues ("Pixbuf", "DemoPixbuf.cs", false); - store.AppendValues ("Size Groups", "DemoSizeGroup.cs", false); - store.AppendValues ("Stock Item and Icon Browser (10% complete)", "DemoStockBrowser.cs", false); - parent = store.AppendValues ("Text Widget"); - store.AppendValues (parent, "HyperText (50%)", "DemoHyperText.cs", false); - store.AppendValues (parent, "Multiple Views", "DemoTextView.cs", false); - parent = store.AppendValues ("Tree View"); - store.AppendValues (parent, "Editable Cells", "DemoEditableCells.cs", false); - store.AppendValues (parent, "List Store", "DemoListStore.cs", false); - store.AppendValues (parent, "Tree Store", "DemoTreeStore.cs", false); - store.AppendValues ("UIManager", "DemoUIManager.cs", false); - + Type[] types = Assembly.GetExecutingAssembly ().GetTypes (); + foreach (Type t in types) + { + if (t.IsDefined (typeof (DemoAttribute), false)) + { + object[] att = t.GetCustomAttributes (typeof (DemoAttribute), false); + foreach (DemoAttribute demo in att) + { + if (demo.Parent != null) + { + if (!parents.Contains (demo.Parent)) + parents.Add (demo.Parent, store.AppendValues (demo.Parent)); + + parent = (TreeIter) 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 OnTreeChanged (object o, EventArgs args) - { - TreeIter iter; - TreeModel model; - - if (treeView.Selection.GetSelected (out model, out iter)) { - string file = (string) model.GetValue (iter, 1); - if (file != null) - LoadFile (file); + TreeIter iter; + TreeModel model; - model.SetValue (iter, 2, true); - if (!oldSelection.Equals (TreeIter.Zero)) - model.SetValue (oldSelection, 2, false); - oldSelection = iter; - } + 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 OnRowActivated (object o, RowActivatedArgs args) { - switch (args.Path.ToString ()) { - case "0": - new DemoApplicationWindow (); - break; - case "1": - new DemoButtonBox (); - break; - case "2": - // - break; - case "3": - new DemoClipboard (); - break; - case "4": - new DemoColorSelection (); - break; - case "5": - new DemoDialog (); - break; - case "6": - new DemoDrawingArea (); - break; - case "7": - new DemoEntryCompletion (); - break; - case "8": - new DemoExpander (); - break; - case "9": - new DemoImages (); - break; - case "10": - new DemoMenus (); - break; - case "11": - new DemoPanes (); - break; - case "12": - new DemoPixbuf (); - break; - case "13": - new DemoSizeGroup (); - break; - case "14": - new DemoStockBrowser (); - break; - case "15": - ToggleRow (args.Path); - break; - case "15:0": - new DemoHyperText (); - break; - case "15:1": - new DemoTextView (); - break; - case "16": - ToggleRow (args.Path); - break; - case "16:0": - new DemoEditableCells (); - break; - case "16:1": - new DemoListStore (); - break; - case "16:2": - new DemoTreeStore (); - break; - case "17": - new DemoUIManager (); - break; - default: - break; - } - } + TreeIter iter; - void ToggleRow (TreePath path) - { - bool isExpanded = treeView.GetRowExpanded (path); - if (isExpanded) - treeView.CollapseRow (path); - else - treeView.ExpandRow (path, false); - } + 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) { diff --git a/sample/GtkDemo/DemoMenus.cs b/sample/GtkDemo/DemoMenus.cs index b1421a01f..bbd556aea 100644 --- a/sample/GtkDemo/DemoMenus.cs +++ b/sample/GtkDemo/DemoMenus.cs @@ -45,6 +45,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Menus", "DemoMenus.cs")] public class DemoMenus : Gtk.Window { public DemoMenus () : base ("Menus") diff --git a/sample/GtkDemo/DemoPanes.cs b/sample/GtkDemo/DemoPanes.cs index 96fe0cd29..937ae7c11 100644 --- a/sample/GtkDemo/DemoPanes.cs +++ b/sample/GtkDemo/DemoPanes.cs @@ -26,6 +26,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Paned Widget", "DemoPanes.cs")] public class DemoPanes : Gtk.Window { private VPaned vpaned; diff --git a/sample/GtkDemo/DemoPixbuf.cs b/sample/GtkDemo/DemoPixbuf.cs index 6f4644716..96d297ea8 100644 --- a/sample/GtkDemo/DemoPixbuf.cs +++ b/sample/GtkDemo/DemoPixbuf.cs @@ -27,7 +27,7 @@ using System; namespace GtkDemo { - + [Demo ("Pixbuf", "DemoPixbuf.cs")] public class DemoPixbuf : Gtk.Window { const int FrameDelay = 50; diff --git a/sample/GtkDemo/DemoSizeGroup.cs b/sample/GtkDemo/DemoSizeGroup.cs index 38297fad0..1e33838ad 100644 --- a/sample/GtkDemo/DemoSizeGroup.cs +++ b/sample/GtkDemo/DemoSizeGroup.cs @@ -26,6 +26,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Size Group", "DemoSizeGroup.cs")] public class DemoSizeGroup : Dialog { private SizeGroup sizeGroup; diff --git a/sample/GtkDemo/DemoStockBrowser.cs b/sample/GtkDemo/DemoStockBrowser.cs index 66a14d936..6232da4c5 100644 --- a/sample/GtkDemo/DemoStockBrowser.cs +++ b/sample/GtkDemo/DemoStockBrowser.cs @@ -11,6 +11,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Stock Item and Icon Browser", "DemoStockBrowser.cs")] public class DemoStockBrowser : Gtk.Window { class StockInfo diff --git a/sample/GtkDemo/DemoTextView.cs b/sample/GtkDemo/DemoTextView.cs index a141a0a6b..0544a787e 100644 --- a/sample/GtkDemo/DemoTextView.cs +++ b/sample/GtkDemo/DemoTextView.cs @@ -21,6 +21,7 @@ using Gtk; namespace GtkDemo { + [Demo ("Multiple Views", "DemoTextView.cs", "Text Widget")] public class DemoTextView : Gtk.Window { TextView view1; diff --git a/sample/GtkDemo/DemoTreeStore.cs b/sample/GtkDemo/DemoTreeStore.cs index 391debc1c..602357280 100644 --- a/sample/GtkDemo/DemoTreeStore.cs +++ b/sample/GtkDemo/DemoTreeStore.cs @@ -22,6 +22,7 @@ using GLib; namespace GtkDemo { + [Demo ("TreeStore", "DemoTreeStore.cs", "Tree View")] public class DemoTreeStore : Gtk.Window { private TreeStore store; diff --git a/sample/GtkDemo/DemoUIManager.cs b/sample/GtkDemo/DemoUIManager.cs index 3ceb6ea52..f49ea4e9a 100644 --- a/sample/GtkDemo/DemoUIManager.cs +++ b/sample/GtkDemo/DemoUIManager.cs @@ -3,6 +3,7 @@ using Gtk; namespace GtkDemo { + [Demo ("UIManager", "DemoUIManager.cs")] public class DemoUIManager : Window { VBox vbox; diff --git a/sample/GtkDemo/Makefile.am b/sample/GtkDemo/Makefile.am index b9b83cc42..dc4218b37 100644 --- a/sample/GtkDemo/Makefile.am +++ b/sample/GtkDemo/Makefile.am @@ -8,6 +8,7 @@ EXTRA_DIST = $(sources) $(image_names) sources = \ DemoApplicationWindow.cs \ + DemoAttribute.cs \ DemoButtonBox.cs \ DemoClipboard.cs \ DemoColorSelection.cs \ diff --git a/sample/GtkDemo/TODO b/sample/GtkDemo/TODO index 7c5902270..4487980aa 100644 --- a/sample/GtkDemo/TODO +++ b/sample/GtkDemo/TODO @@ -3,7 +3,6 @@ General DemoMain - syntax highlighting - - use reflection to fill the tree/launch demos DemoStockBrowser - underline _label properly