New test.
[mono.git] / mcs / class / Mono.Cairo / snippets / SnippetsGtk.cs
1 using System;
2 using Cairo;
3 using Gtk;
4
5 namespace Cairo.Snippets
6 {
7         public class CairoSnippetsGtk
8         {
9                 int width = 400;
10                 int height = 200;
11
12                 DrawingArea da = new DrawingArea ();
13
14                 Snippets snips = new Snippets ();
15                 string selected = "";
16
17                 public static void Main(string[] args)
18                 {
19                         Application.Init ();
20                         new CairoSnippetsGtk ();
21                         Application.Run ();
22                 }
23
24                 public CairoSnippetsGtk ()
25                 {
26                         Window w = new Window ("Cairo snippets");
27                         w.SetDefaultSize (width, height);
28                         w.DeleteEvent += delegate { Application.Quit (); };
29
30                         HPaned hpane = new HPaned ();
31                         ScrolledWindow sw = new ScrolledWindow ();
32                         TreeView tv = new TreeView ();
33                         tv.HeadersVisible = false;
34                         tv.AppendColumn ("snippets", new CellRendererText (), "text", 0);
35                         tv.Model = GetModel ();
36                         tv.Selection.Changed += OnSelectionChanged;
37                         sw.Add (tv);
38                         hpane.Add1 (sw);
39                         da = new DrawingArea ();
40                         da.ExposeEvent += OnExposed;
41                         hpane.Add2 (da);
42                         hpane.Position = width / 2;
43                         w.Add (hpane);
44                         
45                         w.ShowAll ();
46                 }
47
48                 ListStore GetModel ()
49                 {
50                         ListStore store = new ListStore (typeof (string));
51                         foreach (string s in Snippets.snippets)
52                                 store.AppendValues (s);
53                         return store;
54                 }
55
56                 void OnExposed (object sender, ExposeEventArgs e)
57                 {
58                         Context cr = Gdk.CairoHelper.Create (da.GdkWindow);
59
60                         int w, h;
61                         da.GdkWindow.GetSize (out w, out h);
62
63                         // set window bg
64                         cr.ColorRgb = new Color (1, 1, 1 );
65                         cr.Rectangle (0, 0, w, h);
66                         cr.Fill ();
67                         // reset it
68                         cr.ColorRgb = new Color (0, 0, 0);
69
70                         Snippets.InvokeSnippet (snips, selected, cr, w, h);
71
72                         e.RetVal = true;
73                 }
74
75                 void OnSelectionChanged (object sender, EventArgs e)
76                 {
77                         TreeIter iter;
78                         TreeModel model;
79                         TreeSelection selection = sender as TreeSelection;
80
81                         if (selection.GetSelected (out model, out iter))
82                         {
83                                 selected = (string) model.GetValue (iter, 0);
84                         }
85
86                         da.QueueDraw ();
87                 }
88         }
89 }
90