2009-10-20 Chris Toshok <toshok@ximian.com>
[moon.git] / examples / photo-browser / PhotoBrowser.boo
blob2a96fc15ca50460aacc6a126b7854ea026be4b69
1 # Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
3 # Contact:
4 # Moonlight List (moonlight-list@lists.ximian.com)
5 #
6 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation
8 # files (the "Software"), to deal in the Software without
9 # restriction, including without limitation the rights to use,
10 # copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the
12 # Software is furnished to do so, subject to the following
13 # conditions:
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 # OTHER DEALINGS IN THE SOFTWARE.
27 import System
28 import Gtk
29 import Gtk.Moonlight
30 import System.Windows
31 import System.Windows.Media
32 import System.Windows.Media.Animation
33 import System.Windows.Controls
34 import System.Windows.Shapes
35 import System.IO
37 class MyWindow(Window):
39 _canvas as Canvas
40 _currentId as int
41 _silver as GtkSilver
42 _setup as bool
44 def constructor():
45 _currentId = 1
46 _setup = true
48 super ("Photo browser")
49 DeleteEvent += OnDeleteEvent
50 ExposeEvent += OnExposeEvent
52 vbox = VBox (false, 12)
53 buttonBox = HButtonBox ()
54 button1 = Button ("Previous")
55 button2 = Button ("Next")
56 buttonBox.Add (button1)
57 buttonBox.Add (button2)
59 button2.Clicked += OnNextClicked
60 button1.Clicked += OnPrevClicked
62 _silver = GtkSilver (320, 220)
63 _canvas = Canvas ()
64 _silver.Attach (_canvas)
66 Add (vbox)
67 vbox.PackStart (_silver)
68 vbox.PackEnd (buttonBox)
69 _silver.SetSizeRequest (320, 240)
70 BorderWidth = 10
72 private def CreateRectangleAtPosition (fileName as string, position as double, name as string):
73 rectangle = Rectangle ()
74 rectangle.Width = 300
75 rectangle.Height = 200
76 rectangle.RadiusX = 20
77 rectangle.RadiusY = 20
78 rectangle.SetValue (DependencyObject.NameProperty, name)
80 solidBrush = SolidColorBrush ()
81 solidBrush.Color = Color.FromArgb (255, 255, 255, 255)
83 fillBrush = ImageBrush ()
85 rectangle.Fill = fillBrush
86 rectangle.Stroke = solidBrush
87 rectangle.StrokeThickness = 2.0
89 _canvas.Children.Add (rectangle)
90 _canvas.SetLeft (rectangle, position)
91 _canvas.SetTop (rectangle, 10.0)
92 fillBrush.ImageSource = Uri(fileName, UriKind.Relative)
94 rectangle.MouseEnter += OnHover
95 rectangle.MouseLeave += OnUnHover
97 private def CreateNewAnimationForName (name as string, pos as double):
98 sb = Storyboard ()
100 sb.SetValue (Storyboard.TargetNameProperty, name)
101 sb.SetValue (Storyboard.TargetPropertyProperty, "(Canvas.Left)")
102 animation = DoubleAnimationUsingKeyFrames ()
103 xaml = String.Format ('<SplineDoubleKeyFrame KeyTime="0:0:1" Value="{0}" KeySpline="0.5,0.0 0.5, 1.0" />', pos)
104 frame = _silver.CreateFromString (xaml, false)
105 animation.KeyFrames.Add (frame)
106 sb.Children.Add (animation)
107 _canvas.Resources.Add (sb)
108 sb.Begin ()
110 private def OnExposeEvent ():
111 if (_setup):
112 pos = 10
113 currentId = 1
114 for file in Directory.GetFiles ("./", "*.jpg"):
115 CreateRectangleAtPosition (file, pos, currentId.ToString ())
116 currentId += 1
117 pos = 400
119 _setup = false
121 private def OnDeleteEvent():
122 Gtk.Application.Quit ()
124 private def OnNextClicked ():
125 if (_currentId == 5):
126 return
128 if (_currentId > 0):
129 CreateNewAnimationForName (_currentId.ToString (), -400)
131 _currentId += 1
132 CreateNewAnimationForName (_currentId.ToString (), 10)
134 private def OnPrevClicked ():
135 if (_currentId == 1):
136 return
138 CreateNewAnimationForName (_currentId.ToString (), 400)
140 _currentId -= 1
141 CreateNewAnimationForName (_currentId.ToString (), 10)
143 private def OnHover (sender as Rectangle):
144 solid = sender.Stroke as SolidColorBrush
145 solid.Color = Color.FromArgb (255, 255, 0, 0)
147 private def OnUnHover (sender as Rectangle):
148 solid = sender.Stroke as SolidColorBrush
149 solid.Color = Color.FromArgb (255, 255, 255, 255)
152 Gtk.Application.Init ()
153 GtkSilver.Init ()
154 myWindow = MyWindow ()
155 myWindow.ShowAll ()
156 Gtk.Application.Run ()