~dobey/rhetorical/trunk

« back to all changes in this revision

Viewing changes to src/calview-month.c

  • Committer: rodney.dawes at canonical
  • Date: 2008-10-24 02:42:27 UTC
  • Revision ID: rodney.dawes@canonical.com-20081024024227-635l0ef9cge686qu
2008-10-23  Rodney Dawes  <dobey.pwns@gmail.com>

        * *: Import rhetorical into bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Authors: Rodney Dawes <dobey.pwns@gmail.com>
 
3
 *
 
4
 *  Copyright 2008 Rodney Dawes
 
5
 *
 
6
 */
 
7
 
 
8
#ifdef _HAVE_CONFIG_H_
 
9
#include "config.h"
 
10
#endif
 
11
 
 
12
#include "calview-month.h"
 
13
 
 
14
#include <glib/gi18n.h>
 
15
#include <time.h>
 
16
 
 
17
struct _CalViewMonthPrivate {
 
18
};
 
19
 
 
20
/* GObject methods */
 
21
static void cal_view_month_finalize (GObject * object);
 
22
 
 
23
/* CalViewModel methods */
 
24
static void cal_view_month_expose (CalViewModel * model,
 
25
                                   GtkWidget * widget,
 
26
                                   GdkEventExpose * event);
 
27
 
 
28
G_DEFINE_TYPE (CalViewMonth, cal_view_month, CAL_TYPE_VIEW_MODEL)
 
29
 
 
30
static void cal_view_month_class_init (CalViewMonthClass * klass) {
 
31
  GObjectClass * o_class;
 
32
  CalViewModelClass * parent_class;
 
33
 
 
34
  o_class = (GObjectClass *) klass;
 
35
  parent_class = (CalViewModelClass *) klass;
 
36
 
 
37
  /* GObject Methods */
 
38
  o_class->finalize = cal_view_month_finalize;
 
39
 
 
40
  /* CalViewModel methods */
 
41
  parent_class->expose_event = cal_view_month_expose;
 
42
}
 
43
 
 
44
static void cal_view_month_init (CalViewMonth * model) {
 
45
  CalViewMonthPrivate * priv;
 
46
 
 
47
  priv = g_new0 (CalViewMonthPrivate, 1);
 
48
  model->priv = priv;
 
49
}
 
50
 
 
51
static void cal_view_month_finalize (GObject * object) {
 
52
  CalViewMonth * model = CAL_VIEW_MONTH (object);
 
53
 
 
54
  if (model->priv)
 
55
    g_free (model->priv);
 
56
}
 
57
 
 
58
static void cal_view_month_expose (CalViewModel * model,
 
59
                                   GtkWidget * widget,
 
60
                                   GdkEventExpose * event) {
 
61
  cairo_t * context;
 
62
  cairo_text_extents_t text;
 
63
  int bw, bh;
 
64
  int i;
 
65
  time_t tnow;
 
66
  struct tm * tmnow;
 
67
  int daysinmonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
68
  int days;
 
69
  int week, tweek;
 
70
  int wday;
 
71
  int th;
 
72
  int tdw;
 
73
  gboolean use_abbr = FALSE;
 
74
 
 
75
  int wx, wy, ww, wh;
 
76
 
 
77
  tnow = time (NULL);
 
78
  tmnow = localtime (&tnow);
 
79
 
 
80
  wx = 0 + 1; //widget->allocation.x;
 
81
  wy = widget->allocation.y + 1;
 
82
  ww = widget->allocation.width - 2;
 
83
  wh = widget->allocation.height - 2;
 
84
 
 
85
  days = daysinmonth[tmnow->tm_mon];
 
86
  /* If February in leap year, add a day */
 
87
  if (tmnow->tm_mon == 1 && ((tmnow->tm_year + 1900) % 4) == 0)
 
88
    days = 29;
 
89
 
 
90
  context = gdk_cairo_create (widget->window);
 
91
 
 
92
  cairo_set_tolerance (context, 0.0f);
 
93
  cairo_set_operator (context, CAIRO_OPERATOR_OVER);
 
94
  cairo_set_line_width (context, 1.0f);
 
95
 
 
96
  cairo_rectangle (context, event->area.x, event->area.y,
 
97
                   event->area.width, event->area.height);
 
98
  cairo_clip (context);
 
99
 
 
100
  cairo_set_source_rgba (context, 1.0f, 1.0f, 1.0f, 1.0f);
 
101
  cairo_rectangle (context, wx, wy, ww, wh);
 
102
  cairo_fill (context);
 
103
 
 
104
  cairo_text_extents (context, "Wednesday", &text);
 
105
  th = text.height;
 
106
 
 
107
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.05f);
 
