~ted/ubuntu/lucid/tomboy/with-patch

« back to all changes in this revision

Viewing changes to Tomboy/Addins/Tasks/DateButton.cs

Tags: upstream-0.7.2
Import upstream version 0.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *  DateButton.cs
 
3
 *
 
4
 *  Copyright (C) 2005 Novell
 
5
 *  Written by Aaron Bockover (aaron@aaronbock.net)
 
6
 ****************************************************************************/
 
7
 
 
8
/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
 
9
 *
 
10
 *  Permission is hereby granted, free of charge, to any person obtaining a
 
11
 *  copy of this software and associated documentation files (the "Software"),  
 
12
 *  to deal in the Software without restriction, including without limitation  
 
13
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
 
14
 *  and/or sell copies of the Software, and to permit persons to whom the  
 
15
 *  Software is furnished to do so, subject to the following conditions:
 
16
 *
 
17
 *  The above copyright notice and this permission notice shall be included in 
 
18
 *  all copies or substantial portions of the Software.
 
19
 *
 
20
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 
21
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 
22
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 
23
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 
24
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 
25
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
26
 *  DEALINGS IN THE SOFTWARE.
 
27
 */
 
28
 
 
29
using System;
 
30
using GLib;
 
31
using Gtk;
 
32
 
 
33
namespace Gtk.Extras
 
