~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to examples/desklet/simpleclock/clock.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System.Windows;
 
2
using System;
 
3
using System.Globalization;
 
4
using System.Windows.Controls;
 
5
using System.Windows.Media;
 
6
using System.Windows.Media.Animation;
 
7
using System.Windows.Shapes;
 
8
 
 
9
namespace Clock {
 
10
        
 
11
        public class Display : Canvas {
 
12
                TextBlock hour, minute, ampm;
 
13
                Storyboard to_config, to_clock;
 
14
                DateTime last = DateTime.MinValue;
 
15
                bool isAm, use24h;
 
16
                
 
17
                void UpdateTime ()
 
18
                {
 
19
                        DateTime dt = DateTime.Now;
 
20
                        
 
21
                        if (dt.Hour == last.Hour && dt.Minute == last.Minute)
 
22
                                return;
 
23
                        
 
24
                        if (dt.Hour != last.Hour) {
 
25
                                int h = dt.Hour;
 
26
                                if (!use24h) {
 
27
                                        h %= 12;
 
28
                                        if (h == 0)
 
29
                                                h = 12;
 
30
                                }
 
31
                                hour.Text = h.ToString ("00");
 
32
                        }
 
33
                        
 
34
                        minute.Text = dt.Minute.ToString ("00");
 
35
                        last = dt;
 
36
                        
 
37
                        if (!use24h) {
 
38
                                if (isAm && dt.Hour >= 12) {
 
39
                                        isAm = false;
 
40
                                        ampm.Text = "pm";
 
41
                                } else if (!isAm && dt.Hour < 12) {
 
42
                                        isAm = true;
 
43
                                        ampm.Text = "am";
 
44
                                }
 
45
                        }
 
46
                }
 
47
 
 
48
                bool in_config = false;
 
49
 
 
50
                void DoConfigTransition ()
 
51
                {
 
52
                        if (in_config){
 
53
                                in_config = false;
 
54
                                to_clock.Begin ();
 
55
                        } else {
 
56
                                in_config = true;
 
57
                                to_config.Begin ();
 
58
                        }
 
59
                }
 
60
                
 
61
                public void PageLoaded(object o, EventArgs e)
 
62
                {
 
63
                        bool visible = false;
 
64
                        
 
65
                        UIElement r = FindName ("dotcover") as UIElement;
 
66
                        Storyboard sb = FindName ("run") as Storyboard;
 
67
                        hour = FindName ("hour") as TextBlock;
 
68
                        minute = FindName ("minute") as TextBlock;
 
69
                        ampm = FindName ("ampm") as TextBlock;
 
70
                        Canvas config = FindName ("configcanvas") as Canvas;
 
71
                        Canvas config_button = FindName ("config_button") as Canvas;
 
72
                        to_config = FindName ("to_config") as Storyboard;
 
73
                        to_clock = FindName ("to_clock") as Storyboard;
 
74
                        
 
75
                        if (ampm != null) {
 
76
                                if (use24h)
 
77
                                        ampm.Visibility = Visibility.Collapsed;
 
78
                                else
 
79
                                        isAm = ampm.Text.ToLower () == "am";
 
80
                        }
 
81
 
 
82
                        
 
83
                        if (sb == null || r == null || hour == null || minute == null || config == null ||
 
84
                            to_config == null || to_clock == null){
 
85
                                Console.WriteLine ("Elements are missing from the xaml file\n");
 
86
                                return;
 
87
                        }
 
88
                        
 
89
                        DoubleAnimation timer = new DoubleAnimation ();
 
90
                        sb.Children.Add (timer);
 
91
                        timer.Duration = new Duration (TimeSpan.FromSeconds (0.5));
 
92
                        
 
93
                        sb.Completed += delegate {
 
94
                                visible = !visible;
 
95
                                UpdateTime ();
 
96
                                r.Opacity = visible ? 0.0 : 1.0;
 
97
                                sb.Begin ();
 
98
                        };
 
99
                        UpdateTime ();
 
100
 
 
101
                        bool in_config = false;
 
102
                        
 
103
                        config_button.MouseLeftButtonUp += delegate {
 
104
                                DoConfigTransition ();
 
105
                        };
 
106
 
 
107
                        config.MouseLeftButtonUp += delegate {
 
108
                                DoConfigTransition ();
 
109
                        };
 
110
        
 
111
                        sb.Begin ();
 
112
                        
 
113
                }
 
114
        }
 
115
}