108
  cairo_rectangle (context, wx, wy, ww, th + 6.5);
 
109
  cairo_fill (context);
 
110
 
 
111
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.2f);
 
112
  cairo_move_to (context, wx, th + 6.5);
 
113
  cairo_line_to (context, wx + ww, th + 6.5);
 
114
  cairo_stroke (context);
 
115
 
 
116
  bw = widget->allocation.width / 7;
 
117
  bh = (widget->allocation.height - 6 - th) / 6;
 
118
 
 
119
  if (bw < text.width + 12)
 
120
    use_abbr = TRUE;
 
121
 
 
122
  for (i = 1; i < 8; i++) {
 
123
    if (i < 7) {
 
124
      cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.2f);
 
125
      cairo_move_to (context, (bw * i) + 0.5 + wx, th + 6);
 
126
      cairo_line_to (context, (bw * i) + 0.5 + wx, wh);
 
127
      cairo_stroke (context);
 
128
    }
 
129
 
 
130
    cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 1.0f);
 
131
    switch (i) {
 
132
    case 1:
 
133
      cairo_text_extents (context, use_abbr ? _("Sun") : _("Sunday"), &text);
 
134
      cairo_move_to (context, (bw / 2) - (text.width / 2) + 0.5 + wx,
 
135
                     (th + 6) / 2 + 2.5);
 
136
      cairo_show_text (context, use_abbr ? _("Sun") : _("Sunday"));
 
137
      break;
 
138
    case 2:
 
139
      cairo_text_extents (context, use_abbr ? _("Mon") : _("Monday"), &text);
 
140
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
141
                     (th + 6) / 2 + 2.5);
 
142
      cairo_show_text (context, use_abbr ? _("Mon") : _("Monday"));
 
143
      break;
 
144
    case 3:
 
145
      cairo_text_extents (context, use_abbr ? _("Tue") : _("Tuesday"), &text);
 
146
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
147
                     (th + 6) / 2 + 2.5);
 
148
      cairo_show_text (context, use_abbr ? _("Tue") : _("Tuesday"));
 
149
      break;
 
150
    case 4:
 
151
      cairo_text_extents (context, use_abbr ? _("Wed") : _("Wednesday"), &text);
 
152
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
153
                     (th + 6) / 2 + 2.5);
 
154
      cairo_show_text (context, use_abbr ? _("Wed") : _("Wednesday"));
 
155
      break;
 
156
    case 5:
 
157
      cairo_text_extents (context, use_abbr ? _("Thu") : _("Thursday"), &text);
 
158
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
159
                     (th + 6) / 2 + 2.5);
 
160
      cairo_show_text (context, use_abbr ? _("Thu") : _("Thursday"));
 
161
      break;
 
162
    case 6:
 
163
      cairo_text_extents (context, use_abbr ? _("Fri") : _("Friday"), &text);
 
164
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
165
                     (th + 6) / 2 + 2.5);
 
166
      cairo_show_text (context, use_abbr ? _("Fri") : _("Friday"));
 
167
      break;
 
168
    case 7:
 
169
      cairo_text_extents (context, use_abbr ? _("Sat") : _("Saturday"), &text);
 
170
      cairo_move_to (context, bw * (i - 1) + (bw / 2) - (text.width / 2) + 0.5 + wx,
 
171
                     (th + 6) / 2 + 2.5);
 
172
      cairo_show_text (context, use_abbr ? _("Sat") : _("Saturday"));
 
173
      break;
 
174
    }
 
175
  }
 
176
 
 
177
  for (i = 1; i < 7; i++) {
 
178
    if (i < 6) {
 
179
      cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.2f);
 
180
      cairo_move_to (context, wx, (bh * i) + (th + 6) + 0.5);
 
181
      cairo_line_to (context, wx + ww, (bh * i) + (th + 6) + 0.5);
 
182
      cairo_stroke (context);
 
183
    }
 
184
    cairo_set_source_rgba (context, 0.0f, 0.0f, 1.0f, 0.1f);
 
185
    cairo_move_to (context, wx, (bh * (i - 1)) + (th + 6) + 0.5);
 