34
{
 
35
        public class DateButton : ToggleButton
 
36
        {
 
37
                private Window popup;
 
38
                private DateTime date;
 
39
                private Calendar cal;
 
40
                private bool show_time;
 
41
        
 
42
                private const uint CURRENT_TIME = 0;
 
43
                
 
44
                // FIXME: If this is ever moved to its own library
 
45
                // this reference to Tomboy will obviously have to
 
46
                // be removed.
 
47
                public DateButton(DateTime date_time, bool show_time)
 
48
                        : base(Tomboy.GuiUtils.GetPrettyPrintDate (date_time, show_time))
 
49
                {
 
50
                        Toggled += OnToggled;
 
51
                        popup = null;
 
52
                        date = date_time;
 
53
                        this.show_time = show_time;
 
54
                }
 
55
                
 
56
                private void ShowCalendar()
 
57
                {
 
58
                        popup = new Window(WindowType.Popup);
 
59
                        popup.Screen = this.Screen;
 
60
 
 
61
                        Frame frame = new Frame();
 
62
                        frame.Shadow = ShadowType.Out;
 
63
                        frame.Show();
 
64
 
 
65
                        popup.Add(frame);
 
66
 
 
67
                        VBox box = new VBox(false, 0);
 
68
                        box.Show();
 
69
                        frame.Add(box);
 
70
                        
 
71
                        cal = new Calendar();
 
72
                        cal.DisplayOptions = CalendarDisplayOptions.ShowHeading
 
73
                                | CalendarDisplayOptions.ShowDayNames 
 
74
                                | CalendarDisplayOptions.ShowWeekNumbers;
 
75
                                
 
76
                        cal.KeyPressEvent += OnCalendarKeyPressed;
 
77
                        popup.ButtonPressEvent += OnButtonPressed;
 
78
                        
 
79
                        cal.Show();
 
80
                        
 
81
                        Alignment calAlignment = new Alignment(0.0f, 0.0f, 1.0f, 1.0f);
 
82
                        calAlignment.Show();
 
83
                        calAlignment.SetPadding(4, 4, 4, 4);
 
84
                        calAlignment.Add(cal);
 
85
                
 
86
                        box.PackStart(calAlignment, false, false, 0);
 
87
                                
 
88
                        Requisition req = SizeRequest();
 
89
                        int x = 0, y = 0;
 
90
                        GdkWindow.GetOrigin(out x, out y);
 
91
                        popup.Move(x + Allocation.X, y + Allocation.Y + req.Height + 3);
 
92
                        popup.Show();
 
93
                        popup.GrabFocus();
 
94
                                
 
95
                        Grab.Add(popup);
 
96
 
 
97
                        Gdk.GrabStatus grabbed = Gdk.Pointer.Grab(popup.GdkWindow, true, 
 
98
                                Gdk.EventMask.ButtonPressMask 
 
99
                                | Gdk.EventMask.ButtonReleaseMask 
 
100
                                | Gdk.EventMask.PointerMotionMask, null, null, CURRENT_TIME);
 
101
 
 
102
                        if(grabbed == Gdk.GrabStatus.Success) {
 
103
                                grabbed = Gdk.Keyboard.Grab(popup.GdkWindow, 
 
104
                                        true, CURRENT_TIME);
 
105
 
 
106
                                if(grabbed != Gdk.GrabStatus.Success) {
 
107
                                        Grab.Remove(popup);
 
108
                                        popup.Destroy();
 
109
                                        popup = null;
 
110
                                }
 
111
                        } else {
 
112
                                Grab.Remove(popup);
 
113
                                popup.Destroy();
 
114
                                popup = null;
 
115
                        }
 
116
                                
 
117
                cal.DaySelectedDoubleClick += OnCalendarDaySelected;
 
118
                        cal.ButtonPressEvent += OnCalendarButtonPressed;
 
119
 
 
120
                        cal.Date = date;
 
121
                }
 
122
                
 
123
                public void HideCalendar(bool update)
 
124
                {
 
125
                        if(popup != null) {
 
126
                                Grab.Remove(popup);
 
127
                                Gdk.Pointer.Ungrab(CURRENT_TIME);
 
128
                                Gdk.Keyboard.Ungrab(CURRENT_TIME);
 
129
 
 
130
                                popup.Destroy();
 
131
                                popup = null;
 
132
                        }
 
133
 
 
134
                        if(update) {
 
135
                                date = cal.GetDate();
 
136
                                // FIXME: If this is ever moved to its own library
 
137
                                // this reference to Tomboy will obviously have to
 
138
                                // be removed.
 
139
                                Label = Tomboy.GuiUtils.GetPrettyPrintDate (date, show_time);
 
140
                        }
 
141
 
 
142
                        Active = false;
 
143
                }
 
144
                
 
145
                private void OnToggled(object o, EventArgs args)
 
146
                {
 
147
                        if(Active) {
 
148
                                ShowCalendar();
 
149
                        } else {
 
150
                                HideCalendar(false);
 
151
                        }
 
152
                }
 
153
                
 
154
                private void OnButtonPressed(object o, ButtonPressEventArgs args)
 
155
                {
 
156
                        if(popup != null)
 
157
                                HideCalendar(false);
 
158
                }
 
159
                
 
160
                private void OnCalendarDaySelected(object o, EventArgs args)
 
161
                {
 
162
                        HideCalendar(true);
 
163
                }
 
164
                
 
165
                private void OnCalendarButtonPressed(object o, 
 
166
                        ButtonPressEventArgs args)
 
167
                {
 
168
                        args.RetVal = true;
 
169
                }
 
170
        
 
171
                private void OnCalendarKeyPressed(object o, KeyPressEventArgs args)
 
172
                {
 
173
                        switch(args.Event.Key) {
 
174
                                case Gdk.Key.Escape:
 
175
                                        HideCalendar(false);
 
176
                                        break;
 
177
                                case Gdk.Key.KP_Enter:
 
178
                                case Gdk.Key.ISO_Enter:
 
179
                                case Gdk.Key.Key_3270_Enter:
 
180
                                case Gdk.Key.Return:
 
181
                                case Gdk.Key.space:
 
182
                                case Gdk.Key.KP_Space:
 
183
                                        HideCalendar(true);
 
184
                                        break;
 
185
                                default:
 
186
                                        break;
 
187
                        }
 
188
                }
 
189
                
 
190
                public DateTime Date
 
191
                {
 
192
                        get { return date; }
 
193
                        set {
 
194
                                date = value;
 
195
                                Label = Tomboy.GuiUtils.GetPrettyPrintDate (date, show_time);
 
196
                        }
 
197
                }
 
198
                
 
199
                /// <summary>
 
200
                /// If true, both the date and time will be shown.  If false, the time
 
201
                /// will be omitted.
 
202
                /// </summary>
 
203
                public bool ShowTime
 
204
                {
 
205
                        get { return show_time; }
 
206
                        set { show_time = value; }
 
207
                }
 
208
        }
 
209
}