viscal

cairo/gtk vi-like timeblocking calendar
git clone git://jb55.com/viscal
Log | Files | Refs | README | LICENSE

commit 7f7e339c2a087c685b841fd572eb91a91e48a227
parent 4f6087d37cb22082261d3ab5c170c19a92a107ed
Author: William Casarin <bill@casarin.me>
Date:   Fri, 13 Jan 2017 08:37:56 -0800

Initial commit

Diffstat:
MEvent.vala | 2++
Mcalendar.vala | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
2 files changed, 69 insertions(+), 14 deletions(-)

diff --git a/Event.vala b/Event.vala @@ -1,4 +1,6 @@ public class Event { public DateTime start; + public DateTime end; + public string title; } diff --git a/calendar.vala b/calendar.vala @@ -5,7 +5,11 @@ using Cairo; public class Calendar : Gtk.Window { private const int SIZE = 30; - private const int GAP = 20; + private const double MIN_EVENT_HEIGHT = 10.0; + private const int GAP = 0; + private const int LMARGIN = 40; + private const int TXTPAD = 11; + private const int EVPAD = 2; private const double dashed[] = {1.0}; public Calendar () { @@ -21,25 +25,68 @@ public class Calendar : Gtk.Window { add (drawing_area); } - private void draw_event (Context ctx, Event ev) { + private double time_location (DateTime d) { + int doy = d.get_day_of_year(); + int hour = d.get_hour(); + int minute = d.get_minute(); + int second = d.get_second(); + + int seconds = + (hour * 60 * 60) + (minute * 60) + second; + + return ((double)seconds / 86400.0); + } + + private void draw_event (Context ctx, int sx, int sy, Event ev, int width, int height) { + double sloc = time_location(ev.start); + double eloc = time_location(ev.end); + double dloc = eloc - sloc; + double eheight = dloc * height; + + // double height = Math.fmin(, MIN_EVENT_HEIGHT); + // stdout.printf("sloc %f eloc %f dloc %f eheight %f\n", + // sloc, eloc, dloc, eheight); + + ctx.move_to(sx, (sloc * height)+sy); + ctx.set_source_rgba(1.0, 0, 0, 0.25); + rectangle(ctx, (double)width, eheight); + ctx.fill(); + ctx.set_source_rgb(0.5, 0, 0); + ctx.stroke(); + ctx.move_to(sx + EVPAD, (sloc * height)+EVPAD+TXTPAD); + ctx.show_text(ev.title); } - private void draw_hours (Context ctx, int width, int height) { - double hour_height = ((double)height) / 24.0; - const double col = 0.35; + private void draw_hours (Context ctx, int sy, int width, int height) { + double section_height = ((double)height) / 48.0; + char[] buffer; + const double col = 0.4; ctx.set_source_rgb (col, col, col); ctx.set_line_width (1); - for (int hour = 1; hour < 24; hour++) { - double y = ((double)hour) * hour_height; - ctx.move_to (GAP, y+GAP); + + for (int section = 0; section < 48; section++) { + int minutes = section * 30; + double y = sy + ((double)section) * section_height; + ctx.move_to (0, y); ctx.rel_line_to (width, 0); - if (hour % 2 == 0) - ctx.set_dash (dashed, 0); - else + if (section % 2 == 0) ctx.set_dash ({}, 0); + else + ctx.set_dash (dashed, 0); ctx.stroke(); + + bool onhour = ((minutes / 30) % 2) == 0; + if (onhour) { + var t = Time(); + t.minute = 0; + t.hour = minutes / 60; + var strtime = t.format("%H:%M"); + // TODO: text extents for proper time placement? + ctx.move_to(1.0, y+TXTPAD); + ctx.show_text(strtime); + } } } @@ -51,14 +98,20 @@ public class Calendar : Gtk.Window { private bool on_draw (Widget da, Context ctx) { int width, height; + var ev = new Event(); + ev.title = "Coding this"; + ev.start = new DateTime.now_local(); + ev.end = new DateTime.now_local().add_hours(1); this.get_size(out width, out height); - width -= GAP*2; height -= GAP*2; + width -= LMARGIN+GAP; height -= GAP*2; - ctx.move_to (GAP, GAP); + ctx.move_to(LMARGIN, GAP); draw_background(ctx, width, height); // ctx.move_to (GAP, GAP); - draw_hours(ctx, width, height); + draw_hours(ctx, GAP, width+LMARGIN, height); + + draw_event(ctx, LMARGIN, GAP, ev, width, height); return true; }