186
    cairo_rectangle (context, wx, (bh * (i - 1)) + (th + 6) + 0.5,
 
187
                     ww, th + 6 + 6.5);
 
188
    cairo_fill (context);
 
189
  }
 
190
 
 
191
  tweek = 0;
 
192
  wday = tmnow->tm_wday;
 
193
 
 
194
  for (i = tmnow->tm_mday; i > 0; i--) {
 
195
    wday--;
 
196
    if (wday < 0) {
 
197
      wday = 6;
 
198
      tweek++;
 
199
    }
 
200
  }
 
201
 
 
202
  if (tmnow->tm_wday == 6)
 
203
    tdw = ww - (6 * bw);
 
204
  else
 
205
    tdw = bw;
 
206
 
 
207
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.215f);
 
208
  cairo_rectangle (context,
 
209
                   tmnow->tm_wday * bw + wx,
 
210
                   (th + 6) + (bh * tweek),
 
211
                   tdw, bh);
 
212
  cairo_fill (context);
 
213
 
 
214
  week = tweek;
 
215
  wday = tmnow->tm_wday;
 
216
 
 
217
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 1.0f);
 
218
  for (i = tmnow->tm_mday; i > 0; i--) {
 
219
    char * label;
 
220
 
 
221
    label = g_strdup_printf ("%d", i);
 
222
    cairo_text_extents (context, label, &text);
 
223
    cairo_move_to (context, bw * wday + (bw - text.width - 6) + wx,
 
224
                   (th + 16) + (bh * week) + (th  + 6) / 2 + 0.5);
 
225
    cairo_show_text (context, label);
 
226
    g_free (label);
 
227
 
 
228
    wday--;
 
229
    if (wday < 0) {
 
230
      wday = 6;
 
231
      week--;
 
232
    }
 
233
    if (week < 0)
 
234
      break;
 
235
  }
 
236
  if (wday != 0 || week == 0) {
 
237
    cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.08f);
 
238
    cairo_rectangle (context, wx, (th + 6.5),
 
239
                     bw * (wday + 1), bh);
 
240
    cairo_fill (context);
 
241
  }
 
242
 
 
243
  week = tweek;
 
244
  wday = tmnow->tm_wday + 1;
 
245
 
 
246
  if (wday > 6) {
 
247
    wday = 0;
 
248
    week++;
 
249
  }
 
250
 
 
251
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 1.0f);
 
252
  for (i = tmnow->tm_mday + 1; i <= days; i++) {
 
253
    char * label;
 
254
 
 
255
    label = g_strdup_printf ("%d", i);
 
256
    cairo_text_extents (context, label, &text);
 
257
    cairo_move_to (context, bw * wday + (bw - text.width - 6) + wx,
 
258
                   (th + 16) + (bh * week) + (th  + 6) / 2 + 0.5);
 
259
    cairo_show_text (context, label);
 
260
    g_free (label);
 
261
 
 
262
    wday++;
 
263
    if (wday > 6) {
 
264
      wday = 0;
 
265
      week++;
 
266
    }
 
267
    if (week > 6)
 
268
      break;
 
269
  }
 
270
 
 
271
  cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.08f);
 
272
  if (wday <= 6) {
 
273
    cairo_set_source_rgba (context, 0.0f, 0.0f, 0.0f, 0.08f);
 
274
    cairo_rectangle (context, bw * wday + wx,
 
275
                     (th + 6) + bh * week,
 
276
                     ww, bh);
 
277
    cairo_fill (context);
 
278
  }
 
279
  if (week < 5) {
 
280
    cairo_rectangle (context, wx, (th + 6) + bh * 5,
 
281
                     ww, bh);
 
282
    cairo_fill (context);
 
283
  }
 
284
 
 
285
  cairo_destroy (context);
 
286
 
 
287
  /* Just Temporary - Will get a shadow from the ScrolledWindow instead */
 
288
  gtk_paint_shadow (widget->style, widget->window, GTK_STATE_NORMAL,
 
289
                    GTK_SHADOW_IN, &event->area, widget, "calview",
 
290
                    0, 0, widget->allocation.width, widget->allocation.height);
 
291
}
 
292
 
 
293
/* Public methods */
 
294
CalViewModel * cal_view_month_new (void) {
 
295
  return CAL_VIEW_MODEL (g_object_new (CAL_TYPE_VIEW_MONTH, NULL));
 
296
}