~ubuntu-branches/ubuntu/natty/gnome-do/natty

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Interface/Docky.Interface.Painters/CalendarPainter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-03-22 22:44:39 UTC
  • mto: (1.1.7 upstream) (0.1.4 squeeze)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090322224439-ztmk4tbq19s3safs
ImportĀ upstreamĀ versionĀ 0.8.1.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 GNOME Do
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
using System.Collections.Generic;
 
20
using System.Globalization;
 
21
 
 
22
using Cairo;
 
23
using Gdk;
 
24
 
 
25
using Do.Interface;
 
26
 
 
27
using Docky.Core;
 
28
using Docky.Interface;
 
29
using Docky.Utilities;
 
30
 
 
31
namespace Docky.Interface.Painters
 
32
{
 
33
        
 
34
        
 
35
        public class CalendarPainter : AbstractIntegratedPainter
 
36
        {
 
37
                const int LineHeight = 16;
 
38
                const double lowlight = .35;
 
39
                const string BoldFormatString = "<b>{0}</b>";
 
40
                
 
41
                ClockDockItem Clock { get; set; }
 
42
                
 
43
                DateTime CalendarStartDate {
 
44
                        get {
 
45
                                return DateTime.Today.AddDays (0 - (int) DateTime.Today.DayOfWeek + 
 
46
                                                               ((int) DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek));
 
47
                        }
 
48
                }
 
49
                
 
50
                public CalendarPainter (ClockDockItem clock) : base (clock as AbstractDockItem)
 
51
                {
 
52
                        Clock = clock;
 
53
                }
 
54
                
 
55
                #region IDockPainter implementation 
 
56
                
 
57
                protected override int Width {
 
58
                        get {
 
59
                                return 670;
 
60
                        }
 
61
                }
 
62
 
 
63
                
 
64
                protected override void PaintArea (Cairo.Context cr, Gdk.Rectangle paintArea)
 
65
                {
 
66
                        int height = paintArea.Height / LineHeight;
 
67
                        RenderHeader (cr, paintArea);
 
68
                        for (int i = 1; i < height; i++)
 
69
                                RenderLine (cr, paintArea, i);
 
70
                }
 
71
                
 
72
                protected override void ReceiveClick (Gdk.Rectangle paintArea, Gdk.Point cursor)
 
73
                {
 
74
                        OnHideRequested ();
 
75
                }
 
76
                
 
77
                #endregion 
 
78
                
 
79
                void RenderHeader (Context cr, Gdk.Rectangle paintArea)
 
80
                {
 
81
                        int centerLine = paintArea.Y + LineHeight / 2 + ((paintArea.Height % LineHeight) / 2);
 
82
                        int offsetSize = paintArea.Width / 9;
 
83
                        
 
84
                        DateTime day = CalendarStartDate;
 
85
                        TextRenderContext textContext = new TextRenderContext (cr, string.Empty, offsetSize);
 
86
                        textContext.Alignment = Pango.Alignment.Center;
 
87
                        
 
88
                        cr.Color = new Cairo.Color (1, 1, 1, .5);
 
89
                        for (int i = 1; i < 8; i++) {
 
90
                                textContext.Text = string.Format (BoldFormatString, day.ToString ("ddd").ToUpper ());
 
91
                                textContext.LeftCenteredPoint = new Gdk.Point (paintArea.X + offsetSize * i, centerLine);
 
92
                                DockServices.DrawingService.TextPathAtPoint (textContext);
 
93
                                cr.Fill ();
 
94
                                day = day.AddDays (1);
 
95
                        }
 
96
                }
 
97
                
 
98
                void RenderLine (Context cr, Gdk.Rectangle paintArea, int line)
 
99
                {
 
100
                        DateTime lineStart = CalendarStartDate.AddDays ((line - 1) * 7);
 
101
                        int offsetSize = paintArea.Width / 9;
 
102
                        int centerLine = paintArea.Y + LineHeight / 2 + LineHeight * line + ((paintArea.Height % LineHeight) / 2);
 
103
                        
 
104
                        int dayOffset = 0;
 
105
                        TextRenderContext textContext = new TextRenderContext (cr, string.Empty, offsetSize);
 
106
                        for (int i = 0; i < 9; i++) {
 
107
                                if (i == 8) {
 
108
                                        cr.Color = new Cairo.Color (1, 1, 1, lowlight);
 
109
                                        textContext.Text = string.Format (BoldFormatString, lineStart.AddDays (6).ToString ("MMM").ToUpper ());
 
110
                                        textContext.Alignment = Pango.Alignment.Left;
 
111
                                } else if (i == 0) {
 
112
                                        cr.Color = new Cairo.Color (1, 1, 1, lowlight);
 
113
                                        int woy = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear (lineStart, 
 
114
                                                                                                     DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, 
 
115
                                                                                                     DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
 
116
                                        textContext.Text = string.Format ("<b>W{0:00}</b>", woy);
 
117
                                        textContext.Alignment = Pango.Alignment.Right;
 
118
                                } else {
 
119
                                        DateTime day = lineStart.AddDays (dayOffset);
 
120
                                        textContext.Alignment = Pango.Alignment.Center;
 
121
                                        
 
122
                                        if (day.Month == CalendarStartDate.Month)
 
123
                                                cr.Color = new Cairo.Color (1, 1, 1);
 
124
                                        else
 
125
                                                cr.Color = new Cairo.Color (1, 1, 1, .8);
 
126
                                        
 
127
                                        textContext.Text = string.Format ("{0:00}", day.Day);
 
128
                                        if (day.Date == DateTime.Today)
 
129
                                                textContext.Text = string.Format (BoldFormatString, textContext.Text);
 
130
                                        dayOffset++;
 
131
                                }
 
132
                                textContext.LeftCenteredPoint = new Gdk.Point (paintArea.X + offsetSize * i, centerLine);
 
133
                                DockServices.DrawingService.TextPathAtPoint (textContext);
 
134
                                cr.Fill ();
 
135
                                
 
136
                        }
 
137
                }
 
138
                
 
139
                public void Summon ()
 
140
                {
 
141
                        OnShowRequested ();
 
142
                }
 
143
                
 
144
                void RequestDraw ()
 
145
                {
 
146
                        OnPaintNeeded (new PaintNeededArgs ());
 
147
                } 
 
148
                
 
149
        }
 
150
}